(try_window_reusing_current_matrix) <scrolling up>:
[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
e1477f43
GM
491/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
492
493Lisp_Object Qinhibit_menubar_update;
494int inhibit_menubar_update;
495
9142dd5b
GM
496/* Maximum height for resizing mini-windows. Either a float
497 specifying a fraction of the available height, or an integer
498 specifying a number of lines. */
c6e89d6c 499
ad4f174e
GM
500Lisp_Object Vmax_mini_window_height;
501
502/* Non-zero means messages should be displayed with truncated
503 lines instead of being continued. */
504
505int message_truncate_lines;
506Lisp_Object Qmessage_truncate_lines;
c6e89d6c 507
d6d26ed3
GM
508/* Non-zero means we want a hollow cursor in windows that are not
509 selected. Zero means there's no cursor in such windows. */
510
511int cursor_in_non_selected_windows;
512
5f5c8ee5
GM
513/* A scratch glyph row with contents used for generating truncation
514 glyphs. Also used in direct_output_for_insert. */
12adba34 515
5f5c8ee5
GM
516#define MAX_SCRATCH_GLYPHS 100
517struct glyph_row scratch_glyph_row;
518static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
1adc55de 519
5f5c8ee5
GM
520/* Ascent and height of the last line processed by move_it_to. */
521
522static int last_max_ascent, last_height;
523
21fdfb65
GM
524/* Non-zero if there's a help-echo in the echo area. */
525
526int help_echo_showing_p;
527
04612a64
GM
528/* If >= 0, computed, exact values of mode-line and header-line height
529 to use in the macros CURRENT_MODE_LINE_HEIGHT and
530 CURRENT_HEADER_LINE_HEIGHT. */
531
532int current_mode_line_height, current_header_line_height;
533
5f5c8ee5
GM
534/* The maximum distance to look ahead for text properties. Values
535 that are too small let us call compute_char_face and similar
536 functions too often which is expensive. Values that are too large
537 let us call compute_char_face and alike too often because we
538 might not be interested in text properties that far away. */
539
540#define TEXT_PROP_DISTANCE_LIMIT 100
541
47589c8c
GM
542#if GLYPH_DEBUG
543
5f5c8ee5
GM
544/* Non-zero means print traces of redisplay if compiled with
545 GLYPH_DEBUG != 0. */
546
5f5c8ee5 547int trace_redisplay_p;
47589c8c 548
546a4f00 549#endif /* GLYPH_DEBUG */
47589c8c 550
546a4f00
AI
551#ifdef DEBUG_TRACE_MOVE
552/* Non-zero means trace with TRACE_MOVE to stderr. */
47589c8c
GM
553int trace_move;
554
47589c8c
GM
555#define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
556#else
91c3f500 557#define TRACE_MOVE(x) (void) 0
5f5c8ee5 558#endif
546a4f00 559
d475bcb8
GM
560/* Non-zero means automatically scroll windows horizontally to make
561 point visible. */
562
563int automatic_hscrolling_p;
564
e00daaa0
GM
565/* A list of symbols, one for each supported image type. */
566
567Lisp_Object Vimage_types;
568
6422c1d7 569/* The variable `resize-mini-windows'. If nil, don't resize
67526daf 570 mini-windows. If t, always resize them to fit the text they
6422c1d7
GM
571 display. If `grow-only', let mini-windows grow only until they
572 become empty. */
573
574Lisp_Object Vresize_mini_windows;
575
5f5c8ee5
GM
576/* Value returned from text property handlers (see below). */
577
578enum prop_handled
3c6595e0 579{
5f5c8ee5
GM
580 HANDLED_NORMALLY,
581 HANDLED_RECOMPUTE_PROPS,
582 HANDLED_OVERLAY_STRING_CONSUMED,
583 HANDLED_RETURN
584};
3c6595e0 585
5f5c8ee5
GM
586/* A description of text properties that redisplay is interested
587 in. */
3c6595e0 588
5f5c8ee5
GM
589struct props
590{
591 /* The name of the property. */
592 Lisp_Object *name;
90adcf20 593
5f5c8ee5
GM
594 /* A unique index for the property. */
595 enum prop_idx idx;
596
597 /* A handler function called to set up iterator IT from the property
598 at IT's current position. Value is used to steer handle_stop. */
599 enum prop_handled (*handler) P_ ((struct it *it));
600};
601
602static enum prop_handled handle_face_prop P_ ((struct it *));
603static enum prop_handled handle_invisible_prop P_ ((struct it *));
604static enum prop_handled handle_display_prop P_ ((struct it *));
260a86a0 605static enum prop_handled handle_composition_prop P_ ((struct it *));
5f5c8ee5
GM
606static enum prop_handled handle_overlay_change P_ ((struct it *));
607static enum prop_handled handle_fontified_prop P_ ((struct it *));
608
609/* Properties handled by iterators. */
610
611static struct props it_props[] =
5992c4f7 612{
5f5c8ee5
GM
613 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
614 /* Handle `face' before `display' because some sub-properties of
615 `display' need to know the face. */
616 {&Qface, FACE_PROP_IDX, handle_face_prop},
617 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
618 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
260a86a0 619 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
5f5c8ee5
GM
620 {NULL, 0, NULL}
621};
5992c4f7 622
5f5c8ee5
GM
623/* Value is the position described by X. If X is a marker, value is
624 the marker_position of X. Otherwise, value is X. */
12adba34 625
5f5c8ee5 626#define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
12adba34 627
5f5c8ee5 628/* Enumeration returned by some move_it_.* functions internally. */
12adba34 629
5f5c8ee5
GM
630enum move_it_result
631{
632 /* Not used. Undefined value. */
633 MOVE_UNDEFINED,
bab29e15 634
5f5c8ee5
GM
635 /* Move ended at the requested buffer position or ZV. */
636 MOVE_POS_MATCH_OR_ZV,
bab29e15 637
5f5c8ee5
GM
638 /* Move ended at the requested X pixel position. */
639 MOVE_X_REACHED,
12adba34 640
5f5c8ee5
GM
641 /* Move within a line ended at the end of a line that must be
642 continued. */
643 MOVE_LINE_CONTINUED,
644
645 /* Move within a line ended at the end of a line that would
646 be displayed truncated. */
647 MOVE_LINE_TRUNCATED,
ff6c30e5 648
5f5c8ee5
GM
649 /* Move within a line ended at a line end. */
650 MOVE_NEWLINE_OR_CR
651};
12adba34 652
ff6c30e5 653
5f5c8ee5
GM
654\f
655/* Function prototypes. */
656
43c09969 657static void mark_window_display_accurate_1 P_ ((struct window *, int));
74bd6d65
GM
658static int single_display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
659static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
cafafe0b 660static int cursor_row_p P_ ((struct window *, struct glyph_row *));
715e84c9 661static int redisplay_mode_lines P_ ((Lisp_Object, int));
2e621225
GM
662static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
663static int invisible_text_between_p P_ ((struct it *, int, int));
664static int next_element_from_ellipsis P_ ((struct it *));
665static void pint2str P_ ((char *, int, int));
666static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
667 struct text_pos));
668static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
669static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
670static void store_frame_title_char P_ ((char));
671static int store_frame_title P_ ((unsigned char *, int, int));
672static void x_consider_frame_title P_ ((Lisp_Object));
673static void handle_stop P_ ((struct it *));
674static int tool_bar_lines_needed P_ ((struct frame *));
06568bbf 675static int single_display_prop_intangible_p P_ ((Lisp_Object));
5bcfeb49 676static void ensure_echo_area_buffers P_ ((void));
e037b9ec
GM
677static struct glyph_row *row_containing_pos P_ ((struct window *, int,
678 struct glyph_row *,
679 struct glyph_row *));
c6e89d6c
GM
680static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
681static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
23a96c77 682static int with_echo_area_buffer P_ ((struct window *, int,
23dd2d97
KR
683 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
684 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 685static void clear_garbaged_frames P_ ((void));
23dd2d97
KR
686static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
687static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
688static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
c6e89d6c 689static int display_echo_area P_ ((struct window *));
23dd2d97
KR
690static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
691static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
28514cd9 692static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
4fdb80f2 693static int string_char_and_length P_ ((unsigned char *, int, int *));
5f5c8ee5
GM
694static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
695 struct text_pos));
696static int compute_window_start_on_continuation_line P_ ((struct window *));
116d6f5c 697static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
5f5c8ee5
GM
698static void insert_left_trunc_glyphs P_ ((struct it *));
699static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *));
700static void extend_face_to_end_of_line P_ ((struct it *));
80c6cb1f 701static int append_space P_ ((struct it *, int));
5f5c8ee5
GM
702static void make_cursor_line_fully_visible P_ ((struct window *));
703static int try_scrolling P_ ((Lisp_Object, int, int, int, int));
47589c8c 704static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
5f5c8ee5
GM
705static int trailing_whitespace_p P_ ((int));
706static int message_log_check_duplicate P_ ((int, int, int, int));
707int invisible_p P_ ((Lisp_Object, Lisp_Object));
708int invisible_ellipsis_p P_ ((Lisp_Object, Lisp_Object));
709static void push_it P_ ((struct it *));
710static void pop_it P_ ((struct it *));
711static void sync_frame_with_window_matrix_rows P_ ((struct window *));
712static void redisplay_internal P_ ((int));
c6e89d6c 713static int echo_area_display P_ ((int));
5f5c8ee5
GM
714static void redisplay_windows P_ ((Lisp_Object));
715static void redisplay_window P_ ((Lisp_Object, int));
716static void update_menu_bar P_ ((struct frame *, int));
717static int try_window_reusing_current_matrix P_ ((struct window *));
718static int try_window_id P_ ((struct window *));
719static int display_line P_ ((struct it *));
715e84c9 720static int display_mode_lines P_ ((struct window *));
04612a64 721static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
5f5c8ee5 722static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object));
68c45bf0 723static char *decode_mode_spec P_ ((struct window *, int, int, int));
5f5c8ee5
GM
724static void display_menu_bar P_ ((struct window *));
725static int display_count_lines P_ ((int, int, int, int, int *));
726static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
727 int, int, struct it *, int, int, int, int));
728static void compute_line_metrics P_ ((struct it *));
729static void run_redisplay_end_trigger_hook P_ ((struct it *));
730static int get_overlay_strings P_ ((struct it *));
731static void next_overlay_string P_ ((struct it *));
5f5c8ee5
GM
732static void reseat P_ ((struct it *, struct text_pos, int));
733static void reseat_1 P_ ((struct it *, struct text_pos, int));
734static void back_to_previous_visible_line_start P_ ((struct it *));
735static void reseat_at_previous_visible_line_start P_ ((struct it *));
312246d1 736static void reseat_at_next_visible_line_start P_ ((struct it *, int));
5f5c8ee5
GM
737static int next_element_from_display_vector P_ ((struct it *));
738static int next_element_from_string P_ ((struct it *));
739static int next_element_from_c_string P_ ((struct it *));
740static int next_element_from_buffer P_ ((struct it *));
260a86a0 741static int next_element_from_composition P_ ((struct it *));
5f5c8ee5
GM
742static int next_element_from_image P_ ((struct it *));
743static int next_element_from_stretch P_ ((struct it *));
744static void load_overlay_strings P_ ((struct it *));
745static void init_from_display_pos P_ ((struct it *, struct window *,
746 struct display_pos *));
747static void reseat_to_string P_ ((struct it *, unsigned char *,
748 Lisp_Object, int, int, int, int));
5f5c8ee5
GM
749static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
750 int, int, int));
751void move_it_vertically_backward P_ ((struct it *, int));
752static void init_to_row_start P_ ((struct it *, struct window *,
753 struct glyph_row *));
754static void init_to_row_end P_ ((struct it *, struct window *,
755 struct glyph_row *));
756static void back_to_previous_line_start P_ ((struct it *));
cafafe0b 757static int forward_to_next_line_start P_ ((struct it *, int *));
5f5c8ee5
GM
758static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
759 Lisp_Object, int));
760static struct text_pos string_pos P_ ((int, Lisp_Object));
761static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
762static int number_of_chars P_ ((unsigned char *, int));
763static void compute_stop_pos P_ ((struct it *));
764static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
765 Lisp_Object));
766static int face_before_or_after_it_pos P_ ((struct it *, int));
767static int next_overlay_change P_ ((int));
768static int handle_single_display_prop P_ ((struct it *, Lisp_Object,
a61b7058
GM
769 Lisp_Object, struct text_pos *,
770 int));
06a12811 771static int underlying_face_id P_ ((struct it *));
4bde0ebb
GM
772static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
773 struct window *));
5f5c8ee5
GM
774
775#define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
776#define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
ff6c30e5 777
5f5c8ee5 778#ifdef HAVE_WINDOW_SYSTEM
12adba34 779
e037b9ec
GM
780static void update_tool_bar P_ ((struct frame *, int));
781static void build_desired_tool_bar_string P_ ((struct frame *f));
782static int redisplay_tool_bar P_ ((struct frame *));
783static void display_tool_bar_line P_ ((struct it *));
12adba34 784
5f5c8ee5 785#endif /* HAVE_WINDOW_SYSTEM */
12adba34 786
5f5c8ee5
GM
787\f
788/***********************************************************************
789 Window display dimensions
790 ***********************************************************************/
12adba34 791
5f5c8ee5
GM
792/* Return the window-relative maximum y + 1 for glyph rows displaying
793 text in window W. This is the height of W minus the height of a
794 mode line, if any. */
795
796INLINE int
797window_text_bottom_y (w)
798 struct window *w;
799{
800 struct frame *f = XFRAME (w->frame);
801 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
1a578e9b 802
5f5c8ee5
GM
803 if (WINDOW_WANTS_MODELINE_P (w))
804 height -= CURRENT_MODE_LINE_HEIGHT (w);
805 return height;
f88eb0b6
KH
806}
807
f82aff7c 808
5f5c8ee5
GM
809/* Return the pixel width of display area AREA of window W. AREA < 0
810 means return the total width of W, not including bitmap areas to
811 the left and right of the window. */
ff6c30e5 812
5f5c8ee5
GM
813INLINE int
814window_box_width (w, area)
815 struct window *w;
816 int area;
817{
818 struct frame *f = XFRAME (w->frame);
819 int width = XFASTINT (w->width);
820
821 if (!w->pseudo_window_p)
ff6c30e5 822 {
050d82d7 823 width -= FRAME_SCROLL_BAR_WIDTH (f) + FRAME_FLAGS_AREA_COLS (f);
5f5c8ee5
GM
824
825 if (area == TEXT_AREA)
826 {
827 if (INTEGERP (w->left_margin_width))
828 width -= XFASTINT (w->left_margin_width);
829 if (INTEGERP (w->right_margin_width))
830 width -= XFASTINT (w->right_margin_width);
831 }
832 else if (area == LEFT_MARGIN_AREA)
833 width = (INTEGERP (w->left_margin_width)
834 ? XFASTINT (w->left_margin_width) : 0);
835 else if (area == RIGHT_MARGIN_AREA)
836 width = (INTEGERP (w->right_margin_width)
837 ? XFASTINT (w->right_margin_width) : 0);
ff6c30e5 838 }
5f5c8ee5
GM
839
840 return width * CANON_X_UNIT (f);
ff6c30e5 841}
1adc55de 842
1adc55de 843
5f5c8ee5
GM
844/* Return the pixel height of the display area of window W, not
845 including mode lines of W, if any.. */
f88eb0b6 846
5f5c8ee5
GM
847INLINE int
848window_box_height (w)
849 struct window *w;
f88eb0b6 850{
5f5c8ee5
GM
851 struct frame *f = XFRAME (w->frame);
852 int height = XFASTINT (w->height) * CANON_Y_UNIT (f);
7ecd4937
GM
853
854 xassert (height >= 0);
5f5c8ee5 855
d9c9e99c
MB
856 /* Note: the code below that determines the mode-line/header-line
857 height is essentially the same as that contained in the macro
858 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
859 the appropriate glyph row has its `mode_line_p' flag set,
860 and if it doesn't, uses estimate_mode_line_height instead. */
861
5f5c8ee5 862 if (WINDOW_WANTS_MODELINE_P (w))
97dff879
MB
863 {
864 struct glyph_row *ml_row
865 = (w->current_matrix && w->current_matrix->rows
866 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
867 : 0);
868 if (ml_row && ml_row->mode_line_p)
869 height -= ml_row->height;
870 else
871 height -= estimate_mode_line_height (f, MODE_LINE_FACE_ID);
872 }
5f5c8ee5 873
045dee35 874 if (WINDOW_WANTS_HEADER_LINE_P (w))
97dff879
MB
875 {
876 struct glyph_row *hl_row
877 = (w->current_matrix && w->current_matrix->rows
878 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
879 : 0);
880 if (hl_row && hl_row->mode_line_p)
881 height -= hl_row->height;
882 else
883 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
884 }
5f5c8ee5
GM
885
886 return height;
5992c4f7
KH
887}
888
889
5f5c8ee5
GM
890/* Return the frame-relative coordinate of the left edge of display
891 area AREA of window W. AREA < 0 means return the left edge of the
892 whole window, to the right of any bitmap area at the left side of
893 W. */
5992c4f7 894
5f5c8ee5
GM
895INLINE int
896window_box_left (w, area)
897 struct window *w;
898 int area;
90adcf20 899{
5f5c8ee5
GM
900 struct frame *f = XFRAME (w->frame);
901 int x = FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
a3788d53 902
5f5c8ee5 903 if (!w->pseudo_window_p)
90adcf20 904 {
5f5c8ee5 905 x += (WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f)
050d82d7 906 + FRAME_LEFT_FLAGS_AREA_WIDTH (f));
5f5c8ee5
GM
907
908 if (area == TEXT_AREA)
909 x += window_box_width (w, LEFT_MARGIN_AREA);
910 else if (area == RIGHT_MARGIN_AREA)
911 x += (window_box_width (w, LEFT_MARGIN_AREA)
912 + window_box_width (w, TEXT_AREA));
90adcf20 913 }
73af359d 914
5f5c8ee5
GM
915 return x;
916}
90adcf20 917
b6436d4e 918
5f5c8ee5
GM
919/* Return the frame-relative coordinate of the right edge of display
920 area AREA of window W. AREA < 0 means return the left edge of the
921 whole window, to the left of any bitmap area at the right side of
922 W. */
ded34426 923
5f5c8ee5
GM
924INLINE int
925window_box_right (w, area)
926 struct window *w;
927 int area;
928{
929 return window_box_left (w, area) + window_box_width (w, area);
930}
931
932
933/* Get the bounding box of the display area AREA of window W, without
934 mode lines, in frame-relative coordinates. AREA < 0 means the
935 whole window, not including bitmap areas to the left and right of
936 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
937 coordinates of the upper-left corner of the box. Return in
938 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
939
940INLINE void
941window_box (w, area, box_x, box_y, box_width, box_height)
942 struct window *w;
943 int area;
944 int *box_x, *box_y, *box_width, *box_height;
945{
946 struct frame *f = XFRAME (w->frame);
947
948 *box_width = window_box_width (w, area);
949 *box_height = window_box_height (w);
950 *box_x = window_box_left (w, area);
951 *box_y = (FRAME_INTERNAL_BORDER_WIDTH_SAFE (f)
952 + XFASTINT (w->top) * CANON_Y_UNIT (f));
045dee35
GM
953 if (WINDOW_WANTS_HEADER_LINE_P (w))
954 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
ded34426 955}
1adc55de 956
1adc55de 957
5f5c8ee5
GM
958/* Get the bounding box of the display area AREA of window W, without
959 mode lines. AREA < 0 means the whole window, not including bitmap
960 areas to the left and right of the window. Return in *TOP_LEFT_X
961 and TOP_LEFT_Y the frame-relative pixel coordinates of the
962 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
963 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
964 box. */
ded34426 965
5f5c8ee5
GM
966INLINE void
967window_box_edges (w, area, top_left_x, top_left_y,
968 bottom_right_x, bottom_right_y)
969 struct window *w;
970 int area;
971 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
48ae5f0a 972{
5f5c8ee5
GM
973 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
974 bottom_right_y);
975 *bottom_right_x += *top_left_x;
976 *bottom_right_y += *top_left_y;
48ae5f0a
KH
977}
978
5f5c8ee5
GM
979
980\f
981/***********************************************************************
982 Utilities
983 ***********************************************************************/
984
8b6ea97f
GM
985/* Return the bottom y-position of the line the iterator IT is in.
986 This can modify IT's settings. */
987
988int
989line_bottom_y (it)
990 struct it *it;
991{
992 int line_height = it->max_ascent + it->max_descent;
993 int line_top_y = it->current_y;
994
995 if (line_height == 0)
996 {
997 if (last_height)
998 line_height = last_height;
999 else if (IT_CHARPOS (*it) < ZV)
1000 {
1001 move_it_by_lines (it, 1, 1);
1002 line_height = (it->max_ascent || it->max_descent
1003 ? it->max_ascent + it->max_descent
1004 : last_height);
1005 }
1006 else
1007 {
1008 struct glyph_row *row = it->glyph_row;
1009
1010 /* Use the default character height. */
1011 it->glyph_row = NULL;
1012 it->what = IT_CHARACTER;
1013 it->c = ' ';
1014 it->len = 1;
1015 PRODUCE_GLYPHS (it);
1016 line_height = it->ascent + it->descent;
1017 it->glyph_row = row;
1018 }
1019 }
1020
1021 return line_top_y + line_height;
1022}
1023
1024
0db95684
GM
1025/* Return 1 if position CHARPOS is visible in window W. Set *FULLY to
1026 1 if POS is visible and the line containing POS is fully visible.
1027 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1028 and header-lines heights. */
3a641a69
GM
1029
1030int
04612a64 1031pos_visible_p (w, charpos, fully, exact_mode_line_heights_p)
3a641a69 1032 struct window *w;
04612a64 1033 int charpos, *fully, exact_mode_line_heights_p;
3a641a69
GM
1034{
1035 struct it it;
1036 struct text_pos top;
fcab1954
GM
1037 int visible_p;
1038 struct buffer *old_buffer = NULL;
1039
1040 if (XBUFFER (w->buffer) != current_buffer)
1041 {
1042 old_buffer = current_buffer;
1043 set_buffer_internal_1 (XBUFFER (w->buffer));
1044 }
3a641a69
GM
1045
1046 *fully = visible_p = 0;
1047 SET_TEXT_POS_FROM_MARKER (top, w->start);
04612a64
GM
1048
1049 /* Compute exact mode line heights, if requested. */
1050 if (exact_mode_line_heights_p)
1051 {
1052 if (WINDOW_WANTS_MODELINE_P (w))
1053 current_mode_line_height
1054 = display_mode_line (w, MODE_LINE_FACE_ID,
1055 current_buffer->mode_line_format);
1056
1057 if (WINDOW_WANTS_HEADER_LINE_P (w))
1058 current_header_line_height
1059 = display_mode_line (w, HEADER_LINE_FACE_ID,
1060 current_buffer->header_line_format);
1061 }
3a641a69 1062
04612a64 1063 start_display (&it, w, top);
3a641a69
GM
1064 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1065 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
a13be207
GM
1066
1067 /* Note that we may overshoot because of invisible text. */
1068 if (IT_CHARPOS (it) >= charpos)
3a641a69 1069 {
8b6ea97f
GM
1070 int top_y = it.current_y;
1071 int bottom_y = line_bottom_y (&it);
fcab1954 1072 int window_top_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
3a641a69 1073
8b6ea97f
GM
1074 if (top_y < window_top_y)
1075 visible_p = bottom_y > window_top_y;
1076 else if (top_y < it.last_visible_y)
fcab1954
GM
1077 {
1078 visible_p = 1;
8b6ea97f 1079 *fully = bottom_y <= it.last_visible_y;
fcab1954 1080 }
3a641a69
GM
1081 }
1082 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1083 {
1084 move_it_by_lines (&it, 1, 0);
1085 if (charpos < IT_CHARPOS (it))
1086 {
1087 visible_p = 1;
1088 *fully = 0;
1089 }
1090 }
fcab1954
GM
1091
1092 if (old_buffer)
1093 set_buffer_internal_1 (old_buffer);
04612a64
GM
1094
1095 current_header_line_height = current_mode_line_height = -1;
3a641a69
GM
1096 return visible_p;
1097}
1098
1099
4fdb80f2
GM
1100/* Return the next character from STR which is MAXLEN bytes long.
1101 Return in *LEN the length of the character. This is like
1102 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
9ab8560d 1103 we find one, we return a `?', but with the length of the invalid
4fdb80f2
GM
1104 character. */
1105
1106static INLINE int
7a5b8a93 1107string_char_and_length (str, maxlen, len)
4fdb80f2 1108 unsigned char *str;
7a5b8a93 1109 int maxlen, *len;
4fdb80f2
GM
1110{
1111 int c;
1112
1113 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1114 if (!CHAR_VALID_P (c, 1))
1115 /* We may not change the length here because other places in Emacs
9ab8560d 1116 don't use this function, i.e. they silently accept invalid
4fdb80f2
GM
1117 characters. */
1118 c = '?';
1119
1120 return c;
1121}
1122
1123
1124
5f5c8ee5
GM
1125/* Given a position POS containing a valid character and byte position
1126 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1127
1128static struct text_pos
1129string_pos_nchars_ahead (pos, string, nchars)
1130 struct text_pos pos;
1131 Lisp_Object string;
1132 int nchars;
0b1005ef 1133{
5f5c8ee5
GM
1134 xassert (STRINGP (string) && nchars >= 0);
1135
1136 if (STRING_MULTIBYTE (string))
1137 {
1138 int rest = STRING_BYTES (XSTRING (string)) - BYTEPOS (pos);
1139 unsigned char *p = XSTRING (string)->data + BYTEPOS (pos);
1140 int len;
1141
1142 while (nchars--)
1143 {
4fdb80f2 1144 string_char_and_length (p, rest, &len);
5f5c8ee5
GM
1145 p += len, rest -= len;
1146 xassert (rest >= 0);
1147 CHARPOS (pos) += 1;
1148 BYTEPOS (pos) += len;
1149 }
1150 }
1151 else
1152 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1153
1154 return pos;
0a9dc68b
RS
1155}
1156
0a9dc68b 1157
5f5c8ee5
GM
1158/* Value is the text position, i.e. character and byte position,
1159 for character position CHARPOS in STRING. */
1160
1161static INLINE struct text_pos
1162string_pos (charpos, string)
1163 int charpos;
0a9dc68b 1164 Lisp_Object string;
0a9dc68b 1165{
5f5c8ee5
GM
1166 struct text_pos pos;
1167 xassert (STRINGP (string));
1168 xassert (charpos >= 0);
1169 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1170 return pos;
1171}
1172
1173
1174/* Value is a text position, i.e. character and byte position, for
1175 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1176 means recognize multibyte characters. */
1177
1178static struct text_pos
1179c_string_pos (charpos, s, multibyte_p)
1180 int charpos;
1181 unsigned char *s;
1182 int multibyte_p;
1183{
1184 struct text_pos pos;
1185
1186 xassert (s != NULL);
1187 xassert (charpos >= 0);
1188
1189 if (multibyte_p)
0a9dc68b 1190 {
5f5c8ee5
GM
1191 int rest = strlen (s), len;
1192
1193 SET_TEXT_POS (pos, 0, 0);
1194 while (charpos--)
0a9dc68b 1195 {
4fdb80f2 1196 string_char_and_length (s, rest, &len);
5f5c8ee5
GM
1197 s += len, rest -= len;
1198 xassert (rest >= 0);
1199 CHARPOS (pos) += 1;
1200 BYTEPOS (pos) += len;
0a9dc68b
RS
1201 }
1202 }
5f5c8ee5
GM
1203 else
1204 SET_TEXT_POS (pos, charpos, charpos);
0a9dc68b 1205
5f5c8ee5
GM
1206 return pos;
1207}
0a9dc68b 1208
0a9dc68b 1209
5f5c8ee5
GM
1210/* Value is the number of characters in C string S. MULTIBYTE_P
1211 non-zero means recognize multibyte characters. */
0a9dc68b 1212
5f5c8ee5
GM
1213static int
1214number_of_chars (s, multibyte_p)
1215 unsigned char *s;
1216 int multibyte_p;
1217{
1218 int nchars;
1219
1220 if (multibyte_p)
1221 {
1222 int rest = strlen (s), len;
1223 unsigned char *p = (unsigned char *) s;
0a9dc68b 1224
5f5c8ee5
GM
1225 for (nchars = 0; rest > 0; ++nchars)
1226 {
4fdb80f2 1227 string_char_and_length (p, rest, &len);
5f5c8ee5 1228 rest -= len, p += len;
0a9dc68b
RS
1229 }
1230 }
5f5c8ee5
GM
1231 else
1232 nchars = strlen (s);
1233
1234 return nchars;
0b1005ef
KH
1235}
1236
5f5c8ee5
GM
1237
1238/* Compute byte position NEWPOS->bytepos corresponding to
1239 NEWPOS->charpos. POS is a known position in string STRING.
1240 NEWPOS->charpos must be >= POS.charpos. */
76412d64 1241
5f5c8ee5
GM
1242static void
1243compute_string_pos (newpos, pos, string)
1244 struct text_pos *newpos, pos;
1245 Lisp_Object string;
76412d64 1246{
5f5c8ee5
GM
1247 xassert (STRINGP (string));
1248 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1249
1250 if (STRING_MULTIBYTE (string))
6fc556fd
KR
1251 *newpos = string_pos_nchars_ahead (pos, string,
1252 CHARPOS (*newpos) - CHARPOS (pos));
5f5c8ee5
GM
1253 else
1254 BYTEPOS (*newpos) = CHARPOS (*newpos);
76412d64
RS
1255}
1256
9c74a0dd 1257
5f5c8ee5
GM
1258\f
1259/***********************************************************************
1260 Lisp form evaluation
1261 ***********************************************************************/
1262
116d6f5c 1263/* Error handler for safe_eval and safe_call. */
5f5c8ee5
GM
1264
1265static Lisp_Object
116d6f5c 1266safe_eval_handler (arg)
5f5c8ee5
GM
1267 Lisp_Object arg;
1268{
0dbf9fd2 1269 add_to_log ("Error during redisplay: %s", arg, Qnil);
5f5c8ee5
GM
1270 return Qnil;
1271}
1272
1273
1274/* Evaluate SEXPR and return the result, or nil if something went
1275 wrong. */
1276
71e5b1b8 1277Lisp_Object
116d6f5c 1278safe_eval (sexpr)
5f5c8ee5
GM
1279 Lisp_Object sexpr;
1280{
2d27dae2 1281 int count = BINDING_STACK_SIZE ();
0d8b31c0 1282 struct gcpro gcpro1;
5f5c8ee5 1283 Lisp_Object val;
0d8b31c0
GM
1284
1285 GCPRO1 (sexpr);
5f5c8ee5 1286 specbind (Qinhibit_redisplay, Qt);
116d6f5c 1287 val = internal_condition_case_1 (Feval, sexpr, Qerror, safe_eval_handler);
0d8b31c0
GM
1288 UNGCPRO;
1289 return unbind_to (count, val);
1290}
1291
1292
1293/* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1294 Return the result, or nil if something went wrong. */
1295
1296Lisp_Object
116d6f5c 1297safe_call (nargs, args)
0d8b31c0
GM
1298 int nargs;
1299 Lisp_Object *args;
1300{
2d27dae2 1301 int count = BINDING_STACK_SIZE ();
0d8b31c0
GM
1302 Lisp_Object val;
1303 struct gcpro gcpro1;
1304
1305 GCPRO1 (args[0]);
1306 gcpro1.nvars = nargs;
1307 specbind (Qinhibit_redisplay, Qt);
1308 val = internal_condition_case_2 (Ffuncall, nargs, args, Qerror,
116d6f5c 1309 safe_eval_handler);
0d8b31c0 1310 UNGCPRO;
5f5c8ee5
GM
1311 return unbind_to (count, val);
1312}
1313
1314
116d6f5c
GM
1315/* Call function FN with one argument ARG.
1316 Return the result, or nil if something went wrong. */
1317
1318Lisp_Object
1319safe_call1 (fn, arg)
1320 Lisp_Object fn, arg;
1321{
1322 Lisp_Object args[2];
1323 args[0] = fn;
1324 args[1] = arg;
1325 return safe_call (2, args);
1326}
1327
1328
5f5c8ee5
GM
1329\f
1330/***********************************************************************
1331 Debugging
1332 ***********************************************************************/
1333
1334#if 0
1335
1336/* Define CHECK_IT to perform sanity checks on iterators.
1337 This is for debugging. It is too slow to do unconditionally. */
1338
1339static void
1340check_it (it)
1341 struct it *it;
1342{
1343 if (it->method == next_element_from_string)
a2889657 1344 {
5f5c8ee5
GM
1345 xassert (STRINGP (it->string));
1346 xassert (IT_STRING_CHARPOS (*it) >= 0);
1347 }
1348 else if (it->method == next_element_from_buffer)
1349 {
1350 /* Check that character and byte positions agree. */
1351 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
1352 }
73af359d 1353
5f5c8ee5
GM
1354 if (it->dpvec)
1355 xassert (it->current.dpvec_index >= 0);
1356 else
1357 xassert (it->current.dpvec_index < 0);
1358}
1f40cad2 1359
5f5c8ee5
GM
1360#define CHECK_IT(IT) check_it ((IT))
1361
1362#else /* not 0 */
1363
1364#define CHECK_IT(IT) (void) 0
1365
1366#endif /* not 0 */
1367
1368
1369#if GLYPH_DEBUG
1370
1371/* Check that the window end of window W is what we expect it
1372 to be---the last row in the current matrix displaying text. */
1373
1374static void
1375check_window_end (w)
1376 struct window *w;
1377{
1378 if (!MINI_WINDOW_P (w)
1379 && !NILP (w->window_end_valid))
1380 {
1381 struct glyph_row *row;
1382 xassert ((row = MATRIX_ROW (w->current_matrix,
1383 XFASTINT (w->window_end_vpos)),
1384 !row->enabled_p
1385 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
1386 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
1387 }
1388}
1389
1390#define CHECK_WINDOW_END(W) check_window_end ((W))
1391
1392#else /* not GLYPH_DEBUG */
1393
1394#define CHECK_WINDOW_END(W) (void) 0
1395
1396#endif /* not GLYPH_DEBUG */
1397
1398
1399\f
1400/***********************************************************************
1401 Iterator initialization
1402 ***********************************************************************/
1403
1404/* Initialize IT for displaying current_buffer in window W, starting
1405 at character position CHARPOS. CHARPOS < 0 means that no buffer
1406 position is specified which is useful when the iterator is assigned
1407 a position later. BYTEPOS is the byte position corresponding to
1408 CHARPOS. BYTEPOS <= 0 means compute it from CHARPOS.
1409
1410 If ROW is not null, calls to produce_glyphs with IT as parameter
1411 will produce glyphs in that row.
1412
1413 BASE_FACE_ID is the id of a base face to use. It must be one of
1414 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID or
045dee35 1415 HEADER_LINE_FACE_ID for displaying mode lines, or TOOL_BAR_FACE_ID for
e037b9ec 1416 displaying the tool-bar.
5f5c8ee5
GM
1417
1418 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID or
045dee35 1419 HEADER_LINE_FACE_ID, the iterator will be initialized to use the
5f5c8ee5
GM
1420 corresponding mode line glyph row of the desired matrix of W. */
1421
1422void
1423init_iterator (it, w, charpos, bytepos, row, base_face_id)
1424 struct it *it;
1425 struct window *w;
1426 int charpos, bytepos;
1427 struct glyph_row *row;
1428 enum face_id base_face_id;
1429{
1430 int highlight_region_p;
5f5c8ee5
GM
1431
1432 /* Some precondition checks. */
1433 xassert (w != NULL && it != NULL);
5f5c8ee5
GM
1434 xassert (charpos < 0 || (charpos > 0 && charpos <= ZV));
1435
1436 /* If face attributes have been changed since the last redisplay,
1437 free realized faces now because they depend on face definitions
1438 that might have changed. */
1439 if (face_change_count)
1440 {
1441 face_change_count = 0;
1442 free_all_realized_faces (Qnil);
1443 }
1444
1445 /* Use one of the mode line rows of W's desired matrix if
1446 appropriate. */
1447 if (row == NULL)
1448 {
1449 if (base_face_id == MODE_LINE_FACE_ID)
1450 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
045dee35
GM
1451 else if (base_face_id == HEADER_LINE_FACE_ID)
1452 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
5f5c8ee5
GM
1453 }
1454
1455 /* Clear IT. */
1456 bzero (it, sizeof *it);
1457 it->current.overlay_string_index = -1;
1458 it->current.dpvec_index = -1;
5f5c8ee5
GM
1459 it->base_face_id = base_face_id;
1460
1461 /* The window in which we iterate over current_buffer: */
1462 XSETWINDOW (it->window, w);
1463 it->w = w;
1464 it->f = XFRAME (w->frame);
1465
d475bcb8
GM
1466 /* Extra space between lines (on window systems only). */
1467 if (base_face_id == DEFAULT_FACE_ID
1468 && FRAME_WINDOW_P (it->f))
1469 {
1470 if (NATNUMP (current_buffer->extra_line_spacing))
1471 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
1472 else if (it->f->extra_line_spacing > 0)
1473 it->extra_line_spacing = it->f->extra_line_spacing;
1474 }
1475
5f5c8ee5 1476 /* If realized faces have been removed, e.g. because of face
62be9979
GM
1477 attribute changes of named faces, recompute them. When running
1478 in batch mode, the face cache of Vterminal_frame is null. If
1479 we happen to get called, make a dummy face cache. */
9010e6b1
AI
1480 if (
1481#ifndef WINDOWSNT
1482 noninteractive &&
1483#endif
1484 FRAME_FACE_CACHE (it->f) == NULL)
62be9979 1485 init_frame_faces (it->f);
5f5c8ee5
GM
1486 if (FRAME_FACE_CACHE (it->f)->used == 0)
1487 recompute_basic_faces (it->f);
1488
5f5c8ee5
GM
1489 /* Current value of the `space-width', and 'height' properties. */
1490 it->space_width = Qnil;
1491 it->font_height = Qnil;
1492
1493 /* Are control characters displayed as `^C'? */
1494 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
1495
1496 /* -1 means everything between a CR and the following line end
1497 is invisible. >0 means lines indented more than this value are
1498 invisible. */
1499 it->selective = (INTEGERP (current_buffer->selective_display)
1500 ? XFASTINT (current_buffer->selective_display)
1501 : (!NILP (current_buffer->selective_display)
1502 ? -1 : 0));
1503 it->selective_display_ellipsis_p
1504 = !NILP (current_buffer->selective_display_ellipses);
1505
1506 /* Display table to use. */
1507 it->dp = window_display_table (w);
1508
1509 /* Are multibyte characters enabled in current_buffer? */
1510 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
1511
1512 /* Non-zero if we should highlight the region. */
1513 highlight_region_p
1514 = (!NILP (Vtransient_mark_mode)
1515 && !NILP (current_buffer->mark_active)
1516 && XMARKER (current_buffer->mark)->buffer != 0);
1517
1518 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
1519 start and end of a visible region in window IT->w. Set both to
1520 -1 to indicate no region. */
1521 if (highlight_region_p
1522 /* Maybe highlight only in selected window. */
1523 && (/* Either show region everywhere. */
1524 highlight_nonselected_windows
1525 /* Or show region in the selected window. */
1526 || w == XWINDOW (selected_window)
1527 /* Or show the region if we are in the mini-buffer and W is
1528 the window the mini-buffer refers to. */
1529 || (MINI_WINDOW_P (XWINDOW (selected_window))
d8731202 1530 && WINDOWP (Vminibuf_scroll_window)
5f5c8ee5
GM
1531 && w == XWINDOW (Vminibuf_scroll_window))))
1532 {
1533 int charpos = marker_position (current_buffer->mark);
1534 it->region_beg_charpos = min (PT, charpos);
1535 it->region_end_charpos = max (PT, charpos);
1536 }
1537 else
1538 it->region_beg_charpos = it->region_end_charpos = -1;
1539
1540 /* Get the position at which the redisplay_end_trigger hook should
1541 be run, if it is to be run at all. */
1542 if (MARKERP (w->redisplay_end_trigger)
1543 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
1544 it->redisplay_end_trigger_charpos
1545 = marker_position (w->redisplay_end_trigger);
1546 else if (INTEGERP (w->redisplay_end_trigger))
1547 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
1548
1549 /* Correct bogus values of tab_width. */
1550 it->tab_width = XINT (current_buffer->tab_width);
1551 if (it->tab_width <= 0 || it->tab_width > 1000)
1552 it->tab_width = 8;
1553
1554 /* Are lines in the display truncated? */
1555 it->truncate_lines_p
1556 = (base_face_id != DEFAULT_FACE_ID
1557 || XINT (it->w->hscroll)
1558 || (truncate_partial_width_windows
1559 && !WINDOW_FULL_WIDTH_P (it->w))
1560 || !NILP (current_buffer->truncate_lines));
1561
1562 /* Get dimensions of truncation and continuation glyphs. These are
1563 displayed as bitmaps under X, so we don't need them for such
1564 frames. */
1565 if (!FRAME_WINDOW_P (it->f))
1566 {
1567 if (it->truncate_lines_p)
1568 {
1569 /* We will need the truncation glyph. */
1570 xassert (it->glyph_row == NULL);
1571 produce_special_glyphs (it, IT_TRUNCATION);
1572 it->truncation_pixel_width = it->pixel_width;
1573 }
1574 else
1575 {
1576 /* We will need the continuation glyph. */
1577 xassert (it->glyph_row == NULL);
1578 produce_special_glyphs (it, IT_CONTINUATION);
1579 it->continuation_pixel_width = it->pixel_width;
1580 }
1581
1582 /* Reset these values to zero becaue the produce_special_glyphs
1583 above has changed them. */
1584 it->pixel_width = it->ascent = it->descent = 0;
312246d1 1585 it->phys_ascent = it->phys_descent = 0;
5f5c8ee5
GM
1586 }
1587
1588 /* Set this after getting the dimensions of truncation and
1589 continuation glyphs, so that we don't produce glyphs when calling
1590 produce_special_glyphs, above. */
1591 it->glyph_row = row;
1592 it->area = TEXT_AREA;
1593
1594 /* Get the dimensions of the display area. The display area
1595 consists of the visible window area plus a horizontally scrolled
1596 part to the left of the window. All x-values are relative to the
1597 start of this total display area. */
1598 if (base_face_id != DEFAULT_FACE_ID)
1599 {
1600 /* Mode lines, menu bar in terminal frames. */
1601 it->first_visible_x = 0;
1602 it->last_visible_x = XFASTINT (w->width) * CANON_X_UNIT (it->f);
1603 }
1604 else
1605 {
1606 it->first_visible_x
1607 = XFASTINT (it->w->hscroll) * CANON_X_UNIT (it->f);
1608 it->last_visible_x = (it->first_visible_x
1609 + window_box_width (w, TEXT_AREA));
1610
1611 /* If we truncate lines, leave room for the truncator glyph(s) at
1612 the right margin. Otherwise, leave room for the continuation
1613 glyph(s). Truncation and continuation glyphs are not inserted
1614 for window-based redisplay. */
1615 if (!FRAME_WINDOW_P (it->f))
1616 {
1617 if (it->truncate_lines_p)
1618 it->last_visible_x -= it->truncation_pixel_width;
1619 else
1620 it->last_visible_x -= it->continuation_pixel_width;
1621 }
1622
045dee35
GM
1623 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
1624 it->current_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w) + w->vscroll;
5f5c8ee5
GM
1625 }
1626
1627 /* Leave room for a border glyph. */
1628 if (!FRAME_WINDOW_P (it->f)
1629 && !WINDOW_RIGHTMOST_P (it->w))
1630 it->last_visible_x -= 1;
1631
1632 it->last_visible_y = window_text_bottom_y (w);
1633
1634 /* For mode lines and alike, arrange for the first glyph having a
1635 left box line if the face specifies a box. */
1636 if (base_face_id != DEFAULT_FACE_ID)
1637 {
1638 struct face *face;
1639
1640 it->face_id = base_face_id;
1641
1642 /* If we have a boxed mode line, make the first character appear
1643 with a left box line. */
1644 face = FACE_FROM_ID (it->f, base_face_id);
1645 if (face->box != FACE_NO_BOX)
1646 it->start_of_box_run_p = 1;
1647 }
1648
1649 /* If a buffer position was specified, set the iterator there,
1650 getting overlays and face properties from that position. */
1651 if (charpos > 0)
1652 {
1653 it->end_charpos = ZV;
1654 it->face_id = -1;
1655 IT_CHARPOS (*it) = charpos;
1656
1657 /* Compute byte position if not specified. */
1658 if (bytepos <= 0)
1659 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
1660 else
1661 IT_BYTEPOS (*it) = bytepos;
1662
1663 /* Compute faces etc. */
1664 reseat (it, it->current.pos, 1);
1665 }
1666
1667 CHECK_IT (it);
1668}
1669
1670
1671/* Initialize IT for the display of window W with window start POS. */
1672
1673void
1674start_display (it, w, pos)
1675 struct it *it;
1676 struct window *w;
1677 struct text_pos pos;
1678{
1679 int start_at_line_beg_p;
1680 struct glyph_row *row;
045dee35 1681 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
1682 int first_y;
1683
1684 row = w->desired_matrix->rows + first_vpos;
1685 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
1686 first_y = it->current_y;
1687
1688 /* If window start is not at a line start, move back to the line
1689 start. This makes sure that we take continuation lines into
1690 account. */
1691 start_at_line_beg_p = (CHARPOS (pos) == BEGV
1692 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
1693 if (!start_at_line_beg_p)
1694 reseat_at_previous_visible_line_start (it);
1695
5f5c8ee5
GM
1696 /* If window start is not at a line start, skip forward to POS to
1697 get the correct continuation_lines_width and current_x. */
1698 if (!start_at_line_beg_p)
1699 {
1700 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
1701
1702 /* If lines are continued, this line may end in the middle of a
1703 multi-glyph character (e.g. a control character displayed as
1704 \003, or in the middle of an overlay string). In this case
1705 move_it_to above will not have taken us to the start of
1706 the continuation line but to the end of the continued line. */
b28cb6ed 1707 if (!it->truncate_lines_p)
5f5c8ee5 1708 {
b28cb6ed 1709 if (it->current_x > 0)
5f5c8ee5 1710 {
b28cb6ed
GM
1711 if (it->current.dpvec_index >= 0
1712 || it->current.overlay_string_index >= 0)
1713 {
cafafe0b 1714 set_iterator_to_next (it, 1);
b28cb6ed
GM
1715 move_it_in_display_line_to (it, -1, -1, 0);
1716 }
1717
1718 it->continuation_lines_width += it->current_x;
5f5c8ee5 1719 }
b28cb6ed
GM
1720
1721 /* We're starting a new display line, not affected by the
1722 height of the continued line, so clear the appropriate
1723 fields in the iterator structure. */
1724 it->max_ascent = it->max_descent = 0;
1725 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5
GM
1726 }
1727
1728 it->current_y = first_y;
1729 it->vpos = 0;
1730 it->current_x = it->hpos = 0;
1731 }
1732
1733#if 0 /* Don't assert the following because start_display is sometimes
1734 called intentionally with a window start that is not at a
1735 line start. Please leave this code in as a comment. */
1736
1737 /* Window start should be on a line start, now. */
1738 xassert (it->continuation_lines_width
1739 || IT_CHARPOS (it) == BEGV
1740 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
1741#endif /* 0 */
1742}
1743
1744
4bde0ebb
GM
1745/* Return 1 if POS is a position in ellipses displayed for invisible
1746 text. W is the window we display, for text property lookup. */
5f5c8ee5 1747
4bde0ebb
GM
1748static int
1749in_ellipses_for_invisible_text_p (pos, w)
5f5c8ee5 1750 struct display_pos *pos;
4bde0ebb 1751 struct window *w;
5f5c8ee5 1752{
ac90c44f 1753 Lisp_Object prop, window;
4bde0ebb
GM
1754 int ellipses_p = 0;
1755 int charpos = CHARPOS (pos->pos);
ac90c44f
GM
1756
1757 /* If POS specifies a position in a display vector, this might
1758 be for an ellipsis displayed for invisible text. We won't
1759 get the iterator set up for delivering that ellipsis unless
1760 we make sure that it gets aware of the invisible text. */
1761 if (pos->dpvec_index >= 0
1762 && pos->overlay_string_index < 0
1763 && CHARPOS (pos->string_pos) < 0
1764 && charpos > BEGV
1765 && (XSETWINDOW (window, w),
1766 prop = Fget_char_property (make_number (charpos),
1767 Qinvisible, window),
20fbd925 1768 !TEXT_PROP_MEANS_INVISIBLE (prop)))
ac90c44f
GM
1769 {
1770 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
1771 window);
1772 if (TEXT_PROP_MEANS_INVISIBLE (prop)
1773 && TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop))
4bde0ebb
GM
1774 ellipses_p = 1;
1775 }
1776
1777 return ellipses_p;
1778}
1779
1780
1781/* Initialize IT for stepping through current_buffer in window W,
1782 starting at position POS that includes overlay string and display
1783 vector/ control character translation position information. */
1784
1785static void
1786init_from_display_pos (it, w, pos)
1787 struct it *it;
1788 struct window *w;
1789 struct display_pos *pos;
1790{
1791 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
1792
1793 /* If POS specifies a position in a display vector, this might
1794 be for an ellipsis displayed for invisible text. We won't
1795 get the iterator set up for delivering that ellipsis unless
1796 we make sure that it gets aware of the invisible text. */
1797 if (in_ellipses_for_invisible_text_p (pos, w))
1798 {
1799 --charpos;
1800 bytepos = 0;
ac90c44f
GM
1801 }
1802
5f5c8ee5
GM
1803 /* Keep in mind: the call to reseat in init_iterator skips invisible
1804 text, so we might end up at a position different from POS. This
1805 is only a problem when POS is a row start after a newline and an
1806 overlay starts there with an after-string, and the overlay has an
1807 invisible property. Since we don't skip invisible text in
1808 display_line and elsewhere immediately after consuming the
1809 newline before the row start, such a POS will not be in a string,
1810 but the call to init_iterator below will move us to the
1811 after-string. */
ac90c44f 1812 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
5f5c8ee5
GM
1813
1814 /* If position is within an overlay string, set up IT to
1815 the right overlay string. */
1816 if (pos->overlay_string_index >= 0)
1817 {
1818 int relative_index;
1819
1820 /* We already have the first chunk of overlay strings in
1821 IT->overlay_strings. Load more until the one for
1822 pos->overlay_string_index is in IT->overlay_strings. */
1823 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
1824 {
1825 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
1826 it->current.overlay_string_index = 0;
1827 while (n--)
1828 {
1829 load_overlay_strings (it);
1830 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
1831 }
1832 }
1833
1834 it->current.overlay_string_index = pos->overlay_string_index;
1835 relative_index = (it->current.overlay_string_index
1836 % OVERLAY_STRING_CHUNK_SIZE);
1837 it->string = it->overlay_strings[relative_index];
cafafe0b 1838 xassert (STRINGP (it->string));
5f5c8ee5
GM
1839 it->current.string_pos = pos->string_pos;
1840 it->method = next_element_from_string;
1841 }
2be8f184
GM
1842 else if (it->current.overlay_string_index >= 0)
1843 {
1844 /* If POS says we're already after an overlay string ending at
1845 POS, make sure to pop the iterator because it will be in
1846 front of that overlay string. When POS is ZV, we've thereby
1847 also ``processed'' overlay strings at ZV. */
aeb2b8fc
GM
1848 while (it->sp)
1849 pop_it (it);
2be8f184
GM
1850 it->current.overlay_string_index = -1;
1851 it->method = next_element_from_buffer;
1852 if (CHARPOS (pos->pos) == ZV)
1853 it->overlay_strings_at_end_processed_p = 1;
1854 }
1855
1856 if (CHARPOS (pos->string_pos) >= 0)
5f5c8ee5
GM
1857 {
1858 /* Recorded position is not in an overlay string, but in another
1859 string. This can only be a string from a `display' property.
1860 IT should already be filled with that string. */
1861 it->current.string_pos = pos->string_pos;
1862 xassert (STRINGP (it->string));
1863 }
1864
ac90c44f
GM
1865 /* Restore position in display vector translations, control
1866 character translations or ellipses. */
5f5c8ee5
GM
1867 if (pos->dpvec_index >= 0)
1868 {
ac90c44f
GM
1869 if (it->dpvec == NULL)
1870 get_next_display_element (it);
5f5c8ee5
GM
1871 xassert (it->dpvec && it->current.dpvec_index == 0);
1872 it->current.dpvec_index = pos->dpvec_index;
1873 }
1874
1875 CHECK_IT (it);
1876}
1877
1878
1879/* Initialize IT for stepping through current_buffer in window W
1880 starting at ROW->start. */
1881
1882static void
1883init_to_row_start (it, w, row)
1884 struct it *it;
1885 struct window *w;
1886 struct glyph_row *row;
1887{
1888 init_from_display_pos (it, w, &row->start);
1889 it->continuation_lines_width = row->continuation_lines_width;
1890 CHECK_IT (it);
1891}
1892
1893
1894/* Initialize IT for stepping through current_buffer in window W
1895 starting in the line following ROW, i.e. starting at ROW->end. */
1896
1897static void
1898init_to_row_end (it, w, row)
1899 struct it *it;
1900 struct window *w;
1901 struct glyph_row *row;
1902{
1903 init_from_display_pos (it, w, &row->end);
1904
1905 if (row->continued_p)
1906 it->continuation_lines_width = (row->continuation_lines_width
1907 + row->pixel_width);
1908 CHECK_IT (it);
1909}
1910
1911
1912
1913\f
1914/***********************************************************************
1915 Text properties
1916 ***********************************************************************/
1917
1918/* Called when IT reaches IT->stop_charpos. Handle text property and
1919 overlay changes. Set IT->stop_charpos to the next position where
1920 to stop. */
1921
1922static void
1923handle_stop (it)
1924 struct it *it;
1925{
1926 enum prop_handled handled;
1927 int handle_overlay_change_p = 1;
1928 struct props *p;
1929
1930 it->dpvec = NULL;
1931 it->current.dpvec_index = -1;
1932
1933 do
1934 {
1935 handled = HANDLED_NORMALLY;
1936
1937 /* Call text property handlers. */
1938 for (p = it_props; p->handler; ++p)
1939 {
1940 handled = p->handler (it);
2970b9be 1941
5f5c8ee5
GM
1942 if (handled == HANDLED_RECOMPUTE_PROPS)
1943 break;
1944 else if (handled == HANDLED_RETURN)
1945 return;
1946 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
1947 handle_overlay_change_p = 0;
1948 }
1949
1950 if (handled != HANDLED_RECOMPUTE_PROPS)
1951 {
1952 /* Don't check for overlay strings below when set to deliver
1953 characters from a display vector. */
1954 if (it->method == next_element_from_display_vector)
1955 handle_overlay_change_p = 0;
1956
1957 /* Handle overlay changes. */
1958 if (handle_overlay_change_p)
1959 handled = handle_overlay_change (it);
1960
1961 /* Determine where to stop next. */
1962 if (handled == HANDLED_NORMALLY)
1963 compute_stop_pos (it);
1964 }
1965 }
1966 while (handled == HANDLED_RECOMPUTE_PROPS);
1967}
1968
1969
1970/* Compute IT->stop_charpos from text property and overlay change
1971 information for IT's current position. */
1972
1973static void
1974compute_stop_pos (it)
1975 struct it *it;
1976{
1977 register INTERVAL iv, next_iv;
1978 Lisp_Object object, limit, position;
1979
1980 /* If nowhere else, stop at the end. */
1981 it->stop_charpos = it->end_charpos;
1982
1983 if (STRINGP (it->string))
1984 {
1985 /* Strings are usually short, so don't limit the search for
1986 properties. */
1987 object = it->string;
1988 limit = Qnil;
ac90c44f 1989 position = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
1990 }
1991 else
1992 {
1993 int charpos;
1994
1995 /* If next overlay change is in front of the current stop pos
1996 (which is IT->end_charpos), stop there. Note: value of
1997 next_overlay_change is point-max if no overlay change
1998 follows. */
1999 charpos = next_overlay_change (IT_CHARPOS (*it));
2000 if (charpos < it->stop_charpos)
2001 it->stop_charpos = charpos;
2002
2003 /* If showing the region, we have to stop at the region
2004 start or end because the face might change there. */
2005 if (it->region_beg_charpos > 0)
2006 {
2007 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2008 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2009 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2010 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2011 }
2012
2013 /* Set up variables for computing the stop position from text
2014 property changes. */
2015 XSETBUFFER (object, current_buffer);
ac90c44f
GM
2016 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2017 position = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2018
2019 }
2020
2021 /* Get the interval containing IT's position. Value is a null
2022 interval if there isn't such an interval. */
2023 iv = validate_interval_range (object, &position, &position, 0);
2024 if (!NULL_INTERVAL_P (iv))
2025 {
2026 Lisp_Object values_here[LAST_PROP_IDX];
2027 struct props *p;
2028
2029 /* Get properties here. */
2030 for (p = it_props; p->handler; ++p)
2031 values_here[p->idx] = textget (iv->plist, *p->name);
2032
2033 /* Look for an interval following iv that has different
2034 properties. */
2035 for (next_iv = next_interval (iv);
2036 (!NULL_INTERVAL_P (next_iv)
2037 && (NILP (limit)
2038 || XFASTINT (limit) > next_iv->position));
2039 next_iv = next_interval (next_iv))
2040 {
2041 for (p = it_props; p->handler; ++p)
2042 {
2043 Lisp_Object new_value;
2044
2045 new_value = textget (next_iv->plist, *p->name);
2046 if (!EQ (values_here[p->idx], new_value))
2047 break;
2048 }
2049
2050 if (p->handler)
2051 break;
2052 }
2053
2054 if (!NULL_INTERVAL_P (next_iv))
2055 {
2056 if (INTEGERP (limit)
2057 && next_iv->position >= XFASTINT (limit))
2058 /* No text property change up to limit. */
2059 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2060 else
2061 /* Text properties change in next_iv. */
2062 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2063 }
2064 }
2065
2066 xassert (STRINGP (it->string)
2067 || (it->stop_charpos >= BEGV
2068 && it->stop_charpos >= IT_CHARPOS (*it)));
2069}
2070
2071
2072/* Return the position of the next overlay change after POS in
2073 current_buffer. Value is point-max if no overlay change
2074 follows. This is like `next-overlay-change' but doesn't use
2075 xmalloc. */
2076
2077static int
2078next_overlay_change (pos)
2079 int pos;
2080{
2081 int noverlays;
2082 int endpos;
2083 Lisp_Object *overlays;
2084 int len;
2085 int i;
2086
2087 /* Get all overlays at the given position. */
2088 len = 10;
2089 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2090 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2091 if (noverlays > len)
2092 {
2093 len = noverlays;
2094 overlays = (Lisp_Object *) alloca (len * sizeof *overlays);
a0315a63 2095 noverlays = overlays_at (pos, 0, &overlays, &len, &endpos, NULL, 1);
5f5c8ee5
GM
2096 }
2097
2098 /* If any of these overlays ends before endpos,
2099 use its ending point instead. */
2100 for (i = 0; i < noverlays; ++i)
2101 {
2102 Lisp_Object oend;
2103 int oendpos;
2104
2105 oend = OVERLAY_END (overlays[i]);
2106 oendpos = OVERLAY_POSITION (oend);
2107 endpos = min (endpos, oendpos);
2108 }
2109
2110 return endpos;
2111}
2112
2113
2114\f
2115/***********************************************************************
2116 Fontification
2117 ***********************************************************************/
2118
2119/* Handle changes in the `fontified' property of the current buffer by
2120 calling hook functions from Qfontification_functions to fontify
2121 regions of text. */
2122
2123static enum prop_handled
2124handle_fontified_prop (it)
2125 struct it *it;
2126{
2127 Lisp_Object prop, pos;
2128 enum prop_handled handled = HANDLED_NORMALLY;
2129
2130 /* Get the value of the `fontified' property at IT's current buffer
2131 position. (The `fontified' property doesn't have a special
2132 meaning in strings.) If the value is nil, call functions from
2133 Qfontification_functions. */
2134 if (!STRINGP (it->string)
2135 && it->s == NULL
2136 && !NILP (Vfontification_functions)
085536c2 2137 && !NILP (Vrun_hooks)
5f5c8ee5
GM
2138 && (pos = make_number (IT_CHARPOS (*it)),
2139 prop = Fget_char_property (pos, Qfontified, Qnil),
2140 NILP (prop)))
2141 {
2d27dae2 2142 int count = BINDING_STACK_SIZE ();
085536c2
GM
2143 Lisp_Object val;
2144
2145 val = Vfontification_functions;
2146 specbind (Qfontification_functions, Qnil);
2147 specbind (Qafter_change_functions, Qnil);
2148
2149 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
116d6f5c 2150 safe_call1 (val, pos);
085536c2
GM
2151 else
2152 {
2153 Lisp_Object globals, fn;
2154 struct gcpro gcpro1, gcpro2;
5f5c8ee5 2155
085536c2
GM
2156 globals = Qnil;
2157 GCPRO2 (val, globals);
2158
2159 for (; CONSP (val); val = XCDR (val))
2160 {
2161 fn = XCAR (val);
2162
2163 if (EQ (fn, Qt))
2164 {
2165 /* A value of t indicates this hook has a local
2166 binding; it means to run the global binding too.
2167 In a global value, t should not occur. If it
2168 does, we must ignore it to avoid an endless
2169 loop. */
2170 for (globals = Fdefault_value (Qfontification_functions);
2171 CONSP (globals);
2172 globals = XCDR (globals))
2173 {
2174 fn = XCAR (globals);
2175 if (!EQ (fn, Qt))
116d6f5c 2176 safe_call1 (fn, pos);
085536c2
GM
2177 }
2178 }
2179 else
116d6f5c 2180 safe_call1 (fn, pos);
085536c2
GM
2181 }
2182
2183 UNGCPRO;
2184 }
2185
2186 unbind_to (count, Qnil);
5f5c8ee5
GM
2187
2188 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2189 something. This avoids an endless loop if they failed to
2190 fontify the text for which reason ever. */
2191 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2192 handled = HANDLED_RECOMPUTE_PROPS;
2193 }
2194
2195 return handled;
2196}
2197
2198
2199\f
2200/***********************************************************************
2201 Faces
2202 ***********************************************************************/
2203
2204/* Set up iterator IT from face properties at its current position.
2205 Called from handle_stop. */
2206
2207static enum prop_handled
2208handle_face_prop (it)
2209 struct it *it;
2210{
2211 int new_face_id, next_stop;
2212
2213 if (!STRINGP (it->string))
2214 {
2215 new_face_id
2216 = face_at_buffer_position (it->w,
2217 IT_CHARPOS (*it),
2218 it->region_beg_charpos,
2219 it->region_end_charpos,
2220 &next_stop,
2221 (IT_CHARPOS (*it)
2222 + TEXT_PROP_DISTANCE_LIMIT),
2223 0);
2224
2225 /* Is this a start of a run of characters with box face?
2226 Caveat: this can be called for a freshly initialized
2227 iterator; face_id is -1 is this case. We know that the new
2228 face will not change until limit, i.e. if the new face has a
2229 box, all characters up to limit will have one. But, as
2230 usual, we don't know whether limit is really the end. */
2231 if (new_face_id != it->face_id)
2232 {
2233 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2234
2235 /* If new face has a box but old face has not, this is
2236 the start of a run of characters with box, i.e. it has
2237 a shadow on the left side. The value of face_id of the
2238 iterator will be -1 if this is the initial call that gets
2239 the face. In this case, we have to look in front of IT's
2240 position and see whether there is a face != new_face_id. */
2241 it->start_of_box_run_p
2242 = (new_face->box != FACE_NO_BOX
2243 && (it->face_id >= 0
2244 || IT_CHARPOS (*it) == BEG
2245 || new_face_id != face_before_it_pos (it)));
2246 it->face_box_p = new_face->box != FACE_NO_BOX;
2247 }
2248 }
2249 else
2250 {
06a12811
GM
2251 int base_face_id, bufpos;
2252
2253 if (it->current.overlay_string_index >= 0)
2254 bufpos = IT_CHARPOS (*it);
2255 else
2256 bufpos = 0;
2257
2258 /* For strings from a buffer, i.e. overlay strings or strings
2259 from a `display' property, use the face at IT's current
2260 buffer position as the base face to merge with, so that
2261 overlay strings appear in the same face as surrounding
2262 text, unless they specify their own faces. */
2263 base_face_id = underlying_face_id (it);
2264
2265 new_face_id = face_at_string_position (it->w,
2266 it->string,
2267 IT_STRING_CHARPOS (*it),
2268 bufpos,
2269 it->region_beg_charpos,
2270 it->region_end_charpos,
2271 &next_stop,
5de7c6f2 2272 base_face_id, 0);
5f5c8ee5
GM
2273
2274#if 0 /* This shouldn't be neccessary. Let's check it. */
2275 /* If IT is used to display a mode line we would really like to
2276 use the mode line face instead of the frame's default face. */
2277 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
2278 && new_face_id == DEFAULT_FACE_ID)
2279 new_face_id = MODE_LINE_FACE_ID;
2280#endif
2281
2282 /* Is this a start of a run of characters with box? Caveat:
2283 this can be called for a freshly allocated iterator; face_id
2284 is -1 is this case. We know that the new face will not
2285 change until the next check pos, i.e. if the new face has a
2286 box, all characters up to that position will have a
2287 box. But, as usual, we don't know whether that position
2288 is really the end. */
2289 if (new_face_id != it->face_id)
2290 {
2291 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2292 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
2293
2294 /* If new face has a box but old face hasn't, this is the
2295 start of a run of characters with box, i.e. it has a
2296 shadow on the left side. */
2297 it->start_of_box_run_p
2298 = new_face->box && (old_face == NULL || !old_face->box);
2299 it->face_box_p = new_face->box != FACE_NO_BOX;
2300 }
2301 }
2302
2303 it->face_id = new_face_id;
5f5c8ee5
GM
2304 return HANDLED_NORMALLY;
2305}
2306
2307
06a12811
GM
2308/* Return the ID of the face ``underlying'' IT's current position,
2309 which is in a string. If the iterator is associated with a
2310 buffer, return the face at IT's current buffer position.
2311 Otherwise, use the iterator's base_face_id. */
2312
2313static int
2314underlying_face_id (it)
2315 struct it *it;
2316{
2317 int face_id = it->base_face_id, i;
2318
2319 xassert (STRINGP (it->string));
2320
2321 for (i = it->sp - 1; i >= 0; --i)
2322 if (NILP (it->stack[i].string))
2323 face_id = it->stack[i].face_id;
2324
2325 return face_id;
2326}
2327
2328
5f5c8ee5
GM
2329/* Compute the face one character before or after the current position
2330 of IT. BEFORE_P non-zero means get the face in front of IT's
2331 position. Value is the id of the face. */
2332
2333static int
2334face_before_or_after_it_pos (it, before_p)
2335 struct it *it;
2336 int before_p;
2337{
2338 int face_id, limit;
2339 int next_check_charpos;
2340 struct text_pos pos;
2341
2342 xassert (it->s == NULL);
2343
2344 if (STRINGP (it->string))
2345 {
06a12811
GM
2346 int bufpos, base_face_id;
2347
5f5c8ee5
GM
2348 /* No face change past the end of the string (for the case
2349 we are padding with spaces). No face change before the
2350 string start. */
2351 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size
2352 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
2353 return it->face_id;
2354
2355 /* Set pos to the position before or after IT's current position. */
2356 if (before_p)
2357 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
2358 else
260a86a0
KH
2359 /* For composition, we must check the character after the
2360 composition. */
2361 pos = (it->what == IT_COMPOSITION
2362 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
2363 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
5f5c8ee5 2364
06a12811
GM
2365 if (it->current.overlay_string_index >= 0)
2366 bufpos = IT_CHARPOS (*it);
2367 else
2368 bufpos = 0;
2369
2370 base_face_id = underlying_face_id (it);
2371
5f5c8ee5 2372 /* Get the face for ASCII, or unibyte. */
06a12811
GM
2373 face_id = face_at_string_position (it->w,
2374 it->string,
2375 CHARPOS (pos),
2376 bufpos,
2377 it->region_beg_charpos,
2378 it->region_end_charpos,
2379 &next_check_charpos,
5de7c6f2 2380 base_face_id, 0);
5f5c8ee5
GM
2381
2382 /* Correct the face for charsets different from ASCII. Do it
2383 for the multibyte case only. The face returned above is
2384 suitable for unibyte text if IT->string is unibyte. */
2385 if (STRING_MULTIBYTE (it->string))
2386 {
2387 unsigned char *p = XSTRING (it->string)->data + BYTEPOS (pos);
2388 int rest = STRING_BYTES (XSTRING (it->string)) - BYTEPOS (pos);
980806b6
KH
2389 int c, len;
2390 struct face *face = FACE_FROM_ID (it->f, face_id);
5f5c8ee5 2391
4fdb80f2 2392 c = string_char_and_length (p, rest, &len);
980806b6 2393 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2394 }
2395 }
2396 else
2397 {
70851746
GM
2398 if ((IT_CHARPOS (*it) >= ZV && !before_p)
2399 || (IT_CHARPOS (*it) <= BEGV && before_p))
2400 return it->face_id;
2401
5f5c8ee5
GM
2402 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
2403 pos = it->current.pos;
2404
2405 if (before_p)
c1005d06 2406 DEC_TEXT_POS (pos, it->multibyte_p);
5f5c8ee5 2407 else
260a86a0
KH
2408 {
2409 if (it->what == IT_COMPOSITION)
2410 /* For composition, we must check the position after the
2411 composition. */
2412 pos.charpos += it->cmp_len, pos.bytepos += it->len;
2413 else
c1005d06 2414 INC_TEXT_POS (pos, it->multibyte_p);
260a86a0 2415 }
06a12811 2416
5f5c8ee5
GM
2417 /* Determine face for CHARSET_ASCII, or unibyte. */
2418 face_id = face_at_buffer_position (it->w,
2419 CHARPOS (pos),
2420 it->region_beg_charpos,
2421 it->region_end_charpos,
2422 &next_check_charpos,
2423 limit, 0);
2424
2425 /* Correct the face for charsets different from ASCII. Do it
2426 for the multibyte case only. The face returned above is
2427 suitable for unibyte text if current_buffer is unibyte. */
2428 if (it->multibyte_p)
2429 {
980806b6
KH
2430 int c = FETCH_MULTIBYTE_CHAR (CHARPOS (pos));
2431 struct face *face = FACE_FROM_ID (it->f, face_id);
2432 face_id = FACE_FOR_CHAR (it->f, face, c);
5f5c8ee5
GM
2433 }
2434 }
2435
2436 return face_id;
2437}
2438
2439
2440\f
2441/***********************************************************************
2442 Invisible text
2443 ***********************************************************************/
2444
2445/* Set up iterator IT from invisible properties at its current
2446 position. Called from handle_stop. */
2447
2448static enum prop_handled
2449handle_invisible_prop (it)
2450 struct it *it;
2451{
2452 enum prop_handled handled = HANDLED_NORMALLY;
2453
2454 if (STRINGP (it->string))
2455 {
2456 extern Lisp_Object Qinvisible;
2457 Lisp_Object prop, end_charpos, limit, charpos;
2458
2459 /* Get the value of the invisible text property at the
2460 current position. Value will be nil if there is no such
2461 property. */
ac90c44f 2462 charpos = make_number (IT_STRING_CHARPOS (*it));
5f5c8ee5
GM
2463 prop = Fget_text_property (charpos, Qinvisible, it->string);
2464
eadc0bf8
GM
2465 if (!NILP (prop)
2466 && IT_STRING_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2467 {
2468 handled = HANDLED_RECOMPUTE_PROPS;
2469
2470 /* Get the position at which the next change of the
2471 invisible text property can be found in IT->string.
2472 Value will be nil if the property value is the same for
2473 all the rest of IT->string. */
2474 XSETINT (limit, XSTRING (it->string)->size);
2475 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
2476 it->string, limit);
2477
2478 /* Text at current position is invisible. The next
2479 change in the property is at position end_charpos.
2480 Move IT's current position to that position. */
2481 if (INTEGERP (end_charpos)
2482 && XFASTINT (end_charpos) < XFASTINT (limit))
2483 {
2484 struct text_pos old;
2485 old = it->current.string_pos;
2486 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
2487 compute_string_pos (&it->current.string_pos, old, it->string);
2488 }
2489 else
2490 {
2491 /* The rest of the string is invisible. If this is an
2492 overlay string, proceed with the next overlay string
2493 or whatever comes and return a character from there. */
2494 if (it->current.overlay_string_index >= 0)
2495 {
2496 next_overlay_string (it);
2497 /* Don't check for overlay strings when we just
2498 finished processing them. */
2499 handled = HANDLED_OVERLAY_STRING_CONSUMED;
2500 }
2501 else
2502 {
2503 struct Lisp_String *s = XSTRING (it->string);
2504 IT_STRING_CHARPOS (*it) = s->size;
2505 IT_STRING_BYTEPOS (*it) = STRING_BYTES (s);
2506 }
2507 }
2508 }
2509 }
2510 else
2511 {
2512 int visible_p, newpos, next_stop;
2513 Lisp_Object pos, prop;
2514
2515 /* First of all, is there invisible text at this position? */
ac90c44f 2516 pos = make_number (IT_CHARPOS (*it));
5f5c8ee5
GM
2517 prop = Fget_char_property (pos, Qinvisible, it->window);
2518
2519 /* If we are on invisible text, skip over it. */
eadc0bf8
GM
2520 if (TEXT_PROP_MEANS_INVISIBLE (prop)
2521 && IT_CHARPOS (*it) < it->end_charpos)
5f5c8ee5
GM
2522 {
2523 /* Record whether we have to display an ellipsis for the
2524 invisible text. */
2525 int display_ellipsis_p
2526 = TEXT_PROP_MEANS_INVISIBLE_WITH_ELLIPSIS (prop);
2527
2528 handled = HANDLED_RECOMPUTE_PROPS;
2529
2530 /* Loop skipping over invisible text. The loop is left at
2531 ZV or with IT on the first char being visible again. */
2532 do
2533 {
2534 /* Try to skip some invisible text. Return value is the
2535 position reached which can be equal to IT's position
2536 if there is nothing invisible here. This skips both
2537 over invisible text properties and overlays with
2538 invisible property. */
2539 newpos = skip_invisible (IT_CHARPOS (*it),
2540 &next_stop, ZV, it->window);
2541
2542 /* If we skipped nothing at all we weren't at invisible
2543 text in the first place. If everything to the end of
2544 the buffer was skipped, end the loop. */
2545 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
2546 visible_p = 1;
2547 else
2548 {
2549 /* We skipped some characters but not necessarily
2550 all there are. Check if we ended up on visible
2551 text. Fget_char_property returns the property of
2552 the char before the given position, i.e. if we
2553 get visible_p = 1, this means that the char at
2554 newpos is visible. */
ac90c44f 2555 pos = make_number (newpos);
5f5c8ee5
GM
2556 prop = Fget_char_property (pos, Qinvisible, it->window);
2557 visible_p = !TEXT_PROP_MEANS_INVISIBLE (prop);
2558 }
2559
2560 /* If we ended up on invisible text, proceed to
2561 skip starting with next_stop. */
2562 if (!visible_p)
2563 IT_CHARPOS (*it) = next_stop;
2564 }
2565 while (!visible_p);
2566
2567 /* The position newpos is now either ZV or on visible text. */
2568 IT_CHARPOS (*it) = newpos;
2569 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
2570
2571 /* Maybe return `...' next for the end of the invisible text. */
2572 if (display_ellipsis_p)
2573 {
2574 if (it->dp
2575 && VECTORP (DISP_INVIS_VECTOR (it->dp)))
2576 {
2577 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
2578 it->dpvec = v->contents;
2579 it->dpend = v->contents + v->size;
2580 }
2581 else
2582 {
2583 /* Default `...'. */
2584 it->dpvec = default_invis_vector;
2585 it->dpend = default_invis_vector + 3;
2586 }
2587
2588 /* The ellipsis display does not replace the display of
2589 the character at the new position. Indicate this by
2590 setting IT->dpvec_char_len to zero. */
2591 it->dpvec_char_len = 0;
2592
2593 it->current.dpvec_index = 0;
2594 it->method = next_element_from_display_vector;
2595 }
2596 }
2597 }
2598
2599 return handled;
2600}
2601
2602
2603\f
2604/***********************************************************************
2605 'display' property
2606 ***********************************************************************/
2607
2608/* Set up iterator IT from `display' property at its current position.
2609 Called from handle_stop. */
2610
2611static enum prop_handled
2612handle_display_prop (it)
2613 struct it *it;
2614{
2615 Lisp_Object prop, object;
2616 struct text_pos *position;
a61b7058 2617 int display_replaced_p = 0;
5f5c8ee5
GM
2618
2619 if (STRINGP (it->string))
2620 {
2621 object = it->string;
2622 position = &it->current.string_pos;
2623 }
2624 else
2625 {
221dd3e7 2626 object = it->w->buffer;
5f5c8ee5
GM
2627 position = &it->current.pos;
2628 }
2629
2630 /* Reset those iterator values set from display property values. */
2631 it->font_height = Qnil;
2632 it->space_width = Qnil;
2633 it->voffset = 0;
2634
2635 /* We don't support recursive `display' properties, i.e. string
2636 values that have a string `display' property, that have a string
2637 `display' property etc. */
2638 if (!it->string_from_display_prop_p)
2639 it->area = TEXT_AREA;
2640
2641 prop = Fget_char_property (make_number (position->charpos),
2642 Qdisplay, object);
2643 if (NILP (prop))
2644 return HANDLED_NORMALLY;
2645
f3751a65
GM
2646 if (CONSP (prop)
2647 && CONSP (XCAR (prop))
2648 && !EQ (Qmargin, XCAR (XCAR (prop))))
5f5c8ee5 2649 {
f3751a65 2650 /* A list of sub-properties. */
a61b7058 2651 for (; CONSP (prop); prop = XCDR (prop))
5f5c8ee5 2652 {
a61b7058
GM
2653 if (handle_single_display_prop (it, XCAR (prop), object,
2654 position, display_replaced_p))
2655 display_replaced_p = 1;
5f5c8ee5
GM
2656 }
2657 }
2658 else if (VECTORP (prop))
2659 {
2660 int i;
a61b7058
GM
2661 for (i = 0; i < ASIZE (prop); ++i)
2662 if (handle_single_display_prop (it, AREF (prop, i), object,
2663 position, display_replaced_p))
2664 display_replaced_p = 1;
5f5c8ee5
GM
2665 }
2666 else
2667 {
a61b7058
GM
2668 if (handle_single_display_prop (it, prop, object, position, 0))
2669 display_replaced_p = 1;
5f5c8ee5
GM
2670 }
2671
a61b7058 2672 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
5f5c8ee5
GM
2673}
2674
2675
6c577098 2676/* Value is the position of the end of the `display' property starting
5f5c8ee5
GM
2677 at START_POS in OBJECT. */
2678
2679static struct text_pos
2680display_prop_end (it, object, start_pos)
2681 struct it *it;
2682 Lisp_Object object;
2683 struct text_pos start_pos;
2684{
2685 Lisp_Object end;
2686 struct text_pos end_pos;
5f5c8ee5 2687
016b5642
MB
2688 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
2689 Qdisplay, object, Qnil);
6c577098
GM
2690 CHARPOS (end_pos) = XFASTINT (end);
2691 if (STRINGP (object))
5f5c8ee5
GM
2692 compute_string_pos (&end_pos, start_pos, it->string);
2693 else
6c577098 2694 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
5f5c8ee5
GM
2695
2696 return end_pos;
2697}
2698
2699
2700/* Set up IT from a single `display' sub-property value PROP. OBJECT
2701 is the object in which the `display' property was found. *POSITION
a61b7058
GM
2702 is the position at which it was found. DISPLAY_REPLACED_P non-zero
2703 means that we previously saw a display sub-property which already
2704 replaced text display with something else, for example an image;
2705 ignore such properties after the first one has been processed.
5f5c8ee5
GM
2706
2707 If PROP is a `space' or `image' sub-property, set *POSITION to the
2708 end position of the `display' property.
2709
a61b7058
GM
2710 Value is non-zero something was found which replaces the display
2711 of buffer or string text. */
5f5c8ee5
GM
2712
2713static int
a61b7058
GM
2714handle_single_display_prop (it, prop, object, position,
2715 display_replaced_before_p)
5f5c8ee5
GM
2716 struct it *it;
2717 Lisp_Object prop;
2718 Lisp_Object object;
2719 struct text_pos *position;
a61b7058 2720 int display_replaced_before_p;
5f5c8ee5
GM
2721{
2722 Lisp_Object value;
a61b7058 2723 int replaces_text_display_p = 0;
5f5c8ee5
GM
2724 Lisp_Object form;
2725
d3acf96b 2726 /* If PROP is a list of the form `(when FORM . VALUE)', FORM is
5a4c88c4 2727 evaluated. If the result is nil, VALUE is ignored. */
5f5c8ee5 2728 form = Qt;
d3acf96b 2729 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5f5c8ee5
GM
2730 {
2731 prop = XCDR (prop);
2732 if (!CONSP (prop))
2733 return 0;
2734 form = XCAR (prop);
2735 prop = XCDR (prop);
5f5c8ee5
GM
2736 }
2737
2738 if (!NILP (form) && !EQ (form, Qt))
2739 {
2740 struct gcpro gcpro1;
2741 struct text_pos end_pos, pt;
2742
5f5c8ee5 2743 GCPRO1 (form);
c7aca1ad 2744 end_pos = display_prop_end (it, object, *position);
5f5c8ee5
GM
2745
2746 /* Temporarily set point to the end position, and then evaluate
2747 the form. This makes `(eolp)' work as FORM. */
c7aca1ad
GM
2748 if (BUFFERP (object))
2749 {
2750 CHARPOS (pt) = PT;
2751 BYTEPOS (pt) = PT_BYTE;
2752 TEMP_SET_PT_BOTH (CHARPOS (end_pos), BYTEPOS (end_pos));
2753 }
2754
116d6f5c 2755 form = safe_eval (form);
c7aca1ad
GM
2756
2757 if (BUFFERP (object))
2758 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
5f5c8ee5
GM
2759 UNGCPRO;
2760 }
2761
2762 if (NILP (form))
2763 return 0;
2764
2765 if (CONSP (prop)
2766 && EQ (XCAR (prop), Qheight)
2767 && CONSP (XCDR (prop)))
2768 {
e4093f38 2769 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
5f5c8ee5
GM
2770 return 0;
2771
2772 /* `(height HEIGHT)'. */
2773 it->font_height = XCAR (XCDR (prop));
2774 if (!NILP (it->font_height))
2775 {
2776 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2777 int new_height = -1;
2778
2779 if (CONSP (it->font_height)
2780 && (EQ (XCAR (it->font_height), Qplus)
2781 || EQ (XCAR (it->font_height), Qminus))
2782 && CONSP (XCDR (it->font_height))
2783 && INTEGERP (XCAR (XCDR (it->font_height))))
2784 {
2785 /* `(+ N)' or `(- N)' where N is an integer. */
2786 int steps = XINT (XCAR (XCDR (it->font_height)));
2787 if (EQ (XCAR (it->font_height), Qplus))
2788 steps = - steps;
2789 it->face_id = smaller_face (it->f, it->face_id, steps);
2790 }
0d8b31c0 2791 else if (FUNCTIONP (it->font_height))
5f5c8ee5
GM
2792 {
2793 /* Call function with current height as argument.
2794 Value is the new height. */
116d6f5c
GM
2795 Lisp_Object height;
2796 height = safe_call1 (it->font_height,
2797 face->lface[LFACE_HEIGHT_INDEX]);
5f5c8ee5
GM
2798 if (NUMBERP (height))
2799 new_height = XFLOATINT (height);
5f5c8ee5
GM
2800 }
2801 else if (NUMBERP (it->font_height))
2802 {
2803 /* Value is a multiple of the canonical char height. */
2804 struct face *face;
2805
2806 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
2807 new_height = (XFLOATINT (it->font_height)
2808 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
2809 }
2810 else
2811 {
2812 /* Evaluate IT->font_height with `height' bound to the
2813 current specified height to get the new height. */
2814 Lisp_Object value;
2d27dae2 2815 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
2816
2817 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
116d6f5c 2818 value = safe_eval (it->font_height);
5f5c8ee5
GM
2819 unbind_to (count, Qnil);
2820
2821 if (NUMBERP (value))
2822 new_height = XFLOATINT (value);
2823 }
2824
2825 if (new_height > 0)
2826 it->face_id = face_with_height (it->f, it->face_id, new_height);
2827 }
2828 }
2829 else if (CONSP (prop)
2830 && EQ (XCAR (prop), Qspace_width)
2831 && CONSP (XCDR (prop)))
2832 {
2833 /* `(space_width WIDTH)'. */
e4093f38 2834 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2835 return 0;
5f5c8ee5
GM
2836
2837 value = XCAR (XCDR (prop));
2838 if (NUMBERP (value) && XFLOATINT (value) > 0)
2839 it->space_width = value;
2840 }
2841 else if (CONSP (prop)
2842 && EQ (XCAR (prop), Qraise)
2843 && CONSP (XCDR (prop)))
2844 {
5f5c8ee5 2845 /* `(raise FACTOR)'. */
e4093f38 2846 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
bafb434c 2847 return 0;
5f5c8ee5 2848
fb3842a8 2849#ifdef HAVE_WINDOW_SYSTEM
5f5c8ee5
GM
2850 value = XCAR (XCDR (prop));
2851 if (NUMBERP (value))
2852 {
2853 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2854 it->voffset = - (XFLOATINT (value)
1ab3e082 2855 * (FONT_HEIGHT (face->font)));
5f5c8ee5
GM
2856 }
2857#endif /* HAVE_WINDOW_SYSTEM */
2858 }
2859 else if (!it->string_from_display_prop_p)
2860 {
f3751a65
GM
2861 /* `((margin left-margin) VALUE)' or `((margin right-margin)
2862 VALUE) or `((margin nil) VALUE)' or VALUE. */
5f5c8ee5
GM
2863 Lisp_Object location, value;
2864 struct text_pos start_pos;
2865 int valid_p;
2866
2867 /* Characters having this form of property are not displayed, so
2868 we have to find the end of the property. */
5f5c8ee5
GM
2869 start_pos = *position;
2870 *position = display_prop_end (it, object, start_pos);
15e26c76 2871 value = Qnil;
5f5c8ee5
GM
2872
2873 /* Let's stop at the new position and assume that all
2874 text properties change there. */
2875 it->stop_charpos = position->charpos;
2876
f3751a65
GM
2877 location = Qunbound;
2878 if (CONSP (prop) && CONSP (XCAR (prop)))
5f5c8ee5 2879 {
f3751a65
GM
2880 Lisp_Object tem;
2881
5f5c8ee5 2882 value = XCDR (prop);
f3751a65
GM
2883 if (CONSP (value))
2884 value = XCAR (value);
2885
2886 tem = XCAR (prop);
2887 if (EQ (XCAR (tem), Qmargin)
2888 && (tem = XCDR (tem),
2889 tem = CONSP (tem) ? XCAR (tem) : Qnil,
2890 (NILP (tem)
2891 || EQ (tem, Qleft_margin)
2892 || EQ (tem, Qright_margin))))
2893 location = tem;
5f5c8ee5 2894 }
f3751a65
GM
2895
2896 if (EQ (location, Qunbound))
5f5c8ee5
GM
2897 {
2898 location = Qnil;
2899 value = prop;
2900 }
2901
2902#ifdef HAVE_WINDOW_SYSTEM
fb3842a8 2903 if (FRAME_TERMCAP_P (it->f))
5f5c8ee5
GM
2904 valid_p = STRINGP (value);
2905 else
2906 valid_p = (STRINGP (value)
2907 || (CONSP (value) && EQ (XCAR (value), Qspace))
2908 || valid_image_p (value));
2909#else /* not HAVE_WINDOW_SYSTEM */
2910 valid_p = STRINGP (value);
2911#endif /* not HAVE_WINDOW_SYSTEM */
2912
2913 if ((EQ (location, Qleft_margin)
2914 || EQ (location, Qright_margin)
2915 || NILP (location))
a61b7058
GM
2916 && valid_p
2917 && !display_replaced_before_p)
5f5c8ee5 2918 {
a61b7058 2919 replaces_text_display_p = 1;
a21a3943 2920
5f5c8ee5
GM
2921 /* Save current settings of IT so that we can restore them
2922 when we are finished with the glyph property value. */
2923 push_it (it);
2924
2925 if (NILP (location))
2926 it->area = TEXT_AREA;
2927 else if (EQ (location, Qleft_margin))
2928 it->area = LEFT_MARGIN_AREA;
2929 else
2930 it->area = RIGHT_MARGIN_AREA;
2931
2932 if (STRINGP (value))
2933 {
2934 it->string = value;
2935 it->multibyte_p = STRING_MULTIBYTE (it->string);
2936 it->current.overlay_string_index = -1;
2937 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
2938 it->end_charpos = it->string_nchars
2939 = XSTRING (it->string)->size;
2940 it->method = next_element_from_string;
2941 it->stop_charpos = 0;
2942 it->string_from_display_prop_p = 1;
e187cf71
GM
2943 /* Say that we haven't consumed the characters with
2944 `display' property yet. The call to pop_it in
2945 set_iterator_to_next will clean this up. */
2946 *position = start_pos;
5f5c8ee5
GM
2947 }
2948 else if (CONSP (value) && EQ (XCAR (value), Qspace))
2949 {
2950 it->method = next_element_from_stretch;
2951 it->object = value;
2952 it->current.pos = it->position = start_pos;
2953 }
2954#ifdef HAVE_WINDOW_SYSTEM
2955 else
2956 {
2957 it->what = IT_IMAGE;
2958 it->image_id = lookup_image (it->f, value);
2959 it->position = start_pos;
2960 it->object = NILP (object) ? it->w->buffer : object;
2961 it->method = next_element_from_image;
2962
02513cdd 2963 /* Say that we haven't consumed the characters with
5f5c8ee5
GM
2964 `display' property yet. The call to pop_it in
2965 set_iterator_to_next will clean this up. */
2966 *position = start_pos;
2967 }
2968#endif /* HAVE_WINDOW_SYSTEM */
2969 }
a21a3943
GM
2970 else
2971 /* Invalid property or property not supported. Restore
2972 the position to what it was before. */
2973 *position = start_pos;
5f5c8ee5
GM
2974 }
2975
a61b7058 2976 return replaces_text_display_p;
5f5c8ee5
GM
2977}
2978
2979
06568bbf
GM
2980/* Check if PROP is a display sub-property value whose text should be
2981 treated as intangible. */
2982
2983static int
2984single_display_prop_intangible_p (prop)
2985 Lisp_Object prop;
2986{
2987 /* Skip over `when FORM'. */
2988 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
2989 {
2990 prop = XCDR (prop);
2991 if (!CONSP (prop))
2992 return 0;
2993 prop = XCDR (prop);
2994 }
2995
2996 if (!CONSP (prop))
2997 return 0;
2998
2999 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3000 we don't need to treat text as intangible. */
3001 if (EQ (XCAR (prop), Qmargin))
3002 {
3003 prop = XCDR (prop);
3004 if (!CONSP (prop))
3005 return 0;
3006
3007 prop = XCDR (prop);
3008 if (!CONSP (prop)
3009 || EQ (XCAR (prop), Qleft_margin)
3010 || EQ (XCAR (prop), Qright_margin))
3011 return 0;
3012 }
3013
3014 return CONSP (prop) && EQ (XCAR (prop), Qimage);
3015}
3016
3017
3018/* Check if PROP is a display property value whose text should be
3019 treated as intangible. */
3020
3021int
3022display_prop_intangible_p (prop)
3023 Lisp_Object prop;
3024{
3025 if (CONSP (prop)
3026 && CONSP (XCAR (prop))
3027 && !EQ (Qmargin, XCAR (XCAR (prop))))
3028 {
3029 /* A list of sub-properties. */
3030 while (CONSP (prop))
3031 {
3032 if (single_display_prop_intangible_p (XCAR (prop)))
3033 return 1;
3034 prop = XCDR (prop);
3035 }
3036 }
3037 else if (VECTORP (prop))
3038 {
3039 /* A vector of sub-properties. */
3040 int i;
a61b7058
GM
3041 for (i = 0; i < ASIZE (prop); ++i)
3042 if (single_display_prop_intangible_p (AREF (prop, i)))
06568bbf
GM
3043 return 1;
3044 }
3045 else
3046 return single_display_prop_intangible_p (prop);
3047
3048 return 0;
3049}
3050
74bd6d65
GM
3051
3052/* Return 1 if PROP is a display sub-property value containing STRING. */
3053
3054static int
3055single_display_prop_string_p (prop, string)
3056 Lisp_Object prop, string;
3057{
3058 extern Lisp_Object Qwhen, Qmargin;
3059
3060 if (EQ (string, prop))
3061 return 1;
3062
3063 /* Skip over `when FORM'. */
3064 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3065 {
3066 prop = XCDR (prop);
3067 if (!CONSP (prop))
3068 return 0;
3069 prop = XCDR (prop);
3070 }
3071
3072 if (CONSP (prop))
3073 /* Skip over `margin LOCATION'. */
3074 if (EQ (XCAR (prop), Qmargin))
3075 {
3076 prop = XCDR (prop);
3077 if (!CONSP (prop))
3078 return 0;
3079
3080 prop = XCDR (prop);
3081 if (!CONSP (prop))
3082 return 0;
3083 }
3084
3085 return CONSP (prop) && EQ (XCAR (prop), string);
3086}
3087
3088
3089/* Return 1 if STRING appears in the `display' property PROP. */
3090
3091static int
3092display_prop_string_p (prop, string)
3093 Lisp_Object prop, string;
3094{
3095 extern Lisp_Object Qwhen, Qmargin;
3096
3097 if (CONSP (prop)
3098 && CONSP (XCAR (prop))
3099 && !EQ (Qmargin, XCAR (XCAR (prop))))
3100 {
3101 /* A list of sub-properties. */
3102 while (CONSP (prop))
3103 {
3104 if (single_display_prop_string_p (XCAR (prop), string))
3105 return 1;
3106 prop = XCDR (prop);
3107 }
3108 }
3109 else if (VECTORP (prop))
3110 {
3111 /* A vector of sub-properties. */
3112 int i;
3113 for (i = 0; i < ASIZE (prop); ++i)
3114 if (single_display_prop_string_p (AREF (prop, i), string))
3115 return 1;
3116 }
3117 else
3118 return single_display_prop_string_p (prop, string);
3119
3120 return 0;
3121}
3122
3123
3124/* Determine from which buffer position in W's buffer STRING comes
3125 from. AROUND_CHARPOS is an approximate position where it could
3126 be from. Value is the buffer position or 0 if it couldn't be
3127 determined.
3128
3129 W's buffer must be current.
3130
3131 This function is necessary because we don't record buffer positions
3132 in glyphs generated from strings (to keep struct glyph small).
3133 This function may only use code that doesn't eval because it is
3134 called asynchronously from note_mouse_highlight. */
3135
3136int
3137string_buffer_position (w, string, around_charpos)
3138 struct window *w;
3139 Lisp_Object string;
3140 int around_charpos;
3141{
3142 Lisp_Object around = make_number (around_charpos);
3143 Lisp_Object limit, prop, pos;
3144 const int MAX_DISTANCE = 1000;
3145 int found = 0;
3146
d8731202 3147 pos = make_number (around_charpos);
74bd6d65
GM
3148 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
3149 while (!found && !EQ (pos, limit))
3150 {
3151 prop = Fget_char_property (pos, Qdisplay, Qnil);
3152 if (!NILP (prop) && display_prop_string_p (prop, string))
3153 found = 1;
3154 else
3155 pos = Fnext_single_property_change (pos, Qdisplay, Qnil, limit);
3156 }
3157
3158 if (!found)
3159 {
d8731202 3160 pos = make_number (around_charpos);
74bd6d65
GM
3161 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
3162 while (!found && !EQ (pos, limit))
3163 {
3164 prop = Fget_char_property (pos, Qdisplay, Qnil);
3165 if (!NILP (prop) && display_prop_string_p (prop, string))
3166 found = 1;
3167 else
3168 pos = Fprevious_single_property_change (pos, Qdisplay, Qnil,
3169 limit);
3170 }
3171 }
3172
3173 return found ? XINT (pos) : 0;
3174}
3175
3176
5f5c8ee5 3177\f
260a86a0
KH
3178/***********************************************************************
3179 `composition' property
3180 ***********************************************************************/
3181
3182/* Set up iterator IT from `composition' property at its current
3183 position. Called from handle_stop. */
3184
3185static enum prop_handled
3186handle_composition_prop (it)
3187 struct it *it;
3188{
3189 Lisp_Object prop, string;
3190 int pos, pos_byte, end;
3191 enum prop_handled handled = HANDLED_NORMALLY;
3192
3193 if (STRINGP (it->string))
3194 {
3195 pos = IT_STRING_CHARPOS (*it);
3196 pos_byte = IT_STRING_BYTEPOS (*it);
3197 string = it->string;
3198 }
3199 else
3200 {
3201 pos = IT_CHARPOS (*it);
3202 pos_byte = IT_BYTEPOS (*it);
3203 string = Qnil;
3204 }
3205
3206 /* If there's a valid composition and point is not inside of the
3207 composition (in the case that the composition is from the current
3208 buffer), draw a glyph composed from the composition components. */
3209 if (find_composition (pos, -1, &pos, &end, &prop, string)
3210 && COMPOSITION_VALID_P (pos, end, prop)
3211 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
3212 {
3213 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
3214
3215 if (id >= 0)
3216 {
3217 it->method = next_element_from_composition;
3218 it->cmp_id = id;
3219 it->cmp_len = COMPOSITION_LENGTH (prop);
3220 /* For a terminal, draw only the first character of the
3221 components. */
3222 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
3223 it->len = (STRINGP (it->string)
3224 ? string_char_to_byte (it->string, end)
3225 : CHAR_TO_BYTE (end)) - pos_byte;
3226 it->stop_charpos = end;
3227 handled = HANDLED_RETURN;
3228 }
3229 }
3230
3231 return handled;
3232}
3233
3234
3235\f
5f5c8ee5
GM
3236/***********************************************************************
3237 Overlay strings
3238 ***********************************************************************/
3239
3240/* The following structure is used to record overlay strings for
3241 later sorting in load_overlay_strings. */
3242
3243struct overlay_entry
3244{
2970b9be 3245 Lisp_Object overlay;
5f5c8ee5
GM
3246 Lisp_Object string;
3247 int priority;
3248 int after_string_p;
3249};
3250
3251
3252/* Set up iterator IT from overlay strings at its current position.
3253 Called from handle_stop. */
3254
3255static enum prop_handled
3256handle_overlay_change (it)
3257 struct it *it;
3258{
2970b9be
GM
3259 if (!STRINGP (it->string) && get_overlay_strings (it))
3260 return HANDLED_RECOMPUTE_PROPS;
5f5c8ee5 3261 else
2970b9be 3262 return HANDLED_NORMALLY;
5f5c8ee5
GM
3263}
3264
3265
3266/* Set up the next overlay string for delivery by IT, if there is an
3267 overlay string to deliver. Called by set_iterator_to_next when the
3268 end of the current overlay string is reached. If there are more
3269 overlay strings to display, IT->string and
3270 IT->current.overlay_string_index are set appropriately here.
3271 Otherwise IT->string is set to nil. */
3272
3273static void
3274next_overlay_string (it)
3275 struct it *it;
3276{
3277 ++it->current.overlay_string_index;
3278 if (it->current.overlay_string_index == it->n_overlay_strings)
3279 {
3280 /* No more overlay strings. Restore IT's settings to what
3281 they were before overlay strings were processed, and
3282 continue to deliver from current_buffer. */
3283 pop_it (it);
3284 xassert (it->stop_charpos >= BEGV
3285 && it->stop_charpos <= it->end_charpos);
3286 it->string = Qnil;
3287 it->current.overlay_string_index = -1;
3288 SET_TEXT_POS (it->current.string_pos, -1, -1);
3289 it->n_overlay_strings = 0;
3290 it->method = next_element_from_buffer;
2970b9be
GM
3291
3292 /* If we're at the end of the buffer, record that we have
3293 processed the overlay strings there already, so that
3294 next_element_from_buffer doesn't try it again. */
3295 if (IT_CHARPOS (*it) >= it->end_charpos)
3296 it->overlay_strings_at_end_processed_p = 1;
5f5c8ee5
GM
3297 }
3298 else
3299 {
3300 /* There are more overlay strings to process. If
3301 IT->current.overlay_string_index has advanced to a position
3302 where we must load IT->overlay_strings with more strings, do
3303 it. */
3304 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
3305
3306 if (it->current.overlay_string_index && i == 0)
3307 load_overlay_strings (it);
3308
3309 /* Initialize IT to deliver display elements from the overlay
3310 string. */
3311 it->string = it->overlay_strings[i];
3312 it->multibyte_p = STRING_MULTIBYTE (it->string);
3313 SET_TEXT_POS (it->current.string_pos, 0, 0);
3314 it->method = next_element_from_string;
3315 it->stop_charpos = 0;
3316 }
3317
3318 CHECK_IT (it);
3319}
3320
3321
3322/* Compare two overlay_entry structures E1 and E2. Used as a
3323 comparison function for qsort in load_overlay_strings. Overlay
3324 strings for the same position are sorted so that
3325
2970b9be
GM
3326 1. All after-strings come in front of before-strings, except
3327 when they come from the same overlay.
5f5c8ee5
GM
3328
3329 2. Within after-strings, strings are sorted so that overlay strings
3330 from overlays with higher priorities come first.
3331
3332 2. Within before-strings, strings are sorted so that overlay
3333 strings from overlays with higher priorities come last.
3334
3335 Value is analogous to strcmp. */
3336
3337
3338static int
3339compare_overlay_entries (e1, e2)
3340 void *e1, *e2;
3341{
3342 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
3343 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
3344 int result;
3345
3346 if (entry1->after_string_p != entry2->after_string_p)
2970b9be
GM
3347 {
3348 /* Let after-strings appear in front of before-strings if
3349 they come from different overlays. */
3350 if (EQ (entry1->overlay, entry2->overlay))
3351 result = entry1->after_string_p ? 1 : -1;
3352 else
3353 result = entry1->after_string_p ? -1 : 1;
3354 }
5f5c8ee5
GM
3355 else if (entry1->after_string_p)
3356 /* After-strings sorted in order of decreasing priority. */
3357 result = entry2->priority - entry1->priority;
3358 else
3359 /* Before-strings sorted in order of increasing priority. */
3360 result = entry1->priority - entry2->priority;
3361
3362 return result;
3363}
3364
3365
3366/* Load the vector IT->overlay_strings with overlay strings from IT's
3367 current buffer position. Set IT->n_overlays to the total number of
3368 overlay strings found.
3369
3370 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
3371 a time. On entry into load_overlay_strings,
3372 IT->current.overlay_string_index gives the number of overlay
3373 strings that have already been loaded by previous calls to this
3374 function.
3375
2970b9be
GM
3376 IT->add_overlay_start contains an additional overlay start
3377 position to consider for taking overlay strings from, if non-zero.
3378 This position comes into play when the overlay has an `invisible'
3379 property, and both before and after-strings. When we've skipped to
3380 the end of the overlay, because of its `invisible' property, we
3381 nevertheless want its before-string to appear.
3382 IT->add_overlay_start will contain the overlay start position
3383 in this case.
3384
5f5c8ee5
GM
3385 Overlay strings are sorted so that after-string strings come in
3386 front of before-string strings. Within before and after-strings,
3387 strings are sorted by overlay priority. See also function
3388 compare_overlay_entries. */
3389
3390static void
3391load_overlay_strings (it)
3392 struct it *it;
3393{
3394 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
cafafe0b 3395 Lisp_Object ov, overlay, window, str, invisible;
5f5c8ee5
GM
3396 int start, end;
3397 int size = 20;
cafafe0b 3398 int n = 0, i, j, invis_p;
5f5c8ee5
GM
3399 struct overlay_entry *entries
3400 = (struct overlay_entry *) alloca (size * sizeof *entries);
cafafe0b 3401 int charpos = IT_CHARPOS (*it);
5f5c8ee5
GM
3402
3403 /* Append the overlay string STRING of overlay OVERLAY to vector
3404 `entries' which has size `size' and currently contains `n'
3405 elements. AFTER_P non-zero means STRING is an after-string of
3406 OVERLAY. */
3407#define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
3408 do \
3409 { \
3410 Lisp_Object priority; \
3411 \
3412 if (n == size) \
3413 { \
3414 int new_size = 2 * size; \
3415 struct overlay_entry *old = entries; \
3416 entries = \
3417 (struct overlay_entry *) alloca (new_size \
3418 * sizeof *entries); \
3419 bcopy (old, entries, size * sizeof *entries); \
3420 size = new_size; \
3421 } \
3422 \
3423 entries[n].string = (STRING); \
2970b9be 3424 entries[n].overlay = (OVERLAY); \
5f5c8ee5 3425 priority = Foverlay_get ((OVERLAY), Qpriority); \
2970b9be 3426 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5f5c8ee5
GM
3427 entries[n].after_string_p = (AFTER_P); \
3428 ++n; \
3429 } \
3430 while (0)
3431
3432 /* Process overlay before the overlay center. */
2970b9be 3433 for (ov = current_buffer->overlays_before; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3434 {
9472f927 3435 overlay = XCAR (ov);
5f5c8ee5
GM
3436 xassert (OVERLAYP (overlay));
3437 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3438 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3439
cafafe0b 3440 if (end < charpos)
5f5c8ee5
GM
3441 break;
3442
3443 /* Skip this overlay if it doesn't start or end at IT's current
3444 position. */
cafafe0b 3445 if (end != charpos && start != charpos)
5f5c8ee5
GM
3446 continue;
3447
3448 /* Skip this overlay if it doesn't apply to IT->w. */
3449 window = Foverlay_get (overlay, Qwindow);
3450 if (WINDOWP (window) && XWINDOW (window) != it->w)
3451 continue;
3452
cafafe0b
GM
3453 /* If the text ``under'' the overlay is invisible, both before-
3454 and after-strings from this overlay are visible; start and
3455 end position are indistinguishable. */
3456 invisible = Foverlay_get (overlay, Qinvisible);
3457 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3458
5f5c8ee5 3459 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3460 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3461 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3462 && XSTRING (str)->size)
3463 RECORD_OVERLAY_STRING (overlay, str, 0);
3464
3465 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3466 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3467 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3468 && XSTRING (str)->size)
3469 RECORD_OVERLAY_STRING (overlay, str, 1);
3470 }
3471
3472 /* Process overlays after the overlay center. */
2970b9be 3473 for (ov = current_buffer->overlays_after; CONSP (ov); ov = XCDR (ov))
5f5c8ee5 3474 {
9472f927 3475 overlay = XCAR (ov);
5f5c8ee5
GM
3476 xassert (OVERLAYP (overlay));
3477 start = OVERLAY_POSITION (OVERLAY_START (overlay));
3478 end = OVERLAY_POSITION (OVERLAY_END (overlay));
3479
cafafe0b 3480 if (start > charpos)
5f5c8ee5
GM
3481 break;
3482
3483 /* Skip this overlay if it doesn't start or end at IT's current
3484 position. */
cafafe0b 3485 if (end != charpos && start != charpos)
5f5c8ee5
GM
3486 continue;
3487
3488 /* Skip this overlay if it doesn't apply to IT->w. */
3489 window = Foverlay_get (overlay, Qwindow);
3490 if (WINDOWP (window) && XWINDOW (window) != it->w)
3491 continue;
3492
cafafe0b
GM
3493 /* If the text ``under'' the overlay is invisible, it has a zero
3494 dimension, and both before- and after-strings apply. */
3495 invisible = Foverlay_get (overlay, Qinvisible);
3496 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
3497
5f5c8ee5 3498 /* If overlay has a non-empty before-string, record it. */
cafafe0b 3499 if ((start == charpos || (end == charpos && invis_p))
5f5c8ee5
GM
3500 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
3501 && XSTRING (str)->size)
3502 RECORD_OVERLAY_STRING (overlay, str, 0);
3503
3504 /* If overlay has a non-empty after-string, record it. */
cafafe0b 3505 if ((end == charpos || (start == charpos && invis_p))
5f5c8ee5
GM
3506 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
3507 && XSTRING (str)->size)
3508 RECORD_OVERLAY_STRING (overlay, str, 1);
3509 }
3510
3511#undef RECORD_OVERLAY_STRING
3512
3513 /* Sort entries. */
cafafe0b 3514 if (n > 1)
2970b9be 3515 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5f5c8ee5
GM
3516
3517 /* Record the total number of strings to process. */
3518 it->n_overlay_strings = n;
3519
3520 /* IT->current.overlay_string_index is the number of overlay strings
3521 that have already been consumed by IT. Copy some of the
3522 remaining overlay strings to IT->overlay_strings. */
3523 i = 0;
3524 j = it->current.overlay_string_index;
3525 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
3526 it->overlay_strings[i++] = entries[j++].string;
2970b9be 3527
5f5c8ee5
GM
3528 CHECK_IT (it);
3529}
3530
3531
3532/* Get the first chunk of overlay strings at IT's current buffer
3533 position. Value is non-zero if at least one overlay string was
3534 found. */
3535
3536static int
3537get_overlay_strings (it)
3538 struct it *it;
3539{
3540 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
3541 process. This fills IT->overlay_strings with strings, and sets
3542 IT->n_overlay_strings to the total number of strings to process.
3543 IT->pos.overlay_string_index has to be set temporarily to zero
3544 because load_overlay_strings needs this; it must be set to -1
3545 when no overlay strings are found because a zero value would
3546 indicate a position in the first overlay string. */
3547 it->current.overlay_string_index = 0;
3548 load_overlay_strings (it);
3549
3550 /* If we found overlay strings, set up IT to deliver display
3551 elements from the first one. Otherwise set up IT to deliver
3552 from current_buffer. */
3553 if (it->n_overlay_strings)
3554 {
3555 /* Make sure we know settings in current_buffer, so that we can
3556 restore meaningful values when we're done with the overlay
3557 strings. */
3558 compute_stop_pos (it);
3559 xassert (it->face_id >= 0);
3560
3561 /* Save IT's settings. They are restored after all overlay
3562 strings have been processed. */
3563 xassert (it->sp == 0);
3564 push_it (it);
3565
3566 /* Set up IT to deliver display elements from the first overlay
3567 string. */
3568 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3569 it->stop_charpos = 0;
3570 it->string = it->overlay_strings[0];
3571 it->multibyte_p = STRING_MULTIBYTE (it->string);
3572 xassert (STRINGP (it->string));
3573 it->method = next_element_from_string;
3574 }
3575 else
3576 {
3577 it->string = Qnil;
3578 it->current.overlay_string_index = -1;
3579 it->method = next_element_from_buffer;
3580 }
3581
3582 CHECK_IT (it);
3583
3584 /* Value is non-zero if we found at least one overlay string. */
3585 return STRINGP (it->string);
3586}
3587
3588
3589\f
3590/***********************************************************************
3591 Saving and restoring state
3592 ***********************************************************************/
3593
3594/* Save current settings of IT on IT->stack. Called, for example,
3595 before setting up IT for an overlay string, to be able to restore
3596 IT's settings to what they were after the overlay string has been
3597 processed. */
3598
3599static void
3600push_it (it)
3601 struct it *it;
3602{
3603 struct iterator_stack_entry *p;
3604
3605 xassert (it->sp < 2);
3606 p = it->stack + it->sp;
3607
3608 p->stop_charpos = it->stop_charpos;
3609 xassert (it->face_id >= 0);
3610 p->face_id = it->face_id;
3611 p->string = it->string;
3612 p->pos = it->current;
3613 p->end_charpos = it->end_charpos;
3614 p->string_nchars = it->string_nchars;
3615 p->area = it->area;
3616 p->multibyte_p = it->multibyte_p;
3617 p->space_width = it->space_width;
3618 p->font_height = it->font_height;
3619 p->voffset = it->voffset;
3620 p->string_from_display_prop_p = it->string_from_display_prop_p;
3621 ++it->sp;
3622}
3623
3624
3625/* Restore IT's settings from IT->stack. Called, for example, when no
3626 more overlay strings must be processed, and we return to delivering
3627 display elements from a buffer, or when the end of a string from a
3628 `display' property is reached and we return to delivering display
3629 elements from an overlay string, or from a buffer. */
3630
3631static void
3632pop_it (it)
3633 struct it *it;
3634{
3635 struct iterator_stack_entry *p;
3636
3637 xassert (it->sp > 0);
3638 --it->sp;
3639 p = it->stack + it->sp;
3640 it->stop_charpos = p->stop_charpos;
3641 it->face_id = p->face_id;
3642 it->string = p->string;
3643 it->current = p->pos;
3644 it->end_charpos = p->end_charpos;
3645 it->string_nchars = p->string_nchars;
3646 it->area = p->area;
3647 it->multibyte_p = p->multibyte_p;
3648 it->space_width = p->space_width;
3649 it->font_height = p->font_height;
3650 it->voffset = p->voffset;
3651 it->string_from_display_prop_p = p->string_from_display_prop_p;
3652}
3653
3654
3655\f
3656/***********************************************************************
3657 Moving over lines
3658 ***********************************************************************/
3659
3660/* Set IT's current position to the previous line start. */
3661
3662static void
3663back_to_previous_line_start (it)
3664 struct it *it;
3665{
3666 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
3667 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
3668}
3669
3670
cafafe0b
GM
3671/* Move IT to the next line start.
3672
3673 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
3674 we skipped over part of the text (as opposed to moving the iterator
3675 continuously over the text). Otherwise, don't change the value
3676 of *SKIPPED_P.
3677
3678 Newlines may come from buffer text, overlay strings, or strings
3679 displayed via the `display' property. That's the reason we can't
0fd37545
GM
3680 simply use find_next_newline_no_quit.
3681
3682 Note that this function may not skip over invisible text that is so
3683 because of text properties and immediately follows a newline. If
3684 it would, function reseat_at_next_visible_line_start, when called
3685 from set_iterator_to_next, would effectively make invisible
3686 characters following a newline part of the wrong glyph row, which
3687 leads to wrong cursor motion. */
5f5c8ee5 3688
cafafe0b
GM
3689static int
3690forward_to_next_line_start (it, skipped_p)
5f5c8ee5 3691 struct it *it;
cafafe0b 3692 int *skipped_p;
5f5c8ee5 3693{
54918e2b 3694 int old_selective, newline_found_p, n;
cafafe0b
GM
3695 const int MAX_NEWLINE_DISTANCE = 500;
3696
0fd37545
GM
3697 /* If already on a newline, just consume it to avoid unintended
3698 skipping over invisible text below. */
51695746
GM
3699 if (it->what == IT_CHARACTER
3700 && it->c == '\n'
3701 && CHARPOS (it->position) == IT_CHARPOS (*it))
0fd37545
GM
3702 {
3703 set_iterator_to_next (it, 0);
a77dc1ec 3704 it->c = 0;
0fd37545
GM
3705 return 1;
3706 }
3707
54918e2b 3708 /* Don't handle selective display in the following. It's (a)
0fd37545
GM
3709 unnecessary because it's done by the caller, and (b) leads to an
3710 infinite recursion because next_element_from_ellipsis indirectly
3711 calls this function. */
54918e2b
GM
3712 old_selective = it->selective;
3713 it->selective = 0;
3714
cafafe0b
GM
3715 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
3716 from buffer text. */
3aec8722
GM
3717 for (n = newline_found_p = 0;
3718 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
3719 n += STRINGP (it->string) ? 0 : 1)
cafafe0b 3720 {
8692ca92
GM
3721 if (!get_next_display_element (it))
3722 break;
d02f1cb8 3723 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
cafafe0b 3724 set_iterator_to_next (it, 0);
cafafe0b
GM
3725 }
3726
3727 /* If we didn't find a newline near enough, see if we can use a
3728 short-cut. */
3aec8722 3729 if (n == MAX_NEWLINE_DISTANCE)
cafafe0b
GM
3730 {
3731 int start = IT_CHARPOS (*it);
3732 int limit = find_next_newline_no_quit (start, 1);
3733 Lisp_Object pos;
3734
3735 xassert (!STRINGP (it->string));
3736
3737 /* If there isn't any `display' property in sight, and no
3738 overlays, we can just use the position of the newline in
3739 buffer text. */
3740 if (it->stop_charpos >= limit
3741 || ((pos = Fnext_single_property_change (make_number (start),
3742 Qdisplay,
3743 Qnil, make_number (limit)),
3744 NILP (pos))
3745 && next_overlay_change (start) == ZV))
3746 {
3747 IT_CHARPOS (*it) = limit;
3748 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
3749 *skipped_p = newline_found_p = 1;
3750 }
3751 else
3752 {
3753 while (get_next_display_element (it)
3754 && !newline_found_p)
3755 {
3756 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
3757 set_iterator_to_next (it, 0);
3758 }
3759 }
3760 }
3761
54918e2b 3762 it->selective = old_selective;
cafafe0b 3763 return newline_found_p;
5f5c8ee5
GM
3764}
3765
3766
3767/* Set IT's current position to the previous visible line start. Skip
3768 invisible text that is so either due to text properties or due to
3769 selective display. Caution: this does not change IT->current_x and
3770 IT->hpos. */
3771
3772static void
3773back_to_previous_visible_line_start (it)
3774 struct it *it;
3775{
3776 int visible_p = 0;
3777
3778 /* Go back one newline if not on BEGV already. */
3779 if (IT_CHARPOS (*it) > BEGV)
3780 back_to_previous_line_start (it);
3781
3782 /* Move over lines that are invisible because of selective display
3783 or text properties. */
3784 while (IT_CHARPOS (*it) > BEGV
3785 && !visible_p)
3786 {
3787 visible_p = 1;
3788
3789 /* If selective > 0, then lines indented more than that values
3790 are invisible. */
3791 if (it->selective > 0
3792 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
3793 it->selective))
3794 visible_p = 0;
5f5c8ee5
GM
3795 else
3796 {
3797 Lisp_Object prop;
3798
6fc556fd
KR
3799 prop = Fget_char_property (make_number (IT_CHARPOS (*it)),
3800 Qinvisible, it->window);
5f5c8ee5
GM
3801 if (TEXT_PROP_MEANS_INVISIBLE (prop))
3802 visible_p = 0;
3803 }
5f5c8ee5
GM
3804
3805 /* Back one more newline if the current one is invisible. */
3806 if (!visible_p)
3807 back_to_previous_line_start (it);
3808 }
3809
3810 xassert (IT_CHARPOS (*it) >= BEGV);
3811 xassert (IT_CHARPOS (*it) == BEGV
3812 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3813 CHECK_IT (it);
3814}
3815
3816
3817/* Reseat iterator IT at the previous visible line start. Skip
3818 invisible text that is so either due to text properties or due to
3819 selective display. At the end, update IT's overlay information,
3820 face information etc. */
3821
3822static void
3823reseat_at_previous_visible_line_start (it)
3824 struct it *it;
3825{
3826 back_to_previous_visible_line_start (it);
3827 reseat (it, it->current.pos, 1);
3828 CHECK_IT (it);
3829}
3830
3831
3832/* Reseat iterator IT on the next visible line start in the current
312246d1
GM
3833 buffer. ON_NEWLINE_P non-zero means position IT on the newline
3834 preceding the line start. Skip over invisible text that is so
3835 because of selective display. Compute faces, overlays etc at the
3836 new position. Note that this function does not skip over text that
3837 is invisible because of text properties. */
5f5c8ee5
GM
3838
3839static void
312246d1 3840reseat_at_next_visible_line_start (it, on_newline_p)
5f5c8ee5 3841 struct it *it;
312246d1 3842 int on_newline_p;
5f5c8ee5 3843{
cafafe0b
GM
3844 int newline_found_p, skipped_p = 0;
3845
3846 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3847
3848 /* Skip over lines that are invisible because they are indented
3849 more than the value of IT->selective. */
3850 if (it->selective > 0)
3851 while (IT_CHARPOS (*it) < ZV
a77dc1ec 3852 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
cafafe0b 3853 it->selective))
a77dc1ec
GM
3854 {
3855 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
3856 newline_found_p = forward_to_next_line_start (it, &skipped_p);
3857 }
cafafe0b
GM
3858
3859 /* Position on the newline if that's what's requested. */
3860 if (on_newline_p && newline_found_p)
5f5c8ee5 3861 {
cafafe0b 3862 if (STRINGP (it->string))
5f5c8ee5 3863 {
cafafe0b
GM
3864 if (IT_STRING_CHARPOS (*it) > 0)
3865 {
3866 --IT_STRING_CHARPOS (*it);
3867 --IT_STRING_BYTEPOS (*it);
3868 }
5f5c8ee5 3869 }
cafafe0b 3870 else if (IT_CHARPOS (*it) > BEGV)
312246d1
GM
3871 {
3872 --IT_CHARPOS (*it);
cafafe0b
GM
3873 --IT_BYTEPOS (*it);
3874 reseat (it, it->current.pos, 0);
312246d1 3875 }
5f5c8ee5 3876 }
cafafe0b
GM
3877 else if (skipped_p)
3878 reseat (it, it->current.pos, 0);
5f5c8ee5
GM
3879
3880 CHECK_IT (it);
3881}
3882
3883
3884\f
3885/***********************************************************************
3886 Changing an iterator's position
3887***********************************************************************/
3888
3889/* Change IT's current position to POS in current_buffer. If FORCE_P
3890 is non-zero, always check for text properties at the new position.
3891 Otherwise, text properties are only looked up if POS >=
3892 IT->check_charpos of a property. */
3893
3894static void
3895reseat (it, pos, force_p)
3896 struct it *it;
3897 struct text_pos pos;
3898 int force_p;
3899{
3900 int original_pos = IT_CHARPOS (*it);
3901
3902 reseat_1 (it, pos, 0);
3903
3904 /* Determine where to check text properties. Avoid doing it
3905 where possible because text property lookup is very expensive. */
3906 if (force_p
3907 || CHARPOS (pos) > it->stop_charpos
3908 || CHARPOS (pos) < original_pos)
3909 handle_stop (it);
3910
3911 CHECK_IT (it);
3912}
3913
3914
3915/* Change IT's buffer position to POS. SET_STOP_P non-zero means set
3916 IT->stop_pos to POS, also. */
3917
3918static void
3919reseat_1 (it, pos, set_stop_p)
3920 struct it *it;
3921 struct text_pos pos;
3922 int set_stop_p;
3923{
3924 /* Don't call this function when scanning a C string. */
3925 xassert (it->s == NULL);
3926
3927 /* POS must be a reasonable value. */
3928 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
3929
3930 it->current.pos = it->position = pos;
3931 XSETBUFFER (it->object, current_buffer);
78c663d8 3932 it->end_charpos = ZV;
5f5c8ee5
GM
3933 it->dpvec = NULL;
3934 it->current.dpvec_index = -1;
3935 it->current.overlay_string_index = -1;
3936 IT_STRING_CHARPOS (*it) = -1;
3937 IT_STRING_BYTEPOS (*it) = -1;
3938 it->string = Qnil;
3939 it->method = next_element_from_buffer;
3940 it->sp = 0;
4aad61f8 3941 it->face_before_selective_p = 0;
5f5c8ee5
GM
3942
3943 if (set_stop_p)
3944 it->stop_charpos = CHARPOS (pos);
3945}
3946
3947
3948/* Set up IT for displaying a string, starting at CHARPOS in window W.
3949 If S is non-null, it is a C string to iterate over. Otherwise,
3950 STRING gives a Lisp string to iterate over.
3951
3952 If PRECISION > 0, don't return more then PRECISION number of
3953 characters from the string.
3954
3955 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
3956 characters have been returned. FIELD_WIDTH < 0 means an infinite
3957 field width.
3958
3959 MULTIBYTE = 0 means disable processing of multibyte characters,
3960 MULTIBYTE > 0 means enable it,
3961 MULTIBYTE < 0 means use IT->multibyte_p.
3962
3963 IT must be initialized via a prior call to init_iterator before
3964 calling this function. */
3965
3966static void
3967reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
3968 struct it *it;
3969 unsigned char *s;
3970 Lisp_Object string;
3971 int charpos;
3972 int precision, field_width, multibyte;
3973{
3974 /* No region in strings. */
3975 it->region_beg_charpos = it->region_end_charpos = -1;
3976
3977 /* No text property checks performed by default, but see below. */
3978 it->stop_charpos = -1;
3979
3980 /* Set iterator position and end position. */
3981 bzero (&it->current, sizeof it->current);
3982 it->current.overlay_string_index = -1;
3983 it->current.dpvec_index = -1;
5f5c8ee5
GM
3984 xassert (charpos >= 0);
3985
3986 /* Use the setting of MULTIBYTE if specified. */
3987 if (multibyte >= 0)
3988 it->multibyte_p = multibyte > 0;
3989
3990 if (s == NULL)
3991 {
3992 xassert (STRINGP (string));
3993 it->string = string;
3994 it->s = NULL;
3995 it->end_charpos = it->string_nchars = XSTRING (string)->size;
3996 it->method = next_element_from_string;
3997 it->current.string_pos = string_pos (charpos, string);
3998 }
3999 else
4000 {
4001 it->s = s;
4002 it->string = Qnil;
4003
4004 /* Note that we use IT->current.pos, not it->current.string_pos,
4005 for displaying C strings. */
4006 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4007 if (it->multibyte_p)
4008 {
4009 it->current.pos = c_string_pos (charpos, s, 1);
4010 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4011 }
4012 else
4013 {
4014 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4015 it->end_charpos = it->string_nchars = strlen (s);
4016 }
4017
4018 it->method = next_element_from_c_string;
4019 }
4020
4021 /* PRECISION > 0 means don't return more than PRECISION characters
4022 from the string. */
4023 if (precision > 0 && it->end_charpos - charpos > precision)
4024 it->end_charpos = it->string_nchars = charpos + precision;
4025
4026 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4027 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4028 FIELD_WIDTH < 0 means infinite field width. This is useful for
4029 padding with `-' at the end of a mode line. */
4030 if (field_width < 0)
4031 field_width = INFINITY;
4032 if (field_width > it->end_charpos - charpos)
4033 it->end_charpos = charpos + field_width;
4034
4035 /* Use the standard display table for displaying strings. */
4036 if (DISP_TABLE_P (Vstandard_display_table))
4037 it->dp = XCHAR_TABLE (Vstandard_display_table);
4038
4039 it->stop_charpos = charpos;
4040 CHECK_IT (it);
4041}
4042
4043
4044\f
4045/***********************************************************************
4046 Iteration
4047 ***********************************************************************/
4048
4049/* Load IT's display element fields with information about the next
4050 display element from the current position of IT. Value is zero if
4051 end of buffer (or C string) is reached. */
4052
4053int
4054get_next_display_element (it)
4055 struct it *it;
4056{
4057 /* Non-zero means that we found an display element. Zero means that
4058 we hit the end of what we iterate over. Performance note: the
4059 function pointer `method' used here turns out to be faster than
4060 using a sequence of if-statements. */
4061 int success_p = (*it->method) (it);
5f5c8ee5
GM
4062
4063 if (it->what == IT_CHARACTER)
4064 {
4065 /* Map via display table or translate control characters.
4066 IT->c, IT->len etc. have been set to the next character by
4067 the function call above. If we have a display table, and it
4068 contains an entry for IT->c, translate it. Don't do this if
4069 IT->c itself comes from a display table, otherwise we could
4070 end up in an infinite recursion. (An alternative could be to
4071 count the recursion depth of this function and signal an
4072 error when a certain maximum depth is reached.) Is it worth
4073 it? */
4074 if (success_p && it->dpvec == NULL)
4075 {
4076 Lisp_Object dv;
4077
4078 if (it->dp
4079 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
4080 VECTORP (dv)))
4081 {
4082 struct Lisp_Vector *v = XVECTOR (dv);
4083
4084 /* Return the first character from the display table
4085 entry, if not empty. If empty, don't display the
4086 current character. */
4087 if (v->size)
4088 {
4089 it->dpvec_char_len = it->len;
4090 it->dpvec = v->contents;
4091 it->dpend = v->contents + v->size;
4092 it->current.dpvec_index = 0;
4093 it->method = next_element_from_display_vector;
7eee47cc
GM
4094 success_p = get_next_display_element (it);
4095 }
4096 else
4097 {
4098 set_iterator_to_next (it, 0);
4099 success_p = get_next_display_element (it);
5f5c8ee5 4100 }
5f5c8ee5
GM
4101 }
4102
4103 /* Translate control characters into `\003' or `^C' form.
4104 Control characters coming from a display table entry are
4105 currently not translated because we use IT->dpvec to hold
4106 the translation. This could easily be changed but I
197516c2
KH
4107 don't believe that it is worth doing.
4108
4109 Non-printable multibyte characters are also translated
4110 octal form. */
5f5c8ee5
GM
4111 else if ((it->c < ' '
4112 && (it->area != TEXT_AREA
c6e89d6c 4113 || (it->c != '\n' && it->c != '\t')))
54c85a23 4114 || (it->c >= 127
197516c2
KH
4115 && it->len == 1)
4116 || !CHAR_PRINTABLE_P (it->c))
5f5c8ee5
GM
4117 {
4118 /* IT->c is a control character which must be displayed
4119 either as '\003' or as `^C' where the '\\' and '^'
4120 can be defined in the display table. Fill
4121 IT->ctl_chars with glyphs for what we have to
4122 display. Then, set IT->dpvec to these glyphs. */
4123 GLYPH g;
4124
54c85a23 4125 if (it->c < 128 && it->ctl_arrow_p)
5f5c8ee5
GM
4126 {
4127 /* Set IT->ctl_chars[0] to the glyph for `^'. */
4128 if (it->dp
4129 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
4130 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
4131 g = XINT (DISP_CTRL_GLYPH (it->dp));
4132 else
4133 g = FAST_MAKE_GLYPH ('^', 0);
4134 XSETINT (it->ctl_chars[0], g);
4135
4136 g = FAST_MAKE_GLYPH (it->c ^ 0100, 0);
4137 XSETINT (it->ctl_chars[1], g);
4138
4139 /* Set up IT->dpvec and return first character from it. */
4140 it->dpvec_char_len = it->len;
4141 it->dpvec = it->ctl_chars;
4142 it->dpend = it->dpvec + 2;
4143 it->current.dpvec_index = 0;
4144 it->method = next_element_from_display_vector;
4145 get_next_display_element (it);
4146 }
4147 else
4148 {
260a86a0 4149 unsigned char str[MAX_MULTIBYTE_LENGTH];
c5924f47 4150 int len;
197516c2
KH
4151 int i;
4152 GLYPH escape_glyph;
4153
5f5c8ee5
GM
4154 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
4155 if (it->dp
4156 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
4157 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
197516c2 4158 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5f5c8ee5 4159 else
197516c2
KH
4160 escape_glyph = FAST_MAKE_GLYPH ('\\', 0);
4161
c5924f47
KH
4162 if (SINGLE_BYTE_CHAR_P (it->c))
4163 str[0] = it->c, len = 1;
4164 else
4165 len = CHAR_STRING (it->c, str);
4166
197516c2
KH
4167 for (i = 0; i < len; i++)
4168 {
4169 XSETINT (it->ctl_chars[i * 4], escape_glyph);
4170 /* Insert three more glyphs into IT->ctl_chars for
4171 the octal display of the character. */
4172 g = FAST_MAKE_GLYPH (((str[i] >> 6) & 7) + '0', 0);
4173 XSETINT (it->ctl_chars[i * 4 + 1], g);
4174 g = FAST_MAKE_GLYPH (((str[i] >> 3) & 7) + '0', 0);
4175 XSETINT (it->ctl_chars[i * 4 + 2], g);
4176 g = FAST_MAKE_GLYPH ((str[i] & 7) + '0', 0);
4177 XSETINT (it->ctl_chars[i * 4 + 3], g);
4178 }
5f5c8ee5
GM
4179
4180 /* Set up IT->dpvec and return the first character
4181 from it. */
4182 it->dpvec_char_len = it->len;
4183 it->dpvec = it->ctl_chars;
197516c2 4184 it->dpend = it->dpvec + len * 4;
5f5c8ee5
GM
4185 it->current.dpvec_index = 0;
4186 it->method = next_element_from_display_vector;
4187 get_next_display_element (it);
4188 }
4189 }
4190 }
4191
980806b6
KH
4192 /* Adjust face id for a multibyte character. There are no
4193 multibyte character in unibyte text. */
5f5c8ee5
GM
4194 if (it->multibyte_p
4195 && success_p
980806b6 4196 && FRAME_WINDOW_P (it->f))
5f5c8ee5 4197 {
980806b6
KH
4198 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4199 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5f5c8ee5
GM
4200 }
4201 }
4202
4203 /* Is this character the last one of a run of characters with
4204 box? If yes, set IT->end_of_box_run_p to 1. */
4205 if (it->face_box_p
4206 && it->s == NULL)
4207 {
4208 int face_id;
4209 struct face *face;
4210
4211 it->end_of_box_run_p
4212 = ((face_id = face_after_it_pos (it),
4213 face_id != it->face_id)
4214 && (face = FACE_FROM_ID (it->f, face_id),
4215 face->box == FACE_NO_BOX));
4216 }
4217
4218 /* Value is 0 if end of buffer or string reached. */
4219 return success_p;
4220}
4221
4222
4223/* Move IT to the next display element.
4224
cafafe0b
GM
4225 RESEAT_P non-zero means if called on a newline in buffer text,
4226 skip to the next visible line start.
4227
5f5c8ee5
GM
4228 Functions get_next_display_element and set_iterator_to_next are
4229 separate because I find this arrangement easier to handle than a
4230 get_next_display_element function that also increments IT's
4231 position. The way it is we can first look at an iterator's current
4232 display element, decide whether it fits on a line, and if it does,
4233 increment the iterator position. The other way around we probably
4234 would either need a flag indicating whether the iterator has to be
4235 incremented the next time, or we would have to implement a
4236 decrement position function which would not be easy to write. */
4237
4238void
cafafe0b 4239set_iterator_to_next (it, reseat_p)
5f5c8ee5 4240 struct it *it;
cafafe0b 4241 int reseat_p;
5f5c8ee5 4242{
483de32b
GM
4243 /* Reset flags indicating start and end of a sequence of characters
4244 with box. Reset them at the start of this function because
4245 moving the iterator to a new position might set them. */
4246 it->start_of_box_run_p = it->end_of_box_run_p = 0;
4247
5f5c8ee5
GM
4248 if (it->method == next_element_from_buffer)
4249 {
4250 /* The current display element of IT is a character from
4251 current_buffer. Advance in the buffer, and maybe skip over
4252 invisible lines that are so because of selective display. */
cafafe0b 4253 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
312246d1 4254 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
4255 else
4256 {
4257 xassert (it->len != 0);
4258 IT_BYTEPOS (*it) += it->len;
4259 IT_CHARPOS (*it) += 1;
4260 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
4261 }
4262 }
260a86a0
KH
4263 else if (it->method == next_element_from_composition)
4264 {
4265 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
4266 if (STRINGP (it->string))
4267 {
4268 IT_STRING_BYTEPOS (*it) += it->len;
4269 IT_STRING_CHARPOS (*it) += it->cmp_len;
4270 it->method = next_element_from_string;
4271 goto consider_string_end;
4272 }
4273 else
4274 {
4275 IT_BYTEPOS (*it) += it->len;
4276 IT_CHARPOS (*it) += it->cmp_len;
4277 it->method = next_element_from_buffer;
4278 }
4279 }
5f5c8ee5
GM
4280 else if (it->method == next_element_from_c_string)
4281 {
4282 /* Current display element of IT is from a C string. */
4283 IT_BYTEPOS (*it) += it->len;
4284 IT_CHARPOS (*it) += 1;
4285 }
4286 else if (it->method == next_element_from_display_vector)
4287 {
4288 /* Current display element of IT is from a display table entry.
4289 Advance in the display table definition. Reset it to null if
4290 end reached, and continue with characters from buffers/
4291 strings. */
4292 ++it->current.dpvec_index;
286bcbc9 4293
980806b6
KH
4294 /* Restore face of the iterator to what they were before the
4295 display vector entry (these entries may contain faces). */
5f5c8ee5 4296 it->face_id = it->saved_face_id;
286bcbc9 4297
5f5c8ee5
GM
4298 if (it->dpvec + it->current.dpvec_index == it->dpend)
4299 {
4300 if (it->s)
4301 it->method = next_element_from_c_string;
4302 else if (STRINGP (it->string))
4303 it->method = next_element_from_string;
4304 else
4305 it->method = next_element_from_buffer;
4306
4307 it->dpvec = NULL;
4308 it->current.dpvec_index = -1;
4309
312246d1
GM
4310 /* Skip over characters which were displayed via IT->dpvec. */
4311 if (it->dpvec_char_len < 0)
4312 reseat_at_next_visible_line_start (it, 1);
4313 else if (it->dpvec_char_len > 0)
5f5c8ee5
GM
4314 {
4315 it->len = it->dpvec_char_len;
cafafe0b 4316 set_iterator_to_next (it, reseat_p);
5f5c8ee5
GM
4317 }
4318 }
4319 }
4320 else if (it->method == next_element_from_string)
4321 {
4322 /* Current display element is a character from a Lisp string. */
4323 xassert (it->s == NULL && STRINGP (it->string));
4324 IT_STRING_BYTEPOS (*it) += it->len;
4325 IT_STRING_CHARPOS (*it) += 1;
4326
4327 consider_string_end:
4328
4329 if (it->current.overlay_string_index >= 0)
4330 {
4331 /* IT->string is an overlay string. Advance to the
4332 next, if there is one. */
4333 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4334 next_overlay_string (it);
4335 }
4336 else
4337 {
4338 /* IT->string is not an overlay string. If we reached
4339 its end, and there is something on IT->stack, proceed
4340 with what is on the stack. This can be either another
4341 string, this time an overlay string, or a buffer. */
4342 if (IT_STRING_CHARPOS (*it) == XSTRING (it->string)->size
4343 && it->sp > 0)
4344 {
4345 pop_it (it);
4346 if (!STRINGP (it->string))
4347 it->method = next_element_from_buffer;
4348 }
4349 }
4350 }
4351 else if (it->method == next_element_from_image
4352 || it->method == next_element_from_stretch)
4353 {
4354 /* The position etc with which we have to proceed are on
4355 the stack. The position may be at the end of a string,
4356 if the `display' property takes up the whole string. */
4357 pop_it (it);
4358 it->image_id = 0;
4359 if (STRINGP (it->string))
4360 {
4361 it->method = next_element_from_string;
4362 goto consider_string_end;
4363 }
4364 else
4365 it->method = next_element_from_buffer;
4366 }
4367 else
4368 /* There are no other methods defined, so this should be a bug. */
4369 abort ();
4370
5f5c8ee5
GM
4371 xassert (it->method != next_element_from_string
4372 || (STRINGP (it->string)
4373 && IT_STRING_CHARPOS (*it) >= 0));
4374}
4375
4376
4377/* Load IT's display element fields with information about the next
4378 display element which comes from a display table entry or from the
4379 result of translating a control character to one of the forms `^C'
4380 or `\003'. IT->dpvec holds the glyphs to return as characters. */
4381
4382static int
4383next_element_from_display_vector (it)
4384 struct it *it;
4385{
4386 /* Precondition. */
4387 xassert (it->dpvec && it->current.dpvec_index >= 0);
4388
4389 /* Remember the current face id in case glyphs specify faces.
4390 IT's face is restored in set_iterator_to_next. */
4391 it->saved_face_id = it->face_id;
4392
4393 if (INTEGERP (*it->dpvec)
4394 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
4395 {
4396 int lface_id;
4397 GLYPH g;
4398
4399 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
4400 it->c = FAST_GLYPH_CHAR (g);
c5924f47 4401 it->len = CHAR_BYTES (it->c);
5f5c8ee5
GM
4402
4403 /* The entry may contain a face id to use. Such a face id is
4404 the id of a Lisp face, not a realized face. A face id of
969065c3 4405 zero means no face is specified. */
5f5c8ee5
GM
4406 lface_id = FAST_GLYPH_FACE (g);
4407 if (lface_id)
4408 {
969065c3 4409 /* The function returns -1 if lface_id is invalid. */
5f5c8ee5
GM
4410 int face_id = ascii_face_of_lisp_face (it->f, lface_id);
4411 if (face_id >= 0)
969065c3 4412 it->face_id = face_id;
5f5c8ee5
GM
4413 }
4414 }
4415 else
4416 /* Display table entry is invalid. Return a space. */
4417 it->c = ' ', it->len = 1;
4418
4419 /* Don't change position and object of the iterator here. They are
4420 still the values of the character that had this display table
4421 entry or was translated, and that's what we want. */
4422 it->what = IT_CHARACTER;
4423 return 1;
4424}
4425
4426
4427/* Load IT with the next display element from Lisp string IT->string.
4428 IT->current.string_pos is the current position within the string.
4429 If IT->current.overlay_string_index >= 0, the Lisp string is an
4430 overlay string. */
4431
4432static int
4433next_element_from_string (it)
4434 struct it *it;
4435{
4436 struct text_pos position;
4437
4438 xassert (STRINGP (it->string));
4439 xassert (IT_STRING_CHARPOS (*it) >= 0);
4440 position = it->current.string_pos;
4441
4442 /* Time to check for invisible text? */
4443 if (IT_STRING_CHARPOS (*it) < it->end_charpos
4444 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
4445 {
4446 handle_stop (it);
4447
4448 /* Since a handler may have changed IT->method, we must
4449 recurse here. */
4450 return get_next_display_element (it);
4451 }
4452
4453 if (it->current.overlay_string_index >= 0)
4454 {
4455 /* Get the next character from an overlay string. In overlay
4456 strings, There is no field width or padding with spaces to
4457 do. */
4458 if (IT_STRING_CHARPOS (*it) >= XSTRING (it->string)->size)
4459 {
4460 it->what = IT_EOB;
4461 return 0;
4462 }
4463 else if (STRING_MULTIBYTE (it->string))
4464 {
4465 int remaining = (STRING_BYTES (XSTRING (it->string))
4466 - IT_STRING_BYTEPOS (*it));
4467 unsigned char *s = (XSTRING (it->string)->data
4468 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4469 it->c = string_char_and_length (s, remaining, &it->len);
5f5c8ee5
GM
4470 }
4471 else
4472 {
4473 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4474 it->len = 1;
4475 }
4476 }
4477 else
4478 {
4479 /* Get the next character from a Lisp string that is not an
4480 overlay string. Such strings come from the mode line, for
4481 example. We may have to pad with spaces, or truncate the
4482 string. See also next_element_from_c_string. */
4483 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
4484 {
4485 it->what = IT_EOB;
4486 return 0;
4487 }
4488 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
4489 {
4490 /* Pad with spaces. */
4491 it->c = ' ', it->len = 1;
4492 CHARPOS (position) = BYTEPOS (position) = -1;
4493 }
4494 else if (STRING_MULTIBYTE (it->string))
4495 {
4496 int maxlen = (STRING_BYTES (XSTRING (it->string))
4497 - IT_STRING_BYTEPOS (*it));
4498 unsigned char *s = (XSTRING (it->string)->data
4499 + IT_STRING_BYTEPOS (*it));
4fdb80f2 4500 it->c = string_char_and_length (s, maxlen, &it->len);
5f5c8ee5
GM
4501 }
4502 else
4503 {
4504 it->c = XSTRING (it->string)->data[IT_STRING_BYTEPOS (*it)];
4505 it->len = 1;
4506 }
4507 }
4508
4509 /* Record what we have and where it came from. Note that we store a
4510 buffer position in IT->position although it could arguably be a
4511 string position. */
4512 it->what = IT_CHARACTER;
4513 it->object = it->string;
4514 it->position = position;
4515 return 1;
4516}
4517
4518
4519/* Load IT with next display element from C string IT->s.
4520 IT->string_nchars is the maximum number of characters to return
4521 from the string. IT->end_charpos may be greater than
4522 IT->string_nchars when this function is called, in which case we
4523 may have to return padding spaces. Value is zero if end of string
4524 reached, including padding spaces. */
4525
4526static int
4527next_element_from_c_string (it)
4528 struct it *it;
4529{
4530 int success_p = 1;
4531
4532 xassert (it->s);
4533 it->what = IT_CHARACTER;
4534 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
4535 it->object = Qnil;
4536
4537 /* IT's position can be greater IT->string_nchars in case a field
4538 width or precision has been specified when the iterator was
4539 initialized. */
4540 if (IT_CHARPOS (*it) >= it->end_charpos)
4541 {
4542 /* End of the game. */
4543 it->what = IT_EOB;
4544 success_p = 0;
4545 }
4546 else if (IT_CHARPOS (*it) >= it->string_nchars)
4547 {
4548 /* Pad with spaces. */
4549 it->c = ' ', it->len = 1;
4550 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
4551 }
4552 else if (it->multibyte_p)
4553 {
4554 /* Implementation note: The calls to strlen apparently aren't a
4555 performance problem because there is no noticeable performance
4556 difference between Emacs running in unibyte or multibyte mode. */
4557 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
4fdb80f2
GM
4558 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
4559 maxlen, &it->len);
5f5c8ee5
GM
4560 }
4561 else
4562 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
4563
4564 return success_p;
4565}
4566
4567
4568/* Set up IT to return characters from an ellipsis, if appropriate.
4569 The definition of the ellipsis glyphs may come from a display table
4570 entry. This function Fills IT with the first glyph from the
4571 ellipsis if an ellipsis is to be displayed. */
4572
13f19968 4573static int
5f5c8ee5
GM
4574next_element_from_ellipsis (it)
4575 struct it *it;
4576{
13f19968 4577 if (it->selective_display_ellipsis_p)
5f5c8ee5 4578 {
13f19968
GM
4579 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4580 {
4581 /* Use the display table definition for `...'. Invalid glyphs
4582 will be handled by the method returning elements from dpvec. */
4583 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4584 it->dpvec_char_len = it->len;
4585 it->dpvec = v->contents;
4586 it->dpend = v->contents + v->size;
4587 it->current.dpvec_index = 0;
4588 it->method = next_element_from_display_vector;
4589 }
4590 else
4591 {
4592 /* Use default `...' which is stored in default_invis_vector. */
4593 it->dpvec_char_len = it->len;
4594 it->dpvec = default_invis_vector;
4595 it->dpend = default_invis_vector + 3;
4596 it->current.dpvec_index = 0;
4597 it->method = next_element_from_display_vector;
4598 }
5f5c8ee5 4599 }
13f19968 4600 else
54918e2b 4601 {
4aad61f8
GM
4602 /* The face at the current position may be different from the
4603 face we find after the invisible text. Remember what it
4604 was in IT->saved_face_id, and signal that it's there by
4605 setting face_before_selective_p. */
4606 it->saved_face_id = it->face_id;
54918e2b
GM
4607 it->method = next_element_from_buffer;
4608 reseat_at_next_visible_line_start (it, 1);
4aad61f8 4609 it->face_before_selective_p = 1;
54918e2b 4610 }
13f19968
GM
4611
4612 return get_next_display_element (it);
5f5c8ee5
GM
4613}
4614
4615
4616/* Deliver an image display element. The iterator IT is already
4617 filled with image information (done in handle_display_prop). Value
4618 is always 1. */
4619
4620
4621static int
4622next_element_from_image (it)
4623 struct it *it;
4624{
4625 it->what = IT_IMAGE;
4626 return 1;
4627}
4628
4629
4630/* Fill iterator IT with next display element from a stretch glyph
4631 property. IT->object is the value of the text property. Value is
4632 always 1. */
4633
4634static int
4635next_element_from_stretch (it)
4636 struct it *it;
4637{
4638 it->what = IT_STRETCH;
4639 return 1;
4640}
4641
4642
4643/* Load IT with the next display element from current_buffer. Value
4644 is zero if end of buffer reached. IT->stop_charpos is the next
4645 position at which to stop and check for text properties or buffer
4646 end. */
4647
4648static int
4649next_element_from_buffer (it)
4650 struct it *it;
4651{
4652 int success_p = 1;
4653
4654 /* Check this assumption, otherwise, we would never enter the
4655 if-statement, below. */
4656 xassert (IT_CHARPOS (*it) >= BEGV
4657 && IT_CHARPOS (*it) <= it->stop_charpos);
4658
4659 if (IT_CHARPOS (*it) >= it->stop_charpos)
4660 {
4661 if (IT_CHARPOS (*it) >= it->end_charpos)
4662 {
4663 int overlay_strings_follow_p;
4664
4665 /* End of the game, except when overlay strings follow that
4666 haven't been returned yet. */
4667 if (it->overlay_strings_at_end_processed_p)
4668 overlay_strings_follow_p = 0;
4669 else
4670 {
4671 it->overlay_strings_at_end_processed_p = 1;
c880678e 4672 overlay_strings_follow_p = get_overlay_strings (it);
5f5c8ee5
GM
4673 }
4674
4675 if (overlay_strings_follow_p)
4676 success_p = get_next_display_element (it);
4677 else
4678 {
4679 it->what = IT_EOB;
4680 it->position = it->current.pos;
4681 success_p = 0;
4682 }
4683 }
4684 else
4685 {
4686 handle_stop (it);
4687 return get_next_display_element (it);
4688 }
4689 }
4690 else
4691 {
4692 /* No face changes, overlays etc. in sight, so just return a
4693 character from current_buffer. */
4694 unsigned char *p;
4695
4696 /* Maybe run the redisplay end trigger hook. Performance note:
4697 This doesn't seem to cost measurable time. */
4698 if (it->redisplay_end_trigger_charpos
4699 && it->glyph_row
4700 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
4701 run_redisplay_end_trigger_hook (it);
4702
4703 /* Get the next character, maybe multibyte. */
4704 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
260a86a0 4705 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5f5c8ee5
GM
4706 {
4707 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
4708 - IT_BYTEPOS (*it));
4fdb80f2 4709 it->c = string_char_and_length (p, maxlen, &it->len);
5f5c8ee5
GM
4710 }
4711 else
4712 it->c = *p, it->len = 1;
4713
4714 /* Record what we have and where it came from. */
4715 it->what = IT_CHARACTER;;
4716 it->object = it->w->buffer;
4717 it->position = it->current.pos;
4718
4719 /* Normally we return the character found above, except when we
4720 really want to return an ellipsis for selective display. */
4721 if (it->selective)
4722 {
4723 if (it->c == '\n')
4724 {
4725 /* A value of selective > 0 means hide lines indented more
4726 than that number of columns. */
4727 if (it->selective > 0
4728 && IT_CHARPOS (*it) + 1 < ZV
4729 && indented_beyond_p (IT_CHARPOS (*it) + 1,
4730 IT_BYTEPOS (*it) + 1,
4731 it->selective))
312246d1 4732 {
13f19968 4733 success_p = next_element_from_ellipsis (it);
312246d1
GM
4734 it->dpvec_char_len = -1;
4735 }
5f5c8ee5
GM
4736 }
4737 else if (it->c == '\r' && it->selective == -1)
4738 {
4739 /* A value of selective == -1 means that everything from the
4740 CR to the end of the line is invisible, with maybe an
4741 ellipsis displayed for it. */
13f19968 4742 success_p = next_element_from_ellipsis (it);
312246d1 4743 it->dpvec_char_len = -1;
5f5c8ee5
GM
4744 }
4745 }
4746 }
4747
4748 /* Value is zero if end of buffer reached. */
c880678e 4749 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5f5c8ee5
GM
4750 return success_p;
4751}
4752
4753
4754/* Run the redisplay end trigger hook for IT. */
4755
4756static void
4757run_redisplay_end_trigger_hook (it)
4758 struct it *it;
4759{
4760 Lisp_Object args[3];
4761
4762 /* IT->glyph_row should be non-null, i.e. we should be actually
4763 displaying something, or otherwise we should not run the hook. */
4764 xassert (it->glyph_row);
4765
4766 /* Set up hook arguments. */
4767 args[0] = Qredisplay_end_trigger_functions;
4768 args[1] = it->window;
4769 XSETINT (args[2], it->redisplay_end_trigger_charpos);
4770 it->redisplay_end_trigger_charpos = 0;
4771
4772 /* Since we are *trying* to run these functions, don't try to run
4773 them again, even if they get an error. */
4774 it->w->redisplay_end_trigger = Qnil;
4775 Frun_hook_with_args (3, args);
4776
4777 /* Notice if it changed the face of the character we are on. */
4778 handle_face_prop (it);
4779}
4780
4781
260a86a0
KH
4782/* Deliver a composition display element. The iterator IT is already
4783 filled with composition information (done in
4784 handle_composition_prop). Value is always 1. */
4785
4786static int
4787next_element_from_composition (it)
4788 struct it *it;
4789{
4790 it->what = IT_COMPOSITION;
4791 it->position = (STRINGP (it->string)
4792 ? it->current.string_pos
4793 : it->current.pos);
4794 return 1;
4795}
4796
4797
5f5c8ee5
GM
4798\f
4799/***********************************************************************
4800 Moving an iterator without producing glyphs
4801 ***********************************************************************/
4802
4803/* Move iterator IT to a specified buffer or X position within one
4804 line on the display without producing glyphs.
4805
4806 Begin to skip at IT's current position. Skip to TO_CHARPOS or TO_X
4807 whichever is reached first.
4808
4809 TO_CHARPOS <= 0 means no TO_CHARPOS is specified.
4810
4811 TO_X < 0 means that no TO_X is specified. TO_X is normally a value
4812 0 <= TO_X <= IT->last_visible_x. This means in particular, that
4813 TO_X includes the amount by which a window is horizontally
4814 scrolled.
4815
4816 Value is
4817
4818 MOVE_POS_MATCH_OR_ZV
4819 - when TO_POS or ZV was reached.
4820
4821 MOVE_X_REACHED
4822 -when TO_X was reached before TO_POS or ZV were reached.
4823
4824 MOVE_LINE_CONTINUED
4825 - when we reached the end of the display area and the line must
4826 be continued.
4827
4828 MOVE_LINE_TRUNCATED
4829 - when we reached the end of the display area and the line is
4830 truncated.
4831
4832 MOVE_NEWLINE_OR_CR
4833 - when we stopped at a line end, i.e. a newline or a CR and selective
4834 display is on. */
4835
701552dd 4836static enum move_it_result
5f5c8ee5
GM
4837move_it_in_display_line_to (it, to_charpos, to_x, op)
4838 struct it *it;
4839 int to_charpos, to_x, op;
4840{
4841 enum move_it_result result = MOVE_UNDEFINED;
4842 struct glyph_row *saved_glyph_row;
4843
4844 /* Don't produce glyphs in produce_glyphs. */
4845 saved_glyph_row = it->glyph_row;
4846 it->glyph_row = NULL;
4847
5f5c8ee5
GM
4848 while (1)
4849 {
ae26e27d 4850 int x, i, ascent = 0, descent = 0;
5f5c8ee5
GM
4851
4852 /* Stop when ZV or TO_CHARPOS reached. */
4853 if (!get_next_display_element (it)
4854 || ((op & MOVE_TO_POS) != 0
4855 && BUFFERP (it->object)
4856 && IT_CHARPOS (*it) >= to_charpos))
4857 {
4858 result = MOVE_POS_MATCH_OR_ZV;
4859 break;
4860 }
4861
4862 /* The call to produce_glyphs will get the metrics of the
4863 display element IT is loaded with. We record in x the
4864 x-position before this display element in case it does not
4865 fit on the line. */
4866 x = it->current_x;
47589c8c
GM
4867
4868 /* Remember the line height so far in case the next element doesn't
4869 fit on the line. */
4870 if (!it->truncate_lines_p)
4871 {
4872 ascent = it->max_ascent;
4873 descent = it->max_descent;
4874 }
4875
5f5c8ee5
GM
4876 PRODUCE_GLYPHS (it);
4877
4878 if (it->area != TEXT_AREA)
4879 {
cafafe0b 4880 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4881 continue;
4882 }
4883
4884 /* The number of glyphs we get back in IT->nglyphs will normally
4885 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
4886 character on a terminal frame, or (iii) a line end. For the
4887 second case, IT->nglyphs - 1 padding glyphs will be present
4888 (on X frames, there is only one glyph produced for a
4889 composite character.
4890
4891 The behavior implemented below means, for continuation lines,
4892 that as many spaces of a TAB as fit on the current line are
4893 displayed there. For terminal frames, as many glyphs of a
4894 multi-glyph character are displayed in the current line, too.
4895 This is what the old redisplay code did, and we keep it that
4896 way. Under X, the whole shape of a complex character must
4897 fit on the line or it will be completely displayed in the
4898 next line.
4899
4900 Note that both for tabs and padding glyphs, all glyphs have
4901 the same width. */
4902 if (it->nglyphs)
4903 {
4904 /* More than one glyph or glyph doesn't fit on line. All
4905 glyphs have the same width. */
4906 int single_glyph_width = it->pixel_width / it->nglyphs;
4907 int new_x;
4908
4909 for (i = 0; i < it->nglyphs; ++i, x = new_x)
4910 {
4911 new_x = x + single_glyph_width;
4912
4913 /* We want to leave anything reaching TO_X to the caller. */
4914 if ((op & MOVE_TO_X) && new_x > to_x)
4915 {
4916 it->current_x = x;
4917 result = MOVE_X_REACHED;
4918 break;
4919 }
4920 else if (/* Lines are continued. */
4921 !it->truncate_lines_p
4922 && (/* And glyph doesn't fit on the line. */
4923 new_x > it->last_visible_x
4924 /* Or it fits exactly and we're on a window
4925 system frame. */
4926 || (new_x == it->last_visible_x
4927 && FRAME_WINDOW_P (it->f))))
4928 {
4929 if (/* IT->hpos == 0 means the very first glyph
4930 doesn't fit on the line, e.g. a wide image. */
4931 it->hpos == 0
4932 || (new_x == it->last_visible_x
4933 && FRAME_WINDOW_P (it->f)))
4934 {
4935 ++it->hpos;
4936 it->current_x = new_x;
4937 if (i == it->nglyphs - 1)
cafafe0b 4938 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4939 }
4940 else
47589c8c
GM
4941 {
4942 it->current_x = x;
4943 it->max_ascent = ascent;
4944 it->max_descent = descent;
4945 }
4946
4947 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
4948 IT_CHARPOS (*it)));
5f5c8ee5
GM
4949 result = MOVE_LINE_CONTINUED;
4950 break;
4951 }
4952 else if (new_x > it->first_visible_x)
4953 {
4954 /* Glyph is visible. Increment number of glyphs that
4955 would be displayed. */
4956 ++it->hpos;
4957 }
4958 else
4959 {
4960 /* Glyph is completely off the left margin of the display
4961 area. Nothing to do. */
4962 }
4963 }
4964
4965 if (result != MOVE_UNDEFINED)
4966 break;
4967 }
4968 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
4969 {
4970 /* Stop when TO_X specified and reached. This check is
4971 necessary here because of lines consisting of a line end,
4972 only. The line end will not produce any glyphs and we
4973 would never get MOVE_X_REACHED. */
4974 xassert (it->nglyphs == 0);
4975 result = MOVE_X_REACHED;
4976 break;
4977 }
4978
4979 /* Is this a line end? If yes, we're done. */
4980 if (ITERATOR_AT_END_OF_LINE_P (it))
4981 {
4982 result = MOVE_NEWLINE_OR_CR;
4983 break;
4984 }
4985
4986 /* The current display element has been consumed. Advance
4987 to the next. */
cafafe0b 4988 set_iterator_to_next (it, 1);
5f5c8ee5
GM
4989
4990 /* Stop if lines are truncated and IT's current x-position is
4991 past the right edge of the window now. */
4992 if (it->truncate_lines_p
4993 && it->current_x >= it->last_visible_x)
4994 {
4995 result = MOVE_LINE_TRUNCATED;
4996 break;
4997 }
4998 }
4999
5000 /* Restore the iterator settings altered at the beginning of this
5001 function. */
5002 it->glyph_row = saved_glyph_row;
5003 return result;
5004}
5005
5006
5007/* Move IT forward to a specified buffer position TO_CHARPOS, TO_X,
5008 TO_Y, TO_VPOS. OP is a bit-mask that specifies where to stop. See
5009 the description of enum move_operation_enum.
5010
5011 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
5012 screen line, this function will set IT to the next position >
5013 TO_CHARPOS. */
5014
5015void
5016move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
5017 struct it *it;
5018 int to_charpos, to_x, to_y, to_vpos;
5019 int op;
5020{
5021 enum move_it_result skip, skip2 = MOVE_X_REACHED;
5022 int line_height;
47589c8c 5023 int reached = 0;
5f5c8ee5 5024
47589c8c 5025 for (;;)
5f5c8ee5
GM
5026 {
5027 if (op & MOVE_TO_VPOS)
5028 {
5029 /* If no TO_CHARPOS and no TO_X specified, stop at the
5030 start of the line TO_VPOS. */
5031 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
5032 {
5033 if (it->vpos == to_vpos)
47589c8c
GM
5034 {
5035 reached = 1;
5036 break;
5037 }
5038 else
5039 skip = move_it_in_display_line_to (it, -1, -1, 0);
5f5c8ee5
GM
5040 }
5041 else
5042 {
5043 /* TO_VPOS >= 0 means stop at TO_X in the line at
5044 TO_VPOS, or at TO_POS, whichever comes first. */
47589c8c
GM
5045 if (it->vpos == to_vpos)
5046 {
5047 reached = 2;
5048 break;
5049 }
5050
5f5c8ee5
GM
5051 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
5052
5053 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
47589c8c
GM
5054 {
5055 reached = 3;
5056 break;
5057 }
5f5c8ee5
GM
5058 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
5059 {
5060 /* We have reached TO_X but not in the line we want. */
5061 skip = move_it_in_display_line_to (it, to_charpos,
5062 -1, MOVE_TO_POS);
5063 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5064 {
5065 reached = 4;
5066 break;
5067 }
5f5c8ee5
GM
5068 }
5069 }
5070 }
5071 else if (op & MOVE_TO_Y)
5072 {
5073 struct it it_backup;
5f5c8ee5
GM
5074
5075 /* TO_Y specified means stop at TO_X in the line containing
5076 TO_Y---or at TO_CHARPOS if this is reached first. The
5077 problem is that we can't really tell whether the line
5078 contains TO_Y before we have completely scanned it, and
5079 this may skip past TO_X. What we do is to first scan to
5080 TO_X.
5081
5082 If TO_X is not specified, use a TO_X of zero. The reason
5083 is to make the outcome of this function more predictable.
5084 If we didn't use TO_X == 0, we would stop at the end of
5085 the line which is probably not what a caller would expect
5086 to happen. */
5087 skip = move_it_in_display_line_to (it, to_charpos,
5088 ((op & MOVE_TO_X)
5089 ? to_x : 0),
5090 (MOVE_TO_X
5091 | (op & MOVE_TO_POS)));
5092
5093 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
5094 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c
GM
5095 {
5096 reached = 5;
5097 break;
5098 }
5f5c8ee5
GM
5099
5100 /* If TO_X was reached, we would like to know whether TO_Y
5101 is in the line. This can only be said if we know the
5102 total line height which requires us to scan the rest of
5103 the line. */
5f5c8ee5
GM
5104 if (skip == MOVE_X_REACHED)
5105 {
5106 it_backup = *it;
47589c8c 5107 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5108 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
5109 op & MOVE_TO_POS);
47589c8c 5110 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5111 }
5112
5113 /* Now, decide whether TO_Y is in this line. */
5114 line_height = it->max_ascent + it->max_descent;
47589c8c 5115 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
5f5c8ee5
GM
5116
5117 if (to_y >= it->current_y
5118 && to_y < it->current_y + line_height)
5119 {
5120 if (skip == MOVE_X_REACHED)
5121 /* If TO_Y is in this line and TO_X was reached above,
5122 we scanned too far. We have to restore IT's settings
5123 to the ones before skipping. */
5124 *it = it_backup;
47589c8c 5125 reached = 6;
5f5c8ee5
GM
5126 }
5127 else if (skip == MOVE_X_REACHED)
5128 {
5129 skip = skip2;
5130 if (skip == MOVE_POS_MATCH_OR_ZV)
47589c8c 5131 reached = 7;
5f5c8ee5
GM
5132 }
5133
47589c8c 5134 if (reached)
5f5c8ee5
GM
5135 break;
5136 }
5137 else
5138 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
5139
5140 switch (skip)
5141 {
5142 case MOVE_POS_MATCH_OR_ZV:
47589c8c
GM
5143 reached = 8;
5144 goto out;
5f5c8ee5
GM
5145
5146 case MOVE_NEWLINE_OR_CR:
cafafe0b 5147 set_iterator_to_next (it, 1);
5f5c8ee5
GM
5148 it->continuation_lines_width = 0;
5149 break;
5150
5151 case MOVE_LINE_TRUNCATED:
5152 it->continuation_lines_width = 0;
312246d1 5153 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
5154 if ((op & MOVE_TO_POS) != 0
5155 && IT_CHARPOS (*it) > to_charpos)
47589c8c
GM
5156 {
5157 reached = 9;
5158 goto out;
5159 }
5f5c8ee5
GM
5160 break;
5161
5162 case MOVE_LINE_CONTINUED:
5163 it->continuation_lines_width += it->current_x;
5164 break;
5165
5166 default:
5167 abort ();
5168 }
5169
5170 /* Reset/increment for the next run. */
5171 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
5172 it->current_x = it->hpos = 0;
5173 it->current_y += it->max_ascent + it->max_descent;
5174 ++it->vpos;
5175 last_height = it->max_ascent + it->max_descent;
5176 last_max_ascent = it->max_ascent;
5177 it->max_ascent = it->max_descent = 0;
5178 }
47589c8c
GM
5179
5180 out:
5181
5182 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
5f5c8ee5
GM
5183}
5184
5185
5186/* Move iterator IT backward by a specified y-distance DY, DY >= 0.
5187
5188 If DY > 0, move IT backward at least that many pixels. DY = 0
5189 means move IT backward to the preceding line start or BEGV. This
5190 function may move over more than DY pixels if IT->current_y - DY
5191 ends up in the middle of a line; in this case IT->current_y will be
5192 set to the top of the line moved to. */
5193
5194void
5195move_it_vertically_backward (it, dy)
5196 struct it *it;
5197 int dy;
5198{
5199 int nlines, h, line_height;
5200 struct it it2;
5201 int start_pos = IT_CHARPOS (*it);
5202
5203 xassert (dy >= 0);
5204
5205 /* Estimate how many newlines we must move back. */
5206 nlines = max (1, dy / CANON_Y_UNIT (it->f));
5207
5208 /* Set the iterator's position that many lines back. */
5209 while (nlines-- && IT_CHARPOS (*it) > BEGV)
5210 back_to_previous_visible_line_start (it);
5211
5212 /* Reseat the iterator here. When moving backward, we don't want
5213 reseat to skip forward over invisible text, set up the iterator
5214 to deliver from overlay strings at the new position etc. So,
5215 use reseat_1 here. */
5216 reseat_1 (it, it->current.pos, 1);
5217
5218 /* We are now surely at a line start. */
5219 it->current_x = it->hpos = 0;
5220
5221 /* Move forward and see what y-distance we moved. First move to the
5222 start of the next line so that we get its height. We need this
5223 height to be able to tell whether we reached the specified
5224 y-distance. */
5225 it2 = *it;
5226 it2.max_ascent = it2.max_descent = 0;
5227 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
5228 MOVE_TO_POS | MOVE_TO_VPOS);
5229 xassert (IT_CHARPOS (*it) >= BEGV);
5230 line_height = it2.max_ascent + it2.max_descent;
47589c8c 5231
5f5c8ee5
GM
5232 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
5233 xassert (IT_CHARPOS (*it) >= BEGV);
5234 h = it2.current_y - it->current_y;
5235 nlines = it2.vpos - it->vpos;
5236
5237 /* Correct IT's y and vpos position. */
5238 it->vpos -= nlines;
5239 it->current_y -= h;
5240
5241 if (dy == 0)
5242 {
5243 /* DY == 0 means move to the start of the screen line. The
5244 value of nlines is > 0 if continuation lines were involved. */
5245 if (nlines > 0)
5246 move_it_by_lines (it, nlines, 1);
5247 xassert (IT_CHARPOS (*it) <= start_pos);
5248 }
5249 else if (nlines)
5250 {
5251 /* The y-position we try to reach. Note that h has been
5252 subtracted in front of the if-statement. */
5253 int target_y = it->current_y + h - dy;
5254
5255 /* If we did not reach target_y, try to move further backward if
5256 we can. If we moved too far backward, try to move forward. */
5257 if (target_y < it->current_y
5258 && IT_CHARPOS (*it) > BEGV)
5259 {
5260 move_it_vertically (it, target_y - it->current_y);
5261 xassert (IT_CHARPOS (*it) >= BEGV);
5262 }
5263 else if (target_y >= it->current_y + line_height
5264 && IT_CHARPOS (*it) < ZV)
5265 {
5266 move_it_vertically (it, target_y - (it->current_y + line_height));
5267 xassert (IT_CHARPOS (*it) >= BEGV);
5268 }
5269 }
5270}
5271
5272
5273/* Move IT by a specified amount of pixel lines DY. DY negative means
5274 move backwards. DY = 0 means move to start of screen line. At the
5275 end, IT will be on the start of a screen line. */
5276
5277void
5278move_it_vertically (it, dy)
5279 struct it *it;
5280 int dy;
5281{
5282 if (dy <= 0)
5283 move_it_vertically_backward (it, -dy);
5284 else if (dy > 0)
5285 {
47589c8c 5286 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
5f5c8ee5
GM
5287 move_it_to (it, ZV, -1, it->current_y + dy, -1,
5288 MOVE_TO_POS | MOVE_TO_Y);
47589c8c 5289 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
5f5c8ee5
GM
5290
5291 /* If buffer ends in ZV without a newline, move to the start of
5292 the line to satisfy the post-condition. */
5293 if (IT_CHARPOS (*it) == ZV
5294 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
5295 move_it_by_lines (it, 0, 0);
5296 }
5297}
5298
5299
47fc2c10
GM
5300/* Move iterator IT past the end of the text line it is in. */
5301
5302void
5303move_it_past_eol (it)
5304 struct it *it;
5305{
5306 enum move_it_result rc;
5307
5308 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
5309 if (rc == MOVE_NEWLINE_OR_CR)
5310 set_iterator_to_next (it, 0);
5311}
5312
5313
2c79b732
GM
5314#if 0 /* Currently not used. */
5315
5f5c8ee5
GM
5316/* Return non-zero if some text between buffer positions START_CHARPOS
5317 and END_CHARPOS is invisible. IT->window is the window for text
5318 property lookup. */
5319
5320static int
5321invisible_text_between_p (it, start_charpos, end_charpos)
5322 struct it *it;
5323 int start_charpos, end_charpos;
5324{
5f5c8ee5
GM
5325 Lisp_Object prop, limit;
5326 int invisible_found_p;
5327
5328 xassert (it != NULL && start_charpos <= end_charpos);
5329
5330 /* Is text at START invisible? */
5331 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
5332 it->window);
5333 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5334 invisible_found_p = 1;
5335 else
5336 {
016b5642
MB
5337 limit = Fnext_single_char_property_change (make_number (start_charpos),
5338 Qinvisible, Qnil,
5339 make_number (end_charpos));
5f5c8ee5
GM
5340 invisible_found_p = XFASTINT (limit) < end_charpos;
5341 }
5342
5343 return invisible_found_p;
5f5c8ee5
GM
5344}
5345
2c79b732
GM
5346#endif /* 0 */
5347
5f5c8ee5
GM
5348
5349/* Move IT by a specified number DVPOS of screen lines down. DVPOS
5350 negative means move up. DVPOS == 0 means move to the start of the
5351 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
5352 NEED_Y_P is zero, IT->current_y will be left unchanged.
5353
5354 Further optimization ideas: If we would know that IT->f doesn't use
5355 a face with proportional font, we could be faster for
5356 truncate-lines nil. */
5357
5358void
5359move_it_by_lines (it, dvpos, need_y_p)
5360 struct it *it;
5361 int dvpos, need_y_p;
5362{
5363 struct position pos;
5364
5365 if (!FRAME_WINDOW_P (it->f))
5366 {
5367 struct text_pos textpos;
5368
5369 /* We can use vmotion on frames without proportional fonts. */
5370 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
5371 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
5372 reseat (it, textpos, 1);
5373 it->vpos += pos.vpos;
5374 it->current_y += pos.vpos;
5375 }
5376 else if (dvpos == 0)
5377 {
5378 /* DVPOS == 0 means move to the start of the screen line. */
5379 move_it_vertically_backward (it, 0);
5380 xassert (it->current_x == 0 && it->hpos == 0);
5381 }
5382 else if (dvpos > 0)
2c79b732 5383 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
5f5c8ee5
GM
5384 else
5385 {
5386 struct it it2;
5387 int start_charpos, i;
2c79b732 5388
5f5c8ee5
GM
5389 /* Go back -DVPOS visible lines and reseat the iterator there. */
5390 start_charpos = IT_CHARPOS (*it);
5391 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
5392 back_to_previous_visible_line_start (it);
5393 reseat (it, it->current.pos, 1);
5394 it->current_x = it->hpos = 0;
5395
5396 /* Above call may have moved too far if continuation lines
5397 are involved. Scan forward and see if it did. */
5398 it2 = *it;
5399 it2.vpos = it2.current_y = 0;
5400 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
5401 it->vpos -= it2.vpos;
5402 it->current_y -= it2.current_y;
5403 it->current_x = it->hpos = 0;
5404
5405 /* If we moved too far, move IT some lines forward. */
5406 if (it2.vpos > -dvpos)
5407 {
5408 int delta = it2.vpos + dvpos;
5409 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
5410 }
5411 }
5412}
5413
5414
5415\f
5416/***********************************************************************
5417 Messages
5418 ***********************************************************************/
5419
5420
937248bc
GM
5421/* Add a message with format string FORMAT and arguments ARG1 and ARG2
5422 to *Messages*. */
5423
5424void
5425add_to_log (format, arg1, arg2)
5426 char *format;
5427 Lisp_Object arg1, arg2;
5428{
5429 Lisp_Object args[3];
5430 Lisp_Object msg, fmt;
5431 char *buffer;
5432 int len;
5433 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5434
5435 fmt = msg = Qnil;
5436 GCPRO4 (fmt, msg, arg1, arg2);
5437
5438 args[0] = fmt = build_string (format);
5439 args[1] = arg1;
5440 args[2] = arg2;
6fc556fd 5441 msg = Fformat (3, args);
937248bc
GM
5442
5443 len = STRING_BYTES (XSTRING (msg)) + 1;
5444 buffer = (char *) alloca (len);
5445 strcpy (buffer, XSTRING (msg)->data);
5446
796184bc 5447 message_dolog (buffer, len - 1, 1, 0);
937248bc
GM
5448 UNGCPRO;
5449}
5450
5451
5f5c8ee5
GM
5452/* Output a newline in the *Messages* buffer if "needs" one. */
5453
5454void
5455message_log_maybe_newline ()
5456{
5457 if (message_log_need_newline)
5458 message_dolog ("", 0, 1, 0);
5459}
5460
5461
1e313f28 5462/* Add a string M of length NBYTES to the message log, optionally
5f5c8ee5
GM
5463 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
5464 nonzero, means interpret the contents of M as multibyte. This
5465 function calls low-level routines in order to bypass text property
5466 hooks, etc. which might not be safe to run. */
5467
5468void
1e313f28 5469message_dolog (m, nbytes, nlflag, multibyte)
5f5c8ee5 5470 char *m;
1e313f28 5471 int nbytes, nlflag, multibyte;
5f5c8ee5
GM
5472{
5473 if (!NILP (Vmessage_log_max))
5474 {
5475 struct buffer *oldbuf;
5476 Lisp_Object oldpoint, oldbegv, oldzv;
5477 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5478 int point_at_end = 0;
5479 int zv_at_end = 0;
5480 Lisp_Object old_deactivate_mark, tem;
5481 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5482
5483 old_deactivate_mark = Vdeactivate_mark;
5484 oldbuf = current_buffer;
6a94510a 5485 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
5f5c8ee5
GM
5486 current_buffer->undo_list = Qt;
5487
5488 oldpoint = Fpoint_marker ();
5489 oldbegv = Fpoint_min_marker ();
5490 oldzv = Fpoint_max_marker ();
5491 GCPRO4 (oldpoint, oldbegv, oldzv, old_deactivate_mark);
5492
5493 if (PT == Z)
5494 point_at_end = 1;
5495 if (ZV == Z)
5496 zv_at_end = 1;
5497
5498 BEGV = BEG;
5499 BEGV_BYTE = BEG_BYTE;
5500 ZV = Z;
5501 ZV_BYTE = Z_BYTE;
5502 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5503
5504 /* Insert the string--maybe converting multibyte to single byte
5505 or vice versa, so that all the text fits the buffer. */
5506 if (multibyte
5507 && NILP (current_buffer->enable_multibyte_characters))
5508 {
1e313f28 5509 int i, c, char_bytes;
5f5c8ee5
GM
5510 unsigned char work[1];
5511
5512 /* Convert a multibyte string to single-byte
5513 for the *Message* buffer. */
1e313f28 5514 for (i = 0; i < nbytes; i += nbytes)
5f5c8ee5 5515 {
1e313f28 5516 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
5f5c8ee5
GM
5517 work[0] = (SINGLE_BYTE_CHAR_P (c)
5518 ? c
5519 : multibyte_char_to_unibyte (c, Qnil));
5520 insert_1_both (work, 1, 1, 1, 0, 0);
5521 }
5522 }
5523 else if (! multibyte
5524 && ! NILP (current_buffer->enable_multibyte_characters))
5525 {
1e313f28 5526 int i, c, char_bytes;
5f5c8ee5 5527 unsigned char *msg = (unsigned char *) m;
260a86a0 5528 unsigned char str[MAX_MULTIBYTE_LENGTH];
5f5c8ee5
GM
5529 /* Convert a single-byte string to multibyte
5530 for the *Message* buffer. */
1e313f28 5531 for (i = 0; i < nbytes; i++)
5f5c8ee5
GM
5532 {
5533 c = unibyte_char_to_multibyte (msg[i]);
1e313f28
GM
5534 char_bytes = CHAR_STRING (c, str);
5535 insert_1_both (str, 1, char_bytes, 1, 0, 0);
5f5c8ee5
GM
5536 }
5537 }
1e313f28
GM
5538 else if (nbytes)
5539 insert_1 (m, nbytes, 1, 0, 0);
5f5c8ee5
GM
5540
5541 if (nlflag)
5542 {
5543 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
5544 insert_1 ("\n", 1, 1, 0, 0);
5545
5546 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
5547 this_bol = PT;
5548 this_bol_byte = PT_BYTE;
5549
5550 if (this_bol > BEG)
5551 {
5552 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
5553 prev_bol = PT;
5554 prev_bol_byte = PT_BYTE;
5555
5556 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
5557 this_bol, this_bol_byte);
5558 if (dup)
5559 {
5560 del_range_both (prev_bol, prev_bol_byte,
5561 this_bol, this_bol_byte, 0);
5562 if (dup > 1)
5563 {
5564 char dupstr[40];
5565 int duplen;
5566
5567 /* If you change this format, don't forget to also
5568 change message_log_check_duplicate. */
5569 sprintf (dupstr, " [%d times]", dup);
5570 duplen = strlen (dupstr);
5571 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
5572 insert_1 (dupstr, duplen, 1, 0, 1);
5573 }
5574 }
5575 }
5576
5577 if (NATNUMP (Vmessage_log_max))
5578 {
5579 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
5580 -XFASTINT (Vmessage_log_max) - 1, 0);
5581 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
5582 }
5583 }
5584 BEGV = XMARKER (oldbegv)->charpos;
5585 BEGV_BYTE = marker_byte_position (oldbegv);
5586
5587 if (zv_at_end)
5588 {
5589 ZV = Z;
5590 ZV_BYTE = Z_BYTE;
5591 }
5592 else
5593 {
5594 ZV = XMARKER (oldzv)->charpos;
5595 ZV_BYTE = marker_byte_position (oldzv);
5596 }
5597
5598 if (point_at_end)
5599 TEMP_SET_PT_BOTH (Z, Z_BYTE);
5600 else
5601 /* We can't do Fgoto_char (oldpoint) because it will run some
5602 Lisp code. */
5603 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
5604 XMARKER (oldpoint)->bytepos);
5605
5606 UNGCPRO;
5607 free_marker (oldpoint);
5608 free_marker (oldbegv);
5609 free_marker (oldzv);
5610
5611 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
5612 set_buffer_internal (oldbuf);
5613 if (NILP (tem))
5614 windows_or_buffers_changed = old_windows_or_buffers_changed;
5615 message_log_need_newline = !nlflag;
5616 Vdeactivate_mark = old_deactivate_mark;
5617 }
5618}
5619
5620
5621/* We are at the end of the buffer after just having inserted a newline.
5622 (Note: We depend on the fact we won't be crossing the gap.)
5623 Check to see if the most recent message looks a lot like the previous one.
5624 Return 0 if different, 1 if the new one should just replace it, or a
5625 value N > 1 if we should also append " [N times]". */
5626
5627static int
5628message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
5629 int prev_bol, this_bol;
5630 int prev_bol_byte, this_bol_byte;
5631{
5632 int i;
5633 int len = Z_BYTE - 1 - this_bol_byte;
5634 int seen_dots = 0;
5635 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
5636 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
5637
5638 for (i = 0; i < len; i++)
5639 {
509633e3 5640 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
5f5c8ee5
GM
5641 seen_dots = 1;
5642 if (p1[i] != p2[i])
5643 return seen_dots;
5644 }
5645 p1 += len;
5646 if (*p1 == '\n')
5647 return 2;
5648 if (*p1++ == ' ' && *p1++ == '[')
5649 {
5650 int n = 0;
5651 while (*p1 >= '0' && *p1 <= '9')
5652 n = n * 10 + *p1++ - '0';
5653 if (strncmp (p1, " times]\n", 8) == 0)
5654 return n+1;
5655 }
5656 return 0;
5657}
5658
5659
1e313f28
GM
5660/* Display an echo area message M with a specified length of NBYTES
5661 bytes. The string may include null characters. If M is 0, clear
5662 out any existing message, and let the mini-buffer text show
5663 through.
5f5c8ee5
GM
5664
5665 The buffer M must continue to exist until after the echo area gets
5666 cleared or some other message gets displayed there. This means do
5667 not pass text that is stored in a Lisp string; do not pass text in
5668 a buffer that was alloca'd. */
5669
5670void
1e313f28 5671message2 (m, nbytes, multibyte)
5f5c8ee5 5672 char *m;
1e313f28 5673 int nbytes;
5f5c8ee5
GM
5674 int multibyte;
5675{
5676 /* First flush out any partial line written with print. */
5677 message_log_maybe_newline ();
5678 if (m)
1e313f28
GM
5679 message_dolog (m, nbytes, 1, multibyte);
5680 message2_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5681}
5682
5683
5684/* The non-logging counterpart of message2. */
5685
5686void
1e313f28 5687message2_nolog (m, nbytes, multibyte)
5f5c8ee5 5688 char *m;
1e313f28 5689 int nbytes;
5f5c8ee5 5690{
886bd6f2 5691 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5692 message_enable_multibyte = multibyte;
5693
5694 if (noninteractive)
5695 {
5696 if (noninteractive_need_newline)
5697 putc ('\n', stderr);
5698 noninteractive_need_newline = 0;
5699 if (m)
1e313f28 5700 fwrite (m, nbytes, 1, stderr);
5f5c8ee5
GM
5701 if (cursor_in_echo_area == 0)
5702 fprintf (stderr, "\n");
5703 fflush (stderr);
5704 }
5705 /* A null message buffer means that the frame hasn't really been
5706 initialized yet. Error messages get reported properly by
5707 cmd_error, so this must be just an informative message; toss it. */
5708 else if (INTERACTIVE
886bd6f2
GM
5709 && sf->glyphs_initialized_p
5710 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5711 {
5712 Lisp_Object mini_window;
5713 struct frame *f;
5714
5715 /* Get the frame containing the mini-buffer
5716 that the selected frame is using. */
886bd6f2 5717 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5718 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5719
5720 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5721 if (FRAME_VISIBLE_P (sf)
5f5c8ee5
GM
5722 && ! FRAME_VISIBLE_P (f))
5723 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
5724
5725 if (m)
5726 {
1e313f28 5727 set_message (m, Qnil, nbytes, multibyte);
5f5c8ee5
GM
5728 if (minibuffer_auto_raise)
5729 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
5730 }
5731 else
c6e89d6c 5732 clear_message (1, 1);
5f5c8ee5 5733
c6e89d6c 5734 do_pending_window_change (0);
5f5c8ee5 5735 echo_area_display (1);
c6e89d6c 5736 do_pending_window_change (0);
5f5c8ee5
GM
5737 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5738 (*frame_up_to_date_hook) (f);
5739 }
5740}
5741
5742
c6e89d6c
GM
5743/* Display an echo area message M with a specified length of NBYTES
5744 bytes. The string may include null characters. If M is not a
5f5c8ee5
GM
5745 string, clear out any existing message, and let the mini-buffer
5746 text show through. */
5747
5748void
c6e89d6c 5749message3 (m, nbytes, multibyte)
5f5c8ee5 5750 Lisp_Object m;
c6e89d6c 5751 int nbytes;
5f5c8ee5
GM
5752 int multibyte;
5753{
5754 struct gcpro gcpro1;
5755
5756 GCPRO1 (m);
5757
5758 /* First flush out any partial line written with print. */
5759 message_log_maybe_newline ();
5760 if (STRINGP (m))
c6e89d6c
GM
5761 message_dolog (XSTRING (m)->data, nbytes, 1, multibyte);
5762 message3_nolog (m, nbytes, multibyte);
5f5c8ee5
GM
5763
5764 UNGCPRO;
5765}
5766
5767
5768/* The non-logging version of message3. */
5769
5770void
c6e89d6c 5771message3_nolog (m, nbytes, multibyte)
5f5c8ee5 5772 Lisp_Object m;
c6e89d6c 5773 int nbytes, multibyte;
5f5c8ee5 5774{
886bd6f2 5775 struct frame *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5776 message_enable_multibyte = multibyte;
5777
5778 if (noninteractive)
5779 {
5780 if (noninteractive_need_newline)
5781 putc ('\n', stderr);
5782 noninteractive_need_newline = 0;
5783 if (STRINGP (m))
c6e89d6c 5784 fwrite (XSTRING (m)->data, nbytes, 1, stderr);
5f5c8ee5
GM
5785 if (cursor_in_echo_area == 0)
5786 fprintf (stderr, "\n");
5787 fflush (stderr);
5788 }
5789 /* A null message buffer means that the frame hasn't really been
5790 initialized yet. Error messages get reported properly by
5791 cmd_error, so this must be just an informative message; toss it. */
5792 else if (INTERACTIVE
886bd6f2
GM
5793 && sf->glyphs_initialized_p
5794 && FRAME_MESSAGE_BUF (sf))
5f5c8ee5
GM
5795 {
5796 Lisp_Object mini_window;
c6e89d6c 5797 Lisp_Object frame;
5f5c8ee5
GM
5798 struct frame *f;
5799
5800 /* Get the frame containing the mini-buffer
5801 that the selected frame is using. */
886bd6f2 5802 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
5803 frame = XWINDOW (mini_window)->frame;
5804 f = XFRAME (frame);
5f5c8ee5
GM
5805
5806 FRAME_SAMPLE_VISIBILITY (f);
886bd6f2 5807 if (FRAME_VISIBLE_P (sf)
c6e89d6c
GM
5808 && !FRAME_VISIBLE_P (f))
5809 Fmake_frame_visible (frame);
5f5c8ee5 5810
c6e89d6c 5811 if (STRINGP (m) && XSTRING (m)->size)
5f5c8ee5 5812 {
c6e89d6c 5813 set_message (NULL, m, nbytes, multibyte);
468155d7
GM
5814 if (minibuffer_auto_raise)
5815 Fraise_frame (frame);
5f5c8ee5
GM
5816 }
5817 else
c6e89d6c 5818 clear_message (1, 1);
5f5c8ee5 5819
c6e89d6c 5820 do_pending_window_change (0);
5f5c8ee5 5821 echo_area_display (1);
c6e89d6c 5822 do_pending_window_change (0);
5f5c8ee5
GM
5823 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
5824 (*frame_up_to_date_hook) (f);
5825 }
5826}
5827
5828
5829/* Display a null-terminated echo area message M. If M is 0, clear
5830 out any existing message, and let the mini-buffer text show through.
5831
5832 The buffer M must continue to exist until after the echo area gets
5833 cleared or some other message gets displayed there. Do not pass
5834 text that is stored in a Lisp string. Do not pass text in a buffer
5835 that was alloca'd. */
5836
5837void
5838message1 (m)
5839 char *m;
5840{
5841 message2 (m, (m ? strlen (m) : 0), 0);
5842}
5843
5844
5845/* The non-logging counterpart of message1. */
5846
5847void
5848message1_nolog (m)
5849 char *m;
5850{
5851 message2_nolog (m, (m ? strlen (m) : 0), 0);
5852}
5853
5854/* Display a message M which contains a single %s
5855 which gets replaced with STRING. */
5856
5857void
5858message_with_string (m, string, log)
5859 char *m;
5860 Lisp_Object string;
5861 int log;
5862{
5863 if (noninteractive)
5864 {
5865 if (m)
5866 {
5867 if (noninteractive_need_newline)
5868 putc ('\n', stderr);
5869 noninteractive_need_newline = 0;
5870 fprintf (stderr, m, XSTRING (string)->data);
5871 if (cursor_in_echo_area == 0)
5872 fprintf (stderr, "\n");
5873 fflush (stderr);
5874 }
5875 }
5876 else if (INTERACTIVE)
5877 {
5878 /* The frame whose minibuffer we're going to display the message on.
5879 It may be larger than the selected frame, so we need
5880 to use its buffer, not the selected frame's buffer. */
5881 Lisp_Object mini_window;
886bd6f2 5882 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5883
5884 /* Get the frame containing the minibuffer
5885 that the selected frame is using. */
886bd6f2 5886 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5887 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5888
5889 /* A null message buffer means that the frame hasn't really been
5890 initialized yet. Error messages get reported properly by
5891 cmd_error, so this must be just an informative message; toss it. */
5892 if (FRAME_MESSAGE_BUF (f))
5893 {
5894 int len;
5895 char *a[1];
5896 a[0] = (char *) XSTRING (string)->data;
5897
5898 len = doprnt (FRAME_MESSAGE_BUF (f),
5899 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5900
5901 if (log)
5902 message2 (FRAME_MESSAGE_BUF (f), len,
5903 STRING_MULTIBYTE (string));
5904 else
5905 message2_nolog (FRAME_MESSAGE_BUF (f), len,
5906 STRING_MULTIBYTE (string));
5907
5908 /* Print should start at the beginning of the message
5909 buffer next time. */
5910 message_buf_print = 0;
5911 }
5912 }
5913}
5914
5915
5f5c8ee5
GM
5916/* Dump an informative message to the minibuf. If M is 0, clear out
5917 any existing message, and let the mini-buffer text show through. */
5918
5919/* VARARGS 1 */
5920void
5921message (m, a1, a2, a3)
5922 char *m;
5923 EMACS_INT a1, a2, a3;
5924{
5925 if (noninteractive)
5926 {
5927 if (m)
5928 {
5929 if (noninteractive_need_newline)
5930 putc ('\n', stderr);
5931 noninteractive_need_newline = 0;
5932 fprintf (stderr, m, a1, a2, a3);
5933 if (cursor_in_echo_area == 0)
5934 fprintf (stderr, "\n");
5935 fflush (stderr);
5936 }
5937 }
5938 else if (INTERACTIVE)
5939 {
5940 /* The frame whose mini-buffer we're going to display the message
5941 on. It may be larger than the selected frame, so we need to
5942 use its buffer, not the selected frame's buffer. */
5943 Lisp_Object mini_window;
886bd6f2 5944 struct frame *f, *sf = SELECTED_FRAME ();
5f5c8ee5
GM
5945
5946 /* Get the frame containing the mini-buffer
5947 that the selected frame is using. */
886bd6f2 5948 mini_window = FRAME_MINIBUF_WINDOW (sf);
5f5c8ee5
GM
5949 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
5950
5951 /* A null message buffer means that the frame hasn't really been
5952 initialized yet. Error messages get reported properly by
5953 cmd_error, so this must be just an informative message; toss
5954 it. */
5955 if (FRAME_MESSAGE_BUF (f))
5956 {
5957 if (m)
5958 {
5959 int len;
5960#ifdef NO_ARG_ARRAY
5961 char *a[3];
5962 a[0] = (char *) a1;
5963 a[1] = (char *) a2;
5964 a[2] = (char *) a3;
5965
5966 len = doprnt (FRAME_MESSAGE_BUF (f),
5967 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
5968#else
5969 len = doprnt (FRAME_MESSAGE_BUF (f),
5970 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
5971 (char **) &a1);
5972#endif /* NO_ARG_ARRAY */
5973
5974 message2 (FRAME_MESSAGE_BUF (f), len, 0);
5975 }
5976 else
5977 message1 (0);
5978
5979 /* Print should start at the beginning of the message
5980 buffer next time. */
5981 message_buf_print = 0;
5982 }
5983 }
5984}
5985
5986
5987/* The non-logging version of message. */
5988
5989void
5990message_nolog (m, a1, a2, a3)
5991 char *m;
5992 EMACS_INT a1, a2, a3;
5993{
5994 Lisp_Object old_log_max;
5995 old_log_max = Vmessage_log_max;
5996 Vmessage_log_max = Qnil;
5997 message (m, a1, a2, a3);
5998 Vmessage_log_max = old_log_max;
5999}
6000
6001
c6e89d6c
GM
6002/* Display the current message in the current mini-buffer. This is
6003 only called from error handlers in process.c, and is not time
6004 critical. */
5f5c8ee5
GM
6005
6006void
6007update_echo_area ()
6008{
c6e89d6c
GM
6009 if (!NILP (echo_area_buffer[0]))
6010 {
6011 Lisp_Object string;
6012 string = Fcurrent_message ();
6013 message3 (string, XSTRING (string)->size,
6014 !NILP (current_buffer->enable_multibyte_characters));
6015 }
6016}
6017
6018
5bcfeb49
GM
6019/* Make sure echo area buffers in echo_buffers[] are life. If they
6020 aren't, make new ones. */
6021
6022static void
6023ensure_echo_area_buffers ()
6024{
6025 int i;
6026
6027 for (i = 0; i < 2; ++i)
6028 if (!BUFFERP (echo_buffer[i])
6029 || NILP (XBUFFER (echo_buffer[i])->name))
6030 {
6031 char name[30];
ff3d9573
GM
6032 Lisp_Object old_buffer;
6033 int j;
6034
6035 old_buffer = echo_buffer[i];
5bcfeb49
GM
6036 sprintf (name, " *Echo Area %d*", i);
6037 echo_buffer[i] = Fget_buffer_create (build_string (name));
ad4f174e 6038 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
ff3d9573
GM
6039
6040 for (j = 0; j < 2; ++j)
6041 if (EQ (old_buffer, echo_area_buffer[j]))
6042 echo_area_buffer[j] = echo_buffer[i];
5bcfeb49
GM
6043 }
6044}
6045
6046
23a96c77 6047/* Call FN with args A1..A4 with either the current or last displayed
c6e89d6c
GM
6048 echo_area_buffer as current buffer.
6049
6050 WHICH zero means use the current message buffer
6051 echo_area_buffer[0]. If that is nil, choose a suitable buffer
6052 from echo_buffer[] and clear it.
6053
6054 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
6055 suitable buffer from echo_buffer[] and clear it.
6056
6057 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
6058 that the current message becomes the last displayed one, make
6059 choose a suitable buffer for echo_area_buffer[0], and clear it.
6060
6061 Value is what FN returns. */
6062
6063static int
23a96c77 6064with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
c6e89d6c
GM
6065 struct window *w;
6066 int which;
23dd2d97
KR
6067 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
6068 EMACS_INT a1;
6069 Lisp_Object a2;
6070 EMACS_INT a3, a4;
c6e89d6c
GM
6071{
6072 Lisp_Object buffer;
15e26c76 6073 int this_one, the_other, clear_buffer_p, rc;
2d27dae2 6074 int count = BINDING_STACK_SIZE ();
c6e89d6c
GM
6075
6076 /* If buffers aren't life, make new ones. */
5bcfeb49 6077 ensure_echo_area_buffers ();
c6e89d6c
GM
6078
6079 clear_buffer_p = 0;
6080
6081 if (which == 0)
6082 this_one = 0, the_other = 1;
6083 else if (which > 0)
6084 this_one = 1, the_other = 0;
5f5c8ee5 6085 else
c6e89d6c
GM
6086 {
6087 this_one = 0, the_other = 1;
6088 clear_buffer_p = 1;
6089
6090 /* We need a fresh one in case the current echo buffer equals
6091 the one containing the last displayed echo area message. */
6092 if (!NILP (echo_area_buffer[this_one])
6093 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
6094 echo_area_buffer[this_one] = Qnil;
c6e89d6c
GM
6095 }
6096
6097 /* Choose a suitable buffer from echo_buffer[] is we don't
6098 have one. */
6099 if (NILP (echo_area_buffer[this_one]))
6100 {
6101 echo_area_buffer[this_one]
6102 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
6103 ? echo_buffer[the_other]
6104 : echo_buffer[this_one]);
6105 clear_buffer_p = 1;
6106 }
6107
6108 buffer = echo_area_buffer[this_one];
6109
6110 record_unwind_protect (unwind_with_echo_area_buffer,
6111 with_echo_area_buffer_unwind_data (w));
6112
6113 /* Make the echo area buffer current. Note that for display
6114 purposes, it is not necessary that the displayed window's buffer
6115 == current_buffer, except for text property lookup. So, let's
6116 only set that buffer temporarily here without doing a full
6117 Fset_window_buffer. We must also change w->pointm, though,
6118 because otherwise an assertions in unshow_buffer fails, and Emacs
6119 aborts. */
9142dd5b 6120 set_buffer_internal_1 (XBUFFER (buffer));
c6e89d6c
GM
6121 if (w)
6122 {
6123 w->buffer = buffer;
6124 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
6125 }
ad4f174e 6126
c6e89d6c
GM
6127 current_buffer->undo_list = Qt;
6128 current_buffer->read_only = Qnil;
bbbf6d06 6129 specbind (Qinhibit_read_only, Qt);
c6e89d6c
GM
6130
6131 if (clear_buffer_p && Z > BEG)
6132 del_range (BEG, Z);
6133
6134 xassert (BEGV >= BEG);
6135 xassert (ZV <= Z && ZV >= BEGV);
6136
23a96c77 6137 rc = fn (a1, a2, a3, a4);
c6e89d6c
GM
6138
6139 xassert (BEGV >= BEG);
6140 xassert (ZV <= Z && ZV >= BEGV);
6141
6142 unbind_to (count, Qnil);
6143 return rc;
5f5c8ee5
GM
6144}
6145
6146
c6e89d6c
GM
6147/* Save state that should be preserved around the call to the function
6148 FN called in with_echo_area_buffer. */
5f5c8ee5 6149
c6e89d6c
GM
6150static Lisp_Object
6151with_echo_area_buffer_unwind_data (w)
6152 struct window *w;
5f5c8ee5 6153{
c6e89d6c
GM
6154 int i = 0;
6155 Lisp_Object vector;
5f5c8ee5 6156
c6e89d6c
GM
6157 /* Reduce consing by keeping one vector in
6158 Vwith_echo_area_save_vector. */
6159 vector = Vwith_echo_area_save_vector;
6160 Vwith_echo_area_save_vector = Qnil;
6161
6162 if (NILP (vector))
9142dd5b 6163 vector = Fmake_vector (make_number (7), Qnil);
c6e89d6c 6164
a61b7058
GM
6165 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
6166 AREF (vector, i) = Vdeactivate_mark, ++i;
6167 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
c6e89d6c
GM
6168
6169 if (w)
6170 {
a61b7058
GM
6171 XSETWINDOW (AREF (vector, i), w); ++i;
6172 AREF (vector, i) = w->buffer; ++i;
6173 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
6174 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
c6e89d6c
GM
6175 }
6176 else
6177 {
6178 int end = i + 4;
a61b7058
GM
6179 for (; i < end; ++i)
6180 AREF (vector, i) = Qnil;
c6e89d6c 6181 }
5f5c8ee5 6182
a61b7058 6183 xassert (i == ASIZE (vector));
c6e89d6c
GM
6184 return vector;
6185}
5f5c8ee5 6186
5f5c8ee5 6187
c6e89d6c
GM
6188/* Restore global state from VECTOR which was created by
6189 with_echo_area_buffer_unwind_data. */
6190
6191static Lisp_Object
6192unwind_with_echo_area_buffer (vector)
6193 Lisp_Object vector;
6194{
bbbf6d06
GM
6195 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
6196 Vdeactivate_mark = AREF (vector, 1);
6197 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
c6e89d6c 6198
bbbf6d06 6199 if (WINDOWP (AREF (vector, 3)))
c6e89d6c
GM
6200 {
6201 struct window *w;
6202 Lisp_Object buffer, charpos, bytepos;
6203
bbbf6d06
GM
6204 w = XWINDOW (AREF (vector, 3));
6205 buffer = AREF (vector, 4);
6206 charpos = AREF (vector, 5);
6207 bytepos = AREF (vector, 6);
c6e89d6c
GM
6208
6209 w->buffer = buffer;
6210 set_marker_both (w->pointm, buffer,
6211 XFASTINT (charpos), XFASTINT (bytepos));
6212 }
6213
6214 Vwith_echo_area_save_vector = vector;
6215 return Qnil;
6216}
6217
6218
6219/* Set up the echo area for use by print functions. MULTIBYTE_P
6220 non-zero means we will print multibyte. */
6221
6222void
6223setup_echo_area_for_printing (multibyte_p)
6224 int multibyte_p;
6225{
5bcfeb49
GM
6226 ensure_echo_area_buffers ();
6227
c6e89d6c
GM
6228 if (!message_buf_print)
6229 {
6230 /* A message has been output since the last time we printed.
6231 Choose a fresh echo area buffer. */
6232 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6233 echo_area_buffer[0] = echo_buffer[1];
6234 else
6235 echo_area_buffer[0] = echo_buffer[0];
6236
6237 /* Switch to that buffer and clear it. */
6238 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
ab2c5f0a 6239 current_buffer->truncate_lines = Qnil;
bbbf6d06 6240
c6e89d6c 6241 if (Z > BEG)
bbbf6d06
GM
6242 {
6243 int count = BINDING_STACK_SIZE ();
6244 specbind (Qinhibit_read_only, Qt);
6245 del_range (BEG, Z);
6246 unbind_to (count, Qnil);
6247 }
c6e89d6c
GM
6248 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6249
6250 /* Set up the buffer for the multibyteness we need. */
6251 if (multibyte_p
6252 != !NILP (current_buffer->enable_multibyte_characters))
6253 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
6254
6255 /* Raise the frame containing the echo area. */
6256 if (minibuffer_auto_raise)
6257 {
886bd6f2 6258 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6259 Lisp_Object mini_window;
886bd6f2 6260 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6261 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6262 }
6263
8a4e3c0c 6264 message_log_maybe_newline ();
c6e89d6c
GM
6265 message_buf_print = 1;
6266 }
fa77249f
GM
6267 else
6268 {
6269 if (NILP (echo_area_buffer[0]))
6270 {
6271 if (EQ (echo_area_buffer[1], echo_buffer[0]))
6272 echo_area_buffer[0] = echo_buffer[1];
6273 else
6274 echo_area_buffer[0] = echo_buffer[0];
6275 }
6276
6277 if (current_buffer != XBUFFER (echo_area_buffer[0]))
ab2c5f0a
GM
6278 {
6279 /* Someone switched buffers between print requests. */
6280 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
6281 current_buffer->truncate_lines = Qnil;
6282 }
fa77249f 6283 }
c6e89d6c
GM
6284}
6285
6286
dd2eb166
GM
6287/* Display an echo area message in window W. Value is non-zero if W's
6288 height is changed. If display_last_displayed_message_p is
6289 non-zero, display the message that was last displayed, otherwise
6290 display the current message. */
c6e89d6c
GM
6291
6292static int
6293display_echo_area (w)
6294 struct window *w;
6295{
25edb08f
GM
6296 int i, no_message_p, window_height_changed_p, count;
6297
6298 /* Temporarily disable garbage collections while displaying the echo
6299 area. This is done because a GC can print a message itself.
6300 That message would modify the echo area buffer's contents while a
6301 redisplay of the buffer is going on, and seriously confuse
6302 redisplay. */
6303 count = inhibit_garbage_collection ();
dd2eb166
GM
6304
6305 /* If there is no message, we must call display_echo_area_1
6306 nevertheless because it resizes the window. But we will have to
6307 reset the echo_area_buffer in question to nil at the end because
6308 with_echo_area_buffer will sets it to an empty buffer. */
6309 i = display_last_displayed_message_p ? 1 : 0;
6310 no_message_p = NILP (echo_area_buffer[i]);
6311
6312 window_height_changed_p
6313 = with_echo_area_buffer (w, display_last_displayed_message_p,
23a96c77 6314 display_echo_area_1,
23dd2d97 6315 (EMACS_INT) w, Qnil, 0, 0);
dd2eb166
GM
6316
6317 if (no_message_p)
6318 echo_area_buffer[i] = Qnil;
25edb08f
GM
6319
6320 unbind_to (count, Qnil);
dd2eb166 6321 return window_height_changed_p;
c6e89d6c
GM
6322}
6323
6324
6325/* Helper for display_echo_area. Display the current buffer which
23a96c77
GM
6326 contains the current echo area message in window W, a mini-window,
6327 a pointer to which is passed in A1. A2..A4 are currently not used.
c6e89d6c
GM
6328 Change the height of W so that all of the message is displayed.
6329 Value is non-zero if height of W was changed. */
6330
6331static int
23a96c77 6332display_echo_area_1 (a1, a2, a3, a4)
23dd2d97
KR
6333 EMACS_INT a1;
6334 Lisp_Object a2;
6335 EMACS_INT a3, a4;
c6e89d6c 6336{
23a96c77 6337 struct window *w = (struct window *) a1;
c6e89d6c 6338 Lisp_Object window;
c6e89d6c
GM
6339 struct text_pos start;
6340 int window_height_changed_p = 0;
6341
6342 /* Do this before displaying, so that we have a large enough glyph
6343 matrix for the display. */
92a90e89 6344 window_height_changed_p = resize_mini_window (w, 0);
c6e89d6c
GM
6345
6346 /* Display. */
6347 clear_glyph_matrix (w->desired_matrix);
6348 XSETWINDOW (window, w);
6349 SET_TEXT_POS (start, BEG, BEG_BYTE);
6350 try_window (window, start);
6351
c6e89d6c
GM
6352 return window_height_changed_p;
6353}
6354
6355
92a90e89
GM
6356/* Resize the echo area window to exactly the size needed for the
6357 currently displayed message, if there is one. */
6358
6359void
6360resize_echo_area_axactly ()
6361{
6362 if (BUFFERP (echo_area_buffer[0])
6363 && WINDOWP (echo_area_window))
6364 {
6365 struct window *w = XWINDOW (echo_area_window);
6366 int resized_p;
6367
23a96c77 6368 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
23dd2d97 6369 (EMACS_INT) w, Qnil, 0, 0);
92a90e89
GM
6370 if (resized_p)
6371 {
6372 ++windows_or_buffers_changed;
6373 ++update_mode_lines;
6374 redisplay_internal (0);
6375 }
6376 }
6377}
6378
6379
23a96c77
GM
6380/* Callback function for with_echo_area_buffer, when used from
6381 resize_echo_area_axactly. A1 contains a pointer to the window to
6382 resize, A2 to A4 are not used. Value is what resize_mini_window
6383 returns. */
6384
6385static int
6386resize_mini_window_1 (a1, a2, a3, a4)
23dd2d97
KR
6387 EMACS_INT a1;
6388 Lisp_Object a2;
6389 EMACS_INT a3, a4;
23a96c77
GM
6390{
6391 return resize_mini_window ((struct window *) a1, 1);
6392}
6393
6394
92a90e89
GM
6395/* Resize mini-window W to fit the size of its contents. EXACT:P
6396 means size the window exactly to the size needed. Otherwise, it's
6397 only enlarged until W's buffer is empty. Value is non-zero if
6398 the window height has been changed. */
c6e89d6c 6399
9472f927 6400int
92a90e89 6401resize_mini_window (w, exact_p)
c6e89d6c 6402 struct window *w;
92a90e89 6403 int exact_p;
c6e89d6c
GM
6404{
6405 struct frame *f = XFRAME (w->frame);
6406 int window_height_changed_p = 0;
6407
6408 xassert (MINI_WINDOW_P (w));
97cafc0f
GM
6409
6410 /* Nil means don't try to resize. */
6422c1d7 6411 if (NILP (Vresize_mini_windows)
00f6d59e 6412 || (FRAME_X_P (f) && f->output_data.x == NULL))
97cafc0f 6413 return 0;
c6e89d6c
GM
6414
6415 if (!FRAME_MINIBUF_ONLY_P (f))
6416 {
6417 struct it it;
dd2eb166
GM
6418 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
6419 int total_height = XFASTINT (root->height) + XFASTINT (w->height);
6420 int height, max_height;
6421 int unit = CANON_Y_UNIT (f);
6422 struct text_pos start;
1bfdbe43
GM
6423 struct buffer *old_current_buffer = NULL;
6424
6425 if (current_buffer != XBUFFER (w->buffer))
6426 {
6427 old_current_buffer = current_buffer;
6428 set_buffer_internal (XBUFFER (w->buffer));
6429 }
9142dd5b 6430
c6e89d6c 6431 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
c6e89d6c 6432
dd2eb166
GM
6433 /* Compute the max. number of lines specified by the user. */
6434 if (FLOATP (Vmax_mini_window_height))
6435 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
6436 else if (INTEGERP (Vmax_mini_window_height))
6437 max_height = XINT (Vmax_mini_window_height);
97cafc0f
GM
6438 else
6439 max_height = total_height / 4;
dd2eb166
GM
6440
6441 /* Correct that max. height if it's bogus. */
6442 max_height = max (1, max_height);
6443 max_height = min (total_height, max_height);
6444
6445 /* Find out the height of the text in the window. */
ad4f174e
GM
6446 if (it.truncate_lines_p)
6447 height = 1;
55b064bd 6448 else
ad4f174e
GM
6449 {
6450 last_height = 0;
6451 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
6452 if (it.max_ascent == 0 && it.max_descent == 0)
6453 height = it.current_y + last_height;
6454 else
6455 height = it.current_y + it.max_ascent + it.max_descent;
3c4b7685 6456 height -= it.extra_line_spacing;
ad4f174e
GM
6457 height = (height + unit - 1) / unit;
6458 }
dd2eb166
GM
6459
6460 /* Compute a suitable window start. */
6461 if (height > max_height)
6462 {
6463 height = max_height;
6464 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
6465 move_it_vertically_backward (&it, (height - 1) * unit);
6466 start = it.current.pos;
6467 }
6468 else
6469 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
6470 SET_MARKER_FROM_TEXT_POS (w->start, start);
c59c668a 6471
6422c1d7 6472 if (EQ (Vresize_mini_windows, Qgrow_only))
dd2eb166 6473 {
6422c1d7
GM
6474 /* Let it grow only, until we display an empty message, in which
6475 case the window shrinks again. */
6476 if (height > XFASTINT (w->height))
6477 {
6478 int old_height = XFASTINT (w->height);
6479 freeze_window_starts (f, 1);
6480 grow_mini_window (w, height - XFASTINT (w->height));
6481 window_height_changed_p = XFASTINT (w->height) != old_height;
6482 }
6483 else if (height < XFASTINT (w->height)
6484 && (exact_p || BEGV == ZV))
6485 {
6486 int old_height = XFASTINT (w->height);
6487 freeze_window_starts (f, 0);
6488 shrink_mini_window (w);
6489 window_height_changed_p = XFASTINT (w->height) != old_height;
6490 }
da448723 6491 }
6422c1d7 6492 else
da448723 6493 {
6422c1d7
GM
6494 /* Always resize to exact size needed. */
6495 if (height > XFASTINT (w->height))
6496 {
6497 int old_height = XFASTINT (w->height);
6498 freeze_window_starts (f, 1);
6499 grow_mini_window (w, height - XFASTINT (w->height));
6500 window_height_changed_p = XFASTINT (w->height) != old_height;
6501 }
6502 else if (height < XFASTINT (w->height))
6503 {
6504 int old_height = XFASTINT (w->height);
6505 freeze_window_starts (f, 0);
6506 shrink_mini_window (w);
6507
6508 if (height)
6509 {
6510 freeze_window_starts (f, 1);
6511 grow_mini_window (w, height - XFASTINT (w->height));
6512 }
6513
6514 window_height_changed_p = XFASTINT (w->height) != old_height;
6515 }
9142dd5b 6516 }
1bfdbe43
GM
6517
6518 if (old_current_buffer)
6519 set_buffer_internal (old_current_buffer);
c6e89d6c
GM
6520 }
6521
6522 return window_height_changed_p;
6523}
6524
6525
6526/* Value is the current message, a string, or nil if there is no
6527 current message. */
6528
6529Lisp_Object
6530current_message ()
6531{
6532 Lisp_Object msg;
6533
6534 if (NILP (echo_area_buffer[0]))
6535 msg = Qnil;
6536 else
6537 {
23a96c77 6538 with_echo_area_buffer (0, 0, current_message_1,
23dd2d97 6539 (EMACS_INT) &msg, Qnil, 0, 0);
c6e89d6c
GM
6540 if (NILP (msg))
6541 echo_area_buffer[0] = Qnil;
6542 }
6543
6544 return msg;
6545}
6546
6547
6548static int
23a96c77 6549current_message_1 (a1, a2, a3, a4)
23dd2d97
KR
6550 EMACS_INT a1;
6551 Lisp_Object a2;
6552 EMACS_INT a3, a4;
c6e89d6c 6553{
23a96c77
GM
6554 Lisp_Object *msg = (Lisp_Object *) a1;
6555
c6e89d6c
GM
6556 if (Z > BEG)
6557 *msg = make_buffer_string (BEG, Z, 1);
6558 else
6559 *msg = Qnil;
6560 return 0;
6561}
6562
6563
6564/* Push the current message on Vmessage_stack for later restauration
6565 by restore_message. Value is non-zero if the current message isn't
6566 empty. This is a relatively infrequent operation, so it's not
6567 worth optimizing. */
6568
6569int
6570push_message ()
6571{
6572 Lisp_Object msg;
6573 msg = current_message ();
6574 Vmessage_stack = Fcons (msg, Vmessage_stack);
6575 return STRINGP (msg);
6576}
6577
6578
098ec84a
GM
6579/* Handler for record_unwind_protect calling pop_message. */
6580
6581Lisp_Object
6582push_message_unwind (dummy)
6583 Lisp_Object dummy;
6584{
6585 pop_message ();
6586 return Qnil;
6587}
6588
6589
c6e89d6c
GM
6590/* Restore message display from the top of Vmessage_stack. */
6591
6592void
6593restore_message ()
6594{
6595 Lisp_Object msg;
6596
6597 xassert (CONSP (Vmessage_stack));
6598 msg = XCAR (Vmessage_stack);
6599 if (STRINGP (msg))
6600 message3_nolog (msg, STRING_BYTES (XSTRING (msg)), STRING_MULTIBYTE (msg));
6601 else
6602 message3_nolog (msg, 0, 0);
6603}
6604
6605
6606/* Pop the top-most entry off Vmessage_stack. */
6607
6608void
6609pop_message ()
6610{
6611 xassert (CONSP (Vmessage_stack));
6612 Vmessage_stack = XCDR (Vmessage_stack);
6613}
6614
6615
6616/* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
6617 exits. If the stack is not empty, we have a missing pop_message
6618 somewhere. */
6619
6620void
6621check_message_stack ()
6622{
6623 if (!NILP (Vmessage_stack))
6624 abort ();
6625}
6626
6627
6628/* Truncate to NCHARS what will be displayed in the echo area the next
6629 time we display it---but don't redisplay it now. */
6630
6631void
6632truncate_echo_area (nchars)
6633 int nchars;
6634{
6635 if (nchars == 0)
6636 echo_area_buffer[0] = Qnil;
6637 /* A null message buffer means that the frame hasn't really been
6638 initialized yet. Error messages get reported properly by
6639 cmd_error, so this must be just an informative message; toss it. */
6640 else if (!noninteractive
6641 && INTERACTIVE
c6e89d6c 6642 && !NILP (echo_area_buffer[0]))
886bd6f2
GM
6643 {
6644 struct frame *sf = SELECTED_FRAME ();
6645 if (FRAME_MESSAGE_BUF (sf))
23dd2d97 6646 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
886bd6f2 6647 }
c6e89d6c
GM
6648}
6649
6650
6651/* Helper function for truncate_echo_area. Truncate the current
6652 message to at most NCHARS characters. */
6653
6654static int
23a96c77 6655truncate_message_1 (nchars, a2, a3, a4)
23dd2d97
KR
6656 EMACS_INT nchars;
6657 Lisp_Object a2;
6658 EMACS_INT a3, a4;
c6e89d6c
GM
6659{
6660 if (BEG + nchars < Z)
6661 del_range (BEG + nchars, Z);
6662 if (Z == BEG)
6663 echo_area_buffer[0] = Qnil;
6664 return 0;
6665}
6666
6667
6668/* Set the current message to a substring of S or STRING.
6669
6670 If STRING is a Lisp string, set the message to the first NBYTES
6671 bytes from STRING. NBYTES zero means use the whole string. If
6672 STRING is multibyte, the message will be displayed multibyte.
6673
6674 If S is not null, set the message to the first LEN bytes of S. LEN
6675 zero means use the whole string. MULTIBYTE_P non-zero means S is
6676 multibyte. Display the message multibyte in that case. */
6677
6678void
6679set_message (s, string, nbytes, multibyte_p)
6680 char *s;
6681 Lisp_Object string;
6682 int nbytes;
6683{
6684 message_enable_multibyte
6685 = ((s && multibyte_p)
6686 || (STRINGP (string) && STRING_MULTIBYTE (string)));
6687
23a96c77
GM
6688 with_echo_area_buffer (0, -1, set_message_1,
6689 (EMACS_INT) s, string, nbytes, multibyte_p);
c6e89d6c 6690 message_buf_print = 0;
21fdfb65 6691 help_echo_showing_p = 0;
c6e89d6c
GM
6692}
6693
6694
6695/* Helper function for set_message. Arguments have the same meaning
23a96c77
GM
6696 as there, with A1 corresponding to S and A2 corresponding to STRING
6697 This function is called with the echo area buffer being
c6e89d6c
GM
6698 current. */
6699
6700static int
23a96c77 6701set_message_1 (a1, a2, nbytes, multibyte_p)
23dd2d97
KR
6702 EMACS_INT a1;
6703 Lisp_Object a2;
6704 EMACS_INT nbytes, multibyte_p;
c6e89d6c 6705{
23a96c77 6706 char *s = (char *) a1;
23dd2d97 6707 Lisp_Object string = a2;
23a96c77 6708
c6e89d6c
GM
6709 xassert (BEG == Z);
6710
6711 /* Change multibyteness of the echo buffer appropriately. */
6712 if (message_enable_multibyte
6713 != !NILP (current_buffer->enable_multibyte_characters))
6714 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
6715
ad4f174e
GM
6716 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
6717
c6e89d6c
GM
6718 /* Insert new message at BEG. */
6719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
6720
6721 if (STRINGP (string))
6722 {
6723 int nchars;
6724
6725 if (nbytes == 0)
6726 nbytes = XSTRING (string)->size_byte;
6727 nchars = string_byte_to_char (string, nbytes);
6728
6729 /* This function takes care of single/multibyte conversion. We
6730 just have to ensure that the echo area buffer has the right
6731 setting of enable_multibyte_characters. */
6732 insert_from_string (string, 0, 0, nchars, nbytes, 1);
6733 }
6734 else if (s)
6735 {
6736 if (nbytes == 0)
6737 nbytes = strlen (s);
6738
6739 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
6740 {
6741 /* Convert from multi-byte to single-byte. */
6742 int i, c, n;
6743 unsigned char work[1];
6744
6745 /* Convert a multibyte string to single-byte. */
6746 for (i = 0; i < nbytes; i += n)
6747 {
6748 c = string_char_and_length (s + i, nbytes - i, &n);
6749 work[0] = (SINGLE_BYTE_CHAR_P (c)
6750 ? c
6751 : multibyte_char_to_unibyte (c, Qnil));
6752 insert_1_both (work, 1, 1, 1, 0, 0);
6753 }
6754 }
6755 else if (!multibyte_p
6756 && !NILP (current_buffer->enable_multibyte_characters))
6757 {
6758 /* Convert from single-byte to multi-byte. */
6759 int i, c, n;
6760 unsigned char *msg = (unsigned char *) s;
260a86a0 6761 unsigned char str[MAX_MULTIBYTE_LENGTH];
c6e89d6c
GM
6762
6763 /* Convert a single-byte string to multibyte. */
6764 for (i = 0; i < nbytes; i++)
6765 {
6766 c = unibyte_char_to_multibyte (msg[i]);
260a86a0
KH
6767 n = CHAR_STRING (c, str);
6768 insert_1_both (str, 1, n, 1, 0, 0);
c6e89d6c
GM
6769 }
6770 }
6771 else
6772 insert_1 (s, nbytes, 1, 0, 0);
6773 }
6774
6775 return 0;
6776}
6777
6778
6779/* Clear messages. CURRENT_P non-zero means clear the current
6780 message. LAST_DISPLAYED_P non-zero means clear the message
6781 last displayed. */
6782
6783void
6784clear_message (current_p, last_displayed_p)
6785 int current_p, last_displayed_p;
6786{
6787 if (current_p)
6788 echo_area_buffer[0] = Qnil;
6789
6790 if (last_displayed_p)
6791 echo_area_buffer[1] = Qnil;
6792
6793 message_buf_print = 0;
6794}
6795
6796/* Clear garbaged frames.
6797
6798 This function is used where the old redisplay called
6799 redraw_garbaged_frames which in turn called redraw_frame which in
6800 turn called clear_frame. The call to clear_frame was a source of
6801 flickering. I believe a clear_frame is not necessary. It should
6802 suffice in the new redisplay to invalidate all current matrices,
6803 and ensure a complete redisplay of all windows. */
6804
6805static void
6806clear_garbaged_frames ()
6807{
5f5c8ee5
GM
6808 if (frame_garbaged)
6809 {
5f5c8ee5
GM
6810 Lisp_Object tail, frame;
6811
6812 FOR_EACH_FRAME (tail, frame)
6813 {
6814 struct frame *f = XFRAME (frame);
6815
6816 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
6817 {
6818 clear_current_matrices (f);
6819 f->garbaged = 0;
6820 }
6821 }
6822
6823 frame_garbaged = 0;
6824 ++windows_or_buffers_changed;
6825 }
c6e89d6c 6826}
5f5c8ee5 6827
5f5c8ee5 6828
886bd6f2
GM
6829/* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
6830 is non-zero update selected_frame. Value is non-zero if the
c6e89d6c 6831 mini-windows height has been changed. */
5f5c8ee5 6832
c6e89d6c
GM
6833static int
6834echo_area_display (update_frame_p)
6835 int update_frame_p;
6836{
6837 Lisp_Object mini_window;
6838 struct window *w;
6839 struct frame *f;
6840 int window_height_changed_p = 0;
886bd6f2 6841 struct frame *sf = SELECTED_FRAME ();
c6e89d6c 6842
886bd6f2 6843 mini_window = FRAME_MINIBUF_WINDOW (sf);
c6e89d6c
GM
6844 w = XWINDOW (mini_window);
6845 f = XFRAME (WINDOW_FRAME (w));
6846
6847 /* Don't display if frame is invisible or not yet initialized. */
6848 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
6849 return 0;
5f5c8ee5 6850
1a578e9b
AC
6851/* The terminal frame is used as the first Emacs frame on the Mac OS. */
6852#ifndef macintosh
1ab3e082 6853#ifdef HAVE_WINDOW_SYSTEM
c6e89d6c
GM
6854 /* When Emacs starts, selected_frame may be a visible terminal
6855 frame, even if we run under a window system. If we let this
6856 through, a message would be displayed on the terminal. */
622e3754
GM
6857 if (EQ (selected_frame, Vterminal_frame)
6858 && !NILP (Vwindow_system))
c6e89d6c 6859 return 0;
1ab3e082 6860#endif /* HAVE_WINDOW_SYSTEM */
1a578e9b 6861#endif
c6e89d6c
GM
6862
6863 /* Redraw garbaged frames. */
6864 if (frame_garbaged)
6865 clear_garbaged_frames ();
6866
6867 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
6868 {
6869 echo_area_window = mini_window;
6870 window_height_changed_p = display_echo_area (w);
5f5c8ee5 6871 w->must_be_updated_p = 1;
c59c668a 6872
d4358b37
GM
6873 /* Update the display, unless called from redisplay_internal.
6874 Also don't update the screen during redisplay itself. The
6875 update will happen at the end of redisplay, and an update
6876 here could cause confusion. */
6877 if (update_frame_p && !redisplaying_p)
5f5c8ee5 6878 {
715e84c9 6879 int n = 0;
edc68111 6880
715e84c9
GM
6881 /* If the display update has been interrupted by pending
6882 input, update mode lines in the frame. Due to the
6883 pending input, it might have been that redisplay hasn't
6884 been called, so that mode lines above the echo area are
6885 garbaged. This looks odd, so we prevent it here. */
6886 if (!display_completed)
6887 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
6888
8af1308e
GM
6889 if (window_height_changed_p
6890 /* Don't do this if Emacs is shutting down. Redisplay
6891 needs to run hooks. */
6892 && !NILP (Vrun_hooks))
c59c668a 6893 {
c06017fb
GM
6894 /* Must update other windows. Likewise as in other
6895 cases, don't let this update be interrupted by
6896 pending input. */
6897 int count = BINDING_STACK_SIZE ();
6898 specbind (Qredisplay_dont_pause, Qt);
edc68111 6899 windows_or_buffers_changed = 1;
c59c668a 6900 redisplay_internal (0);
c06017fb 6901 unbind_to (count, Qnil);
c59c668a 6902 }
715e84c9 6903 else if (FRAME_WINDOW_P (f) && n == 0)
5f5c8ee5 6904 {
edc68111 6905 /* Window configuration is the same as before.
715e84c9
GM
6906 Can do with a display update of the echo area,
6907 unless we displayed some mode lines. */
5f5c8ee5
GM
6908 update_single_window (w, 1);
6909 rif->flush_display (f);
6910 }
6911 else
6912 update_frame (f, 1, 1);
31b6671b
GM
6913
6914 /* If cursor is in the echo area, make sure that the next
6915 redisplay displays the minibuffer, so that the cursor will
6916 be replaced with what the minibuffer wants. */
6917 if (cursor_in_echo_area)
6918 ++windows_or_buffers_changed;
5f5c8ee5
GM
6919 }
6920 }
6921 else if (!EQ (mini_window, selected_window))
6922 windows_or_buffers_changed++;
c59c668a
GM
6923
6924 /* Last displayed message is now the current message. */
dd2eb166
GM
6925 echo_area_buffer[1] = echo_area_buffer[0];
6926
5f5c8ee5
GM
6927 /* Prevent redisplay optimization in redisplay_internal by resetting
6928 this_line_start_pos. This is done because the mini-buffer now
6929 displays the message instead of its buffer text. */
6930 if (EQ (mini_window, selected_window))
6931 CHARPOS (this_line_start_pos) = 0;
c6e89d6c
GM
6932
6933 return window_height_changed_p;
5f5c8ee5
GM
6934}
6935
6936
6937\f
6938/***********************************************************************
6939 Frame Titles
6940 ***********************************************************************/
6941
6942
6943#ifdef HAVE_WINDOW_SYSTEM
6944
6945/* A buffer for constructing frame titles in it; allocated from the
6946 heap in init_xdisp and resized as needed in store_frame_title_char. */
6947
6948static char *frame_title_buf;
6949
6950/* The buffer's end, and a current output position in it. */
6951
6952static char *frame_title_buf_end;
6953static char *frame_title_ptr;
6954
6955
6956/* Store a single character C for the frame title in frame_title_buf.
6957 Re-allocate frame_title_buf if necessary. */
6958
6959static void
6960store_frame_title_char (c)
6961 char c;
6962{
6963 /* If output position has reached the end of the allocated buffer,
6964 double the buffer's size. */
6965 if (frame_title_ptr == frame_title_buf_end)
6966 {
6967 int len = frame_title_ptr - frame_title_buf;
6968 int new_size = 2 * len * sizeof *frame_title_buf;
6969 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
6970 frame_title_buf_end = frame_title_buf + new_size;
6971 frame_title_ptr = frame_title_buf + len;
6972 }
6973
6974 *frame_title_ptr++ = c;
6975}
6976
6977
6978/* Store part of a frame title in frame_title_buf, beginning at
d26b89b8
KH
6979 frame_title_ptr. STR is the string to store. Do not copy
6980 characters that yield more columns than PRECISION; PRECISION <= 0
6981 means copy the whole string. Pad with spaces until FIELD_WIDTH
6982 number of characters have been copied; FIELD_WIDTH <= 0 means don't
6983 pad. Called from display_mode_element when it is used to build a
6984 frame title. */
5f5c8ee5
GM
6985
6986static int
6987store_frame_title (str, field_width, precision)
6988 unsigned char *str;
6989 int field_width, precision;
6990{
6991 int n = 0;
d26b89b8 6992 int dummy, nbytes, width;
5f5c8ee5
GM
6993
6994 /* Copy at most PRECISION chars from STR. */
d26b89b8
KH
6995 nbytes = strlen (str);
6996 n+= c_string_width (str, nbytes, precision, &dummy, &nbytes);
6997 while (nbytes--)
6998 store_frame_title_char (*str++);
5f5c8ee5
GM
6999
7000 /* Fill up with spaces until FIELD_WIDTH reached. */
7001 while (field_width > 0
7002 && n < field_width)
7003 {
7004 store_frame_title_char (' ');
7005 ++n;
7006 }
7007
7008 return n;
7009}
7010
7011
7012/* Set the title of FRAME, if it has changed. The title format is
7013 Vicon_title_format if FRAME is iconified, otherwise it is
7014 frame_title_format. */
7015
7016static void
7017x_consider_frame_title (frame)
7018 Lisp_Object frame;
7019{
7020 struct frame *f = XFRAME (frame);
7021
7022 if (FRAME_WINDOW_P (f)
7023 || FRAME_MINIBUF_ONLY_P (f)
7024 || f->explicit_name)
7025 {
7026 /* Do we have more than one visible frame on this X display? */
7027 Lisp_Object tail;
7028 Lisp_Object fmt;
7029 struct buffer *obuf;
7030 int len;
7031 struct it it;
7032
9472f927 7033 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 7034 {
9472f927 7035 struct frame *tf = XFRAME (XCAR (tail));
5f5c8ee5
GM
7036
7037 if (tf != f
7038 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
7039 && !FRAME_MINIBUF_ONLY_P (tf)
7040 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
7041 break;
7042 }
7043
7044 /* Set global variable indicating that multiple frames exist. */
7045 multiple_frames = CONSP (tail);
7046
7047 /* Switch to the buffer of selected window of the frame. Set up
7048 frame_title_ptr so that display_mode_element will output into it;
7049 then display the title. */
7050 obuf = current_buffer;
7051 Fset_buffer (XWINDOW (f->selected_window)->buffer);
7052 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
7053 frame_title_ptr = frame_title_buf;
7054 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
7055 NULL, DEFAULT_FACE_ID);
d26b89b8
KH
7056 display_mode_element (&it, 0, -1, -1, fmt);
7057 len = frame_title_ptr - frame_title_buf;
5f5c8ee5
GM
7058 frame_title_ptr = NULL;
7059 set_buffer_internal (obuf);
7060
7061 /* Set the title only if it's changed. This avoids consing in
7062 the common case where it hasn't. (If it turns out that we've
7063 already wasted too much time by walking through the list with
7064 display_mode_element, then we might need to optimize at a
7065 higher level than this.) */
7066 if (! STRINGP (f->name)
7067 || STRING_BYTES (XSTRING (f->name)) != len
7068 || bcmp (frame_title_buf, XSTRING (f->name)->data, len) != 0)
7069 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
7070 }
7071}
7072
7073#else /* not HAVE_WINDOW_SYSTEM */
7074
7075#define frame_title_ptr ((char *)0)
7076#define store_frame_title(str, mincol, maxcol) 0
7077
7078#endif /* not HAVE_WINDOW_SYSTEM */
7079
7080
7081
7082\f
7083/***********************************************************************
7084 Menu Bars
7085 ***********************************************************************/
7086
7087
7088/* Prepare for redisplay by updating menu-bar item lists when
7089 appropriate. This can call eval. */
7090
7091void
7092prepare_menu_bars ()
7093{
7094 int all_windows;
7095 struct gcpro gcpro1, gcpro2;
7096 struct frame *f;
6b5c4794 7097 Lisp_Object tooltip_frame;
5f5c8ee5
GM
7098
7099#ifdef HAVE_X_WINDOWS
7100 tooltip_frame = tip_frame;
7101#else
6b5c4794 7102 tooltip_frame = Qnil;
5f5c8ee5
GM
7103#endif
7104
7105 /* Update all frame titles based on their buffer names, etc. We do
7106 this before the menu bars so that the buffer-menu will show the
7107 up-to-date frame titles. */
7108#ifdef HAVE_WINDOW_SYSTEM
7109 if (windows_or_buffers_changed || update_mode_lines)
7110 {
7111 Lisp_Object tail, frame;
7112
7113 FOR_EACH_FRAME (tail, frame)
7114 {
7115 f = XFRAME (frame);
6b5c4794 7116 if (!EQ (frame, tooltip_frame)
5f5c8ee5
GM
7117 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
7118 x_consider_frame_title (frame);
7119 }
7120 }
7121#endif /* HAVE_WINDOW_SYSTEM */
7122
7123 /* Update the menu bar item lists, if appropriate. This has to be
7124 done before any actual redisplay or generation of display lines. */
7125 all_windows = (update_mode_lines
7126 || buffer_shared > 1
7127 || windows_or_buffers_changed);
7128 if (all_windows)
7129 {
7130 Lisp_Object tail, frame;
2d27dae2 7131 int count = BINDING_STACK_SIZE ();
5f5c8ee5
GM
7132
7133 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7134
7135 FOR_EACH_FRAME (tail, frame)
7136 {
7137 f = XFRAME (frame);
7138
7139 /* Ignore tooltip frame. */
6b5c4794 7140 if (EQ (frame, tooltip_frame))
5f5c8ee5
GM
7141 continue;
7142
7143 /* If a window on this frame changed size, report that to
7144 the user and clear the size-change flag. */
7145 if (FRAME_WINDOW_SIZES_CHANGED (f))
7146 {
7147 Lisp_Object functions;
7148
7149 /* Clear flag first in case we get an error below. */
7150 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
7151 functions = Vwindow_size_change_functions;
7152 GCPRO2 (tail, functions);
7153
7154 while (CONSP (functions))
7155 {
7156 call1 (XCAR (functions), frame);
7157 functions = XCDR (functions);
7158 }
7159 UNGCPRO;
7160 }
7161
7162 GCPRO1 (tail);
7163 update_menu_bar (f, 0);
7164#ifdef HAVE_WINDOW_SYSTEM
e037b9ec 7165 update_tool_bar (f, 0);
5f5c8ee5
GM
7166#endif
7167 UNGCPRO;
7168 }
7169
7170 unbind_to (count, Qnil);
7171 }
7172 else
7173 {
886bd6f2
GM
7174 struct frame *sf = SELECTED_FRAME ();
7175 update_menu_bar (sf, 1);
5f5c8ee5 7176#ifdef HAVE_WINDOW_SYSTEM
886bd6f2 7177 update_tool_bar (sf, 1);
5f5c8ee5
GM
7178#endif
7179 }
7180
7181 /* Motif needs this. See comment in xmenu.c. Turn it off when
7182 pending_menu_activation is not defined. */
7183#ifdef USE_X_TOOLKIT
7184 pending_menu_activation = 0;
7185#endif
7186}
7187
7188
7189/* Update the menu bar item list for frame F. This has to be done
7190 before we start to fill in any display lines, because it can call
7191 eval.
7192
7193 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
7194
7195static void
7196update_menu_bar (f, save_match_data)
7197 struct frame *f;
7198 int save_match_data;
7199{
7200 Lisp_Object window;
7201 register struct window *w;
7202
e1477f43
GM
7203 /* If called recursively during a menu update, do nothing. This can
7204 happen when, for instance, an activate-menubar-hook causes a
7205 redisplay. */
7206 if (inhibit_menubar_update)
7207 return;
7208
5f5c8ee5
GM
7209 window = FRAME_SELECTED_WINDOW (f);
7210 w = XWINDOW (window);
7211
7212 if (update_mode_lines)
7213 w->update_mode_line = Qt;
7214
7215 if (FRAME_WINDOW_P (f)
7216 ?
1a578e9b 7217#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5
GM
7218 FRAME_EXTERNAL_MENU_BAR (f)
7219#else
7220 FRAME_MENU_BAR_LINES (f) > 0
7221#endif
7222 : FRAME_MENU_BAR_LINES (f) > 0)
7223 {
7224 /* If the user has switched buffers or windows, we need to
7225 recompute to reflect the new bindings. But we'll
7226 recompute when update_mode_lines is set too; that means
7227 that people can use force-mode-line-update to request
7228 that the menu bar be recomputed. The adverse effect on
7229 the rest of the redisplay algorithm is about the same as
7230 windows_or_buffers_changed anyway. */
7231 if (windows_or_buffers_changed
7232 || !NILP (w->update_mode_line)
7233 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7234 < BUF_MODIFF (XBUFFER (w->buffer)))
7235 != !NILP (w->last_had_star))
7236 || ((!NILP (Vtransient_mark_mode)
7237 && !NILP (XBUFFER (w->buffer)->mark_active))
7238 != !NILP (w->region_showing)))
7239 {
7240 struct buffer *prev = current_buffer;
2d27dae2 7241 int count = BINDING_STACK_SIZE ();
5f5c8ee5 7242
e1477f43
GM
7243 specbind (Qinhibit_menubar_update, Qt);
7244
5f5c8ee5
GM
7245 set_buffer_internal_1 (XBUFFER (w->buffer));
7246 if (save_match_data)
7247 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7248 if (NILP (Voverriding_local_map_menu_flag))
7249 {
7250 specbind (Qoverriding_terminal_local_map, Qnil);
7251 specbind (Qoverriding_local_map, Qnil);
7252 }
7253
7254 /* Run the Lucid hook. */
65048e97 7255 safe_run_hooks (Qactivate_menubar_hook);
5f5c8ee5
GM
7256
7257 /* If it has changed current-menubar from previous value,
7258 really recompute the menu-bar from the value. */
7259 if (! NILP (Vlucid_menu_bar_dirty_flag))
7260 call0 (Qrecompute_lucid_menubar);
7261
7262 safe_run_hooks (Qmenu_bar_update_hook);
7263 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
7264
7265 /* Redisplay the menu bar in case we changed it. */
1a578e9b
AC
7266#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
7267 if (FRAME_WINDOW_P (f)
7268#if defined (macintosh)
7269 /* All frames on Mac OS share the same menubar. So only the
7270 selected frame should be allowed to set it. */
7271 && f == SELECTED_FRAME ()
7272#endif
7273 )
5f5c8ee5
GM
7274 set_frame_menubar (f, 0, 0);
7275 else
7276 /* On a terminal screen, the menu bar is an ordinary screen
7277 line, and this makes it get updated. */
7278 w->update_mode_line = Qt;
7279#else /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7280 /* In the non-toolkit version, the menu bar is an ordinary screen
7281 line, and this makes it get updated. */
7282 w->update_mode_line = Qt;
7283#endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI) */
7284
7285 unbind_to (count, Qnil);
7286 set_buffer_internal_1 (prev);
7287 }
7288 }
7289}
7290
7291
7292\f
7293/***********************************************************************
e037b9ec 7294 Tool-bars
5f5c8ee5
GM
7295 ***********************************************************************/
7296
7297#ifdef HAVE_WINDOW_SYSTEM
7298
e037b9ec 7299/* Update the tool-bar item list for frame F. This has to be done
5f5c8ee5
GM
7300 before we start to fill in any display lines. Called from
7301 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
7302 and restore it here. */
7303
7304static void
e037b9ec 7305update_tool_bar (f, save_match_data)
5f5c8ee5
GM
7306 struct frame *f;
7307 int save_match_data;
7308{
e037b9ec
GM
7309 if (WINDOWP (f->tool_bar_window)
7310 && XFASTINT (XWINDOW (f->tool_bar_window)->height) > 0)
5f5c8ee5
GM
7311 {
7312 Lisp_Object window;
7313 struct window *w;
7314
7315 window = FRAME_SELECTED_WINDOW (f);
7316 w = XWINDOW (window);
7317
7318 /* If the user has switched buffers or windows, we need to
7319 recompute to reflect the new bindings. But we'll
7320 recompute when update_mode_lines is set too; that means
7321 that people can use force-mode-line-update to request
7322 that the menu bar be recomputed. The adverse effect on
7323 the rest of the redisplay algorithm is about the same as
7324 windows_or_buffers_changed anyway. */
7325 if (windows_or_buffers_changed
7326 || !NILP (w->update_mode_line)
7327 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
7328 < BUF_MODIFF (XBUFFER (w->buffer)))
7329 != !NILP (w->last_had_star))
7330 || ((!NILP (Vtransient_mark_mode)
7331 && !NILP (XBUFFER (w->buffer)->mark_active))
7332 != !NILP (w->region_showing)))
7333 {
7334 struct buffer *prev = current_buffer;
2d27dae2 7335 int count = BINDING_STACK_SIZE ();
a2889657 7336
5f5c8ee5
GM
7337 /* Set current_buffer to the buffer of the selected
7338 window of the frame, so that we get the right local
7339 keymaps. */
7340 set_buffer_internal_1 (XBUFFER (w->buffer));
1f40cad2 7341
5f5c8ee5
GM
7342 /* Save match data, if we must. */
7343 if (save_match_data)
7344 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
7345
7346 /* Make sure that we don't accidentally use bogus keymaps. */
7347 if (NILP (Voverriding_local_map_menu_flag))
7348 {
7349 specbind (Qoverriding_terminal_local_map, Qnil);
7350 specbind (Qoverriding_local_map, Qnil);
1f40cad2 7351 }
1f40cad2 7352
e037b9ec 7353 /* Build desired tool-bar items from keymaps. */
7464726e
GM
7354 f->tool_bar_items
7355 = tool_bar_items (f->tool_bar_items, &f->n_tool_bar_items);
5f5c8ee5 7356
e037b9ec 7357 /* Redisplay the tool-bar in case we changed it. */
5f5c8ee5
GM
7358 w->update_mode_line = Qt;
7359
7360 unbind_to (count, Qnil);
7361 set_buffer_internal_1 (prev);
81d478f3 7362 }
a2889657
JB
7363 }
7364}
7365
6c4429a5 7366
e037b9ec 7367/* Set F->desired_tool_bar_string to a Lisp string representing frame
7464726e 7368 F's desired tool-bar contents. F->tool_bar_items must have
5f5c8ee5
GM
7369 been set up previously by calling prepare_menu_bars. */
7370
a2889657 7371static void
e037b9ec 7372build_desired_tool_bar_string (f)
5f5c8ee5 7373 struct frame *f;
a2889657 7374{
a23887b9 7375 int i, size, size_needed;
5f5c8ee5
GM
7376 struct gcpro gcpro1, gcpro2, gcpro3;
7377 Lisp_Object image, plist, props;
a2889657 7378
5f5c8ee5
GM
7379 image = plist = props = Qnil;
7380 GCPRO3 (image, plist, props);
a2889657 7381
e037b9ec 7382 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
5f5c8ee5
GM
7383 Otherwise, make a new string. */
7384
7385 /* The size of the string we might be able to reuse. */
e037b9ec
GM
7386 size = (STRINGP (f->desired_tool_bar_string)
7387 ? XSTRING (f->desired_tool_bar_string)->size
5f5c8ee5
GM
7388 : 0);
7389
b94c0d9c 7390 /* We need one space in the string for each image. */
a23887b9 7391 size_needed = f->n_tool_bar_items;
b94c0d9c
GM
7392
7393 /* Reuse f->desired_tool_bar_string, if possible. */
cb2ddc53 7394 if (size < size_needed || NILP (f->desired_tool_bar_string))
6fc556fd
KR
7395 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
7396 make_number (' '));
5f5c8ee5
GM
7397 else
7398 {
7399 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
7400 Fremove_text_properties (make_number (0), make_number (size),
e037b9ec 7401 props, f->desired_tool_bar_string);
5f5c8ee5 7402 }
a2889657 7403
5f5c8ee5 7404 /* Put a `display' property on the string for the images to display,
e037b9ec
GM
7405 put a `menu_item' property on tool-bar items with a value that
7406 is the index of the item in F's tool-bar item vector. */
a23887b9 7407 for (i = 0; i < f->n_tool_bar_items; ++i)
a2889657 7408 {
7464726e 7409#define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
5f5c8ee5 7410
e037b9ec
GM
7411 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
7412 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
35a41507 7413 int hmargin, vmargin, relief, idx, end;
ecd01a0e 7414 extern Lisp_Object QCrelief, QCmargin, QCconversion, Qimage;
5f5c8ee5
GM
7415 extern Lisp_Object Qlaplace;
7416
7417 /* If image is a vector, choose the image according to the
7418 button state. */
e037b9ec 7419 image = PROP (TOOL_BAR_ITEM_IMAGES);
5f5c8ee5
GM
7420 if (VECTORP (image))
7421 {
5f5c8ee5
GM
7422 if (enabled_p)
7423 idx = (selected_p
e037b9ec
GM
7424 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
7425 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
5f5c8ee5
GM
7426 else
7427 idx = (selected_p
e037b9ec
GM
7428 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
7429 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
5f5c8ee5 7430
37e4e482
GM
7431 xassert (ASIZE (image) >= idx);
7432 image = AREF (image, idx);
5f5c8ee5 7433 }
37e4e482
GM
7434 else
7435 idx = -1;
5f5c8ee5
GM
7436
7437 /* Ignore invalid image specifications. */
7438 if (!valid_image_p (image))
7439 continue;
7440
e037b9ec 7441 /* Display the tool-bar button pressed, or depressed. */
5f5c8ee5
GM
7442 plist = Fcopy_sequence (XCDR (image));
7443
7444 /* Compute margin and relief to draw. */
c3d76173
GM
7445 relief = (tool_bar_button_relief > 0
7446 ? tool_bar_button_relief
7447 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
35a41507
GM
7448 hmargin = vmargin = relief;
7449
7450 if (INTEGERP (Vtool_bar_button_margin)
7451 && XINT (Vtool_bar_button_margin) > 0)
7452 {
7453 hmargin += XFASTINT (Vtool_bar_button_margin);
7454 vmargin += XFASTINT (Vtool_bar_button_margin);
7455 }
7456 else if (CONSP (Vtool_bar_button_margin))
7457 {
7458 if (INTEGERP (XCAR (Vtool_bar_button_margin))
7459 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
7460 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
7461
7462 if (INTEGERP (XCDR (Vtool_bar_button_margin))
7463 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
7464 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
7465 }
5f5c8ee5 7466
e037b9ec 7467 if (auto_raise_tool_bar_buttons_p)
5f5c8ee5
GM
7468 {
7469 /* Add a `:relief' property to the image spec if the item is
7470 selected. */
7471 if (selected_p)
7472 {
7473 plist = Fplist_put (plist, QCrelief, make_number (-relief));
35a41507
GM
7474 hmargin -= relief;
7475 vmargin -= relief;
5f5c8ee5
GM
7476 }
7477 }
7478 else
7479 {
7480 /* If image is selected, display it pressed, i.e. with a
7481 negative relief. If it's not selected, display it with a
7482 raised relief. */
7483 plist = Fplist_put (plist, QCrelief,
7484 (selected_p
7485 ? make_number (-relief)
7486 : make_number (relief)));
35a41507
GM
7487 hmargin -= relief;
7488 vmargin -= relief;
5f5c8ee5
GM
7489 }
7490
7491 /* Put a margin around the image. */
35a41507
GM
7492 if (hmargin || vmargin)
7493 {
7494 if (hmargin == vmargin)
7495 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
7496 else
7497 plist = Fplist_put (plist, QCmargin,
7498 Fcons (make_number (hmargin),
7499 make_number (vmargin)));
7500 }
5f5c8ee5 7501
37e4e482
GM
7502 /* If button is not enabled, and we don't have special images
7503 for the disabled state, make the image appear disabled by
5f5c8ee5 7504 applying an appropriate algorithm to it. */
37e4e482 7505 if (!enabled_p && idx < 0)
ecd01a0e 7506 plist = Fplist_put (plist, QCconversion, Qdisabled);
5f5c8ee5
GM
7507
7508 /* Put a `display' text property on the string for the image to
7509 display. Put a `menu-item' property on the string that gives
e037b9ec 7510 the start of this item's properties in the tool-bar items
5f5c8ee5
GM
7511 vector. */
7512 image = Fcons (Qimage, plist);
7513 props = list4 (Qdisplay, image,
a23887b9
GM
7514 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
7515
7516 /* Let the last image hide all remaining spaces in the tool bar
7517 string. The string can be longer than needed when we reuse a
7518 previous string. */
7519 if (i + 1 == f->n_tool_bar_items)
7520 end = XSTRING (f->desired_tool_bar_string)->size;
7521 else
7522 end = i + 1;
7523 Fadd_text_properties (make_number (i), make_number (end),
e037b9ec 7524 props, f->desired_tool_bar_string);
5f5c8ee5 7525#undef PROP
a2889657
JB
7526 }
7527
5f5c8ee5
GM
7528 UNGCPRO;
7529}
7530
7531
e037b9ec 7532/* Display one line of the tool-bar of frame IT->f. */
5f5c8ee5
GM
7533
7534static void
e037b9ec 7535display_tool_bar_line (it)
5f5c8ee5
GM
7536 struct it *it;
7537{
7538 struct glyph_row *row = it->glyph_row;
7539 int max_x = it->last_visible_x;
7540 struct glyph *last;
7541
7542 prepare_desired_row (row);
7543 row->y = it->current_y;
90aa2856
GM
7544
7545 /* Note that this isn't made use of if the face hasn't a box,
7546 so there's no need to check the face here. */
7547 it->start_of_box_run_p = 1;
5f5c8ee5
GM
7548
7549 while (it->current_x < max_x)
a2889657 7550 {
5f5c8ee5 7551 int x_before, x, n_glyphs_before, i, nglyphs;
a2f016e3 7552
5f5c8ee5
GM
7553 /* Get the next display element. */
7554 if (!get_next_display_element (it))
7555 break;
73af359d 7556
5f5c8ee5
GM
7557 /* Produce glyphs. */
7558 x_before = it->current_x;
7559 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
7560 PRODUCE_GLYPHS (it);
daa37602 7561
5f5c8ee5
GM
7562 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
7563 i = 0;
7564 x = x_before;
7565 while (i < nglyphs)
7566 {
7567 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
7568
7569 if (x + glyph->pixel_width > max_x)
7570 {
7571 /* Glyph doesn't fit on line. */
7572 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
7573 it->current_x = x;
7574 goto out;
7575 }
daa37602 7576
5f5c8ee5
GM
7577 ++it->hpos;
7578 x += glyph->pixel_width;
7579 ++i;
7580 }
7581
7582 /* Stop at line ends. */
7583 if (ITERATOR_AT_END_OF_LINE_P (it))
7584 break;
7585
cafafe0b 7586 set_iterator_to_next (it, 1);
a2889657 7587 }
a2889657 7588
5f5c8ee5 7589 out:;
a2889657 7590
5f5c8ee5
GM
7591 row->displays_text_p = row->used[TEXT_AREA] != 0;
7592 extend_face_to_end_of_line (it);
7593 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
7594 last->right_box_line_p = 1;
90aa2856
GM
7595 if (last == row->glyphs[TEXT_AREA])
7596 last->left_box_line_p = 1;
5f5c8ee5
GM
7597 compute_line_metrics (it);
7598
e037b9ec 7599 /* If line is empty, make it occupy the rest of the tool-bar. */
5f5c8ee5
GM
7600 if (!row->displays_text_p)
7601 {
312246d1
GM
7602 row->height = row->phys_height = it->last_visible_y - row->y;
7603 row->ascent = row->phys_ascent = 0;
5f5c8ee5
GM
7604 }
7605
7606 row->full_width_p = 1;
7607 row->continued_p = 0;
7608 row->truncated_on_left_p = 0;
7609 row->truncated_on_right_p = 0;
7610
7611 it->current_x = it->hpos = 0;
7612 it->current_y += row->height;
7613 ++it->vpos;
7614 ++it->glyph_row;
a2889657 7615}
96a410bc 7616
5f5c8ee5 7617
e037b9ec 7618/* Value is the number of screen lines needed to make all tool-bar
5f5c8ee5 7619 items of frame F visible. */
96a410bc 7620
d39b6696 7621static int
e037b9ec 7622tool_bar_lines_needed (f)
5f5c8ee5 7623 struct frame *f;
d39b6696 7624{
e037b9ec 7625 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
7626 struct it it;
7627
e037b9ec
GM
7628 /* Initialize an iterator for iteration over
7629 F->desired_tool_bar_string in the tool-bar window of frame F. */
7630 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7631 it.first_visible_x = 0;
7632 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
e037b9ec 7633 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
5f5c8ee5
GM
7634
7635 while (!ITERATOR_AT_END_P (&it))
7636 {
7637 it.glyph_row = w->desired_matrix->rows;
7638 clear_glyph_row (it.glyph_row);
e037b9ec 7639 display_tool_bar_line (&it);
5f5c8ee5
GM
7640 }
7641
7642 return (it.current_y + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
d39b6696 7643}
96a410bc 7644
5f5c8ee5 7645
57c28064
GM
7646DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
7647 0, 1, 0,
7648 "Return the number of lines occupied by the tool bar of FRAME.")
7649 (frame)
7650 Lisp_Object frame;
7651{
7652 struct frame *f;
7653 struct window *w;
7654 int nlines = 0;
7655
7656 if (NILP (frame))
7657 frame = selected_frame;
7658 else
7659 CHECK_FRAME (frame, 0);
7660 f = XFRAME (frame);
7661
7662 if (WINDOWP (f->tool_bar_window)
7663 || (w = XWINDOW (f->tool_bar_window),
7664 XFASTINT (w->height) > 0))
7665 {
7666 update_tool_bar (f, 1);
7667 if (f->n_tool_bar_items)
7668 {
7669 build_desired_tool_bar_string (f);
7670 nlines = tool_bar_lines_needed (f);
7671 }
7672 }
7673
7674 return make_number (nlines);
7675}
7676
7677
e037b9ec 7678/* Display the tool-bar of frame F. Value is non-zero if tool-bar's
5f5c8ee5
GM
7679 height should be changed. */
7680
7681static int
e037b9ec 7682redisplay_tool_bar (f)
5f5c8ee5 7683 struct frame *f;
96a410bc 7684{
5f5c8ee5
GM
7685 struct window *w;
7686 struct it it;
7687 struct glyph_row *row;
7688 int change_height_p = 0;
7689
e037b9ec
GM
7690 /* If frame hasn't a tool-bar window or if it is zero-height, don't
7691 do anything. This means you must start with tool-bar-lines
5f5c8ee5 7692 non-zero to get the auto-sizing effect. Or in other words, you
e037b9ec
GM
7693 can turn off tool-bars by specifying tool-bar-lines zero. */
7694 if (!WINDOWP (f->tool_bar_window)
7695 || (w = XWINDOW (f->tool_bar_window),
5f5c8ee5
GM
7696 XFASTINT (w->height) == 0))
7697 return 0;
96a410bc 7698
e037b9ec
GM
7699 /* Set up an iterator for the tool-bar window. */
7700 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
5f5c8ee5
GM
7701 it.first_visible_x = 0;
7702 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
7703 row = it.glyph_row;
3450d04c 7704
e037b9ec
GM
7705 /* Build a string that represents the contents of the tool-bar. */
7706 build_desired_tool_bar_string (f);
7707 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
3450d04c 7708
e037b9ec 7709 /* Display as many lines as needed to display all tool-bar items. */
5f5c8ee5 7710 while (it.current_y < it.last_visible_y)
e037b9ec 7711 display_tool_bar_line (&it);
3450d04c 7712
e037b9ec 7713 /* It doesn't make much sense to try scrolling in the tool-bar
5f5c8ee5
GM
7714 window, so don't do it. */
7715 w->desired_matrix->no_scrolling_p = 1;
7716 w->must_be_updated_p = 1;
3450d04c 7717
e037b9ec 7718 if (auto_resize_tool_bars_p)
5f5c8ee5
GM
7719 {
7720 int nlines;
6e33e6f0
GM
7721
7722 /* If we couldn't display everything, change the tool-bar's
7723 height. */
7724 if (IT_STRING_CHARPOS (it) < it.end_charpos)
7725 change_height_p = 1;
5f5c8ee5
GM
7726
7727 /* If there are blank lines at the end, except for a partially
7728 visible blank line at the end that is smaller than
e037b9ec 7729 CANON_Y_UNIT, change the tool-bar's height. */
5f5c8ee5
GM
7730 row = it.glyph_row - 1;
7731 if (!row->displays_text_p
7732 && row->height >= CANON_Y_UNIT (f))
7733 change_height_p = 1;
7734
e037b9ec
GM
7735 /* If row displays tool-bar items, but is partially visible,
7736 change the tool-bar's height. */
5f5c8ee5
GM
7737 if (row->displays_text_p
7738 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
7739 change_height_p = 1;
7740
e037b9ec 7741 /* Resize windows as needed by changing the `tool-bar-lines'
5f5c8ee5
GM
7742 frame parameter. */
7743 if (change_height_p
e037b9ec 7744 && (nlines = tool_bar_lines_needed (f),
5f5c8ee5
GM
7745 nlines != XFASTINT (w->height)))
7746 {
e037b9ec 7747 extern Lisp_Object Qtool_bar_lines;
5f5c8ee5 7748 Lisp_Object frame;
e85ee976 7749 int old_height = XFASTINT (w->height);
5f5c8ee5
GM
7750
7751 XSETFRAME (frame, f);
7752 clear_glyph_matrix (w->desired_matrix);
7753 Fmodify_frame_parameters (frame,
e037b9ec 7754 Fcons (Fcons (Qtool_bar_lines,
5f5c8ee5
GM
7755 make_number (nlines)),
7756 Qnil));
e85ee976
GM
7757 if (XFASTINT (w->height) != old_height)
7758 fonts_changed_p = 1;
5f5c8ee5
GM
7759 }
7760 }
3450d04c 7761
5f5c8ee5 7762 return change_height_p;
96a410bc 7763}
90adcf20 7764
5f5c8ee5 7765
e037b9ec
GM
7766/* Get information about the tool-bar item which is displayed in GLYPH
7767 on frame F. Return in *PROP_IDX the index where tool-bar item
7464726e 7768 properties start in F->tool_bar_items. Value is zero if
e037b9ec 7769 GLYPH doesn't display a tool-bar item. */
5f5c8ee5
GM
7770
7771int
e037b9ec 7772tool_bar_item_info (f, glyph, prop_idx)
5f5c8ee5
GM
7773 struct frame *f;
7774 struct glyph *glyph;
7775 int *prop_idx;
90adcf20 7776{
5f5c8ee5
GM
7777 Lisp_Object prop;
7778 int success_p;
7779
7780 /* Get the text property `menu-item' at pos. The value of that
7781 property is the start index of this item's properties in
7464726e 7782 F->tool_bar_items. */
5f5c8ee5 7783 prop = Fget_text_property (make_number (glyph->charpos),
e037b9ec 7784 Qmenu_item, f->current_tool_bar_string);
5f5c8ee5
GM
7785 if (INTEGERP (prop))
7786 {
7787 *prop_idx = XINT (prop);
7788 success_p = 1;
7789 }
7790 else
7791 success_p = 0;
90adcf20 7792
5f5c8ee5
GM
7793 return success_p;
7794}
7795
7796#endif /* HAVE_WINDOW_SYSTEM */
90adcf20 7797
feb0c42f 7798
5f5c8ee5
GM
7799\f
7800/************************************************************************
7801 Horizontal scrolling
7802 ************************************************************************/
feb0c42f 7803
5f5c8ee5
GM
7804static int hscroll_window_tree P_ ((Lisp_Object));
7805static int hscroll_windows P_ ((Lisp_Object));
feb0c42f 7806
5f5c8ee5
GM
7807/* For all leaf windows in the window tree rooted at WINDOW, set their
7808 hscroll value so that PT is (i) visible in the window, and (ii) so
7809 that it is not within a certain margin at the window's left and
7810 right border. Value is non-zero if any window's hscroll has been
7811 changed. */
7812
7813static int
7814hscroll_window_tree (window)
7815 Lisp_Object window;
7816{
7817 int hscrolled_p = 0;
7818
7819 while (WINDOWP (window))
90adcf20 7820 {
5f5c8ee5
GM
7821 struct window *w = XWINDOW (window);
7822
7823 if (WINDOWP (w->hchild))
7824 hscrolled_p |= hscroll_window_tree (w->hchild);
7825 else if (WINDOWP (w->vchild))
7826 hscrolled_p |= hscroll_window_tree (w->vchild);
7827 else if (w->cursor.vpos >= 0)
7828 {
7829 int hscroll_margin, text_area_x, text_area_y;
7830 int text_area_width, text_area_height;
92a90e89
GM
7831 struct glyph_row *current_cursor_row
7832 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
7833 struct glyph_row *desired_cursor_row
7834 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
7835 struct glyph_row *cursor_row
7836 = (desired_cursor_row->enabled_p
7837 ? desired_cursor_row
7838 : current_cursor_row);
a2725ab2 7839
5f5c8ee5
GM
7840 window_box (w, TEXT_AREA, &text_area_x, &text_area_y,
7841 &text_area_width, &text_area_height);
90adcf20 7842
5f5c8ee5
GM
7843 /* Scroll when cursor is inside this scroll margin. */
7844 hscroll_margin = 5 * CANON_X_UNIT (XFRAME (w->frame));
7845
7846 if ((XFASTINT (w->hscroll)
7847 && w->cursor.x < hscroll_margin)
92a90e89
GM
7848 || (cursor_row->enabled_p
7849 && cursor_row->truncated_on_right_p
5f5c8ee5 7850 && (w->cursor.x > text_area_width - hscroll_margin)))
08b610e4 7851 {
5f5c8ee5
GM
7852 struct it it;
7853 int hscroll;
7854 struct buffer *saved_current_buffer;
7855 int pt;
7856
7857 /* Find point in a display of infinite width. */
7858 saved_current_buffer = current_buffer;
7859 current_buffer = XBUFFER (w->buffer);
7860
7861 if (w == XWINDOW (selected_window))
7862 pt = BUF_PT (current_buffer);
7863 else
08b610e4 7864 {
5f5c8ee5
GM
7865 pt = marker_position (w->pointm);
7866 pt = max (BEGV, pt);
7867 pt = min (ZV, pt);
7868 }
7869
7870 /* Move iterator to pt starting at cursor_row->start in
7871 a line with infinite width. */
7872 init_to_row_start (&it, w, cursor_row);
7873 it.last_visible_x = INFINITY;
7874 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
7875 current_buffer = saved_current_buffer;
7876
7877 /* Center cursor in window. */
7878 hscroll = (max (0, it.current_x - text_area_width / 2)
7879 / CANON_X_UNIT (it.f));
3172f70b 7880 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
5f5c8ee5
GM
7881
7882 /* Don't call Fset_window_hscroll if value hasn't
7883 changed because it will prevent redisplay
7884 optimizations. */
7885 if (XFASTINT (w->hscroll) != hscroll)
7886 {
3172f70b
GM
7887 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7888 w->hscroll = make_number (hscroll);
5f5c8ee5 7889 hscrolled_p = 1;
08b610e4 7890 }
08b610e4 7891 }
08b610e4 7892 }
a2725ab2 7893
5f5c8ee5 7894 window = w->next;
90adcf20 7895 }
cd6dfed6 7896
5f5c8ee5
GM
7897 /* Value is non-zero if hscroll of any leaf window has been changed. */
7898 return hscrolled_p;
7899}
7900
7901
7902/* Set hscroll so that cursor is visible and not inside horizontal
7903 scroll margins for all windows in the tree rooted at WINDOW. See
7904 also hscroll_window_tree above. Value is non-zero if any window's
7905 hscroll has been changed. If it has, desired matrices on the frame
7906 of WINDOW are cleared. */
7907
7908static int
7909hscroll_windows (window)
7910 Lisp_Object window;
7911{
d475bcb8
GM
7912 int hscrolled_p;
7913
7914 if (automatic_hscrolling_p)
7915 {
7916 hscrolled_p = hscroll_window_tree (window);
7917 if (hscrolled_p)
7918 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
7919 }
7920 else
7921 hscrolled_p = 0;
5f5c8ee5 7922 return hscrolled_p;
90adcf20 7923}
5f5c8ee5
GM
7924
7925
90adcf20 7926\f
5f5c8ee5
GM
7927/************************************************************************
7928 Redisplay
7929 ************************************************************************/
7930
7931/* Variables holding some state of redisplay if GLYPH_DEBUG is defined
7932 to a non-zero value. This is sometimes handy to have in a debugger
7933 session. */
7934
7935#if GLYPH_DEBUG
a2889657 7936
5f5c8ee5
GM
7937/* First and last unchanged row for try_window_id. */
7938
7939int debug_first_unchanged_at_end_vpos;
7940int debug_last_unchanged_at_beg_vpos;
7941
7942/* Delta vpos and y. */
7943
7944int debug_dvpos, debug_dy;
7945
7946/* Delta in characters and bytes for try_window_id. */
7947
7948int debug_delta, debug_delta_bytes;
7949
7950/* Values of window_end_pos and window_end_vpos at the end of
7951 try_window_id. */
7952
7953int debug_end_pos, debug_end_vpos;
7954
7955/* Append a string to W->desired_matrix->method. FMT is a printf
7956 format string. A1...A9 are a supplement for a variable-length
7957 argument list. If trace_redisplay_p is non-zero also printf the
7958 resulting string to stderr. */
7959
7960static void
7961debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
7962 struct window *w;
7963 char *fmt;
7964 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
7965{
7966 char buffer[512];
7967 char *method = w->desired_matrix->method;
7968 int len = strlen (method);
7969 int size = sizeof w->desired_matrix->method;
7970 int remaining = size - len - 1;
7971
7972 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
7973 if (len && remaining)
7974 {
7975 method[len] = '|';
7976 --remaining, ++len;
7977 }
7978
7979 strncpy (method + len, buffer, remaining);
7980
7981 if (trace_redisplay_p)
7982 fprintf (stderr, "%p (%s): %s\n",
7983 w,
7984 ((BUFFERP (w->buffer)
7985 && STRINGP (XBUFFER (w->buffer)->name))
7986 ? (char *) XSTRING (XBUFFER (w->buffer)->name)->data
7987 : "no buffer"),
7988 buffer);
7989}
a2889657 7990
5f5c8ee5 7991#endif /* GLYPH_DEBUG */
90adcf20 7992
a2889657 7993
5f5c8ee5
GM
7994/* This counter is used to clear the face cache every once in a while
7995 in redisplay_internal. It is incremented for each redisplay.
7996 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
7997 cleared. */
0d231165 7998
5f5c8ee5 7999#define CLEAR_FACE_CACHE_COUNT 10000
463f6b91
RS
8000static int clear_face_cache_count;
8001
20de20dc 8002/* Record the previous terminal frame we displayed. */
5f5c8ee5
GM
8003
8004static struct frame *previous_terminal_frame;
8005
8006/* Non-zero while redisplay_internal is in progress. */
8007
8008int redisplaying_p;
8009
8010
8011/* Value is non-zero if all changes in window W, which displays
8012 current_buffer, are in the text between START and END. START is a
8013 buffer position, END is given as a distance from Z. Used in
8014 redisplay_internal for display optimization. */
8015
8016static INLINE int
8017text_outside_line_unchanged_p (w, start, end)
8018 struct window *w;
8019 int start, end;
8020{
8021 int unchanged_p = 1;
8022
8023 /* If text or overlays have changed, see where. */
8024 if (XFASTINT (w->last_modified) < MODIFF
8025 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
8026 {
8027 /* Gap in the line? */
8028 if (GPT < start || Z - GPT < end)
8029 unchanged_p = 0;
8030
8031 /* Changes start in front of the line, or end after it? */
8032 if (unchanged_p
9142dd5b
GM
8033 && (BEG_UNCHANGED < start - 1
8034 || END_UNCHANGED < end))
5f5c8ee5
GM
8035 unchanged_p = 0;
8036
8037 /* If selective display, can't optimize if changes start at the
8038 beginning of the line. */
8039 if (unchanged_p
8040 && INTEGERP (current_buffer->selective_display)
8041 && XINT (current_buffer->selective_display) > 0
9142dd5b 8042 && (BEG_UNCHANGED < start || GPT <= start))
5f5c8ee5
GM
8043 unchanged_p = 0;
8044 }
8045
8046 return unchanged_p;
8047}
8048
8049
8050/* Do a frame update, taking possible shortcuts into account. This is
8051 the main external entry point for redisplay.
8052
8053 If the last redisplay displayed an echo area message and that message
8054 is no longer requested, we clear the echo area or bring back the
8055 mini-buffer if that is in use. */
20de20dc 8056
a2889657
JB
8057void
8058redisplay ()
e9874cee
RS
8059{
8060 redisplay_internal (0);
8061}
8062
260a86a0
KH
8063/* Return 1 if point moved out of or into a composition. Otherwise
8064 return 0. PREV_BUF and PREV_PT are the last point buffer and
8065 position. BUF and PT are the current point buffer and position. */
8066
8067int
8068check_point_in_composition (prev_buf, prev_pt, buf, pt)
8069 struct buffer *prev_buf, *buf;
8070 int prev_pt, pt;
8071{
8072 int start, end;
8073 Lisp_Object prop;
8074 Lisp_Object buffer;
8075
8076 XSETBUFFER (buffer, buf);
8077 /* Check a composition at the last point if point moved within the
8078 same buffer. */
8079 if (prev_buf == buf)
8080 {
8081 if (prev_pt == pt)
8082 /* Point didn't move. */
8083 return 0;
8084
8085 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
8086 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
8087 && COMPOSITION_VALID_P (start, end, prop)
8088 && start < prev_pt && end > prev_pt)
8089 /* The last point was within the composition. Return 1 iff
8090 point moved out of the composition. */
8091 return (pt <= start || pt >= end);
8092 }
8093
8094 /* Check a composition at the current point. */
8095 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
8096 && find_composition (pt, -1, &start, &end, &prop, buffer)
8097 && COMPOSITION_VALID_P (start, end, prop)
8098 && start < pt && end > pt);
8099}
5f5c8ee5 8100
9142dd5b
GM
8101/* Reconsider the setting of B->clip_changed which is displayed
8102 in window W. */
8103
8104static INLINE void
8105reconsider_clip_changes (w, b)
8106 struct window *w;
8107 struct buffer *b;
8108{
8109 if (b->prevent_redisplay_optimizations_p)
8110 b->clip_changed = 1;
8111 else if (b->clip_changed
8112 && !NILP (w->window_end_valid)
8113 && w->current_matrix->buffer == b
8114 && w->current_matrix->zv == BUF_ZV (b)
8115 && w->current_matrix->begv == BUF_BEGV (b))
8116 b->clip_changed = 0;
260a86a0
KH
8117
8118 /* If display wasn't paused, and W is not a tool bar window, see if
8119 point has been moved into or out of a composition. In that case,
8120 we set b->clip_changed to 1 to force updating the screen. If
8121 b->clip_changed has already been set to 1, we can skip this
8122 check. */
8123 if (!b->clip_changed
8124 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
8125 {
8126 int pt;
8127
8128 if (w == XWINDOW (selected_window))
8129 pt = BUF_PT (current_buffer);
8130 else
8131 pt = marker_position (w->pointm);
8132
8133 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
6fc556fd 8134 || pt != XINT (w->last_point))
260a86a0 8135 && check_point_in_composition (w->current_matrix->buffer,
6fc556fd 8136 XINT (w->last_point),
260a86a0
KH
8137 XBUFFER (w->buffer), pt))
8138 b->clip_changed = 1;
8139 }
9142dd5b
GM
8140}
8141
8142
5f5c8ee5
GM
8143/* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
8144 response to any user action; therefore, we should preserve the echo
8145 area. (Actually, our caller does that job.) Perhaps in the future
8146 avoid recentering windows if it is not necessary; currently that
8147 causes some problems. */
e9874cee
RS
8148
8149static void
8150redisplay_internal (preserve_echo_area)
8151 int preserve_echo_area;
a2889657 8152{
5f5c8ee5
GM
8153 struct window *w = XWINDOW (selected_window);
8154 struct frame *f = XFRAME (w->frame);
8155 int pause;
a2889657 8156 int must_finish = 0;
5f5c8ee5 8157 struct text_pos tlbufpos, tlendpos;
89819bdd 8158 int number_of_visible_frames;
28514cd9 8159 int count;
886bd6f2 8160 struct frame *sf = SELECTED_FRAME ();
a2889657 8161
5f5c8ee5
GM
8162 /* Non-zero means redisplay has to consider all windows on all
8163 frames. Zero means, only selected_window is considered. */
8164 int consider_all_windows_p;
8165
8166 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
8167
8168 /* No redisplay if running in batch mode or frame is not yet fully
8169 initialized, or redisplay is explicitly turned off by setting
8170 Vinhibit_redisplay. */
8171 if (noninteractive
8172 || !NILP (Vinhibit_redisplay)
8173 || !f->glyphs_initialized_p)
a2889657
JB
8174 return;
8175
5f5c8ee5
GM
8176 /* The flag redisplay_performed_directly_p is set by
8177 direct_output_for_insert when it already did the whole screen
8178 update necessary. */
8179 if (redisplay_performed_directly_p)
8180 {
8181 redisplay_performed_directly_p = 0;
8182 if (!hscroll_windows (selected_window))
8183 return;
8184 }
8185
15f0cf78
RS
8186#ifdef USE_X_TOOLKIT
8187 if (popup_activated ())
8188 return;
8189#endif
8190
28514cd9 8191 /* I don't think this happens but let's be paranoid. */
5f5c8ee5 8192 if (redisplaying_p)
735c094c
KH
8193 return;
8194
28514cd9
GM
8195 /* Record a function that resets redisplaying_p to its old value
8196 when we leave this function. */
2d27dae2 8197 count = BINDING_STACK_SIZE ();
28514cd9
GM
8198 record_unwind_protect (unwind_redisplay, make_number (redisplaying_p));
8199 ++redisplaying_p;
8200
8b32d885 8201 retry:
bd9d0f3f 8202 pause = 0;
9142dd5b
GM
8203 reconsider_clip_changes (w, current_buffer);
8204
5f5c8ee5
GM
8205 /* If new fonts have been loaded that make a glyph matrix adjustment
8206 necessary, do it. */
8207 if (fonts_changed_p)
8208 {
8209 adjust_glyphs (NULL);
8210 ++windows_or_buffers_changed;
8211 fonts_changed_p = 0;
8212 }
8213
6961e0c1
GM
8214 /* If face_change_count is non-zero, init_iterator will free all
8215 realized faces, which includes the faces referenced from current
8216 matrices. So, we can't reuse current matrices in this case. */
8217 if (face_change_count)
8218 ++windows_or_buffers_changed;
8219
886bd6f2
GM
8220 if (! FRAME_WINDOW_P (sf)
8221 && previous_terminal_frame != sf)
20de20dc 8222 {
5f5c8ee5
GM
8223 /* Since frames on an ASCII terminal share the same display
8224 area, displaying a different frame means redisplay the whole
8225 thing. */
20de20dc 8226 windows_or_buffers_changed++;
886bd6f2
GM
8227 SET_FRAME_GARBAGED (sf);
8228 XSETFRAME (Vterminal_frame, sf);
20de20dc 8229 }
886bd6f2 8230 previous_terminal_frame = sf;
20de20dc 8231
5f5c8ee5
GM
8232 /* Set the visible flags for all frames. Do this before checking
8233 for resized or garbaged frames; they want to know if their frames
8234 are visible. See the comment in frame.h for
8235 FRAME_SAMPLE_VISIBILITY. */
d724d989 8236 {
35f56f96 8237 Lisp_Object tail, frame;
d724d989 8238
89819bdd
RS
8239 number_of_visible_frames = 0;
8240
35f56f96 8241 FOR_EACH_FRAME (tail, frame)
f82aff7c 8242 {
5f5c8ee5
GM
8243 struct frame *f = XFRAME (frame);
8244
8245 FRAME_SAMPLE_VISIBILITY (f);
8246 if (FRAME_VISIBLE_P (f))
8247 ++number_of_visible_frames;
8248 clear_desired_matrices (f);
f82aff7c 8249 }
d724d989
JB
8250 }
8251
44fa5b1e 8252 /* Notice any pending interrupt request to change frame size. */
c6e89d6c 8253 do_pending_window_change (1);
a2889657 8254
5f5c8ee5 8255 /* Clear frames marked as garbaged. */
44fa5b1e 8256 if (frame_garbaged)
c6e89d6c 8257 clear_garbaged_frames ();
a2889657 8258
e037b9ec 8259 /* Build menubar and tool-bar items. */
f82aff7c
RS
8260 prepare_menu_bars ();
8261
28995e67 8262 if (windows_or_buffers_changed)
a2889657
JB
8263 update_mode_lines++;
8264
538f13d4
RS
8265 /* Detect case that we need to write or remove a star in the mode line. */
8266 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
a2889657
JB
8267 {
8268 w->update_mode_line = Qt;
8269 if (buffer_shared > 1)
8270 update_mode_lines++;
8271 }
8272
5f5c8ee5 8273 /* If %c is in the mode line, update it if needed. */
28995e67
RS
8274 if (!NILP (w->column_number_displayed)
8275 /* This alternative quickly identifies a common case
8276 where no change is needed. */
8277 && !(PT == XFASTINT (w->last_point)
8850a573
RS
8278 && XFASTINT (w->last_modified) >= MODIFF
8279 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
8280 && XFASTINT (w->column_number_displayed) != current_column ())
8281 w->update_mode_line = Qt;
8282
44fa5b1e 8283 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
a2889657 8284
5f5c8ee5
GM
8285 /* The variable buffer_shared is set in redisplay_window and
8286 indicates that we redisplay a buffer in different windows. See
8287 there. */
8288 consider_all_windows_p = update_mode_lines || buffer_shared > 1;
a2889657
JB
8289
8290 /* If specs for an arrow have changed, do thorough redisplay
8291 to ensure we remove any arrow that should no longer exist. */
d45de95b 8292 if (! EQ (COERCE_MARKER (Voverlay_arrow_position), last_arrow_position)
ded34426 8293 || ! EQ (Voverlay_arrow_string, last_arrow_string))
5f5c8ee5 8294 consider_all_windows_p = windows_or_buffers_changed = 1;
a2889657 8295
90adcf20
RS
8296 /* Normally the message* functions will have already displayed and
8297 updated the echo area, but the frame may have been trashed, or
8298 the update may have been preempted, so display the echo area
c6e89d6c
GM
8299 again here. Checking both message buffers captures the case that
8300 the echo area should be cleared. */
8301 if (!NILP (echo_area_buffer[0]) || !NILP (echo_area_buffer[1]))
90adcf20 8302 {
c6e89d6c 8303 int window_height_changed_p = echo_area_display (0);
90adcf20 8304 must_finish = 1;
dd2eb166 8305
c6e89d6c
GM
8306 if (fonts_changed_p)
8307 goto retry;
8308 else if (window_height_changed_p)
8309 {
8310 consider_all_windows_p = 1;
8311 ++update_mode_lines;
8312 ++windows_or_buffers_changed;
9142dd5b
GM
8313
8314 /* If window configuration was changed, frames may have been
8315 marked garbaged. Clear them or we will experience
8316 surprises wrt scrolling. */
8317 if (frame_garbaged)
8318 clear_garbaged_frames ();
c6e89d6c 8319 }
90adcf20 8320 }
97a36635 8321 else if (EQ (selected_window, minibuf_window)
dd2eb166
GM
8322 && (current_buffer->clip_changed
8323 || XFASTINT (w->last_modified) < MODIFF
8324 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
92a90e89 8325 && resize_mini_window (w, 0))
c6e89d6c
GM
8326 {
8327 /* Resized active mini-window to fit the size of what it is
dd2eb166
GM
8328 showing if its contents might have changed. */
8329 must_finish = 1;
8330 consider_all_windows_p = 1;
c6e89d6c 8331 ++windows_or_buffers_changed;
dd2eb166 8332 ++update_mode_lines;
9142dd5b
GM
8333
8334 /* If window configuration was changed, frames may have been
8335 marked garbaged. Clear them or we will experience
8336 surprises wrt scrolling. */
8337 if (frame_garbaged)
8338 clear_garbaged_frames ();
c6e89d6c
GM
8339 }
8340
90adcf20 8341
5f5c8ee5
GM
8342 /* If showing the region, and mark has changed, we must redisplay
8343 the whole window. The assignment to this_line_start_pos prevents
8344 the optimization directly below this if-statement. */
bd66d1ba
RS
8345 if (((!NILP (Vtransient_mark_mode)
8346 && !NILP (XBUFFER (w->buffer)->mark_active))
8347 != !NILP (w->region_showing))
82d04750
JB
8348 || (!NILP (w->region_showing)
8349 && !EQ (w->region_showing,
8350 Fmarker_position (XBUFFER (w->buffer)->mark))))
5f5c8ee5
GM
8351 CHARPOS (this_line_start_pos) = 0;
8352
8353 /* Optimize the case that only the line containing the cursor in the
8354 selected window has changed. Variables starting with this_ are
8355 set in display_line and record information about the line
8356 containing the cursor. */
8357 tlbufpos = this_line_start_pos;
8358 tlendpos = this_line_end_pos;
8359 if (!consider_all_windows_p
8360 && CHARPOS (tlbufpos) > 0
8361 && NILP (w->update_mode_line)
73af359d 8362 && !current_buffer->clip_changed
44fa5b1e 8363 && FRAME_VISIBLE_P (XFRAME (w->frame))
f21ef775 8364 && !FRAME_OBSCURED_P (XFRAME (w->frame))
5f5c8ee5 8365 /* Make sure recorded data applies to current buffer, etc. */
a2889657
JB
8366 && this_line_buffer == current_buffer
8367 && current_buffer == XBUFFER (w->buffer)
265a9e55 8368 && NILP (w->force_start)
5f5c8ee5
GM
8369 /* Point must be on the line that we have info recorded about. */
8370 && PT >= CHARPOS (tlbufpos)
8371 && PT <= Z - CHARPOS (tlendpos)
a2889657
JB
8372 /* All text outside that line, including its final newline,
8373 must be unchanged */
5f5c8ee5
GM
8374 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
8375 CHARPOS (tlendpos)))
8376 {
8377 if (CHARPOS (tlbufpos) > BEGV
8378 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
8379 && (CHARPOS (tlbufpos) == ZV
8380 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
a2889657
JB
8381 /* Former continuation line has disappeared by becoming empty */
8382 goto cancel;
8383 else if (XFASTINT (w->last_modified) < MODIFF
8850a573 8384 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
a2889657
JB
8385 || MINI_WINDOW_P (w))
8386 {
1c9241f5
KH
8387 /* We have to handle the case of continuation around a
8388 wide-column character (See the comment in indent.c around
8389 line 885).
8390
8391 For instance, in the following case:
8392
8393 -------- Insert --------
8394 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
8395 J_I_ ==> J_I_ `^^' are cursors.
8396 ^^ ^^
8397 -------- --------
8398
8399 As we have to redraw the line above, we should goto cancel. */
8400
5f5c8ee5
GM
8401 struct it it;
8402 int line_height_before = this_line_pixel_height;
8403
8404 /* Note that start_display will handle the case that the
8405 line starting at tlbufpos is a continuation lines. */
8406 start_display (&it, w, tlbufpos);
8407
8408 /* Implementation note: It this still necessary? */
8409 if (it.current_x != this_line_start_x)
1c9241f5
KH
8410 goto cancel;
8411
5f5c8ee5
GM
8412 TRACE ((stderr, "trying display optimization 1\n"));
8413 w->cursor.vpos = -1;
a2889657 8414 overlay_arrow_seen = 0;
5f5c8ee5
GM
8415 it.vpos = this_line_vpos;
8416 it.current_y = this_line_y;
8417 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
8418 display_line (&it);
8419
a2889657 8420 /* If line contains point, is not continued,
5f5c8ee5
GM
8421 and ends at same distance from eob as before, we win */
8422 if (w->cursor.vpos >= 0
8423 /* Line is not continued, otherwise this_line_start_pos
8424 would have been set to 0 in display_line. */
8425 && CHARPOS (this_line_start_pos)
8426 /* Line ends as before. */
8427 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
8428 /* Line has same height as before. Otherwise other lines
8429 would have to be shifted up or down. */
8430 && this_line_pixel_height == line_height_before)
a2889657 8431 {
5f5c8ee5
GM
8432 /* If this is not the window's last line, we must adjust
8433 the charstarts of the lines below. */
8434 if (it.current_y < it.last_visible_y)
8435 {
8436 struct glyph_row *row
8437 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
8438 int delta, delta_bytes;
8439
8440 if (Z - CHARPOS (tlendpos) == ZV)
8441 {
8442 /* This line ends at end of (accessible part of)
8443 buffer. There is no newline to count. */
8444 delta = (Z
8445 - CHARPOS (tlendpos)
8446 - MATRIX_ROW_START_CHARPOS (row));
8447 delta_bytes = (Z_BYTE
8448 - BYTEPOS (tlendpos)
8449 - MATRIX_ROW_START_BYTEPOS (row));
8450 }
8451 else
8452 {
8453 /* This line ends in a newline. Must take
8454 account of the newline and the rest of the
8455 text that follows. */
8456 delta = (Z
8457 - CHARPOS (tlendpos)
8458 - MATRIX_ROW_START_CHARPOS (row));
8459 delta_bytes = (Z_BYTE
8460 - BYTEPOS (tlendpos)
8461 - MATRIX_ROW_START_BYTEPOS (row));
8462 }
8463
f2d86d7a
GM
8464 increment_matrix_positions (w->current_matrix,
8465 this_line_vpos + 1,
8466 w->current_matrix->nrows,
8467 delta, delta_bytes);
85bcef6c 8468 }
46db8486 8469
5f5c8ee5
GM
8470 /* If this row displays text now but previously didn't,
8471 or vice versa, w->window_end_vpos may have to be
8472 adjusted. */
8473 if ((it.glyph_row - 1)->displays_text_p)
8474 {
8475 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
8476 XSETINT (w->window_end_vpos, this_line_vpos);
8477 }
8478 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
8479 && this_line_vpos > 0)
8480 XSETINT (w->window_end_vpos, this_line_vpos - 1);
8481 w->window_end_valid = Qnil;
8482
8483 /* Update hint: No need to try to scroll in update_window. */
8484 w->desired_matrix->no_scrolling_p = 1;
8485
8486#if GLYPH_DEBUG
8487 *w->desired_matrix->method = 0;
8488 debug_method_add (w, "optimization 1");
8489#endif
a2889657
JB
8490 goto update;
8491 }
8492 else
8493 goto cancel;
8494 }
5f5c8ee5
GM
8495 else if (/* Cursor position hasn't changed. */
8496 PT == XFASTINT (w->last_point)
b6f0fe04
RS
8497 /* Make sure the cursor was last displayed
8498 in this window. Otherwise we have to reposition it. */
5f5c8ee5
GM
8499 && 0 <= w->cursor.vpos
8500 && XINT (w->height) > w->cursor.vpos)
a2889657
JB
8501 {
8502 if (!must_finish)
8503 {
c6e89d6c 8504 do_pending_window_change (1);
5f5c8ee5
GM
8505
8506 /* We used to always goto end_of_redisplay here, but this
8507 isn't enough if we have a blinking cursor. */
8508 if (w->cursor_off_p == w->last_cursor_off_p)
8509 goto end_of_redisplay;
a2889657
JB
8510 }
8511 goto update;
8512 }
8b51f1e3
KH
8513 /* If highlighting the region, or if the cursor is in the echo area,
8514 then we can't just move the cursor. */
bd66d1ba
RS
8515 else if (! (!NILP (Vtransient_mark_mode)
8516 && !NILP (current_buffer->mark_active))
97a36635 8517 && (EQ (selected_window, current_buffer->last_selected_window)
293a54ce 8518 || highlight_nonselected_windows)
8b51f1e3 8519 && NILP (w->region_showing)
8f897821 8520 && NILP (Vshow_trailing_whitespace)
8b51f1e3 8521 && !cursor_in_echo_area)
a2889657 8522 {
5f5c8ee5
GM
8523 struct it it;
8524 struct glyph_row *row;
8525
8526 /* Skip from tlbufpos to PT and see where it is. Note that
8527 PT may be in invisible text. If so, we will end at the
8528 next visible position. */
8529 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
8530 NULL, DEFAULT_FACE_ID);
8531 it.current_x = this_line_start_x;
8532 it.current_y = this_line_y;
8533 it.vpos = this_line_vpos;
8534
8535 /* The call to move_it_to stops in front of PT, but
8536 moves over before-strings. */
8537 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
8538
8539 if (it.vpos == this_line_vpos
8540 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
8541 row->enabled_p))
a2889657 8542 {
5f5c8ee5
GM
8543 xassert (this_line_vpos == it.vpos);
8544 xassert (this_line_y == it.current_y);
8545 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
ade0bee1
GM
8546#if GLYPH_DEBUG
8547 *w->desired_matrix->method = 0;
8548 debug_method_add (w, "optimization 3");
8549#endif
a2889657
JB
8550 goto update;
8551 }
8552 else
8553 goto cancel;
8554 }
5f5c8ee5 8555
a2889657 8556 cancel:
5f5c8ee5
GM
8557 /* Text changed drastically or point moved off of line. */
8558 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
a2889657
JB
8559 }
8560
5f5c8ee5
GM
8561 CHARPOS (this_line_start_pos) = 0;
8562 consider_all_windows_p |= buffer_shared > 1;
8563 ++clear_face_cache_count;
a2889657 8564
5f5c8ee5 8565
bd9d0f3f
GM
8566 /* Build desired matrices, and update the display. If
8567 consider_all_windows_p is non-zero, do it for all windows on all
8568 frames. Otherwise do it for selected_window, only. */
463f6b91 8569
5f5c8ee5 8570 if (consider_all_windows_p)
a2889657 8571 {
35f56f96 8572 Lisp_Object tail, frame;
0528abe1
GM
8573 int i, n = 0, size = 50;
8574 struct frame **updated
8575 = (struct frame **) alloca (size * sizeof *updated);
a2889657 8576
5f5c8ee5
GM
8577 /* Clear the face cache eventually. */
8578 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
463f6b91 8579 {
5f5c8ee5 8580 clear_face_cache (0);
463f6b91
RS
8581 clear_face_cache_count = 0;
8582 }
31b24551 8583
5f5c8ee5
GM
8584 /* Recompute # windows showing selected buffer. This will be
8585 incremented each time such a window is displayed. */
a2889657
JB
8586 buffer_shared = 0;
8587
35f56f96 8588 FOR_EACH_FRAME (tail, frame)
30c566e4 8589 {
5f5c8ee5 8590 struct frame *f = XFRAME (frame);
bd9d0f3f 8591
886bd6f2 8592 if (FRAME_WINDOW_P (f) || f == sf)
9769686d 8593 {
5f5c8ee5
GM
8594 /* Mark all the scroll bars to be removed; we'll redeem
8595 the ones we want when we redisplay their windows. */
9769686d 8596 if (condemn_scroll_bars_hook)
504454e8 8597 condemn_scroll_bars_hook (f);
30c566e4 8598
f21ef775 8599 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
5f5c8ee5 8600 redisplay_windows (FRAME_ROOT_WINDOW (f));
30c566e4 8601
5f5c8ee5
GM
8602 /* Any scroll bars which redisplay_windows should have
8603 nuked should now go away. */
9769686d 8604 if (judge_scroll_bars_hook)
504454e8 8605 judge_scroll_bars_hook (f);
bd9d0f3f
GM
8606
8607 /* If fonts changed, display again. */
8608 if (fonts_changed_p)
8609 goto retry;
8610
8611 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
8612 {
8613 /* See if we have to hscroll. */
8614 if (hscroll_windows (f->root_window))
8615 goto retry;
8616
8617 /* Prevent various kinds of signals during display
8618 update. stdio is not robust about handling
8619 signals, which can cause an apparent I/O
8620 error. */
8621 if (interrupt_input)
8622 unrequest_sigio ();
8623 stop_polling ();
8624
8625 /* Update the display. */
8626 set_window_update_flags (XWINDOW (f->root_window), 1);
8627 pause |= update_frame (f, 0, 0);
8628 if (pause)
8629 break;
8630
0528abe1
GM
8631 if (n == size)
8632 {
8633 int nbytes = size * sizeof *updated;
8634 struct frame **p = (struct frame **) alloca (2 * nbytes);
8635 bcopy (updated, p, nbytes);
8636 size *= 2;
8637 }
8638
8639 updated[n++] = f;
bd9d0f3f 8640 }
9769686d 8641 }
30c566e4 8642 }
0528abe1
GM
8643
8644 /* Do the mark_window_display_accurate after all windows have
8645 been redisplayed because this call resets flags in buffers
8646 which are needed for proper redisplay. */
8647 for (i = 0; i < n; ++i)
8648 {
8649 struct frame *f = updated[i];
8650 mark_window_display_accurate (f->root_window, 1);
8651 if (frame_up_to_date_hook)
8652 frame_up_to_date_hook (f);
8653 }
a2889657 8654 }
bd9d0f3f
GM
8655 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
8656 {
8657 Lisp_Object mini_window;
8658 struct frame *mini_frame;
5f5c8ee5 8659
bd9d0f3f 8660 redisplay_window (selected_window, 1);
5f5c8ee5 8661
bd9d0f3f
GM
8662 /* Compare desired and current matrices, perform output. */
8663 update:
5f5c8ee5 8664
bd9d0f3f
GM
8665 /* If fonts changed, display again. */
8666 if (fonts_changed_p)
92a90e89 8667 goto retry;
a2889657 8668
bd9d0f3f
GM
8669 /* Prevent various kinds of signals during display update.
8670 stdio is not robust about handling signals,
8671 which can cause an apparent I/O error. */
8672 if (interrupt_input)
8673 unrequest_sigio ();
8674 stop_polling ();
1af9f229 8675
bd9d0f3f 8676 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
5f5c8ee5 8677 {
92a90e89
GM
8678 if (hscroll_windows (selected_window))
8679 goto retry;
8680
5f5c8ee5 8681 XWINDOW (selected_window)->must_be_updated_p = 1;
886bd6f2 8682 pause = update_frame (sf, 0, 0);
5f5c8ee5 8683 }
d724d989 8684
8de2d90b 8685 /* We may have called echo_area_display at the top of this
44fa5b1e
JB
8686 function. If the echo area is on another frame, that may
8687 have put text on a frame other than the selected one, so the
8688 above call to update_frame would not have caught it. Catch
8de2d90b 8689 it here. */
bd9d0f3f
GM
8690 mini_window = FRAME_MINIBUF_WINDOW (sf);
8691 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8de2d90b 8692
bd9d0f3f
GM
8693 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
8694 {
8695 XWINDOW (mini_window)->must_be_updated_p = 1;
8696 pause |= update_frame (mini_frame, 0, 0);
8697 if (!pause && hscroll_windows (mini_window))
8698 goto retry;
8699 }
6e8290aa 8700 }
a2889657 8701
5f5c8ee5
GM
8702 /* If display was paused because of pending input, make sure we do a
8703 thorough update the next time. */
a2889657
JB
8704 if (pause)
8705 {
5f5c8ee5
GM
8706 /* Prevent the optimization at the beginning of
8707 redisplay_internal that tries a single-line update of the
8708 line containing the cursor in the selected window. */
8709 CHARPOS (this_line_start_pos) = 0;
8710
8711 /* Let the overlay arrow be updated the next time. */
265a9e55 8712 if (!NILP (last_arrow_position))
a2889657
JB
8713 {
8714 last_arrow_position = Qt;
8715 last_arrow_string = Qt;
8716 }
5f5c8ee5
GM
8717
8718 /* If we pause after scrolling, some rows in the current
8719 matrices of some windows are not valid. */
8720 if (!WINDOW_FULL_WIDTH_P (w)
8721 && !FRAME_WINDOW_P (XFRAME (w->frame)))
a2889657
JB
8722 update_mode_lines = 1;
8723 }
43c09969 8724 else
a2889657 8725 {
43c09969 8726 if (!consider_all_windows_p)
a2889657 8727 {
43c09969
GM
8728 /* This has already been done above if
8729 consider_all_windows_p is set. */
8730 mark_window_display_accurate_1 (w, 1);
927c5b3b
GM
8731
8732 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
8733 last_arrow_string = Voverlay_arrow_string;
8734
efc63ef0 8735 if (frame_up_to_date_hook != 0)
43c09969 8736 frame_up_to_date_hook (sf);
a2889657 8737 }
15e26c76 8738
a2889657
JB
8739 update_mode_lines = 0;
8740 windows_or_buffers_changed = 0;
8741 }
8742
5f5c8ee5
GM
8743 /* Start SIGIO interrupts coming again. Having them off during the
8744 code above makes it less likely one will discard output, but not
8745 impossible, since there might be stuff in the system buffer here.
a2889657 8746 But it is much hairier to try to do anything about that. */
a2889657
JB
8747 if (interrupt_input)
8748 request_sigio ();
8749 start_polling ();
8750
5f5c8ee5
GM
8751 /* If a frame has become visible which was not before, redisplay
8752 again, so that we display it. Expose events for such a frame
8753 (which it gets when becoming visible) don't call the parts of
8754 redisplay constructing glyphs, so simply exposing a frame won't
8755 display anything in this case. So, we have to display these
8756 frames here explicitly. */
11c52c4f
RS
8757 if (!pause)
8758 {
8759 Lisp_Object tail, frame;
8760 int new_count = 0;
8761
8762 FOR_EACH_FRAME (tail, frame)
8763 {
8764 int this_is_visible = 0;
8e83f802
RS
8765
8766 if (XFRAME (frame)->visible)
8767 this_is_visible = 1;
8768 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
8769 if (XFRAME (frame)->visible)
8770 this_is_visible = 1;
11c52c4f
RS
8771
8772 if (this_is_visible)
8773 new_count++;
8774 }
8775
89819bdd 8776 if (new_count != number_of_visible_frames)
11c52c4f
RS
8777 windows_or_buffers_changed++;
8778 }
8779
44fa5b1e 8780 /* Change frame size now if a change is pending. */
c6e89d6c 8781 do_pending_window_change (1);
d8e242fd 8782
8b32d885
RS
8783 /* If we just did a pending size change, or have additional
8784 visible frames, redisplay again. */
3c8c72e0 8785 if (windows_or_buffers_changed && !pause)
8b32d885 8786 goto retry;
5f5c8ee5
GM
8787
8788 end_of_redisplay:;
c6e89d6c 8789
28514cd9 8790 unbind_to (count, Qnil);
a2889657
JB
8791}
8792
5f5c8ee5
GM
8793
8794/* Redisplay, but leave alone any recent echo area message unless
8795 another message has been requested in its place.
a2889657
JB
8796
8797 This is useful in situations where you need to redisplay but no
8798 user action has occurred, making it inappropriate for the message
8799 area to be cleared. See tracking_off and
9bf76936
GM
8800 wait_reading_process_input for examples of these situations.
8801
8802 FROM_WHERE is an integer saying from where this function was
8803 called. This is useful for debugging. */
a2889657 8804
8991bb31 8805void
9bf76936
GM
8806redisplay_preserve_echo_area (from_where)
8807 int from_where;
a2889657 8808{
9bf76936
GM
8809 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
8810
c6e89d6c 8811 if (!NILP (echo_area_buffer[1]))
a2889657 8812 {
c6e89d6c
GM
8813 /* We have a previously displayed message, but no current
8814 message. Redisplay the previous message. */
8815 display_last_displayed_message_p = 1;
e9874cee 8816 redisplay_internal (1);
c6e89d6c 8817 display_last_displayed_message_p = 0;
a2889657
JB
8818 }
8819 else
e9874cee 8820 redisplay_internal (1);
a2889657
JB
8821}
8822
5f5c8ee5 8823
28514cd9
GM
8824/* Function registered with record_unwind_protect in
8825 redisplay_internal. Clears the flag indicating that a redisplay is
8826 in progress. */
8827
8828static Lisp_Object
8829unwind_redisplay (old_redisplaying_p)
8830 Lisp_Object old_redisplaying_p;
8831{
8832 redisplaying_p = XFASTINT (old_redisplaying_p);
c6e89d6c 8833 return Qnil;
28514cd9
GM
8834}
8835
8836
43c09969
GM
8837/* Mark the display of window W as accurate or inaccurate. If
8838 ACCURATE_P is non-zero mark display of W as accurate. If
8839 ACCURATE_P is zero, arrange for W to be redisplayed the next time
8840 redisplay_internal is called. */
5f5c8ee5 8841
43c09969
GM
8842static void
8843mark_window_display_accurate_1 (w, accurate_p)
8844 struct window *w;
5f5c8ee5 8845 int accurate_p;
a2889657 8846{
43c09969 8847 if (BUFFERP (w->buffer))
a2889657 8848 {
43c09969
GM
8849 struct buffer *b = XBUFFER (w->buffer);
8850
8851 w->last_modified
8852 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
8853 w->last_overlay_modified
8854 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
8855 w->last_had_star
8856 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
a2889657 8857
43c09969 8858 if (accurate_p)
bd66d1ba 8859 {
43c09969
GM
8860 b->clip_changed = 0;
8861 b->prevent_redisplay_optimizations_p = 0;
8862
8863 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
8864 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
8865 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
8866 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
5f5c8ee5 8867
43c09969
GM
8868 w->current_matrix->buffer = b;
8869 w->current_matrix->begv = BUF_BEGV (b);
8870 w->current_matrix->zv = BUF_ZV (b);
5f5c8ee5 8871
43c09969
GM
8872 w->last_cursor = w->cursor;
8873 w->last_cursor_off_p = w->cursor_off_p;
8874
8875 if (w == XWINDOW (selected_window))
8876 w->last_point = make_number (BUF_PT (b));
8877 else
8878 w->last_point = make_number (XMARKER (w->pointm)->charpos);
bd66d1ba 8879 }
43c09969 8880 }
bd66d1ba 8881
43c09969
GM
8882 if (accurate_p)
8883 {
d2f84654 8884 w->window_end_valid = w->buffer;
a2889657 8885 w->update_mode_line = Qnil;
43c09969
GM
8886 }
8887}
8888
8889
8890/* Mark the display of windows in the window tree rooted at WINDOW as
8891 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
8892 windows as accurate. If ACCURATE_P is zero, arrange for windows to
8893 be redisplayed the next time redisplay_internal is called. */
8894
8895void
8896mark_window_display_accurate (window, accurate_p)
8897 Lisp_Object window;
8898 int accurate_p;
8899{
8900 struct window *w;
8901
8902 for (; !NILP (window); window = w->next)
8903 {
8904 w = XWINDOW (window);
8905 mark_window_display_accurate_1 (w, accurate_p);
a2889657 8906
265a9e55 8907 if (!NILP (w->vchild))
5f5c8ee5 8908 mark_window_display_accurate (w->vchild, accurate_p);
265a9e55 8909 if (!NILP (w->hchild))
5f5c8ee5 8910 mark_window_display_accurate (w->hchild, accurate_p);
a2889657
JB
8911 }
8912
5f5c8ee5 8913 if (accurate_p)
a2889657 8914 {
d45de95b 8915 last_arrow_position = COERCE_MARKER (Voverlay_arrow_position);
a2889657
JB
8916 last_arrow_string = Voverlay_arrow_string;
8917 }
8918 else
8919 {
5f5c8ee5
GM
8920 /* Force a thorough redisplay the next time by setting
8921 last_arrow_position and last_arrow_string to t, which is
8922 unequal to any useful value of Voverlay_arrow_... */
a2889657
JB
8923 last_arrow_position = Qt;
8924 last_arrow_string = Qt;
8925 }
8926}
5f5c8ee5
GM
8927
8928
8929/* Return value in display table DP (Lisp_Char_Table *) for character
8930 C. Since a display table doesn't have any parent, we don't have to
8931 follow parent. Do not call this function directly but use the
8932 macro DISP_CHAR_VECTOR. */
8933
8934Lisp_Object
8935disp_char_vector (dp, c)
8936 struct Lisp_Char_Table *dp;
8937 int c;
8938{
8939 int code[4], i;
8940 Lisp_Object val;
8941
8942 if (SINGLE_BYTE_CHAR_P (c))
8943 return (dp->contents[c]);
8944
c5924f47 8945 SPLIT_CHAR (c, code[0], code[1], code[2]);
260a86a0
KH
8946 if (code[1] < 32)
8947 code[1] = -1;
8948 else if (code[2] < 32)
8949 code[2] = -1;
5f5c8ee5
GM
8950
8951 /* Here, the possible range of code[0] (== charset ID) is
8952 128..max_charset. Since the top level char table contains data
8953 for multibyte characters after 256th element, we must increment
8954 code[0] by 128 to get a correct index. */
8955 code[0] += 128;
8956 code[3] = -1; /* anchor */
8957
8958 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
8959 {
8960 val = dp->contents[code[i]];
8961 if (!SUB_CHAR_TABLE_P (val))
8962 return (NILP (val) ? dp->defalt : val);
8963 }
8964
8965 /* Here, val is a sub char table. We return the default value of
8966 it. */
8967 return (dp->defalt);
8968}
8969
8970
a2889657 8971\f
5f5c8ee5
GM
8972/***********************************************************************
8973 Window Redisplay
8974 ***********************************************************************/
a2725ab2 8975
5f5c8ee5 8976/* Redisplay all leaf windows in the window tree rooted at WINDOW. */
90adcf20
RS
8977
8978static void
5f5c8ee5
GM
8979redisplay_windows (window)
8980 Lisp_Object window;
90adcf20 8981{
5f5c8ee5
GM
8982 while (!NILP (window))
8983 {
8984 struct window *w = XWINDOW (window);
8985
8986 if (!NILP (w->hchild))
8987 redisplay_windows (w->hchild);
8988 else if (!NILP (w->vchild))
8989 redisplay_windows (w->vchild);
8990 else
8991 redisplay_window (window, 0);
a2725ab2 8992
5f5c8ee5
GM
8993 window = w->next;
8994 }
8995}
8996
8997
8998/* Set cursor position of W. PT is assumed to be displayed in ROW.
8999 DELTA is the number of bytes by which positions recorded in ROW
9000 differ from current buffer positions. */
9001
9002void
9003set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
9004 struct window *w;
9005 struct glyph_row *row;
9006 struct glyph_matrix *matrix;
9007 int delta, delta_bytes, dy, dvpos;
9008{
9009 struct glyph *glyph = row->glyphs[TEXT_AREA];
9010 struct glyph *end = glyph + row->used[TEXT_AREA];
9011 int x = row->x;
9012 int pt_old = PT - delta;
9013
9014 /* Skip over glyphs not having an object at the start of the row.
9015 These are special glyphs like truncation marks on terminal
9016 frames. */
9017 if (row->displays_text_p)
9018 while (glyph < end
6fc556fd 9019 && INTEGERP (glyph->object)
5f5c8ee5
GM
9020 && glyph->charpos < 0)
9021 {
9022 x += glyph->pixel_width;
9023 ++glyph;
9024 }
9025
9026 while (glyph < end
6fc556fd 9027 && !INTEGERP (glyph->object)
5f5c8ee5
GM
9028 && (!BUFFERP (glyph->object)
9029 || glyph->charpos < pt_old))
9030 {
9031 x += glyph->pixel_width;
9032 ++glyph;
9033 }
9034
9035 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
9036 w->cursor.x = x;
9037 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
9038 w->cursor.y = row->y + dy;
9039
9040 if (w == XWINDOW (selected_window))
9041 {
9042 if (!row->continued_p
9043 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
9044 && row->x == 0)
9045 {
9046 this_line_buffer = XBUFFER (w->buffer);
9047
9048 CHARPOS (this_line_start_pos)
9049 = MATRIX_ROW_START_CHARPOS (row) + delta;
9050 BYTEPOS (this_line_start_pos)
9051 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
9052
9053 CHARPOS (this_line_end_pos)
9054 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
9055 BYTEPOS (this_line_end_pos)
9056 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
9057
9058 this_line_y = w->cursor.y;
9059 this_line_pixel_height = row->height;
9060 this_line_vpos = w->cursor.vpos;
9061 this_line_start_x = row->x;
9062 }
9063 else
9064 CHARPOS (this_line_start_pos) = 0;
9065 }
9066}
9067
9068
9069/* Run window scroll functions, if any, for WINDOW with new window
756a3cb6
RS
9070 start STARTP. Sets the window start of WINDOW to that position.
9071
9072 We assume that the window's buffer is really current. */
5f5c8ee5
GM
9073
9074static INLINE struct text_pos
9075run_window_scroll_functions (window, startp)
9076 Lisp_Object window;
9077 struct text_pos startp;
9078{
9079 struct window *w = XWINDOW (window);
9080 SET_MARKER_FROM_TEXT_POS (w->start, startp);
756a3cb6
RS
9081
9082 if (current_buffer != XBUFFER (w->buffer))
9083 abort ();
9084
5f5c8ee5
GM
9085 if (!NILP (Vwindow_scroll_functions))
9086 {
9087 run_hook_with_args_2 (Qwindow_scroll_functions, window,
9088 make_number (CHARPOS (startp)));
9089 SET_TEXT_POS_FROM_MARKER (startp, w->start);
756a3cb6
RS
9090 /* In case the hook functions switch buffers. */
9091 if (current_buffer != XBUFFER (w->buffer))
9092 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5 9093 }
90adcf20 9094
5f5c8ee5
GM
9095 return startp;
9096}
9097
9098
9099/* Modify the desired matrix of window W and W->vscroll so that the
9100 line containing the cursor is fully visible. */
9101
9102static void
9103make_cursor_line_fully_visible (w)
9104 struct window *w;
9105{
9106 struct glyph_matrix *matrix;
9107 struct glyph_row *row;
478d746b 9108 int window_height;
5f5c8ee5
GM
9109
9110 /* It's not always possible to find the cursor, e.g, when a window
9111 is full of overlay strings. Don't do anything in that case. */
9112 if (w->cursor.vpos < 0)
9113 return;
9114
9115 matrix = w->desired_matrix;
9116 row = MATRIX_ROW (matrix, w->cursor.vpos);
9117
b28cb6ed
GM
9118 /* If the cursor row is not partially visible, there's nothing
9119 to do. */
9120 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
9121 return;
9122
9123 /* If the row the cursor is in is taller than the window's height,
9124 it's not clear what to do, so do nothing. */
9125 window_height = window_box_height (w);
9126 if (row->height >= window_height)
9127 return;
9128
9129 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
5f5c8ee5
GM
9130 {
9131 int dy = row->height - row->visible_height;
9132 w->vscroll = 0;
9133 w->cursor.y += dy;
9134 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9135 }
b28cb6ed 9136 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
5f5c8ee5
GM
9137 {
9138 int dy = - (row->height - row->visible_height);
9139 w->vscroll = dy;
9140 w->cursor.y += dy;
9141 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
9142 }
b28cb6ed 9143
5f5c8ee5
GM
9144 /* When we change the cursor y-position of the selected window,
9145 change this_line_y as well so that the display optimization for
9146 the cursor line of the selected window in redisplay_internal uses
9147 the correct y-position. */
9148 if (w == XWINDOW (selected_window))
9149 this_line_y = w->cursor.y;
9150}
9151
9152
9153/* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
9154 non-zero means only WINDOW is redisplayed in redisplay_internal.
9155 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
9156 in redisplay_window to bring a partially visible line into view in
9157 the case that only the cursor has moved.
9158
9159 Value is
9160
9161 1 if scrolling succeeded
9162
9163 0 if scrolling didn't find point.
9164
9165 -1 if new fonts have been loaded so that we must interrupt
9166 redisplay, adjust glyph matrices, and try again. */
9167
9168static int
9169try_scrolling (window, just_this_one_p, scroll_conservatively,
9170 scroll_step, temp_scroll_step)
9171 Lisp_Object window;
9172 int just_this_one_p;
9173 int scroll_conservatively, scroll_step;
9174 int temp_scroll_step;
9175{
9176 struct window *w = XWINDOW (window);
9177 struct frame *f = XFRAME (w->frame);
9178 struct text_pos scroll_margin_pos;
9179 struct text_pos pos;
9180 struct text_pos startp;
9181 struct it it;
9182 Lisp_Object window_end;
9183 int this_scroll_margin;
9184 int dy = 0;
9185 int scroll_max;
b8a63ccb 9186 int rc;
5f5c8ee5
GM
9187 int amount_to_scroll = 0;
9188 Lisp_Object aggressive;
9189 int height;
9190
9191#if GLYPH_DEBUG
9192 debug_method_add (w, "try_scrolling");
78614721 9193#endif
5f5c8ee5
GM
9194
9195 SET_TEXT_POS_FROM_MARKER (startp, w->start);
9196
9197 /* Compute scroll margin height in pixels. We scroll when point is
9198 within this distance from the top or bottom of the window. */
9199 if (scroll_margin > 0)
90adcf20 9200 {
5f5c8ee5
GM
9201 this_scroll_margin = min (scroll_margin, XINT (w->height) / 4);
9202 this_scroll_margin *= CANON_Y_UNIT (f);
9203 }
9204 else
9205 this_scroll_margin = 0;
9206
9207 /* Compute how much we should try to scroll maximally to bring point
9208 into view. */
0894e696
SM
9209 if (scroll_step || scroll_conservatively || temp_scroll_step)
9210 scroll_max = max (scroll_step,
9211 max (scroll_conservatively, temp_scroll_step));
5f5c8ee5
GM
9212 else if (NUMBERP (current_buffer->scroll_down_aggressively)
9213 || NUMBERP (current_buffer->scroll_up_aggressively))
9214 /* We're trying to scroll because of aggressive scrolling
9215 but no scroll_step is set. Choose an arbitrary one. Maybe
9216 there should be a variable for this. */
9217 scroll_max = 10;
9218 else
9219 scroll_max = 0;
9220 scroll_max *= CANON_Y_UNIT (f);
9221
9222 /* Decide whether we have to scroll down. Start at the window end
9223 and move this_scroll_margin up to find the position of the scroll
9224 margin. */
9225 window_end = Fwindow_end (window, Qt);
9226 CHARPOS (scroll_margin_pos) = XINT (window_end);
9227 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
9228 if (this_scroll_margin)
9229 {
9230 start_display (&it, w, scroll_margin_pos);
9231 move_it_vertically (&it, - this_scroll_margin);
9232 scroll_margin_pos = it.current.pos;
9233 }
9234
9235 if (PT >= CHARPOS (scroll_margin_pos))
9236 {
9237 int y0;
9238
9239 /* Point is in the scroll margin at the bottom of the window, or
9240 below. Compute a new window start that makes point visible. */
47589c8c 9241
5f5c8ee5
GM
9242 /* Compute the distance from the scroll margin to PT.
9243 Give up if the distance is greater than scroll_max. */
9244 start_display (&it, w, scroll_margin_pos);
9245 y0 = it.current_y;
9246 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9247 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
e89aaabd 9248
82e274d1
GM
9249 /* With a scroll_margin of 0, scroll_margin_pos is at the window
9250 end, which is one line below the window. The iterator's
9251 current_y will be same as y0 in that case, but we have to
9252 scroll a line to make PT visible. That's the reason why 1 is
9253 added below. */
9254 dy = 1 + it.current_y - y0;
47589c8c 9255
5f5c8ee5
GM
9256 if (dy > scroll_max)
9257 return 0;
9258
9259 /* Move the window start down. If scrolling conservatively,
9260 move it just enough down to make point visible. If
9261 scroll_step is set, move it down by scroll_step. */
9262 start_display (&it, w, startp);
9263
9264 if (scroll_conservatively)
e89aaabd
GM
9265 amount_to_scroll
9266 = max (max (dy, CANON_Y_UNIT (f)),
9267 CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9268 else if (scroll_step || temp_scroll_step)
9269 amount_to_scroll = scroll_max;
9270 else
90adcf20 9271 {
5f5c8ee5
GM
9272 aggressive = current_buffer->scroll_down_aggressively;
9273 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9274 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9275 if (NUMBERP (aggressive))
9276 amount_to_scroll = XFLOATINT (aggressive) * height;
9277 }
a2725ab2 9278
5f5c8ee5
GM
9279 if (amount_to_scroll <= 0)
9280 return 0;
a2725ab2 9281
5f5c8ee5
GM
9282 move_it_vertically (&it, amount_to_scroll);
9283 startp = it.current.pos;
9284 }
9285 else
9286 {
9287 /* See if point is inside the scroll margin at the top of the
9288 window. */
9289 scroll_margin_pos = startp;
9290 if (this_scroll_margin)
9291 {
9292 start_display (&it, w, startp);
9293 move_it_vertically (&it, this_scroll_margin);
9294 scroll_margin_pos = it.current.pos;
9295 }
9296
9297 if (PT < CHARPOS (scroll_margin_pos))
9298 {
9299 /* Point is in the scroll margin at the top of the window or
9300 above what is displayed in the window. */
9301 int y0;
9302
9303 /* Compute the vertical distance from PT to the scroll
9304 margin position. Give up if distance is greater than
9305 scroll_max. */
9306 SET_TEXT_POS (pos, PT, PT_BYTE);
9307 start_display (&it, w, pos);
9308 y0 = it.current_y;
9309 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
9310 it.last_visible_y, -1,
9311 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9312 dy = it.current_y - y0;
9313 if (dy > scroll_max)
9314 return 0;
9315
9316 /* Compute new window start. */
9317 start_display (&it, w, startp);
9318
9319 if (scroll_conservatively)
0894e696
SM
9320 amount_to_scroll =
9321 max (dy, CANON_Y_UNIT (f) * max (scroll_step, temp_scroll_step));
5f5c8ee5
GM
9322 else if (scroll_step || temp_scroll_step)
9323 amount_to_scroll = scroll_max;
538f13d4 9324 else
5f5c8ee5
GM
9325 {
9326 aggressive = current_buffer->scroll_up_aggressively;
9327 height = (WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (w)
045dee35 9328 - WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
9329 if (NUMBERP (aggressive))
9330 amount_to_scroll = XFLOATINT (aggressive) * height;
9331 }
a2725ab2 9332
5f5c8ee5
GM
9333 if (amount_to_scroll <= 0)
9334 return 0;
9335
9336 move_it_vertically (&it, - amount_to_scroll);
9337 startp = it.current.pos;
90adcf20
RS
9338 }
9339 }
a2889657 9340
5f5c8ee5
GM
9341 /* Run window scroll functions. */
9342 startp = run_window_scroll_functions (window, startp);
90adcf20 9343
5f5c8ee5
GM
9344 /* Display the window. Give up if new fonts are loaded, or if point
9345 doesn't appear. */
9346 if (!try_window (window, startp))
9347 rc = -1;
9348 else if (w->cursor.vpos < 0)
9349 {
9350 clear_glyph_matrix (w->desired_matrix);
9351 rc = 0;
9352 }
9353 else
9354 {
9355 /* Maybe forget recorded base line for line number display. */
9356 if (!just_this_one_p
9357 || current_buffer->clip_changed
9142dd5b 9358 || BEG_UNCHANGED < CHARPOS (startp))
5f5c8ee5
GM
9359 w->base_line_number = Qnil;
9360
9361 /* If cursor ends up on a partially visible line, shift display
9362 lines up or down. */
9363 make_cursor_line_fully_visible (w);
9364 rc = 1;
9365 }
9366
9367 return rc;
a2889657
JB
9368}
9369
5f5c8ee5
GM
9370
9371/* Compute a suitable window start for window W if display of W starts
9372 on a continuation line. Value is non-zero if a new window start
9373 was computed.
9374
9375 The new window start will be computed, based on W's width, starting
9376 from the start of the continued line. It is the start of the
9377 screen line with the minimum distance from the old start W->start. */
9378
9379static int
9380compute_window_start_on_continuation_line (w)
9381 struct window *w;
1f1ff51d 9382{
5f5c8ee5
GM
9383 struct text_pos pos, start_pos;
9384 int window_start_changed_p = 0;
1f1ff51d 9385
5f5c8ee5 9386 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
1f1ff51d 9387
5f5c8ee5
GM
9388 /* If window start is on a continuation line... Window start may be
9389 < BEGV in case there's invisible text at the start of the
9390 buffer (M-x rmail, for example). */
9391 if (CHARPOS (start_pos) > BEGV
9392 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
1f1ff51d 9393 {
5f5c8ee5
GM
9394 struct it it;
9395 struct glyph_row *row;
f3751a65
GM
9396
9397 /* Handle the case that the window start is out of range. */
9398 if (CHARPOS (start_pos) < BEGV)
9399 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
9400 else if (CHARPOS (start_pos) > ZV)
9401 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
5f5c8ee5
GM
9402
9403 /* Find the start of the continued line. This should be fast
9404 because scan_buffer is fast (newline cache). */
045dee35 9405 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
5f5c8ee5
GM
9406 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
9407 row, DEFAULT_FACE_ID);
9408 reseat_at_previous_visible_line_start (&it);
9409
9410 /* If the line start is "too far" away from the window start,
9411 say it takes too much time to compute a new window start. */
9412 if (CHARPOS (start_pos) - IT_CHARPOS (it)
9413 < XFASTINT (w->height) * XFASTINT (w->width))
9414 {
9415 int min_distance, distance;
9416
9417 /* Move forward by display lines to find the new window
9418 start. If window width was enlarged, the new start can
9419 be expected to be > the old start. If window width was
9420 decreased, the new window start will be < the old start.
9421 So, we're looking for the display line start with the
9422 minimum distance from the old window start. */
9423 pos = it.current.pos;
9424 min_distance = INFINITY;
9425 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
9426 distance < min_distance)
9427 {
9428 min_distance = distance;
9429 pos = it.current.pos;
9430 move_it_by_lines (&it, 1, 0);
9431 }
9432
9433 /* Set the window start there. */
9434 SET_MARKER_FROM_TEXT_POS (w->start, pos);
9435 window_start_changed_p = 1;
9436 }
1f1ff51d 9437 }
5f5c8ee5
GM
9438
9439 return window_start_changed_p;
1f1ff51d
KH
9440}
9441
5f5c8ee5 9442
47589c8c
GM
9443/* Try cursor movement in case text has not changes in window WINDOW,
9444 with window start STARTP. Value is
9445
9446 1 if successful
9447
9448 0 if this method cannot be used
9449
9450 -1 if we know we have to scroll the display. *SCROLL_STEP is
9451 set to 1, under certain circumstances, if we want to scroll as
9452 if scroll-step were set to 1. See the code. */
9453
9454static int
9455try_cursor_movement (window, startp, scroll_step)
9456 Lisp_Object window;
9457 struct text_pos startp;
9458 int *scroll_step;
9459{
9460 struct window *w = XWINDOW (window);
9461 struct frame *f = XFRAME (w->frame);
9462 int rc = 0;
9463
9464 /* Handle case where text has not changed, only point, and it has
9465 not moved off the frame. */
9466 if (/* Point may be in this window. */
9467 PT >= CHARPOS (startp)
47589c8c
GM
9468 /* Selective display hasn't changed. */
9469 && !current_buffer->clip_changed
4db87380
GM
9470 /* Function force-mode-line-update is used to force a thorough
9471 redisplay. It sets either windows_or_buffers_changed or
9472 update_mode_lines. So don't take a shortcut here for these
9473 cases. */
9474 && !update_mode_lines
9475 && !windows_or_buffers_changed
47589c8c
GM
9476 /* Can't use this case if highlighting a region. When a
9477 region exists, cursor movement has to do more than just
9478 set the cursor. */
9479 && !(!NILP (Vtransient_mark_mode)
9480 && !NILP (current_buffer->mark_active))
9481 && NILP (w->region_showing)
9482 && NILP (Vshow_trailing_whitespace)
9483 /* Right after splitting windows, last_point may be nil. */
9484 && INTEGERP (w->last_point)
9485 /* This code is not used for mini-buffer for the sake of the case
9486 of redisplaying to replace an echo area message; since in
9487 that case the mini-buffer contents per se are usually
9488 unchanged. This code is of no real use in the mini-buffer
9489 since the handling of this_line_start_pos, etc., in redisplay
9490 handles the same cases. */
9491 && !EQ (window, minibuf_window)
9492 /* When splitting windows or for new windows, it happens that
9493 redisplay is called with a nil window_end_vpos or one being
9494 larger than the window. This should really be fixed in
9495 window.c. I don't have this on my list, now, so we do
9496 approximately the same as the old redisplay code. --gerd. */
9497 && INTEGERP (w->window_end_vpos)
9498 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
9499 && (FRAME_WINDOW_P (f)
9500 || !MARKERP (Voverlay_arrow_position)
9501 || current_buffer != XMARKER (Voverlay_arrow_position)->buffer))
9502 {
9503 int this_scroll_margin;
9504 struct glyph_row *row;
9505
9506#if GLYPH_DEBUG
9507 debug_method_add (w, "cursor movement");
9508#endif
9509
9510 /* Scroll if point within this distance from the top or bottom
9511 of the window. This is a pixel value. */
9512 this_scroll_margin = max (0, scroll_margin);
9513 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
9514 this_scroll_margin *= CANON_Y_UNIT (f);
9515
9516 /* Start with the row the cursor was displayed during the last
9517 not paused redisplay. Give up if that row is not valid. */
bd9d0f3f
GM
9518 if (w->last_cursor.vpos < 0
9519 || w->last_cursor.vpos >= w->current_matrix->nrows)
47589c8c
GM
9520 rc = -1;
9521 else
9522 {
9523 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
9524 if (row->mode_line_p)
9525 ++row;
9526 if (!row->enabled_p)
9527 rc = -1;
9528 }
9529
9530 if (rc == 0)
9531 {
9532 int scroll_p = 0;
68c5d1db
GM
9533 int last_y = window_text_bottom_y (w) - this_scroll_margin;
9534
47589c8c
GM
9535 if (PT > XFASTINT (w->last_point))
9536 {
9537 /* Point has moved forward. */
47589c8c
GM
9538 while (MATRIX_ROW_END_CHARPOS (row) < PT
9539 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
9540 {
9541 xassert (row->enabled_p);
9542 ++row;
9543 }
9544
9545 /* The end position of a row equals the start position
9546 of the next row. If PT is there, we would rather
cafafe0b
GM
9547 display it in the next line. */
9548 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9549 && MATRIX_ROW_END_CHARPOS (row) == PT
9550 && !cursor_row_p (w, row))
9551 ++row;
47589c8c
GM
9552
9553 /* If within the scroll margin, scroll. Note that
9554 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
9555 the next line would be drawn, and that
9556 this_scroll_margin can be zero. */
9557 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
9558 || PT > MATRIX_ROW_END_CHARPOS (row)
9559 /* Line is completely visible last line in window
9560 and PT is to be set in the next line. */
9561 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
9562 && PT == MATRIX_ROW_END_CHARPOS (row)
9563 && !row->ends_at_zv_p
9564 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
9565 scroll_p = 1;
9566 }
9567 else if (PT < XFASTINT (w->last_point))
9568 {
9569 /* Cursor has to be moved backward. Note that PT >=
9570 CHARPOS (startp) because of the outer
9571 if-statement. */
9572 while (!row->mode_line_p
9573 && (MATRIX_ROW_START_CHARPOS (row) > PT
9574 || (MATRIX_ROW_START_CHARPOS (row) == PT
9575 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
9576 && (row->y > this_scroll_margin
9577 || CHARPOS (startp) == BEGV))
9578 {
9579 xassert (row->enabled_p);
9580 --row;
9581 }
9582
9583 /* Consider the following case: Window starts at BEGV,
9584 there is invisible, intangible text at BEGV, so that
9585 display starts at some point START > BEGV. It can
9586 happen that we are called with PT somewhere between
9587 BEGV and START. Try to handle that case. */
9588 if (row < w->current_matrix->rows
9589 || row->mode_line_p)
9590 {
9591 row = w->current_matrix->rows;
9592 if (row->mode_line_p)
9593 ++row;
9594 }
9595
9596 /* Due to newlines in overlay strings, we may have to
9597 skip forward over overlay strings. */
68c5d1db
GM
9598 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
9599 && MATRIX_ROW_END_CHARPOS (row) == PT
9600 && !cursor_row_p (w, row))
47589c8c
GM
9601 ++row;
9602
9603 /* If within the scroll margin, scroll. */
9604 if (row->y < this_scroll_margin
9605 && CHARPOS (startp) != BEGV)
9606 scroll_p = 1;
9607 }
9608
9609 if (PT < MATRIX_ROW_START_CHARPOS (row)
9610 || PT > MATRIX_ROW_END_CHARPOS (row))
9611 {
9612 /* if PT is not in the glyph row, give up. */
9613 rc = -1;
9614 }
440fc135 9615 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (row))
47589c8c 9616 {
8de4aaf8
GM
9617 if (PT == MATRIX_ROW_END_CHARPOS (row)
9618 && !row->ends_at_zv_p
9619 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
3f7e3031
GM
9620 rc = -1;
9621 else if (row->height > window_box_height (w))
440fc135 9622 {
8de4aaf8
GM
9623 /* If we end up in a partially visible line, let's
9624 make it fully visible, except when it's taller
9625 than the window, in which case we can't do much
9626 about it. */
440fc135
GM
9627 *scroll_step = 1;
9628 rc = -1;
9629 }
9630 else
9631 {
9632 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9633 try_window (window, startp);
9634 make_cursor_line_fully_visible (w);
9635 rc = 1;
9636 }
47589c8c
GM
9637 }
9638 else if (scroll_p)
9639 rc = -1;
9640 else
9641 {
9642 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
9643 rc = 1;
9644 }
9645 }
9646 }
9647
9648 return rc;
9649}
9650
9651
5f5c8ee5
GM
9652/* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
9653 selected_window is redisplayed. */
90adcf20 9654
a2889657 9655static void
5f5c8ee5 9656redisplay_window (window, just_this_one_p)
a2889657 9657 Lisp_Object window;
5f5c8ee5 9658 int just_this_one_p;
a2889657 9659{
5f5c8ee5
GM
9660 struct window *w = XWINDOW (window);
9661 struct frame *f = XFRAME (w->frame);
9662 struct buffer *buffer = XBUFFER (w->buffer);
a2889657 9663 struct buffer *old = current_buffer;
5f5c8ee5 9664 struct text_pos lpoint, opoint, startp;
e481f960 9665 int update_mode_line;
5f5c8ee5
GM
9666 int tem;
9667 struct it it;
9668 /* Record it now because it's overwritten. */
9669 int current_matrix_up_to_date_p = 0;
5f5c8ee5 9670 int temp_scroll_step = 0;
2d27dae2 9671 int count = BINDING_STACK_SIZE ();
47589c8c 9672 int rc;
a2889657 9673
5f5c8ee5
GM
9674 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9675 opoint = lpoint;
a2889657 9676
5f5c8ee5
GM
9677 /* W must be a leaf window here. */
9678 xassert (!NILP (w->buffer));
9679#if GLYPH_DEBUG
9680 *w->desired_matrix->method = 0;
9681#endif
2e54982e
RS
9682
9683 specbind (Qinhibit_point_motion_hooks, Qt);
9142dd5b
GM
9684
9685 reconsider_clip_changes (w, buffer);
9686
5f5c8ee5
GM
9687 /* Has the mode line to be updated? */
9688 update_mode_line = (!NILP (w->update_mode_line)
9689 || update_mode_lines
9690 || buffer->clip_changed);
8de2d90b
JB
9691
9692 if (MINI_WINDOW_P (w))
9693 {
5f5c8ee5 9694 if (w == XWINDOW (echo_area_window)
c6e89d6c 9695 && !NILP (echo_area_buffer[0]))
5f5c8ee5
GM
9696 {
9697 if (update_mode_line)
9698 /* We may have to update a tty frame's menu bar or a
e037b9ec 9699 tool-bar. Example `M-x C-h C-h C-g'. */
5f5c8ee5
GM
9700 goto finish_menu_bars;
9701 else
9702 /* We've already displayed the echo area glyphs in this window. */
9703 goto finish_scroll_bars;
9704 }
73af359d 9705 else if (w != XWINDOW (minibuf_window))
8de2d90b 9706 {
5f5c8ee5
GM
9707 /* W is a mini-buffer window, but it's not the currently
9708 active one, so clear it. */
9709 int yb = window_text_bottom_y (w);
9710 struct glyph_row *row;
9711 int y;
9712
9713 for (y = 0, row = w->desired_matrix->rows;
9714 y < yb;
9715 y += row->height, ++row)
9716 blank_row (w, row, y);
88f22aff 9717 goto finish_scroll_bars;
8de2d90b
JB
9718 }
9719 }
a2889657 9720
5f5c8ee5
GM
9721 /* Otherwise set up data on this window; select its buffer and point
9722 value. */
6a93695f
GM
9723 /* Really select the buffer, for the sake of buffer-local
9724 variables. */
9725 set_buffer_internal_1 (XBUFFER (w->buffer));
5f5c8ee5
GM
9726 SET_TEXT_POS (opoint, PT, PT_BYTE);
9727
9728 current_matrix_up_to_date_p
9729 = (!NILP (w->window_end_valid)
9730 && !current_buffer->clip_changed
9731 && XFASTINT (w->last_modified) >= MODIFF
9732 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
e481f960 9733
5f5c8ee5
GM
9734 /* When windows_or_buffers_changed is non-zero, we can't rely on
9735 the window end being valid, so set it to nil there. */
9736 if (windows_or_buffers_changed)
9737 {
9738 /* If window starts on a continuation line, maybe adjust the
9739 window start in case the window's width changed. */
9740 if (XMARKER (w->start)->buffer == current_buffer)
9741 compute_window_start_on_continuation_line (w);
9742
9743 w->window_end_valid = Qnil;
9744 }
12adba34 9745
5f5c8ee5
GM
9746 /* Some sanity checks. */
9747 CHECK_WINDOW_END (w);
9748 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12adba34 9749 abort ();
5f5c8ee5 9750 if (BYTEPOS (opoint) < CHARPOS (opoint))
12adba34 9751 abort ();
a2889657 9752
28995e67
RS
9753 /* If %c is in mode line, update it if needed. */
9754 if (!NILP (w->column_number_displayed)
9755 /* This alternative quickly identifies a common case
9756 where no change is needed. */
9757 && !(PT == XFASTINT (w->last_point)
8850a573
RS
9758 && XFASTINT (w->last_modified) >= MODIFF
9759 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
28995e67
RS
9760 && XFASTINT (w->column_number_displayed) != current_column ())
9761 update_mode_line = 1;
9762
5f5c8ee5
GM
9763 /* Count number of windows showing the selected buffer. An indirect
9764 buffer counts as its base buffer. */
9765 if (!just_this_one_p)
42640f83
RS
9766 {
9767 struct buffer *current_base, *window_base;
9768 current_base = current_buffer;
9769 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
9770 if (current_base->base_buffer)
9771 current_base = current_base->base_buffer;
9772 if (window_base->base_buffer)
9773 window_base = window_base->base_buffer;
9774 if (current_base == window_base)
9775 buffer_shared++;
9776 }
a2889657 9777
5f5c8ee5
GM
9778 /* Point refers normally to the selected window. For any other
9779 window, set up appropriate value. */
a2889657
JB
9780 if (!EQ (window, selected_window))
9781 {
12adba34
RS
9782 int new_pt = XMARKER (w->pointm)->charpos;
9783 int new_pt_byte = marker_byte_position (w->pointm);
f67a0f51 9784 if (new_pt < BEGV)
a2889657 9785 {
f67a0f51 9786 new_pt = BEGV;
12adba34
RS
9787 new_pt_byte = BEGV_BYTE;
9788 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
a2889657 9789 }
f67a0f51 9790 else if (new_pt > (ZV - 1))
a2889657 9791 {
f67a0f51 9792 new_pt = ZV;
12adba34
RS
9793 new_pt_byte = ZV_BYTE;
9794 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
a2889657 9795 }
5f5c8ee5 9796
f67a0f51 9797 /* We don't use SET_PT so that the point-motion hooks don't run. */
12adba34 9798 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
a2889657
JB
9799 }
9800
f4faa47c 9801 /* If any of the character widths specified in the display table
5f5c8ee5
GM
9802 have changed, invalidate the width run cache. It's true that
9803 this may be a bit late to catch such changes, but the rest of
f4faa47c
JB
9804 redisplay goes (non-fatally) haywire when the display table is
9805 changed, so why should we worry about doing any better? */
9806 if (current_buffer->width_run_cache)
9807 {
f908610f 9808 struct Lisp_Char_Table *disptab = buffer_display_table ();
f4faa47c
JB
9809
9810 if (! disptab_matches_widthtab (disptab,
9811 XVECTOR (current_buffer->width_table)))
9812 {
9813 invalidate_region_cache (current_buffer,
9814 current_buffer->width_run_cache,
9815 BEG, Z);
9816 recompute_width_table (current_buffer, disptab);
9817 }
9818 }
9819
a2889657 9820 /* If window-start is screwed up, choose a new one. */
a2889657
JB
9821 if (XMARKER (w->start)->buffer != current_buffer)
9822 goto recenter;
9823
5f5c8ee5 9824 SET_TEXT_POS_FROM_MARKER (startp, w->start);
a2889657 9825
cf0df6ab
RS
9826 /* If someone specified a new starting point but did not insist,
9827 check whether it can be used. */
cfad01b4
GM
9828 if (!NILP (w->optional_new_start)
9829 && CHARPOS (startp) >= BEGV
9830 && CHARPOS (startp) <= ZV)
cf0df6ab
RS
9831 {
9832 w->optional_new_start = Qnil;
5f5c8ee5
GM
9833 start_display (&it, w, startp);
9834 move_it_to (&it, PT, 0, it.last_visible_y, -1,
9835 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9836 if (IT_CHARPOS (it) == PT)
cf0df6ab
RS
9837 w->force_start = Qt;
9838 }
9839
8de2d90b 9840 /* Handle case where place to start displaying has been specified,
aa6d10fa 9841 unless the specified location is outside the accessible range. */
9472f927
GM
9842 if (!NILP (w->force_start)
9843 || w->frozen_window_start_p)
a2889657 9844 {
e63574d7 9845 w->force_start = Qnil;
5f5c8ee5 9846 w->vscroll = 0;
b5174a51 9847 w->window_end_valid = Qnil;
5f5c8ee5
GM
9848
9849 /* Forget any recorded base line for line number display. */
9850 if (!current_matrix_up_to_date_p
9851 || current_buffer->clip_changed)
9852 w->base_line_number = Qnil;
9853
75c43375
RS
9854 /* Redisplay the mode line. Select the buffer properly for that.
9855 Also, run the hook window-scroll-functions
9856 because we have scrolled. */
e63574d7
RS
9857 /* Note, we do this after clearing force_start because
9858 if there's an error, it is better to forget about force_start
9859 than to get into an infinite loop calling the hook functions
9860 and having them get more errors. */
75c43375
RS
9861 if (!update_mode_line
9862 || ! NILP (Vwindow_scroll_functions))
e481f960 9863 {
e481f960
RS
9864 update_mode_line = 1;
9865 w->update_mode_line = Qt;
5f5c8ee5 9866 startp = run_window_scroll_functions (window, startp);
e481f960 9867 }
5f5c8ee5 9868
ac90c44f
GM
9869 w->last_modified = make_number (0);
9870 w->last_overlay_modified = make_number (0);
5f5c8ee5
GM
9871 if (CHARPOS (startp) < BEGV)
9872 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
9873 else if (CHARPOS (startp) > ZV)
9874 SET_TEXT_POS (startp, ZV, ZV_BYTE);
9875
9876 /* Redisplay, then check if cursor has been set during the
9877 redisplay. Give up if new fonts were loaded. */
9878 if (!try_window (window, startp))
9879 {
9880 w->force_start = Qt;
9881 clear_glyph_matrix (w->desired_matrix);
504454e8 9882 goto finish_scroll_bars;
5f5c8ee5
GM
9883 }
9884
9472f927 9885 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
5f5c8ee5 9886 {
b28cb6ed
GM
9887 /* If point does not appear, try to move point so it does
9888 appear. The desired matrix has been built above, so we
9889 can use it here. */
9890 int window_height;
9891 struct glyph_row *row;
9892
9893 window_height = window_box_height (w) / 2;
9894 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
9895 while (MATRIX_ROW_BOTTOM_Y (row) < window_height)
5f5c8ee5
GM
9896 ++row;
9897
9898 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
9899 MATRIX_ROW_START_BYTEPOS (row));
9900
90adcf20 9901 if (w != XWINDOW (selected_window))
12adba34 9902 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5f5c8ee5
GM
9903 else if (current_buffer == old)
9904 SET_TEXT_POS (lpoint, PT, PT_BYTE);
9905
9906 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
9907
9908 /* If we are highlighting the region, then we just changed
9909 the region, so redisplay to show it. */
df0b5ea1
RS
9910 if (!NILP (Vtransient_mark_mode)
9911 && !NILP (current_buffer->mark_active))
6f27fa9b 9912 {
5f5c8ee5
GM
9913 clear_glyph_matrix (w->desired_matrix);
9914 if (!try_window (window, startp))
504454e8 9915 goto finish_scroll_bars;
6f27fa9b 9916 }
a2889657 9917 }
5f5c8ee5
GM
9918
9919 make_cursor_line_fully_visible (w);
9920#if GLYPH_DEBUG
9921 debug_method_add (w, "forced window start");
9922#endif
a2889657
JB
9923 goto done;
9924 }
9925
5f5c8ee5
GM
9926 /* Handle case where text has not changed, only point, and it has
9927 not moved off the frame. */
9928 if (current_matrix_up_to_date_p
47589c8c
GM
9929 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
9930 rc != 0))
a2889657 9931 {
47589c8c 9932 if (rc == -1)
5f5c8ee5 9933 goto try_to_scroll;
47589c8c
GM
9934 else
9935 goto done;
a2889657
JB
9936 }
9937 /* If current starting point was originally the beginning of a line
9938 but no longer is, find a new starting point. */
265a9e55 9939 else if (!NILP (w->start_at_line_beg)
5f5c8ee5
GM
9940 && !(CHARPOS (startp) <= BEGV
9941 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
a2889657 9942 {
5f5c8ee5
GM
9943#if GLYPH_DEBUG
9944 debug_method_add (w, "recenter 1");
9945#endif
a2889657
JB
9946 goto recenter;
9947 }
5f5c8ee5
GM
9948
9949 /* Try scrolling with try_window_id. */
9142dd5b
GM
9950 else if (/* Windows and buffers haven't changed. */
9951 !windows_or_buffers_changed
5f5c8ee5
GM
9952 /* Window must be either use window-based redisplay or
9953 be full width. */
9954 && (FRAME_WINDOW_P (f)
c59c668a 9955 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)))
5f5c8ee5
GM
9956 && !MINI_WINDOW_P (w)
9957 /* Point is not known NOT to appear in window. */
9958 && PT >= CHARPOS (startp)
a2889657 9959 && XFASTINT (w->last_modified)
5f5c8ee5
GM
9960 /* Window is not hscrolled. */
9961 && XFASTINT (w->hscroll) == 0
9962 /* Selective display has not changed. */
9963 && !current_buffer->clip_changed
9964 /* Current matrix is up to date. */
9965 && !NILP (w->window_end_valid)
9966 /* Can't use this case if highlighting a region because
9967 a cursor movement will do more than just set the cursor. */
bd66d1ba
RS
9968 && !(!NILP (Vtransient_mark_mode)
9969 && !NILP (current_buffer->mark_active))
9970 && NILP (w->region_showing)
8f897821 9971 && NILP (Vshow_trailing_whitespace)
5f5c8ee5 9972 /* Overlay arrow position and string not changed. */
d45de95b 9973 && EQ (last_arrow_position, COERCE_MARKER (Voverlay_arrow_position))
a2889657 9974 && EQ (last_arrow_string, Voverlay_arrow_string)
5f5c8ee5
GM
9975 /* Value is > 0 if update has been done, it is -1 if we
9976 know that the same window start will not work. It is 0
9977 if unsuccessful for some other reason. */
9978 && (tem = try_window_id (w)) != 0)
a2889657 9979 {
5f5c8ee5 9980#if GLYPH_DEBUG
ef121659 9981 debug_method_add (w, "try_window_id %d", tem);
5f5c8ee5
GM
9982#endif
9983
9984 if (fonts_changed_p)
504454e8 9985 goto finish_scroll_bars;
a2889657
JB
9986 if (tem > 0)
9987 goto done;
ef121659 9988
5f5c8ee5
GM
9989 /* Otherwise try_window_id has returned -1 which means that we
9990 don't want the alternative below this comment to execute. */
a2889657 9991 }
5f5c8ee5
GM
9992 else if (CHARPOS (startp) >= BEGV
9993 && CHARPOS (startp) <= ZV
9994 && PT >= CHARPOS (startp)
9995 && (CHARPOS (startp) < ZV
e9874cee 9996 /* Avoid starting at end of buffer. */
5f5c8ee5 9997 || CHARPOS (startp) == BEGV
8850a573
RS
9998 || (XFASTINT (w->last_modified) >= MODIFF
9999 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
a2889657 10000 {
5f5c8ee5
GM
10001#if GLYPH_DEBUG
10002 debug_method_add (w, "same window start");
10003#endif
10004
10005 /* Try to redisplay starting at same place as before.
10006 If point has not moved off frame, accept the results. */
10007 if (!current_matrix_up_to_date_p
10008 /* Don't use try_window_reusing_current_matrix in this case
15e26c76
GM
10009 because a window scroll function can have changed the
10010 buffer. */
5f5c8ee5
GM
10011 || !NILP (Vwindow_scroll_functions)
10012 || MINI_WINDOW_P (w)
10013 || !try_window_reusing_current_matrix (w))
10014 {
10015 IF_DEBUG (debug_method_add (w, "1"));
10016 try_window (window, startp);
10017 }
10018
10019 if (fonts_changed_p)
504454e8 10020 goto finish_scroll_bars;
5f5c8ee5
GM
10021
10022 if (w->cursor.vpos >= 0)
aa6d10fa 10023 {
5f5c8ee5
GM
10024 if (!just_this_one_p
10025 || current_buffer->clip_changed
9142dd5b 10026 || BEG_UNCHANGED < CHARPOS (startp))
aa6d10fa
RS
10027 /* Forget any recorded base line for line number display. */
10028 w->base_line_number = Qnil;
5f5c8ee5
GM
10029
10030 make_cursor_line_fully_visible (w);
aa6d10fa
RS
10031 goto done;
10032 }
a2889657 10033 else
5f5c8ee5 10034 clear_glyph_matrix (w->desired_matrix);
a2889657
JB
10035 }
10036
5f5c8ee5
GM
10037 try_to_scroll:
10038
ac90c44f
GM
10039 w->last_modified = make_number (0);
10040 w->last_overlay_modified = make_number (0);
5f5c8ee5 10041
e481f960
RS
10042 /* Redisplay the mode line. Select the buffer properly for that. */
10043 if (!update_mode_line)
10044 {
e481f960
RS
10045 update_mode_line = 1;
10046 w->update_mode_line = Qt;
10047 }
a2889657 10048
5f5c8ee5
GM
10049 /* Try to scroll by specified few lines. */
10050 if ((scroll_conservatively
10051 || scroll_step
10052 || temp_scroll_step
10053 || NUMBERP (current_buffer->scroll_up_aggressively)
10054 || NUMBERP (current_buffer->scroll_down_aggressively))
09cacf9c 10055 && !current_buffer->clip_changed
5f5c8ee5
GM
10056 && CHARPOS (startp) >= BEGV
10057 && CHARPOS (startp) <= ZV)
0789adb2 10058 {
5f5c8ee5
GM
10059 /* The function returns -1 if new fonts were loaded, 1 if
10060 successful, 0 if not successful. */
10061 int rc = try_scrolling (window, just_this_one_p,
10062 scroll_conservatively,
10063 scroll_step,
10064 temp_scroll_step);
10065 if (rc > 0)
10066 goto done;
10067 else if (rc < 0)
504454e8 10068 goto finish_scroll_bars;
5f5c8ee5 10069 }
f9c8af06 10070
5f5c8ee5 10071 /* Finally, just choose place to start which centers point */
5936754e 10072
5f5c8ee5 10073 recenter:
44173109 10074
5f5c8ee5
GM
10075#if GLYPH_DEBUG
10076 debug_method_add (w, "recenter");
10077#endif
0789adb2 10078
5f5c8ee5 10079 /* w->vscroll = 0; */
0789adb2 10080
5f5c8ee5
GM
10081 /* Forget any previously recorded base line for line number display. */
10082 if (!current_matrix_up_to_date_p
10083 || current_buffer->clip_changed)
10084 w->base_line_number = Qnil;
10085
10086 /* Move backward half the height of the window. */
10087 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10088 it.current_y = it.last_visible_y;
10089 move_it_vertically_backward (&it, it.last_visible_y / 2);
10090 xassert (IT_CHARPOS (it) >= BEGV);
10091
10092 /* The function move_it_vertically_backward may move over more
10093 than the specified y-distance. If it->w is small, e.g. a
10094 mini-buffer window, we may end up in front of the window's
10095 display area. Start displaying at the start of the line
10096 containing PT in this case. */
10097 if (it.current_y <= 0)
10098 {
10099 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
10100 move_it_vertically (&it, 0);
10101 xassert (IT_CHARPOS (it) <= PT);
10102 it.current_y = 0;
0789adb2
RS
10103 }
10104
5f5c8ee5
GM
10105 it.current_x = it.hpos = 0;
10106
10107 /* Set startp here explicitly in case that helps avoid an infinite loop
10108 in case the window-scroll-functions functions get errors. */
10109 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
10110
10111 /* Run scroll hooks. */
10112 startp = run_window_scroll_functions (window, it.current.pos);
10113
10114 /* Redisplay the window. */
10115 if (!current_matrix_up_to_date_p
10116 || windows_or_buffers_changed
10117 /* Don't use try_window_reusing_current_matrix in this case
10118 because it can have changed the buffer. */
10119 || !NILP (Vwindow_scroll_functions)
10120 || !just_this_one_p
10121 || MINI_WINDOW_P (w)
10122 || !try_window_reusing_current_matrix (w))
10123 try_window (window, startp);
10124
10125 /* If new fonts have been loaded (due to fontsets), give up. We
10126 have to start a new redisplay since we need to re-adjust glyph
10127 matrices. */
10128 if (fonts_changed_p)
504454e8 10129 goto finish_scroll_bars;
5f5c8ee5
GM
10130
10131 /* If cursor did not appear assume that the middle of the window is
10132 in the first line of the window. Do it again with the next line.
10133 (Imagine a window of height 100, displaying two lines of height
10134 60. Moving back 50 from it->last_visible_y will end in the first
10135 line.) */
10136 if (w->cursor.vpos < 0)
a2889657 10137 {
5f5c8ee5
GM
10138 if (!NILP (w->window_end_valid)
10139 && PT >= Z - XFASTINT (w->window_end_pos))
a2889657 10140 {
5f5c8ee5
GM
10141 clear_glyph_matrix (w->desired_matrix);
10142 move_it_by_lines (&it, 1, 0);
10143 try_window (window, it.current.pos);
a2889657 10144 }
5f5c8ee5 10145 else if (PT < IT_CHARPOS (it))
a2889657 10146 {
5f5c8ee5
GM
10147 clear_glyph_matrix (w->desired_matrix);
10148 move_it_by_lines (&it, -1, 0);
10149 try_window (window, it.current.pos);
10150 }
10151 else
10152 {
10153 /* Not much we can do about it. */
a2889657 10154 }
a2889657 10155 }
010494d0 10156
5f5c8ee5
GM
10157 /* Consider the following case: Window starts at BEGV, there is
10158 invisible, intangible text at BEGV, so that display starts at
10159 some point START > BEGV. It can happen that we are called with
10160 PT somewhere between BEGV and START. Try to handle that case. */
10161 if (w->cursor.vpos < 0)
835766b6 10162 {
5f5c8ee5
GM
10163 struct glyph_row *row = w->current_matrix->rows;
10164 if (row->mode_line_p)
10165 ++row;
10166 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
835766b6 10167 }
5f5c8ee5
GM
10168
10169 make_cursor_line_fully_visible (w);
b5174a51 10170
74d481ac
GM
10171 done:
10172
5f5c8ee5
GM
10173 SET_TEXT_POS_FROM_MARKER (startp, w->start);
10174 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
10175 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
10176 ? Qt : Qnil);
a2889657 10177
5f5c8ee5 10178 /* Display the mode line, if we must. */
e481f960 10179 if ((update_mode_line
aa6d10fa 10180 /* If window not full width, must redo its mode line
5f5c8ee5
GM
10181 if (a) the window to its side is being redone and
10182 (b) we do a frame-based redisplay. This is a consequence
10183 of how inverted lines are drawn in frame-based redisplay. */
10184 || (!just_this_one_p
10185 && !FRAME_WINDOW_P (f)
10186 && !WINDOW_FULL_WIDTH_P (w))
10187 /* Line number to display. */
155ef550 10188 || INTEGERP (w->base_line_pos)
5f5c8ee5 10189 /* Column number is displayed and different from the one displayed. */
155ef550
KH
10190 || (!NILP (w->column_number_displayed)
10191 && XFASTINT (w->column_number_displayed) != current_column ()))
5f5c8ee5
GM
10192 /* This means that the window has a mode line. */
10193 && (WINDOW_WANTS_MODELINE_P (w)
045dee35 10194 || WINDOW_WANTS_HEADER_LINE_P (w)))
5ba50c51 10195 {
5da11938
GM
10196 Lisp_Object old_selected_frame;
10197
10198 old_selected_frame = selected_frame;
10199
5da11938 10200 XSETFRAME (selected_frame, f);
5f5c8ee5 10201 display_mode_lines (w);
5da11938 10202 selected_frame = old_selected_frame;
5f5c8ee5
GM
10203
10204 /* If mode line height has changed, arrange for a thorough
10205 immediate redisplay using the correct mode line height. */
10206 if (WINDOW_WANTS_MODELINE_P (w)
10207 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
5ba50c51 10208 {
5f5c8ee5
GM
10209 fonts_changed_p = 1;
10210 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
10211 = DESIRED_MODE_LINE_HEIGHT (w);
5ba50c51 10212 }
5f5c8ee5
GM
10213
10214 /* If top line height has changed, arrange for a thorough
10215 immediate redisplay using the correct mode line height. */
045dee35
GM
10216 if (WINDOW_WANTS_HEADER_LINE_P (w)
10217 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
5f5c8ee5
GM
10218 {
10219 fonts_changed_p = 1;
045dee35
GM
10220 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
10221 = DESIRED_HEADER_LINE_HEIGHT (w);
5f5c8ee5
GM
10222 }
10223
10224 if (fonts_changed_p)
504454e8 10225 goto finish_scroll_bars;
5ba50c51 10226 }
5f5c8ee5
GM
10227
10228 if (!line_number_displayed
10229 && !BUFFERP (w->base_line_pos))
aa6d10fa
RS
10230 {
10231 w->base_line_pos = Qnil;
10232 w->base_line_number = Qnil;
10233 }
a2889657 10234
5f5c8ee5
GM
10235 finish_menu_bars:
10236
7ce2c095 10237 /* When we reach a frame's selected window, redo the frame's menu bar. */
e481f960 10238 if (update_mode_line
5f5c8ee5
GM
10239 && EQ (FRAME_SELECTED_WINDOW (f), window))
10240 {
10241 int redisplay_menu_p = 0;
10242
10243 if (FRAME_WINDOW_P (f))
10244 {
1a578e9b 10245#if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5f5c8ee5 10246 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
76412d64 10247#else
5f5c8ee5 10248 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
76412d64 10249#endif
5f5c8ee5
GM
10250 }
10251 else
10252 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
10253
10254 if (redisplay_menu_p)
10255 display_menu_bar (w);
10256
10257#ifdef HAVE_WINDOW_SYSTEM
e037b9ec
GM
10258 if (WINDOWP (f->tool_bar_window)
10259 && (FRAME_TOOL_BAR_LINES (f) > 0
10260 || auto_resize_tool_bars_p))
10261 redisplay_tool_bar (f);
5f5c8ee5
GM
10262#endif
10263 }
7ce2c095 10264
88f22aff 10265 finish_scroll_bars:
5f5c8ee5 10266
88f22aff 10267 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
30c566e4 10268 {
b1d1124b 10269 int start, end, whole;
30c566e4 10270
b1d1124b 10271 /* Calculate the start and end positions for the current window.
3505ea70
JB
10272 At some point, it would be nice to choose between scrollbars
10273 which reflect the whole buffer size, with special markers
10274 indicating narrowing, and scrollbars which reflect only the
10275 visible region.
10276
5f5c8ee5 10277 Note that mini-buffers sometimes aren't displaying any text. */
c6e89d6c 10278 if (!MINI_WINDOW_P (w)
5f5c8ee5 10279 || (w == XWINDOW (minibuf_window)
c6e89d6c 10280 && NILP (echo_area_buffer[0])))
b1d1124b 10281 {
8a9311d7 10282 whole = ZV - BEGV;
4d641a15 10283 start = marker_position (w->start) - BEGV;
b1d1124b
JB
10284 /* I don't think this is guaranteed to be right. For the
10285 moment, we'll pretend it is. */
5f5c8ee5 10286 end = (Z - XFASTINT (w->window_end_pos)) - BEGV;
3505ea70 10287
5f5c8ee5
GM
10288 if (end < start)
10289 end = start;
10290 if (whole < (end - start))
10291 whole = end - start;
b1d1124b
JB
10292 }
10293 else
10294 start = end = whole = 0;
30c566e4 10295
88f22aff 10296 /* Indicate what this scroll bar ought to be displaying now. */
504454e8 10297 set_vertical_scroll_bar_hook (w, end - start, whole, start);
30c566e4 10298
5f5c8ee5
GM
10299 /* Note that we actually used the scroll bar attached to this
10300 window, so it shouldn't be deleted at the end of redisplay. */
504454e8 10301 redeem_scroll_bar_hook (w);
30c566e4 10302 }
b1d1124b 10303
5f5c8ee5
GM
10304 /* Restore current_buffer and value of point in it. */
10305 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
6a93695f 10306 set_buffer_internal_1 (old);
5f5c8ee5 10307 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
2e54982e
RS
10308
10309 unbind_to (count, Qnil);
a2889657 10310}
a2889657 10311
5f5c8ee5
GM
10312
10313/* Build the complete desired matrix of WINDOW with a window start
10314 buffer position POS. Value is non-zero if successful. It is zero
10315 if fonts were loaded during redisplay which makes re-adjusting
10316 glyph matrices necessary. */
10317
10318int
a2889657
JB
10319try_window (window, pos)
10320 Lisp_Object window;
5f5c8ee5
GM
10321 struct text_pos pos;
10322{
10323 struct window *w = XWINDOW (window);
10324 struct it it;
10325 struct glyph_row *last_text_row = NULL;
9cbab4ff 10326
5f5c8ee5
GM
10327 /* Make POS the new window start. */
10328 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12adba34 10329
5f5c8ee5
GM
10330 /* Mark cursor position as unknown. No overlay arrow seen. */
10331 w->cursor.vpos = -1;
a2889657 10332 overlay_arrow_seen = 0;
642eefc6 10333
5f5c8ee5
GM
10334 /* Initialize iterator and info to start at POS. */
10335 start_display (&it, w, pos);
a2889657 10336
5f5c8ee5
GM
10337 /* Display all lines of W. */
10338 while (it.current_y < it.last_visible_y)
10339 {
10340 if (display_line (&it))
10341 last_text_row = it.glyph_row - 1;
10342 if (fonts_changed_p)
10343 return 0;
10344 }
a2889657 10345
5f5c8ee5
GM
10346 /* If bottom moved off end of frame, change mode line percentage. */
10347 if (XFASTINT (w->window_end_pos) <= 0
10348 && Z != IT_CHARPOS (it))
a2889657
JB
10349 w->update_mode_line = Qt;
10350
5f5c8ee5
GM
10351 /* Set window_end_pos to the offset of the last character displayed
10352 on the window from the end of current_buffer. Set
10353 window_end_vpos to its row number. */
10354 if (last_text_row)
10355 {
10356 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
10357 w->window_end_bytepos
10358 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10359 w->window_end_pos
10360 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10361 w->window_end_vpos
10362 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10363 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
10364 ->displays_text_p);
10365 }
10366 else
10367 {
10368 w->window_end_bytepos = 0;
ac90c44f 10369 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10370 }
10371
a2889657
JB
10372 /* But that is not valid info until redisplay finishes. */
10373 w->window_end_valid = Qnil;
5f5c8ee5 10374 return 1;
a2889657 10375}
5f5c8ee5
GM
10376
10377
a2889657 10378\f
5f5c8ee5
GM
10379/************************************************************************
10380 Window redisplay reusing current matrix when buffer has not changed
10381 ************************************************************************/
10382
10383/* Try redisplay of window W showing an unchanged buffer with a
10384 different window start than the last time it was displayed by
10385 reusing its current matrix. Value is non-zero if successful.
10386 W->start is the new window start. */
a2889657
JB
10387
10388static int
5f5c8ee5
GM
10389try_window_reusing_current_matrix (w)
10390 struct window *w;
a2889657 10391{
5f5c8ee5
GM
10392 struct frame *f = XFRAME (w->frame);
10393 struct glyph_row *row, *bottom_row;
10394 struct it it;
10395 struct run run;
10396 struct text_pos start, new_start;
10397 int nrows_scrolled, i;
10398 struct glyph_row *last_text_row;
10399 struct glyph_row *last_reused_text_row;
10400 struct glyph_row *start_row;
10401 int start_vpos, min_y, max_y;
d18354b6
GM
10402
10403 if (/* This function doesn't handle terminal frames. */
10404 !FRAME_WINDOW_P (f)
10405 /* Don't try to reuse the display if windows have been split
10406 or such. */
10407 || windows_or_buffers_changed)
5f5c8ee5 10408 return 0;
a2889657 10409
5f5c8ee5
GM
10410 /* Can't do this if region may have changed. */
10411 if ((!NILP (Vtransient_mark_mode)
10412 && !NILP (current_buffer->mark_active))
8f897821
GM
10413 || !NILP (w->region_showing)
10414 || !NILP (Vshow_trailing_whitespace))
5f5c8ee5 10415 return 0;
a2889657 10416
5f5c8ee5 10417 /* If top-line visibility has changed, give up. */
045dee35
GM
10418 if (WINDOW_WANTS_HEADER_LINE_P (w)
10419 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
5f5c8ee5
GM
10420 return 0;
10421
10422 /* Give up if old or new display is scrolled vertically. We could
10423 make this function handle this, but right now it doesn't. */
10424 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10425 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (start_row))
10426 return 0;
10427
10428 /* The variable new_start now holds the new window start. The old
10429 start `start' can be determined from the current matrix. */
10430 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
10431 start = start_row->start.pos;
10432 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
a2889657 10433
5f5c8ee5
GM
10434 /* Clear the desired matrix for the display below. */
10435 clear_glyph_matrix (w->desired_matrix);
10436
10437 if (CHARPOS (new_start) <= CHARPOS (start))
10438 {
10439 int first_row_y;
10440
607ec83c
GM
10441 /* Don't use this method if the display starts with an ellipsis
10442 displayed for invisible text. It's not easy to handle that case
10443 below, and it's certainly not worth the effort since this is
10444 not a frequent case. */
10445 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
10446 return 0;
10447
5f5c8ee5
GM
10448 IF_DEBUG (debug_method_add (w, "twu1"));
10449
10450 /* Display up to a row that can be reused. The variable
10451 last_text_row is set to the last row displayed that displays
b48f74cb
GM
10452 text. Note that it.vpos == 0 if or if not there is a
10453 header-line; it's not the same as the MATRIX_ROW_VPOS! */
5f5c8ee5
GM
10454 start_display (&it, w, new_start);
10455 first_row_y = it.current_y;
10456 w->cursor.vpos = -1;
10457 last_text_row = last_reused_text_row = NULL;
b48f74cb 10458
5f5c8ee5
GM
10459 while (it.current_y < it.last_visible_y
10460 && IT_CHARPOS (it) < CHARPOS (start)
10461 && !fonts_changed_p)
10462 if (display_line (&it))
10463 last_text_row = it.glyph_row - 1;
10464
10465 /* A value of current_y < last_visible_y means that we stopped
10466 at the previous window start, which in turn means that we
10467 have at least one reusable row. */
10468 if (it.current_y < it.last_visible_y)
a2889657 10469 {
b48f74cb 10470 /* IT.vpos always starts from 0; it counts text lines. */
5f5c8ee5
GM
10471 nrows_scrolled = it.vpos;
10472
10473 /* Find PT if not already found in the lines displayed. */
10474 if (w->cursor.vpos < 0)
a2889657 10475 {
5f5c8ee5
GM
10476 int dy = it.current_y - first_row_y;
10477
10478 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10479 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10480 {
4bde0ebb
GM
10481 if (PT >= MATRIX_ROW_START_CHARPOS (row)
10482 && PT < MATRIX_ROW_END_CHARPOS (row))
5f5c8ee5
GM
10483 {
10484 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
10485 dy, nrows_scrolled);
10486 break;
10487 }
10488
10489 if (MATRIX_ROW_BOTTOM_Y (row) + dy >= it.last_visible_y)
10490 break;
10491
10492 ++row;
10493 }
10494
10495 /* Give up if point was not found. This shouldn't
10496 happen often; not more often than with try_window
10497 itself. */
10498 if (w->cursor.vpos < 0)
10499 {
10500 clear_glyph_matrix (w->desired_matrix);
10501 return 0;
10502 }
a2889657 10503 }
5f5c8ee5
GM
10504
10505 /* Scroll the display. Do it before the current matrix is
10506 changed. The problem here is that update has not yet
10507 run, i.e. part of the current matrix is not up to date.
10508 scroll_run_hook will clear the cursor, and use the
10509 current matrix to get the height of the row the cursor is
10510 in. */
10511 run.current_y = first_row_y;
10512 run.desired_y = it.current_y;
10513 run.height = it.last_visible_y - it.current_y;
b48f74cb
GM
10514
10515 if (run.height > 0 && run.current_y != run.desired_y)
a2889657 10516 {
5f5c8ee5
GM
10517 update_begin (f);
10518 rif->update_window_begin_hook (w);
64d1e7d3 10519 rif->clear_mouse_face (w);
5f5c8ee5 10520 rif->scroll_run_hook (w, &run);
64d1e7d3 10521 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5 10522 update_end (f);
a2889657 10523 }
5f5c8ee5
GM
10524
10525 /* Shift current matrix down by nrows_scrolled lines. */
10526 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
10527 rotate_matrix (w->current_matrix,
10528 start_vpos,
10529 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10530 nrows_scrolled);
10531
9c8b8382 10532 /* Disable lines that must be updated. */
5f5c8ee5 10533 for (i = 0; i < it.vpos; ++i)
b48f74cb 10534 (start_row + i)->enabled_p = 0;
9c8b8382 10535
5f5c8ee5 10536 /* Re-compute Y positions. */
045dee35 10537 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10538 max_y = it.last_visible_y;
b48f74cb
GM
10539 for (row = start_row + nrows_scrolled;
10540 row < bottom_row;
10541 ++row)
d2f84654 10542 {
5f5c8ee5
GM
10543 row->y = it.current_y;
10544
10545 if (row->y < min_y)
10546 row->visible_height = row->height - (min_y - row->y);
10547 else if (row->y + row->height > max_y)
10548 row->visible_height
10549 = row->height - (row->y + row->height - max_y);
10550 else
10551 row->visible_height = row->height;
10552
10553 it.current_y += row->height;
5f5c8ee5
GM
10554
10555 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10556 last_reused_text_row = row;
10557 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
10558 break;
d2f84654 10559 }
9c8b8382
GM
10560
10561 /* Disable lines in the current matrix which are now
10562 below the window. */
bafb434c 10563 for (++row; row < bottom_row; ++row)
9c8b8382 10564 row->enabled_p = 0;
a2889657 10565 }
5f5c8ee5
GM
10566
10567 /* Update window_end_pos etc.; last_reused_text_row is the last
10568 reused row from the current matrix containing text, if any.
10569 The value of last_text_row is the last displayed line
10570 containing text. */
10571 if (last_reused_text_row)
a2889657 10572 {
5f5c8ee5
GM
10573 w->window_end_bytepos
10574 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
ac90c44f
GM
10575 w->window_end_pos
10576 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
10577 w->window_end_vpos
10578 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
10579 w->current_matrix));
a2889657 10580 }
5f5c8ee5
GM
10581 else if (last_text_row)
10582 {
10583 w->window_end_bytepos
10584 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10585 w->window_end_pos
10586 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10587 w->window_end_vpos
10588 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10589 }
10590 else
10591 {
10592 /* This window must be completely empty. */
10593 w->window_end_bytepos = 0;
ac90c44f 10594 w->window_end_pos = w->window_end_vpos = make_number (0);
5f5c8ee5
GM
10595 }
10596 w->window_end_valid = Qnil;
a2889657 10597
5f5c8ee5
GM
10598 /* Update hint: don't try scrolling again in update_window. */
10599 w->desired_matrix->no_scrolling_p = 1;
10600
10601#if GLYPH_DEBUG
10602 debug_method_add (w, "try_window_reusing_current_matrix 1");
10603#endif
10604 return 1;
a2889657 10605 }
5f5c8ee5
GM
10606 else if (CHARPOS (new_start) > CHARPOS (start))
10607 {
10608 struct glyph_row *pt_row, *row;
10609 struct glyph_row *first_reusable_row;
10610 struct glyph_row *first_row_to_display;
10611 int dy;
10612 int yb = window_text_bottom_y (w);
10613
5f5c8ee5
GM
10614 /* Find the row starting at new_start, if there is one. Don't
10615 reuse a partially visible line at the end. */
b48f74cb 10616 first_reusable_row = start_row;
5f5c8ee5
GM
10617 while (first_reusable_row->enabled_p
10618 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
10619 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10620 < CHARPOS (new_start)))
10621 ++first_reusable_row;
10622
10623 /* Give up if there is no row to reuse. */
10624 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
28514cd9
GM
10625 || !first_reusable_row->enabled_p
10626 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
10627 != CHARPOS (new_start)))
5f5c8ee5
GM
10628 return 0;
10629
5f5c8ee5
GM
10630 /* We can reuse fully visible rows beginning with
10631 first_reusable_row to the end of the window. Set
10632 first_row_to_display to the first row that cannot be reused.
10633 Set pt_row to the row containing point, if there is any. */
5f5c8ee5 10634 pt_row = NULL;
ac90c44f
GM
10635 for (first_row_to_display = first_reusable_row;
10636 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
10637 ++first_row_to_display)
5f5c8ee5 10638 {
4bde0ebb
GM
10639 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
10640 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
5f5c8ee5 10641 pt_row = first_row_to_display;
5f5c8ee5 10642 }
a2889657 10643
5f5c8ee5
GM
10644 /* Start displaying at the start of first_row_to_display. */
10645 xassert (first_row_to_display->y < yb);
10646 init_to_row_start (&it, w, first_row_to_display);
607ec83c 10647
b48f74cb
GM
10648 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
10649 - start_vpos);
5f5c8ee5
GM
10650 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
10651 - nrows_scrolled);
b48f74cb
GM
10652 it.current_y = (first_row_to_display->y - first_reusable_row->y
10653 + WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w));
5f5c8ee5
GM
10654
10655 /* Display lines beginning with first_row_to_display in the
10656 desired matrix. Set last_text_row to the last row displayed
10657 that displays text. */
10658 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
10659 if (pt_row == NULL)
10660 w->cursor.vpos = -1;
10661 last_text_row = NULL;
10662 while (it.current_y < it.last_visible_y && !fonts_changed_p)
10663 if (display_line (&it))
10664 last_text_row = it.glyph_row - 1;
10665
10666 /* Give up If point isn't in a row displayed or reused. */
10667 if (w->cursor.vpos < 0)
10668 {
10669 clear_glyph_matrix (w->desired_matrix);
10670 return 0;
10671 }
12adba34 10672
5f5c8ee5
GM
10673 /* If point is in a reused row, adjust y and vpos of the cursor
10674 position. */
10675 if (pt_row)
10676 {
10677 w->cursor.vpos -= MATRIX_ROW_VPOS (first_reusable_row,
10678 w->current_matrix);
10679 w->cursor.y -= first_reusable_row->y;
a2889657
JB
10680 }
10681
5f5c8ee5
GM
10682 /* Scroll the display. */
10683 run.current_y = first_reusable_row->y;
045dee35 10684 run.desired_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10685 run.height = it.last_visible_y - run.current_y;
4da61803
GM
10686 dy = run.current_y - run.desired_y;
10687
5f5c8ee5
GM
10688 if (run.height)
10689 {
10690 struct frame *f = XFRAME (WINDOW_FRAME (w));
10691 update_begin (f);
10692 rif->update_window_begin_hook (w);
64d1e7d3 10693 rif->clear_mouse_face (w);
5f5c8ee5 10694 rif->scroll_run_hook (w, &run);
64d1e7d3 10695 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
10696 update_end (f);
10697 }
a2889657 10698
5f5c8ee5
GM
10699 /* Adjust Y positions of reused rows. */
10700 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
045dee35 10701 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5f5c8ee5 10702 max_y = it.last_visible_y;
b48f74cb 10703 for (row = first_reusable_row; row < first_row_to_display; ++row)
5f5c8ee5
GM
10704 {
10705 row->y -= dy;
10706 if (row->y < min_y)
10707 row->visible_height = row->height - (min_y - row->y);
10708 else if (row->y + row->height > max_y)
10709 row->visible_height
10710 = row->height - (row->y + row->height - max_y);
10711 else
10712 row->visible_height = row->height;
5f5c8ee5 10713 }
a2889657 10714
5f5c8ee5
GM
10715 /* Scroll the current matrix. */
10716 xassert (nrows_scrolled > 0);
10717 rotate_matrix (w->current_matrix,
10718 start_vpos,
10719 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
10720 -nrows_scrolled);
10721
ac90c44f
GM
10722 /* Disable rows not reused. */
10723 for (row -= nrows_scrolled; row < bottom_row; ++row)
10724 row->enabled_p = 0;
10725
5f5c8ee5
GM
10726 /* Adjust window end. A null value of last_text_row means that
10727 the window end is in reused rows which in turn means that
10728 only its vpos can have changed. */
10729 if (last_text_row)
10730 {
10731 w->window_end_bytepos
10732 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
10733 w->window_end_pos
10734 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
10735 w->window_end_vpos
10736 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
5f5c8ee5
GM
10737 }
10738 else
a2889657 10739 {
ac90c44f
GM
10740 w->window_end_vpos
10741 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
a2889657 10742 }
5f5c8ee5
GM
10743
10744 w->window_end_valid = Qnil;
10745 w->desired_matrix->no_scrolling_p = 1;
10746
10747#if GLYPH_DEBUG
10748 debug_method_add (w, "try_window_reusing_current_matrix 2");
10749#endif
10750 return 1;
a2889657 10751 }
5f5c8ee5
GM
10752
10753 return 0;
10754}
a2889657 10755
a2889657 10756
5f5c8ee5
GM
10757\f
10758/************************************************************************
10759 Window redisplay reusing current matrix when buffer has changed
10760 ************************************************************************/
10761
1ec185cb
GM
10762static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
10763static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
5f5c8ee5
GM
10764 int *, int *));
10765static struct glyph_row *
10766find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
10767 struct glyph_row *));
10768
10769
10770/* Return the last row in MATRIX displaying text. If row START is
10771 non-null, start searching with that row. IT gives the dimensions
10772 of the display. Value is null if matrix is empty; otherwise it is
10773 a pointer to the row found. */
10774
10775static struct glyph_row *
10776find_last_row_displaying_text (matrix, it, start)
10777 struct glyph_matrix *matrix;
10778 struct it *it;
10779 struct glyph_row *start;
10780{
10781 struct glyph_row *row, *row_found;
10782
10783 /* Set row_found to the last row in IT->w's current matrix
10784 displaying text. The loop looks funny but think of partially
10785 visible lines. */
10786 row_found = NULL;
10787 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
10788 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10789 {
10790 xassert (row->enabled_p);
10791 row_found = row;
10792 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
10793 break;
10794 ++row;
a2889657 10795 }
5f5c8ee5
GM
10796
10797 return row_found;
10798}
10799
a2889657 10800
5f5c8ee5
GM
10801/* Return the last row in the current matrix of W that is not affected
10802 by changes at the start of current_buffer that occurred since the
10803 last time W was redisplayed. Value is null if no such row exists.
a2889657 10804
5f5c8ee5
GM
10805 The global variable beg_unchanged has to contain the number of
10806 bytes unchanged at the start of current_buffer. BEG +
10807 beg_unchanged is the buffer position of the first changed byte in
10808 current_buffer. Characters at positions < BEG + beg_unchanged are
10809 at the same buffer positions as they were when the current matrix
10810 was built. */
10811
10812static struct glyph_row *
1ec185cb 10813find_last_unchanged_at_beg_row (w)
5f5c8ee5
GM
10814 struct window *w;
10815{
9142dd5b 10816 int first_changed_pos = BEG + BEG_UNCHANGED;
5f5c8ee5
GM
10817 struct glyph_row *row;
10818 struct glyph_row *row_found = NULL;
10819 int yb = window_text_bottom_y (w);
10820
10821 /* Find the last row displaying unchanged text. */
10822 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10823 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
10824 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
a2889657 10825 {
5f5c8ee5
GM
10826 if (/* If row ends before first_changed_pos, it is unchanged,
10827 except in some case. */
10828 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
10829 /* When row ends in ZV and we write at ZV it is not
10830 unchanged. */
10831 && !row->ends_at_zv_p
10832 /* When first_changed_pos is the end of a continued line,
10833 row is not unchanged because it may be no longer
10834 continued. */
10835 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
10836 && row->continued_p))
10837 row_found = row;
10838
10839 /* Stop if last visible row. */
10840 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
10841 break;
10842
10843 ++row;
a2889657
JB
10844 }
10845
5f5c8ee5 10846 return row_found;
a2889657 10847}
5f5c8ee5
GM
10848
10849
10850/* Find the first glyph row in the current matrix of W that is not
10851 affected by changes at the end of current_buffer since the last
10852 time the window was redisplayed. Return in *DELTA the number of
c59c668a
GM
10853 chars by which buffer positions in unchanged text at the end of
10854 current_buffer must be adjusted. Return in *DELTA_BYTES the
10855 corresponding number of bytes. Value is null if no such row
10856 exists, i.e. all rows are affected by changes. */
5f5c8ee5
GM
10857
10858static struct glyph_row *
1ec185cb 10859find_first_unchanged_at_end_row (w, delta, delta_bytes)
5f5c8ee5
GM
10860 struct window *w;
10861 int *delta, *delta_bytes;
a2889657 10862{
5f5c8ee5
GM
10863 struct glyph_row *row;
10864 struct glyph_row *row_found = NULL;
c581d710 10865
5f5c8ee5 10866 *delta = *delta_bytes = 0;
b2a76982 10867
1ec185cb
GM
10868 /* Display must not have been paused, otherwise the current matrix
10869 is not up to date. */
10870 if (NILP (w->window_end_valid))
10871 abort ();
10872
10873 /* A value of window_end_pos >= END_UNCHANGED means that the window
5f5c8ee5
GM
10874 end is in the range of changed text. If so, there is no
10875 unchanged row at the end of W's current matrix. */
9142dd5b 10876 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
5f5c8ee5
GM
10877 return NULL;
10878
10879 /* Set row to the last row in W's current matrix displaying text. */
10880 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
10881
5f5c8ee5
GM
10882 /* If matrix is entirely empty, no unchanged row exists. */
10883 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
10884 {
10885 /* The value of row is the last glyph row in the matrix having a
10886 meaningful buffer position in it. The end position of row
10887 corresponds to window_end_pos. This allows us to translate
10888 buffer positions in the current matrix to current buffer
10889 positions for characters not in changed text. */
10890 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
10891 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
10892 int last_unchanged_pos, last_unchanged_pos_old;
10893 struct glyph_row *first_text_row
10894 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
10895
10896 *delta = Z - Z_old;
10897 *delta_bytes = Z_BYTE - Z_BYTE_old;
10898
10899 /* Set last_unchanged_pos to the buffer position of the last
10900 character in the buffer that has not been changed. Z is the
10901 index + 1 of the last byte in current_buffer, i.e. by
10902 subtracting end_unchanged we get the index of the last
10903 unchanged character, and we have to add BEG to get its buffer
10904 position. */
9142dd5b 10905 last_unchanged_pos = Z - END_UNCHANGED + BEG;
5f5c8ee5
GM
10906 last_unchanged_pos_old = last_unchanged_pos - *delta;
10907
10908 /* Search backward from ROW for a row displaying a line that
10909 starts at a minimum position >= last_unchanged_pos_old. */
1ec185cb 10910 for (; row > first_text_row; --row)
5f5c8ee5 10911 {
1ec185cb
GM
10912 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
10913 abort ();
5f5c8ee5
GM
10914
10915 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
10916 row_found = row;
5f5c8ee5
GM
10917 }
10918 }
10919
1ec185cb
GM
10920 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
10921 abort ();
10922
5f5c8ee5 10923 return row_found;
c581d710
RS
10924}
10925
c581d710 10926
5f5c8ee5
GM
10927/* Make sure that glyph rows in the current matrix of window W
10928 reference the same glyph memory as corresponding rows in the
10929 frame's frame matrix. This function is called after scrolling W's
10930 current matrix on a terminal frame in try_window_id and
10931 try_window_reusing_current_matrix. */
10932
10933static void
10934sync_frame_with_window_matrix_rows (w)
10935 struct window *w;
c581d710 10936{
5f5c8ee5
GM
10937 struct frame *f = XFRAME (w->frame);
10938 struct glyph_row *window_row, *window_row_end, *frame_row;
10939
10940 /* Preconditions: W must be a leaf window and full-width. Its frame
10941 must have a frame matrix. */
10942 xassert (NILP (w->hchild) && NILP (w->vchild));
10943 xassert (WINDOW_FULL_WIDTH_P (w));
10944 xassert (!FRAME_WINDOW_P (f));
10945
10946 /* If W is a full-width window, glyph pointers in W's current matrix
10947 have, by definition, to be the same as glyph pointers in the
10948 corresponding frame matrix. */
10949 window_row = w->current_matrix->rows;
10950 window_row_end = window_row + w->current_matrix->nrows;
10951 frame_row = f->current_matrix->rows + XFASTINT (w->top);
10952 while (window_row < window_row_end)
659a218f 10953 {
5f5c8ee5 10954 int area;
f002db93 10955
5f5c8ee5
GM
10956 for (area = LEFT_MARGIN_AREA; area <= LAST_AREA; ++area)
10957 frame_row->glyphs[area] = window_row->glyphs[area];
f002db93
GM
10958
10959 /* Disable frame rows whose corresponding window rows have
10960 been disabled in try_window_id. */
10961 if (!window_row->enabled_p)
10962 frame_row->enabled_p = 0;
10963
5f5c8ee5 10964 ++window_row, ++frame_row;
659a218f 10965 }
a2889657 10966}
5f5c8ee5
GM
10967
10968
e037b9ec
GM
10969/* Find the glyph row in window W containing CHARPOS. Consider all
10970 rows between START and END (not inclusive). END null means search
10971 all rows to the end of the display area of W. Value is the row
10972 containing CHARPOS or null. */
10973
10974static struct glyph_row *
10975row_containing_pos (w, charpos, start, end)
10976 struct window *w;
10977 int charpos;
10978 struct glyph_row *start, *end;
10979{
10980 struct glyph_row *row = start;
10981 int last_y;
10982
10983 /* If we happen to start on a header-line, skip that. */
10984 if (row->mode_line_p)
10985 ++row;
10986
10987 if ((end && row >= end) || !row->enabled_p)
10988 return NULL;
10989
10990 last_y = window_text_bottom_y (w);
10991
10992 while ((end == NULL || row < end)
10993 && (MATRIX_ROW_END_CHARPOS (row) < charpos
10994 /* The end position of a row equals the start
10995 position of the next row. If CHARPOS is there, we
10996 would rather display it in the next line, except
10997 when this line ends in ZV. */
10998 || (MATRIX_ROW_END_CHARPOS (row) == charpos
10999 && (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)
11000 || !row->ends_at_zv_p)))
11001 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11002 ++row;
11003
11004 /* Give up if CHARPOS not found. */
11005 if ((end && row >= end)
11006 || charpos < MATRIX_ROW_START_CHARPOS (row)
11007 || charpos > MATRIX_ROW_END_CHARPOS (row))
11008 row = NULL;
11009
11010 return row;
11011}
11012
11013
5f5c8ee5
GM
11014/* Try to redisplay window W by reusing its existing display. W's
11015 current matrix must be up to date when this function is called,
11016 i.e. window_end_valid must not be nil.
11017
11018 Value is
11019
11020 1 if display has been updated
11021 0 if otherwise unsuccessful
11022 -1 if redisplay with same window start is known not to succeed
11023
11024 The following steps are performed:
11025
11026 1. Find the last row in the current matrix of W that is not
11027 affected by changes at the start of current_buffer. If no such row
11028 is found, give up.
11029
11030 2. Find the first row in W's current matrix that is not affected by
11031 changes at the end of current_buffer. Maybe there is no such row.
11032
11033 3. Display lines beginning with the row + 1 found in step 1 to the
11034 row found in step 2 or, if step 2 didn't find a row, to the end of
11035 the window.
11036
11037 4. If cursor is not known to appear on the window, give up.
11038
11039 5. If display stopped at the row found in step 2, scroll the
11040 display and current matrix as needed.
11041
11042 6. Maybe display some lines at the end of W, if we must. This can
11043 happen under various circumstances, like a partially visible line
11044 becoming fully visible, or because newly displayed lines are displayed
11045 in smaller font sizes.
11046
11047 7. Update W's window end information. */
11048
11049 /* Check that window end is what we expect it to be. */
12adba34
RS
11050
11051static int
5f5c8ee5 11052try_window_id (w)
12adba34 11053 struct window *w;
12adba34 11054{
5f5c8ee5
GM
11055 struct frame *f = XFRAME (w->frame);
11056 struct glyph_matrix *current_matrix = w->current_matrix;
11057 struct glyph_matrix *desired_matrix = w->desired_matrix;
11058 struct glyph_row *last_unchanged_at_beg_row;
11059 struct glyph_row *first_unchanged_at_end_row;
11060 struct glyph_row *row;
11061 struct glyph_row *bottom_row;
11062 int bottom_vpos;
11063 struct it it;
11064 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
11065 struct text_pos start_pos;
11066 struct run run;
11067 int first_unchanged_at_end_vpos = 0;
11068 struct glyph_row *last_text_row, *last_text_row_at_end;
11069 struct text_pos start;
11070
11071 SET_TEXT_POS_FROM_MARKER (start, w->start);
11072
11073 /* Check pre-conditions. Window end must be valid, otherwise
11074 the current matrix would not be up to date. */
11075 xassert (!NILP (w->window_end_valid));
11076 xassert (FRAME_WINDOW_P (XFRAME (w->frame))
11077 || (line_ins_del_ok && WINDOW_FULL_WIDTH_P (w)));
11078
11079 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
11080 only if buffer has really changed. The reason is that the gap is
11081 initially at Z for freshly visited files. The code below would
11082 set end_unchanged to 0 in that case. */
28ee91c0
GM
11083 if (MODIFF > SAVE_MODIFF
11084 /* This seems to happen sometimes after saving a buffer. */
11085 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
5f5c8ee5 11086 {
9142dd5b
GM
11087 if (GPT - BEG < BEG_UNCHANGED)
11088 BEG_UNCHANGED = GPT - BEG;
11089 if (Z - GPT < END_UNCHANGED)
11090 END_UNCHANGED = Z - GPT;
5f5c8ee5 11091 }
bf9249e3 11092
5f5c8ee5
GM
11093 /* If window starts after a line end, and the last change is in
11094 front of that newline, then changes don't affect the display.
f2d86d7a
GM
11095 This case happens with stealth-fontification. Note that although
11096 the display is unchanged, glyph positions in the matrix have to
11097 be adjusted, of course. */
5f5c8ee5
GM
11098 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
11099 if (CHARPOS (start) > BEGV
9142dd5b 11100 && Z - END_UNCHANGED < CHARPOS (start) - 1
5f5c8ee5
GM
11101 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n'
11102 && PT < MATRIX_ROW_END_CHARPOS (row))
11103 {
f2d86d7a
GM
11104 struct glyph_row *r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
11105 int delta = CHARPOS (start) - MATRIX_ROW_START_CHARPOS (r0);
11106
11107 if (delta)
11108 {
11109 struct glyph_row *r1 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11110 int delta_bytes = BYTEPOS (start) - MATRIX_ROW_START_BYTEPOS (r0);
11111
11112 increment_matrix_positions (w->current_matrix,
11113 MATRIX_ROW_VPOS (r0, current_matrix),
11114 MATRIX_ROW_VPOS (r1, current_matrix),
11115 delta, delta_bytes);
11116 }
11117
11118#if 0 /* If changes are all in front of the window start, the
11119 distance of the last displayed glyph from Z hasn't
11120 changed. */
5f5c8ee5
GM
11121 w->window_end_pos
11122 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11123 w->window_end_bytepos
6fc556fd 11124 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
f2d86d7a
GM
11125#endif
11126
e5959a9a
GM
11127 row = row_containing_pos (w, PT, r0, NULL);
11128 if (row == NULL)
11129 return 0;
11130
11131 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
11132 return 1;
11133 }
11134
11135 /* Return quickly if changes are all below what is displayed in the
11136 window, and if PT is in the window. */
9142dd5b 11137 if (BEG_UNCHANGED > MATRIX_ROW_END_CHARPOS (row)
5f5c8ee5
GM
11138 && PT < MATRIX_ROW_END_CHARPOS (row))
11139 {
11140 /* We have to update window end positions because the buffer's
11141 size has changed. */
11142 w->window_end_pos
11143 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
11144 w->window_end_bytepos
6fc556fd 11145 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ef121659
GM
11146
11147 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11148 row = row_containing_pos (w, PT, row, NULL);
11149 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11150 return 2;
5f5c8ee5
GM
11151 }
11152
11153 /* Check that window start agrees with the start of the first glyph
11154 row in its current matrix. Check this after we know the window
11155 start is not in changed text, otherwise positions would not be
11156 comparable. */
11157 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
11158 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
11159 return 0;
11160
5f5c8ee5
GM
11161 /* Compute the position at which we have to start displaying new
11162 lines. Some of the lines at the top of the window might be
11163 reusable because they are not displaying changed text. Find the
11164 last row in W's current matrix not affected by changes at the
11165 start of current_buffer. Value is null if changes start in the
11166 first line of window. */
1ec185cb 11167 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
5f5c8ee5
GM
11168 if (last_unchanged_at_beg_row)
11169 {
67f1cf4c
GM
11170 /* Avoid starting to display in the moddle of a character, a TAB
11171 for instance. This is easier than to set up the iterator
11172 exactly, and it's not a frequent case, so the additional
11173 effort wouldn't really pay off. */
11174 while (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
11175 && last_unchanged_at_beg_row > w->current_matrix->rows)
11176 --last_unchanged_at_beg_row;
11177
11178 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
11179 return 0;
11180
5f5c8ee5
GM
11181 init_to_row_end (&it, w, last_unchanged_at_beg_row);
11182 start_pos = it.current.pos;
11183
11184 /* Start displaying new lines in the desired matrix at the same
11185 vpos we would use in the current matrix, i.e. below
11186 last_unchanged_at_beg_row. */
11187 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
11188 current_matrix);
11189 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11190 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
11191
11192 xassert (it.hpos == 0 && it.current_x == 0);
11193 }
11194 else
11195 {
11196 /* There are no reusable lines at the start of the window.
11197 Start displaying in the first line. */
11198 start_display (&it, w, start);
11199 start_pos = it.current.pos;
11200 }
11201
5f5c8ee5
GM
11202 /* Find the first row that is not affected by changes at the end of
11203 the buffer. Value will be null if there is no unchanged row, in
11204 which case we must redisplay to the end of the window. delta
11205 will be set to the value by which buffer positions beginning with
11206 first_unchanged_at_end_row have to be adjusted due to text
11207 changes. */
11208 first_unchanged_at_end_row
1ec185cb 11209 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
5f5c8ee5
GM
11210 IF_DEBUG (debug_delta = delta);
11211 IF_DEBUG (debug_delta_bytes = delta_bytes);
11212
11213 /* Set stop_pos to the buffer position up to which we will have to
11214 display new lines. If first_unchanged_at_end_row != NULL, this
11215 is the buffer position of the start of the line displayed in that
11216 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
11217 that we don't stop at a buffer position. */
11218 stop_pos = 0;
11219 if (first_unchanged_at_end_row)
11220 {
11221 xassert (last_unchanged_at_beg_row == NULL
11222 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
11223
11224 /* If this is a continuation line, move forward to the next one
11225 that isn't. Changes in lines above affect this line.
11226 Caution: this may move first_unchanged_at_end_row to a row
11227 not displaying text. */
11228 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
11229 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11230 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11231 < it.last_visible_y))
11232 ++first_unchanged_at_end_row;
11233
11234 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
11235 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
11236 >= it.last_visible_y))
11237 first_unchanged_at_end_row = NULL;
11238 else
11239 {
11240 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
11241 + delta);
11242 first_unchanged_at_end_vpos
11243 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
9142dd5b 11244 xassert (stop_pos >= Z - END_UNCHANGED);
5f5c8ee5
GM
11245 }
11246 }
11247 else if (last_unchanged_at_beg_row == NULL)
11248 return 0;
11249
11250
11251#if GLYPH_DEBUG
11252
11253 /* Either there is no unchanged row at the end, or the one we have
11254 now displays text. This is a necessary condition for the window
11255 end pos calculation at the end of this function. */
11256 xassert (first_unchanged_at_end_row == NULL
11257 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
11258
11259 debug_last_unchanged_at_beg_vpos
11260 = (last_unchanged_at_beg_row
11261 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
11262 : -1);
11263 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
11264
11265#endif /* GLYPH_DEBUG != 0 */
11266
11267
11268 /* Display new lines. Set last_text_row to the last new line
11269 displayed which has text on it, i.e. might end up as being the
11270 line where the window_end_vpos is. */
11271 w->cursor.vpos = -1;
11272 last_text_row = NULL;
11273 overlay_arrow_seen = 0;
11274 while (it.current_y < it.last_visible_y
11275 && !fonts_changed_p
11276 && (first_unchanged_at_end_row == NULL
11277 || IT_CHARPOS (it) < stop_pos))
11278 {
11279 if (display_line (&it))
11280 last_text_row = it.glyph_row - 1;
11281 }
11282
11283 if (fonts_changed_p)
11284 return -1;
11285
11286
11287 /* Compute differences in buffer positions, y-positions etc. for
11288 lines reused at the bottom of the window. Compute what we can
11289 scroll. */
ca42b2e8
GM
11290 if (first_unchanged_at_end_row
11291 /* No lines reused because we displayed everything up to the
11292 bottom of the window. */
11293 && it.current_y < it.last_visible_y)
5f5c8ee5
GM
11294 {
11295 dvpos = (it.vpos
11296 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
11297 current_matrix));
11298 dy = it.current_y - first_unchanged_at_end_row->y;
11299 run.current_y = first_unchanged_at_end_row->y;
11300 run.desired_y = run.current_y + dy;
11301 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
11302 }
11303 else
ca42b2e8
GM
11304 {
11305 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
11306 first_unchanged_at_end_row = NULL;
11307 }
5f5c8ee5
GM
11308 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
11309
8f8ba186 11310
5f5c8ee5
GM
11311 /* Find the cursor if not already found. We have to decide whether
11312 PT will appear on this window (it sometimes doesn't, but this is
11313 not a very frequent case.) This decision has to be made before
11314 the current matrix is altered. A value of cursor.vpos < 0 means
11315 that PT is either in one of the lines beginning at
11316 first_unchanged_at_end_row or below the window. Don't care for
11317 lines that might be displayed later at the window end; as
11318 mentioned, this is not a frequent case. */
11319 if (w->cursor.vpos < 0)
11320 {
5f5c8ee5
GM
11321 /* Cursor in unchanged rows at the top? */
11322 if (PT < CHARPOS (start_pos)
11323 && last_unchanged_at_beg_row)
11324 {
e037b9ec
GM
11325 row = row_containing_pos (w, PT,
11326 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
11327 last_unchanged_at_beg_row + 1);
bfe0ee88
GM
11328 if (row)
11329 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
11330 }
11331
11332 /* Start from first_unchanged_at_end_row looking for PT. */
11333 else if (first_unchanged_at_end_row)
11334 {
e037b9ec
GM
11335 row = row_containing_pos (w, PT - delta,
11336 first_unchanged_at_end_row, NULL);
11337 if (row)
468155d7
GM
11338 set_cursor_from_row (w, row, w->current_matrix, delta,
11339 delta_bytes, dy, dvpos);
5f5c8ee5
GM
11340 }
11341
11342 /* Give up if cursor was not found. */
11343 if (w->cursor.vpos < 0)
11344 {
11345 clear_glyph_matrix (w->desired_matrix);
11346 return -1;
11347 }
11348 }
11349
11350 /* Don't let the cursor end in the scroll margins. */
11351 {
11352 int this_scroll_margin, cursor_height;
11353
11354 this_scroll_margin = max (0, scroll_margin);
11355 this_scroll_margin = min (this_scroll_margin,
11356 XFASTINT (w->height) / 4);
11357 this_scroll_margin *= CANON_Y_UNIT (it.f);
11358 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
11359
11360 if ((w->cursor.y < this_scroll_margin
11361 && CHARPOS (start) > BEGV)
11362 /* Don't take scroll margin into account at the bottom because
11363 old redisplay didn't do it either. */
11364 || w->cursor.y + cursor_height > it.last_visible_y)
11365 {
11366 w->cursor.vpos = -1;
11367 clear_glyph_matrix (w->desired_matrix);
11368 return -1;
11369 }
11370 }
11371
11372 /* Scroll the display. Do it before changing the current matrix so
11373 that xterm.c doesn't get confused about where the cursor glyph is
11374 found. */
fa77249f 11375 if (dy && run.height)
5f5c8ee5
GM
11376 {
11377 update_begin (f);
11378
11379 if (FRAME_WINDOW_P (f))
11380 {
11381 rif->update_window_begin_hook (w);
64d1e7d3 11382 rif->clear_mouse_face (w);
5f5c8ee5 11383 rif->scroll_run_hook (w, &run);
64d1e7d3 11384 rif->update_window_end_hook (w, 0, 0);
5f5c8ee5
GM
11385 }
11386 else
11387 {
11388 /* Terminal frame. In this case, dvpos gives the number of
11389 lines to scroll by; dvpos < 0 means scroll up. */
11390 int first_unchanged_at_end_vpos
11391 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
11392 int from = XFASTINT (w->top) + first_unchanged_at_end_vpos;
11393 int end = XFASTINT (w->top) + window_internal_height (w);
11394
11395 /* Perform the operation on the screen. */
11396 if (dvpos > 0)
11397 {
11398 /* Scroll last_unchanged_at_beg_row to the end of the
11399 window down dvpos lines. */
11400 set_terminal_window (end);
11401
11402 /* On dumb terminals delete dvpos lines at the end
11403 before inserting dvpos empty lines. */
11404 if (!scroll_region_ok)
11405 ins_del_lines (end - dvpos, -dvpos);
11406
11407 /* Insert dvpos empty lines in front of
11408 last_unchanged_at_beg_row. */
11409 ins_del_lines (from, dvpos);
11410 }
11411 else if (dvpos < 0)
11412 {
11413 /* Scroll up last_unchanged_at_beg_vpos to the end of
11414 the window to last_unchanged_at_beg_vpos - |dvpos|. */
11415 set_terminal_window (end);
11416
11417 /* Delete dvpos lines in front of
11418 last_unchanged_at_beg_vpos. ins_del_lines will set
11419 the cursor to the given vpos and emit |dvpos| delete
11420 line sequences. */
11421 ins_del_lines (from + dvpos, dvpos);
11422
11423 /* On a dumb terminal insert dvpos empty lines at the
11424 end. */
11425 if (!scroll_region_ok)
11426 ins_del_lines (end + dvpos, -dvpos);
11427 }
11428
11429 set_terminal_window (0);
11430 }
11431
11432 update_end (f);
11433 }
11434
ca42b2e8
GM
11435 /* Shift reused rows of the current matrix to the right position.
11436 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
11437 text. */
11438 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
11439 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
5f5c8ee5
GM
11440 if (dvpos < 0)
11441 {
11442 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
11443 bottom_vpos, dvpos);
11444 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
11445 bottom_vpos, 0);
11446 }
11447 else if (dvpos > 0)
11448 {
11449 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
11450 bottom_vpos, dvpos);
11451 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
11452 first_unchanged_at_end_vpos + dvpos, 0);
11453 }
11454
11455 /* For frame-based redisplay, make sure that current frame and window
11456 matrix are in sync with respect to glyph memory. */
11457 if (!FRAME_WINDOW_P (f))
11458 sync_frame_with_window_matrix_rows (w);
11459
11460 /* Adjust buffer positions in reused rows. */
11461 if (delta)
f2d86d7a
GM
11462 increment_matrix_positions (current_matrix,
11463 first_unchanged_at_end_vpos + dvpos,
11464 bottom_vpos, delta, delta_bytes);
5f5c8ee5
GM
11465
11466 /* Adjust Y positions. */
11467 if (dy)
11468 shift_glyph_matrix (w, current_matrix,
11469 first_unchanged_at_end_vpos + dvpos,
11470 bottom_vpos, dy);
11471
11472 if (first_unchanged_at_end_row)
11473 first_unchanged_at_end_row += dvpos;
11474
11475 /* If scrolling up, there may be some lines to display at the end of
11476 the window. */
11477 last_text_row_at_end = NULL;
11478 if (dy < 0)
11479 {
11480 /* Set last_row to the glyph row in the current matrix where the
11481 window end line is found. It has been moved up or down in
11482 the matrix by dvpos. */
11483 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
11484 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
11485
11486 /* If last_row is the window end line, it should display text. */
11487 xassert (last_row->displays_text_p);
11488
11489 /* If window end line was partially visible before, begin
11490 displaying at that line. Otherwise begin displaying with the
11491 line following it. */
11492 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
11493 {
11494 init_to_row_start (&it, w, last_row);
11495 it.vpos = last_vpos;
11496 it.current_y = last_row->y;
11497 }
11498 else
11499 {
11500 init_to_row_end (&it, w, last_row);
11501 it.vpos = 1 + last_vpos;
11502 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
11503 ++last_row;
11504 }
12adba34 11505
5f5c8ee5
GM
11506 /* We may start in a continuation line. If so, we have to get
11507 the right continuation_lines_width and current_x. */
11508 it.continuation_lines_width = last_row->continuation_lines_width;
11509 it.hpos = it.current_x = 0;
11510
11511 /* Display the rest of the lines at the window end. */
11512 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
11513 while (it.current_y < it.last_visible_y
11514 && !fonts_changed_p)
11515 {
11516 /* Is it always sure that the display agrees with lines in
11517 the current matrix? I don't think so, so we mark rows
11518 displayed invalid in the current matrix by setting their
11519 enabled_p flag to zero. */
11520 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
11521 if (display_line (&it))
11522 last_text_row_at_end = it.glyph_row - 1;
11523 }
11524 }
12adba34 11525
5f5c8ee5
GM
11526 /* Update window_end_pos and window_end_vpos. */
11527 if (first_unchanged_at_end_row
11528 && first_unchanged_at_end_row->y < it.last_visible_y
11529 && !last_text_row_at_end)
11530 {
11531 /* Window end line if one of the preserved rows from the current
11532 matrix. Set row to the last row displaying text in current
11533 matrix starting at first_unchanged_at_end_row, after
11534 scrolling. */
11535 xassert (first_unchanged_at_end_row->displays_text_p);
11536 row = find_last_row_displaying_text (w->current_matrix, &it,
11537 first_unchanged_at_end_row);
11538 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
11539
ac90c44f 11540 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
5f5c8ee5 11541 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
ac90c44f
GM
11542 w->window_end_vpos
11543 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
5f5c8ee5
GM
11544 }
11545 else if (last_text_row_at_end)
11546 {
ac90c44f
GM
11547 w->window_end_pos
11548 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
5f5c8ee5
GM
11549 w->window_end_bytepos
11550 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
ac90c44f
GM
11551 w->window_end_vpos
11552 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
5f5c8ee5
GM
11553 }
11554 else if (last_text_row)
11555 {
11556 /* We have displayed either to the end of the window or at the
11557 end of the window, i.e. the last row with text is to be found
11558 in the desired matrix. */
ac90c44f
GM
11559 w->window_end_pos
11560 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
5f5c8ee5
GM
11561 w->window_end_bytepos
11562 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
ac90c44f
GM
11563 w->window_end_vpos
11564 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
5f5c8ee5
GM
11565 }
11566 else if (first_unchanged_at_end_row == NULL
11567 && last_text_row == NULL
11568 && last_text_row_at_end == NULL)
11569 {
11570 /* Displayed to end of window, but no line containing text was
11571 displayed. Lines were deleted at the end of the window. */
11572 int vpos;
045dee35 11573 int header_line_p = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5f5c8ee5
GM
11574
11575 for (vpos = XFASTINT (w->window_end_vpos); vpos > 0; --vpos)
045dee35
GM
11576 if ((w->desired_matrix->rows[vpos + header_line_p].enabled_p
11577 && w->desired_matrix->rows[vpos + header_line_p].displays_text_p)
11578 || (!w->desired_matrix->rows[vpos + header_line_p].enabled_p
11579 && w->current_matrix->rows[vpos + header_line_p].displays_text_p))
5f5c8ee5 11580 break;
12adba34 11581
5f5c8ee5
GM
11582 w->window_end_vpos = make_number (vpos);
11583 }
11584 else
11585 abort ();
ac90c44f 11586
8cdb267e
GM
11587#if 0 /* This leads to problems, for instance when the cursor is
11588 at ZV, and the cursor line displays no text. */
ac90c44f
GM
11589 /* Disable rows below what's displayed in the window. This makes
11590 debugging easier. */
11591 enable_glyph_matrix_rows (current_matrix,
11592 XFASTINT (w->window_end_vpos) + 1,
11593 bottom_vpos, 0);
8cdb267e 11594#endif
5f5c8ee5
GM
11595
11596 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
11597 debug_end_vpos = XFASTINT (w->window_end_vpos));
12adba34 11598
5f5c8ee5
GM
11599 /* Record that display has not been completed. */
11600 w->window_end_valid = Qnil;
11601 w->desired_matrix->no_scrolling_p = 1;
ef121659 11602 return 3;
12adba34 11603}
0f9c0ff0 11604
a2889657 11605
5f5c8ee5
GM
11606\f
11607/***********************************************************************
11608 More debugging support
11609 ***********************************************************************/
a2889657 11610
5f5c8ee5 11611#if GLYPH_DEBUG
a2889657 11612
9ab436b9
GM
11613void dump_glyph_row P_ ((struct glyph_matrix *, int, int));
11614void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
e187cf71 11615void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
1c9241f5 11616
31b24551 11617
9ab436b9 11618/* Dump the contents of glyph matrix MATRIX on stderr.
ca26e1c8 11619
9ab436b9
GM
11620 GLYPHS 0 means don't show glyph contents.
11621 GLYPHS 1 means show glyphs in short form
11622 GLYPHS > 1 means show glyphs in long form. */
11623
11624void
11625dump_glyph_matrix (matrix, glyphs)
5f5c8ee5 11626 struct glyph_matrix *matrix;
9ab436b9 11627 int glyphs;
5f5c8ee5 11628{
efc63ef0 11629 int i;
5f5c8ee5 11630 for (i = 0; i < matrix->nrows; ++i)
9ab436b9 11631 dump_glyph_row (matrix, i, glyphs);
5f5c8ee5 11632}
31b24551 11633
68a37fa8 11634
e187cf71
GM
11635/* Dump contents of glyph GLYPH to stderr. ROW and AREA are
11636 the glyph row and area where the glyph comes from. */
11637
11638void
11639dump_glyph (row, glyph, area)
11640 struct glyph_row *row;
11641 struct glyph *glyph;
11642 int area;
11643{
11644 if (glyph->type == CHAR_GLYPH)
11645 {
11646 fprintf (stderr,
11647 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11648 glyph - row->glyphs[TEXT_AREA],
11649 'C',
11650 glyph->charpos,
11651 (BUFFERP (glyph->object)
11652 ? 'B'
11653 : (STRINGP (glyph->object)
11654 ? 'S'
11655 : '-')),
11656 glyph->pixel_width,
11657 glyph->u.ch,
11658 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
11659 ? glyph->u.ch
11660 : '.'),
11661 glyph->face_id,
11662 glyph->left_box_line_p,
11663 glyph->right_box_line_p);
11664 }
11665 else if (glyph->type == STRETCH_GLYPH)
11666 {
11667 fprintf (stderr,
11668 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11669 glyph - row->glyphs[TEXT_AREA],
11670 'S',
11671 glyph->charpos,
11672 (BUFFERP (glyph->object)
11673 ? 'B'
11674 : (STRINGP (glyph->object)
11675 ? 'S'
11676 : '-')),
11677 glyph->pixel_width,
11678 0,
11679 '.',
11680 glyph->face_id,
11681 glyph->left_box_line_p,
11682 glyph->right_box_line_p);
11683 }
11684 else if (glyph->type == IMAGE_GLYPH)
11685 {
11686 fprintf (stderr,
11687 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
11688 glyph - row->glyphs[TEXT_AREA],
11689 'I',
11690 glyph->charpos,
11691 (BUFFERP (glyph->object)
11692 ? 'B'
11693 : (STRINGP (glyph->object)
11694 ? 'S'
11695 : '-')),
11696 glyph->pixel_width,
11697 glyph->u.img_id,
11698 '.',
11699 glyph->face_id,
11700 glyph->left_box_line_p,
11701 glyph->right_box_line_p);
11702 }
11703}
11704
11705
5f5c8ee5 11706/* Dump the contents of glyph row at VPOS in MATRIX to stderr.
9ab436b9
GM
11707 GLYPHS 0 means don't show glyph contents.
11708 GLYPHS 1 means show glyphs in short form
11709 GLYPHS > 1 means show glyphs in long form. */
a2889657 11710
5f5c8ee5 11711void
9ab436b9 11712dump_glyph_row (matrix, vpos, glyphs)
5f5c8ee5 11713 struct glyph_matrix *matrix;
9ab436b9 11714 int vpos, glyphs;
5f5c8ee5
GM
11715{
11716 struct glyph_row *row;
11717
11718 if (vpos < 0 || vpos >= matrix->nrows)
11719 return;
11720
11721 row = MATRIX_ROW (matrix, vpos);
9ab436b9
GM
11722
11723 if (glyphs != 1)
11724 {
b94c0d9c 11725 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
9ab436b9 11726 fprintf (stderr, "=======================================================================\n");
5f5c8ee5 11727
9ab436b9 11728 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d\
b94c0d9c 11729%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
11730 row - matrix->rows,
11731 MATRIX_ROW_START_CHARPOS (row),
11732 MATRIX_ROW_END_CHARPOS (row),
11733 row->used[TEXT_AREA],
11734 row->contains_overlapping_glyphs_p,
11735 row->enabled_p,
11736 row->inverse_p,
11737 row->truncated_on_left_p,
11738 row->truncated_on_right_p,
11739 row->overlay_arrow_p,
11740 row->continued_p,
11741 MATRIX_ROW_CONTINUATION_LINE_P (row),
11742 row->displays_text_p,
11743 row->ends_at_zv_p,
11744 row->fill_line_p,
11745 row->ends_in_middle_of_char_p,
11746 row->starts_in_middle_of_char_p,
b94c0d9c 11747 row->mouse_face_p,
9ab436b9
GM
11748 row->x,
11749 row->y,
11750 row->pixel_width,
11751 row->height,
11752 row->visible_height,
11753 row->ascent,
11754 row->phys_ascent);
11755 fprintf (stderr, "%9d %5d\n", row->start.overlay_string_index,
11756 row->end.overlay_string_index);
11757 fprintf (stderr, "%9d %5d\n",
11758 CHARPOS (row->start.string_pos),
11759 CHARPOS (row->end.string_pos));
11760 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
11761 row->end.dpvec_index);
11762 }
5f5c8ee5 11763
9ab436b9 11764 if (glyphs > 1)
bd66d1ba 11765 {
e187cf71
GM
11766 int area;
11767
11768 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
11769 {
091f8878
GM
11770 struct glyph *glyph = row->glyphs[area];
11771 struct glyph *glyph_end = glyph + row->used[area];
5f5c8ee5 11772
e187cf71 11773 /* Glyph for a line end in text. */
091f8878 11774 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
e187cf71 11775 ++glyph_end;
5f5c8ee5 11776
e187cf71
GM
11777 if (glyph < glyph_end)
11778 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
5f5c8ee5 11779
e187cf71
GM
11780 for (; glyph < glyph_end; ++glyph)
11781 dump_glyph (row, glyph, area);
f7b4b63a 11782 }
f4faa47c 11783 }
9ab436b9
GM
11784 else if (glyphs == 1)
11785 {
e187cf71 11786 int area;
9ab436b9 11787
e187cf71 11788 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
9ab436b9 11789 {
e187cf71
GM
11790 char *s = (char *) alloca (row->used[area] + 1);
11791 int i;
11792
11793 for (i = 0; i < row->used[area]; ++i)
11794 {
11795 struct glyph *glyph = row->glyphs[area] + i;
11796 if (glyph->type == CHAR_GLYPH
11797 && glyph->u.ch < 0x80
11798 && glyph->u.ch >= ' ')
11799 s[i] = glyph->u.ch;
11800 else
11801 s[i] = '.';
11802 }
9ab436b9 11803
e187cf71
GM
11804 s[i] = '\0';
11805 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
11806 }
9ab436b9 11807 }
5f5c8ee5 11808}
f4faa47c 11809
a2889657 11810
5f5c8ee5
GM
11811DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
11812 Sdump_glyph_matrix, 0, 1, "p",
11813 "Dump the current matrix of the selected window to stderr.\n\
9ab436b9
GM
11814Shows contents of glyph row structures. With non-nil\n\
11815parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show\n\
11816glyphs in short form, otherwise show glyphs in long form.")
11817 (glyphs)
11818 Lisp_Object glyphs;
5f5c8ee5
GM
11819{
11820 struct window *w = XWINDOW (selected_window);
11821 struct buffer *buffer = XBUFFER (w->buffer);
11822
11823 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
11824 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
11825 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
11826 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
11827 fprintf (stderr, "=============================================\n");
9ab436b9
GM
11828 dump_glyph_matrix (w->current_matrix,
11829 NILP (glyphs) ? 0 : XINT (glyphs));
5f5c8ee5
GM
11830 return Qnil;
11831}
1c2250c2 11832
1fca3fae 11833
e9abc296
GM
11834DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
11835 "Dump glyph row ROW to stderr.\n\
11836GLYPH 0 means don't dump glyphs.\n\
11837GLYPH 1 means dump glyphs in short form.\n\
7ef18b28 11838GLYPH > 1 or omitted means dump glyphs in long form.")
e9abc296
GM
11839 (row, glyphs)
11840 Lisp_Object row, glyphs;
5f5c8ee5
GM
11841{
11842 CHECK_NUMBER (row, 0);
e9abc296
GM
11843 dump_glyph_row (XWINDOW (selected_window)->current_matrix,
11844 XINT (row),
11845 INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
11846 return Qnil;
11847}
1fca3fae 11848
67481ae5 11849
b94c0d9c
GM
11850DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
11851 "Dump glyph row ROW of the tool-bar of the current frame to stderr.\n\
11852GLYPH 0 means don't dump glyphs.\n\
11853GLYPH 1 means dump glyphs in short form.\n\
11854GLYPH > 1 or omitted means dump glyphs in long form.")
11855 (row, glyphs)
11856 Lisp_Object row, glyphs;
5f5c8ee5 11857{
886bd6f2 11858 struct frame *sf = SELECTED_FRAME ();
b94c0d9c
GM
11859 struct glyph_matrix *m = (XWINDOW (sf->tool_bar_window)->current_matrix);
11860 dump_glyph_row (m, XINT (row), INTEGERP (glyphs) ? XINT (glyphs) : 2);
5f5c8ee5
GM
11861 return Qnil;
11862}
ca26e1c8 11863
0f9c0ff0 11864
5f5c8ee5
GM
11865DEFUN ("trace-redisplay-toggle", Ftrace_redisplay_toggle,
11866 Strace_redisplay_toggle, 0, 0, "",
11867 "Toggle tracing of redisplay.")
11868 ()
11869{
11870 trace_redisplay_p = !trace_redisplay_p;
11871 return Qnil;
11872}
bf9249e3
GM
11873
11874
11875DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, 1, "",
11876 "Print STRING to stderr.")
11877 (string)
11878 Lisp_Object string;
11879{
11880 CHECK_STRING (string, 0);
11881 fprintf (stderr, "%s", XSTRING (string)->data);
11882 return Qnil;
11883}
5f5c8ee5
GM
11884
11885#endif /* GLYPH_DEBUG */
ca26e1c8 11886
ca26e1c8 11887
5f5c8ee5
GM
11888\f
11889/***********************************************************************
11890 Building Desired Matrix Rows
11891 ***********************************************************************/
a2889657 11892
5f5c8ee5
GM
11893/* Return a temporary glyph row holding the glyphs of an overlay
11894 arrow. Only used for non-window-redisplay windows. */
ca26e1c8 11895
5f5c8ee5
GM
11896static struct glyph_row *
11897get_overlay_arrow_glyph_row (w)
11898 struct window *w;
11899{
11900 struct frame *f = XFRAME (WINDOW_FRAME (w));
11901 struct buffer *buffer = XBUFFER (w->buffer);
11902 struct buffer *old = current_buffer;
11903 unsigned char *arrow_string = XSTRING (Voverlay_arrow_string)->data;
11904 int arrow_len = XSTRING (Voverlay_arrow_string)->size;
11905 unsigned char *arrow_end = arrow_string + arrow_len;
11906 unsigned char *p;
11907 struct it it;
11908 int multibyte_p;
11909 int n_glyphs_before;
11910
11911 set_buffer_temp (buffer);
11912 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
11913 it.glyph_row->used[TEXT_AREA] = 0;
11914 SET_TEXT_POS (it.position, 0, 0);
11915
11916 multibyte_p = !NILP (buffer->enable_multibyte_characters);
11917 p = arrow_string;
11918 while (p < arrow_end)
11919 {
11920 Lisp_Object face, ilisp;
11921
11922 /* Get the next character. */
11923 if (multibyte_p)
4fdb80f2 11924 it.c = string_char_and_length (p, arrow_len, &it.len);
5f5c8ee5
GM
11925 else
11926 it.c = *p, it.len = 1;
11927 p += it.len;
11928
11929 /* Get its face. */
ac90c44f 11930 ilisp = make_number (p - arrow_string);
5f5c8ee5
GM
11931 face = Fget_text_property (ilisp, Qface, Voverlay_arrow_string);
11932 it.face_id = compute_char_face (f, it.c, face);
11933
11934 /* Compute its width, get its glyphs. */
11935 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
337042a9 11936 SET_TEXT_POS (it.position, -1, -1);
5f5c8ee5
GM
11937 PRODUCE_GLYPHS (&it);
11938
11939 /* If this character doesn't fit any more in the line, we have
11940 to remove some glyphs. */
11941 if (it.current_x > it.last_visible_x)
11942 {
11943 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
11944 break;
11945 }
11946 }
11947
11948 set_buffer_temp (old);
11949 return it.glyph_row;
11950}
ca26e1c8 11951
b0a0fbda 11952
5f5c8ee5
GM
11953/* Insert truncation glyphs at the start of IT->glyph_row. Truncation
11954 glyphs are only inserted for terminal frames since we can't really
11955 win with truncation glyphs when partially visible glyphs are
11956 involved. Which glyphs to insert is determined by
11957 produce_special_glyphs. */
67481ae5 11958
5f5c8ee5
GM
11959static void
11960insert_left_trunc_glyphs (it)
11961 struct it *it;
11962{
11963 struct it truncate_it;
11964 struct glyph *from, *end, *to, *toend;
11965
11966 xassert (!FRAME_WINDOW_P (it->f));
11967
11968 /* Get the truncation glyphs. */
11969 truncate_it = *it;
5f5c8ee5
GM
11970 truncate_it.current_x = 0;
11971 truncate_it.face_id = DEFAULT_FACE_ID;
11972 truncate_it.glyph_row = &scratch_glyph_row;
11973 truncate_it.glyph_row->used[TEXT_AREA] = 0;
11974 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
6fc556fd 11975 truncate_it.object = make_number (0);
5f5c8ee5
GM
11976 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
11977
11978 /* Overwrite glyphs from IT with truncation glyphs. */
11979 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11980 end = from + truncate_it.glyph_row->used[TEXT_AREA];
11981 to = it->glyph_row->glyphs[TEXT_AREA];
11982 toend = to + it->glyph_row->used[TEXT_AREA];
11983
11984 while (from < end)
11985 *to++ = *from++;
11986
37be86f2
KH
11987 /* There may be padding glyphs left over. Overwrite them too. */
11988 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
11989 {
11990 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
11991 while (from < end)
11992 *to++ = *from++;
11993 }
5f5c8ee5 11994
37be86f2
KH
11995 if (to > toend)
11996 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
5f5c8ee5 11997}
e0bfbde6 11998
e0bfbde6 11999
5f5c8ee5 12000/* Compute the pixel height and width of IT->glyph_row.
9c49d3d7 12001
5f5c8ee5
GM
12002 Most of the time, ascent and height of a display line will be equal
12003 to the max_ascent and max_height values of the display iterator
12004 structure. This is not the case if
67481ae5 12005
5f5c8ee5
GM
12006 1. We hit ZV without displaying anything. In this case, max_ascent
12007 and max_height will be zero.
1c9241f5 12008
5f5c8ee5
GM
12009 2. We have some glyphs that don't contribute to the line height.
12010 (The glyph row flag contributes_to_line_height_p is for future
12011 pixmap extensions).
f6fd109b 12012
5f5c8ee5
GM
12013 The first case is easily covered by using default values because in
12014 these cases, the line height does not really matter, except that it
12015 must not be zero. */
67481ae5 12016
5f5c8ee5
GM
12017static void
12018compute_line_metrics (it)
12019 struct it *it;
12020{
12021 struct glyph_row *row = it->glyph_row;
12022 int area, i;
1c2250c2 12023
5f5c8ee5
GM
12024 if (FRAME_WINDOW_P (it->f))
12025 {
045dee35 12026 int i, header_line_height;
1c2250c2 12027
5f5c8ee5
GM
12028 /* The line may consist of one space only, that was added to
12029 place the cursor on it. If so, the row's height hasn't been
12030 computed yet. */
12031 if (row->height == 0)
12032 {
12033 if (it->max_ascent + it->max_descent == 0)
312246d1 12034 it->max_descent = it->max_phys_descent = CANON_Y_UNIT (it->f);
5f5c8ee5
GM
12035 row->ascent = it->max_ascent;
12036 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12037 row->phys_ascent = it->max_phys_ascent;
12038 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12039 }
12040
12041 /* Compute the width of this line. */
12042 row->pixel_width = row->x;
12043 for (i = 0; i < row->used[TEXT_AREA]; ++i)
12044 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
12045
12046 xassert (row->pixel_width >= 0);
12047 xassert (row->ascent >= 0 && row->height > 0);
12048
312246d1
GM
12049 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
12050 || MATRIX_ROW_OVERLAPS_PRED_P (row));
12051
12052 /* If first line's physical ascent is larger than its logical
12053 ascent, use the physical ascent, and make the row taller.
12054 This makes accented characters fully visible. */
b28cb6ed 12055 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
312246d1
GM
12056 && row->phys_ascent > row->ascent)
12057 {
12058 row->height += row->phys_ascent - row->ascent;
12059 row->ascent = row->phys_ascent;
12060 }
12061
5f5c8ee5
GM
12062 /* Compute how much of the line is visible. */
12063 row->visible_height = row->height;
12064
045dee35
GM
12065 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (it->w);
12066 if (row->y < header_line_height)
12067 row->visible_height -= header_line_height - row->y;
5f5c8ee5
GM
12068 else
12069 {
12070 int max_y = WINDOW_DISPLAY_HEIGHT_NO_MODE_LINE (it->w);
12071 if (row->y + row->height > max_y)
12072 row->visible_height -= row->y + row->height - max_y;
12073 }
12074 }
12075 else
12076 {
12077 row->pixel_width = row->used[TEXT_AREA];
312246d1
GM
12078 row->ascent = row->phys_ascent = 0;
12079 row->height = row->phys_height = row->visible_height = 1;
5f5c8ee5 12080 }
67481ae5 12081
5f5c8ee5
GM
12082 /* Compute a hash code for this row. */
12083 row->hash = 0;
12084 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
12085 for (i = 0; i < row->used[area]; ++i)
12086 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
12087 + row->glyphs[area][i].u.val
43d120d8
KH
12088 + row->glyphs[area][i].face_id
12089 + row->glyphs[area][i].padding_p
5f5c8ee5 12090 + (row->glyphs[area][i].type << 2));
a2889657 12091
5f5c8ee5 12092 it->max_ascent = it->max_descent = 0;
312246d1 12093 it->max_phys_ascent = it->max_phys_descent = 0;
5f5c8ee5 12094}
12adba34 12095
ca26e1c8 12096
5f5c8ee5
GM
12097/* Append one space to the glyph row of iterator IT if doing a
12098 window-based redisplay. DEFAULT_FACE_P non-zero means let the
12099 space have the default face, otherwise let it have the same face as
80c6cb1f 12100 IT->face_id. Value is non-zero if a space was added.
c6e89d6c
GM
12101
12102 This function is called to make sure that there is always one glyph
12103 at the end of a glyph row that the cursor can be set on under
12104 window-systems. (If there weren't such a glyph we would not know
12105 how wide and tall a box cursor should be displayed).
12106
12107 At the same time this space let's a nicely handle clearing to the
12108 end of the line if the row ends in italic text. */
ca26e1c8 12109
80c6cb1f 12110static int
5f5c8ee5
GM
12111append_space (it, default_face_p)
12112 struct it *it;
12113 int default_face_p;
12114{
12115 if (FRAME_WINDOW_P (it->f))
12116 {
12117 int n = it->glyph_row->used[TEXT_AREA];
ca26e1c8 12118
5f5c8ee5
GM
12119 if (it->glyph_row->glyphs[TEXT_AREA] + n
12120 < it->glyph_row->glyphs[1 + TEXT_AREA])
a2889657 12121 {
cafafe0b
GM
12122 /* Save some values that must not be changed.
12123 Must save IT->c and IT->len because otherwise
12124 ITERATOR_AT_END_P wouldn't work anymore after
12125 append_space has been called. */
478d746b 12126 enum display_element_type saved_what = it->what;
cafafe0b
GM
12127 int saved_c = it->c, saved_len = it->len;
12128 int saved_x = it->current_x;
5f5c8ee5 12129 int saved_face_id = it->face_id;
cafafe0b 12130 struct text_pos saved_pos;
5f5c8ee5 12131 Lisp_Object saved_object;
980806b6 12132 struct face *face;
5f5c8ee5
GM
12133
12134 saved_object = it->object;
12135 saved_pos = it->position;
12136
12137 it->what = IT_CHARACTER;
12138 bzero (&it->position, sizeof it->position);
6fc556fd 12139 it->object = make_number (0);
5f5c8ee5
GM
12140 it->c = ' ';
12141 it->len = 1;
5f5c8ee5
GM
12142
12143 if (default_face_p)
12144 it->face_id = DEFAULT_FACE_ID;
4aad61f8
GM
12145 else if (it->face_before_selective_p)
12146 it->face_id = it->saved_face_id;
980806b6
KH
12147 face = FACE_FROM_ID (it->f, it->face_id);
12148 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
1842fc1a 12149
5f5c8ee5
GM
12150 PRODUCE_GLYPHS (it);
12151
12152 it->current_x = saved_x;
12153 it->object = saved_object;
12154 it->position = saved_pos;
12155 it->what = saved_what;
12156 it->face_id = saved_face_id;
cafafe0b
GM
12157 it->len = saved_len;
12158 it->c = saved_c;
80c6cb1f 12159 return 1;
5f5c8ee5
GM
12160 }
12161 }
80c6cb1f
GM
12162
12163 return 0;
5f5c8ee5 12164}
12adba34 12165
1842fc1a 12166
5f5c8ee5
GM
12167/* Extend the face of the last glyph in the text area of IT->glyph_row
12168 to the end of the display line. Called from display_line.
12169 If the glyph row is empty, add a space glyph to it so that we
12170 know the face to draw. Set the glyph row flag fill_line_p. */
12171
12172static void
12173extend_face_to_end_of_line (it)
12174 struct it *it;
12175{
12176 struct face *face;
12177 struct frame *f = it->f;
1842fc1a 12178
5f5c8ee5
GM
12179 /* If line is already filled, do nothing. */
12180 if (it->current_x >= it->last_visible_x)
12181 return;
12182
12183 /* Face extension extends the background and box of IT->face_id
12184 to the end of the line. If the background equals the background
4aad61f8
GM
12185 of the frame, we don't have to do anything. */
12186 if (it->face_before_selective_p)
12187 face = FACE_FROM_ID (it->f, it->saved_face_id);
12188 else
12189 face = FACE_FROM_ID (f, it->face_id);
12190
5f5c8ee5
GM
12191 if (FRAME_WINDOW_P (f)
12192 && face->box == FACE_NO_BOX
12193 && face->background == FRAME_BACKGROUND_PIXEL (f)
12194 && !face->stipple)
12195 return;
1842fc1a 12196
5f5c8ee5
GM
12197 /* Set the glyph row flag indicating that the face of the last glyph
12198 in the text area has to be drawn to the end of the text area. */
12199 it->glyph_row->fill_line_p = 1;
545e04f6 12200
980806b6
KH
12201 /* If current character of IT is not ASCII, make sure we have the
12202 ASCII face. This will be automatically undone the next time
12203 get_next_display_element returns a multibyte character. Note
12204 that the character will always be single byte in unibyte text. */
12205 if (!SINGLE_BYTE_CHAR_P (it->c))
5f5c8ee5 12206 {
980806b6 12207 it->face_id = FACE_FOR_CHAR (f, face, 0);
5f5c8ee5 12208 }
545e04f6 12209
5f5c8ee5
GM
12210 if (FRAME_WINDOW_P (f))
12211 {
12212 /* If the row is empty, add a space with the current face of IT,
12213 so that we know which face to draw. */
12214 if (it->glyph_row->used[TEXT_AREA] == 0)
a2889657 12215 {
5f5c8ee5 12216 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
43d120d8 12217 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
5f5c8ee5 12218 it->glyph_row->used[TEXT_AREA] = 1;
a2889657 12219 }
5f5c8ee5
GM
12220 }
12221 else
12222 {
12223 /* Save some values that must not be changed. */
12224 int saved_x = it->current_x;
12225 struct text_pos saved_pos;
12226 Lisp_Object saved_object;
478d746b 12227 enum display_element_type saved_what = it->what;
4aad61f8 12228 int saved_face_id = it->face_id;
5f5c8ee5
GM
12229
12230 saved_object = it->object;
12231 saved_pos = it->position;
12232
12233 it->what = IT_CHARACTER;
12234 bzero (&it->position, sizeof it->position);
6fc556fd 12235 it->object = make_number (0);
5f5c8ee5
GM
12236 it->c = ' ';
12237 it->len = 1;
4aad61f8 12238 it->face_id = face->id;
5f5c8ee5
GM
12239
12240 PRODUCE_GLYPHS (it);
12241
12242 while (it->current_x <= it->last_visible_x)
12243 PRODUCE_GLYPHS (it);
12244
12245 /* Don't count these blanks really. It would let us insert a left
12246 truncation glyph below and make us set the cursor on them, maybe. */
12247 it->current_x = saved_x;
12248 it->object = saved_object;
12249 it->position = saved_pos;
12250 it->what = saved_what;
4aad61f8 12251 it->face_id = saved_face_id;
5f5c8ee5
GM
12252 }
12253}
12adba34 12254
545e04f6 12255
5f5c8ee5
GM
12256/* Value is non-zero if text starting at CHARPOS in current_buffer is
12257 trailing whitespace. */
1c9241f5 12258
5f5c8ee5
GM
12259static int
12260trailing_whitespace_p (charpos)
12261 int charpos;
12262{
12263 int bytepos = CHAR_TO_BYTE (charpos);
12264 int c = 0;
7bbe686f 12265
5f5c8ee5
GM
12266 while (bytepos < ZV_BYTE
12267 && (c = FETCH_CHAR (bytepos),
12268 c == ' ' || c == '\t'))
12269 ++bytepos;
0d09d1e6 12270
8f897821
GM
12271 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
12272 {
12273 if (bytepos != PT_BYTE)
12274 return 1;
12275 }
12276 return 0;
5f5c8ee5 12277}
31b24551 12278
545e04f6 12279
5f5c8ee5 12280/* Highlight trailing whitespace, if any, in ROW. */
545e04f6 12281
5f5c8ee5
GM
12282void
12283highlight_trailing_whitespace (f, row)
12284 struct frame *f;
12285 struct glyph_row *row;
12286{
12287 int used = row->used[TEXT_AREA];
12288
12289 if (used)
12290 {
12291 struct glyph *start = row->glyphs[TEXT_AREA];
12292 struct glyph *glyph = start + used - 1;
12293
ade0bee1
GM
12294 /* Skip over glyphs inserted to display the cursor at the
12295 end of a line, for extending the face of the last glyph
12296 to the end of the line on terminals, and for truncation
12297 and continuation glyphs. */
65008712
GM
12298 while (glyph >= start
12299 && glyph->type == CHAR_GLYPH
65008712 12300 && INTEGERP (glyph->object))
5f5c8ee5
GM
12301 --glyph;
12302
12303 /* If last glyph is a space or stretch, and it's trailing
12304 whitespace, set the face of all trailing whitespace glyphs in
12305 IT->glyph_row to `trailing-whitespace'. */
12306 if (glyph >= start
12307 && BUFFERP (glyph->object)
12308 && (glyph->type == STRETCH_GLYPH
12309 || (glyph->type == CHAR_GLYPH
43d120d8 12310 && glyph->u.ch == ' '))
5f5c8ee5 12311 && trailing_whitespace_p (glyph->charpos))
545e04f6 12312 {
980806b6 12313 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
5f5c8ee5
GM
12314
12315 while (glyph >= start
12316 && BUFFERP (glyph->object)
12317 && (glyph->type == STRETCH_GLYPH
12318 || (glyph->type == CHAR_GLYPH
43d120d8
KH
12319 && glyph->u.ch == ' ')))
12320 (glyph--)->face_id = face_id;
545e04f6 12321 }
a2889657 12322 }
5f5c8ee5 12323}
a2889657 12324
5fcbb24d 12325
cafafe0b 12326/* Value is non-zero if glyph row ROW in window W should be
0fd37545 12327 used to hold the cursor. */
cafafe0b
GM
12328
12329static int
12330cursor_row_p (w, row)
12331 struct window *w;
12332 struct glyph_row *row;
12333{
9a038881
GM
12334 int cursor_row_p = 1;
12335
cafafe0b
GM
12336 if (PT == MATRIX_ROW_END_CHARPOS (row))
12337 {
9a038881
GM
12338 /* If the row ends with a newline from a string, we don't want
12339 the cursor there (if the row is continued it doesn't end in a
12340 newline). */
12341 if (CHARPOS (row->end.string_pos) >= 0
12342 || MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12343 cursor_row_p = row->continued_p;
12344
12345 /* If the row ends at ZV, display the cursor at the end of that
12346 row instead of at the start of the row below. */
12347 else if (row->ends_at_zv_p)
12348 cursor_row_p = 1;
12349 else
12350 cursor_row_p = 0;
cafafe0b
GM
12351 }
12352
9a038881 12353 return cursor_row_p;
cafafe0b
GM
12354}
12355
12356
5f5c8ee5
GM
12357/* Construct the glyph row IT->glyph_row in the desired matrix of
12358 IT->w from text at the current position of IT. See dispextern.h
12359 for an overview of struct it. Value is non-zero if
12360 IT->glyph_row displays text, as opposed to a line displaying ZV
12361 only. */
12362
12363static int
12364display_line (it)
12365 struct it *it;
12366{
12367 struct glyph_row *row = it->glyph_row;
12368
12369 /* We always start displaying at hpos zero even if hscrolled. */
12370 xassert (it->hpos == 0 && it->current_x == 0);
a2889657 12371
5f5c8ee5
GM
12372 /* We must not display in a row that's not a text row. */
12373 xassert (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
12374 < it->w->desired_matrix->nrows);
12adba34 12375
5f5c8ee5
GM
12376 /* Is IT->w showing the region? */
12377 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
12adba34 12378
5f5c8ee5
GM
12379 /* Clear the result glyph row and enable it. */
12380 prepare_desired_row (row);
12adba34 12381
5f5c8ee5
GM
12382 row->y = it->current_y;
12383 row->start = it->current;
12384 row->continuation_lines_width = it->continuation_lines_width;
12385 row->displays_text_p = 1;
91004049
GM
12386 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
12387 it->starts_in_middle_of_char_p = 0;
5f5c8ee5
GM
12388
12389 /* Arrange the overlays nicely for our purposes. Usually, we call
12390 display_line on only one line at a time, in which case this
12391 can't really hurt too much, or we call it on lines which appear
12392 one after another in the buffer, in which case all calls to
12393 recenter_overlay_lists but the first will be pretty cheap. */
12394 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
12395
5f5c8ee5
GM
12396 /* Move over display elements that are not visible because we are
12397 hscrolled. This may stop at an x-position < IT->first_visible_x
12398 if the first glyph is partially visible or if we hit a line end. */
12399 if (it->current_x < it->first_visible_x)
12400 move_it_in_display_line_to (it, ZV, it->first_visible_x,
12401 MOVE_TO_POS | MOVE_TO_X);
12402
12403 /* Get the initial row height. This is either the height of the
12404 text hscrolled, if there is any, or zero. */
12405 row->ascent = it->max_ascent;
12406 row->height = it->max_ascent + it->max_descent;
312246d1
GM
12407 row->phys_ascent = it->max_phys_ascent;
12408 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
5f5c8ee5
GM
12409
12410 /* Loop generating characters. The loop is left with IT on the next
12411 character to display. */
12412 while (1)
12413 {
12414 int n_glyphs_before, hpos_before, x_before;
12415 int x, i, nglyphs;
ae26e27d 12416 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
b28cb6ed 12417
5f5c8ee5
GM
12418 /* Retrieve the next thing to display. Value is zero if end of
12419 buffer reached. */
12420 if (!get_next_display_element (it))
12421 {
12422 /* Maybe add a space at the end of this line that is used to
1b335865
GM
12423 display the cursor there under X. Set the charpos of the
12424 first glyph of blank lines not corresponding to any text
12425 to -1. */
12426 if ((append_space (it, 1) && row->used[TEXT_AREA] == 1)
12427 || row->used[TEXT_AREA] == 0)
a2889657 12428 {
5f5c8ee5
GM
12429 row->glyphs[TEXT_AREA]->charpos = -1;
12430 row->displays_text_p = 0;
12431
12432 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines))
12433 row->indicate_empty_line_p = 1;
a2889657 12434 }
5f5c8ee5
GM
12435
12436 it->continuation_lines_width = 0;
12437 row->ends_at_zv_p = 1;
12438 break;
a2889657 12439 }
a2889657 12440
5f5c8ee5
GM
12441 /* Now, get the metrics of what we want to display. This also
12442 generates glyphs in `row' (which is IT->glyph_row). */
12443 n_glyphs_before = row->used[TEXT_AREA];
12444 x = it->current_x;
e6819faf
GM
12445
12446 /* Remember the line height so far in case the next element doesn't
12447 fit on the line. */
12448 if (!it->truncate_lines_p)
12449 {
12450 ascent = it->max_ascent;
12451 descent = it->max_descent;
12452 phys_ascent = it->max_phys_ascent;
12453 phys_descent = it->max_phys_descent;
12454 }
12455
5f5c8ee5 12456 PRODUCE_GLYPHS (it);
a2889657 12457
5f5c8ee5
GM
12458 /* If this display element was in marginal areas, continue with
12459 the next one. */
12460 if (it->area != TEXT_AREA)
a2889657 12461 {
5f5c8ee5
GM
12462 row->ascent = max (row->ascent, it->max_ascent);
12463 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12464 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12465 row->phys_height = max (row->phys_height,
12466 it->max_phys_ascent + it->max_phys_descent);
cafafe0b 12467 set_iterator_to_next (it, 1);
5f5c8ee5
GM
12468 continue;
12469 }
5936754e 12470
5f5c8ee5
GM
12471 /* Does the display element fit on the line? If we truncate
12472 lines, we should draw past the right edge of the window. If
12473 we don't truncate, we want to stop so that we can display the
12474 continuation glyph before the right margin. If lines are
12475 continued, there are two possible strategies for characters
12476 resulting in more than 1 glyph (e.g. tabs): Display as many
12477 glyphs as possible in this line and leave the rest for the
12478 continuation line, or display the whole element in the next
12479 line. Original redisplay did the former, so we do it also. */
12480 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12481 hpos_before = it->hpos;
12482 x_before = x;
4dcd74e6
GM
12483
12484 if (/* Not a newline. */
12485 nglyphs > 0
202c1b5b 12486 /* Glyphs produced fit entirely in the line. */
4dcd74e6
GM
12487 && it->current_x < it->last_visible_x)
12488 {
37be86f2 12489 it->hpos += nglyphs;
5f5c8ee5
GM
12490 row->ascent = max (row->ascent, it->max_ascent);
12491 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12492 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12493 row->phys_height = max (row->phys_height,
12494 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12495 if (it->current_x - it->pixel_width < it->first_visible_x)
12496 row->x = x - it->first_visible_x;
12497 }
12498 else
12499 {
12500 int new_x;
12501 struct glyph *glyph;
12502
12503 for (i = 0; i < nglyphs; ++i, x = new_x)
b5bbc9a5 12504 {
5f5c8ee5
GM
12505 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12506 new_x = x + glyph->pixel_width;
12507
12508 if (/* Lines are continued. */
12509 !it->truncate_lines_p
12510 && (/* Glyph doesn't fit on the line. */
12511 new_x > it->last_visible_x
12512 /* Or it fits exactly on a window system frame. */
12513 || (new_x == it->last_visible_x
12514 && FRAME_WINDOW_P (it->f))))
a2889657 12515 {
5f5c8ee5
GM
12516 /* End of a continued line. */
12517
12518 if (it->hpos == 0
12519 || (new_x == it->last_visible_x
12520 && FRAME_WINDOW_P (it->f)))
12521 {
e6819faf
GM
12522 /* Current glyph is the only one on the line or
12523 fits exactly on the line. We must continue
12524 the line because we can't draw the cursor
12525 after the glyph. */
5f5c8ee5
GM
12526 row->continued_p = 1;
12527 it->current_x = new_x;
12528 it->continuation_lines_width += new_x;
12529 ++it->hpos;
12530 if (i == nglyphs - 1)
cafafe0b 12531 set_iterator_to_next (it, 1);
5f5c8ee5 12532 }
3b3c4bf0
GM
12533 else if (CHAR_GLYPH_PADDING_P (*glyph)
12534 && !FRAME_WINDOW_P (it->f))
12535 {
12536 /* A padding glyph that doesn't fit on this line.
12537 This means the whole character doesn't fit
12538 on the line. */
12539 row->used[TEXT_AREA] = n_glyphs_before;
12540
12541 /* Fill the rest of the row with continuation
12542 glyphs like in 20.x. */
12543 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
12544 < row->glyphs[1 + TEXT_AREA])
12545 produce_special_glyphs (it, IT_CONTINUATION);
12546
12547 row->continued_p = 1;
12548 it->current_x = x_before;
12549 it->continuation_lines_width += x_before;
12550
12551 /* Restore the height to what it was before the
12552 element not fitting on the line. */
12553 it->max_ascent = ascent;
12554 it->max_descent = descent;
12555 it->max_phys_ascent = phys_ascent;
12556 it->max_phys_descent = phys_descent;
12557 }
5f5c8ee5 12558 else
5936754e 12559 {
5f5c8ee5
GM
12560 /* Display element draws past the right edge of
12561 the window. Restore positions to values
12562 before the element. The next line starts
12563 with current_x before the glyph that could
12564 not be displayed, so that TAB works right. */
12565 row->used[TEXT_AREA] = n_glyphs_before + i;
12566
12567 /* Display continuation glyphs. */
12568 if (!FRAME_WINDOW_P (it->f))
12569 produce_special_glyphs (it, IT_CONTINUATION);
12570 row->continued_p = 1;
12571
12572 it->current_x = x;
12573 it->continuation_lines_width += x;
91004049
GM
12574 if (nglyphs > 1 && i > 0)
12575 {
12576 row->ends_in_middle_of_char_p = 1;
12577 it->starts_in_middle_of_char_p = 1;
12578 }
e6819faf
GM
12579
12580 /* Restore the height to what it was before the
12581 element not fitting on the line. */
12582 it->max_ascent = ascent;
12583 it->max_descent = descent;
12584 it->max_phys_ascent = phys_ascent;
12585 it->max_phys_descent = phys_descent;
5936754e 12586 }
e6819faf 12587
5f5c8ee5
GM
12588 break;
12589 }
12590 else if (new_x > it->first_visible_x)
12591 {
12592 /* Increment number of glyphs actually displayed. */
12593 ++it->hpos;
12594
12595 if (x < it->first_visible_x)
12596 /* Glyph is partially visible, i.e. row starts at
12597 negative X position. */
12598 row->x = x - it->first_visible_x;
12599 }
12600 else
12601 {
12602 /* Glyph is completely off the left margin of the
12603 window. This should not happen because of the
12604 move_it_in_display_line at the start of
12605 this function. */
12606 abort ();
a2889657 12607 }
a2889657 12608 }
5f5c8ee5
GM
12609
12610 row->ascent = max (row->ascent, it->max_ascent);
12611 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
12612 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
12613 row->phys_height = max (row->phys_height,
12614 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
12615
12616 /* End of this display line if row is continued. */
12617 if (row->continued_p)
12618 break;
a2889657 12619 }
a2889657 12620
5f5c8ee5
GM
12621 /* Is this a line end? If yes, we're also done, after making
12622 sure that a non-default face is extended up to the right
12623 margin of the window. */
12624 if (ITERATOR_AT_END_OF_LINE_P (it))
1c9241f5 12625 {
5f5c8ee5
GM
12626 int used_before = row->used[TEXT_AREA];
12627
12628 /* Add a space at the end of the line that is used to
12629 display the cursor there. */
12630 append_space (it, 0);
12631
12632 /* Extend the face to the end of the line. */
12633 extend_face_to_end_of_line (it);
12634
12635 /* Make sure we have the position. */
12636 if (used_before == 0)
12637 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
12638
12639 /* Consume the line end. This skips over invisible lines. */
cafafe0b 12640 set_iterator_to_next (it, 1);
5f5c8ee5
GM
12641 it->continuation_lines_width = 0;
12642 break;
1c9241f5 12643 }
a2889657 12644
5f5c8ee5
GM
12645 /* Proceed with next display element. Note that this skips
12646 over lines invisible because of selective display. */
cafafe0b 12647 set_iterator_to_next (it, 1);
b1d1124b 12648
5f5c8ee5
GM
12649 /* If we truncate lines, we are done when the last displayed
12650 glyphs reach past the right margin of the window. */
12651 if (it->truncate_lines_p
12652 && (FRAME_WINDOW_P (it->f)
12653 ? (it->current_x >= it->last_visible_x)
12654 : (it->current_x > it->last_visible_x)))
75d13c64 12655 {
5f5c8ee5
GM
12656 /* Maybe add truncation glyphs. */
12657 if (!FRAME_WINDOW_P (it->f))
12658 {
a98b5ed9
GM
12659 int i, n;
12660
12661 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
12662 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
12663 break;
12664
12665 for (n = row->used[TEXT_AREA]; i < n; ++i)
12666 {
12667 row->used[TEXT_AREA] = i;
12668 produce_special_glyphs (it, IT_TRUNCATION);
12669 }
5f5c8ee5
GM
12670 }
12671
12672 row->truncated_on_right_p = 1;
12673 it->continuation_lines_width = 0;
312246d1 12674 reseat_at_next_visible_line_start (it, 0);
5f5c8ee5
GM
12675 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
12676 it->hpos = hpos_before;
12677 it->current_x = x_before;
12678 break;
75d13c64 12679 }
a2889657 12680 }
a2889657 12681
5f5c8ee5
GM
12682 /* If line is not empty and hscrolled, maybe insert truncation glyphs
12683 at the left window margin. */
12684 if (it->first_visible_x
12685 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
12686 {
12687 if (!FRAME_WINDOW_P (it->f))
12688 insert_left_trunc_glyphs (it);
12689 row->truncated_on_left_p = 1;
12690 }
a2889657 12691
5f5c8ee5
GM
12692 /* If the start of this line is the overlay arrow-position, then
12693 mark this glyph row as the one containing the overlay arrow.
12694 This is clearly a mess with variable size fonts. It would be
12695 better to let it be displayed like cursors under X. */
e24c997d 12696 if (MARKERP (Voverlay_arrow_position)
a2889657 12697 && current_buffer == XMARKER (Voverlay_arrow_position)->buffer
5f5c8ee5
GM
12698 && (MATRIX_ROW_START_CHARPOS (row)
12699 == marker_position (Voverlay_arrow_position))
e24c997d 12700 && STRINGP (Voverlay_arrow_string)
a2889657
JB
12701 && ! overlay_arrow_seen)
12702 {
5f5c8ee5
GM
12703 /* Overlay arrow in window redisplay is a bitmap. */
12704 if (!FRAME_WINDOW_P (it->f))
c4628384 12705 {
5f5c8ee5
GM
12706 struct glyph_row *arrow_row = get_overlay_arrow_glyph_row (it->w);
12707 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
12708 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
12709 struct glyph *p = row->glyphs[TEXT_AREA];
12710 struct glyph *p2, *end;
12711
12712 /* Copy the arrow glyphs. */
12713 while (glyph < arrow_end)
12714 *p++ = *glyph++;
12715
12716 /* Throw away padding glyphs. */
12717 p2 = p;
12718 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
12719 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
12720 ++p2;
12721 if (p2 > p)
212e4f87 12722 {
5f5c8ee5
GM
12723 while (p2 < end)
12724 *p++ = *p2++;
12725 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
c4628384 12726 }
c4628384 12727 }
5f5c8ee5 12728
a2889657 12729 overlay_arrow_seen = 1;
5f5c8ee5 12730 row->overlay_arrow_p = 1;
a2889657
JB
12731 }
12732
5f5c8ee5
GM
12733 /* Compute pixel dimensions of this line. */
12734 compute_line_metrics (it);
12735
12736 /* Remember the position at which this line ends. */
12737 row->end = it->current;
12738
173cbca8 12739 /* Maybe set the cursor. */
5f5c8ee5
GM
12740 if (it->w->cursor.vpos < 0
12741 && PT >= MATRIX_ROW_START_CHARPOS (row)
cafafe0b
GM
12742 && PT <= MATRIX_ROW_END_CHARPOS (row)
12743 && cursor_row_p (it->w, row))
12744 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
5f5c8ee5
GM
12745
12746 /* Highlight trailing whitespace. */
8f897821 12747 if (!NILP (Vshow_trailing_whitespace))
5f5c8ee5
GM
12748 highlight_trailing_whitespace (it->f, it->glyph_row);
12749
12750 /* Prepare for the next line. This line starts horizontally at (X
12751 HPOS) = (0 0). Vertical positions are incremented. As a
12752 convenience for the caller, IT->glyph_row is set to the next
12753 row to be used. */
12754 it->current_x = it->hpos = 0;
12755 it->current_y += row->height;
12756 ++it->vpos;
12757 ++it->glyph_row;
12758 return row->displays_text_p;
a2889657 12759}
5f5c8ee5
GM
12760
12761
a2889657 12762\f
5f5c8ee5
GM
12763/***********************************************************************
12764 Menu Bar
12765 ***********************************************************************/
12766
12767/* Redisplay the menu bar in the frame for window W.
12768
12769 The menu bar of X frames that don't have X toolkit support is
12770 displayed in a special window W->frame->menu_bar_window.
12771
12772 The menu bar of terminal frames is treated specially as far as
12773 glyph matrices are concerned. Menu bar lines are not part of
12774 windows, so the update is done directly on the frame matrix rows
12775 for the menu bar. */
7ce2c095
RS
12776
12777static void
12778display_menu_bar (w)
12779 struct window *w;
12780{
5f5c8ee5
GM
12781 struct frame *f = XFRAME (WINDOW_FRAME (w));
12782 struct it it;
12783 Lisp_Object items;
8351baf2 12784 int i;
7ce2c095 12785
5f5c8ee5 12786 /* Don't do all this for graphical frames. */
dc937613 12787#ifdef HAVE_NTGUI
d129c4c2
KH
12788 if (!NILP (Vwindow_system))
12789 return;
dc937613 12790#endif
dc937613 12791#ifdef USE_X_TOOLKIT
d3413a53 12792 if (FRAME_X_P (f))
7ce2c095 12793 return;
5f5c8ee5 12794#endif
1a578e9b
AC
12795#ifdef macintosh
12796 if (FRAME_MAC_P (f))
12797 return;
12798#endif
5f5c8ee5
GM
12799
12800#ifdef USE_X_TOOLKIT
12801 xassert (!FRAME_WINDOW_P (f));
52377a47 12802 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
5f5c8ee5
GM
12803 it.first_visible_x = 0;
12804 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12805#else /* not USE_X_TOOLKIT */
12806 if (FRAME_WINDOW_P (f))
12807 {
12808 /* Menu bar lines are displayed in the desired matrix of the
12809 dummy window menu_bar_window. */
12810 struct window *menu_w;
12811 xassert (WINDOWP (f->menu_bar_window));
12812 menu_w = XWINDOW (f->menu_bar_window);
12813 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
52377a47 12814 MENU_FACE_ID);
5f5c8ee5
GM
12815 it.first_visible_x = 0;
12816 it.last_visible_x = FRAME_WINDOW_WIDTH (f) * CANON_X_UNIT (f);
12817 }
12818 else
12819 {
12820 /* This is a TTY frame, i.e. character hpos/vpos are used as
12821 pixel x/y. */
12822 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
52377a47 12823 MENU_FACE_ID);
5f5c8ee5
GM
12824 it.first_visible_x = 0;
12825 it.last_visible_x = FRAME_WIDTH (f);
12826 }
12827#endif /* not USE_X_TOOLKIT */
12828
1862a24e
MB
12829 if (! mode_line_inverse_video)
12830 /* Force the menu-bar to be displayed in the default face. */
12831 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12832
5f5c8ee5
GM
12833 /* Clear all rows of the menu bar. */
12834 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
12835 {
12836 struct glyph_row *row = it.glyph_row + i;
12837 clear_glyph_row (row);
12838 row->enabled_p = 1;
12839 row->full_width_p = 1;
12840 }
7ce2c095 12841
5f5c8ee5
GM
12842 /* Display all items of the menu bar. */
12843 items = FRAME_MENU_BAR_ITEMS (it.f);
469937ac 12844 for (i = 0; i < XVECTOR (items)->size; i += 4)
7ce2c095 12845 {
5f5c8ee5
GM
12846 Lisp_Object string;
12847
12848 /* Stop at nil string. */
a61b7058 12849 string = AREF (items, i + 1);
8351baf2
RS
12850 if (NILP (string))
12851 break;
2d66ad19 12852
5f5c8ee5 12853 /* Remember where item was displayed. */
a61b7058 12854 AREF (items, i + 3) = make_number (it.hpos);
7ce2c095 12855
5f5c8ee5
GM
12856 /* Display the item, pad with one space. */
12857 if (it.current_x < it.last_visible_x)
12858 display_string (NULL, string, Qnil, 0, 0, &it,
12859 XSTRING (string)->size + 1, 0, 0, -1);
7ce2c095
RS
12860 }
12861
2d66ad19 12862 /* Fill out the line with spaces. */
5f5c8ee5
GM
12863 if (it.current_x < it.last_visible_x)
12864 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
db6f348c 12865
5f5c8ee5
GM
12866 /* Compute the total height of the lines. */
12867 compute_line_metrics (&it);
7ce2c095 12868}
5f5c8ee5
GM
12869
12870
7ce2c095 12871\f
5f5c8ee5
GM
12872/***********************************************************************
12873 Mode Line
12874 ***********************************************************************/
12875
715e84c9
GM
12876/* Redisplay mode lines in the window tree whose root is WINDOW. If
12877 FORCE is non-zero, redisplay mode lines unconditionally.
12878 Otherwise, redisplay only mode lines that are garbaged. Value is
12879 the number of windows whose mode lines were redisplayed. */
a2889657 12880
715e84c9
GM
12881static int
12882redisplay_mode_lines (window, force)
12883 Lisp_Object window;
12884 int force;
12885{
12886 int nwindows = 0;
12887
12888 while (!NILP (window))
12889 {
12890 struct window *w = XWINDOW (window);
12891
12892 if (WINDOWP (w->hchild))
12893 nwindows += redisplay_mode_lines (w->hchild, force);
12894 else if (WINDOWP (w->vchild))
12895 nwindows += redisplay_mode_lines (w->vchild, force);
12896 else if (force
12897 || FRAME_GARBAGED_P (XFRAME (w->frame))
12898 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
12899 {
12900 Lisp_Object old_selected_frame;
12901 struct text_pos lpoint;
12902 struct buffer *old = current_buffer;
12903
12904 /* Set the window's buffer for the mode line display. */
12905 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12906 set_buffer_internal_1 (XBUFFER (w->buffer));
12907
12908 /* Point refers normally to the selected window. For any
12909 other window, set up appropriate value. */
12910 if (!EQ (window, selected_window))
12911 {
12912 struct text_pos pt;
12913
12914 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
12915 if (CHARPOS (pt) < BEGV)
12916 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
12917 else if (CHARPOS (pt) > (ZV - 1))
12918 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
12919 else
12920 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
12921 }
12922
12923 /* Temporarily set up the selected frame. */
12924 old_selected_frame = selected_frame;
12925 selected_frame = w->frame;
12926
12927 /* Display mode lines. */
12928 clear_glyph_matrix (w->desired_matrix);
12929 if (display_mode_lines (w))
12930 {
12931 ++nwindows;
12932 w->must_be_updated_p = 1;
12933 }
12934
12935 /* Restore old settings. */
12936 selected_frame = old_selected_frame;
12937 set_buffer_internal_1 (old);
12938 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12939 }
12940
12941 window = w->next;
12942 }
12943
12944 return nwindows;
12945}
12946
12947
12948/* Display the mode and/or top line of window W. Value is the number
12949 of mode lines displayed. */
12950
12951static int
5f5c8ee5 12952display_mode_lines (w)
a2889657
JB
12953 struct window *w;
12954{
715e84c9
GM
12955 int n = 0;
12956
5f5c8ee5 12957 /* These will be set while the mode line specs are processed. */
aa6d10fa 12958 line_number_displayed = 0;
155ef550 12959 w->column_number_displayed = Qnil;
aa6d10fa 12960
5f5c8ee5 12961 if (WINDOW_WANTS_MODELINE_P (w))
715e84c9
GM
12962 {
12963 display_mode_line (w, MODE_LINE_FACE_ID,
12964 current_buffer->mode_line_format);
12965 ++n;
12966 }
5f5c8ee5 12967
045dee35 12968 if (WINDOW_WANTS_HEADER_LINE_P (w))
715e84c9
GM
12969 {
12970 display_mode_line (w, HEADER_LINE_FACE_ID,
12971 current_buffer->header_line_format);
12972 ++n;
12973 }
12974
12975 return n;
5f5c8ee5 12976}
03b294dc 12977
03b294dc 12978
5f5c8ee5 12979/* Display mode or top line of window W. FACE_ID specifies which line
045dee35 12980 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
04612a64
GM
12981 FORMAT is the mode line format to display. Value is the pixel
12982 height of the mode line displayed. */
03b294dc 12983
04612a64 12984static int
5f5c8ee5
GM
12985display_mode_line (w, face_id, format)
12986 struct window *w;
12987 enum face_id face_id;
12988 Lisp_Object format;
12989{
12990 struct it it;
12991 struct face *face;
03b294dc 12992
5f5c8ee5
GM
12993 init_iterator (&it, w, -1, -1, NULL, face_id);
12994 prepare_desired_row (it.glyph_row);
12995
1862a24e
MB
12996 if (! mode_line_inverse_video)
12997 /* Force the mode-line to be displayed in the default face. */
12998 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
12999
5f5c8ee5
GM
13000 /* Temporarily make frame's keyboard the current kboard so that
13001 kboard-local variables in the mode_line_format will get the right
13002 values. */
13003 push_frame_kboard (it.f);
13004 display_mode_element (&it, 0, 0, 0, format);
13005 pop_frame_kboard ();
a2889657 13006
5f5c8ee5
GM
13007 /* Fill up with spaces. */
13008 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
13009
13010 compute_line_metrics (&it);
13011 it.glyph_row->full_width_p = 1;
13012 it.glyph_row->mode_line_p = 1;
1862a24e 13013 it.glyph_row->inverse_p = 0;
5f5c8ee5
GM
13014 it.glyph_row->continued_p = 0;
13015 it.glyph_row->truncated_on_left_p = 0;
13016 it.glyph_row->truncated_on_right_p = 0;
13017
13018 /* Make a 3D mode-line have a shadow at its right end. */
13019 face = FACE_FROM_ID (it.f, face_id);
13020 extend_face_to_end_of_line (&it);
13021 if (face->box != FACE_NO_BOX)
d7eb09a0 13022 {
5f5c8ee5
GM
13023 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
13024 + it.glyph_row->used[TEXT_AREA] - 1);
13025 last->right_box_line_p = 1;
d7eb09a0 13026 }
04612a64
GM
13027
13028 return it.glyph_row->height;
a2889657
JB
13029}
13030
a2889657 13031
5f5c8ee5
GM
13032/* Contribute ELT to the mode line for window IT->w. How it
13033 translates into text depends on its data type.
a2889657 13034
5f5c8ee5 13035 IT describes the display environment in which we display, as usual.
a2889657
JB
13036
13037 DEPTH is the depth in recursion. It is used to prevent
13038 infinite recursion here.
13039
5f5c8ee5
GM
13040 FIELD_WIDTH is the number of characters the display of ELT should
13041 occupy in the mode line, and PRECISION is the maximum number of
13042 characters to display from ELT's representation. See
13043 display_string for details. *
a2889657 13044
5f5c8ee5 13045 Returns the hpos of the end of the text generated by ELT. */
a2889657
JB
13046
13047static int
5f5c8ee5
GM
13048display_mode_element (it, depth, field_width, precision, elt)
13049 struct it *it;
a2889657 13050 int depth;
5f5c8ee5
GM
13051 int field_width, precision;
13052 Lisp_Object elt;
a2889657 13053{
5f5c8ee5
GM
13054 int n = 0, field, prec;
13055
a2889657
JB
13056 tail_recurse:
13057 if (depth > 10)
13058 goto invalid;
13059
13060 depth++;
13061
0220c518 13062 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
a2889657
JB
13063 {
13064 case Lisp_String:
13065 {
13066 /* A string: output it and check for %-constructs within it. */
5f5c8ee5
GM
13067 unsigned char c;
13068 unsigned char *this = XSTRING (elt)->data;
13069 unsigned char *lisp_string = this;
13070
13071 while ((precision <= 0 || n < precision)
13072 && *this
13073 && (frame_title_ptr
13074 || it->current_x < it->last_visible_x))
a2889657
JB
13075 {
13076 unsigned char *last = this;
5f5c8ee5
GM
13077
13078 /* Advance to end of string or next format specifier. */
a2889657
JB
13079 while ((c = *this++) != '\0' && c != '%')
13080 ;
5f5c8ee5 13081
a2889657
JB
13082 if (this - 1 != last)
13083 {
5f5c8ee5
GM
13084 /* Output to end of string or up to '%'. Field width
13085 is length of string. Don't output more than
13086 PRECISION allows us. */
d26b89b8
KH
13087 --this;
13088 prec = strwidth (last, this - last);
5f5c8ee5
GM
13089 if (precision > 0 && prec > precision - n)
13090 prec = precision - n;
13091
d39b6696 13092 if (frame_title_ptr)
d26b89b8 13093 n += store_frame_title (last, 0, prec);
d39b6696 13094 else
5f5c8ee5
GM
13095 n += display_string (NULL, elt, Qnil, 0, last - lisp_string,
13096 it, 0, prec, 0, -1);
a2889657
JB
13097 }
13098 else /* c == '%' */
13099 {
5f5c8ee5
GM
13100 unsigned char *percent_position = this;
13101
13102 /* Get the specified minimum width. Zero means
13103 don't pad. */
13104 field = 0;
a2889657 13105 while ((c = *this++) >= '0' && c <= '9')
5f5c8ee5 13106 field = field * 10 + c - '0';
a2889657 13107
5f5c8ee5
GM
13108 /* Don't pad beyond the total padding allowed. */
13109 if (field_width - n > 0 && field > field_width - n)
13110 field = field_width - n;
a2889657 13111
5f5c8ee5
GM
13112 /* Note that either PRECISION <= 0 or N < PRECISION. */
13113 prec = precision - n;
13114
a2889657 13115 if (c == 'M')
5f5c8ee5
GM
13116 n += display_mode_element (it, depth, field, prec,
13117 Vglobal_mode_string);
a2889657 13118 else if (c != 0)
d39b6696 13119 {
5f5c8ee5
GM
13120 unsigned char *spec
13121 = decode_mode_spec (it->w, c, field, prec);
13122
d39b6696 13123 if (frame_title_ptr)
5f5c8ee5 13124 n += store_frame_title (spec, field, prec);
d39b6696 13125 else
5f5c8ee5
GM
13126 {
13127 int nglyphs_before
13128 = it->glyph_row->used[TEXT_AREA];
13129 int charpos
13130 = percent_position - XSTRING (elt)->data;
13131 int nwritten
13132 = display_string (spec, Qnil, elt, charpos, 0, it,
13133 field, prec, 0, -1);
13134
13135 /* Assign to the glyphs written above the
13136 string where the `%x' came from, position
13137 of the `%'. */
13138 if (nwritten > 0)
13139 {
13140 struct glyph *glyph
13141 = (it->glyph_row->glyphs[TEXT_AREA]
13142 + nglyphs_before);
13143 int i;
13144
13145 for (i = 0; i < nwritten; ++i)
13146 {
13147 glyph[i].object = elt;
13148 glyph[i].charpos = charpos;
13149 }
13150
13151 n += nwritten;
13152 }
13153 }
d39b6696 13154 }
a2889657
JB
13155 }
13156 }
13157 }
13158 break;
13159
13160 case Lisp_Symbol:
13161 /* A symbol: process the value of the symbol recursively
13162 as if it appeared here directly. Avoid error if symbol void.
13163 Special case: if value of symbol is a string, output the string
13164 literally. */
13165 {
13166 register Lisp_Object tem;
13167 tem = Fboundp (elt);
265a9e55 13168 if (!NILP (tem))
a2889657
JB
13169 {
13170 tem = Fsymbol_value (elt);
13171 /* If value is a string, output that string literally:
13172 don't check for % within it. */
e24c997d 13173 if (STRINGP (tem))
d39b6696 13174 {
d26b89b8 13175 prec = precision - n;
d39b6696 13176 if (frame_title_ptr)
5f5c8ee5 13177 n += store_frame_title (XSTRING (tem)->data, -1, prec);
d39b6696 13178 else
5f5c8ee5
GM
13179 n += display_string (NULL, tem, Qnil, 0, 0, it,
13180 0, prec, 0, -1);
d39b6696 13181 }
a2889657 13182 else if (!EQ (tem, elt))
5f5c8ee5
GM
13183 {
13184 /* Give up right away for nil or t. */
13185 elt = tem;
13186 goto tail_recurse;
13187 }
a2889657
JB
13188 }
13189 }
13190 break;
13191
13192 case Lisp_Cons:
13193 {
13194 register Lisp_Object car, tem;
13195
13196 /* A cons cell: three distinct cases.
13197 If first element is a string or a cons, process all the elements
13198 and effectively concatenate them.
13199 If first element is a negative number, truncate displaying cdr to
13200 at most that many characters. If positive, pad (with spaces)
13201 to at least that many characters.
13202 If first element is a symbol, process the cadr or caddr recursively
13203 according to whether the symbol's value is non-nil or nil. */
9472f927 13204 car = XCAR (elt);
5f5c8ee5
GM
13205 if (EQ (car, QCeval) && CONSP (XCDR (elt)))
13206 {
13207 /* An element of the form (:eval FORM) means evaluate FORM
13208 and use the result as mode line elements. */
13209 struct gcpro gcpro1;
13210 Lisp_Object spec;
13211
116d6f5c 13212 spec = safe_eval (XCAR (XCDR (elt)));
5f5c8ee5
GM
13213 GCPRO1 (spec);
13214 n += display_mode_element (it, depth, field_width - n,
13215 precision - n, spec);
13216 UNGCPRO;
13217 }
13218 else if (SYMBOLP (car))
a2889657
JB
13219 {
13220 tem = Fboundp (car);
9472f927 13221 elt = XCDR (elt);
e24c997d 13222 if (!CONSP (elt))
a2889657
JB
13223 goto invalid;
13224 /* elt is now the cdr, and we know it is a cons cell.
13225 Use its car if CAR has a non-nil value. */
265a9e55 13226 if (!NILP (tem))
a2889657
JB
13227 {
13228 tem = Fsymbol_value (car);
265a9e55 13229 if (!NILP (tem))
9472f927
GM
13230 {
13231 elt = XCAR (elt);
13232 goto tail_recurse;
13233 }
a2889657
JB
13234 }
13235 /* Symbol's value is nil (or symbol is unbound)
13236 Get the cddr of the original list
13237 and if possible find the caddr and use that. */
9472f927 13238 elt = XCDR (elt);
265a9e55 13239 if (NILP (elt))
a2889657 13240 break;
e24c997d 13241 else if (!CONSP (elt))
a2889657 13242 goto invalid;
9472f927 13243 elt = XCAR (elt);
a2889657
JB
13244 goto tail_recurse;
13245 }
e24c997d 13246 else if (INTEGERP (car))
a2889657
JB
13247 {
13248 register int lim = XINT (car);
9472f927 13249 elt = XCDR (elt);
a2889657 13250 if (lim < 0)
5f5c8ee5
GM
13251 {
13252 /* Negative int means reduce maximum width. */
13253 if (precision <= 0)
13254 precision = -lim;
13255 else
13256 precision = min (precision, -lim);
13257 }
a2889657
JB
13258 else if (lim > 0)
13259 {
13260 /* Padding specified. Don't let it be more than
13261 current maximum. */
5f5c8ee5
GM
13262 if (precision > 0)
13263 lim = min (precision, lim);
13264
a2889657
JB
13265 /* If that's more padding than already wanted, queue it.
13266 But don't reduce padding already specified even if
13267 that is beyond the current truncation point. */
5f5c8ee5 13268 field_width = max (lim, field_width);
a2889657
JB
13269 }
13270 goto tail_recurse;
13271 }
e24c997d 13272 else if (STRINGP (car) || CONSP (car))
a2889657
JB
13273 {
13274 register int limit = 50;
5f5c8ee5
GM
13275 /* Limit is to protect against circular lists. */
13276 while (CONSP (elt)
13277 && --limit > 0
13278 && (precision <= 0 || n < precision))
a2889657 13279 {
5f5c8ee5 13280 n += display_mode_element (it, depth, field_width - n,
9472f927
GM
13281 precision - n, XCAR (elt));
13282 elt = XCDR (elt);
a2889657
JB
13283 }
13284 }
13285 }
13286 break;
13287
13288 default:
13289 invalid:
d39b6696 13290 if (frame_title_ptr)
5f5c8ee5 13291 n += store_frame_title ("*invalid*", 0, precision - n);
d39b6696 13292 else
5f5c8ee5
GM
13293 n += display_string ("*invalid*", Qnil, Qnil, 0, 0, it, 0,
13294 precision - n, 0, 0);
13295 return n;
a2889657
JB
13296 }
13297
5f5c8ee5
GM
13298 /* Pad to FIELD_WIDTH. */
13299 if (field_width > 0 && n < field_width)
13300 {
13301 if (frame_title_ptr)
13302 n += store_frame_title ("", field_width - n, 0);
13303 else
13304 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
13305 0, 0, 0);
13306 }
13307
13308 return n;
a2889657 13309}
5f5c8ee5
GM
13310
13311
766525bc
RS
13312/* Write a null-terminated, right justified decimal representation of
13313 the positive integer D to BUF using a minimal field width WIDTH. */
13314
13315static void
13316pint2str (buf, width, d)
13317 register char *buf;
13318 register int width;
13319 register int d;
13320{
13321 register char *p = buf;
13322
13323 if (d <= 0)
5f5c8ee5 13324 *p++ = '0';
766525bc 13325 else
5f5c8ee5 13326 {
766525bc 13327 while (d > 0)
5f5c8ee5 13328 {
766525bc
RS
13329 *p++ = d % 10 + '0';
13330 d /= 10;
5f5c8ee5
GM
13331 }
13332 }
13333
13334 for (width -= (int) (p - buf); width > 0; --width)
13335 *p++ = ' ';
766525bc
RS
13336 *p-- = '\0';
13337 while (p > buf)
5f5c8ee5 13338 {
766525bc
RS
13339 d = *buf;
13340 *buf++ = *p;
13341 *p-- = d;
5f5c8ee5 13342 }
766525bc
RS
13343}
13344
5f5c8ee5 13345/* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
1c9241f5
KH
13346 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
13347 type of CODING_SYSTEM. Return updated pointer into BUF. */
13348
6693a99a 13349static unsigned char invalid_eol_type[] = "(*invalid*)";
d24715e8 13350
1c9241f5
KH
13351static char *
13352decode_mode_spec_coding (coding_system, buf, eol_flag)
13353 Lisp_Object coding_system;
13354 register char *buf;
13355 int eol_flag;
13356{
1e1078d6 13357 Lisp_Object val;
916848d8 13358 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
302f2b38
EZ
13359 unsigned char *eol_str;
13360 int eol_str_len;
13361 /* The EOL conversion we are using. */
13362 Lisp_Object eoltype;
1e1078d6 13363
4a09dee0 13364 val = Fget (coding_system, Qcoding_system);
085536c2 13365 eoltype = Qnil;
1c9241f5 13366
4a09dee0 13367 if (!VECTORP (val)) /* Not yet decided. */
1c9241f5 13368 {
916848d8
RS
13369 if (multibyte)
13370 *buf++ = '-';
21e989e3 13371 if (eol_flag)
302f2b38 13372 eoltype = eol_mnemonic_undecided;
1e1078d6 13373 /* Don't mention EOL conversion if it isn't decided. */
1c9241f5
KH
13374 }
13375 else
13376 {
1e1078d6
RS
13377 Lisp_Object eolvalue;
13378
13379 eolvalue = Fget (coding_system, Qeol_type);
13380
916848d8 13381 if (multibyte)
a61b7058 13382 *buf++ = XFASTINT (AREF (val, 1));
916848d8 13383
1c9241f5
KH
13384 if (eol_flag)
13385 {
1e1078d6
RS
13386 /* The EOL conversion that is normal on this system. */
13387
13388 if (NILP (eolvalue)) /* Not yet decided. */
13389 eoltype = eol_mnemonic_undecided;
13390 else if (VECTORP (eolvalue)) /* Not yet decided. */
13391 eoltype = eol_mnemonic_undecided;
13392 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
13393 eoltype = (XFASTINT (eolvalue) == 0
13394 ? eol_mnemonic_unix
13395 : (XFASTINT (eolvalue) == 1
13396 ? eol_mnemonic_dos : eol_mnemonic_mac));
302f2b38
EZ
13397 }
13398 }
5f5c8ee5 13399
302f2b38
EZ
13400 if (eol_flag)
13401 {
13402 /* Mention the EOL conversion if it is not the usual one. */
13403 if (STRINGP (eoltype))
13404 {
13405 eol_str = XSTRING (eoltype)->data;
13406 eol_str_len = XSTRING (eoltype)->size;
13407 }
f30b3499
KH
13408 else if (INTEGERP (eoltype)
13409 && CHAR_VALID_P (XINT (eoltype), 0))
13410 {
4a09dee0 13411 eol_str = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
260a86a0 13412 eol_str_len = CHAR_STRING (XINT (eoltype), eol_str);
f30b3499 13413 }
302f2b38
EZ
13414 else
13415 {
13416 eol_str = invalid_eol_type;
13417 eol_str_len = sizeof (invalid_eol_type) - 1;
1c9241f5 13418 }
f30b3499 13419 bcopy (eol_str, buf, eol_str_len);
302f2b38 13420 buf += eol_str_len;
1c9241f5 13421 }
302f2b38 13422
1c9241f5
KH
13423 return buf;
13424}
13425
a2889657 13426/* Return a string for the output of a mode line %-spec for window W,
5f5c8ee5
GM
13427 generated by character C. PRECISION >= 0 means don't return a
13428 string longer than that value. FIELD_WIDTH > 0 means pad the
13429 string returned with spaces to that value. */
a2889657 13430
11e82b76
JB
13431static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
13432
a2889657 13433static char *
5f5c8ee5 13434decode_mode_spec (w, c, field_width, precision)
a2889657 13435 struct window *w;
68c45bf0 13436 register int c;
5f5c8ee5 13437 int field_width, precision;
a2889657 13438{
0b67772d 13439 Lisp_Object obj;
5f5c8ee5
GM
13440 struct frame *f = XFRAME (WINDOW_FRAME (w));
13441 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
d39b6696 13442 struct buffer *b = XBUFFER (w->buffer);
a2889657 13443
0b67772d 13444 obj = Qnil;
a2889657
JB
13445
13446 switch (c)
13447 {
1af9f229
RS
13448 case '*':
13449 if (!NILP (b->read_only))
13450 return "%";
13451 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13452 return "*";
13453 return "-";
13454
13455 case '+':
13456 /* This differs from %* only for a modified read-only buffer. */
13457 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13458 return "*";
13459 if (!NILP (b->read_only))
13460 return "%";
13461 return "-";
13462
13463 case '&':
13464 /* This differs from %* in ignoring read-only-ness. */
13465 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
13466 return "*";
13467 return "-";
13468
13469 case '%':
13470 return "%";
13471
13472 case '[':
13473 {
13474 int i;
13475 char *p;
13476
13477 if (command_loop_level > 5)
13478 return "[[[... ";
13479 p = decode_mode_spec_buf;
13480 for (i = 0; i < command_loop_level; i++)
13481 *p++ = '[';
13482 *p = 0;
13483 return decode_mode_spec_buf;
13484 }
13485
13486 case ']':
13487 {
13488 int i;
13489 char *p;
13490
13491 if (command_loop_level > 5)
13492 return " ...]]]";
13493 p = decode_mode_spec_buf;
13494 for (i = 0; i < command_loop_level; i++)
13495 *p++ = ']';
13496 *p = 0;
13497 return decode_mode_spec_buf;
13498 }
13499
13500 case '-':
13501 {
1af9f229 13502 register int i;
5f5c8ee5
GM
13503
13504 /* Let lots_of_dashes be a string of infinite length. */
13505 if (field_width <= 0
13506 || field_width > sizeof (lots_of_dashes))
1af9f229 13507 {
5f5c8ee5
GM
13508 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
13509 decode_mode_spec_buf[i] = '-';
13510 decode_mode_spec_buf[i] = '\0';
13511 return decode_mode_spec_buf;
1af9f229 13512 }
5f5c8ee5
GM
13513 else
13514 return lots_of_dashes;
1af9f229
RS
13515 }
13516
a2889657 13517 case 'b':
d39b6696 13518 obj = b->name;
a2889657
JB
13519 break;
13520
1af9f229
RS
13521 case 'c':
13522 {
13523 int col = current_column ();
ac90c44f 13524 w->column_number_displayed = make_number (col);
5f5c8ee5 13525 pint2str (decode_mode_spec_buf, field_width, col);
1af9f229
RS
13526 return decode_mode_spec_buf;
13527 }
13528
13529 case 'F':
13530 /* %F displays the frame name. */
5f5c8ee5 13531 if (!NILP (f->title))
95184b48 13532 return (char *) XSTRING (f->title)->data;
fd8ff63d 13533 if (f->explicit_name || ! FRAME_WINDOW_P (f))
95184b48 13534 return (char *) XSTRING (f->name)->data;
9c6da96f 13535 return "Emacs";
1af9f229 13536
a2889657 13537 case 'f':
d39b6696 13538 obj = b->filename;
a2889657
JB
13539 break;
13540
aa6d10fa
RS
13541 case 'l':
13542 {
12adba34
RS
13543 int startpos = XMARKER (w->start)->charpos;
13544 int startpos_byte = marker_byte_position (w->start);
13545 int line, linepos, linepos_byte, topline;
aa6d10fa 13546 int nlines, junk;
aa6d10fa
RS
13547 int height = XFASTINT (w->height);
13548
13549 /* If we decided that this buffer isn't suitable for line numbers,
13550 don't forget that too fast. */
13551 if (EQ (w->base_line_pos, w->buffer))
766525bc 13552 goto no_value;
5300fd39
RS
13553 /* But do forget it, if the window shows a different buffer now. */
13554 else if (BUFFERP (w->base_line_pos))
13555 w->base_line_pos = Qnil;
aa6d10fa
RS
13556
13557 /* If the buffer is very big, don't waste time. */
37d034d3 13558 if (INTEGERP (Vline_number_display_limit)
090703f4 13559 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
aa6d10fa
RS
13560 {
13561 w->base_line_pos = Qnil;
13562 w->base_line_number = Qnil;
766525bc 13563 goto no_value;
aa6d10fa
RS
13564 }
13565
13566 if (!NILP (w->base_line_number)
13567 && !NILP (w->base_line_pos)
12adba34 13568 && XFASTINT (w->base_line_pos) <= startpos)
aa6d10fa
RS
13569 {
13570 line = XFASTINT (w->base_line_number);
13571 linepos = XFASTINT (w->base_line_pos);
12adba34 13572 linepos_byte = buf_charpos_to_bytepos (b, linepos);
aa6d10fa
RS
13573 }
13574 else
13575 {
13576 line = 1;
d39b6696 13577 linepos = BUF_BEGV (b);
12adba34 13578 linepos_byte = BUF_BEGV_BYTE (b);
aa6d10fa
RS
13579 }
13580
13581 /* Count lines from base line to window start position. */
12adba34
RS
13582 nlines = display_count_lines (linepos, linepos_byte,
13583 startpos_byte,
13584 startpos, &junk);
aa6d10fa
RS
13585
13586 topline = nlines + line;
13587
13588 /* Determine a new base line, if the old one is too close
13589 or too far away, or if we did not have one.
13590 "Too close" means it's plausible a scroll-down would
13591 go back past it. */
d39b6696 13592 if (startpos == BUF_BEGV (b))
aa6d10fa 13593 {
ac90c44f
GM
13594 w->base_line_number = make_number (topline);
13595 w->base_line_pos = make_number (BUF_BEGV (b));
aa6d10fa
RS
13596 }
13597 else if (nlines < height + 25 || nlines > height * 3 + 50
d39b6696 13598 || linepos == BUF_BEGV (b))
aa6d10fa 13599 {
d39b6696 13600 int limit = BUF_BEGV (b);
12adba34 13601 int limit_byte = BUF_BEGV_BYTE (b);
aa6d10fa 13602 int position;
5d121aec 13603 int distance = (height * 2 + 30) * line_number_display_limit_width;
aa6d10fa
RS
13604
13605 if (startpos - distance > limit)
12adba34
RS
13606 {
13607 limit = startpos - distance;
13608 limit_byte = CHAR_TO_BYTE (limit);
13609 }
aa6d10fa 13610
12adba34
RS
13611 nlines = display_count_lines (startpos, startpos_byte,
13612 limit_byte,
13613 - (height * 2 + 30),
aa6d10fa
RS
13614 &position);
13615 /* If we couldn't find the lines we wanted within
5d121aec 13616 line_number_display_limit_width chars per line,
aa6d10fa 13617 give up on line numbers for this window. */
12adba34 13618 if (position == limit_byte && limit == startpos - distance)
aa6d10fa
RS
13619 {
13620 w->base_line_pos = w->buffer;
13621 w->base_line_number = Qnil;
766525bc 13622 goto no_value;
aa6d10fa
RS
13623 }
13624
ac90c44f
GM
13625 w->base_line_number = make_number (topline - nlines);
13626 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
aa6d10fa
RS
13627 }
13628
13629 /* Now count lines from the start pos to point. */
12adba34
RS
13630 nlines = display_count_lines (startpos, startpos_byte,
13631 PT_BYTE, PT, &junk);
aa6d10fa
RS
13632
13633 /* Record that we did display the line number. */
13634 line_number_displayed = 1;
13635
13636 /* Make the string to show. */
5f5c8ee5 13637 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
aa6d10fa 13638 return decode_mode_spec_buf;
766525bc
RS
13639 no_value:
13640 {
13641 char* p = decode_mode_spec_buf;
5f5c8ee5
GM
13642 int pad = field_width - 2;
13643 while (pad-- > 0)
13644 *p++ = ' ';
13645 *p++ = '?';
b357b9d4
KR
13646 *p++ = '?';
13647 *p = '\0';
766525bc
RS
13648 return decode_mode_spec_buf;
13649 }
aa6d10fa
RS
13650 }
13651 break;
13652
a2889657 13653 case 'm':
d39b6696 13654 obj = b->mode_name;
a2889657
JB
13655 break;
13656
13657 case 'n':
d39b6696 13658 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
a2889657
JB
13659 return " Narrow";
13660 break;
13661
a2889657
JB
13662 case 'p':
13663 {
13664 int pos = marker_position (w->start);
d39b6696 13665 int total = BUF_ZV (b) - BUF_BEGV (b);
a2889657 13666
d39b6696 13667 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
a2889657 13668 {
d39b6696 13669 if (pos <= BUF_BEGV (b))
a2889657
JB
13670 return "All";
13671 else
13672 return "Bottom";
13673 }
d39b6696 13674 else if (pos <= BUF_BEGV (b))
a2889657
JB
13675 return "Top";
13676 else
13677 {
3c7d31b9
RS
13678 if (total > 1000000)
13679 /* Do it differently for a large value, to avoid overflow. */
13680 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13681 else
13682 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
a2889657
JB
13683 /* We can't normally display a 3-digit number,
13684 so get us a 2-digit number that is close. */
13685 if (total == 100)
13686 total = 99;
13687 sprintf (decode_mode_spec_buf, "%2d%%", total);
13688 return decode_mode_spec_buf;
13689 }
13690 }
13691
8ffcb79f
RS
13692 /* Display percentage of size above the bottom of the screen. */
13693 case 'P':
13694 {
13695 int toppos = marker_position (w->start);
d39b6696
KH
13696 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
13697 int total = BUF_ZV (b) - BUF_BEGV (b);
8ffcb79f 13698
d39b6696 13699 if (botpos >= BUF_ZV (b))
8ffcb79f 13700 {
d39b6696 13701 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
13702 return "All";
13703 else
13704 return "Bottom";
13705 }
13706 else
13707 {
3c7d31b9
RS
13708 if (total > 1000000)
13709 /* Do it differently for a large value, to avoid overflow. */
13710 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
13711 else
13712 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
8ffcb79f
RS
13713 /* We can't normally display a 3-digit number,
13714 so get us a 2-digit number that is close. */
13715 if (total == 100)
13716 total = 99;
d39b6696 13717 if (toppos <= BUF_BEGV (b))
8ffcb79f
RS
13718 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
13719 else
13720 sprintf (decode_mode_spec_buf, "%2d%%", total);
13721 return decode_mode_spec_buf;
13722 }
13723 }
13724
1af9f229
RS
13725 case 's':
13726 /* status of process */
13727 obj = Fget_buffer_process (w->buffer);
13728 if (NILP (obj))
13729 return "no process";
13730#ifdef subprocesses
13731 obj = Fsymbol_name (Fprocess_status (obj));
13732#endif
13733 break;
d39b6696 13734
1af9f229
RS
13735 case 't': /* indicate TEXT or BINARY */
13736#ifdef MODE_LINE_BINARY_TEXT
13737 return MODE_LINE_BINARY_TEXT (b);
13738#else
13739 return "T";
13740#endif
1c9241f5
KH
13741
13742 case 'z':
13743 /* coding-system (not including end-of-line format) */
13744 case 'Z':
13745 /* coding-system (including end-of-line type) */
13746 {
13747 int eol_flag = (c == 'Z');
539b4d41 13748 char *p = decode_mode_spec_buf;
1c9241f5 13749
d30e754b 13750 if (! FRAME_WINDOW_P (f))
1c9241f5 13751 {
11c52c4f
RS
13752 /* No need to mention EOL here--the terminal never needs
13753 to do EOL conversion. */
13754 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
13755 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
1c9241f5 13756 }
f13c925f 13757 p = decode_mode_spec_coding (b->buffer_file_coding_system,
539b4d41 13758 p, eol_flag);
f13c925f 13759
11c52c4f 13760#if 0 /* This proves to be annoying; I think we can do without. -- rms. */
1c9241f5
KH
13761#ifdef subprocesses
13762 obj = Fget_buffer_process (Fcurrent_buffer ());
13763 if (PROCESSP (obj))
13764 {
13765 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
13766 p, eol_flag);
13767 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
13768 p, eol_flag);
13769 }
13770#endif /* subprocesses */
11c52c4f 13771#endif /* 0 */
1c9241f5
KH
13772 *p = 0;
13773 return decode_mode_spec_buf;
13774 }
a2889657 13775 }
d39b6696 13776
e24c997d 13777 if (STRINGP (obj))
a2889657
JB
13778 return (char *) XSTRING (obj)->data;
13779 else
13780 return "";
13781}
5f5c8ee5
GM
13782
13783
12adba34
RS
13784/* Count up to COUNT lines starting from START / START_BYTE.
13785 But don't go beyond LIMIT_BYTE.
13786 Return the number of lines thus found (always nonnegative).
59b49f63 13787
12adba34 13788 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
59b49f63
RS
13789
13790static int
12adba34
RS
13791display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
13792 int start, start_byte, limit_byte, count;
13793 int *byte_pos_ptr;
59b49f63 13794{
59b49f63
RS
13795 register unsigned char *cursor;
13796 unsigned char *base;
13797
13798 register int ceiling;
13799 register unsigned char *ceiling_addr;
12adba34 13800 int orig_count = count;
59b49f63
RS
13801
13802 /* If we are not in selective display mode,
13803 check only for newlines. */
12adba34
RS
13804 int selective_display = (!NILP (current_buffer->selective_display)
13805 && !INTEGERP (current_buffer->selective_display));
59b49f63
RS
13806
13807 if (count > 0)
12adba34
RS
13808 {
13809 while (start_byte < limit_byte)
13810 {
13811 ceiling = BUFFER_CEILING_OF (start_byte);
13812 ceiling = min (limit_byte - 1, ceiling);
13813 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
13814 base = (cursor = BYTE_POS_ADDR (start_byte));
13815 while (1)
13816 {
13817 if (selective_display)
13818 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
13819 ;
13820 else
13821 while (*cursor != '\n' && ++cursor != ceiling_addr)
13822 ;
13823
13824 if (cursor != ceiling_addr)
13825 {
13826 if (--count == 0)
13827 {
13828 start_byte += cursor - base + 1;
13829 *byte_pos_ptr = start_byte;
13830 return orig_count;
13831 }
13832 else
13833 if (++cursor == ceiling_addr)
13834 break;
13835 }
13836 else
13837 break;
13838 }
13839 start_byte += cursor - base;
13840 }
13841 }
59b49f63
RS
13842 else
13843 {
12adba34
RS
13844 while (start_byte > limit_byte)
13845 {
13846 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
13847 ceiling = max (limit_byte, ceiling);
13848 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
13849 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
59b49f63
RS
13850 while (1)
13851 {
12adba34
RS
13852 if (selective_display)
13853 while (--cursor != ceiling_addr
13854 && *cursor != '\n' && *cursor != 015)
13855 ;
13856 else
13857 while (--cursor != ceiling_addr && *cursor != '\n')
13858 ;
13859
59b49f63
RS
13860 if (cursor != ceiling_addr)
13861 {
13862 if (++count == 0)
13863 {
12adba34
RS
13864 start_byte += cursor - base + 1;
13865 *byte_pos_ptr = start_byte;
13866 /* When scanning backwards, we should
13867 not count the newline posterior to which we stop. */
13868 return - orig_count - 1;
59b49f63
RS
13869 }
13870 }
13871 else
13872 break;
13873 }
12adba34
RS
13874 /* Here we add 1 to compensate for the last decrement
13875 of CURSOR, which took it past the valid range. */
13876 start_byte += cursor - base + 1;
59b49f63
RS
13877 }
13878 }
13879
12adba34 13880 *byte_pos_ptr = limit_byte;
aa6d10fa 13881
12adba34
RS
13882 if (count < 0)
13883 return - orig_count + count;
13884 return orig_count - count;
aa6d10fa 13885
12adba34 13886}
a2889657 13887
a2889657 13888
5f5c8ee5
GM
13889\f
13890/***********************************************************************
13891 Displaying strings
13892 ***********************************************************************/
278feba9 13893
5f5c8ee5 13894/* Display a NUL-terminated string, starting with index START.
a3788d53 13895
5f5c8ee5
GM
13896 If STRING is non-null, display that C string. Otherwise, the Lisp
13897 string LISP_STRING is displayed.
a2889657 13898
5f5c8ee5
GM
13899 If FACE_STRING is not nil, FACE_STRING_POS is a position in
13900 FACE_STRING. Display STRING or LISP_STRING with the face at
13901 FACE_STRING_POS in FACE_STRING:
a2889657 13902
5f5c8ee5
GM
13903 Display the string in the environment given by IT, but use the
13904 standard display table, temporarily.
a3788d53 13905
5f5c8ee5
GM
13906 FIELD_WIDTH is the minimum number of output glyphs to produce.
13907 If STRING has fewer characters than FIELD_WIDTH, pad to the right
13908 with spaces. If STRING has more characters, more than FIELD_WIDTH
13909 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
13910
13911 PRECISION is the maximum number of characters to output from
13912 STRING. PRECISION < 0 means don't truncate the string.
a2889657 13913
5f5c8ee5 13914 This is roughly equivalent to printf format specifiers:
a2889657 13915
5f5c8ee5
GM
13916 FIELD_WIDTH PRECISION PRINTF
13917 ----------------------------------------
13918 -1 -1 %s
13919 -1 10 %.10s
13920 10 -1 %10s
13921 20 10 %20.10s
a2889657 13922
5f5c8ee5
GM
13923 MULTIBYTE zero means do not display multibyte chars, > 0 means do
13924 display them, and < 0 means obey the current buffer's value of
13925 enable_multibyte_characters.
278feba9 13926
5f5c8ee5 13927 Value is the number of glyphs produced. */
b1d1124b 13928
5f5c8ee5
GM
13929static int
13930display_string (string, lisp_string, face_string, face_string_pos,
13931 start, it, field_width, precision, max_x, multibyte)
13932 unsigned char *string;
13933 Lisp_Object lisp_string;
68c45bf0
PE
13934 Lisp_Object face_string;
13935 int face_string_pos;
5f5c8ee5
GM
13936 int start;
13937 struct it *it;
13938 int field_width, precision, max_x;
13939 int multibyte;
13940{
13941 int hpos_at_start = it->hpos;
13942 int saved_face_id = it->face_id;
13943 struct glyph_row *row = it->glyph_row;
13944
13945 /* Initialize the iterator IT for iteration over STRING beginning
13946 with index START. We assume that IT may be modified here (which
13947 means that display_line has to do something when displaying a
13948 mini-buffer prompt, which it does). */
13949 reseat_to_string (it, string, lisp_string, start,
13950 precision, field_width, multibyte);
13951
13952 /* If displaying STRING, set up the face of the iterator
13953 from LISP_STRING, if that's given. */
13954 if (STRINGP (face_string))
13955 {
13956 int endptr;
13957 struct face *face;
13958
13959 it->face_id
13960 = face_at_string_position (it->w, face_string, face_string_pos,
13961 0, it->region_beg_charpos,
13962 it->region_end_charpos,
5de7c6f2 13963 &endptr, it->base_face_id, 0);
5f5c8ee5
GM
13964 face = FACE_FROM_ID (it->f, it->face_id);
13965 it->face_box_p = face->box != FACE_NO_BOX;
b1d1124b 13966 }
a2889657 13967
5f5c8ee5
GM
13968 /* Set max_x to the maximum allowed X position. Don't let it go
13969 beyond the right edge of the window. */
13970 if (max_x <= 0)
13971 max_x = it->last_visible_x;
13972 else
13973 max_x = min (max_x, it->last_visible_x);
efc63ef0 13974
5f5c8ee5
GM
13975 /* Skip over display elements that are not visible. because IT->w is
13976 hscrolled. */
13977 if (it->current_x < it->first_visible_x)
13978 move_it_in_display_line_to (it, 100000, it->first_visible_x,
13979 MOVE_TO_POS | MOVE_TO_X);
a2889657 13980
5f5c8ee5
GM
13981 row->ascent = it->max_ascent;
13982 row->height = it->max_ascent + it->max_descent;
312246d1
GM
13983 row->phys_ascent = it->max_phys_ascent;
13984 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
1c9241f5 13985
5f5c8ee5
GM
13986 /* This condition is for the case that we are called with current_x
13987 past last_visible_x. */
13988 while (it->current_x < max_x)
a2889657 13989 {
5f5c8ee5 13990 int x_before, x, n_glyphs_before, i, nglyphs;
1c9241f5 13991
5f5c8ee5
GM
13992 /* Get the next display element. */
13993 if (!get_next_display_element (it))
90adcf20 13994 break;
1c9241f5 13995
5f5c8ee5
GM
13996 /* Produce glyphs. */
13997 x_before = it->current_x;
13998 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
13999 PRODUCE_GLYPHS (it);
90adcf20 14000
5f5c8ee5
GM
14001 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
14002 i = 0;
14003 x = x_before;
14004 while (i < nglyphs)
a2889657 14005 {
5f5c8ee5
GM
14006 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14007
14008 if (!it->truncate_lines_p
14009 && x + glyph->pixel_width > max_x)
14010 {
14011 /* End of continued line or max_x reached. */
37be86f2
KH
14012 if (CHAR_GLYPH_PADDING_P (*glyph))
14013 {
14014 /* A wide character is unbreakable. */
14015 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
14016 it->current_x = x_before;
14017 }
14018 else
14019 {
14020 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
14021 it->current_x = x;
14022 }
5f5c8ee5
GM
14023 break;
14024 }
14025 else if (x + glyph->pixel_width > it->first_visible_x)
14026 {
14027 /* Glyph is at least partially visible. */
14028 ++it->hpos;
14029 if (x < it->first_visible_x)
14030 it->glyph_row->x = x - it->first_visible_x;
14031 }
14032 else
a2889657 14033 {
5f5c8ee5
GM
14034 /* Glyph is off the left margin of the display area.
14035 Should not happen. */
14036 abort ();
a2889657 14037 }
5f5c8ee5
GM
14038
14039 row->ascent = max (row->ascent, it->max_ascent);
14040 row->height = max (row->height, it->max_ascent + it->max_descent);
312246d1
GM
14041 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14042 row->phys_height = max (row->phys_height,
14043 it->max_phys_ascent + it->max_phys_descent);
5f5c8ee5
GM
14044 x += glyph->pixel_width;
14045 ++i;
a2889657 14046 }
5f5c8ee5
GM
14047
14048 /* Stop if max_x reached. */
14049 if (i < nglyphs)
14050 break;
14051
14052 /* Stop at line ends. */
14053 if (ITERATOR_AT_END_OF_LINE_P (it))
a2889657 14054 {
5f5c8ee5
GM
14055 it->continuation_lines_width = 0;
14056 break;
a2889657 14057 }
1c9241f5 14058
cafafe0b 14059 set_iterator_to_next (it, 1);
a688bb24 14060
5f5c8ee5
GM
14061 /* Stop if truncating at the right edge. */
14062 if (it->truncate_lines_p
14063 && it->current_x >= it->last_visible_x)
14064 {
14065 /* Add truncation mark, but don't do it if the line is
14066 truncated at a padding space. */
14067 if (IT_CHARPOS (*it) < it->string_nchars)
1c9241f5 14068 {
5f5c8ee5 14069 if (!FRAME_WINDOW_P (it->f))
37be86f2
KH
14070 {
14071 int i, n;
14072
9299cb15 14073 if (it->current_x > it->last_visible_x)
37be86f2 14074 {
9299cb15
KH
14075 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
14076 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
14077 break;
14078 for (n = row->used[TEXT_AREA]; i < n; ++i)
14079 {
14080 row->used[TEXT_AREA] = i;
14081 produce_special_glyphs (it, IT_TRUNCATION);
14082 }
37be86f2 14083 }
9299cb15 14084 produce_special_glyphs (it, IT_TRUNCATION);
37be86f2 14085 }
5f5c8ee5 14086 it->glyph_row->truncated_on_right_p = 1;
1c9241f5 14087 }
5f5c8ee5 14088 break;
1c9241f5 14089 }
a2889657
JB
14090 }
14091
5f5c8ee5
GM
14092 /* Maybe insert a truncation at the left. */
14093 if (it->first_visible_x
14094 && IT_CHARPOS (*it) > 0)
a2889657 14095 {
5f5c8ee5
GM
14096 if (!FRAME_WINDOW_P (it->f))
14097 insert_left_trunc_glyphs (it);
14098 it->glyph_row->truncated_on_left_p = 1;
a2889657
JB
14099 }
14100
5f5c8ee5
GM
14101 it->face_id = saved_face_id;
14102
14103 /* Value is number of columns displayed. */
14104 return it->hpos - hpos_at_start;
14105}
a2889657 14106
a2889657 14107
a2889657 14108\f
5f5c8ee5
GM
14109/* This is like a combination of memq and assq. Return 1 if PROPVAL
14110 appears as an element of LIST or as the car of an element of LIST.
14111 If PROPVAL is a list, compare each element against LIST in that
14112 way, and return 1 if any element of PROPVAL is found in LIST.
14113 Otherwise return 0. This function cannot quit. */
642eefc6
RS
14114
14115int
14116invisible_p (propval, list)
14117 register Lisp_Object propval;
14118 Lisp_Object list;
14119{
af460d46 14120 register Lisp_Object tail, proptail;
715e84c9 14121
9472f927 14122 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
14123 {
14124 register Lisp_Object tem;
9472f927 14125 tem = XCAR (tail);
642eefc6
RS
14126 if (EQ (propval, tem))
14127 return 1;
9472f927 14128 if (CONSP (tem) && EQ (propval, XCAR (tem)))
642eefc6
RS
14129 return 1;
14130 }
715e84c9 14131
af460d46 14132 if (CONSP (propval))
715e84c9
GM
14133 {
14134 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
14135 {
14136 Lisp_Object propelt;
14137 propelt = XCAR (proptail);
14138 for (tail = list; CONSP (tail); tail = XCDR (tail))
14139 {
14140 register Lisp_Object tem;
14141 tem = XCAR (tail);
14142 if (EQ (propelt, tem))
14143 return 1;
14144 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14145 return 1;
14146 }
14147 }
14148 }
14149
642eefc6
RS
14150 return 0;
14151}
14152
5f5c8ee5
GM
14153
14154/* Return 1 if PROPVAL appears as the car of an element of LIST and
14155 the cdr of that element is non-nil. If PROPVAL is a list, check
14156 each element of PROPVAL in that way, and the first time some
14157 element is found, return 1 if the cdr of that element is non-nil.
14158 Otherwise return 0. This function cannot quit. */
642eefc6
RS
14159
14160int
14161invisible_ellipsis_p (propval, list)
14162 register Lisp_Object propval;
14163 Lisp_Object list;
14164{
af460d46 14165 register Lisp_Object tail, proptail;
9472f927
GM
14166
14167 for (tail = list; CONSP (tail); tail = XCDR (tail))
642eefc6
RS
14168 {
14169 register Lisp_Object tem;
9472f927
GM
14170 tem = XCAR (tail);
14171 if (CONSP (tem) && EQ (propval, XCAR (tem)))
14172 return ! NILP (XCDR (tem));
642eefc6 14173 }
9472f927 14174
af460d46 14175 if (CONSP (propval))
9472f927 14176 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
af460d46
RS
14177 {
14178 Lisp_Object propelt;
9472f927
GM
14179 propelt = XCAR (proptail);
14180 for (tail = list; CONSP (tail); tail = XCDR (tail))
af460d46
RS
14181 {
14182 register Lisp_Object tem;
9472f927
GM
14183 tem = XCAR (tail);
14184 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
14185 return ! NILP (XCDR (tem));
af460d46
RS
14186 }
14187 }
9472f927 14188
642eefc6
RS
14189 return 0;
14190}
5f5c8ee5
GM
14191
14192
642eefc6 14193\f
5f5c8ee5
GM
14194/***********************************************************************
14195 Initialization
14196 ***********************************************************************/
14197
a2889657
JB
14198void
14199syms_of_xdisp ()
14200{
c6e89d6c
GM
14201 Vwith_echo_area_save_vector = Qnil;
14202 staticpro (&Vwith_echo_area_save_vector);
5f5c8ee5 14203
c6e89d6c
GM
14204 Vmessage_stack = Qnil;
14205 staticpro (&Vmessage_stack);
14206
735c094c 14207 Qinhibit_redisplay = intern ("inhibit-redisplay");
c6e89d6c 14208 staticpro (&Qinhibit_redisplay);
735c094c 14209
5f5c8ee5
GM
14210#if GLYPH_DEBUG
14211 defsubr (&Sdump_glyph_matrix);
14212 defsubr (&Sdump_glyph_row);
e037b9ec 14213 defsubr (&Sdump_tool_bar_row);
5f5c8ee5 14214 defsubr (&Strace_redisplay_toggle);
bf9249e3 14215 defsubr (&Strace_to_stderr);
5f5c8ee5 14216#endif
99a5de87 14217#ifdef HAVE_WINDOW_SYSTEM
57c28064 14218 defsubr (&Stool_bar_lines_needed);
99a5de87 14219#endif
5f5c8ee5 14220
cf074754
RS
14221 staticpro (&Qmenu_bar_update_hook);
14222 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
14223
d46fb96a 14224 staticpro (&Qoverriding_terminal_local_map);
7079aefa 14225 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
d46fb96a 14226
399164b4
KH
14227 staticpro (&Qoverriding_local_map);
14228 Qoverriding_local_map = intern ("overriding-local-map");
14229
75c43375
RS
14230 staticpro (&Qwindow_scroll_functions);
14231 Qwindow_scroll_functions = intern ("window-scroll-functions");
14232
e0bfbde6
RS
14233 staticpro (&Qredisplay_end_trigger_functions);
14234 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
5f5c8ee5 14235
2e54982e
RS
14236 staticpro (&Qinhibit_point_motion_hooks);
14237 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
14238
9499d71b
GM
14239 QCdata = intern (":data");
14240 staticpro (&QCdata);
5f5c8ee5 14241 Qdisplay = intern ("display");
f3751a65 14242 staticpro (&Qdisplay);
5f5c8ee5
GM
14243 Qspace_width = intern ("space-width");
14244 staticpro (&Qspace_width);
5f5c8ee5
GM
14245 Qraise = intern ("raise");
14246 staticpro (&Qraise);
14247 Qspace = intern ("space");
14248 staticpro (&Qspace);
f3751a65
GM
14249 Qmargin = intern ("margin");
14250 staticpro (&Qmargin);
5f5c8ee5 14251 Qleft_margin = intern ("left-margin");
f3751a65 14252 staticpro (&Qleft_margin);
5f5c8ee5 14253 Qright_margin = intern ("right-margin");
f3751a65 14254 staticpro (&Qright_margin);
5f5c8ee5
GM
14255 Qalign_to = intern ("align-to");
14256 staticpro (&Qalign_to);
14257 QCalign_to = intern (":align-to");
14258 staticpro (&QCalign_to);
5f5c8ee5
GM
14259 Qrelative_width = intern ("relative-width");
14260 staticpro (&Qrelative_width);
14261 QCrelative_width = intern (":relative-width");
14262 staticpro (&QCrelative_width);
14263 QCrelative_height = intern (":relative-height");
14264 staticpro (&QCrelative_height);
14265 QCeval = intern (":eval");
14266 staticpro (&QCeval);
d3acf96b 14267 Qwhen = intern ("when");
f3751a65 14268 staticpro (&Qwhen);
886bd6f2
GM
14269 QCfile = intern (":file");
14270 staticpro (&QCfile);
5f5c8ee5
GM
14271 Qfontified = intern ("fontified");
14272 staticpro (&Qfontified);
14273 Qfontification_functions = intern ("fontification-functions");
14274 staticpro (&Qfontification_functions);
5f5c8ee5
GM
14275 Qtrailing_whitespace = intern ("trailing-whitespace");
14276 staticpro (&Qtrailing_whitespace);
14277 Qimage = intern ("image");
14278 staticpro (&Qimage);
ad4f174e
GM
14279 Qmessage_truncate_lines = intern ("message-truncate-lines");
14280 staticpro (&Qmessage_truncate_lines);
6422c1d7
GM
14281 Qgrow_only = intern ("grow-only");
14282 staticpro (&Qgrow_only);
e1477f43
GM
14283 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
14284 staticpro (&Qinhibit_menubar_update);
5f5c8ee5 14285
a2889657
JB
14286 last_arrow_position = Qnil;
14287 last_arrow_string = Qnil;
f3751a65
GM
14288 staticpro (&last_arrow_position);
14289 staticpro (&last_arrow_string);
c6e89d6c
GM
14290
14291 echo_buffer[0] = echo_buffer[1] = Qnil;
14292 staticpro (&echo_buffer[0]);
14293 staticpro (&echo_buffer[1]);
14294
14295 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
14296 staticpro (&echo_area_buffer[0]);
14297 staticpro (&echo_area_buffer[1]);
a2889657 14298
6a94510a
GM
14299 Vmessages_buffer_name = build_string ("*Messages*");
14300 staticpro (&Vmessages_buffer_name);
14301
8f897821
GM
14302 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
14303 "Non-nil means highlight trailing whitespace.\n\
14304The face used for trailing whitespace is `trailing-whitespace'.");
14305 Vshow_trailing_whitespace = Qnil;
14306
735c094c
KH
14307 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
14308 "Non-nil means don't actually do any redisplay.\n\
14309This is used for internal purposes.");
14310 Vinhibit_redisplay = Qnil;
14311
a2889657 14312 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
8c45d522 14313 "String (or mode line construct) included (normally) in `mode-line-format'.");
a2889657
JB
14314 Vglobal_mode_string = Qnil;
14315
14316 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
14317 "Marker for where to display an arrow on top of the buffer text.\n\
14318This must be the beginning of a line in order to work.\n\
14319See also `overlay-arrow-string'.");
14320 Voverlay_arrow_position = Qnil;
14321
14322 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
14323 "String to display as an arrow. See also `overlay-arrow-position'.");
14324 Voverlay_arrow_string = Qnil;
14325
14326 DEFVAR_INT ("scroll-step", &scroll_step,
14327 "*The number of lines to try scrolling a window by when point moves out.\n\
44fa5b1e 14328If that fails to bring point back on frame, point is centered instead.\n\
0894e696
SM
14329If this is zero, point is always centered after it moves off frame.\n\
14330If you want scrolling to always be a line at a time, you should set\n\
14331 `scroll-conservatively' to a large value rather than set this to 1.");
a2889657 14332
0789adb2 14333 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
090f7baa
GM
14334 "*Scroll up to this many lines, to bring point back on screen.\n\
14335A value of zero means to scroll the text to center point vertically\n\
14336in the window.");
0789adb2
RS
14337 scroll_conservatively = 0;
14338
9afd2168
RS
14339 DEFVAR_INT ("scroll-margin", &scroll_margin,
14340 "*Number of lines of margin at the top and bottom of a window.\n\
14341Recenter the window whenever point gets within this many lines\n\
14342of the top or bottom of the window.");
14343 scroll_margin = 0;
14344
5f5c8ee5 14345#if GLYPH_DEBUG
a2889657 14346 DEFVAR_INT ("debug-end-pos", &debug_end_pos, "Don't ask");
5f5c8ee5 14347#endif
a2889657
JB
14348
14349 DEFVAR_BOOL ("truncate-partial-width-windows",
14350 &truncate_partial_width_windows,
44fa5b1e 14351 "*Non-nil means truncate lines in all windows less than full frame wide.");
a2889657
JB
14352 truncate_partial_width_windows = 1;
14353
14354 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
1862a24e
MB
14355 "nil means display the mode-line/header-line/menu-bar in the default face.\n\
14356Any other value means to use the appropriate face, `mode-line',\n\
14357`header-line', or `menu' respectively.\n\
1b6022cf 14358\n\
1862a24e
MB
14359This variable is deprecated; please change the above faces instead.");
14360 mode_line_inverse_video = 1;
aa6d10fa 14361
090703f4 14362 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
5f5c8ee5 14363 "*Maximum buffer size for which line number should be displayed.\n\
db4f2bfa 14364If the buffer is bigger than this, the line number does not appear\n\
090703f4
GM
14365in the mode line. A value of nil means no limit.");
14366 Vline_number_display_limit = Qnil;
fba9ce76 14367
090703f4
GM
14368 DEFVAR_INT ("line-number-display-limit-width",
14369 &line_number_display_limit_width,
5d121aec
KH
14370 "*Maximum line width (in characters) for line number display.\n\
14371If the average length of the lines near point is bigger than this, then the\n\
14372line number may be omitted from the mode line.");
14373 line_number_display_limit_width = 200;
14374
fba9ce76
RS
14375 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
14376 "*Non-nil means highlight region even in nonselected windows.");
293a54ce 14377 highlight_nonselected_windows = 0;
d39b6696
KH
14378
14379 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
3450d04c
KH
14380 "Non-nil if more than one frame is visible on this display.\n\
14381Minibuffer-only frames don't count, but iconified frames do.\n\
4c2eb242
RS
14382This variable is not guaranteed to be accurate except while processing\n\
14383`frame-title-format' and `icon-title-format'.");
d39b6696
KH
14384
14385 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
5f5c8ee5 14386 "Template for displaying the title bar of visible frames.\n\
d39b6696
KH
14387\(Assuming the window manager supports this feature.)\n\
14388This variable has the same structure as `mode-line-format' (which see),\n\
14389and is used only on frames for which no explicit name has been set\n\
14390\(see `modify-frame-parameters').");
14391 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
5f5c8ee5 14392 "Template for displaying the title bar of an iconified frame.\n\
d39b6696
KH
14393\(Assuming the window manager supports this feature.)\n\
14394This variable has the same structure as `mode-line-format' (which see),\n\
14395and is used only on frames for which no explicit name has been set\n\
14396\(see `modify-frame-parameters').");
14397 Vicon_title_format
14398 = Vframe_title_format
14399 = Fcons (intern ("multiple-frames"),
14400 Fcons (build_string ("%b"),
14401 Fcons (Fcons (build_string (""),
14402 Fcons (intern ("invocation-name"),
14403 Fcons (build_string ("@"),
14404 Fcons (intern ("system-name"),
14405 Qnil)))),
14406 Qnil)));
5992c4f7
KH
14407
14408 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
14409 "Maximum number of lines to keep in the message log buffer.\n\
14410If nil, disable message logging. If t, log messages but don't truncate\n\
14411the buffer when it becomes large.");
ac90c44f 14412 Vmessage_log_max = make_number (50);
08b610e4
RS
14413
14414 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
14415 "Functions called before redisplay, if window sizes have changed.\n\
14416The value should be a list of functions that take one argument.\n\
14417Just before redisplay, for each frame, if any of its windows have changed\n\
14418size since the last redisplay, or have been split or deleted,\n\
14419all the functions in the list are called, with the frame as argument.");
14420 Vwindow_size_change_functions = Qnil;
75c43375
RS
14421
14422 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
5f5c8ee5 14423 "List of Functions to call before redisplaying a window with scrolling.\n\
75c43375 14424Each function is called with two arguments, the window\n\
8d9583b0
RS
14425and its new display-start position. Note that the value of `window-end'\n\
14426is not valid when these functions are called.");
75c43375 14427 Vwindow_scroll_functions = Qnil;
5f5c8ee5 14428
e037b9ec
GM
14429 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
14430 "*Non-nil means automatically resize tool-bars.\n\
14431This increases a tool-bar's height if not all tool-bar items are visible.\n\
14432It decreases a tool-bar's height when it would display blank lines\n\
5f5c8ee5 14433otherwise.");
e037b9ec 14434 auto_resize_tool_bars_p = 1;
5f5c8ee5 14435
e037b9ec
GM
14436 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
14437 "*Non-nil means raise tool-bar buttons when the mouse moves over them.");
14438 auto_raise_tool_bar_buttons_p = 1;
5f5c8ee5 14439
35a41507
GM
14440 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
14441 "*Margin around tool-bar buttons in pixels.\n\
14442If an integer, use that for both horizontal and vertical margins.\n\
14443Otherwise, value should be a pair of integers `(HORZ : VERT)' with\n\
14444HORZ specifying the horizontal margin, and VERT specifying the\n\
14445vertical margin.");
c3d76173 14446 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
5f5c8ee5 14447
e037b9ec
GM
14448 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
14449 "Relief thickness of tool-bar buttons.");
c3d76173 14450 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
5f5c8ee5
GM
14451
14452 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
14453 "List of functions to call to fontify regions of text.\n\
14454Each function is called with one argument POS. Functions must\n\
14455fontify a region starting at POS in the current buffer, and give\n\
14456fontified regions the property `fontified'.\n\
14457This variable automatically becomes buffer-local when set.");
14458 Vfontification_functions = Qnil;
6b9f0906 14459 Fmake_variable_buffer_local (Qfontification_functions);
7bbe686f
AI
14460
14461 DEFVAR_BOOL ("unibyte-display-via-language-environment",
5f5c8ee5
GM
14462 &unibyte_display_via_language_environment,
14463 "*Non-nil means display unibyte text according to language environment.\n\
7bbe686f
AI
14464Specifically this means that unibyte non-ASCII characters\n\
14465are displayed by converting them to the equivalent multibyte characters\n\
14466according to the current language environment. As a result, they are\n\
14467displayed according to the current fontset.");
14468 unibyte_display_via_language_environment = 0;
c6e89d6c
GM
14469
14470 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
14471 "*Maximum height for resizing mini-windows.\n\
14472If a float, it specifies a fraction of the mini-window frame's height.\n\
6422c1d7 14473If an integer, it specifies a number of lines.");
c6e89d6c 14474 Vmax_mini_window_height = make_float (0.25);
6422c1d7
GM
14475
14476 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
504454e8 14477 "*How to resize mini-windows.\n\
6422c1d7 14478A value of nil means don't automatically resize mini-windows.\n\
504454e8 14479A value of t means resize them to fit the text displayed in them.\n\
6422c1d7 14480A value of `grow-only', the default, means let mini-windows grow\n\
504454e8
GM
14481only, until their display becomes empty, at which point the windows\n\
14482go back to their normal size.");
6422c1d7
GM
14483 Vresize_mini_windows = Qgrow_only;
14484
d6d26ed3 14485 DEFVAR_BOOL ("cursor-in-non-selected-windows",
f8156156 14486 &cursor_in_non_selected_windows,
d6d26ed3
GM
14487 "*Non-nil means display a hollow cursor in non-selected windows.\n\
14488Nil means don't display a cursor there.");
14489 cursor_in_non_selected_windows = 1;
d475bcb8
GM
14490
14491 DEFVAR_BOOL ("automatic-hscrolling", &automatic_hscrolling_p,
14492 "*Non-nil means scroll the display automatically to make point visible.");
14493 automatic_hscrolling_p = 1;
e00daaa0
GM
14494
14495 DEFVAR_LISP ("image-types", &Vimage_types,
ad4f174e 14496 "List of supported image types.\n\
e00daaa0
GM
14497Each element of the list is a symbol for a supported image type.");
14498 Vimage_types = Qnil;
ad4f174e
GM
14499
14500 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
14501 "If non-nil, messages are truncated instead of resizing the echo area.\n\
14502Bind this around calls to `message' to let it take effect.");
14503 message_truncate_lines = 0;
0bca8940
DL
14504
14505 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
14506 "Normal hook run for clicks on menu bar, before displaying a submenu.\n\
14507Can be used to update submenus whose contents should vary.");
6422c1d7 14508 Vmenu_bar_update_hook = Qnil;
e1477f43
GM
14509
14510 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
14511 "Non-nil means don't update menu bars. Internal use only.");
14512 inhibit_menubar_update = 0;
a2889657
JB
14513}
14514
5f5c8ee5
GM
14515
14516/* Initialize this module when Emacs starts. */
14517
dfcf069d 14518void
a2889657
JB
14519init_xdisp ()
14520{
14521 Lisp_Object root_window;
5f5c8ee5 14522 struct window *mini_w;
a2889657 14523
04612a64
GM
14524 current_header_line_height = current_mode_line_height = -1;
14525
5f5c8ee5 14526 CHARPOS (this_line_start_pos) = 0;
a2889657
JB
14527
14528 mini_w = XWINDOW (minibuf_window);
11e82b76 14529 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
a2889657 14530
a2889657
JB
14531 if (!noninteractive)
14532 {
5f5c8ee5
GM
14533 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
14534 int i;
14535
ac90c44f 14536 XWINDOW (root_window)->top = make_number (FRAME_TOP_MARGIN (f));
12c226c5 14537 set_window_height (root_window,
5f5c8ee5 14538 FRAME_HEIGHT (f) - 1 - FRAME_TOP_MARGIN (f),
12c226c5 14539 0);
ac90c44f 14540 mini_w->top = make_number (FRAME_HEIGHT (f) - 1);
a2889657
JB
14541 set_window_height (minibuf_window, 1, 0);
14542
ac90c44f
GM
14543 XWINDOW (root_window)->width = make_number (FRAME_WIDTH (f));
14544 mini_w->width = make_number (FRAME_WIDTH (f));
5f5c8ee5
GM
14545
14546 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
14547 scratch_glyph_row.glyphs[TEXT_AREA + 1]
14548 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
14549
14550 /* The default ellipsis glyphs `...'. */
14551 for (i = 0; i < 3; ++i)
ac90c44f 14552 default_invis_vector[i] = make_number ('.');
a2889657 14553 }
5f5c8ee5
GM
14554
14555#ifdef HAVE_WINDOW_SYSTEM
14556 {
14557 /* Allocate the buffer for frame titles. */
14558 int size = 100;
14559 frame_title_buf = (char *) xmalloc (size);
14560 frame_title_buf_end = frame_title_buf + size;
14561 frame_title_ptr = NULL;
14562 }
14563#endif /* HAVE_WINDOW_SYSTEM */
21fdfb65
GM
14564
14565 help_echo_showing_p = 0;
a2889657 14566}
5f5c8ee5
GM
14567
14568