Merge changes from emacs-23 branch.
[bpt/emacs.git] / src / dispnew.c
CommitLineData
4588ec20 1/* Updating of data structures for redisplay.
0b5538bd
TTN
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
114f9c96 4 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4588ec20
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
4588ec20 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
4588ec20
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
4588ec20 20
18160b98 21#include <config.h>
68c45bf0 22#include <signal.h>
565620a5 23#include <stdio.h>
4588ec20 24#include <ctype.h>
d7306fe6 25#include <setjmp.h>
4588ec20 26
dfcf069d
AS
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30
47099d6f 31#include "lisp.h"
4588ec20
JB
32#include "termchar.h"
33#include "termopts.h"
a0879520 34/* cm.h must come after dispextern.h on Windows. */
fd2e066a
GV
35#include "dispextern.h"
36#include "cm.h"
4588ec20 37#include "buffer.h"
83be827a 38#include "character.h"
d43721a2 39#include "keyboard.h"
502b9b64 40#include "frame.h"
428a555e 41#include "termhooks.h"
4588ec20
JB
42#include "window.h"
43#include "commands.h"
44#include "disptab.h"
45#include "indent.h"
d169fe39 46#include "intervals.h"
97cf50e7 47#include "blockinput.h"
dfcf069d 48#include "process.h"
4588ec20 49
58b2bb63 50#include "syssignal.h"
a41f8bed 51
4588ec20
JB
52#ifdef HAVE_X_WINDOWS
53#include "xterm.h"
5f5c8ee5 54#endif /* HAVE_X_WINDOWS */
4588ec20 55
fd2e066a
GV
56#ifdef HAVE_NTGUI
57#include "w32term.h"
58#endif /* HAVE_NTGUI */
59
edfda783
AR
60#ifdef HAVE_NS
61#include "nsterm.h"
62#endif
63
5f5c8ee5 64/* Include systime.h after xterm.h to avoid double inclusion of time.h. */
6cbd1643 65
5f5c8ee5 66#include "systime.h"
3883a901
RS
67#include <errno.h>
68
4588ec20 69/* Get number of chars of output now in the buffer of a stdio stream.
5f5c8ee5
GM
70 This ought to be built in in stdio, but it isn't. Some s- files
71 override this because their stdio internals differ. */
72
e3271ae5 73#ifdef __GNU_LIBRARY__
5f5c8ee5
GM
74
75/* The s- file might have overridden the definition with one that
76 works for the system's C library. But we are using the GNU C
77 library, so this is the right definition for every system. */
78
3883a901
RS
79#ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
80#define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
81#else
cb5558ff 82#undef PENDING_OUTPUT_COUNT
e3271ae5 83#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
3883a901
RS
84#endif
85#else /* not __GNU_LIBRARY__ */
68c45bf0
PE
86#if !defined (PENDING_OUTPUT_COUNT) && HAVE_STDIO_EXT_H && HAVE___FPENDING
87#include <stdio_ext.h>
88#define PENDING_OUTPUT_COUNT(FILE) __fpending (FILE)
89#endif
cb5558ff 90#ifndef PENDING_OUTPUT_COUNT
4588ec20
JB
91#define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
92#endif
5f5c8ee5
GM
93#endif /* not __GNU_LIBRARY__ */
94
8a2a6032 95#if defined(HAVE_TERM_H) && defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
fa0ec9f4
DL
96#include <term.h> /* for tgetent */
97#endif
5f5c8ee5
GM
98\f
99/* Structure to pass dimensions around. Used for character bounding
100 boxes, glyph matrix dimensions and alike. */
101
102struct dim
103{
104 int width;
105 int height;
106};
107
108\f
109/* Function prototypes. */
110
f57e2426
J
111static struct glyph_matrix *save_current_matrix (struct frame *);
112static void restore_current_matrix (struct frame *, struct glyph_matrix *);
113static int showing_window_margins_p (struct window *);
114static void fake_current_matrices (Lisp_Object);
115static void redraw_overlapping_rows (struct window *, int);
116static void redraw_overlapped_rows (struct window *, int);
117static int count_blanks (struct glyph *, int);
118static int count_match (struct glyph *, struct glyph *,
119 struct glyph *, struct glyph *);
120static unsigned line_draw_cost (struct glyph_matrix *, int);
121static void update_frame_line (struct frame *, int);
5f5c8ee5 122static struct dim allocate_matrices_for_frame_redisplay
f57e2426
J
123 (Lisp_Object, int, int, int, int *);
124static void allocate_matrices_for_window_redisplay (struct window *);
125static int realloc_glyph_pool (struct glyph_pool *, struct dim);
126static void adjust_frame_glyphs (struct frame *);
127struct glyph_matrix *new_glyph_matrix (struct glyph_pool *);
128static void free_glyph_matrix (struct glyph_matrix *);
129static void adjust_glyph_matrix (struct window *, struct glyph_matrix *,
130 int, int, struct dim);
131static void change_frame_size_1 (struct frame *, int, int, int, int, int);
132static void swap_glyph_pointers (struct glyph_row *, struct glyph_row *);
f1d2ce7f 133#if GLYPH_DEBUG
f57e2426 134static int glyph_row_slice_p (struct glyph_row *, struct glyph_row *);
9339e87f 135#endif
f57e2426
J
136static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
137static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
138 struct window *);
139static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
140 struct window *);
141static struct glyph_pool *new_glyph_pool (void);
142static void free_glyph_pool (struct glyph_pool *);
143static void adjust_frame_glyphs_initially (void);
144static void adjust_frame_message_buffer (struct frame *);
145static void adjust_decode_mode_spec_buffer (struct frame *);
146static void fill_up_glyph_row_with_spaces (struct glyph_row *);
147static void build_frame_matrix (struct frame *);
148void clear_current_matrices (struct frame *);
149void scroll_glyph_matrix_range (struct glyph_matrix *, int, int,
150 int, int);
151static void clear_window_matrices (struct window *, int);
152static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
153static int scrolling_window (struct window *, int);
154static int update_window_line (struct window *, int, int *);
155static void update_marginal_area (struct window *, int, int);
156static int update_text_area (struct window *, int);
157static void make_current (struct glyph_matrix *, struct glyph_matrix *,
158 int);
159static void mirror_make_current (struct window *, int);
160void check_window_matrix_pointers (struct window *);
b96fd3e8 161#if GLYPH_DEBUG
f57e2426
J
162static void check_matrix_pointers (struct glyph_matrix *,
163 struct glyph_matrix *);
b96fd3e8 164#endif
f57e2426
J
165static void mirror_line_dance (struct window *, int, int, int *, char *);
166static int update_window_tree (struct window *, int);
167static int update_window (struct window *, int);
168static int update_frame_1 (struct frame *, int, int);
169static void set_window_cursor_after_update (struct window *);
170static int row_equal_p (struct window *, struct glyph_row *,
171 struct glyph_row *, int);
172static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
173static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
174static void reverse_rows (struct glyph_matrix *, int, int);
175static int margin_glyphs_to_reserve (struct window *, int, Lisp_Object);
176static void sync_window_with_frame_matrix_rows (struct window *);
177struct window *frame_row_to_window (struct window *, int);
5f5c8ee5 178
5f5c8ee5
GM
179\f
180/* Non-zero means don't pause redisplay for pending input. (This is
181 for debugging and for a future implementation of EDT-like
182 scrolling. */
4588ec20 183
5f5c8ee5 184int redisplay_dont_pause;
45140e01 185
d012c62b
KS
186/* Define PERIODIC_PREEMPTION_CHECKING to 1, if micro-second timers
187 are supported, so we can check for input during redisplay at
188 regular intervals. */
189#ifdef EMACS_HAS_USECS
190#define PERIODIC_PREEMPTION_CHECKING 1
191#else
192#define PERIODIC_PREEMPTION_CHECKING 0
193#endif
194
195#if PERIODIC_PREEMPTION_CHECKING
196
197/* If a number (float), check for user input every N seconds. */
198
199Lisp_Object Vredisplay_preemption_period;
200
201/* Redisplay preemption timers. */
202
203static EMACS_TIME preemption_period;
204static EMACS_TIME preemption_next_check;
205
206#endif
207
a41f8bed 208/* Nonzero upon entry to redisplay means do not assume anything about
502b9b64 209 current contents of actual terminal frame; clear and redraw it. */
4588ec20 210
502b9b64 211int frame_garbaged;
4588ec20 212
5f5c8ee5 213/* Nonzero means last display completed. Zero means it was preempted. */
4588ec20
JB
214
215int display_completed;
216
5f5c8ee5
GM
217/* Lisp variable visible-bell; enables use of screen-flash instead of
218 audible bell. */
4588ec20
JB
219
220int visible_bell;
221
502b9b64 222/* Invert the color of the whole frame, at a low level. */
4588ec20
JB
223
224int inverse_video;
225
226/* Line speed of the terminal. */
227
31ade731 228EMACS_INT baud_rate;
4588ec20 229
5f5c8ee5 230/* Either nil or a symbol naming the window system under which Emacs
2246281f 231 creates the first frame. */
4588ec20 232
2246281f 233Lisp_Object Vinitial_window_system;
4588ec20
JB
234
235/* Version number of X windows: 10, 11 or nil. */
5f5c8ee5 236
4588ec20
JB
237Lisp_Object Vwindow_system_version;
238
5f5c8ee5
GM
239/* Vector of glyph definitions. Indexed by glyph number, the contents
240 are a string which is how to output the glyph.
4588ec20
JB
241
242 If Vglyph_table is nil, a glyph is output by using its low 8 bits
5f5c8ee5
GM
243 as a character code.
244
245 This is an obsolete feature that is no longer used. The variable
246 is retained for compatibility. */
4588ec20
JB
247
248Lisp_Object Vglyph_table;
249
250/* Display table to use for vectors that don't specify their own. */
251
252Lisp_Object Vstandard_display_table;
253
5f5c8ee5 254/* Nonzero means reading single-character input with prompt so put
153a073c 255 cursor on mini-buffer after the prompt. Positive means at end of
5f5c8ee5
GM
256 text in echo area; negative means at beginning of line. */
257
4588ec20 258int cursor_in_echo_area;
9cda4f7c 259
e7067d00 260Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
5f5c8ee5 261
4588ec20 262\f
5f5c8ee5 263/* The currently selected frame. In a single-frame version, this
91fb7e1b 264 variable always equals the_only_frame. */
4588ec20 265
91fb7e1b 266Lisp_Object selected_frame;
4588ec20 267
5f5c8ee5 268/* A frame which is not just a mini-buffer, or 0 if there are no such
502b9b64 269 frames. This is usually the most recent such frame that was
87485d6f
MW
270 selected. In a single-frame version, this variable always holds
271 the address of the_only_frame. */
4588ec20 272
5f5c8ee5 273struct frame *last_nonminibuf_frame;
d52bad65 274
5f5c8ee5 275/* 1 means SIGWINCH happened when not safe. */
4588ec20 276
5f5c8ee5 277int delayed_size_change;
4588ec20 278
5f5c8ee5 279/* 1 means glyph initialization has been completed at startup. */
4588ec20 280
5f5c8ee5 281static int glyphs_initialized_initially_p;
4588ec20 282
5f5c8ee5 283/* Updated window if != 0. Set by update_window. */
4588ec20 284
5f5c8ee5 285struct window *updated_window;
4588ec20 286
5f5c8ee5 287/* Glyph row updated in update_window_line, and area that is updated. */
4588ec20 288
5f5c8ee5
GM
289struct glyph_row *updated_row;
290int updated_area;
4588ec20 291
5f5c8ee5 292/* A glyph for a space. */
4588ec20 293
5f5c8ee5 294struct glyph space_glyph;
4588ec20 295
5f5c8ee5
GM
296/* Counts of allocated structures. These counts serve to diagnose
297 memory leaks and double frees. */
4588ec20 298
5f5c8ee5
GM
299int glyph_matrix_count;
300int glyph_pool_count;
4588ec20 301
5f5c8ee5
GM
302/* If non-null, the frame whose frame matrices are manipulated. If
303 null, window matrices are worked on. */
4588ec20 304
5f5c8ee5 305static struct frame *frame_matrix_frame;
4588ec20 306
5f5c8ee5
GM
307/* Non-zero means that fonts have been loaded since the last glyph
308 matrix adjustments. Redisplay must stop, and glyph matrices must
309 be adjusted when this flag becomes non-zero during display. The
310 reason fonts can be loaded so late is that fonts of fontsets are
3aa9f0c4
KH
311 loaded on demand. Another reason is that a line contains many
312 characters displayed by zero width or very narrow glyphs of
313 variable-width fonts. */
836d2cde 314
5f5c8ee5 315int fonts_changed_p;
836d2cde 316
177c0ea7 317/* Convert vpos and hpos from frame to window and vice versa.
5f5c8ee5 318 This may only be used for terminal frames. */
836d2cde 319
5f5c8ee5 320#if GLYPH_DEBUG
4588ec20 321
f57e2426
J
322static int window_to_frame_vpos (struct window *, int);
323static int window_to_frame_hpos (struct window *, int);
5f5c8ee5
GM
324#define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
325#define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
4588ec20 326
9b0e97aa
GM
327/* One element of the ring buffer containing redisplay history
328 information. */
329
330struct redisplay_history
331{
332 char trace[512 + 100];
333};
334
335/* The size of the history buffer. */
336
337#define REDISPLAY_HISTORY_SIZE 30
338
339/* The redisplay history buffer. */
340
341static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
342
343/* Next free entry in redisplay_history. */
344
345static int history_idx;
346
347/* A tick that's incremented each time something is added to the
348 history. */
349
350static unsigned history_tick;
351
f57e2426
J
352static void add_frame_display_history (struct frame *, int);
353static void add_window_display_history (struct window *, char *, int);
6a8aec41 354\f
9b0e97aa
GM
355/* Add to the redisplay history how window W has been displayed.
356 MSG is a trace containing the information how W's glyph matrix
153a073c 357 has been constructed. PAUSED_P non-zero means that the update
9b0e97aa
GM
358 has been interrupted for pending input. */
359
360static void
361add_window_display_history (w, msg, paused_p)
362 struct window *w;
363 char *msg;
364 int paused_p;
365{
366 char *buf;
177c0ea7 367
9b0e97aa
GM
368 if (history_idx >= REDISPLAY_HISTORY_SIZE)
369 history_idx = 0;
370 buf = redisplay_history[history_idx].trace;
371 ++history_idx;
177c0ea7 372
9b0e97aa
GM
373 sprintf (buf, "%d: window %p (`%s')%s\n",
374 history_tick++,
375 w,
376 ((BUFFERP (w->buffer)
377 && STRINGP (XBUFFER (w->buffer)->name))
d5db4077 378 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9b0e97aa
GM
379 : "???"),
380 paused_p ? " ***paused***" : "");
381 strcat (buf, msg);
382}
383
384
385/* Add to the redisplay history that frame F has been displayed.
386 PAUSED_P non-zero means that the update has been interrupted for
387 pending input. */
388
389static void
390add_frame_display_history (f, paused_p)
391 struct frame *f;
392 int paused_p;
393{
394 char *buf;
177c0ea7 395
9b0e97aa
GM
396 if (history_idx >= REDISPLAY_HISTORY_SIZE)
397 history_idx = 0;
398 buf = redisplay_history[history_idx].trace;
399 ++history_idx;
177c0ea7 400
9b0e97aa
GM
401 sprintf (buf, "%d: update frame %p%s",
402 history_tick++,
403 f, paused_p ? " ***paused***" : "");
404}
405
406
407DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
408 Sdump_redisplay_history, 0, 0, "",
7ee72033 409 doc: /* Dump redisplay history to stderr. */)
5842a27b 410 (void)
9b0e97aa
GM
411{
412 int i;
413
414 for (i = history_idx - 1; i != history_idx; --i)
415 {
416 if (i < 0)
417 i = REDISPLAY_HISTORY_SIZE - 1;
418 fprintf (stderr, "%s\n", redisplay_history[i].trace);
419 }
420
421 return Qnil;
422}
423
424
5f5c8ee5 425#else /* GLYPH_DEBUG == 0 */
4588ec20 426
7df02016
KS
427#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
428#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
4588ec20 429
5f5c8ee5 430#endif /* GLYPH_DEBUG == 0 */
4588ec20 431
4588ec20 432
dcc19aac 433#if defined PROFILING && !HAVE___EXECUTABLE_START
72af86bd 434/* FIXME: only used to find text start for profiling. */
4588ec20
JB
435
436void
971de7fb 437safe_bcopy (const char *from, char *to, int size)
4588ec20 438{
72af86bd 439 abort ();
177c0ea7 440}
72af86bd 441#endif
5f5c8ee5
GM
442\f
443/***********************************************************************
444 Glyph Matrices
445 ***********************************************************************/
446
447/* Allocate and return a glyph_matrix structure. POOL is the glyph
448 pool from which memory for the matrix should be allocated, or null
449 for window-based redisplay where no glyph pools are used. The
450 member `pool' of the glyph matrix structure returned is set to
451 POOL, the structure is otherwise zeroed. */
452
453struct glyph_matrix *
971de7fb 454new_glyph_matrix (struct glyph_pool *pool)
4588ec20 455{
5f5c8ee5
GM
456 struct glyph_matrix *result;
457
458 /* Allocate and clear. */
459 result = (struct glyph_matrix *) xmalloc (sizeof *result);
72af86bd 460 memset (result, 0, sizeof *result);
4588ec20 461
5f5c8ee5
GM
462 /* Increment number of allocated matrices. This count is used
463 to detect memory leaks. */
464 ++glyph_matrix_count;
4588ec20 465
5f5c8ee5
GM
466 /* Set pool and return. */
467 result->pool = pool;
468 return result;
4588ec20
JB
469}
470
4588ec20 471
5f5c8ee5
GM
472/* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
473
474 The global counter glyph_matrix_count is decremented when a matrix
475 is freed. If the count gets negative, more structures were freed
476 than allocated, i.e. one matrix was freed more than once or a bogus
477 pointer was passed to this function.
177c0ea7 478
5f5c8ee5
GM
479 If MATRIX->pool is null, this means that the matrix manages its own
480 glyph memory---this is done for matrices on X frames. Freeing the
481 matrix also frees the glyph memory in this case. */
482
483static void
971de7fb 484free_glyph_matrix (struct glyph_matrix *matrix)
4588ec20 485{
5f5c8ee5
GM
486 if (matrix)
487 {
488 int i;
489
490 /* Detect the case that more matrices are freed than were
491 allocated. */
492 if (--glyph_matrix_count < 0)
493 abort ();
494
495 /* Free glyph memory if MATRIX owns it. */
496 if (matrix->pool == NULL)
497 for (i = 0; i < matrix->rows_allocated; ++i)
498 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
177c0ea7 499
5f5c8ee5
GM
500 /* Free row structures and the matrix itself. */
501 xfree (matrix->rows);
502 xfree (matrix);
503 }
504}
4588ec20 505
4588ec20 506
5f5c8ee5
GM
507/* Return the number of glyphs to reserve for a marginal area of
508 window W. TOTAL_GLYPHS is the number of glyphs in a complete
509 display line of window W. MARGIN gives the width of the marginal
510 area in canonical character units. MARGIN should be an integer
511 or a float. */
512
513static int
971de7fb 514margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
5f5c8ee5
GM
515{
516 int n;
4588ec20 517
5f5c8ee5 518 if (NUMBERP (margin))
4588ec20 519 {
7df02016 520 int width = XFASTINT (w->total_cols);
5f5c8ee5
GM
521 double d = max (0, XFLOATINT (margin));
522 d = min (width / 2 - 1, d);
35ec832e 523 n = (int) ((double) total_glyphs / width * d);
5f5c8ee5
GM
524 }
525 else
526 n = 0;
527
528 return n;
529}
530
4588ec20 531
5f5c8ee5
GM
532/* Adjust glyph matrix MATRIX on window W or on a frame to changed
533 window sizes.
4588ec20 534
5f5c8ee5
GM
535 W is null if the function is called for a frame glyph matrix.
536 Otherwise it is the window MATRIX is a member of. X and Y are the
537 indices of the first column and row of MATRIX within the frame
538 matrix, if such a matrix exists. They are zero for purely
539 window-based redisplay. DIM is the needed size of the matrix.
d52bad65 540
5f5c8ee5
GM
541 In window-based redisplay, where no frame matrices exist, glyph
542 matrices manage their own glyph storage. Otherwise, they allocate
543 storage from a common frame glyph pool which can be found in
544 MATRIX->pool.
23b0200c 545
5f5c8ee5
GM
546 The reason for this memory management strategy is to avoid complete
547 frame redraws if possible. When we allocate from a common pool, a
548 change of the location or size of a sub-matrix within the pool
549 requires a complete redisplay of the frame because we cannot easily
550 make sure that the current matrices of all windows still agree with
551 what is displayed on the screen. While this is usually fast, it
552 leads to screen flickering. */
23b0200c 553
5f5c8ee5 554static void
971de7fb 555adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
5f5c8ee5
GM
556{
557 int i;
558 int new_rows;
559 int marginal_areas_changed_p = 0;
045dee35
GM
560 int header_line_changed_p = 0;
561 int header_line_p = 0;
5f5c8ee5 562 int left = -1, right = -1;
ca8a12a7 563 int window_width = -1, window_height = -1;
5f5c8ee5 564
6b61353c
KH
565 /* See if W had a header line that has disappeared now, or vice versa.
566 Get W's size. */
5f5c8ee5
GM
567 if (w)
568 {
6b61353c
KH
569 window_box (w, -1, 0, 0, &window_width, &window_height);
570
045dee35
GM
571 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
572 header_line_changed_p = header_line_p != matrix->header_line_p;
5f5c8ee5 573 }
045dee35 574 matrix->header_line_p = header_line_p;
23b0200c 575
6b61353c
KH
576 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
577 Do nothing if MATRIX' size, position, vscroll, and marginal areas
5f5c8ee5
GM
578 haven't changed. This optimization is important because preserving
579 the matrix means preventing redisplay. */
580 if (matrix->pool == NULL)
581 {
7df02016
KS
582 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
583 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
5f5c8ee5
GM
584 xassert (left >= 0 && right >= 0);
585 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
586 || right != matrix->right_margin_glyphs);
587
588 if (!marginal_areas_changed_p
589 && !fonts_changed_p
045dee35 590 && !header_line_changed_p
7df02016
KS
591 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
592 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
5f5c8ee5
GM
593 && matrix->window_height == window_height
594 && matrix->window_vscroll == w->vscroll
595 && matrix->window_width == window_width)
596 return;
597 }
177c0ea7 598
5f5c8ee5
GM
599 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
600 if (matrix->rows_allocated < dim.height)
601 {
602 int size = dim.height * sizeof (struct glyph_row);
603 new_rows = dim.height - matrix->rows_allocated;
604 matrix->rows = (struct glyph_row *) xrealloc (matrix->rows, size);
72af86bd
AS
605 memset (matrix->rows + matrix->rows_allocated, 0,
606 new_rows * sizeof *matrix->rows);
5f5c8ee5
GM
607 matrix->rows_allocated = dim.height;
608 }
609 else
610 new_rows = 0;
60a8948a 611
5f5c8ee5
GM
612 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
613 on a frame not using window-based redisplay. Set up pointers for
614 each row into the glyph pool. */
615 if (matrix->pool)
616 {
617 xassert (matrix->pool->glyphs);
177c0ea7 618
5f5c8ee5 619 if (w)
60a8948a 620 {
5f5c8ee5 621 left = margin_glyphs_to_reserve (w, dim.width,
7df02016 622 w->left_margin_cols);
5f5c8ee5 623 right = margin_glyphs_to_reserve (w, dim.width,
7df02016 624 w->right_margin_cols);
60a8948a 625 }
5f5c8ee5
GM
626 else
627 left = right = 0;
177c0ea7 628
5f5c8ee5 629 for (i = 0; i < dim.height; ++i)
60a8948a 630 {
5f5c8ee5 631 struct glyph_row *row = &matrix->rows[i];
177c0ea7
JB
632
633 row->glyphs[LEFT_MARGIN_AREA]
5f5c8ee5
GM
634 = (matrix->pool->glyphs
635 + (y + i) * matrix->pool->ncolumns
636 + x);
177c0ea7 637
5f5c8ee5
GM
638 if (w == NULL
639 || row == matrix->rows + dim.height - 1
045dee35 640 || (row == matrix->rows && matrix->header_line_p))
5f5c8ee5
GM
641 {
642 row->glyphs[TEXT_AREA]
643 = row->glyphs[LEFT_MARGIN_AREA];
644 row->glyphs[RIGHT_MARGIN_AREA]
645 = row->glyphs[TEXT_AREA] + dim.width;
646 row->glyphs[LAST_AREA]
647 = row->glyphs[RIGHT_MARGIN_AREA];
648 }
649 else
650 {
651 row->glyphs[TEXT_AREA]
652 = row->glyphs[LEFT_MARGIN_AREA] + left;
653 row->glyphs[RIGHT_MARGIN_AREA]
654 = row->glyphs[TEXT_AREA] + dim.width - left - right;
655 row->glyphs[LAST_AREA]
656 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
657 }
60a8948a 658 }
177c0ea7 659
5f5c8ee5
GM
660 matrix->left_margin_glyphs = left;
661 matrix->right_margin_glyphs = right;
662 }
663 else
664 {
665 /* If MATRIX->pool is null, MATRIX is responsible for managing
6b61353c
KH
666 its own memory. It is a window matrix for window-based redisplay.
667 Allocate glyph memory from the heap. */
5f5c8ee5
GM
668 if (dim.width > matrix->matrix_w
669 || new_rows
045dee35 670 || header_line_changed_p
5f5c8ee5 671 || marginal_areas_changed_p)
4588ec20 672 {
5f5c8ee5
GM
673 struct glyph_row *row = matrix->rows;
674 struct glyph_row *end = row + matrix->rows_allocated;
177c0ea7 675
5f5c8ee5
GM
676 while (row < end)
677 {
678 row->glyphs[LEFT_MARGIN_AREA]
679 = (struct glyph *) xrealloc (row->glyphs[LEFT_MARGIN_AREA],
680 (dim.width
681 * sizeof (struct glyph)));
177c0ea7 682
5f5c8ee5
GM
683 /* The mode line never has marginal areas. */
684 if (row == matrix->rows + dim.height - 1
045dee35 685 || (row == matrix->rows && matrix->header_line_p))
5f5c8ee5
GM
686 {
687 row->glyphs[TEXT_AREA]
688 = row->glyphs[LEFT_MARGIN_AREA];
689 row->glyphs[RIGHT_MARGIN_AREA]
690 = row->glyphs[TEXT_AREA] + dim.width;
691 row->glyphs[LAST_AREA]
692 = row->glyphs[RIGHT_MARGIN_AREA];
693 }
694 else
695 {
696 row->glyphs[TEXT_AREA]
697 = row->glyphs[LEFT_MARGIN_AREA] + left;
698 row->glyphs[RIGHT_MARGIN_AREA]
699 = row->glyphs[TEXT_AREA] + dim.width - left - right;
700 row->glyphs[LAST_AREA]
701 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
702 }
703 ++row;
704 }
4588ec20
JB
705 }
706
5f5c8ee5
GM
707 xassert (left >= 0 && right >= 0);
708 matrix->left_margin_glyphs = left;
709 matrix->right_margin_glyphs = right;
710 }
177c0ea7 711
5f5c8ee5
GM
712 /* Number of rows to be used by MATRIX. */
713 matrix->nrows = dim.height;
3ed36514 714 xassert (matrix->nrows >= 0);
5f5c8ee5 715
b7b20fbd 716 if (w)
5f5c8ee5 717 {
b7b20fbd 718 if (matrix == w->current_matrix)
4588ec20 719 {
b7b20fbd
GM
720 /* Mark rows in a current matrix of a window as not having
721 valid contents. It's important to not do this for
722 desired matrices. When Emacs starts, it may already be
723 building desired matrices when this function runs. */
724 if (window_width < 0)
725 window_width = window_box_width (w, -1);
177c0ea7 726
b7b20fbd
GM
727 /* Optimize the case that only the height has changed (C-x 2,
728 upper window). Invalidate all rows that are no longer part
729 of the window. */
730 if (!marginal_areas_changed_p
73d75a62
GM
731 && !header_line_changed_p
732 && new_rows == 0
733 && dim.width == matrix->matrix_w
7df02016
KS
734 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
735 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
73d75a62 736 && matrix->window_width == window_width)
b7b20fbd 737 {
dd42b31a
GM
738 /* Find the last row in the window. */
739 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
740 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
741 {
26447967 742 ++i;
dd42b31a
GM
743 break;
744 }
b7b20fbd
GM
745
746 /* Window end is invalid, if inside of the rows that
dd42b31a 747 are invalidated below. */
b7b20fbd
GM
748 if (INTEGERP (w->window_end_vpos)
749 && XFASTINT (w->window_end_vpos) >= i)
750 w->window_end_valid = Qnil;
177c0ea7 751
b7b20fbd
GM
752 while (i < matrix->nrows)
753 matrix->rows[i++].enabled_p = 0;
754 }
755 else
756 {
757 for (i = 0; i < matrix->nrows; ++i)
758 matrix->rows[i].enabled_p = 0;
759 }
5f5c8ee5 760 }
b7b20fbd 761 else if (matrix == w->desired_matrix)
5f5c8ee5 762 {
b7b20fbd
GM
763 /* Rows in desired matrices always have to be cleared;
764 redisplay expects this is the case when it runs, so it
765 had better be the case when we adjust matrices between
766 redisplays. */
5f5c8ee5
GM
767 for (i = 0; i < matrix->nrows; ++i)
768 matrix->rows[i].enabled_p = 0;
4588ec20 769 }
4588ec20 770 }
177c0ea7
JB
771
772
5f5c8ee5
GM
773 /* Remember last values to be able to optimize frame redraws. */
774 matrix->matrix_x = x;
775 matrix->matrix_y = y;
776 matrix->matrix_w = dim.width;
777 matrix->matrix_h = dim.height;
778
779 /* Record the top y location and height of W at the time the matrix
780 was last adjusted. This is used to optimize redisplay above. */
781 if (w)
4588ec20 782 {
7df02016
KS
783 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
784 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
5f5c8ee5
GM
785 matrix->window_height = window_height;
786 matrix->window_width = window_width;
787 matrix->window_vscroll = w->vscroll;
788 }
789}
4588ec20 790
4588ec20 791
5f5c8ee5
GM
792/* Reverse the contents of rows in MATRIX between START and END. The
793 contents of the row at END - 1 end up at START, END - 2 at START +
794 1 etc. This is part of the implementation of rotate_matrix (see
795 below). */
d52bad65 796
5f5c8ee5 797static void
971de7fb 798reverse_rows (struct glyph_matrix *matrix, int start, int end)
5f5c8ee5
GM
799{
800 int i, j;
23b0200c 801
5f5c8ee5
GM
802 for (i = start, j = end - 1; i < j; ++i, --j)
803 {
804 /* Non-ISO HP/UX compiler doesn't like auto struct
805 initialization. */
806 struct glyph_row temp;
807 temp = matrix->rows[i];
808 matrix->rows[i] = matrix->rows[j];
809 matrix->rows[j] = temp;
810 }
811}
23b0200c 812
23b0200c 813
5f5c8ee5
GM
814/* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
815 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
816 indices. (Note: this does not copy glyphs, only glyph pointers in
817 row structures are moved around).
60a8948a 818
5f5c8ee5
GM
819 The algorithm used for rotating the vector was, I believe, first
820 described by Kernighan. See the vector R as consisting of two
821 sub-vectors AB, where A has length BY for BY >= 0. The result
822 after rotating is then BA. Reverse both sub-vectors to get ArBr
823 and reverse the result to get (ArBr)r which is BA. Similar for
824 rotating right. */
825
826void
971de7fb 827rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
5f5c8ee5
GM
828{
829 if (by < 0)
830 {
831 /* Up (rotate left, i.e. towards lower indices). */
832 by = -by;
833 reverse_rows (matrix, first, first + by);
834 reverse_rows (matrix, first + by, last);
835 reverse_rows (matrix, first, last);
836 }
837 else if (by > 0)
838 {
839 /* Down (rotate right, i.e. towards higher indices). */
840 reverse_rows (matrix, last - by, last);
841 reverse_rows (matrix, first, last - by);
842 reverse_rows (matrix, first, last);
843 }
844}
845
846
847/* Increment buffer positions in glyph rows of MATRIX. Do it for rows
848 with indices START <= index < END. Increment positions by DELTA/
849 DELTA_BYTES. */
850
851void
971de7fb 852increment_matrix_positions (struct glyph_matrix *matrix, int start, int end, int delta, int delta_bytes)
5f5c8ee5
GM
853{
854 /* Check that START and END are reasonable values. */
855 xassert (start >= 0 && start <= matrix->nrows);
856 xassert (end >= 0 && end <= matrix->nrows);
857 xassert (start <= end);
858
859 for (; start < end; ++start)
86c11ba1 860 increment_row_positions (matrix->rows + start, delta, delta_bytes);
5f5c8ee5
GM
861}
862
863
864/* Enable a range of rows in glyph matrix MATRIX. START and END are
865 the row indices of the first and last + 1 row to enable. If
866 ENABLED_P is non-zero, enabled_p flags in rows will be set to 1. */
867
868void
971de7fb 869enable_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end, int enabled_p)
5f5c8ee5 870{
3ed36514
GM
871 xassert (start <= end);
872 xassert (start >= 0 && start < matrix->nrows);
873 xassert (end >= 0 && end <= matrix->nrows);
177c0ea7 874
5f5c8ee5
GM
875 for (; start < end; ++start)
876 matrix->rows[start].enabled_p = enabled_p != 0;
877}
878
879
880/* Clear MATRIX.
881
882 This empties all rows in MATRIX by setting the enabled_p flag for
883 all rows of the matrix to zero. The function prepare_desired_row
884 will eventually really clear a row when it sees one with a zero
885 enabled_p flag.
886
887 Resets update hints to defaults value. The only update hint
888 currently present is the flag MATRIX->no_scrolling_p. */
889
890void
971de7fb 891clear_glyph_matrix (struct glyph_matrix *matrix)
5f5c8ee5
GM
892{
893 if (matrix)
894 {
895 enable_glyph_matrix_rows (matrix, 0, matrix->nrows, 0);
896 matrix->no_scrolling_p = 0;
897 }
898}
177c0ea7 899
5f5c8ee5
GM
900
901/* Shift part of the glyph matrix MATRIX of window W up or down.
902 Increment y-positions in glyph rows between START and END by DY,
903 and recompute their visible height. */
904
905void
971de7fb 906shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
5f5c8ee5
GM
907{
908 int min_y, max_y;
177c0ea7 909
5f5c8ee5
GM
910 xassert (start <= end);
911 xassert (start >= 0 && start < matrix->nrows);
912 xassert (end >= 0 && end <= matrix->nrows);
177c0ea7 913
7df02016
KS
914 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
915 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
177c0ea7 916
5f5c8ee5
GM
917 for (; start < end; ++start)
918 {
919 struct glyph_row *row = &matrix->rows[start];
177c0ea7 920
5f5c8ee5 921 row->y += dy;
d6c747a4 922 row->visible_height = row->height;
177c0ea7 923
5f5c8ee5 924 if (row->y < min_y)
d6c747a4
GM
925 row->visible_height -= min_y - row->y;
926 if (row->y + row->height > max_y)
927 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
928 }
929}
930
931
932/* Mark all rows in current matrices of frame F as invalid. Marking
933 invalid is done by setting enabled_p to zero for all rows in a
934 current matrix. */
935
936void
971de7fb 937clear_current_matrices (register struct frame *f)
5f5c8ee5
GM
938{
939 /* Clear frame current matrix, if we have one. */
940 if (f->current_matrix)
941 clear_glyph_matrix (f->current_matrix);
942
943 /* Clear the matrix of the menu bar window, if such a window exists.
944 The menu bar window is currently used to display menus on X when
945 no toolkit support is compiled in. */
946 if (WINDOWP (f->menu_bar_window))
947 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
948
9ea173e8
GM
949 /* Clear the matrix of the tool-bar window, if any. */
950 if (WINDOWP (f->tool_bar_window))
951 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
5f5c8ee5
GM
952
953 /* Clear current window matrices. */
954 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
955 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
956}
957
958
959/* Clear out all display lines of F for a coming redisplay. */
960
961void
971de7fb 962clear_desired_matrices (register struct frame *f)
5f5c8ee5
GM
963{
964 if (f->desired_matrix)
965 clear_glyph_matrix (f->desired_matrix);
177c0ea7 966
5f5c8ee5
GM
967 if (WINDOWP (f->menu_bar_window))
968 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
969
9ea173e8
GM
970 if (WINDOWP (f->tool_bar_window))
971 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
5f5c8ee5
GM
972
973 /* Do it for window matrices. */
974 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
975 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
976}
977
978
979/* Clear matrices in window tree rooted in W. If DESIRED_P is
980 non-zero clear desired matrices, otherwise clear current matrices. */
981
982static void
971de7fb 983clear_window_matrices (struct window *w, int desired_p)
5f5c8ee5
GM
984{
985 while (w)
986 {
987 if (!NILP (w->hchild))
988 {
989 xassert (WINDOWP (w->hchild));
990 clear_window_matrices (XWINDOW (w->hchild), desired_p);
991 }
992 else if (!NILP (w->vchild))
993 {
994 xassert (WINDOWP (w->vchild));
995 clear_window_matrices (XWINDOW (w->vchild), desired_p);
996 }
997 else
998 {
999 if (desired_p)
1000 clear_glyph_matrix (w->desired_matrix);
1001 else
1002 {
1003 clear_glyph_matrix (w->current_matrix);
1004 w->window_end_valid = Qnil;
1005 }
1006 }
1007
1008 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1009 }
1010}
1011
1012
1013\f
1014/***********************************************************************
1015 Glyph Rows
1016
1017 See dispextern.h for an overall explanation of glyph rows.
1018 ***********************************************************************/
1019
1020/* Clear glyph row ROW. Do it in a way that makes it robust against
1021 changes in the glyph_row structure, i.e. addition or removal of
1022 structure members. */
1023
2cca872d
KH
1024static struct glyph_row null_row;
1025
5f5c8ee5 1026void
971de7fb 1027clear_glyph_row (struct glyph_row *row)
5f5c8ee5
GM
1028{
1029 struct glyph *p[1 + LAST_AREA];
5f5c8ee5
GM
1030
1031 /* Save pointers. */
1032 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
1033 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
1034 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
1035 p[LAST_AREA] = row->glyphs[LAST_AREA];
1036
1037 /* Clear. */
1038 *row = null_row;
1039
1040 /* Restore pointers. */
1041 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
1042 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
1043 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
1044 row->glyphs[LAST_AREA] = p[LAST_AREA];
45560b1a
GM
1045
1046#if 0 /* At some point, some bit-fields of struct glyph were not set,
1047 which made glyphs unequal when compared with GLYPH_EQUAL_P.
1048 Redisplay outputs such glyphs, and flickering effects were
1049 the result. This also depended on the contents of memory
1050 returned by xmalloc. If flickering happens again, activate
153a073c 1051 the code below. If the flickering is gone with that, chances
45560b1a 1052 are that the flickering has the same reason as here. */
72af86bd 1053 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
45560b1a 1054#endif
5f5c8ee5
GM
1055}
1056
1057
1058/* Make ROW an empty, enabled row of canonical character height,
1059 in window W starting at y-position Y. */
1060
1061void
971de7fb 1062blank_row (struct window *w, struct glyph_row *row, int y)
5f5c8ee5
GM
1063{
1064 int min_y, max_y;
177c0ea7 1065
7df02016
KS
1066 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
1067 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
177c0ea7 1068
5f5c8ee5
GM
1069 clear_glyph_row (row);
1070 row->y = y;
408f5064 1071 row->ascent = row->phys_ascent = 0;
7df02016 1072 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
d6c747a4 1073 row->visible_height = row->height;
177c0ea7 1074
5f5c8ee5 1075 if (row->y < min_y)
d6c747a4
GM
1076 row->visible_height -= min_y - row->y;
1077 if (row->y + row->height > max_y)
1078 row->visible_height -= row->y + row->height - max_y;
5f5c8ee5
GM
1079
1080 row->enabled_p = 1;
1081}
1082
1083
1084/* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
1085 are the amounts by which to change positions. Note that the first
1086 glyph of the text area of a row can have a buffer position even if
1087 the used count of the text area is zero. Such rows display line
1088 ends. */
1089
1090void
971de7fb 1091increment_row_positions (struct glyph_row *row, int delta, int delta_bytes)
5f5c8ee5
GM
1092{
1093 int area, i;
1094
1095 /* Increment start and end positions. */
1096 MATRIX_ROW_START_CHARPOS (row) += delta;
1097 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
1098 MATRIX_ROW_END_CHARPOS (row) += delta;
1099 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
d36fe237
EZ
1100 CHARPOS (row->start.pos) += delta;
1101 BYTEPOS (row->start.pos) += delta_bytes;
1102 CHARPOS (row->end.pos) += delta;
1103 BYTEPOS (row->end.pos) += delta_bytes;
5f5c8ee5 1104
5cacdcfd
KS
1105 if (!row->enabled_p)
1106 return;
1107
5f5c8ee5
GM
1108 /* Increment positions in glyphs. */
1109 for (area = 0; area < LAST_AREA; ++area)
1110 for (i = 0; i < row->used[area]; ++i)
1111 if (BUFFERP (row->glyphs[area][i].object)
1112 && row->glyphs[area][i].charpos > 0)
1113 row->glyphs[area][i].charpos += delta;
1114
1115 /* Capture the case of rows displaying a line end. */
1116 if (row->used[TEXT_AREA] == 0
1117 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
1118 row->glyphs[TEXT_AREA]->charpos += delta;
1119}
1120
1121
9af3e742 1122#if 0
5f5c8ee5
GM
1123/* Swap glyphs between two glyph rows A and B. This exchanges glyph
1124 contents, i.e. glyph structure contents are exchanged between A and
1125 B without changing glyph pointers in A and B. */
1126
1127static void
1128swap_glyphs_in_rows (a, b)
1129 struct glyph_row *a, *b;
1130{
1131 int area;
1132
1133 for (area = 0; area < LAST_AREA; ++area)
1134 {
1135 /* Number of glyphs to swap. */
1136 int max_used = max (a->used[area], b->used[area]);
1137
1138 /* Start of glyphs in area of row A. */
1139 struct glyph *glyph_a = a->glyphs[area];
1140
1141 /* End + 1 of glyphs in area of row A. */
1142 struct glyph *glyph_a_end = a->glyphs[max_used];
1143
1144 /* Start of glyphs in area of row B. */
1145 struct glyph *glyph_b = b->glyphs[area];
1146
1147 while (glyph_a < glyph_a_end)
1148 {
1149 /* Non-ISO HP/UX compiler doesn't like auto struct
1150 initialization. */
1151 struct glyph temp;
1152 temp = *glyph_a;
1153 *glyph_a = *glyph_b;
1154 *glyph_b = temp;
1155 ++glyph_a;
1156 ++glyph_b;
1157 }
1158 }
1159}
1160
9af3e742 1161#endif /* 0 */
5f5c8ee5
GM
1162
1163/* Exchange pointers to glyph memory between glyph rows A and B. */
1164
1165static INLINE void
971de7fb 1166swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
5f5c8ee5
GM
1167{
1168 int i;
1169 for (i = 0; i < LAST_AREA + 1; ++i)
1170 {
1171 struct glyph *temp = a->glyphs[i];
1172 a->glyphs[i] = b->glyphs[i];
1173 b->glyphs[i] = temp;
1174 }
1175}
1176
1177
1178/* Copy glyph row structure FROM to glyph row structure TO, except
1179 that glyph pointers in the structures are left unchanged. */
1180
1181INLINE void
971de7fb 1182copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
5f5c8ee5
GM
1183{
1184 struct glyph *pointers[1 + LAST_AREA];
1185
1186 /* Save glyph pointers of TO. */
72af86bd 1187 memcpy (pointers, to->glyphs, sizeof to->glyphs);
5f5c8ee5
GM
1188
1189 /* Do a structure assignment. */
1190 *to = *from;
1191
1192 /* Restore original pointers of TO. */
72af86bd 1193 memcpy (to->glyphs, pointers, sizeof to->glyphs);
5f5c8ee5
GM
1194}
1195
1196
1197/* Copy contents of glyph row FROM to glyph row TO. Glyph pointers in
1198 TO and FROM are left unchanged. Glyph contents are copied from the
1199 glyph memory of FROM to the glyph memory of TO. Increment buffer
1200 positions in row TO by DELTA/ DELTA_BYTES. */
1201
1202void
971de7fb 1203copy_glyph_row_contents (struct glyph_row *to, struct glyph_row *from, int delta, int delta_bytes)
5f5c8ee5
GM
1204{
1205 int area;
1206
1207 /* This is like a structure assignment TO = FROM, except that
1208 glyph pointers in the rows are left unchanged. */
1209 copy_row_except_pointers (to, from);
1210
1211 /* Copy glyphs from FROM to TO. */
1212 for (area = 0; area < LAST_AREA; ++area)
1213 if (from->used[area])
72af86bd
AS
1214 memcpy (to->glyphs[area], from->glyphs[area],
1215 from->used[area] * sizeof (struct glyph));
5f5c8ee5
GM
1216
1217 /* Increment buffer positions in TO by DELTA. */
86c11ba1 1218 increment_row_positions (to, delta, delta_bytes);
5f5c8ee5
GM
1219}
1220
1221
1222/* Assign glyph row FROM to glyph row TO. This works like a structure
1223 assignment TO = FROM, except that glyph pointers are not copied but
1224 exchanged between TO and FROM. Pointers must be exchanged to avoid
1225 a memory leak. */
1226
1227static INLINE void
971de7fb 1228assign_row (struct glyph_row *to, struct glyph_row *from)
5f5c8ee5
GM
1229{
1230 swap_glyph_pointers (to, from);
1231 copy_row_except_pointers (to, from);
1232}
1233
1234
1235/* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1236 a row in a window matrix, is a slice of the glyph memory of the
1237 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1238 is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
1239 memory of FRAME_ROW. */
1240
2d5912c1 1241#if GLYPH_DEBUG
9339e87f 1242
5f5c8ee5
GM
1243static int
1244glyph_row_slice_p (window_row, frame_row)
1245 struct glyph_row *window_row, *frame_row;
1246{
1247 struct glyph *window_glyph_start = window_row->glyphs[0];
1248 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1249 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1250
1251 return (frame_glyph_start <= window_glyph_start
1252 && window_glyph_start < frame_glyph_end);
1253}
1254
9339e87f 1255#endif /* GLYPH_DEBUG */
5f5c8ee5 1256
9af3e742
GM
1257#if 0
1258
5f5c8ee5
GM
1259/* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1260 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1261 in WINDOW_MATRIX is found satisfying the condition. */
1262
1263static struct glyph_row *
1264find_glyph_row_slice (window_matrix, frame_matrix, row)
1265 struct glyph_matrix *window_matrix, *frame_matrix;
1266 int row;
1267{
1268 int i;
1269
1270 xassert (row >= 0 && row < frame_matrix->nrows);
1271
1272 for (i = 0; i < window_matrix->nrows; ++i)
1273 if (glyph_row_slice_p (window_matrix->rows + i,
1274 frame_matrix->rows + row))
1275 break;
1276
1277 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1278}
1279
9af3e742 1280#endif /* 0 */
5f5c8ee5
GM
1281
1282/* Prepare ROW for display. Desired rows are cleared lazily,
1283 i.e. they are only marked as to be cleared by setting their
1284 enabled_p flag to zero. When a row is to be displayed, a prior
1285 call to this function really clears it. */
1286
1287void
971de7fb 1288prepare_desired_row (struct glyph_row *row)
5f5c8ee5
GM
1289{
1290 if (!row->enabled_p)
1291 {
5009f85e
EZ
1292 unsigned rp = row->reversed_p;
1293
5f5c8ee5
GM
1294 clear_glyph_row (row);
1295 row->enabled_p = 1;
5009f85e 1296 row->reversed_p = rp;
5f5c8ee5
GM
1297 }
1298}
1299
1300
1301/* Return a hash code for glyph row ROW. */
1302
1303int
971de7fb 1304line_hash_code (struct glyph_row *row)
5f5c8ee5
GM
1305{
1306 int hash = 0;
177c0ea7 1307
5f5c8ee5
GM
1308 if (row->enabled_p)
1309 {
4cecaab8
MB
1310 struct glyph *glyph = row->glyphs[TEXT_AREA];
1311 struct glyph *end = glyph + row->used[TEXT_AREA];
1312
1313 while (glyph < end)
1314 {
1315 int c = glyph->u.ch;
1316 int face_id = glyph->face_id;
daf01701 1317 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
4cecaab8
MB
1318 c -= SPACEGLYPH;
1319 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1320 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1321 ++glyph;
1322 }
5f5c8ee5 1323
4cecaab8
MB
1324 if (hash == 0)
1325 hash = 1;
5f5c8ee5
GM
1326 }
1327
1328 return hash;
1329}
1330
1331
153a073c 1332/* Return the cost of drawing line VPOS in MATRIX. The cost equals
5f5c8ee5
GM
1333 the number of characters in the line. If must_write_spaces is
1334 zero, leading and trailing spaces are ignored. */
1335
1336static unsigned int
971de7fb 1337line_draw_cost (struct glyph_matrix *matrix, int vpos)
5f5c8ee5
GM
1338{
1339 struct glyph_row *row = matrix->rows + vpos;
1340 struct glyph *beg = row->glyphs[TEXT_AREA];
1341 struct glyph *end = beg + row->used[TEXT_AREA];
1342 int len;
1343 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1344 int glyph_table_len = GLYPH_TABLE_LENGTH;
1345
1346 /* Ignore trailing and leading spaces if we can. */
daf01701 1347 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
5f5c8ee5
GM
1348 {
1349 /* Skip from the end over trailing spaces. */
2b4ec4d7 1350 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
5f5c8ee5
GM
1351 --end;
1352
177c0ea7 1353 /* All blank line. */
5f5c8ee5
GM
1354 if (end == beg)
1355 return 0;
1356
1357 /* Skip over leading spaces. */
1358 while (CHAR_GLYPH_SPACE_P (*beg))
1359 ++beg;
1360 }
1361
1362 /* If we don't have a glyph-table, each glyph is one character,
1363 so return the number of glyphs. */
1364 if (glyph_table_base == 0)
1365 len = end - beg;
1366 else
1367 {
1368 /* Otherwise, scan the glyphs and accumulate their total length
1369 in LEN. */
1370 len = 0;
1371 while (beg < end)
1372 {
3fc809e2 1373 GLYPH g;
177c0ea7 1374
3fc809e2
KS
1375 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1376
565b39a8 1377 if (GLYPH_INVALID_P (g)
42186983 1378 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
5f5c8ee5
GM
1379 len += 1;
1380 else
1381 len += GLYPH_LENGTH (glyph_table_base, g);
177c0ea7 1382
5f5c8ee5
GM
1383 ++beg;
1384 }
1385 }
177c0ea7 1386
5f5c8ee5
GM
1387 return len;
1388}
1389
1390
1391/* Test two glyph rows A and B for equality. Value is non-zero if A
1392 and B have equal contents. W is the window to which the glyphs
1393 rows A and B belong. It is needed here to test for partial row
e4e0bee9
GM
1394 visibility. MOUSE_FACE_P non-zero means compare the mouse_face_p
1395 flags of A and B, too. */
5f5c8ee5 1396
177c0ea7 1397static INLINE int
971de7fb 1398row_equal_p (struct window *w, struct glyph_row *a, struct glyph_row *b, int mouse_face_p)
5f5c8ee5
GM
1399{
1400 if (a == b)
1401 return 1;
1402 else if (a->hash != b->hash)
1403 return 0;
1404 else
1405 {
1406 struct glyph *a_glyph, *b_glyph, *a_end;
1407 int area;
1408
e4e0bee9
GM
1409 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1410 return 0;
1411
5f5c8ee5
GM
1412 /* Compare glyphs. */
1413 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1414 {
1415 if (a->used[area] != b->used[area])
1416 return 0;
177c0ea7 1417
5f5c8ee5
GM
1418 a_glyph = a->glyphs[area];
1419 a_end = a_glyph + a->used[area];
1420 b_glyph = b->glyphs[area];
177c0ea7 1421
5f5c8ee5
GM
1422 while (a_glyph < a_end
1423 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1424 ++a_glyph, ++b_glyph;
177c0ea7 1425
5f5c8ee5
GM
1426 if (a_glyph != a_end)
1427 return 0;
1428 }
1429
6b61353c
KH
1430 if (a->fill_line_p != b->fill_line_p
1431 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1432 || a->left_fringe_bitmap != b->left_fringe_bitmap
1433 || a->left_fringe_face_id != b->left_fringe_face_id
1434 || a->right_fringe_bitmap != b->right_fringe_bitmap
1435 || a->right_fringe_face_id != b->right_fringe_face_id
3d4a365f 1436 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
6b61353c 1437 || a->exact_window_width_line_p != b->exact_window_width_line_p
408f5064 1438 || a->overlapped_p != b->overlapped_p
5f5c8ee5
GM
1439 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1440 != MATRIX_ROW_CONTINUATION_LINE_P (b))
918f4159 1441 || a->reversed_p != b->reversed_p
5f5c8ee5
GM
1442 /* Different partially visible characters on left margin. */
1443 || a->x != b->x
1444 /* Different height. */
1445 || a->ascent != b->ascent
408f5064
GM
1446 || a->phys_ascent != b->phys_ascent
1447 || a->phys_height != b->phys_height
5f5c8ee5
GM
1448 || a->visible_height != b->visible_height)
1449 return 0;
1450 }
1451
1452 return 1;
1453}
1454
1455
1456\f
1457/***********************************************************************
1458 Glyph Pool
1459
1460 See dispextern.h for an overall explanation of glyph pools.
1461 ***********************************************************************/
1462
1463/* Allocate a glyph_pool structure. The structure returned is
1464 initialized with zeros. The global variable glyph_pool_count is
1465 incremented for each pool allocated. */
1466
1467static struct glyph_pool *
971de7fb 1468new_glyph_pool (void)
5f5c8ee5
GM
1469{
1470 struct glyph_pool *result;
1471
1472 /* Allocate a new glyph_pool and clear it. */
1473 result = (struct glyph_pool *) xmalloc (sizeof *result);
72af86bd 1474 memset (result, 0, sizeof *result);
177c0ea7 1475
5f5c8ee5
GM
1476 /* For memory leak and double deletion checking. */
1477 ++glyph_pool_count;
177c0ea7 1478
5f5c8ee5
GM
1479 return result;
1480}
1481
1482
1483/* Free a glyph_pool structure POOL. The function may be called with
1484 a null POOL pointer. The global variable glyph_pool_count is
1485 decremented with every pool structure freed. If this count gets
1486 negative, more structures were freed than allocated, i.e. one
1487 structure must have been freed more than once or a bogus pointer
1488 was passed to free_glyph_pool. */
1489
1490static void
971de7fb 1491free_glyph_pool (struct glyph_pool *pool)
5f5c8ee5
GM
1492{
1493 if (pool)
1494 {
153a073c 1495 /* More freed than allocated? */
5f5c8ee5
GM
1496 --glyph_pool_count;
1497 xassert (glyph_pool_count >= 0);
1498
1499 xfree (pool->glyphs);
1500 xfree (pool);
1501 }
1502}
1503
1504
1505/* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1506 columns we need. This function never shrinks a pool. The only
1507 case in which this would make sense, would be when a frame's size
1508 is changed from a large value to a smaller one. But, if someone
1509 does it once, we can expect that he will do it again.
1510
1511 Value is non-zero if the pool changed in a way which makes
1512 re-adjusting window glyph matrices necessary. */
1513
1514static int
971de7fb 1515realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
5f5c8ee5
GM
1516{
1517 int needed;
1518 int changed_p;
1519
1520 changed_p = (pool->glyphs == 0
1521 || matrix_dim.height != pool->nrows
1522 || matrix_dim.width != pool->ncolumns);
1523
1524 /* Enlarge the glyph pool. */
1525 needed = matrix_dim.width * matrix_dim.height;
1526 if (needed > pool->nglyphs)
1527 {
1528 int size = needed * sizeof (struct glyph);
1529
1530 if (pool->glyphs)
cb4545ad
EZ
1531 {
1532 pool->glyphs = (struct glyph *) xrealloc (pool->glyphs, size);
1533 memset (pool->glyphs + pool->nglyphs, 0,
1534 size - pool->nglyphs * sizeof (struct glyph));
1535 }
5f5c8ee5
GM
1536 else
1537 {
1538 pool->glyphs = (struct glyph *) xmalloc (size);
72af86bd 1539 memset (pool->glyphs, 0, size);
5f5c8ee5
GM
1540 }
1541
1542 pool->nglyphs = needed;
1543 }
1544
153a073c 1545 /* Remember the number of rows and columns because (a) we use them
5f5c8ee5
GM
1546 to do sanity checks, and (b) the number of columns determines
1547 where rows in the frame matrix start---this must be available to
1548 determine pointers to rows of window sub-matrices. */
1549 pool->nrows = matrix_dim.height;
1550 pool->ncolumns = matrix_dim.width;
177c0ea7 1551
5f5c8ee5
GM
1552 return changed_p;
1553}
1554
1555
1556\f
1557/***********************************************************************
1558 Debug Code
1559 ***********************************************************************/
1560
1561#if GLYPH_DEBUG
1562
a13396c9 1563
28d440ab
KL
1564/* Flush standard output. This is sometimes useful to call from the debugger.
1565 XXX Maybe this should be changed to flush the current terminal instead of
1566 stdout.
1567*/
a13396c9
GM
1568
1569void
1570flush_stdout ()
1571{
1572 fflush (stdout);
1573}
1574
1575
5f5c8ee5
GM
1576/* Check that no glyph pointers have been lost in MATRIX. If a
1577 pointer has been lost, e.g. by using a structure assignment between
1578 rows, at least one pointer must occur more than once in the rows of
1579 MATRIX. */
1580
1581void
1582check_matrix_pointer_lossage (matrix)
1583 struct glyph_matrix *matrix;
1584{
1585 int i, j;
177c0ea7 1586
5f5c8ee5
GM
1587 for (i = 0; i < matrix->nrows; ++i)
1588 for (j = 0; j < matrix->nrows; ++j)
1589 xassert (i == j
1590 || (matrix->rows[i].glyphs[TEXT_AREA]
1591 != matrix->rows[j].glyphs[TEXT_AREA]));
1592}
1593
1594
1595/* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1596
1597struct glyph_row *
1598matrix_row (matrix, row)
1599 struct glyph_matrix *matrix;
1600 int row;
1601{
1602 xassert (matrix && matrix->rows);
1603 xassert (row >= 0 && row < matrix->nrows);
1604
1605 /* That's really too slow for normal testing because this function
1606 is called almost everywhere. Although---it's still astonishingly
1607 fast, so it is valuable to have for debugging purposes. */
1608#if 0
1609 check_matrix_pointer_lossage (matrix);
1610#endif
177c0ea7 1611
5f5c8ee5
GM
1612 return matrix->rows + row;
1613}
1614
1615
1616#if 0 /* This function makes invalid assumptions when text is
1617 partially invisible. But it might come handy for debugging
1618 nevertheless. */
1619
1620/* Check invariants that must hold for an up to date current matrix of
1621 window W. */
1622
1623static void
1624check_matrix_invariants (w)
1625 struct window *w;
1626{
1627 struct glyph_matrix *matrix = w->current_matrix;
1628 int yb = window_text_bottom_y (w);
1629 struct glyph_row *row = matrix->rows;
1630 struct glyph_row *last_text_row = NULL;
1631 struct buffer *saved = current_buffer;
1632 struct buffer *buffer = XBUFFER (w->buffer);
1633 int c;
177c0ea7 1634
5f5c8ee5
GM
1635 /* This can sometimes happen for a fresh window. */
1636 if (matrix->nrows < 2)
1637 return;
1638
1639 set_buffer_temp (buffer);
1640
1641 /* Note: last row is always reserved for the mode line. */
1642 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1643 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1644 {
1645 struct glyph_row *next = row + 1;
1646
1647 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1648 last_text_row = row;
1649
1650 /* Check that character and byte positions are in sync. */
1651 xassert (MATRIX_ROW_START_BYTEPOS (row)
1652 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
d36fe237
EZ
1653 xassert (BYTEPOS (row->start.pos)
1654 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
5f5c8ee5
GM
1655
1656 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1657 have such a position temporarily in case of a minibuffer
1658 displaying something like `[Sole completion]' at its end. */
1659 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
d36fe237
EZ
1660 {
1661 xassert (MATRIX_ROW_END_BYTEPOS (row)
1662 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1663 xassert (BYTEPOS (row->end.pos)
1664 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1665 }
5f5c8ee5
GM
1666
1667 /* Check that end position of `row' is equal to start position
1668 of next row. */
1669 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1670 {
1671 xassert (MATRIX_ROW_END_CHARPOS (row)
1672 == MATRIX_ROW_START_CHARPOS (next));
1673 xassert (MATRIX_ROW_END_BYTEPOS (row)
1674 == MATRIX_ROW_START_BYTEPOS (next));
d36fe237
EZ
1675 xassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1676 xassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
5f5c8ee5
GM
1677 }
1678 row = next;
1679 }
1680
1681 xassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1682 xassert (w->desired_matrix->rows != NULL);
1683 set_buffer_temp (saved);
1684}
1685
1686#endif /* 0 */
1687
1688#endif /* GLYPH_DEBUG != 0 */
1689
1690
1691\f
1692/**********************************************************************
1693 Allocating/ Adjusting Glyph Matrices
1694 **********************************************************************/
1695
1696/* Allocate glyph matrices over a window tree for a frame-based
1697 redisplay
1698
1699 X and Y are column/row within the frame glyph matrix where
1700 sub-matrices for the window tree rooted at WINDOW must be
8ce2650d
YM
1701 allocated. DIM_ONLY_P non-zero means that the caller of this
1702 function is only interested in the result matrix dimension, and
1703 matrix adjustments should not be performed.
5f5c8ee5
GM
1704
1705 The function returns the total width/height of the sub-matrices of
1706 the window tree. If called on a frame root window, the computation
1707 will take the mini-buffer window into account.
1708
1709 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1710
1711 NEW_LEAF_MATRIX set if any window in the tree did not have a
1712 glyph matrices yet, and
1713
1714 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1715 any window in the tree will be changed or have been changed (see
153a073c 1716 DIM_ONLY_P)
5f5c8ee5
GM
1717
1718 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1719 function.
1720
1721 Windows are arranged into chains of windows on the same level
1722 through the next fields of window structures. Such a level can be
1723 either a sequence of horizontally adjacent windows from left to
1724 right, or a sequence of vertically adjacent windows from top to
1725 bottom. Each window in a horizontal sequence can be either a leaf
1726 window or a vertical sequence; a window in a vertical sequence can
1727 be either a leaf or a horizontal sequence. All windows in a
1728 horizontal sequence have the same height, and all windows in a
1729 vertical sequence have the same width.
1730
1731 This function uses, for historical reasons, a more general
1732 algorithm to determine glyph matrix dimensions that would be
1733 necessary.
1734
1735 The matrix height of a horizontal sequence is determined by the
1736 maximum height of any matrix in the sequence. The matrix width of
1737 a horizontal sequence is computed by adding up matrix widths of
1738 windows in the sequence.
1739
1740 |<------- result width ------->|
1741 +---------+----------+---------+ ---
1742 | | | | |
1743 | | | |
1744 +---------+ | | result height
1745 | +---------+
1746 | | |
1747 +----------+ ---
1748
1749 The matrix width of a vertical sequence is the maximum matrix width
1750 of any window in the sequence. Its height is computed by adding up
1751 matrix heights of windows in the sequence.
1752
1753 |<---- result width -->|
1754 +---------+ ---
1755 | | |
1756 | | |
1757 +---------+--+ |
1758 | | |
1759 | | result height
1760 | |
1761 +------------+---------+ |
1762 | | |
1763 | | |
1764 +------------+---------+ --- */
1765
1766/* Bit indicating that a new matrix will be allocated or has been
1767 allocated. */
1768
1769#define NEW_LEAF_MATRIX (1 << 0)
1770
1771/* Bit indicating that a matrix will or has changed its location or
1772 size. */
1773
1774#define CHANGED_LEAF_MATRIX (1 << 1)
1775
1776static struct dim
6f704c76
DN
1777allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1778 int dim_only_p, int *window_change_flags)
5f5c8ee5
GM
1779{
1780 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1781 int x0 = x, y0 = y;
1782 int wmax = 0, hmax = 0;
1783 struct dim total;
1784 struct dim dim;
1785 struct window *w;
1786 int in_horz_combination_p;
1787
1788 /* What combination is WINDOW part of? Compute this once since the
1789 result is the same for all windows in the `next' chain. The
1790 special case of a root window (parent equal to nil) is treated
1791 like a vertical combination because a root window's `next'
1792 points to the mini-buffer window, if any, which is arranged
1793 vertically below other windows. */
1794 in_horz_combination_p
1795 = (!NILP (XWINDOW (window)->parent)
1796 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1797
1798 /* For WINDOW and all windows on the same level. */
177c0ea7 1799 do
5f5c8ee5
GM
1800 {
1801 w = XWINDOW (window);
1802
1803 /* Get the dimension of the window sub-matrix for W, depending
153a073c 1804 on whether this is a combination or a leaf window. */
5f5c8ee5 1805 if (!NILP (w->hchild))
177c0ea7 1806 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
5f5c8ee5
GM
1807 dim_only_p,
1808 window_change_flags);
1809 else if (!NILP (w->vchild))
177c0ea7 1810 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
5f5c8ee5
GM
1811 dim_only_p,
1812 window_change_flags);
1813 else
1814 {
1815 /* If not already done, allocate sub-matrix structures. */
1816 if (w->desired_matrix == NULL)
1817 {
1818 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1819 w->current_matrix = new_glyph_matrix (f->current_pool);
1820 *window_change_flags |= NEW_LEAF_MATRIX;
1821 }
177c0ea7 1822
5f5c8ee5
GM
1823 /* Width and height MUST be chosen so that there are no
1824 holes in the frame matrix. */
5d3cc43c
GM
1825 dim.width = required_matrix_width (w);
1826 dim.height = required_matrix_height (w);
5f5c8ee5
GM
1827
1828 /* Will matrix be re-allocated? */
1829 if (x != w->desired_matrix->matrix_x
1830 || y != w->desired_matrix->matrix_y
1831 || dim.width != w->desired_matrix->matrix_w
1832 || dim.height != w->desired_matrix->matrix_h
1833 || (margin_glyphs_to_reserve (w, dim.width,
cc834cf9 1834 w->left_margin_cols)
5f5c8ee5
GM
1835 != w->desired_matrix->left_margin_glyphs)
1836 || (margin_glyphs_to_reserve (w, dim.width,
cc834cf9 1837 w->right_margin_cols)
5f5c8ee5
GM
1838 != w->desired_matrix->right_margin_glyphs))
1839 *window_change_flags |= CHANGED_LEAF_MATRIX;
1840
1841 /* Actually change matrices, if allowed. Do not consider
1842 CHANGED_LEAF_MATRIX computed above here because the pool
1843 may have been changed which we don't now here. We trust
1844 that we only will be called with DIM_ONLY_P != 0 when
1845 necessary. */
1846 if (!dim_only_p)
1847 {
1848 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1849 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1850 }
1851 }
1852
1853 /* If we are part of a horizontal combination, advance x for
1854 windows to the right of W; otherwise advance y for windows
1855 below W. */
1856 if (in_horz_combination_p)
1857 x += dim.width;
177c0ea7 1858 else
5f5c8ee5
GM
1859 y += dim.height;
1860
1861 /* Remember maximum glyph matrix dimensions. */
1862 wmax = max (wmax, dim.width);
1863 hmax = max (hmax, dim.height);
1864
1865 /* Next window on same level. */
1866 window = w->next;
1867 }
1868 while (!NILP (window));
1869
1870 /* Set `total' to the total glyph matrix dimension of this window
1871 level. In a vertical combination, the width is the width of the
1872 widest window; the height is the y we finally reached, corrected
1873 by the y we started with. In a horizontal combination, the total
1874 height is the height of the tallest window, and the width is the
1875 x we finally reached, corrected by the x we started with. */
1876 if (in_horz_combination_p)
1877 {
1878 total.width = x - x0;
1879 total.height = hmax;
1880 }
177c0ea7 1881 else
5f5c8ee5
GM
1882 {
1883 total.width = wmax;
1884 total.height = y - y0;
1885 }
1886
1887 return total;
1888}
1889
1890
5d3cc43c
GM
1891/* Return the required height of glyph matrices for window W. */
1892
1893int
971de7fb 1894required_matrix_height (struct window *w)
5d3cc43c
GM
1895{
1896#ifdef HAVE_WINDOW_SYSTEM
1897 struct frame *f = XFRAME (w->frame);
177c0ea7 1898
5d3cc43c
GM
1899 if (FRAME_WINDOW_P (f))
1900 {
1901 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1ea40aa2 1902 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
5d3cc43c 1903 return (((window_pixel_height + ch_height - 1)
cc834cf9 1904 / ch_height) * w->nrows_scale_factor
5d3cc43c
GM
1905 /* One partially visible line at the top and
1906 bottom of the window. */
1907 + 2
153a073c 1908 /* 2 for header and mode line. */
5d3cc43c
GM
1909 + 2);
1910 }
1911#endif /* HAVE_WINDOW_SYSTEM */
177c0ea7 1912
7df02016 1913 return WINDOW_TOTAL_LINES (w);
5d3cc43c
GM
1914}
1915
1916
1917/* Return the required width of glyph matrices for window W. */
1918
1919int
971de7fb 1920required_matrix_width (struct window *w)
5d3cc43c
GM
1921{
1922#ifdef HAVE_WINDOW_SYSTEM
1923 struct frame *f = XFRAME (w->frame);
1924 if (FRAME_WINDOW_P (f))
1925 {
1926 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
7df02016 1927 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
177c0ea7 1928
5d3cc43c
GM
1929 /* Compute number of glyphs needed in a glyph row. */
1930 return (((window_pixel_width + ch_width - 1)
cc834cf9 1931 / ch_width) * w->ncols_scale_factor
5d3cc43c
GM
1932 /* 2 partially visible columns in the text area. */
1933 + 2
1934 /* One partially visible column at the right
1935 edge of each marginal area. */
1936 + 1 + 1);
1937 }
1938#endif /* HAVE_WINDOW_SYSTEM */
1939
7df02016 1940 return XINT (w->total_cols);
5d3cc43c
GM
1941}
1942
1943
5f5c8ee5 1944/* Allocate window matrices for window-based redisplay. W is the
8ce2650d 1945 window whose matrices must be allocated/reallocated. */
177c0ea7 1946
5f5c8ee5 1947static void
971de7fb 1948allocate_matrices_for_window_redisplay (struct window *w)
5f5c8ee5 1949{
5f5c8ee5
GM
1950 while (w)
1951 {
1952 if (!NILP (w->vchild))
5d3cc43c 1953 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
5f5c8ee5 1954 else if (!NILP (w->hchild))
5d3cc43c 1955 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
5f5c8ee5
GM
1956 else
1957 {
1958 /* W is a leaf window. */
5f5c8ee5
GM
1959 struct dim dim;
1960
1961 /* If matrices are not yet allocated, allocate them now. */
1962 if (w->desired_matrix == NULL)
1963 {
1964 w->desired_matrix = new_glyph_matrix (NULL);
1965 w->current_matrix = new_glyph_matrix (NULL);
1966 }
1967
5d3cc43c
GM
1968 dim.width = required_matrix_width (w);
1969 dim.height = required_matrix_height (w);
5f5c8ee5
GM
1970 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1971 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1972 }
177c0ea7 1973
5f5c8ee5
GM
1974 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1975 }
1976}
1977
1978
1979/* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1980 do it for all frames; otherwise do it just for the given frame.
1981 This function must be called when a new frame is created, its size
1982 changes, or its window configuration changes. */
1983
1984void
971de7fb 1985adjust_glyphs (struct frame *f)
5f5c8ee5 1986{
408f5064
GM
1987 /* Block input so that expose events and other events that access
1988 glyph matrices are not processed while we are changing them. */
1989 BLOCK_INPUT;
1990
5f5c8ee5
GM
1991 if (f)
1992 adjust_frame_glyphs (f);
1993 else
1994 {
1995 Lisp_Object tail, lisp_frame;
177c0ea7 1996
5f5c8ee5
GM
1997 FOR_EACH_FRAME (tail, lisp_frame)
1998 adjust_frame_glyphs (XFRAME (lisp_frame));
1999 }
408f5064
GM
2000
2001 UNBLOCK_INPUT;
5f5c8ee5
GM
2002}
2003
2004
2005/* Adjust frame glyphs when Emacs is initialized.
177c0ea7
JB
2006
2007 To be called from init_display.
2008
5f5c8ee5
GM
2009 We need a glyph matrix because redraw will happen soon.
2010 Unfortunately, window sizes on selected_frame are not yet set to
2011 meaningful values. I believe we can assume that there are only two
2012 windows on the frame---the mini-buffer and the root window. Frame
2013 height and width seem to be correct so far. So, set the sizes of
2014 windows to estimated values. */
2015
2016static void
971de7fb 2017adjust_frame_glyphs_initially (void)
5f5c8ee5 2018{
91fb7e1b
GM
2019 struct frame *sf = SELECTED_FRAME ();
2020 struct window *root = XWINDOW (sf->root_window);
5f5c8ee5 2021 struct window *mini = XWINDOW (root->next);
7df02016
KS
2022 int frame_lines = FRAME_LINES (sf);
2023 int frame_cols = FRAME_COLS (sf);
91fb7e1b 2024 int top_margin = FRAME_TOP_MARGIN (sf);
5f5c8ee5
GM
2025
2026 /* Do it for the root window. */
7df02016
KS
2027 XSETFASTINT (root->top_line, top_margin);
2028 XSETFASTINT (root->total_cols, frame_cols);
2029 set_window_height (sf->root_window, frame_lines - 1 - top_margin, 0);
5f5c8ee5
GM
2030
2031 /* Do it for the mini-buffer window. */
7df02016
KS
2032 XSETFASTINT (mini->top_line, frame_lines - 1);
2033 XSETFASTINT (mini->total_cols, frame_cols);
5f5c8ee5
GM
2034 set_window_height (root->next, 1, 0);
2035
91fb7e1b 2036 adjust_frame_glyphs (sf);
5f5c8ee5
GM
2037 glyphs_initialized_initially_p = 1;
2038}
177c0ea7 2039
5f5c8ee5
GM
2040
2041/* Allocate/reallocate glyph matrices of a single frame F. */
2042
2043static void
971de7fb 2044adjust_frame_glyphs (struct frame *f)
5f5c8ee5
GM
2045{
2046 if (FRAME_WINDOW_P (f))
2047 adjust_frame_glyphs_for_window_redisplay (f);
2048 else
2049 adjust_frame_glyphs_for_frame_redisplay (f);
177c0ea7 2050
5f5c8ee5
GM
2051 /* Don't forget the message buffer and the buffer for
2052 decode_mode_spec. */
2053 adjust_frame_message_buffer (f);
2054 adjust_decode_mode_spec_buffer (f);
2055
2056 f->glyphs_initialized_p = 1;
2057}
2058
075757cc
CY
2059/* Return 1 if any window in the tree has nonzero window margins. See
2060 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
2061static int
971de7fb 2062showing_window_margins_p (struct window *w)
075757cc
CY
2063{
2064 while (w)
2065 {
2066 if (!NILP (w->hchild))
2067 {
2068 if (showing_window_margins_p (XWINDOW (w->hchild)))
2069 return 1;
2070 }
2071 else if (!NILP (w->vchild))
2072 {
2073 if (showing_window_margins_p (XWINDOW (w->vchild)))
2074 return 1;
2075 }
2076 else if (!NILP (w->left_margin_cols)
2077 || !NILP (w->right_margin_cols))
2078 return 1;
3fc809e2 2079
075757cc
CY
2080 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2081 }
2082 return 0;
2083}
2084
5f5c8ee5 2085
c9f00270
GM
2086/* In the window tree with root W, build current matrices of leaf
2087 windows from the frame's current matrix. */
2088
2089static void
971de7fb 2090fake_current_matrices (Lisp_Object window)
c9f00270
GM
2091{
2092 struct window *w;
177c0ea7 2093
c9f00270
GM
2094 for (; !NILP (window); window = w->next)
2095 {
2096 w = XWINDOW (window);
177c0ea7 2097
c9f00270
GM
2098 if (!NILP (w->hchild))
2099 fake_current_matrices (w->hchild);
2100 else if (!NILP (w->vchild))
2101 fake_current_matrices (w->vchild);
2102 else
2103 {
2104 int i;
2105 struct frame *f = XFRAME (w->frame);
2106 struct glyph_matrix *m = w->current_matrix;
2107 struct glyph_matrix *fm = f->current_matrix;
2108
7df02016
KS
2109 xassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
2110 xassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
177c0ea7 2111
c9f00270
GM
2112 for (i = 0; i < m->matrix_h; ++i)
2113 {
2114 struct glyph_row *r = m->rows + i;
7df02016 2115 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
c9f00270
GM
2116
2117 xassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
2118 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
2119
2120 r->enabled_p = fr->enabled_p;
2121 if (r->enabled_p)
2122 {
2123 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
2124 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
2125 r->used[TEXT_AREA] = (m->matrix_w
2126 - r->used[LEFT_MARGIN_AREA]
2127 - r->used[RIGHT_MARGIN_AREA]);
2128 r->mode_line_p = 0;
c9f00270
GM
2129 }
2130 }
2131 }
2132 }
2133}
2134
2135
bccee4f2 2136/* Save away the contents of frame F's current frame matrix. Value is
153a073c 2137 a glyph matrix holding the contents of F's current frame matrix. */
c9f00270 2138
bccee4f2 2139static struct glyph_matrix *
971de7fb 2140save_current_matrix (struct frame *f)
c9f00270 2141{
bccee4f2
GM
2142 int i;
2143 struct glyph_matrix *saved;
e797dea6 2144
bccee4f2 2145 saved = (struct glyph_matrix *) xmalloc (sizeof *saved);
72af86bd 2146 memset (saved, 0, sizeof *saved);
bccee4f2
GM
2147 saved->nrows = f->current_matrix->nrows;
2148 saved->rows = (struct glyph_row *) xmalloc (saved->nrows
2149 * sizeof *saved->rows);
72af86bd 2150 memset (saved->rows, 0, saved->nrows * sizeof *saved->rows);
bccee4f2
GM
2151
2152 for (i = 0; i < saved->nrows; ++i)
31798cfe 2153 {
bccee4f2
GM
2154 struct glyph_row *from = f->current_matrix->rows + i;
2155 struct glyph_row *to = saved->rows + i;
2156 size_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2157 to->glyphs[TEXT_AREA] = (struct glyph *) xmalloc (nbytes);
72af86bd 2158 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
bccee4f2 2159 to->used[TEXT_AREA] = from->used[TEXT_AREA];
e797dea6 2160 }
bccee4f2
GM
2161
2162 return saved;
2163}
2164
2165
2166/* Restore the contents of frame F's current frame matrix from SAVED,
2167 and free memory associated with SAVED. */
2168
2169static void
971de7fb 2170restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
bccee4f2
GM
2171{
2172 int i;
2173
2174 for (i = 0; i < saved->nrows; ++i)
e797dea6 2175 {
bccee4f2
GM
2176 struct glyph_row *from = saved->rows + i;
2177 struct glyph_row *to = f->current_matrix->rows + i;
e797dea6 2178 size_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
72af86bd 2179 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
e797dea6 2180 to->used[TEXT_AREA] = from->used[TEXT_AREA];
bccee4f2 2181 xfree (from->glyphs[TEXT_AREA]);
e797dea6 2182 }
177c0ea7 2183
bccee4f2
GM
2184 xfree (saved->rows);
2185 xfree (saved);
c9f00270
GM
2186}
2187
2188
bccee4f2 2189
5f5c8ee5
GM
2190/* Allocate/reallocate glyph matrices of a single frame F for
2191 frame-based redisplay. */
2192
2193static void
971de7fb 2194adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
5f5c8ee5 2195{
5f5c8ee5
GM
2196 struct dim matrix_dim;
2197 int pool_changed_p;
2198 int window_change_flags;
2199 int top_window_y;
2200
2201 if (!FRAME_LIVE_P (f))
2202 return;
2203
5f5c8ee5
GM
2204 top_window_y = FRAME_TOP_MARGIN (f);
2205
2206 /* Allocate glyph pool structures if not already done. */
2207 if (f->desired_pool == NULL)
2208 {
2209 f->desired_pool = new_glyph_pool ();
2210 f->current_pool = new_glyph_pool ();
2211 }
2212
2213 /* Allocate frames matrix structures if needed. */
2214 if (f->desired_matrix == NULL)
2215 {
2216 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2217 f->current_matrix = new_glyph_matrix (f->current_pool);
2218 }
177c0ea7 2219
5f5c8ee5
GM
2220 /* Compute window glyph matrices. (This takes the mini-buffer
2221 window into account). The result is the size of the frame glyph
2222 matrix needed. The variable window_change_flags is set to a bit
2223 mask indicating whether new matrices will be allocated or
2224 existing matrices change their size or location within the frame
2225 matrix. */
2226 window_change_flags = 0;
2227 matrix_dim
2228 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2229 0, top_window_y,
5d3cc43c 2230 1,
5f5c8ee5
GM
2231 &window_change_flags);
2232
2233 /* Add in menu bar lines, if any. */
2234 matrix_dim.height += top_window_y;
2235
2236 /* Enlarge pools as necessary. */
2237 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2238 realloc_glyph_pool (f->current_pool, matrix_dim);
2239
177c0ea7 2240 /* Set up glyph pointers within window matrices. Do this only if
5f5c8ee5
GM
2241 absolutely necessary since it requires a frame redraw. */
2242 if (pool_changed_p || window_change_flags)
2243 {
2244 /* Do it for window matrices. */
2245 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
5d3cc43c 2246 0, top_window_y, 0,
5f5c8ee5
GM
2247 &window_change_flags);
2248
2249 /* Size of frame matrices must equal size of frame. Note
2250 that we are called for X frames with window widths NOT equal
2251 to the frame width (from CHANGE_FRAME_SIZE_1). */
7df02016
KS
2252 xassert (matrix_dim.width == FRAME_COLS (f)
2253 && matrix_dim.height == FRAME_LINES (f));
177c0ea7 2254
e797dea6
GM
2255 /* Pointers to glyph memory in glyph rows are exchanged during
2256 the update phase of redisplay, which means in general that a
2257 frame's current matrix consists of pointers into both the
2258 desired and current glyph pool of the frame. Adjusting a
2259 matrix sets the frame matrix up so that pointers are all into
2260 the same pool. If we want to preserve glyph contents of the
2261 current matrix over a call to adjust_glyph_matrix, we must
2262 make a copy of the current glyphs, and restore the current
2263 matrix' contents from that copy. */
2264 if (display_completed
2265 && !FRAME_GARBAGED_P (f)
2266 && matrix_dim.width == f->current_matrix->matrix_w
075757cc
CY
2267 && matrix_dim.height == f->current_matrix->matrix_h
2268 /* For some reason, the frame glyph matrix gets corrupted if
2269 any of the windows contain margins. I haven't been able
2270 to hunt down the reason, but for the moment this prevents
2271 the problem from manifesting. -- cyd */
2272 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
e797dea6 2273 {
bccee4f2
GM
2274 struct glyph_matrix *copy = save_current_matrix (f);
2275 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
e797dea6 2276 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
bccee4f2 2277 restore_current_matrix (f, copy);
e797dea6
GM
2278 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2279 }
c9f00270 2280 else
e797dea6 2281 {
bccee4f2 2282 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
e797dea6
GM
2283 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2284 SET_FRAME_GARBAGED (f);
2285 }
5f5c8ee5
GM
2286 }
2287}
2288
2289
2290/* Allocate/reallocate glyph matrices of a single frame F for
2291 window-based redisplay. */
2292
2293static void
971de7fb 2294adjust_frame_glyphs_for_window_redisplay (struct frame *f)
5f5c8ee5 2295{
5f5c8ee5
GM
2296 struct window *w;
2297
2298 xassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
177c0ea7 2299
5f5c8ee5 2300 /* Allocate/reallocate window matrices. */
5d3cc43c 2301 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
5f5c8ee5 2302
8ce2650d 2303#ifdef HAVE_X_WINDOWS
5f5c8ee5
GM
2304 /* Allocate/ reallocate matrices of the dummy window used to display
2305 the menu bar under X when no X toolkit support is available. */
488dd4c4 2306#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
5f5c8ee5
GM
2307 {
2308 /* Allocate a dummy window if not already done. */
2309 if (NILP (f->menu_bar_window))
2310 {
2311 f->menu_bar_window = make_window ();
2312 w = XWINDOW (f->menu_bar_window);
2313 XSETFRAME (w->frame, f);
2314 w->pseudo_window_p = 1;
2315 }
2316 else
2317 w = XWINDOW (f->menu_bar_window);
2318
2319 /* Set window dimensions to frame dimensions and allocate or
2320 adjust glyph matrices of W. */
7df02016
KS
2321 XSETFASTINT (w->top_line, 0);
2322 XSETFASTINT (w->left_col, 0);
2323 XSETFASTINT (w->total_lines, FRAME_MENU_BAR_LINES (f));
2324 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
5d3cc43c 2325 allocate_matrices_for_window_redisplay (w);
5f5c8ee5 2326 }
57714a86
YM
2327#endif /* not USE_X_TOOLKIT && not USE_GTK */
2328#endif /* HAVE_X_WINDOWS */
5f5c8ee5 2329
488dd4c4 2330#ifndef USE_GTK
9ea173e8
GM
2331 /* Allocate/ reallocate matrices of the tool bar window. If we
2332 don't have a tool bar window yet, make one. */
2333 if (NILP (f->tool_bar_window))
5f5c8ee5 2334 {
9ea173e8
GM
2335 f->tool_bar_window = make_window ();
2336 w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
2337 XSETFRAME (w->frame, f);
2338 w->pseudo_window_p = 1;
2339 }
2340 else
9ea173e8 2341 w = XWINDOW (f->tool_bar_window);
5f5c8ee5 2342
7df02016
KS
2343 XSETFASTINT (w->top_line, FRAME_MENU_BAR_LINES (f));
2344 XSETFASTINT (w->left_col, 0);
2345 XSETFASTINT (w->total_lines, FRAME_TOOL_BAR_LINES (f));
2346 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
5d3cc43c 2347 allocate_matrices_for_window_redisplay (w);
488dd4c4 2348#endif
5f5c8ee5
GM
2349}
2350
2351
177c0ea7 2352/* Adjust/ allocate message buffer of frame F.
5f5c8ee5 2353
5f5c8ee5
GM
2354 Note that the message buffer is never freed. Since I could not
2355 find a free in 19.34, I assume that freeing it would be
2356 problematic in some way and don't do it either.
2357
2358 (Implementation note: It should be checked if we can free it
2359 eventually without causing trouble). */
2360
2361static void
971de7fb 2362adjust_frame_message_buffer (struct frame *f)
5f5c8ee5
GM
2363{
2364 int size = FRAME_MESSAGE_BUF_SIZE (f) + 1;
2365
2366 if (FRAME_MESSAGE_BUF (f))
2367 {
2368 char *buffer = FRAME_MESSAGE_BUF (f);
2369 char *new_buffer = (char *) xrealloc (buffer, size);
5f5c8ee5
GM
2370 FRAME_MESSAGE_BUF (f) = new_buffer;
2371 }
2372 else
2373 FRAME_MESSAGE_BUF (f) = (char *) xmalloc (size);
2374}
2375
2376
2377/* Re-allocate buffer for decode_mode_spec on frame F. */
2378
2379static void
971de7fb 2380adjust_decode_mode_spec_buffer (struct frame *f)
5f5c8ee5
GM
2381{
2382 f->decode_mode_spec_buffer
2383 = (char *) xrealloc (f->decode_mode_spec_buffer,
2384 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2385}
2386
2387
2388\f
2389/**********************************************************************
2390 Freeing Glyph Matrices
2391 **********************************************************************/
2392
2393/* Free glyph memory for a frame F. F may be null. This function can
2394 be called for the same frame more than once. The root window of
2395 F may be nil when this function is called. This is the case when
2396 the function is called when F is destroyed. */
2397
2398void
971de7fb 2399free_glyphs (struct frame *f)
5f5c8ee5
GM
2400{
2401 if (f && f->glyphs_initialized_p)
2402 {
42f55fe0
GM
2403 /* Block interrupt input so that we don't get surprised by an X
2404 event while we're in an inconsistent state. */
2405 BLOCK_INPUT;
5f5c8ee5 2406 f->glyphs_initialized_p = 0;
177c0ea7 2407
5f5c8ee5
GM
2408 /* Release window sub-matrices. */
2409 if (!NILP (f->root_window))
2410 free_window_matrices (XWINDOW (f->root_window));
2411
2412 /* Free the dummy window for menu bars without X toolkit and its
2413 glyph matrices. */
2414 if (!NILP (f->menu_bar_window))
2415 {
2416 struct window *w = XWINDOW (f->menu_bar_window);
2417 free_glyph_matrix (w->desired_matrix);
2418 free_glyph_matrix (w->current_matrix);
2419 w->desired_matrix = w->current_matrix = NULL;
2420 f->menu_bar_window = Qnil;
2421 }
2422
9ea173e8
GM
2423 /* Free the tool bar window and its glyph matrices. */
2424 if (!NILP (f->tool_bar_window))
5f5c8ee5 2425 {
9ea173e8 2426 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
2427 free_glyph_matrix (w->desired_matrix);
2428 free_glyph_matrix (w->current_matrix);
2429 w->desired_matrix = w->current_matrix = NULL;
9ea173e8 2430 f->tool_bar_window = Qnil;
5f5c8ee5
GM
2431 }
2432
2433 /* Release frame glyph matrices. Reset fields to zero in
2434 case we are called a second time. */
2435 if (f->desired_matrix)
2436 {
2437 free_glyph_matrix (f->desired_matrix);
2438 free_glyph_matrix (f->current_matrix);
2439 f->desired_matrix = f->current_matrix = NULL;
2440 }
2441
2442 /* Release glyph pools. */
2443 if (f->desired_pool)
2444 {
2445 free_glyph_pool (f->desired_pool);
2446 free_glyph_pool (f->current_pool);
2447 f->desired_pool = f->current_pool = NULL;
2448 }
177c0ea7 2449
42f55fe0 2450 UNBLOCK_INPUT;
5f5c8ee5
GM
2451 }
2452}
2453
2454
2455/* Free glyph sub-matrices in the window tree rooted at W. This
2456 function may be called with a null pointer, and it may be called on
2457 the same tree more than once. */
2458
2459void
971de7fb 2460free_window_matrices (struct window *w)
5f5c8ee5
GM
2461{
2462 while (w)
2463 {
2464 if (!NILP (w->hchild))
2465 free_window_matrices (XWINDOW (w->hchild));
2466 else if (!NILP (w->vchild))
2467 free_window_matrices (XWINDOW (w->vchild));
177c0ea7 2468 else
5f5c8ee5
GM
2469 {
2470 /* This is a leaf window. Free its memory and reset fields
2471 to zero in case this function is called a second time for
2472 W. */
2473 free_glyph_matrix (w->current_matrix);
2474 free_glyph_matrix (w->desired_matrix);
2475 w->current_matrix = w->desired_matrix = NULL;
2476 }
2477
2478 /* Next window on same level. */
2479 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2480 }
2481}
2482
2483
2484/* Check glyph memory leaks. This function is called from
2485 shut_down_emacs. Note that frames are not destroyed when Emacs
2486 exits. We therefore free all glyph memory for all active frames
2487 explicitly and check that nothing is left allocated. */
2488
2489void
971de7fb 2490check_glyph_memory (void)
5f5c8ee5
GM
2491{
2492 Lisp_Object tail, frame;
2493
2494 /* Free glyph memory for all frames. */
2495 FOR_EACH_FRAME (tail, frame)
2496 free_glyphs (XFRAME (frame));
2497
2498 /* Check that nothing is left allocated. */
2499 if (glyph_matrix_count)
2500 abort ();
2501 if (glyph_pool_count)
2502 abort ();
2503}
2504
2505
2506\f
2507/**********************************************************************
2508 Building a Frame Matrix
2509 **********************************************************************/
2510
2511/* Most of the redisplay code works on glyph matrices attached to
2512 windows. This is a good solution most of the time, but it is not
2513 suitable for terminal code. Terminal output functions cannot rely
2514 on being able to set an arbitrary terminal window. Instead they
2515 must be provided with a view of the whole frame, i.e. the whole
2516 screen. We build such a view by constructing a frame matrix from
2517 window matrices in this section.
2518
2519 Windows that must be updated have their must_be_update_p flag set.
2520 For all such windows, their desired matrix is made part of the
2521 desired frame matrix. For other windows, their current matrix is
2522 made part of the desired frame matrix.
2523
2524 +-----------------+----------------+
2525 | desired | desired |
2526 | | |
2527 +-----------------+----------------+
2528 | current |
2529 | |
2530 +----------------------------------+
2531
2532 Desired window matrices can be made part of the frame matrix in a
2533 cheap way: We exploit the fact that the desired frame matrix and
2534 desired window matrices share their glyph memory. This is not
2535 possible for current window matrices. Their glyphs are copied to
2536 the desired frame matrix. The latter is equivalent to
2537 preserve_other_columns in the old redisplay.
2538
2539 Used glyphs counters for frame matrix rows are the result of adding
2540 up glyph lengths of the window matrices. A line in the frame
2541 matrix is enabled, if a corresponding line in a window matrix is
2542 enabled.
177c0ea7 2543
5f5c8ee5
GM
2544 After building the desired frame matrix, it will be passed to
2545 terminal code, which will manipulate both the desired and current
2546 frame matrix. Changes applied to the frame's current matrix have
2547 to be visible in current window matrices afterwards, of course.
2548
2549 This problem is solved like this:
2550
2551 1. Window and frame matrices share glyphs. Window matrices are
2552 constructed in a way that their glyph contents ARE the glyph
2553 contents needed in a frame matrix. Thus, any modification of
2554 glyphs done in terminal code will be reflected in window matrices
2555 automatically.
177c0ea7 2556
5f5c8ee5
GM
2557 2. Exchanges of rows in a frame matrix done by terminal code are
2558 intercepted by hook functions so that corresponding row operations
2559 on window matrices can be performed. This is necessary because we
2560 use pointers to glyphs in glyph row structures. To satisfy the
2561 assumption of point 1 above that glyphs are updated implicitly in
2562 window matrices when they are manipulated via the frame matrix,
2563 window and frame matrix must of course agree where to find the
2564 glyphs for their rows. Possible manipulations that must be
2565 mirrored are assignments of rows of the desired frame matrix to the
2566 current frame matrix and scrolling the current frame matrix. */
2567
2568/* Build frame F's desired matrix from window matrices. Only windows
2569 which have the flag must_be_updated_p set have to be updated. Menu
2570 bar lines of a frame are not covered by window matrices, so make
2571 sure not to touch them in this function. */
2572
2573static void
971de7fb 2574build_frame_matrix (struct frame *f)
5f5c8ee5
GM
2575{
2576 int i;
2577
2578 /* F must have a frame matrix when this function is called. */
2579 xassert (!FRAME_WINDOW_P (f));
177c0ea7 2580
5f5c8ee5
GM
2581 /* Clear all rows in the frame matrix covered by window matrices.
2582 Menu bar lines are not covered by windows. */
2583 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2584 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2585
2586 /* Build the matrix by walking the window tree. */
2587 build_frame_matrix_from_window_tree (f->desired_matrix,
2588 XWINDOW (FRAME_ROOT_WINDOW (f)));
2589}
2590
2591
2592/* Walk a window tree, building a frame matrix MATRIX from window
2593 matrices. W is the root of a window tree. */
2594
2595static void
971de7fb 2596build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
5f5c8ee5
GM
2597{
2598 while (w)
2599 {
2600 if (!NILP (w->hchild))
2601 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2602 else if (!NILP (w->vchild))
2603 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2604 else
2605 build_frame_matrix_from_leaf_window (matrix, w);
2606
2607 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2608 }
2609}
2610
2611
2612/* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2613 desired frame matrix built. W is a leaf window whose desired or
2614 current matrix is to be added to FRAME_MATRIX. W's flag
2615 must_be_updated_p determines which matrix it contributes to
2616 FRAME_MATRIX. If must_be_updated_p is non-zero, W's desired matrix
2617 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2618 Adding a desired matrix means setting up used counters and such in
2619 frame rows, while adding a current window matrix to FRAME_MATRIX
2620 means copying glyphs. The latter case corresponds to
2621 preserve_other_columns in the old redisplay. */
2622
2623static void
971de7fb 2624build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
5f5c8ee5
GM
2625{
2626 struct glyph_matrix *window_matrix;
2627 int window_y, frame_y;
2628 /* If non-zero, a glyph to insert at the right border of W. */
3fc809e2
KS
2629 GLYPH right_border_glyph;
2630
2631 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
5f5c8ee5
GM
2632
2633 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2634 if (w->must_be_updated_p)
2635 {
2636 window_matrix = w->desired_matrix;
2637
2638 /* Decide whether we want to add a vertical border glyph. */
2639 if (!WINDOW_RIGHTMOST_P (w))
2640 {
2641 struct Lisp_Char_Table *dp = window_display_table (w);
3fc809e2 2642 Lisp_Object gc;
b4e49aee 2643
3fc809e2
KS
2644 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2645 if (dp
2646 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc))
2647 && GLYPH_CODE_CHAR_VALID_P (gc))
2648 {
2649 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2650 spec_glyph_lookup_face (w, &right_border_glyph);
2651 }
b4e49aee 2652
3fc809e2
KS
2653 if (GLYPH_FACE (right_border_glyph) <= 0)
2654 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
5f5c8ee5
GM
2655 }
2656 }
2657 else
2658 window_matrix = w->current_matrix;
2659
2660 /* For all rows in the window matrix and corresponding rows in the
2661 frame matrix. */
2662 window_y = 0;
2663 frame_y = window_matrix->matrix_y;
2664 while (window_y < window_matrix->nrows)
2665 {
2666 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2667 struct glyph_row *window_row = window_matrix->rows + window_y;
2a8bd25f 2668 int current_row_p = window_matrix == w->current_matrix;
5f5c8ee5
GM
2669
2670 /* Fill up the frame row with spaces up to the left margin of the
2671 window row. */
2672 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2673
2674 /* Fill up areas in the window matrix row with spaces. */
2675 fill_up_glyph_row_with_spaces (window_row);
2a8bd25f
GM
2676
2677 /* If only part of W's desired matrix has been built, and
2678 window_row wasn't displayed, use the corresponding current
2679 row instead. */
2680 if (window_matrix == w->desired_matrix
2681 && !window_row->enabled_p)
2682 {
2683 window_row = w->current_matrix->rows + window_y;
2684 current_row_p = 1;
2685 }
177c0ea7 2686
2a8bd25f 2687 if (current_row_p)
5f5c8ee5 2688 {
2a8bd25f 2689 /* Copy window row to frame row. */
72af86bd
AS
2690 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2691 window_row->glyphs[0],
2692 window_matrix->matrix_w * sizeof (struct glyph));
5f5c8ee5
GM
2693 }
2694 else
2695 {
2a8bd25f 2696 xassert (window_row->enabled_p);
177c0ea7 2697
2a8bd25f
GM
2698 /* Only when a desired row has been displayed, we want
2699 the corresponding frame row to be updated. */
2700 frame_row->enabled_p = 1;
177c0ea7 2701
5f5c8ee5
GM
2702 /* Maybe insert a vertical border between horizontally adjacent
2703 windows. */
3fc809e2 2704 if (GLYPH_CHAR (right_border_glyph) != 0)
5f5c8ee5
GM
2705 {
2706 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2707 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2708 }
2709
498c08ea 2710#if GLYPH_DEBUG
a13396c9
GM
2711 /* Window row window_y must be a slice of frame row
2712 frame_y. */
5f5c8ee5 2713 xassert (glyph_row_slice_p (window_row, frame_row));
177c0ea7 2714
5f5c8ee5
GM
2715 /* If rows are in sync, we don't have to copy glyphs because
2716 frame and window share glyphs. */
177c0ea7 2717
a38634ff 2718 strcpy (w->current_matrix->method, w->desired_matrix->method);
9b0e97aa 2719 add_window_display_history (w, w->current_matrix->method, 0);
a38634ff 2720#endif
5f5c8ee5
GM
2721 }
2722
2723 /* Set number of used glyphs in the frame matrix. Since we fill
2724 up with spaces, and visit leaf windows from left to right it
2725 can be done simply. */
177c0ea7 2726 frame_row->used[TEXT_AREA]
5f5c8ee5
GM
2727 = window_matrix->matrix_x + window_matrix->matrix_w;
2728
5f5c8ee5
GM
2729 /* Next row. */
2730 ++window_y;
2731 ++frame_y;
2732 }
2733}
2734
5f81871e
RS
2735/* Given a user-specified glyph, possibly including a Lisp-level face
2736 ID, return a glyph that has a realized face ID.
2737 This is used for glyphs displayed specially and not part of the text;
2738 for instance, vertical separators, truncation markers, etc. */
2739
3fc809e2 2740void
971de7fb 2741spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
5f81871e 2742{
3fc809e2 2743 int lface_id = GLYPH_FACE (*glyph);
5f81871e
RS
2744 /* Convert the glyph's specified face to a realized (cache) face. */
2745 if (lface_id > 0)
2746 {
2747 int face_id = merge_faces (XFRAME (w->frame),
2748 Qt, lface_id, DEFAULT_FACE_ID);
3fc809e2 2749 SET_GLYPH_FACE (*glyph, face_id);
5f81871e 2750 }
5f81871e 2751}
5f5c8ee5
GM
2752
2753/* Add spaces to a glyph row ROW in a window matrix.
2754
2755 Each row has the form:
2756
2757 +---------+-----------------------------+------------+
2758 | left | text | right |
2759 +---------+-----------------------------+------------+
2760
2761 Left and right marginal areas are optional. This function adds
2762 spaces to areas so that there are no empty holes between areas.
2763 In other words: If the right area is not empty, the text area
2764 is filled up with spaces up to the right area. If the text area
2765 is not empty, the left area is filled up.
2766
2767 To be called for frame-based redisplay, only. */
2768
2769static void
971de7fb 2770fill_up_glyph_row_with_spaces (struct glyph_row *row)
5f5c8ee5
GM
2771{
2772 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2773 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2774 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2775}
2776
2777
2778/* Fill area AREA of glyph row ROW with spaces. To be called for
2779 frame-based redisplay only. */
2780
2781static void
971de7fb 2782fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
5f5c8ee5
GM
2783{
2784 if (row->glyphs[area] < row->glyphs[area + 1])
2785 {
2786 struct glyph *end = row->glyphs[area + 1];
2787 struct glyph *text = row->glyphs[area] + row->used[area];
2788
2789 while (text < end)
2790 *text++ = space_glyph;
2791 row->used[area] = text - row->glyphs[area];
2792 }
2793}
2794
2795
2796/* Add spaces to the end of ROW in a frame matrix until index UPTO is
2797 reached. In frame matrices only one area, TEXT_AREA, is used. */
2798
2799static void
971de7fb 2800fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
5f5c8ee5
GM
2801{
2802 int i = row->used[TEXT_AREA];
2803 struct glyph *glyph = row->glyphs[TEXT_AREA];
177c0ea7 2804
5f5c8ee5
GM
2805 while (i < upto)
2806 glyph[i++] = space_glyph;
2807
2808 row->used[TEXT_AREA] = i;
2809}
2810
2811
2812\f
2813/**********************************************************************
2814 Mirroring operations on frame matrices in window matrices
2815 **********************************************************************/
2816
2817/* Set frame being updated via frame-based redisplay to F. This
2818 function must be called before updates to make explicit that we are
2819 working on frame matrices or not. */
2820
2821static INLINE void
971de7fb 2822set_frame_matrix_frame (struct frame *f)
5f5c8ee5
GM
2823{
2824 frame_matrix_frame = f;
2825}
2826
2827
2828/* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2829 DESIRED_MATRIX is the desired matrix corresponding to
2830 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2831 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2832 frame_matrix_frame is non-null, this indicates that the exchange is
2833 done in frame matrices, and that we have to perform analogous
2834 operations in window matrices of frame_matrix_frame. */
2835
2836static INLINE void
971de7fb 2837make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
5f5c8ee5
GM
2838{
2839 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2840 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
e876ff42 2841 int mouse_face_p = current_row->mouse_face_p;
5f5c8ee5
GM
2842
2843 /* Do current_row = desired_row. This exchanges glyph pointers
2844 between both rows, and does a structure assignment otherwise. */
2845 assign_row (current_row, desired_row);
2846
2847 /* Enable current_row to mark it as valid. */
2848 current_row->enabled_p = 1;
e876ff42 2849 current_row->mouse_face_p = mouse_face_p;
5f5c8ee5
GM
2850
2851 /* If we are called on frame matrices, perform analogous operations
2852 for window matrices. */
2853 if (frame_matrix_frame)
2854 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2855}
2856
2857
2858/* W is the root of a window tree. FRAME_ROW is the index of a row in
2859 W's frame which has been made current (by swapping pointers between
2860 current and desired matrix). Perform analogous operations in the
2861 matrices of leaf windows in the window tree rooted at W. */
2862
2863static void
971de7fb 2864mirror_make_current (struct window *w, int frame_row)
5f5c8ee5
GM
2865{
2866 while (w)
2867 {
2868 if (!NILP (w->hchild))
2869 mirror_make_current (XWINDOW (w->hchild), frame_row);
2870 else if (!NILP (w->vchild))
2871 mirror_make_current (XWINDOW (w->vchild), frame_row);
2872 else
2873 {
2874 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2875 here because the checks performed in debug mode there
2876 will not allow the conversion. */
2877 int row = frame_row - w->desired_matrix->matrix_y;
2878
2879 /* If FRAME_ROW is within W, assign the desired row to the
2880 current row (exchanging glyph pointers). */
2881 if (row >= 0 && row < w->desired_matrix->matrix_h)
2882 {
2883 struct glyph_row *current_row
2884 = MATRIX_ROW (w->current_matrix, row);
2885 struct glyph_row *desired_row
2886 = MATRIX_ROW (w->desired_matrix, row);
a38634ff
GM
2887
2888 if (desired_row->enabled_p)
2889 assign_row (current_row, desired_row);
2890 else
2891 swap_glyph_pointers (desired_row, current_row);
5f5c8ee5
GM
2892 current_row->enabled_p = 1;
2893 }
2894 }
177c0ea7 2895
5f5c8ee5
GM
2896 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2897 }
2898}
2899
2900
2901/* Perform row dance after scrolling. We are working on the range of
2902 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2903 including) in MATRIX. COPY_FROM is a vector containing, for each
2904 row I in the range 0 <= I < NLINES, the index of the original line
2905 to move to I. This index is relative to the row range, i.e. 0 <=
2906 index < NLINES. RETAINED_P is a vector containing zero for each
2907 row 0 <= I < NLINES which is empty.
2908
2909 This function is called from do_scrolling and do_direct_scrolling. */
177c0ea7 2910
5f5c8ee5 2911void
6f704c76
DN
2912mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2913 int *copy_from, char *retained_p)
5f5c8ee5
GM
2914{
2915 /* A copy of original rows. */
2916 struct glyph_row *old_rows;
2917
2918 /* Rows to assign to. */
2919 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
177c0ea7 2920
5f5c8ee5
GM
2921 int i;
2922
2923 /* Make a copy of the original rows. */
2924 old_rows = (struct glyph_row *) alloca (nlines * sizeof *old_rows);
72af86bd 2925 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
5f5c8ee5
GM
2926
2927 /* Assign new rows, maybe clear lines. */
2928 for (i = 0; i < nlines; ++i)
2929 {
2930 int enabled_before_p = new_rows[i].enabled_p;
2931
2932 xassert (i + unchanged_at_top < matrix->nrows);
2933 xassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2934 new_rows[i] = old_rows[copy_from[i]];
2935 new_rows[i].enabled_p = enabled_before_p;
2936
2937 /* RETAINED_P is zero for empty lines. */
2938 if (!retained_p[copy_from[i]])
2939 new_rows[i].enabled_p = 0;
2940 }
2941
153a073c 2942 /* Do the same for window matrices, if MATRIX is a frame matrix. */
5f5c8ee5
GM
2943 if (frame_matrix_frame)
2944 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2945 unchanged_at_top, nlines, copy_from, retained_p);
2946}
2947
2948
a13396c9 2949/* Synchronize glyph pointers in the current matrix of window W with
5cb88111 2950 the current frame matrix. */
a13396c9
GM
2951
2952static void
971de7fb 2953sync_window_with_frame_matrix_rows (struct window *w)
a13396c9
GM
2954{
2955 struct frame *f = XFRAME (w->frame);
2956 struct glyph_row *window_row, *window_row_end, *frame_row;
075c507b 2957 int left, right, x, width;
a13396c9 2958
5cb88111 2959 /* Preconditions: W must be a leaf window on a tty frame. */
a13396c9 2960 xassert (NILP (w->hchild) && NILP (w->vchild));
a13396c9
GM
2961 xassert (!FRAME_WINDOW_P (f));
2962
7df02016
KS
2963 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2964 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
5cb88111
GM
2965 x = w->current_matrix->matrix_x;
2966 width = w->current_matrix->matrix_w;
2967
a13396c9
GM
2968 window_row = w->current_matrix->rows;
2969 window_row_end = window_row + w->current_matrix->nrows;
7df02016 2970 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
177c0ea7 2971
5cb88111 2972 for (; window_row < window_row_end; ++window_row, ++frame_row)
a13396c9 2973 {
5cb88111
GM
2974 window_row->glyphs[LEFT_MARGIN_AREA]
2975 = frame_row->glyphs[0] + x;
2976 window_row->glyphs[TEXT_AREA]
2977 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2978 window_row->glyphs[LAST_AREA]
2979 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2980 window_row->glyphs[RIGHT_MARGIN_AREA]
2981 = window_row->glyphs[LAST_AREA] - right;
a13396c9
GM
2982 }
2983}
2984
2985
2986/* Return the window in the window tree rooted in W containing frame
2987 row ROW. Value is null if none is found. */
2988
2989struct window *
971de7fb 2990frame_row_to_window (struct window *w, int row)
a13396c9
GM
2991{
2992 struct window *found = NULL;
177c0ea7 2993
a13396c9
GM
2994 while (w && !found)
2995 {
2996 if (!NILP (w->hchild))
2997 found = frame_row_to_window (XWINDOW (w->hchild), row);
2998 else if (!NILP (w->vchild))
2999 found = frame_row_to_window (XWINDOW (w->vchild), row);
7df02016
KS
3000 else if (row >= WINDOW_TOP_EDGE_LINE (w)
3001 && row < WINDOW_BOTTOM_EDGE_LINE (w))
a13396c9 3002 found = w;
177c0ea7 3003
a13396c9
GM
3004 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3005 }
3006
3007 return found;
3008}
3009
3010
5f5c8ee5
GM
3011/* Perform a line dance in the window tree rooted at W, after
3012 scrolling a frame matrix in mirrored_line_dance.
3013
3014 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
3015 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
3016 COPY_FROM is a vector containing, for each row I in the range 0 <=
3017 I < NLINES, the index of the original line to move to I. This
3018 index is relative to the row range, i.e. 0 <= index < NLINES.
3019 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
3020 which is empty. */
3021
3022static void
971de7fb 3023mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
5f5c8ee5
GM
3024{
3025 while (w)
3026 {
3027 if (!NILP (w->hchild))
3028 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
3029 nlines, copy_from, retained_p);
3030 else if (!NILP (w->vchild))
3031 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
3032 nlines, copy_from, retained_p);
3033 else
3034 {
3035 /* W is a leaf window, and we are working on its current
3036 matrix m. */
3037 struct glyph_matrix *m = w->current_matrix;
a13396c9 3038 int i, sync_p = 0;
5f5c8ee5
GM
3039 struct glyph_row *old_rows;
3040
3041 /* Make a copy of the original rows of matrix m. */
3042 old_rows = (struct glyph_row *) alloca (m->nrows * sizeof *old_rows);
72af86bd 3043 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
5f5c8ee5
GM
3044
3045 for (i = 0; i < nlines; ++i)
3046 {
3047 /* Frame relative line assigned to. */
3048 int frame_to = i + unchanged_at_top;
177c0ea7 3049
5f5c8ee5
GM
3050 /* Frame relative line assigned. */
3051 int frame_from = copy_from[i] + unchanged_at_top;
177c0ea7 3052
5f5c8ee5
GM
3053 /* Window relative line assigned to. */
3054 int window_to = frame_to - m->matrix_y;
177c0ea7 3055
5f5c8ee5
GM
3056 /* Window relative line assigned. */
3057 int window_from = frame_from - m->matrix_y;
177c0ea7 3058
5f5c8ee5
GM
3059 /* Is assigned line inside window? */
3060 int from_inside_window_p
3061 = window_from >= 0 && window_from < m->matrix_h;
177c0ea7 3062
a13396c9
GM
3063 /* Is assigned to line inside window? */
3064 int to_inside_window_p
3065 = window_to >= 0 && window_to < m->matrix_h;
177c0ea7 3066
a13396c9 3067 if (from_inside_window_p && to_inside_window_p)
5f5c8ee5 3068 {
5f5c8ee5
GM
3069 /* Enabled setting before assignment. */
3070 int enabled_before_p;
177c0ea7 3071
5f5c8ee5
GM
3072 /* Do the assignment. The enabled_p flag is saved
3073 over the assignment because the old redisplay did
3074 that. */
3075 enabled_before_p = m->rows[window_to].enabled_p;
3076 m->rows[window_to] = old_rows[window_from];
3077 m->rows[window_to].enabled_p = enabled_before_p;
177c0ea7 3078
5f5c8ee5
GM
3079 /* If frame line is empty, window line is empty, too. */
3080 if (!retained_p[copy_from[i]])
3081 m->rows[window_to].enabled_p = 0;
3082 }
a13396c9
GM
3083 else if (to_inside_window_p)
3084 {
3085 /* A copy between windows. This is an infrequent
3086 case not worth optimizing. */
3087 struct frame *f = XFRAME (w->frame);
3088 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
3089 struct window *w2;
3090 struct glyph_matrix *m2;
3091 int m2_from;
3092
fe8b8401 3093 w2 = frame_row_to_window (root, frame_from);
6183d842
RS
3094 /* ttn@surf.glug.org: when enabling menu bar using `emacs
3095 -nw', FROM_FRAME sometimes has no associated window.
3096 This check avoids a segfault if W2 is null. */
3097 if (w2)
3098 {
3099 m2 = w2->current_matrix;
3100 m2_from = frame_from - m2->matrix_y;
3101 copy_row_except_pointers (m->rows + window_to,
3102 m2->rows + m2_from);
3103
3104 /* If frame line is empty, window line is empty, too. */
3105 if (!retained_p[copy_from[i]])
3106 m->rows[window_to].enabled_p = 0;
3107 }
a13396c9
GM
3108 sync_p = 1;
3109 }
3110 else if (from_inside_window_p)
3111 sync_p = 1;
5f5c8ee5 3112 }
a13396c9
GM
3113
3114 /* If there was a copy between windows, make sure glyph
3115 pointers are in sync with the frame matrix. */
3116 if (sync_p)
3117 sync_window_with_frame_matrix_rows (w);
177c0ea7 3118
5f5c8ee5
GM
3119 /* Check that no pointers are lost. */
3120 CHECK_MATRIX (m);
3121 }
3122
3123 /* Next window on same level. */
3124 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3125 }
3126}
3127
3128
3129#if GLYPH_DEBUG
3130
3131/* Check that window and frame matrices agree about their
3132 understanding where glyphs of the rows are to find. For each
3133 window in the window tree rooted at W, check that rows in the
3134 matrices of leaf window agree with their frame matrices about
3135 glyph pointers. */
3136
3137void
3138check_window_matrix_pointers (w)
3139 struct window *w;
3140{
3141 while (w)
3142 {
3143 if (!NILP (w->hchild))
3144 check_window_matrix_pointers (XWINDOW (w->hchild));
3145 else if (!NILP (w->vchild))
3146 check_window_matrix_pointers (XWINDOW (w->vchild));
3147 else
60a8948a 3148 {
5f5c8ee5
GM
3149 struct frame *f = XFRAME (w->frame);
3150 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
3151 check_matrix_pointers (w->current_matrix, f->current_matrix);
60a8948a 3152 }
177c0ea7 3153
5f5c8ee5
GM
3154 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3155 }
3156}
3157
3158
3159/* Check that window rows are slices of frame rows. WINDOW_MATRIX is
3160 a window and FRAME_MATRIX is the corresponding frame matrix. For
3161 each row in WINDOW_MATRIX check that it's a slice of the
3162 corresponding frame row. If it isn't, abort. */
3163
3164static void
3165check_matrix_pointers (window_matrix, frame_matrix)
3166 struct glyph_matrix *window_matrix, *frame_matrix;
3167{
3168 /* Row number in WINDOW_MATRIX. */
3169 int i = 0;
3170
3171 /* Row number corresponding to I in FRAME_MATRIX. */
3172 int j = window_matrix->matrix_y;
3173
177c0ea7 3174 /* For all rows check that the row in the window matrix is a
5f5c8ee5
GM
3175 slice of the row in the frame matrix. If it isn't we didn't
3176 mirror an operation on the frame matrix correctly. */
3177 while (i < window_matrix->nrows)
3178 {
3179 if (!glyph_row_slice_p (window_matrix->rows + i,
3180 frame_matrix->rows + j))
3181 abort ();
3182 ++i, ++j;
3183 }
3184}
3185
3186#endif /* GLYPH_DEBUG != 0 */
3187
3188
3189\f
3190/**********************************************************************
3191 VPOS and HPOS translations
3192 **********************************************************************/
3193
3194#if GLYPH_DEBUG
3195
3196/* Translate vertical position VPOS which is relative to window W to a
3197 vertical position relative to W's frame. */
3198
3199static int
3200window_to_frame_vpos (w, vpos)
3201 struct window *w;
3202 int vpos;
3203{
3204 struct frame *f = XFRAME (w->frame);
177c0ea7 3205
5f5c8ee5
GM
3206 xassert (!FRAME_WINDOW_P (f));
3207 xassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
7df02016
KS
3208 vpos += WINDOW_TOP_EDGE_LINE (w);
3209 xassert (vpos >= 0 && vpos <= FRAME_LINES (f));
5f5c8ee5
GM
3210 return vpos;
3211}
3212
3213
3214/* Translate horizontal position HPOS which is relative to window W to
153a073c 3215 a horizontal position relative to W's frame. */
5f5c8ee5
GM
3216
3217static int
3218window_to_frame_hpos (w, hpos)
3219 struct window *w;
3220 int hpos;
3221{
82f4a138 3222 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
7df02016 3223 hpos += WINDOW_LEFT_EDGE_COL (w);
5f5c8ee5
GM
3224 return hpos;
3225}
177c0ea7 3226
5f5c8ee5
GM
3227#endif /* GLYPH_DEBUG */
3228
3229
3230\f
3231/**********************************************************************
3232 Redrawing Frames
3233 **********************************************************************/
3234
3235DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
7ee72033 3236 doc: /* Clear frame FRAME and output again what is supposed to appear on it. */)
5842a27b 3237 (Lisp_Object frame)
5f5c8ee5
GM
3238{
3239 struct frame *f;
3240
b7826503 3241 CHECK_LIVE_FRAME (frame);
5f5c8ee5
GM
3242 f = XFRAME (frame);
3243
3244 /* Ignore redraw requests, if frame has no glyphs yet.
3245 (Implementation note: It still has to be checked why we are
3246 called so early here). */
3247 if (!glyphs_initialized_initially_p)
3248 return Qnil;
3249
3250 update_begin (f);
3224dac1 3251#ifdef MSDOS
f2adda5e 3252 if (FRAME_MSDOS_P (f))
79d39b69 3253 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3224dac1 3254#endif
385ed61f 3255 clear_frame (f);
5f5c8ee5
GM
3256 clear_current_matrices (f);
3257 update_end (f);
daf01701 3258 if (FRAME_TERMCAP_P (f))
0b0d3e0b 3259 fflush (FRAME_TTY (f)->output);
5f5c8ee5
GM
3260 windows_or_buffers_changed++;
3261 /* Mark all windows as inaccurate, so that every window will have
3262 its redisplay done. */
3263 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3264 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3265 f->garbaged = 0;
3266 return Qnil;
3267}
3268
3269
3270/* Redraw frame F. This is nothing more than a call to the Lisp
3271 function redraw-frame. */
3272
3273void
971de7fb 3274redraw_frame (struct frame *f)
5f5c8ee5
GM
3275{
3276 Lisp_Object frame;
3277 XSETFRAME (frame, f);
3278 Fredraw_frame (frame);
3279}
3280
3281
3282DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
7ee72033 3283 doc: /* Clear and redisplay all visible frames. */)
5842a27b 3284 (void)
5f5c8ee5
GM
3285{
3286 Lisp_Object tail, frame;
3287
3288 FOR_EACH_FRAME (tail, frame)
3289 if (FRAME_VISIBLE_P (XFRAME (frame)))
3290 Fredraw_frame (frame);
3291
3292 return Qnil;
3293}
3294
3295
3296/* This is used when frame_garbaged is set. Call Fredraw_frame on all
3297 visible frames marked as garbaged. */
3298
3299void
971de7fb 3300redraw_garbaged_frames (void)
5f5c8ee5
GM
3301{
3302 Lisp_Object tail, frame;
3303
3304 FOR_EACH_FRAME (tail, frame)
3305 if (FRAME_VISIBLE_P (XFRAME (frame))
3306 && FRAME_GARBAGED_P (XFRAME (frame)))
3307 Fredraw_frame (frame);
3308}
3309
3310
3311\f
5f5c8ee5
GM
3312/***********************************************************************
3313 Frame Update
3314 ***********************************************************************/
4588ec20 3315
5f5c8ee5 3316/* Update frame F based on the data in desired matrices.
4588ec20 3317
5f5c8ee5
GM
3318 If FORCE_P is non-zero, don't let redisplay be stopped by detecting
3319 pending input. If INHIBIT_HAIRY_ID_P is non-zero, don't try
3320 scrolling.
177c0ea7 3321
5f5c8ee5 3322 Value is non-zero if redisplay was stopped due to pending input. */
4588ec20 3323
5f5c8ee5 3324int
971de7fb 3325update_frame (struct frame *f, int force_p, int inhibit_hairy_id_p)
5f5c8ee5
GM
3326{
3327 /* 1 means display has been paused because of pending input. */
3328 int paused_p;
3329 struct window *root_window = XWINDOW (f->root_window);
3330
a2130dbb
KS
3331 if (redisplay_dont_pause)
3332 force_p = 1;
d012c62b 3333#if PERIODIC_PREEMPTION_CHECKING
a2130dbb
KS
3334 else if (NILP (Vredisplay_preemption_period))
3335 force_p = 1;
3336 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
d012c62b
KS
3337 {
3338 EMACS_TIME tm;
3339 double p = XFLOATINT (Vredisplay_preemption_period);
3340 int sec, usec;
3341
993d4ab6
KS
3342 if (detect_input_pending_ignore_squeezables ())
3343 {
3344 paused_p = 1;
3345 goto do_pause;
3346 }
3347
d012c62b
KS
3348 sec = (int) p;
3349 usec = (p - sec) * 1000000;
3350
3351 EMACS_GET_TIME (tm);
3352 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3353 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3354 }
3355#endif
3356
5f5c8ee5 3357 if (FRAME_WINDOW_P (f))
4588ec20 3358 {
5f5c8ee5
GM
3359 /* We are working on window matrix basis. All windows whose
3360 flag must_be_updated_p is set have to be updated. */
3361
3362 /* Record that we are not working on frame matrices. */
3363 set_frame_matrix_frame (NULL);
3364
3365 /* Update all windows in the window tree of F, maybe stopping
3366 when pending input is detected. */
3367 update_begin (f);
3368
3369 /* Update the menu bar on X frames that don't have toolkit
3370 support. */
3371 if (WINDOWP (f->menu_bar_window))
3372 update_window (XWINDOW (f->menu_bar_window), 1);
3373
3374 /* Update the tool-bar window, if present. */
9ea173e8 3375 if (WINDOWP (f->tool_bar_window))
4588ec20 3376 {
9ea173e8 3377 struct window *w = XWINDOW (f->tool_bar_window);
5f5c8ee5
GM
3378
3379 /* Update tool-bar window. */
3380 if (w->must_be_updated_p)
4588ec20 3381 {
153a073c
PJ
3382 Lisp_Object tem;
3383
5f5c8ee5
GM
3384 update_window (w, 1);
3385 w->must_be_updated_p = 0;
3386
3387 /* Swap tool-bar strings. We swap because we want to
3388 reuse strings. */
9ea173e8
GM
3389 tem = f->current_tool_bar_string;
3390 f->current_tool_bar_string = f->desired_tool_bar_string;
3391 f->desired_tool_bar_string = tem;
4588ec20
JB
3392 }
3393 }
177c0ea7 3394
5f5c8ee5
GM
3395
3396 /* Update windows. */
3397 paused_p = update_window_tree (root_window, force_p);
3398 update_end (f);
177c0ea7 3399
6b61353c 3400 /* This flush is a performance bottleneck under X,
e581a466 3401 and it doesn't seem to be necessary anyway (in general).
6b61353c 3402 It is necessary when resizing the window with the mouse, or
e581a466 3403 at least the fringes are not redrawn in a timely manner. ++kfs */
6b61353c 3404 if (f->force_flush_display_p)
e581a466
KL
3405 {
3406 FRAME_RIF (f)->flush_display (f);
3407 f->force_flush_display_p = 0;
3408 }
4588ec20 3409 }
5f5c8ee5
GM
3410 else
3411 {
3412 /* We are working on frame matrix basis. Set the frame on whose
3413 frame matrix we operate. */
3414 set_frame_matrix_frame (f);
3415
868dd24b 3416 /* Build F's desired matrix from window matrices. */
5f5c8ee5 3417 build_frame_matrix (f);
177c0ea7 3418
868dd24b
GM
3419 /* Update the display */
3420 update_begin (f);
5f5c8ee5 3421 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
868dd24b
GM
3422 update_end (f);
3423
7d2f1216 3424 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3224dac1 3425 {
0b0d3e0b
KL
3426 if (FRAME_TTY (f)->termscript)
3427 fflush (FRAME_TTY (f)->termscript);
7d2f1216
EZ
3428 if (FRAME_TERMCAP_P (f))
3429 fflush (FRAME_TTY (f)->output);
3224dac1 3430 }
868dd24b 3431
5f5c8ee5 3432 /* Check window matrices for lost pointers. */
2d5912c1 3433#if GLYPH_DEBUG
9b0e97aa
GM
3434 check_window_matrix_pointers (root_window);
3435 add_frame_display_history (f, paused_p);
3436#endif
5f5c8ee5
GM
3437 }
3438
993d4ab6 3439 do_pause:
5f5c8ee5
GM
3440 /* Reset flags indicating that a window should be updated. */
3441 set_window_update_flags (root_window, 0);
177c0ea7 3442
868dd24b 3443 display_completed = !paused_p;
5f5c8ee5 3444 return paused_p;
4588ec20 3445}
5f5c8ee5
GM
3446
3447
4588ec20 3448\f
5f5c8ee5
GM
3449/************************************************************************
3450 Window-based updates
3451 ************************************************************************/
3452
3453/* Perform updates in window tree rooted at W. FORCE_P non-zero means
3454 don't stop updating when input is pending. */
3455
3456static int
971de7fb 3457update_window_tree (struct window *w, int force_p)
5f5c8ee5
GM
3458{
3459 int paused_p = 0;
177c0ea7 3460
5f5c8ee5
GM
3461 while (w && !paused_p)
3462 {
3463 if (!NILP (w->hchild))
3464 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3465 else if (!NILP (w->vchild))
3466 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3467 else if (w->must_be_updated_p)
3468 paused_p |= update_window (w, force_p);
3469
3470 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3471 }
3472
3473 return paused_p;
3474}
3475
3476
3477/* Update window W if its flag must_be_updated_p is non-zero. If
3478 FORCE_P is non-zero, don't stop updating if input is pending. */
3479
3480void
971de7fb 3481update_single_window (struct window *w, int force_p)
5f5c8ee5
GM
3482{
3483 if (w->must_be_updated_p)
3484 {
3485 struct frame *f = XFRAME (WINDOW_FRAME (w));
3486
3487 /* Record that this is not a frame-based redisplay. */
3488 set_frame_matrix_frame (NULL);
3489
a2130dbb
KS
3490 if (redisplay_dont_pause)
3491 force_p = 1;
d012c62b 3492#if PERIODIC_PREEMPTION_CHECKING
a2130dbb
KS
3493 else if (NILP (Vredisplay_preemption_period))
3494 force_p = 1;
3495 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
d012c62b
KS
3496 {
3497 EMACS_TIME tm;
3498 double p = XFLOATINT (Vredisplay_preemption_period);
3499 int sec, usec;
3500
3501 sec = (int) p;
3502 usec = (p - sec) * 1000000;
3503
3504 EMACS_GET_TIME (tm);
3505 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3506 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3507 }
3508#endif
3509
5f5c8ee5
GM
3510 /* Update W. */
3511 update_begin (f);
3512 update_window (w, force_p);
3513 update_end (f);
4588ec20 3514
5f5c8ee5
GM
3515 /* Reset flag in W. */
3516 w->must_be_updated_p = 0;
3517 }
3518}
4588ec20 3519
93e023fc 3520#ifdef HAVE_WINDOW_SYSTEM
4588ec20 3521
408f5064
GM
3522/* Redraw lines from the current matrix of window W that are
3523 overlapped by other rows. YB is bottom-most y-position in W. */
3524
3525static void
971de7fb 3526redraw_overlapped_rows (struct window *w, int yb)
408f5064 3527{
d8ee7803 3528 int i;
fa971ac3 3529 struct frame *f = XFRAME (WINDOW_FRAME (w));
3fc809e2 3530
408f5064
GM
3531 /* If rows overlapping others have been changed, the rows being
3532 overlapped have to be redrawn. This won't draw lines that have
3533 already been drawn in update_window_line because overlapped_p in
3534 desired rows is 0, so after row assignment overlapped_p in
3535 current rows is 0. */
3536 for (i = 0; i < w->current_matrix->nrows; ++i)
3537 {
e876ff42 3538 struct glyph_row *row = w->current_matrix->rows + i;
408f5064
GM
3539
3540 if (!row->enabled_p)
3541 break;
3542 else if (row->mode_line_p)
3543 continue;
177c0ea7 3544
408f5064
GM
3545 if (row->overlapped_p)
3546 {
3547 enum glyph_row_area area;
177c0ea7 3548
408f5064
GM
3549 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3550 {
3551 updated_row = row;
3552 updated_area = area;
fa971ac3
KL
3553 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3554 area == TEXT_AREA ? row->x : 0);
408f5064 3555 if (row->used[area])
fa971ac3
KL
3556 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3557 row->used[area]);
3558 FRAME_RIF (f)->clear_end_of_line (-1);
408f5064 3559 }
177c0ea7 3560
408f5064
GM
3561 row->overlapped_p = 0;
3562 }
3563
e876ff42 3564 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
408f5064
GM
3565 break;
3566 }
3567}
3568
3569
3570/* Redraw lines from the current matrix of window W that overlap
3571 others. YB is bottom-most y-position in W. */
3572
3573static void
971de7fb 3574redraw_overlapping_rows (struct window *w, int yb)
408f5064
GM
3575{
3576 int i, bottom_y;
3577 struct glyph_row *row;
fa971ac3 3578 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3fc809e2 3579
408f5064
GM
3580 for (i = 0; i < w->current_matrix->nrows; ++i)
3581 {
3582 row = w->current_matrix->rows + i;
3583
3584 if (!row->enabled_p)
3585 break;
3586 else if (row->mode_line_p)
3587 continue;
177c0ea7 3588
408f5064
GM
3589 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3590
b4b2c2ca 3591 if (row->overlapping_p)
408f5064 3592 {
41124e06
YM
3593 int overlaps = 0;
3594
b4b2c2ca 3595 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
41124e06
YM
3596 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3597 overlaps |= OVERLAPS_PRED;
b4b2c2ca 3598 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
41124e06
YM
3599 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3600 overlaps |= OVERLAPS_SUCC;
3601
3602 if (overlaps)
3603 {
3604 if (row->used[LEFT_MARGIN_AREA])
3605 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3606
3607 if (row->used[TEXT_AREA])
3608 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3609
3610 if (row->used[RIGHT_MARGIN_AREA])
3611 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3612
3613 /* Record in neighbour rows that ROW overwrites part of
3614 their display. */
3615 if (overlaps & OVERLAPS_PRED)
3616 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3617 if (overlaps & OVERLAPS_SUCC)
3618 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3619 }
408f5064
GM
3620 }
3621
3622 if (bottom_y >= yb)
3623 break;
3624 }
3625}
3626
93e023fc
KS
3627#endif /* HAVE_WINDOW_SYSTEM */
3628
408f5064 3629
93962bfa
GM
3630#ifdef GLYPH_DEBUG
3631
3632/* Check that no row in the current matrix of window W is enabled
3633 which is below what's displayed in the window. */
3634
3635void
971de7fb 3636check_current_matrix_flags (struct window *w)
93962bfa
GM
3637{
3638 int last_seen_p = 0;
3639 int i, yb = window_text_bottom_y (w);
3640
3641 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3642 {
3643 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3644 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3645 last_seen_p = 1;
3646 else if (last_seen_p && row->enabled_p)
3647 abort ();
3648 }
3649}
3650
3651#endif /* GLYPH_DEBUG */
3652
3653
5f5c8ee5
GM
3654/* Update display of window W. FORCE_P non-zero means that we should
3655 not stop when detecting pending input. */
3656
3657static int
971de7fb 3658update_window (struct window *w, int force_p)
4588ec20 3659{
5f5c8ee5
GM
3660 struct glyph_matrix *desired_matrix = w->desired_matrix;
3661 int paused_p;
d012c62b 3662#if !PERIODIC_PREEMPTION_CHECKING
5f5c8ee5 3663 int preempt_count = baud_rate / 2400 + 1;
d012c62b 3664#endif
597cfb3f 3665 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
b96fd3e8 3666#if GLYPH_DEBUG
5f5c8ee5 3667 /* Check that W's frame doesn't have glyph matrices. */
82f4a138 3668 xassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
4a34b529 3669#endif
5f5c8ee5
GM
3670
3671 /* Check pending input the first time so that we can quickly return. */
a2130dbb
KS
3672#if !PERIODIC_PREEMPTION_CHECKING
3673 if (!force_p)
a2d5fca0 3674 detect_input_pending_ignore_squeezables ();
d012c62b 3675#endif
4588ec20 3676
5f5c8ee5
GM
3677 /* If forced to complete the update, or if no input is pending, do
3678 the update. */
8fd9a666 3679 if (force_p || !input_pending || !NILP (do_mouse_tracking))
4588ec20 3680 {
5f5c8ee5
GM
3681 struct glyph_row *row, *end;
3682 struct glyph_row *mode_line_row;
56974e03 3683 struct glyph_row *header_line_row;
e87b8809 3684 int yb, changed_p = 0, mouse_face_overwritten_p = 0, n_updated;
5f5c8ee5
GM
3685
3686 rif->update_window_begin_hook (w);
3687 yb = window_text_bottom_y (w);
3688
153a073c
PJ
3689 /* If window has a header line, update it before everything else.
3690 Adjust y-positions of other rows by the header line height. */
5f5c8ee5
GM
3691 row = desired_matrix->rows;
3692 end = row + desired_matrix->nrows - 1;
177c0ea7 3693
5f5c8ee5 3694 if (row->mode_line_p)
56974e03
GM
3695 {
3696 header_line_row = row;
3697 ++row;
3698 }
3699 else
3700 header_line_row = NULL;
5f5c8ee5
GM
3701
3702 /* Update the mode line, if necessary. */
3703 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3704 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3705 {
3706 mode_line_row->y = yb;
3707 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
e876ff42
GM
3708 desired_matrix),
3709 &mouse_face_overwritten_p);
5f5c8ee5
GM
3710 }
3711
3712 /* Find first enabled row. Optimizations in redisplay_internal
3713 may lead to an update with only one row enabled. There may
3714 be also completely empty matrices. */
3715 while (row < end && !row->enabled_p)
3716 ++row;
177c0ea7 3717
bfdcafe1 3718 /* Try reusing part of the display by copying. */
5f5c8ee5 3719 if (row < end && !desired_matrix->no_scrolling_p)
56974e03
GM
3720 {
3721 int rc = scrolling_window (w, header_line_row != NULL);
3722 if (rc < 0)
3723 {
3724 /* All rows were found to be equal. */
3725 paused_p = 0;
3726 goto set_cursor;
3727 }
3728 else if (rc > 0)
6b61353c
KH
3729 {
3730 /* We've scrolled the display. */
3731 force_p = 1;
3732 changed_p = 1;
3733 }
5f5c8ee5
GM
3734 }
3735
3736 /* Update the rest of the lines. */
e87b8809 3737 for (n_updated = 0; row < end && (force_p || !input_pending); ++row)
b7412313 3738 if (row->enabled_p)
5f5c8ee5
GM
3739 {
3740 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3741 int i;
177c0ea7 3742
153a073c 3743 /* We'll have to play a little bit with when to
5f5c8ee5
GM
3744 detect_input_pending. If it's done too often,
3745 scrolling large windows with repeated scroll-up
3746 commands will too quickly pause redisplay. */
d012c62b
KS
3747#if PERIODIC_PREEMPTION_CHECKING
3748 if (!force_p)
3749 {
3750 EMACS_TIME tm, dif;
3751 EMACS_GET_TIME (tm);
3752 EMACS_SUB_TIME (dif, preemption_next_check, tm);
3753 if (EMACS_TIME_NEG_P (dif))
3754 {
3755 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
993d4ab6
KS
3756 if (detect_input_pending_ignore_squeezables ())
3757 break;
d012c62b
KS
3758 }
3759 }
3760#else
e87b8809 3761 if (!force_p && ++n_updated % preempt_count == 0)
a2d5fca0 3762 detect_input_pending_ignore_squeezables ();
d012c62b 3763#endif
e876ff42
GM
3764 changed_p |= update_window_line (w, vpos,
3765 &mouse_face_overwritten_p);
5f5c8ee5
GM
3766
3767 /* Mark all rows below the last visible one in the current
3768 matrix as invalid. This is necessary because of
3769 variable line heights. Consider the case of three
3770 successive redisplays, where the first displays 5
3771 lines, the second 3 lines, and the third 5 lines again.
3772 If the second redisplay wouldn't mark rows in the
3773 current matrix invalid, the third redisplay might be
3774 tempted to optimize redisplay based on lines displayed
3775 in the first redisplay. */
3776 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3777 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3778 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3779 }
3780
3781 /* Was display preempted? */
3782 paused_p = row < end;
177c0ea7 3783
56974e03 3784 set_cursor:
177c0ea7 3785
6b61353c
KH
3786 /* Update the header line after scrolling because a new header
3787 line would otherwise overwrite lines at the top of the window
3788 that can be scrolled. */
3789 if (header_line_row && header_line_row->enabled_p)
3790 {
3791 header_line_row->y = 0;
3792 update_window_line (w, 0, &mouse_face_overwritten_p);
6b61353c
KH
3793 }
3794
153a073c 3795 /* Fix the appearance of overlapping/overlapped rows. */
5f5c8ee5
GM
3796 if (!paused_p && !w->pseudo_window_p)
3797 {
93e023fc 3798#ifdef HAVE_WINDOW_SYSTEM
e876ff42
GM
3799 if (changed_p && rif->fix_overlapping_area)
3800 {
3801 redraw_overlapped_rows (w, yb);
3802 redraw_overlapping_rows (w, yb);
3803 }
93e023fc 3804#endif
177c0ea7 3805
5f5c8ee5
GM
3806 /* Make cursor visible at cursor position of W. */
3807 set_window_cursor_after_update (w);
3808
e876ff42
GM
3809#if 0 /* Check that current matrix invariants are satisfied. This is
3810 for debugging only. See the comment of check_matrix_invariants. */
5f5c8ee5
GM
3811 IF_DEBUG (check_matrix_invariants (w));
3812#endif
4588ec20 3813 }
5f5c8ee5
GM
3814
3815#if GLYPH_DEBUG
3816 /* Remember the redisplay method used to display the matrix. */
3817 strcpy (w->current_matrix->method, w->desired_matrix->method);
3818#endif
408f5064 3819
6b61353c
KH
3820#ifdef HAVE_WINDOW_SYSTEM
3821 update_window_fringes (w, 0);
3822#endif
3823
56974e03
GM
3824 /* End the update of window W. Don't set the cursor if we
3825 paused updating the display because in this case,
3826 set_window_cursor_after_update hasn't been called, and
3827 output_cursor doesn't contain the cursor location. */
3828 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
4588ec20 3829 }
5f5c8ee5
GM
3830 else
3831 paused_p = 1;
3832
2d5912c1 3833#if GLYPH_DEBUG
0cffd9d8 3834 /* check_current_matrix_flags (w); */
9b0e97aa
GM
3835 add_window_display_history (w, w->current_matrix->method, paused_p);
3836#endif
177c0ea7 3837
5f5c8ee5 3838 clear_glyph_matrix (desired_matrix);
408f5064 3839
5f5c8ee5 3840 return paused_p;
4588ec20
JB
3841}
3842
b64b3980 3843
5f5c8ee5
GM
3844/* Update the display of area AREA in window W, row number VPOS.
3845 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3846
3847static void
971de7fb 3848update_marginal_area (struct window *w, int area, int vpos)
b64b3980 3849{
5f5c8ee5 3850 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
fa971ac3 3851 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
5f5c8ee5
GM
3852
3853 /* Let functions in xterm.c know what area subsequent X positions
3854 will be relative to. */
3855 updated_area = area;
3856
3857 /* Set cursor to start of glyphs, write them, and clear to the end
3858 of the area. I don't think that something more sophisticated is
3859 necessary here, since marginal areas will not be the default. */
3860 rif->cursor_to (vpos, 0, desired_row->y, 0);
3861 if (desired_row->used[area])
3862 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3863 rif->clear_end_of_line (-1);
b64b3980 3864}
23b0200c 3865
352f1545 3866
408f5064
GM
3867/* Update the display of the text area of row VPOS in window W.
3868 Value is non-zero if display has changed. */
5f5c8ee5 3869
408f5064 3870static int
971de7fb 3871update_text_area (struct window *w, int vpos)
23b0200c 3872{
5f5c8ee5
GM
3873 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3874 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
fa971ac3 3875 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
408f5064 3876 int changed_p = 0;
5f5c8ee5
GM
3877
3878 /* Let functions in xterm.c know what area subsequent X positions
3879 will be relative to. */
3880 updated_area = TEXT_AREA;
177c0ea7 3881
5f5c8ee5
GM
3882 /* If rows are at different X or Y, or rows have different height,
3883 or the current row is marked invalid, write the entire line. */
3884 if (!current_row->enabled_p
3885 || desired_row->y != current_row->y
3886 || desired_row->ascent != current_row->ascent
408f5064
GM
3887 || desired_row->phys_ascent != current_row->phys_ascent
3888 || desired_row->phys_height != current_row->phys_height
5f5c8ee5 3889 || desired_row->visible_height != current_row->visible_height
408f5064 3890 || current_row->overlapped_p
fee8b690
CY
3891 /* This next line is necessary for correctly redrawing
3892 mouse-face areas after scrolling and other operations.
3893 However, it causes excessive flickering when mouse is moved
3894 across the mode line. Luckily, turning it off for the mode
a75b65c4
KS
3895 line doesn't seem to hurt anything. -- cyd.
3896 But it is still needed for the header line. -- kfs. */
3897 || (current_row->mouse_face_p
3898 && !(current_row->mode_line_p && vpos > 0))
5f5c8ee5
GM
3899 || current_row->x != desired_row->x)
3900 {
3901 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
177c0ea7 3902
5f5c8ee5
GM
3903 if (desired_row->used[TEXT_AREA])
3904 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3905 desired_row->used[TEXT_AREA]);
177c0ea7 3906
5f5c8ee5
GM
3907 /* Clear to end of window. */
3908 rif->clear_end_of_line (-1);
408f5064 3909 changed_p = 1;
60f2f3f1
GM
3910
3911 /* This erases the cursor. We do this here because
3912 notice_overwritten_cursor cannot easily check this, which
3913 might indicate that the whole functionality of
3914 notice_overwritten_cursor would better be implemented here.
3915 On the other hand, we need notice_overwritten_cursor as long
3916 as mouse highlighting is done asynchronously outside of
3917 redisplay. */
3918 if (vpos == w->phys_cursor.vpos)
3919 w->phys_cursor_on_p = 0;
5f5c8ee5
GM
3920 }
3921 else
3922 {
3923 int stop, i, x;
3924 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3925 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
e47306e6 3926 int overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
074b781a 3927 int desired_stop_pos = desired_row->used[TEXT_AREA];
91d227d9 3928 int abort_skipping = 0;
5f5c8ee5 3929
78897758
YM
3930 /* If the desired row extends its face to the text area end, and
3931 unless the current row also does so at the same position,
5f5c8ee5
GM
3932 make sure we write at least one glyph, so that the face
3933 extension actually takes place. */
78897758
YM
3934 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3935 && (desired_stop_pos < current_row->used[TEXT_AREA]
3936 || (desired_stop_pos == current_row->used[TEXT_AREA]
3937 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
074b781a 3938 --desired_stop_pos;
177c0ea7 3939
5f5c8ee5
GM
3940 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3941 i = 0;
3942 x = desired_row->x;
8399f8a0
GM
3943
3944 /* Loop over glyphs that current and desired row may have
3945 in common. */
5f5c8ee5 3946 while (i < stop)
352f1545 3947 {
91d227d9 3948 int can_skip_p = !abort_skipping;
177c0ea7 3949
5f5c8ee5 3950 /* Skip over glyphs that both rows have in common. These
e47306e6
GM
3951 don't have to be written. We can't skip if the last
3952 current glyph overlaps the glyph to its right. For
3953 example, consider a current row of `if ' with the `f' in
3954 Courier bold so that it overlaps the ` ' to its right.
3955 If the desired row is ` ', we would skip over the space
3956 after the `if' and there would remain a pixel from the
3957 `f' on the screen. */
3958 if (overlapping_glyphs_p && i > 0)
352f1545 3959 {
e47306e6
GM
3960 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3961 int left, right;
177c0ea7 3962
e47306e6
GM
3963 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3964 &left, &right);
91d227d9 3965 can_skip_p = (right == 0 && !abort_skipping);
352f1545 3966 }
177c0ea7 3967
6d950f4c 3968 if (can_skip_p)
352f1545 3969 {
91d227d9
CY
3970 int start_hpos = i;
3971
6d950f4c
GM
3972 while (i < stop
3973 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3974 {
3975 x += desired_glyph->pixel_width;
3976 ++desired_glyph, ++current_glyph, ++i;
3977 }
3978
3979 /* Consider the case that the current row contains "xxx
3980 ppp ggg" in italic Courier font, and the desired row
3981 is "xxx ggg". The character `p' has lbearing, `g'
3982 has not. The loop above will stop in front of the
3983 first `p' in the current row. If we would start
3984 writing glyphs there, we wouldn't erase the lbearing
3985 of the `p'. The rest of the lbearing problem is then
641bdbf3 3986 taken care of by draw_glyphs. */
6d950f4c
GM
3987 if (overlapping_glyphs_p
3988 && i > 0
3989 && i < current_row->used[TEXT_AREA]
3990 && (current_row->used[TEXT_AREA]
3991 != desired_row->used[TEXT_AREA]))
5f5c8ee5 3992 {
6d950f4c 3993 int left, right;
177c0ea7 3994
6d950f4c
GM
3995 rif->get_glyph_overhangs (current_glyph, XFRAME (w->frame),
3996 &left, &right);
3997 while (left > 0 && i > 0)
3998 {
3999 --i, --desired_glyph, --current_glyph;
4000 x -= desired_glyph->pixel_width;
4001 left -= desired_glyph->pixel_width;
4002 }
91d227d9
CY
4003
4004 /* Abort the skipping algorithm if we end up before
4005 our starting point, to avoid looping (bug#1070).
4006 This can happen when the lbearing is larger than
4007 the pixel width. */
4008 abort_skipping = (i < start_hpos);
5f5c8ee5 4009 }
352f1545 4010 }
177c0ea7 4011
5f5c8ee5
GM
4012 /* Try to avoid writing the entire rest of the desired row
4013 by looking for a resync point. This mainly prevents
4014 mode line flickering in the case the mode line is in
4015 fixed-pitch font, which it usually will be. */
4016 if (i < desired_row->used[TEXT_AREA])
4017 {
4018 int start_x = x, start_hpos = i;
4019 struct glyph *start = desired_glyph;
4020 int current_x = x;
6d950f4c
GM
4021 int skip_first_p = !can_skip_p;
4022
5f5c8ee5
GM
4023 /* Find the next glyph that's equal again. */
4024 while (i < stop
6d950f4c
GM
4025 && (skip_first_p
4026 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
5f5c8ee5
GM
4027 && x == current_x)
4028 {
4029 x += desired_glyph->pixel_width;
4030 current_x += current_glyph->pixel_width;
4031 ++desired_glyph, ++current_glyph, ++i;
6d950f4c 4032 skip_first_p = 0;
5f5c8ee5 4033 }
23b0200c 4034
5f5c8ee5
GM
4035 if (i == start_hpos || x != current_x)
4036 {
4037 i = start_hpos;
4038 x = start_x;
4039 desired_glyph = start;
4040 break;
4041 }
e47306e6 4042
5f5c8ee5
GM
4043 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
4044 rif->write_glyphs (start, i - start_hpos);
408f5064 4045 changed_p = 1;
5f5c8ee5
GM
4046 }
4047 }
177c0ea7 4048
5f5c8ee5
GM
4049 /* Write the rest. */
4050 if (i < desired_row->used[TEXT_AREA])
4051 {
4052 rif->cursor_to (vpos, i, desired_row->y, x);
4053 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
408f5064 4054 changed_p = 1;
5f5c8ee5 4055 }
177c0ea7 4056
5f5c8ee5
GM
4057 /* Maybe clear to end of line. */
4058 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
4059 {
4060 /* If new row extends to the end of the text area, nothing
4061 has to be cleared, if and only if we did a write_glyphs
4062 above. This is made sure by setting desired_stop_pos
4063 appropriately above. */
78897758
YM
4064 xassert (i < desired_row->used[TEXT_AREA]
4065 || ((desired_row->used[TEXT_AREA]
4066 == current_row->used[TEXT_AREA])
4067 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
5f5c8ee5
GM
4068 }
4069 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
4070 {
4071 /* If old row extends to the end of the text area, clear. */
4072 if (i >= desired_row->used[TEXT_AREA])
4073 rif->cursor_to (vpos, i, desired_row->y,
6b61353c 4074 desired_row->pixel_width);
5f5c8ee5 4075 rif->clear_end_of_line (-1);
408f5064 4076 changed_p = 1;
5f5c8ee5
GM
4077 }
4078 else if (desired_row->pixel_width < current_row->pixel_width)
2e8907d3 4079 {
5f5c8ee5
GM
4080 /* Otherwise clear to the end of the old row. Everything
4081 after that position should be clear already. */
4082 int x;
177c0ea7 4083
5f5c8ee5
GM
4084 if (i >= desired_row->used[TEXT_AREA])
4085 rif->cursor_to (vpos, i, desired_row->y,
6b61353c 4086 desired_row->pixel_width);
5f5c8ee5
GM
4087
4088 /* If cursor is displayed at the end of the line, make sure
4089 it's cleared. Nowadays we don't have a phys_cursor_glyph
4090 with which to erase the cursor (because this method
4091 doesn't work with lbearing/rbearing), so we must do it
4092 this way. */
4093 if (vpos == w->phys_cursor.vpos
f951a506
EZ
4094 && (desired_row->reversed_p
4095 ? (w->phys_cursor.hpos < 0)
4096 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
5f5c8ee5
GM
4097 {
4098 w->phys_cursor_on_p = 0;
4099 x = -1;
4100 }
4101 else
6b61353c 4102 x = current_row->pixel_width;
5f5c8ee5 4103 rif->clear_end_of_line (x);
408f5064 4104 changed_p = 1;
2e8907d3 4105 }
23b0200c 4106 }
408f5064
GM
4107
4108 return changed_p;
23b0200c 4109}
4588ec20 4110
5f5c8ee5 4111
408f5064
GM
4112/* Update row VPOS in window W. Value is non-zero if display has been
4113 changed. */
5f5c8ee5 4114
408f5064 4115static int
971de7fb 4116update_window_line (struct window *w, int vpos, int *mouse_face_overwritten_p)
5f5c8ee5
GM
4117{
4118 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
4119 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
fa971ac3 4120 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
408f5064 4121 int changed_p = 0;
5f5c8ee5 4122
5f5c8ee5
GM
4123 /* Set the row being updated. This is important to let xterm.c
4124 know what line height values are in effect. */
4125 updated_row = desired_row;
4126
177c0ea7
JB
4127 /* A row can be completely invisible in case a desired matrix was
4128 built with a vscroll and then make_cursor_line_fully_visible shifts
504805ac
GM
4129 the matrix. Make sure to make such rows current anyway, since
4130 we need the correct y-position, for example, in the current matrix. */
001f93f3
GM
4131 if (desired_row->mode_line_p
4132 || desired_row->visible_height > 0)
408f5064 4133 {
504805ac
GM
4134 xassert (desired_row->enabled_p);
4135
4136 /* Update display of the left margin area, if there is one. */
4137 if (!desired_row->full_width_p
7df02016 4138 && !NILP (w->left_margin_cols))
504805ac
GM
4139 {
4140 changed_p = 1;
4141 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
4142 }
177c0ea7 4143
504805ac
GM
4144 /* Update the display of the text area. */
4145 if (update_text_area (w, vpos))
4146 {
4147 changed_p = 1;
4148 if (current_row->mouse_face_p)
4149 *mouse_face_overwritten_p = 1;
4150 }
177c0ea7 4151
504805ac
GM
4152 /* Update display of the right margin area, if there is one. */
4153 if (!desired_row->full_width_p
7df02016 4154 && !NILP (w->right_margin_cols))
504805ac
GM
4155 {
4156 changed_p = 1;
4157 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
4158 }
177c0ea7 4159
504805ac
GM
4160 /* Draw truncation marks etc. */
4161 if (!current_row->enabled_p
4162 || desired_row->y != current_row->y
4163 || desired_row->visible_height != current_row->visible_height
6b61353c 4164 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
3d4a365f 4165 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
6b61353c 4166 || current_row->redraw_fringe_bitmaps_p
504805ac 4167 || desired_row->mode_line_p != current_row->mode_line_p
6b61353c 4168 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
504805ac
GM
4169 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
4170 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
4171 rif->after_update_window_line_hook (desired_row);
408f5064 4172 }
177c0ea7 4173
5f5c8ee5
GM
4174 /* Update current_row from desired_row. */
4175 make_current (w->desired_matrix, w->current_matrix, vpos);
4176 updated_row = NULL;
408f5064 4177 return changed_p;
5f5c8ee5
GM
4178}
4179
4180
4181/* Set the cursor after an update of window W. This function may only
4182 be called from update_window. */
4183
4184static void
971de7fb 4185set_window_cursor_after_update (struct window *w)
4588ec20 4186{
5f5c8ee5 4187 struct frame *f = XFRAME (w->frame);
fa971ac3 4188 struct redisplay_interface *rif = FRAME_RIF (f);
5f5c8ee5
GM
4189 int cx, cy, vpos, hpos;
4190
4191 /* Not intended for frame matrix updates. */
4192 xassert (FRAME_WINDOW_P (f));
177c0ea7 4193
b96fd3e8
GM
4194 if (cursor_in_echo_area
4195 && !NILP (echo_area_buffer[0])
4196 /* If we are showing a message instead of the mini-buffer,
4197 show the cursor for the message instead. */
4198 && XWINDOW (minibuf_window) == w
4199 && EQ (minibuf_window, echo_area_window)
5f5c8ee5
GM
4200 /* These cases apply only to the frame that contains
4201 the active mini-buffer window. */
4202 && FRAME_HAS_MINIBUF_P (f)
4203 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4204 {
4205 cx = cy = vpos = hpos = 0;
4206
4207 if (cursor_in_echo_area >= 0)
4208 {
4209 /* If the mini-buffer is several lines high, find the last
4210 line that has any text on it. Note: either all lines
4211 are enabled or none. Otherwise we wouldn't be able to
4212 determine Y. */
862b4790
GM
4213 struct glyph_row *row, *last_row;
4214 struct glyph *glyph;
4215 int yb = window_text_bottom_y (w);
5f5c8ee5 4216
862b4790 4217 last_row = NULL;
6206f8ce
GM
4218 row = w->current_matrix->rows;
4219 while (row->enabled_p
4220 && (last_row == NULL
4221 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
5f5c8ee5 4222 {
862b4790
GM
4223 if (row->used[TEXT_AREA]
4224 && row->glyphs[TEXT_AREA][0].charpos >= 0)
4225 last_row = row;
6206f8ce 4226 ++row;
862b4790 4227 }
177c0ea7 4228
5f5c8ee5
GM
4229 if (last_row)
4230 {
6206f8ce
GM
4231 struct glyph *start = last_row->glyphs[TEXT_AREA];
4232 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
862b4790 4233
b96fd3e8 4234 while (last > start && last->charpos < 0)
862b4790 4235 --last;
177c0ea7 4236
862b4790
GM
4237 for (glyph = start; glyph < last; ++glyph)
4238 {
4239 cx += glyph->pixel_width;
4240 ++hpos;
4241 }
4242
5f5c8ee5 4243 cy = last_row->y;
862b4790 4244 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
5f5c8ee5
GM
4245 }
4246 }
4247 }
4248 else
4249 {
4250 cx = w->cursor.x;
4251 cy = w->cursor.y;
4252 hpos = w->cursor.hpos;
4253 vpos = w->cursor.vpos;
4254 }
4588ec20 4255
5f5c8ee5 4256 /* Window cursor can be out of sync for horizontally split windows. */
f951a506 4257 hpos = max (-1, hpos); /* -1 is for when cursor is on the left fringe */
5f5c8ee5
GM
4258 hpos = min (w->current_matrix->matrix_w - 1, hpos);
4259 vpos = max (0, vpos);
4260 vpos = min (w->current_matrix->nrows - 1, vpos);
4261 rif->cursor_to (vpos, hpos, cy, cx);
4588ec20 4262}
4588ec20 4263
5f5c8ee5 4264
6df5d75f
GM
4265/* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4266 tree rooted at W. */
4267
4268void
971de7fb 4269set_window_update_flags (struct window *w, int on_p)
6df5d75f
GM
4270{
4271 while (w)
4272 {
4273 if (!NILP (w->hchild))
4274 set_window_update_flags (XWINDOW (w->hchild), on_p);
4275 else if (!NILP (w->vchild))
4276 set_window_update_flags (XWINDOW (w->vchild), on_p);
4277 else
4278 w->must_be_updated_p = on_p;
4279
4280 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4281 }
177c0ea7 4282}
6df5d75f
GM
4283
4284
4285\f
4286/***********************************************************************
4287 Window-Based Scrolling
4288 ***********************************************************************/
4289
4290/* Structure describing rows in scrolling_window. */
4291
4292struct row_entry
4293{
4294 /* Number of occurrences of this row in desired and current matrix. */
4295 int old_uses, new_uses;
177c0ea7 4296
6df5d75f
GM
4297 /* Vpos of row in new matrix. */
4298 int new_line_number;
4299
4300 /* Bucket index of this row_entry in the hash table row_table. */
4301 int bucket;
177c0ea7 4302
6df5d75f
GM
4303 /* The row described by this entry. */
4304 struct glyph_row *row;
177c0ea7 4305
6df5d75f
GM
4306 /* Hash collision chain. */
4307 struct row_entry *next;
4308};
4309
4310/* A pool to allocate row_entry structures from, and the size of the
4311 pool. The pool is reallocated in scrolling_window when we find
4312 that we need a larger one. */
4313
4314static struct row_entry *row_entry_pool;
4315static int row_entry_pool_size;
4316
4317/* Index of next free entry in row_entry_pool. */
4318
4319static int row_entry_idx;
4320
4321/* The hash table used during scrolling, and the table's size. This
4322 table is used to quickly identify equal rows in the desired and
4323 current matrix. */
4324
4325static struct row_entry **row_table;
4326static int row_table_size;
4327
4328/* Vectors of pointers to row_entry structures belonging to the
4329 current and desired matrix, and the size of the vectors. */
4330
4331static struct row_entry **old_lines, **new_lines;
4332static int old_lines_size, new_lines_size;
4333
4334/* A pool to allocate run structures from, and its size. */
4335
4336static struct run *run_pool;
4337static int runs_size;
4338
4339/* A vector of runs of lines found during scrolling. */
4340
4341static struct run **runs;
4342
6df5d75f
GM
4343/* Add glyph row ROW to the scrolling hash table during the scrolling
4344 of window W. */
4345
4346static INLINE struct row_entry *
971de7fb 4347add_row_entry (struct window *w, struct glyph_row *row)
6df5d75f
GM
4348{
4349 struct row_entry *entry;
4350 int i = row->hash % row_table_size;
177c0ea7 4351
6df5d75f 4352 entry = row_table[i];
e4e0bee9 4353 while (entry && !row_equal_p (w, entry->row, row, 1))
6df5d75f 4354 entry = entry->next;
177c0ea7 4355
6df5d75f
GM
4356 if (entry == NULL)
4357 {
4358 entry = row_entry_pool + row_entry_idx++;
4359 entry->row = row;
4360 entry->old_uses = entry->new_uses = 0;
4361 entry->new_line_number = 0;
4362 entry->bucket = i;
4363 entry->next = row_table[i];
4364 row_table[i] = entry;
4365 }
4366
4367 return entry;
4368}
4369
4370
5f5c8ee5 4371/* Try to reuse part of the current display of W by scrolling lines.
153a073c 4372 HEADER_LINE_P non-zero means W has a header line.
5f5c8ee5
GM
4373
4374 The algorithm is taken from Communications of the ACM, Apr78 "A
4375 Technique for Isolating Differences Between Files." It should take
4376 O(N) time.
4377
4378 A short outline of the steps of the algorithm
4379
4380 1. Skip lines equal at the start and end of both matrices.
4381
4382 2. Enter rows in the current and desired matrix into a symbol
4383 table, counting how often they appear in both matrices.
4384
4385 3. Rows that appear exactly once in both matrices serve as anchors,
4386 i.e. we assume that such lines are likely to have been moved.
4387
4388 4. Starting from anchor lines, extend regions to be scrolled both
4389 forward and backward.
4390
4391 Value is
4392
4393 -1 if all rows were found to be equal.
4394 0 to indicate that we did not scroll the display, or
4395 1 if we did scroll. */
4396
4397static int
971de7fb 4398scrolling_window (struct window *w, int header_line_p)
4588ec20 4399{
5f5c8ee5
GM
4400 struct glyph_matrix *desired_matrix = w->desired_matrix;
4401 struct glyph_matrix *current_matrix = w->current_matrix;
4402 int yb = window_text_bottom_y (w);
6df5d75f
GM
4403 int i, j, first_old, first_new, last_old, last_new;
4404 int nruns, nbytes, n, run_idx;
4405 struct row_entry *entry;
fa971ac3 4406 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
5f5c8ee5
GM
4407
4408 /* Skip over rows equal at the start. */
bfdcafe1 4409 for (i = header_line_p ? 1 : 0; i < current_matrix->nrows - 1; ++i)
5f5c8ee5 4410 {
bfdcafe1
GM
4411 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4412 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4413
4414 if (c->enabled_p
4415 && d->enabled_p
6b61353c 4416 && !d->redraw_fringe_bitmaps_p
bfdcafe1
GM
4417 && c->y == d->y
4418 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4419 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4420 && row_equal_p (w, c, d, 1))
4421 {
4422 assign_row (c, d);
4423 d->enabled_p = 0;
4424 }
4425 else
4426 break;
5f5c8ee5 4427 }
4588ec20 4428
5f5c8ee5
GM
4429 /* Give up if some rows in the desired matrix are not enabled. */
4430 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4431 return -1;
177c0ea7 4432
5f5c8ee5
GM
4433 first_old = first_new = i;
4434
4435 /* Set last_new to the index + 1 of the last enabled row in the
4436 desired matrix. */
4437 i = first_new + 1;
4438 while (i < desired_matrix->nrows - 1
4439 && MATRIX_ROW (desired_matrix, i)->enabled_p
6df5d75f 4440 && MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i)) <= yb)
5f5c8ee5
GM
4441 ++i;
4442
4443 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4444 return 0;
4445
4446 last_new = i;
4447
4448 /* Set last_old to the index + 1 of the last enabled row in the
4449 current matrix. We don't look at the enabled flag here because
4450 we plan to reuse part of the display even if other parts are
4451 disabled. */
4452 i = first_old + 1;
93962bfa
GM
4453 while (i < current_matrix->nrows - 1)
4454 {
4455 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4456 if (bottom <= yb)
4457 ++i;
4458 if (bottom >= yb)
4459 break;
4460 }
4461
5f5c8ee5
GM
4462 last_old = i;
4463
4464 /* Skip over rows equal at the bottom. */
4465 i = last_new;
4466 j = last_old;
4467 while (i - 1 > first_new
4468 && j - 1 > first_old
4469 && MATRIX_ROW (current_matrix, i - 1)->enabled_p
4470 && (MATRIX_ROW (current_matrix, i - 1)->y
4471 == MATRIX_ROW (desired_matrix, j - 1)->y)
6b61353c 4472 && !MATRIX_ROW (desired_matrix, j - 1)->redraw_fringe_bitmaps_p
5f5c8ee5
GM
4473 && row_equal_p (w,
4474 MATRIX_ROW (desired_matrix, i - 1),
e4e0bee9 4475 MATRIX_ROW (current_matrix, j - 1), 1))
5f5c8ee5
GM
4476 --i, --j;
4477 last_new = i;
4478 last_old = j;
4479
4480 /* Nothing to do if all rows are equal. */
4481 if (last_new == first_new)
4482 return 0;
4483
6df5d75f 4484 /* Reallocate vectors, tables etc. if necessary. */
177c0ea7 4485
6df5d75f
GM
4486 if (current_matrix->nrows > old_lines_size)
4487 {
4488 old_lines_size = current_matrix->nrows;
4489 nbytes = old_lines_size * sizeof *old_lines;
4490 old_lines = (struct row_entry **) xrealloc (old_lines, nbytes);
4491 }
177c0ea7 4492
6df5d75f
GM
4493 if (desired_matrix->nrows > new_lines_size)
4494 {
4495 new_lines_size = desired_matrix->nrows;
4496 nbytes = new_lines_size * sizeof *new_lines;
4497 new_lines = (struct row_entry **) xrealloc (new_lines, nbytes);
4498 }
4499
4500 n = desired_matrix->nrows + current_matrix->nrows;
4501 if (3 * n > row_table_size)
4502 {
4503 row_table_size = next_almost_prime (3 * n);
4504 nbytes = row_table_size * sizeof *row_table;
4505 row_table = (struct row_entry **) xrealloc (row_table, nbytes);
72af86bd 4506 memset (row_table, 0, nbytes);
6df5d75f
GM
4507 }
4508
4509 if (n > row_entry_pool_size)
4510 {
4511 row_entry_pool_size = n;
4512 nbytes = row_entry_pool_size * sizeof *row_entry_pool;
4513 row_entry_pool = (struct row_entry *) xrealloc (row_entry_pool, nbytes);
4514 }
4515
4516 if (desired_matrix->nrows > runs_size)
4517 {
4518 runs_size = desired_matrix->nrows;
4519 nbytes = runs_size * sizeof *runs;
4520 runs = (struct run **) xrealloc (runs, nbytes);
4521 nbytes = runs_size * sizeof *run_pool;
4522 run_pool = (struct run *) xrealloc (run_pool, nbytes);
4523 }
4524
4525 nruns = run_idx = 0;
4526 row_entry_idx = 0;
4527
4528 /* Add rows from the current and desired matrix to the hash table
4529 row_hash_table to be able to find equal ones quickly. */
177c0ea7 4530
5f5c8ee5
GM
4531 for (i = first_old; i < last_old; ++i)
4532 {
4533 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4534 {
6df5d75f
GM
4535 entry = add_row_entry (w, MATRIX_ROW (current_matrix, i));
4536 old_lines[i] = entry;
4537 ++entry->old_uses;
5f5c8ee5
GM
4538 }
4539 else
6df5d75f 4540 old_lines[i] = NULL;
5f5c8ee5
GM
4541 }
4542
5f5c8ee5
GM
4543 for (i = first_new; i < last_new; ++i)
4544 {
4545 xassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
6df5d75f
GM
4546 entry = add_row_entry (w, MATRIX_ROW (desired_matrix, i));
4547 ++entry->new_uses;
4548 entry->new_line_number = i;
4549 new_lines[i] = entry;
5f5c8ee5 4550 }
4588ec20 4551
5f5c8ee5
GM
4552 /* Identify moves based on lines that are unique and equal
4553 in both matrices. */
4554 for (i = first_old; i < last_old;)
6df5d75f
GM
4555 if (old_lines[i]
4556 && old_lines[i]->old_uses == 1
4557 && old_lines[i]->new_uses == 1)
5f5c8ee5
GM
4558 {
4559 int j, k;
6df5d75f
GM
4560 int new_line = old_lines[i]->new_line_number;
4561 struct run *run = run_pool + run_idx++;
5f5c8ee5
GM
4562
4563 /* Record move. */
4564 run->current_vpos = i;
4565 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4566 run->desired_vpos = new_line;
4567 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4568 run->nrows = 1;
4569 run->height = MATRIX_ROW (current_matrix, i)->height;
4570
4571 /* Extend backward. */
4572 j = i - 1;
4573 k = new_line - 1;
4574 while (j > first_old
4575 && k > first_new
6df5d75f 4576 && old_lines[j] == new_lines[k])
5f5c8ee5
GM
4577 {
4578 int h = MATRIX_ROW (current_matrix, j)->height;
177c0ea7
JB
4579 --run->current_vpos;
4580 --run->desired_vpos;
5f5c8ee5
GM
4581 ++run->nrows;
4582 run->height += h;
4583 run->desired_y -= h;
4584 run->current_y -= h;
4585 --j, --k;
4586 }
4588ec20 4587
5f5c8ee5
GM
4588 /* Extend forward. */
4589 j = i + 1;
4590 k = new_line + 1;
4591 while (j < last_old
4592 && k < last_new
6df5d75f 4593 && old_lines[j] == new_lines[k])
5f5c8ee5
GM
4594 {
4595 int h = MATRIX_ROW (current_matrix, j)->height;
177c0ea7 4596 ++run->nrows;
5f5c8ee5
GM
4597 run->height += h;
4598 ++j, ++k;
4599 }
19dff8dc 4600
5f5c8ee5
GM
4601 /* Insert run into list of all runs. Order runs by copied
4602 pixel lines. Note that we record runs that don't have to
4603 be copied because they are already in place. This is done
4604 because we can avoid calling update_window_line in this
4605 case. */
4606 for (j = 0; j < nruns && runs[j]->height > run->height; ++j)
4607 ;
2b4ec4d7 4608 for (k = nruns; k > j; --k)
5f5c8ee5
GM
4609 runs[k] = runs[k - 1];
4610 runs[j] = run;
4611 ++nruns;
4612
4613 i += run->nrows;
4614 }
4615 else
4616 ++i;
4588ec20 4617
5f5c8ee5
GM
4618 /* Do the moves. Do it in a way that we don't overwrite something
4619 we want to copy later on. This is not solvable in general
4620 because there is only one display and we don't have a way to
4621 exchange areas on this display. Example:
4588ec20 4622
5f5c8ee5
GM
4623 +-----------+ +-----------+
4624 | A | | B |
4625 +-----------+ --> +-----------+
4626 | B | | A |
4627 +-----------+ +-----------+
4588ec20 4628
5f5c8ee5
GM
4629 Instead, prefer bigger moves, and invalidate moves that would
4630 copy from where we copied to. */
ea0d86af 4631
5f5c8ee5
GM
4632 for (i = 0; i < nruns; ++i)
4633 if (runs[i]->nrows > 0)
4634 {
4635 struct run *r = runs[i];
24e86043 4636
5f5c8ee5
GM
4637 /* Copy on the display. */
4638 if (r->current_y != r->desired_y)
4639 {
4640 rif->scroll_run_hook (w, r);
4641
4642 /* Invalidate runs that copy from where we copied to. */
4643 for (j = i + 1; j < nruns; ++j)
4644 {
4645 struct run *p = runs[j];
177c0ea7 4646
5f5c8ee5
GM
4647 if ((p->current_y >= r->desired_y
4648 && p->current_y < r->desired_y + r->height)
4649 || (p->current_y + p->height >= r->desired_y
4650 && (p->current_y + p->height
4651 < r->desired_y + r->height)))
4652 p->nrows = 0;
4653 }
4654 }
ea0d86af 4655
5f5c8ee5
GM
4656 /* Assign matrix rows. */
4657 for (j = 0; j < r->nrows; ++j)
4658 {
4659 struct glyph_row *from, *to;
408f5064 4660 int to_overlapped_p;
e876ff42 4661
5f5c8ee5
GM
4662 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4663 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
e876ff42 4664 to_overlapped_p = to->overlapped_p;
6b61353c
KH
4665 if (!from->mode_line_p && !w->pseudo_window_p
4666 && (to->left_fringe_bitmap != from->left_fringe_bitmap
4667 || to->right_fringe_bitmap != from->right_fringe_bitmap
4668 || to->left_fringe_face_id != from->left_fringe_face_id
4669 || to->right_fringe_face_id != from->right_fringe_face_id
3d4a365f 4670 || to->overlay_arrow_bitmap != from->overlay_arrow_bitmap))
6b61353c 4671 from->redraw_fringe_bitmaps_p = 1;
5f5c8ee5
GM
4672 assign_row (to, from);
4673 to->enabled_p = 1, from->enabled_p = 0;
408f5064 4674 to->overlapped_p = to_overlapped_p;
5f5c8ee5
GM
4675 }
4676 }
15874c59 4677
6df5d75f
GM
4678 /* Clear the hash table, for the next time. */
4679 for (i = 0; i < row_entry_idx; ++i)
4680 row_table[row_entry_pool[i].bucket] = NULL;
4681
6b61353c
KH
4682 /* Value is > 0 to indicate that we scrolled the display. */
4683 return nruns;
5f5c8ee5 4684}
de83c314 4685
15874c59 4686
4588ec20 4687\f
5f5c8ee5
GM
4688/************************************************************************
4689 Frame-Based Updates
4690 ************************************************************************/
4588ec20 4691
5f5c8ee5 4692/* Update the desired frame matrix of frame F.
4588ec20 4693
5f5c8ee5
GM
4694 FORCE_P non-zero means that the update should not be stopped by
4695 pending input. INHIBIT_HAIRY_ID_P non-zero means that scrolling
4696 should not be tried.
4697
4698 Value is non-zero if update was stopped due to pending input. */
4699
4700static int
971de7fb 4701update_frame_1 (struct frame *f, int force_p, int inhibit_id_p)
4588ec20 4702{
5f5c8ee5
GM
4703 /* Frame matrices to work on. */
4704 struct glyph_matrix *current_matrix = f->current_matrix;
4705 struct glyph_matrix *desired_matrix = f->desired_matrix;
4706 int i;
4588ec20
JB
4707 int pause;
4708 int preempt_count = baud_rate / 2400 + 1;
5f5c8ee5
GM
4709
4710 xassert (current_matrix && desired_matrix);
4588ec20 4711
c37e4889
RS
4712 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4713 calculate_costs (f);
4714
d88c2b9e
RS
4715 if (preempt_count <= 0)
4716 preempt_count = 1;
4717
a2130dbb
KS
4718#if !PERIODIC_PREEMPTION_CHECKING
4719 if (!force_p && detect_input_pending_ignore_squeezables ())
4588ec20
JB
4720 {
4721 pause = 1;
4722 goto do_pause;
4723 }
d012c62b 4724#endif
4588ec20 4725
5f5c8ee5 4726 /* If we cannot insert/delete lines, it's no use trying it. */
daf01701 4727 if (!FRAME_LINE_INS_DEL_OK (f))
5f5c8ee5 4728 inhibit_id_p = 1;
7098a0fa 4729
efb859b4 4730 /* See if any of the desired lines are enabled; don't compute for
5f5c8ee5
GM
4731 i/d line if just want cursor motion. */
4732 for (i = 0; i < desired_matrix->nrows; i++)
4733 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4588ec20
JB
4734 break;
4735
4736 /* Try doing i/d line, if not yet inhibited. */
5f5c8ee5
GM
4737 if (!inhibit_id_p && i < desired_matrix->nrows)
4738 force_p |= scrolling (f);
4588ec20
JB
4739
4740 /* Update the individual lines as needed. Do bottom line first. */
5f5c8ee5
GM
4741 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4742 update_frame_line (f, desired_matrix->nrows - 1);
4588ec20 4743
5f5c8ee5
GM
4744 /* Now update the rest of the lines. */
4745 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4588ec20 4746 {
5f5c8ee5 4747 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4588ec20 4748 {
b6a65ac2 4749 if (FRAME_TERMCAP_P (f))
4588ec20
JB
4750 {
4751 /* Flush out every so many lines.
4752 Also flush out if likely to have more than 1k buffered
4753 otherwise. I'm told that some telnet connections get
4754 really screwed by more than 1k output at once. */
fced92f1
CY
4755 FILE *display_output = FRAME_TTY (f)->output;
4756 if (display_output)
4588ec20 4757 {
fced92f1
CY
4758 int outq = PENDING_OUTPUT_COUNT (display_output);
4759 if (outq > 900
4760 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4588ec20 4761 {
fced92f1
CY
4762 fflush (display_output);
4763 if (preempt_count == 1)
4764 {
a41f8bed 4765#ifdef EMACS_OUTQSIZE
fced92f1
CY
4766 if (EMACS_OUTQSIZE (0, &outq) < 0)
4767 /* Probably not a tty. Ignore the error and reset
4768 the outq count. */
4769 outq = PENDING_OUTPUT_COUNT (FRAME_TTY (f->output));
4588ec20 4770#endif
fced92f1
CY
4771 outq *= 10;
4772 if (baud_rate <= outq && baud_rate > 0)
4773 sleep (outq / baud_rate);
4774 }
4588ec20
JB
4775 }
4776 }
4588ec20
JB
4777 }
4778
d012c62b
KS
4779#if PERIODIC_PREEMPTION_CHECKING
4780 if (!force_p)
4781 {
4782 EMACS_TIME tm, dif;
4783 EMACS_GET_TIME (tm);
4784 EMACS_SUB_TIME (dif, preemption_next_check, tm);
4785 if (EMACS_TIME_NEG_P (dif))
4786 {
4787 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
993d4ab6
KS
4788 if (detect_input_pending_ignore_squeezables ())
4789 break;
d012c62b
KS
4790 }
4791 }
4792#else
4793 if (!force_p && (i - 1) % preempt_count == 0)
a2d5fca0 4794 detect_input_pending_ignore_squeezables ();
d012c62b 4795#endif
a2960116 4796
5f5c8ee5 4797 update_frame_line (f, i);
4588ec20 4798 }
4588ec20 4799 }
177c0ea7 4800
7df02016 4801 pause = (i < FRAME_LINES (f) - 1) ? i : 0;
4588ec20
JB
4802
4803 /* Now just clean up termcap drivers and set cursor, etc. */
4804 if (!pause)
4805 {
48cf7030 4806 if ((cursor_in_echo_area
5f5c8ee5 4807 /* If we are showing a message instead of the mini-buffer,
2577053b 4808 show the cursor for the message instead of for the
5f5c8ee5 4809 (now hidden) mini-buffer contents. */
2577053b
RS
4810 || (EQ (minibuf_window, selected_window)
4811 && EQ (minibuf_window, echo_area_window)
b96fd3e8 4812 && !NILP (echo_area_buffer[0])))
2577053b 4813 /* These cases apply only to the frame that contains
5f5c8ee5 4814 the active mini-buffer window. */
2577053b 4815 && FRAME_HAS_MINIBUF_P (f)
140f8645 4816 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
1113d9db 4817 {
7df02016 4818 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
648fa17d
JB
4819 int row, col;
4820
4821 if (cursor_in_echo_area < 0)
4822 {
5f5c8ee5
GM
4823 /* Negative value of cursor_in_echo_area means put
4824 cursor at beginning of line. */
648fa17d
JB
4825 row = top;
4826 col = 0;
4827 }
1113d9db 4828 else
648fa17d 4829 {
5f5c8ee5
GM
4830 /* Positive value of cursor_in_echo_area means put
4831 cursor at the end of the prompt. If the mini-buffer
4832 is several lines high, find the last line that has
4833 any text on it. */
7df02016 4834 row = FRAME_LINES (f);
177c0ea7 4835 do
648fa17d 4836 {
5f5c8ee5
GM
4837 --row;
4838 col = 0;
177c0ea7 4839
5f5c8ee5
GM
4840 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4841 {
4842 /* Frame rows are filled up with spaces that
4843 must be ignored here. */
5f5c8ee5
GM
4844 struct glyph_row *r = MATRIX_ROW (current_matrix,
4845 row);
4846 struct glyph *start = r->glyphs[TEXT_AREA];
4847 struct glyph *last = start + r->used[TEXT_AREA];
4848
4849 while (last > start
4850 && (last - 1)->charpos < 0)
4851 --last;
177c0ea7 4852
5f5c8ee5
GM
4853 col = last - start;
4854 }
648fa17d
JB
4855 }
4856 while (row > top && col == 0);
4857
6395da5c 4858 /* Make sure COL is not out of range. */
868e640e 4859 if (col >= FRAME_CURSOR_X_LIMIT (f))
648fa17d 4860 {
6395da5c 4861 /* If we have another row, advance cursor into it. */
7df02016 4862 if (row < FRAME_LINES (f) - 1)
6395da5c 4863 {
7df02016 4864 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
6395da5c
RS
4865 row++;
4866 }
4867 /* Otherwise move it back in range. */
4868 else
868e640e 4869 col = FRAME_CURSOR_X_LIMIT (f) - 1;
648fa17d
JB
4870 }
4871 }
4872
385ed61f 4873 cursor_to (f, row, col);
1113d9db 4874 }
4588ec20 4875 else
5f5c8ee5
GM
4876 {
4877 /* We have only one cursor on terminal frames. Use it to
4878 display the cursor of the selected window. */
4879 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
a871441d
GM
4880 if (w->cursor.vpos >= 0
4881 /* The cursor vpos may be temporarily out of bounds
4882 in the following situation: There is one window,
4883 with the cursor in the lower half of it. The window
4884 is split, and a message causes a redisplay before
4885 a new cursor position has been computed. */
7df02016 4886 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
5f5c8ee5
GM
4887 {
4888 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4889 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4890
7df02016
KS
4891 if (INTEGERP (w->left_margin_cols))
4892 x += XFASTINT (w->left_margin_cols);
177c0ea7 4893
7df02016 4894 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
385ed61f 4895 cursor_to (f, y, x);
5f5c8ee5
GM
4896 }
4897 }
4588ec20
JB
4898 }
4899
4588ec20
JB
4900 do_pause:
4901
5f5c8ee5 4902 clear_desired_matrices (f);
4588ec20
JB
4903 return pause;
4904}
4905
4588ec20 4906
5f5c8ee5 4907/* Do line insertions/deletions on frame F for frame-based redisplay. */
4588ec20 4908
dfcf069d 4909int
971de7fb 4910scrolling (struct frame *frame)
4588ec20
JB
4911{
4912 int unchanged_at_top, unchanged_at_bottom;
4913 int window_size;
4914 int changed_lines;
7df02016
KS
4915 int *old_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4916 int *new_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4917 int *draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4918 int *old_draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4588ec20 4919 register int i;
7df02016 4920 int free_at_end_vpos = FRAME_LINES (frame);
5f5c8ee5
GM
4921 struct glyph_matrix *current_matrix = frame->current_matrix;
4922 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4588ec20 4923
5f5c8ee5
GM
4924 if (!current_matrix)
4925 abort ();
4588ec20 4926
5f5c8ee5
GM
4927 /* Compute hash codes of all the lines. Also calculate number of
4928 changed lines, number of unchanged lines at the beginning, and
4929 number of unchanged lines at the end. */
4588ec20
JB
4930 changed_lines = 0;
4931 unchanged_at_top = 0;
7df02016
KS
4932 unchanged_at_bottom = FRAME_LINES (frame);
4933 for (i = 0; i < FRAME_LINES (frame); i++)
4588ec20
JB
4934 {
4935 /* Give up on this scrolling if some old lines are not enabled. */
5f5c8ee5 4936 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4588ec20 4937 return 0;
5f5c8ee5
GM
4938 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4939 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
f188b3c4
RS
4940 {
4941 /* This line cannot be redrawn, so don't let scrolling mess it. */
4942 new_hash[i] = old_hash[i];
4943#define INFINITY 1000000 /* Taken from scroll.c */
4944 draw_cost[i] = INFINITY;
4945 }
4588ec20 4946 else
f188b3c4 4947 {
5f5c8ee5
GM
4948 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4949 draw_cost[i] = line_draw_cost (desired_matrix, i);
f188b3c4 4950 }
4588ec20
JB
4951
4952 if (old_hash[i] != new_hash[i])
4953 {
4954 changed_lines++;
7df02016 4955 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4588ec20
JB
4956 }
4957 else if (i == unchanged_at_top)
4958 unchanged_at_top++;
5f5c8ee5 4959 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4588ec20
JB
4960 }
4961
4962 /* If changed lines are few, don't allow preemption, don't scroll. */
daf01701 4963 if ((!FRAME_SCROLL_REGION_OK (frame)
8a56675d 4964 && changed_lines < baud_rate / 2400)
7df02016 4965 || unchanged_at_bottom == FRAME_LINES (frame))
4588ec20
JB
4966 return 1;
4967
7df02016 4968 window_size = (FRAME_LINES (frame) - unchanged_at_top
4588ec20
JB
4969 - unchanged_at_bottom);
4970
daf01701 4971 if (FRAME_SCROLL_REGION_OK (frame))
4588ec20 4972 free_at_end_vpos -= unchanged_at_bottom;
daf01701 4973 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4588ec20
JB
4974 free_at_end_vpos = -1;
4975
4976 /* If large window, fast terminal and few lines in common between
5f5c8ee5 4977 current frame and desired frame, don't bother with i/d calc. */
daf01701 4978 if (!FRAME_SCROLL_REGION_OK (frame)
8a56675d 4979 && window_size >= 18 && baud_rate > 2400
4588ec20
JB
4980 && (window_size >=
4981 10 * scrolling_max_lines_saved (unchanged_at_top,
7df02016 4982 FRAME_LINES (frame) - unchanged_at_bottom,
4588ec20
JB
4983 old_hash, new_hash, draw_cost)))
4984 return 0;
4985
5f5c8ee5
GM
4986 if (window_size < 2)
4987 return 0;
4988
502b9b64 4989 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4588ec20 4990 draw_cost + unchanged_at_top - 1,
190bb91a 4991 old_draw_cost + unchanged_at_top - 1,
4588ec20
JB
4992 old_hash + unchanged_at_top - 1,
4993 new_hash + unchanged_at_top - 1,
4994 free_at_end_vpos - unchanged_at_top);
4995
4996 return 0;
4997}
4588ec20 4998
4588ec20 4999
5f5c8ee5
GM
5000/* Count the number of blanks at the start of the vector of glyphs R
5001 which is LEN glyphs long. */
4588ec20 5002
4588ec20 5003static int
971de7fb 5004count_blanks (struct glyph *r, int len)
4588ec20 5005{
5f5c8ee5 5006 int i;
177c0ea7 5007
5f5c8ee5
GM
5008 for (i = 0; i < len; ++i)
5009 if (!CHAR_GLYPH_SPACE_P (r[i]))
5010 break;
5011
5012 return i;
4588ec20
JB
5013}
5014
5f5c8ee5
GM
5015
5016/* Count the number of glyphs in common at the start of the glyph
5017 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
5018 of STR2. Value is the number of equal glyphs equal at the start. */
5019
4588ec20 5020static int
971de7fb 5021count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4588ec20 5022{
5f5c8ee5
GM
5023 struct glyph *p1 = str1;
5024 struct glyph *p2 = str2;
177c0ea7 5025
5f5c8ee5
GM
5026 while (p1 < end1
5027 && p2 < end2
42186983 5028 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
5f5c8ee5 5029 ++p1, ++p2;
177c0ea7 5030
5f5c8ee5 5031 return p1 - str1;
4588ec20
JB
5032}
5033
5f5c8ee5 5034
4588ec20 5035/* Char insertion/deletion cost vector, from term.c */
4588ec20 5036
7df02016 5037#define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS((f))])
4588ec20 5038
5f5c8ee5
GM
5039
5040/* Perform a frame-based update on line VPOS in frame FRAME. */
5041
4588ec20 5042static void
971de7fb 5043update_frame_line (struct frame *f, int vpos)
4588ec20 5044{
5f5c8ee5 5045 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4588ec20
JB
5046 int tem;
5047 int osp, nsp, begmatch, endmatch, olen, nlen;
c363a1d6
GM
5048 struct glyph_matrix *current_matrix = f->current_matrix;
5049 struct glyph_matrix *desired_matrix = f->desired_matrix;
5f5c8ee5
GM
5050 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
5051 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
5052 int must_write_whole_line_p;
daf01701 5053 int write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
c363a1d6 5054 int colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
0a894bad
GM
5055 != FACE_TTY_DEFAULT_BG_COLOR);
5056
5057 if (colored_spaces_p)
5058 write_spaces_p = 1;
5f5c8ee5 5059
26c63686
GM
5060 /* Current row not enabled means it has unknown contents. We must
5061 write the whole desired line in that case. */
5f5c8ee5
GM
5062 must_write_whole_line_p = !current_row->enabled_p;
5063 if (must_write_whole_line_p)
4588ec20 5064 {
5f5c8ee5 5065 obody = 0;
4588ec20
JB
5066 olen = 0;
5067 }
5068 else
5069 {
5f5c8ee5
GM
5070 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
5071 olen = current_row->used[TEXT_AREA];
177c0ea7 5072
4cecaab8
MB
5073 /* Ignore trailing spaces, if we can. */
5074 if (!write_spaces_p)
5075 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
5076 olen--;
4588ec20
JB
5077 }
5078
5f5c8ee5
GM
5079 current_row->enabled_p = 1;
5080 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4588ec20 5081
5f5c8ee5
GM
5082 /* If desired line is empty, just clear the line. */
5083 if (!desired_row->enabled_p)
4588ec20
JB
5084 {
5085 nlen = 0;
5086 goto just_erase;
5087 }
5088
5f5c8ee5
GM
5089 nbody = desired_row->glyphs[TEXT_AREA];
5090 nlen = desired_row->used[TEXT_AREA];
5091 nend = nbody + nlen;
5092
5093 /* If display line has unknown contents, write the whole line. */
5094 if (must_write_whole_line_p)
5095 {
26c63686 5096 /* Ignore spaces at the end, if we can. */
0a894bad 5097 if (!write_spaces_p)
74ca462f
GM
5098 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5099 --nlen;
5100
26c63686 5101 /* Write the contents of the desired line. */
74ca462f 5102 if (nlen)
26c63686 5103 {
385ed61f
KL
5104 cursor_to (f, vpos, 0);
5105 write_glyphs (f, nbody, nlen);
26c63686 5106 }
177c0ea7 5107
26c63686
GM
5108 /* Don't call clear_end_of_line if we already wrote the whole
5109 line. The cursor will not be at the right margin in that
5110 case but in the line below. */
7df02016 5111 if (nlen < FRAME_TOTAL_COLS (f))
26c63686 5112 {
385ed61f
KL
5113 cursor_to (f, vpos, nlen);
5114 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
26c63686 5115 }
58827478
GM
5116 else
5117 /* Make sure we are in the right row, otherwise cursor movement
5118 with cmgoto might use `ch' in the wrong row. */
385ed61f 5119 cursor_to (f, vpos, 0);
177c0ea7 5120
5f5c8ee5
GM
5121 make_current (desired_matrix, current_matrix, vpos);
5122 return;
5123 }
4588ec20
JB
5124
5125 /* Pretend trailing spaces are not there at all,
5126 unless for one reason or another we must write all spaces. */
4cecaab8
MB
5127 if (!write_spaces_p)
5128 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5129 nlen--;
4588ec20
JB
5130
5131 /* If there's no i/d char, quickly do the best we can without it. */
daf01701 5132 if (!FRAME_CHAR_INS_DEL_OK (f))
4588ec20 5133 {
5f5c8ee5 5134 int i, j;
4588ec20 5135
5f5c8ee5
GM
5136 /* Find the first glyph in desired row that doesn't agree with
5137 a glyph in the current row, and write the rest from there on. */
4588ec20
JB
5138 for (i = 0; i < nlen; i++)
5139 {
5f5c8ee5 5140 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
4588ec20 5141 {
5f5c8ee5
GM
5142 /* Find the end of the run of different glyphs. */
5143 j = i + 1;
5144 while (j < nlen
5145 && (j >= olen
5146 || !GLYPH_EQUAL_P (nbody + j, obody + j)
5147 || CHAR_GLYPH_PADDING_P (nbody[j])))
5148 ++j;
177c0ea7
JB
5149
5150 /* Output this run of non-matching chars. */
385ed61f
KL
5151 cursor_to (f, vpos, i);
5152 write_glyphs (f, nbody + i, j - i);
5f5c8ee5 5153 i = j - 1;
4588ec20
JB
5154
5155 /* Now find the next non-match. */
5156 }
5157 }
5158
5159 /* Clear the rest of the line, or the non-clear part of it. */
5160 if (olen > nlen)
5161 {
385ed61f
KL
5162 cursor_to (f, vpos, nlen);
5163 clear_end_of_line (f, olen);
4588ec20
JB
5164 }
5165
5f5c8ee5
GM
5166 /* Make current row = desired row. */
5167 make_current (desired_matrix, current_matrix, vpos);
4588ec20
JB
5168 return;
5169 }
5170
5f5c8ee5
GM
5171 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5172 characters in a row. */
5173
4588ec20
JB
5174 if (!olen)
5175 {
5f5c8ee5
GM
5176 /* If current line is blank, skip over initial spaces, if
5177 possible, and write the rest. */
4cecaab8 5178 if (write_spaces_p)
5f5c8ee5
GM
5179 nsp = 0;
5180 else
5181 nsp = count_blanks (nbody, nlen);
5182
4588ec20
JB
5183 if (nlen > nsp)
5184 {
385ed61f
KL
5185 cursor_to (f, vpos, nsp);
5186 write_glyphs (f, nbody + nsp, nlen - nsp);
4588ec20
JB
5187 }
5188
502b9b64 5189 /* Exchange contents between current_frame and new_frame. */
5f5c8ee5 5190 make_current (desired_matrix, current_matrix, vpos);
4588ec20
JB
5191 return;
5192 }
5193
4588ec20 5194 /* Compute number of leading blanks in old and new contents. */
5f5c8ee5 5195 osp = count_blanks (obody, olen);
4cecaab8 5196 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
4588ec20 5197
5f5c8ee5
GM
5198 /* Compute number of matching chars starting with first non-blank. */
5199 begmatch = count_match (obody + osp, obody + olen,
5200 nbody + nsp, nbody + nlen);
4588ec20
JB
5201
5202 /* Spaces in new match implicit space past the end of old. */
5203 /* A bug causing this to be a no-op was fixed in 18.29. */
0a894bad 5204 if (!write_spaces_p && osp + begmatch == olen)
4588ec20
JB
5205 {
5206 np1 = nbody + nsp;
5f5c8ee5
GM
5207 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
5208 ++begmatch;
4588ec20
JB
5209 }
5210
5211 /* Avoid doing insert/delete char
5212 just cause number of leading spaces differs
5f5c8ee5 5213 when the following text does not match. */
4588ec20
JB
5214 if (begmatch == 0 && osp != nsp)
5215 osp = nsp = min (osp, nsp);
5216
5217 /* Find matching characters at end of line */
5218 op1 = obody + olen;
5219 np1 = nbody + nlen;
5220 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5f5c8ee5
GM
5221 while (op1 > op2
5222 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
4588ec20
JB
5223 {
5224 op1--;
5225 np1--;
5226 }
5227 endmatch = obody + olen - op1;
5228
4588ec20
JB
5229 /* tem gets the distance to insert or delete.
5230 endmatch is how many characters we save by doing so.
5231 Is it worth it? */
5232
5233 tem = (nlen - nsp) - (olen - osp);
5234 if (endmatch && tem
daf01701 5235 && (!FRAME_CHAR_INS_DEL_OK (f)
8a56675d 5236 || endmatch <= char_ins_del_cost (f)[tem]))
4588ec20
JB
5237 endmatch = 0;
5238
5239 /* nsp - osp is the distance to insert or delete.
5240 If that is nonzero, begmatch is known to be nonzero also.
5241 begmatch + endmatch is how much we save by doing the ins/del.
5242 Is it worth it? */
5243
5244 if (nsp != osp
daf01701 5245 && (!FRAME_CHAR_INS_DEL_OK (f)
c363a1d6 5246 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
4588ec20
JB
5247 {
5248 begmatch = 0;
5249 endmatch = 0;
5250 osp = nsp = min (osp, nsp);
5251 }
5252
5253 /* Now go through the line, inserting, writing and
5254 deleting as appropriate. */
5255
5256 if (osp > nsp)
5257 {
385ed61f
KL
5258 cursor_to (f, vpos, nsp);
5259 delete_glyphs (f, osp - nsp);
4588ec20
JB
5260 }
5261 else if (nsp > osp)
5262 {
5263 /* If going to delete chars later in line
5264 and insert earlier in the line,
5265 must delete first to avoid losing data in the insert */
5266 if (endmatch && nlen < olen + nsp - osp)
5267 {
385ed61f
KL
5268 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5269 delete_glyphs (f, olen + nsp - osp - nlen);
4588ec20
JB
5270 olen = nlen - (nsp - osp);
5271 }
385ed61f
KL
5272 cursor_to (f, vpos, osp);
5273 insert_glyphs (f, 0, nsp - osp);
4588ec20
JB
5274 }
5275 olen += nsp - osp;
5276
5277 tem = nsp + begmatch + endmatch;
5278 if (nlen != tem || olen != tem)
5279 {
4588ec20
JB
5280 if (!endmatch || nlen == olen)
5281 {
c363a1d6
GM
5282 /* If new text being written reaches right margin, there is
5283 no need to do clear-to-eol at the end of this function
5284 (and it would not be safe, since cursor is not going to
5285 be "at the margin" after the text is done). */
7df02016 5286 if (nlen == FRAME_TOTAL_COLS (f))
4588ec20 5287 olen = 0;
c363a1d6
GM
5288
5289 /* Function write_glyphs is prepared to do nothing
5290 if passed a length <= 0. Check it here to avoid
5291 unnecessary cursor movement. */
5292 if (nlen - tem > 0)
5293 {
385ed61f
KL
5294 cursor_to (f, vpos, nsp + begmatch);
5295 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
c363a1d6 5296 }
4588ec20
JB
5297 }
5298 else if (nlen > olen)
5299 {
24e86043
KH
5300 /* Here, we used to have the following simple code:
5301 ----------------------------------------
5302 write_glyphs (nbody + nsp + begmatch, olen - tem);
5303 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5304 ----------------------------------------
5305 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5306 is a padding glyph. */
5307 int out = olen - tem; /* Columns to be overwritten originally. */
5308 int del;
5f5c8ee5 5309
385ed61f 5310 cursor_to (f, vpos, nsp + begmatch);
177c0ea7 5311
5f5c8ee5 5312 /* Calculate columns we can actually overwrite. */
c363a1d6
GM
5313 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5314 out--;
385ed61f 5315 write_glyphs (f, nbody + nsp + begmatch, out);
177c0ea7 5316
5f5c8ee5
GM
5317 /* If we left columns to be overwritten, we must delete them. */
5318 del = olen - tem - out;
c363a1d6 5319 if (del > 0)
385ed61f 5320 delete_glyphs (f, del);
177c0ea7 5321
5f5c8ee5 5322 /* At last, we insert columns not yet written out. */
385ed61f 5323 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5f5c8ee5
GM
5324 olen = nlen;
5325 }
5326 else if (olen > nlen)
5327 {
385ed61f
KL
5328 cursor_to (f, vpos, nsp + begmatch);
5329 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5330 delete_glyphs (f, olen - nlen);
5f5c8ee5
GM
5331 olen = nlen;
5332 }
bd9e3e75 5333 }
5f5c8ee5
GM
5334
5335 just_erase:
5336 /* If any unerased characters remain after the new line, erase them. */
5337 if (olen > nlen)
078b3696 5338 {
385ed61f
KL
5339 cursor_to (f, vpos, nlen);
5340 clear_end_of_line (f, olen);
078b3696 5341 }
5f5c8ee5
GM
5342
5343 /* Exchange contents between current_frame and new_frame. */
5344 make_current (desired_matrix, current_matrix, vpos);
078b3696 5345}
5f5c8ee5
GM
5346
5347
078b3696 5348\f
5f5c8ee5
GM
5349/***********************************************************************
5350 X/Y Position -> Buffer Position
5351 ***********************************************************************/
5352
a1b0a6db 5353/* Determine what's under window-relative pixel position (*X, *Y).
2396cbba 5354 Return the OBJECT (string or buffer) that's there.
6b61353c 5355 Return in *POS the position in that object.
2396cbba
EZ
5356 Adjust *X and *Y to character positions.
5357 Return in *DX and *DY the pixel coordinates of the click,
5358 relative to the top left corner of OBJECT, or relative to
5359 the top left corner of the character glyph at (*X, *Y)
5360 if OBJECT is nil.
5361 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5362 if the coordinates point to an empty area of the display. */
5f5c8ee5 5363
6b61353c 5364Lisp_Object
971de7fb 5365buffer_posn_from_coords (struct window *w, int *x, int *y, struct display_pos *pos, Lisp_Object *object, int *dx, int *dy, int *width, int *height)
4588ec20 5366{
5f5c8ee5 5367 struct it it;
707ea971 5368 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5f5c8ee5 5369 struct text_pos startp;
6b61353c
KH
5370 Lisp_Object string;
5371 struct glyph_row *row;
cedd7cd9
KS
5372#ifdef HAVE_WINDOW_SYSTEM
5373 struct image *img = 0;
5374#endif
2396cbba 5375 int x0, x1, to_x;
5f5c8ee5 5376
707ea971
CY
5377 /* We used to set current_buffer directly here, but that does the
5378 wrong thing with `face-remapping-alist' (bug#2044). */
5379 Fset_buffer (w->buffer);
5f5c8ee5
GM
5380 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5381 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5382 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5383 start_display (&it, w, startp);
177c0ea7 5384
6b61353c 5385 x0 = *x - WINDOW_LEFT_MARGIN_WIDTH (w);
2396cbba
EZ
5386
5387 /* First, move to the beginning of the row corresponding to *Y. We
5388 need to be in that row to get the correct value of base paragraph
40629f17 5389 direction for the text at (*X, *Y). */
2396cbba
EZ
5390 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5391
5392 /* TO_X is the pixel position that the iterator will compute for the
40629f17
EZ
5393 glyph at *X. We add it.first_visible_x because iterator
5394 positions include the hscroll. */
2396cbba
EZ
5395 to_x = x0 + it.first_visible_x;
5396 if (it.bidi_it.paragraph_dir == R2L)
5397 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5398 text area. This is because the iterator, even in R2L
5399 paragraphs, delivers glyphs as if they started at the left
5400 margin of the window. (When we actually produce glyphs for
5401 display, we reverse their order in PRODUCE_GLYPHS, but the
5402 iterator doesn't know about that.) The following line adjusts
5403 the pixel position to the iterator geometry, which is what
40629f17
EZ
5404 move_it_* routines use. (The -1 is because in a window whose
5405 text-area width is W, the rightmost pixel position is W-1, and
5406 it should be mirrored into zero pixel position.) */
5407 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5408
5409 /* Now move horizontally in the row to the glyph under *X. Second
5410 argument is ZV to prevent move_it_in_display_line from matching
5411 based on buffer positions. */
2396cbba 5412 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
177c0ea7 5413
707ea971 5414 Fset_buffer (old_current_buffer);
a1b0a6db 5415
6b61353c
KH
5416 *dx = x0 + it.first_visible_x - it.current_x;
5417 *dy = *y - it.current_y;
5418
5419 string = w->buffer;
5420 if (STRINGP (it.string))
5421 string = it.string;
a1b0a6db 5422 *pos = it.current;
6b61353c
KH
5423
5424#ifdef HAVE_WINDOW_SYSTEM
5425 if (it.what == IT_IMAGE)
5426 {
6b61353c
KH
5427 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5428 && !NILP (img->spec))
5429 *object = img->spec;
5430 }
5431#endif
5432
b70d98e1
KS
5433 if (it.vpos < w->current_matrix->nrows
5434 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5435 row->enabled_p))
6b61353c
KH
5436 {
5437 if (it.hpos < row->used[TEXT_AREA])
5438 {
5439 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
6b61353c 5440#ifdef HAVE_WINDOW_SYSTEM
cedd7cd9
KS
5441 if (img)
5442 {
5443 *dy -= row->ascent - glyph->ascent;
5444 *dx += glyph->slice.x;
5445 *dy += glyph->slice.y;
5446 /* Image slices positions are still relative to the entire image */
5447 *width = img->width;
5448 *height = img->height;
5449 }
5450 else
6b61353c 5451#endif
cedd7cd9
KS
5452 {
5453 *width = glyph->pixel_width;
5454 *height = glyph->ascent + glyph->descent;
5455 }
6b61353c
KH
5456 }
5457 else
5458 {
5459 *width = 0;
5460 *height = row->height;
5461 }
5462 }
5463 else
5464 {
5465 *width = *height = 0;
5466 }
5467
5468 /* Add extra (default width) columns if clicked after EOL. */
5469 x1 = max(0, it.current_x + it.pixel_width - it.first_visible_x);
5470 if (x0 > x1)
5471 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5472
5473 *x = it.hpos;
5474 *y = it.vpos;
5475
5476 return string;
5f5c8ee5 5477}
4588ec20 5478
5f5c8ee5
GM
5479
5480/* Value is the string under window-relative coordinates X/Y in the
6b61353c
KH
5481 mode line or header line (PART says which) of window W, or nil if none.
5482 *CHARPOS is set to the position in the string returned. */
5f5c8ee5
GM
5483
5484Lisp_Object
971de7fb 5485mode_line_string (struct window *w, enum window_part part, int *x, int *y, int *charpos, Lisp_Object *object, int *dx, int *dy, int *width, int *height)
5f5c8ee5
GM
5486{
5487 struct glyph_row *row;
5488 struct glyph *glyph, *end;
6b61353c 5489 int x0, y0;
5f5c8ee5
GM
5490 Lisp_Object string = Qnil;
5491
641bdbf3 5492 if (part == ON_MODE_LINE)
5f5c8ee5
GM
5493 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5494 else
045dee35 5495 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6b61353c
KH
5496 y0 = *y - row->y;
5497 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5f5c8ee5
GM
5498
5499 if (row->mode_line_p && row->enabled_p)
4588ec20 5500 {
5f5c8ee5
GM
5501 /* Find the glyph under X. If we find one with a string object,
5502 it's the one we were looking for. */
5503 glyph = row->glyphs[TEXT_AREA];
5504 end = glyph + row->used[TEXT_AREA];
cfde341f 5505 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
6b61353c
KH
5506 x0 -= glyph->pixel_width;
5507 *x = glyph - row->glyphs[TEXT_AREA];
5508 if (glyph < end)
5509 {
5510 string = glyph->object;
5511 *charpos = glyph->charpos;
5512 *width = glyph->pixel_width;
5513 *height = glyph->ascent + glyph->descent;
5514#ifdef HAVE_WINDOW_SYSTEM
5515 if (glyph->type == IMAGE_GLYPH)
5516 {
5517 struct image *img;
5518 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5519 if (img != NULL)
5520 *object = img->spec;
5521 y0 -= row->ascent - glyph->ascent;
5522 }
5523#endif
5524 }
5525 else
5526 {
5527 /* Add extra (default width) columns if clicked after EOL. */
5528 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5529 *width = 0;
5530 *height = row->height;
5531 }
5532 }
5533 else
5534 {
5535 *x = 0;
5536 x0 = 0;
5537 *width = *height = 0;
4588ec20 5538 }
5f5c8ee5 5539
6b61353c
KH
5540 *dx = x0;
5541 *dy = y0;
5542
5f5c8ee5 5543 return string;
4588ec20 5544}
5f5c8ee5
GM
5545
5546
db1db309
GM
5547/* Value is the string under window-relative coordinates X/Y in either
5548 marginal area, or nil if none. *CHARPOS is set to the position in
5549 the string returned. */
5550
5551Lisp_Object
971de7fb 5552marginal_area_string (struct window *w, enum window_part part, int *x, int *y, int *charpos, Lisp_Object *object, int *dx, int *dy, int *width, int *height)
db1db309
GM
5553{
5554 struct glyph_row *row = w->current_matrix->rows;
5555 struct glyph *glyph, *end;
6b61353c 5556 int x0, y0, i, wy = *y;
641bdbf3 5557 int area;
db1db309
GM
5558 Lisp_Object string = Qnil;
5559
641bdbf3 5560 if (part == ON_LEFT_MARGIN)
db1db309 5561 area = LEFT_MARGIN_AREA;
641bdbf3 5562 else if (part == ON_RIGHT_MARGIN)
db1db309
GM
5563 area = RIGHT_MARGIN_AREA;
5564 else
5565 abort ();
5566
5567 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5568 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5569 break;
6b61353c
KH
5570 y0 = *y - row->y;
5571 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
db1db309
GM
5572
5573 if (row->enabled_p)
5574 {
5575 /* Find the glyph under X. If we find one with a string object,
5576 it's the one we were looking for. */
db1db309 5577 if (area == RIGHT_MARGIN_AREA)
7df02016 5578 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6b61353c 5579 ? WINDOW_LEFT_FRINGE_WIDTH (w)
7df02016
KS
5580 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5581 + window_box_width (w, LEFT_MARGIN_AREA)
5582 + window_box_width (w, TEXT_AREA));
db1db309 5583 else
7df02016 5584 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6b61353c 5585 ? WINDOW_LEFT_FRINGE_WIDTH (w)
7df02016
KS
5586 : 0);
5587
6b61353c
KH
5588 glyph = row->glyphs[area];
5589 end = glyph + row->used[area];
cfde341f 5590 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
6b61353c
KH
5591 x0 -= glyph->pixel_width;
5592 *x = glyph - row->glyphs[area];
5593 if (glyph < end)
5594 {
5595 string = glyph->object;
5596 *charpos = glyph->charpos;
5597 *width = glyph->pixel_width;
5598 *height = glyph->ascent + glyph->descent;
5599#ifdef HAVE_WINDOW_SYSTEM
5600 if (glyph->type == IMAGE_GLYPH)
5601 {
5602 struct image *img;
5603 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5604 if (img != NULL)
5605 *object = img->spec;
5606 y0 -= row->ascent - glyph->ascent;
cedd7cd9
KS
5607 x0 += glyph->slice.x;
5608 y0 += glyph->slice.y;
6b61353c
KH
5609 }
5610#endif
5611 }
5612 else
5613 {
5614 /* Add extra (default width) columns if clicked after EOL. */
5615 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5616 *width = 0;
5617 *height = row->height;
5618 }
db1db309 5619 }
6b61353c
KH
5620 else
5621 {
5622 x0 = 0;
5623 *x = 0;
5624 *width = *height = 0;
5625 }
5626
5627 *dx = x0;
5628 *dy = y0;
db1db309
GM
5629
5630 return string;
5631}
5632
5633
5f5c8ee5
GM
5634/***********************************************************************
5635 Changing Frame Sizes
5636 ***********************************************************************/
4588ec20
JB
5637
5638#ifdef SIGWINCH
5f5c8ee5 5639
efb859b4 5640SIGTYPE
971de7fb
DN
5641window_change_signal (int signalnum) /* If we don't have an argument, */
5642 /* some compilers complain in signal calls. */
4588ec20
JB
5643{
5644 int width, height;
4588ec20
JB
5645 int old_errno = errno;
5646
28d7d09f 5647 struct tty_display_info *tty;
4d553a13 5648
333f1b6f
JD
5649 signal (SIGWINCH, window_change_signal);
5650 SIGNAL_THREAD_CHECK (signalnum);
5651
9628b887
KL
5652 /* The frame size change obviously applies to a single
5653 termcap-controlled terminal, but we can't decide which.
5654 Therefore, we resize the frames corresponding to each tty.
9628b887
KL
5655 */
5656 for (tty = tty_list; tty; tty = tty->next) {
4588ec20 5657
0a125897
KL
5658 if (! tty->term_initted)
5659 continue;
4588ec20 5660
db878925
DN
5661 /* Suspended tty frames have tty->input == NULL avoid trying to
5662 use it. */
5663 if (!tty->input)
5664 continue;
5665
0b0d3e0b 5666 get_tty_size (fileno (tty->input), &width, &height);
3fc809e2 5667
bedeffcf 5668 if (width > 5 && height > 2) {
9628b887 5669 Lisp_Object tail, frame;
3fc809e2 5670
9628b887 5671 FOR_EACH_FRAME (tail, frame)
4d553a13
KL
5672 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5673 /* Record the new sizes, but don't reallocate the data
5674 structures now. Let that be done later outside of the
5675 signal handler. */
5676 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
9628b887 5677 }
4588ec20 5678 }
3fc809e2 5679
4588ec20
JB
5680 errno = old_errno;
5681}
5682#endif /* SIGWINCH */
5683
5684
b96fd3e8
GM
5685/* Do any change in frame size that was requested by a signal. SAFE
5686 non-zero means this function is called from a place where it is
5687 safe to change frame sizes while a redisplay is in progress. */
4588ec20 5688
dfcf069d 5689void
971de7fb 5690do_pending_window_change (int safe)
4588ec20
JB
5691{
5692 /* If window_change_signal should have run before, run it now. */
b96fd3e8
GM
5693 if (redisplaying_p && !safe)
5694 return;
177c0ea7 5695
4588ec20
JB
5696 while (delayed_size_change)
5697 {
35f56f96 5698 Lisp_Object tail, frame;
4588ec20
JB
5699
5700 delayed_size_change = 0;
5701
35f56f96 5702 FOR_EACH_FRAME (tail, frame)
4588ec20 5703 {
5f5c8ee5 5704 struct frame *f = XFRAME (frame);
35f56f96 5705
7df02016
KS
5706 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5707 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5708 0, 0, safe);
4588ec20
JB
5709 }
5710 }
5711}
5712
5713
502b9b64 5714/* Change the frame height and/or width. Values may be given as zero to
177c0ea7 5715 indicate no change is to take place.
4588ec20 5716
b6a65ac2
JB
5717 If DELAY is non-zero, then assume we're being called from a signal
5718 handler, and queue the change for later - perhaps the next
5719 redisplay. Since this tries to resize windows, we can't call it
b96fd3e8
GM
5720 from a signal handler.
5721
5722 SAFE non-zero means this function is called from a place where it's
5723 safe to change frame sizes while a redisplay is in progress. */
b6a65ac2 5724
dfcf069d 5725void
971de7fb 5726change_frame_size (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
45140e01
RS
5727{
5728 Lisp_Object tail, frame;
3826ea1a 5729
9628b887 5730 if (FRAME_MSDOS_P (f))
45140e01 5731 {
9628b887
KL
5732 /* On MS-DOS, all frames use the same screen, so a change in
5733 size affects all frames. Termcap now supports multiple
5734 ttys. */
45140e01 5735 FOR_EACH_FRAME (tail, frame)
8a376b3b 5736 if (! FRAME_WINDOW_P (XFRAME (frame)))
45140e01 5737 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
b96fd3e8 5738 pretend, delay, safe);
45140e01
RS
5739 }
5740 else
b96fd3e8 5741 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
45140e01
RS
5742}
5743
5744static void
971de7fb 5745change_frame_size_1 (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
4588ec20 5746{
7df02016 5747 int new_frame_total_cols;
aed13378 5748 int count = SPECPDL_INDEX ();
3826ea1a 5749
4588ec20 5750 /* If we can't deal with the change now, queue it for later. */
b96fd3e8 5751 if (delay || (redisplaying_p && !safe))
4588ec20 5752 {
7df02016
KS
5753 f->new_text_lines = newheight;
5754 f->new_text_cols = newwidth;
4588ec20
JB
5755 delayed_size_change = 1;
5756 return;
5757 }
5758
502b9b64 5759 /* This size-change overrides any pending one for this frame. */
7df02016
KS
5760 f->new_text_lines = 0;
5761 f->new_text_cols = 0;
b6a65ac2 5762
08f7aa3e 5763 /* If an argument is zero, set it to the current value. */
ae19c6f2 5764 if (newheight == 0)
7df02016 5765 newheight = FRAME_LINES (f);
ae19c6f2 5766 if (newwidth == 0)
7df02016 5767 newwidth = FRAME_COLS (f);
3826ea1a 5768
5f5c8ee5
GM
5769 /* Compute width of windows in F.
5770 This is the width of the frame without vertical scroll bars. */
7df02016 5771 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
3826ea1a 5772
b6a65ac2 5773 /* Round up to the smallest acceptable size. */
5f5c8ee5 5774 check_frame_size (f, &newheight, &newwidth);
b6a65ac2
JB
5775
5776 /* If we're not changing the frame size, quit now. */
5075d853
JD
5777 /* Frame width may be unchanged but the text portion may change, for example,
5778 fullscreen and remove/add scroll bar. */
7df02016 5779 if (newheight == FRAME_LINES (f)
5075d853
JD
5780 && newwidth == FRAME_COLS (f) // text portion unchanged
5781 && new_frame_total_cols == FRAME_TOTAL_COLS (f)) // frame width unchanged
4588ec20
JB
5782 return;
5783
cbb95688
RS
5784 BLOCK_INPUT;
5785
886a8a6c
KH
5786#ifdef MSDOS
5787 /* We only can set screen dimensions to certain values supported
5788 by our video hardware. Try to find the smallest size greater
5789 or equal to the requested dimensions. */
5790 dos_set_window_size (&newheight, &newwidth);
5791#endif
5792
7df02016 5793 if (newheight != FRAME_LINES (f))
4588ec20 5794 {
5f5c8ee5 5795 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
4588ec20 5796 {
5f5c8ee5 5797 /* Frame has both root and mini-buffer. */
7df02016 5798 XSETFASTINT (XWINDOW (FRAME_ROOT_WINDOW (f))->top_line,
5f5c8ee5
GM
5799 FRAME_TOP_MARGIN (f));
5800 set_window_height (FRAME_ROOT_WINDOW (f),
5801 (newheight
5802 - 1
5803 - FRAME_TOP_MARGIN (f)),
f8748f0f 5804 2);
7df02016 5805 XSETFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top_line,
a5d8b611 5806 newheight - 1);
5f5c8ee5 5807 set_window_height (FRAME_MINIBUF_WINDOW (f), 1, 0);
4588ec20
JB
5808 }
5809 else
502b9b64 5810 /* Frame has just one top-level window. */
5f5c8ee5 5811 set_window_height (FRAME_ROOT_WINDOW (f),
f8748f0f 5812 newheight - FRAME_TOP_MARGIN (f), 2);
b6a65ac2 5813
48e482e6
EZ
5814 /* MSDOS frames cannot PRETEND, as they change frame size by
5815 manipulating video hardware. */
5816 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
6548cf00 5817 FrameRows (FRAME_TTY (f)) = newheight;
4588ec20
JB
5818 }
5819
7df02016 5820 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
4588ec20 5821 {
f8748f0f 5822 set_window_width (FRAME_ROOT_WINDOW (f), new_frame_total_cols, 2);
5f5c8ee5 5823 if (FRAME_HAS_MINIBUF_P (f))
7df02016 5824 set_window_width (FRAME_MINIBUF_WINDOW (f), new_frame_total_cols, 0);
4588ec20 5825
48e482e6
EZ
5826 /* MSDOS frames cannot PRETEND, as they change frame size by
5827 manipulating video hardware. */
5828 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
6548cf00 5829 FrameCols (FRAME_TTY (f)) = newwidth;
5f5c8ee5 5830
9ea173e8 5831 if (WINDOWP (f->tool_bar_window))
7df02016 5832 XSETFASTINT (XWINDOW (f->tool_bar_window)->total_cols, newwidth);
4588ec20
JB
5833 }
5834
7df02016
KS
5835 FRAME_LINES (f) = newheight;
5836 SET_FRAME_COLS (f, newwidth);
986e61b8 5837
5f5c8ee5
GM
5838 {
5839 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5840 int text_area_x, text_area_y, text_area_width, text_area_height;
177c0ea7 5841
5f5c8ee5
GM
5842 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5843 &text_area_height);
5844 if (w->cursor.x >= text_area_x + text_area_width)
5845 w->cursor.hpos = w->cursor.x = 0;
5846 if (w->cursor.y >= text_area_y + text_area_height)
5847 w->cursor.vpos = w->cursor.y = 0;
5848 }
986e61b8 5849
5f5c8ee5 5850 adjust_glyphs (f);
5f5c8ee5 5851 calculate_costs (f);
cb02fa39
GM
5852 SET_FRAME_GARBAGED (f);
5853 f->resized_p = 1;
97cf50e7
RS
5854
5855 UNBLOCK_INPUT;
61730a69 5856
e523f7e5
RS
5857 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5858
ef264c42 5859 run_window_configuration_change_hook (f);
e523f7e5
RS
5860
5861 unbind_to (count, Qnil);
4588ec20 5862}
5f5c8ee5
GM
5863
5864
4588ec20 5865\f
5f5c8ee5
GM
5866/***********************************************************************
5867 Terminal Related Lisp Functions
5868 ***********************************************************************/
5869
5870DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
228299fa 5871 1, 1, "FOpen termscript file: ",
7ee72033
MB
5872 doc: /* Start writing all terminal output to FILE as well as the terminal.
5873FILE = nil means just close any termscript file currently open. */)
5842a27b 5874 (Lisp_Object file)
5f5c8ee5 5875{
0b0d3e0b
KL
5876 struct tty_display_info *tty;
5877
7d2f1216
EZ
5878 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5879 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
0b0d3e0b
KL
5880 error ("Current frame is not on a tty device");
5881
5882 tty = CURTTY ();
5883
5884 if (tty->termscript != 0)
e18c7093
KL
5885 {
5886 BLOCK_INPUT;
0b0d3e0b 5887 fclose (tty->termscript);
e18c7093
KL
5888 UNBLOCK_INPUT;
5889 }
0b0d3e0b 5890 tty->termscript = 0;
5f5c8ee5
GM
5891
5892 if (! NILP (file))
5893 {
5894 file = Fexpand_file_name (file, Qnil);
0b0d3e0b
KL
5895 tty->termscript = fopen (SDATA (file), "w");
5896 if (tty->termscript == 0)
5f5c8ee5
GM
5897 report_file_error ("Opening termscript", Fcons (file, Qnil));
5898 }
5899 return Qnil;
5900}
5901
5902
4588ec20 5903DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
62af879c 5904 Ssend_string_to_terminal, 1, 2, 0,
7ee72033 5905 doc: /* Send STRING to the terminal without alteration.
62af879c
KL
5906Control characters in STRING will have terminal-dependent effects.
5907
6ed8eeff 5908Optional parameter TERMINAL specifies the tty terminal device to use.
558759e4
AM
5909It may be a terminal object, a frame, or nil for the terminal used by
5910the currently selected frame. In batch mode, STRING is sent to stdout
5911when TERMINAL is nil. */)
5842a27b 5912 (Lisp_Object string, Lisp_Object terminal)
4588ec20 5913{
7cef7ce3
SM
5914 struct terminal *t = get_terminal (terminal, 1);
5915 FILE *out;
0b0d3e0b 5916
94f3db62 5917 /* ??? Perhaps we should do something special for multibyte strings here. */
b7826503 5918 CHECK_STRING (string);
2c1e4409 5919 BLOCK_INPUT;
0b0d3e0b 5920
6ed8eeff
KL
5921 if (!t)
5922 error ("Unknown terminal device");
0b0d3e0b 5923
7cef7ce3
SM
5924 if (t->type == output_initial)
5925 out = stdout;
5926 else if (t->type != output_termcap && t->type != output_msdos_raw)
5927 error ("Device %d is not a termcap terminal device", t->id);
5928 else
5929 {
5930 struct tty_display_info *tty = t->display_info.tty;
3fc809e2 5931
7cef7ce3
SM
5932 if (! tty->output)
5933 error ("Terminal is currently suspended");
cecfdea6 5934
7cef7ce3
SM
5935 if (tty->termscript)
5936 {
5937 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5938 fflush (tty->termscript);
5939 }
5940 out = tty->output;
4588ec20 5941 }
7cef7ce3
SM
5942 fwrite (SDATA (string), 1, SBYTES (string), out);
5943 fflush (out);
2c1e4409 5944 UNBLOCK_INPUT;
4588ec20
JB
5945 return Qnil;
5946}
5947
5f5c8ee5 5948
4588ec20 5949DEFUN ("ding", Fding, Sding, 0, 1, 0,
7ee72033 5950 doc: /* Beep, or flash the screen.
228299fa 5951Also, unless an argument is given,
7ee72033 5952terminate any keyboard macro currently executing. */)
5842a27b 5953 (Lisp_Object arg)
4588ec20 5954{
efb859b4 5955 if (!NILP (arg))
4588ec20 5956 {
7fa788da
RS
5957 if (noninteractive)
5958 putchar (07);
5959 else
385ed61f 5960 ring_bell (XFRAME (selected_frame));
4588ec20
JB
5961 }
5962 else
5963 bitch_at_user ();
5964
5965 return Qnil;
5966}
5967
dfcf069d 5968void
971de7fb 5969bitch_at_user (void)
4588ec20
JB
5970{
5971 if (noninteractive)
5972 putchar (07);
5f5c8ee5 5973 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
4588ec20
JB
5974 error ("Keyboard macro terminated by a command ringing the bell");
5975 else
385ed61f 5976 ring_bell (XFRAME (selected_frame));
4588ec20
JB
5977}
5978
5f5c8ee5
GM
5979
5980\f
5981/***********************************************************************
5982 Sleeping, Waiting
5983 ***********************************************************************/
5984
4588ec20 5985DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
7ee72033 5986 doc: /* Pause, without updating display, for SECONDS seconds.
228299fa
GM
5987SECONDS may be a floating-point value, meaning that you can wait for a
5988fraction of a second. Optional second arg MILLISECONDS specifies an
5989additional wait period, in milliseconds; this may be useful if your
5990Emacs was built without floating point support.
7ee72033 5991\(Not all operating systems support waiting for a fraction of a second.) */)
5842a27b 5992 (Lisp_Object seconds, Lisp_Object milliseconds)
4588ec20 5993{
767229f8 5994 int sec, usec;
4588ec20 5995
767229f8 5996 if (NILP (milliseconds))
e9c9a718 5997 XSETINT (milliseconds, 0);
767229f8 5998 else
b7826503 5999 CHECK_NUMBER (milliseconds);
b07646f5
JB
6000 usec = XINT (milliseconds) * 1000;
6001
b07646f5
JB
6002 {
6003 double duration = extract_float (seconds);
6004 sec = (int) duration;
6005 usec += (duration - sec) * 1000000;
6006 }
4588ec20 6007
a41f8bed 6008#ifndef EMACS_HAS_USECS
767229f8 6009 if (sec == 0 && usec != 0)
861e19b9 6010 error ("Millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
4588ec20 6011#endif
767229f8
JB
6012
6013 /* Assure that 0 <= usec < 1000000. */
6014 if (usec < 0)
6015 {
153a073c 6016 /* We can't rely on the rounding being correct if usec is negative. */
767229f8
JB
6017 if (-1000000 < usec)
6018 sec--, usec += 1000000;
6019 else
6020 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
4588ec20 6021 }
767229f8
JB
6022 else
6023 sec += usec / 1000000, usec %= 1000000;
6024
6b5153b1 6025 if (sec < 0 || (sec == 0 && usec == 0))
767229f8 6026 return Qnil;
4588ec20 6027
dcd1ca45 6028 wait_reading_process_output (sec, usec, 0, 0, Qnil, NULL, 0);
4588ec20
JB
6029
6030 return Qnil;
6031}
6032
5f5c8ee5 6033
dcd1ca45 6034/* This is just like wait_reading_process_output, except that
902b9b6d 6035 it does redisplay.
f76475ad 6036
2bcac766
CY
6037 TIMEOUT is number of seconds to wait (float or integer),
6038 or t to wait forever.
902b9b6d
KS
6039 READING is 1 if reading input.
6040 If DO_DISPLAY is >0 display process output while waiting.
6041 If DO_DISPLAY is >1 perform an initial redisplay before waiting.
6042*/
4588ec20 6043
f76475ad 6044Lisp_Object
971de7fb 6045sit_for (Lisp_Object timeout, int reading, int do_display)
f76475ad 6046{
902b9b6d 6047 int sec, usec;
f222369e 6048
902b9b6d 6049 swallow_events (do_display);
ccddf474 6050
902b9b6d 6051 if ((detect_input_pending_run_timers (do_display))
29bcbe54 6052 || !NILP (Vexecuting_kbd_macro))
4588ec20 6053 return Qnil;
4588ec20 6054
902b9b6d
KS
6055 if (do_display >= 2)
6056 redisplay_preserve_echo_area (2);
6057
8e09f23f 6058 if (INTEGERP (timeout))
f222369e 6059 {
7ff14773 6060 sec = XINT (timeout);
8e09f23f 6061 usec = 0;
f222369e 6062 }
8e09f23f
KS
6063 else if (FLOATP (timeout))
6064 {
99a20368 6065 double seconds = XFLOAT_DATA (timeout);
902b9b6d
KS
6066 sec = (int) seconds;
6067 usec = (int) ((seconds - sec) * 1000000);
6068 }
2bcac766
CY
6069 else if (EQ (timeout, Qt))
6070 {
6071 sec = 0;
6072 usec = 0;
6073 }
902b9b6d 6074 else
8e09f23f 6075 wrong_type_argument (Qnumberp, timeout);
4588ec20 6076
2bcac766 6077 if (sec == 0 && usec == 0 && !EQ (timeout, Qt))
dfdb645c
JB
6078 return Qt;
6079
4588ec20 6080#ifdef SIGIO
8fc798e9 6081 gobble_input (0);
f76475ad
JB
6082#endif
6083
902b9b6d 6084 wait_reading_process_output (sec, usec, reading ? -1 : 1, do_display,
dcd1ca45 6085 Qnil, NULL, 0);
f76475ad 6086
4588ec20
JB
6087 return detect_input_pending () ? Qnil : Qt;
6088}
6089
5f5c8ee5 6090
01327edf
KS
6091DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
6092 doc: /* Perform redisplay if no input is available.
a5c2c707 6093If optional arg FORCE is non-nil or `redisplay-dont-pause' is non-nil,
03240971
KS
6094perform a full redisplay even if input is available.
6095Return t if redisplay was performed, nil otherwise. */)
5842a27b 6096 (Lisp_Object force)
f76475ad 6097{
01327edf 6098 int count;
659d9874 6099
902b9b6d
KS
6100 swallow_events (1);
6101 if ((detect_input_pending_run_timers (1)
01327edf 6102 && NILP (force) && !redisplay_dont_pause)
7ba07a1a
CY
6103 || !NILP (Vexecuting_kbd_macro))
6104 return Qnil;
f76475ad 6105
01327edf
KS
6106 count = SPECPDL_INDEX ();
6107 if (!NILP (force) && !redisplay_dont_pause)
6108 specbind (Qredisplay_dont_pause, Qt);
7ba07a1a 6109 redisplay_preserve_echo_area (2);
01327edf 6110 unbind_to (count, Qnil);
7ba07a1a 6111 return Qt;
f76475ad 6112}
5f5c8ee5
GM
6113
6114
6115\f
6116/***********************************************************************
6117 Other Lisp Functions
6118 ***********************************************************************/
6119
6120/* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
6121 session's frames, frame names, buffers, buffer-read-only flags, and
44edd521 6122 buffer-modified-flags. */
5f5c8ee5
GM
6123
6124static Lisp_Object frame_and_buffer_state;
6125
6126
6127DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
44edd521 6128 Sframe_or_buffer_changed_p, 0, 1, 0,
bacf09b4 6129 doc: /* Return non-nil if the frame and buffer state appears to have changed.
44edd521
RS
6130VARIABLE is a variable name whose value is either nil or a state vector
6131that will be updated to contain all frames and buffers,
228299fa 6132aside from buffers whose names start with space,
44edd521
RS
6133along with the buffers' read-only and modified flags. This allows a fast
6134check to see whether buffer menus might need to be recomputed.
228299fa 6135If this function returns non-nil, it updates the internal vector to reflect
44edd521
RS
6136the current state.
6137
6138If VARIABLE is nil, an internal variable is used. Users should not
6139pass nil for VARIABLE. */)
5842a27b 6140 (Lisp_Object variable)
5f5c8ee5 6141{
44edd521
RS
6142 Lisp_Object state, tail, frame, buf;
6143 Lisp_Object *vecp, *end;
5f5c8ee5
GM
6144 int n;
6145
44edd521
RS
6146 if (! NILP (variable))
6147 {
6148 CHECK_SYMBOL (variable);
6149 state = Fsymbol_value (variable);
6150 if (! VECTORP (state))
6151 goto changed;
6152 }
6153 else
6154 state = frame_and_buffer_state;
6155
6156 vecp = XVECTOR (state)->contents;
6157 end = vecp + XVECTOR (state)->size;
6158
5f5c8ee5
GM
6159 FOR_EACH_FRAME (tail, frame)
6160 {
44edd521
RS
6161 if (vecp == end)
6162 goto changed;
5f5c8ee5
GM
6163 if (!EQ (*vecp++, frame))
6164 goto changed;
44edd521
RS
6165 if (vecp == end)
6166 goto changed;
5f5c8ee5
GM
6167 if (!EQ (*vecp++, XFRAME (frame)->name))
6168 goto changed;
6169 }
44edd521 6170 /* Check that the buffer info matches. */
7539e11f 6171 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6172 {
7539e11f 6173 buf = XCDR (XCAR (tail));
5f5c8ee5 6174 /* Ignore buffers that aren't included in buffer lists. */
d5db4077 6175 if (SREF (XBUFFER (buf)->name, 0) == ' ')
5f5c8ee5 6176 continue;
44edd521
RS
6177 if (vecp == end)
6178 goto changed;
5f5c8ee5
GM
6179 if (!EQ (*vecp++, buf))
6180 goto changed;
44edd521
RS
6181 if (vecp == end)
6182 goto changed;
5f5c8ee5
GM
6183 if (!EQ (*vecp++, XBUFFER (buf)->read_only))
6184 goto changed;
44edd521
RS
6185 if (vecp == end)
6186 goto changed;
5f5c8ee5
GM
6187 if (!EQ (*vecp++, Fbuffer_modified_p (buf)))
6188 goto changed;
6189 }
44edd521
RS
6190 if (vecp == end)
6191 goto changed;
5f5c8ee5
GM
6192 /* Detect deletion of a buffer at the end of the list. */
6193 if (EQ (*vecp, Qlambda))
6194 return Qnil;
44edd521
RS
6195
6196 /* Come here if we decide the data has changed. */
5f5c8ee5 6197 changed:
44edd521
RS
6198 /* Count the size we will need.
6199 Start with 1 so there is room for at least one lambda at the end. */
5f5c8ee5
GM
6200 n = 1;
6201 FOR_EACH_FRAME (tail, frame)
6202 n += 2;
7539e11f 6203 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6204 n += 3;
44edd521
RS
6205 /* Reallocate the vector if data has grown to need it,
6206 or if it has shrunk a lot. */
6207 if (! VECTORP (state)
6208 || n > XVECTOR (state)->size
6209 || n + 20 < XVECTOR (state)->size / 2)
5f5c8ee5 6210 /* Add 20 extra so we grow it less often. */
44edd521
RS
6211 {
6212 state = Fmake_vector (make_number (n + 20), Qlambda);
6213 if (! NILP (variable))
6214 Fset (variable, state);
6215 else
6216 frame_and_buffer_state = state;
6217 }
6218
6219 /* Record the new data in the (possibly reallocated) vector. */
6220 vecp = XVECTOR (state)->contents;
5f5c8ee5
GM
6221 FOR_EACH_FRAME (tail, frame)
6222 {
6223 *vecp++ = frame;
6224 *vecp++ = XFRAME (frame)->name;
6225 }
7539e11f 6226 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
5f5c8ee5 6227 {
7539e11f 6228 buf = XCDR (XCAR (tail));
5f5c8ee5 6229 /* Ignore buffers that aren't included in buffer lists. */
d5db4077 6230 if (SREF (XBUFFER (buf)->name, 0) == ' ')
5f5c8ee5
GM
6231 continue;
6232 *vecp++ = buf;
6233 *vecp++ = XBUFFER (buf)->read_only;
6234 *vecp++ = Fbuffer_modified_p (buf);
6235 }
6236 /* Fill up the vector with lambdas (always at least one). */
6237 *vecp++ = Qlambda;
44edd521
RS
6238 while (vecp - XVECTOR (state)->contents
6239 < XVECTOR (state)->size)
5f5c8ee5
GM
6240 *vecp++ = Qlambda;
6241 /* Make sure we didn't overflow the vector. */
44edd521
RS
6242 if (vecp - XVECTOR (state)->contents
6243 > XVECTOR (state)->size)
5f5c8ee5
GM
6244 abort ();
6245 return Qt;
6246}
6247
6248
4588ec20 6249\f
5f5c8ee5
GM
6250/***********************************************************************
6251 Initialization
6252***********************************************************************/
6253
5f5c8ee5
GM
6254/* Initialization done when Emacs fork is started, before doing stty.
6255 Determine terminal type and set terminal_driver. Then invoke its
6256 decoding routine to set up variables in the terminal package. */
4588ec20 6257
dfcf069d 6258void
971de7fb 6259init_display (void)
4588ec20 6260{
28d440ab
KL
6261 char *terminal_type;
6262
5f5c8ee5
GM
6263 /* Construct the space glyph. */
6264 space_glyph.type = CHAR_GLYPH;
3fc809e2 6265 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
5f5c8ee5
GM
6266 space_glyph.charpos = -1;
6267
4588ec20
JB
6268 inverse_video = 0;
6269 cursor_in_echo_area = 0;
6270 terminal_type = (char *) 0;
6271
1315c181
JB
6272 /* Now is the time to initialize this; it's used by init_sys_modes
6273 during startup. */
2246281f 6274 Vinitial_window_system = Qnil;
4588ec20 6275
4181ad9c
KL
6276 /* SIGWINCH needs to be handled no matter what display we start
6277 with. Otherwise newly opened tty frames will not resize
6278 automatically. */
6279#ifdef SIGWINCH
6280#ifndef CANNOT_DUMP
6281 if (initialized)
6282#endif /* CANNOT_DUMP */
6283 signal (SIGWINCH, window_change_signal);
6284#endif /* SIGWINCH */
4588ec20 6285
d6ef42b9 6286 /* If running as a daemon, no need to initialize any frames/terminal. */
ff808935
DN
6287 if (IS_DAEMON)
6288 return;
d6ef42b9 6289
1315c181
JB
6290 /* If the user wants to use a window system, we shouldn't bother
6291 initializing the terminal. This is especially important when the
6292 terminal is so dumb that emacs gives up before and doesn't bother
6293 using the window system.
4588ec20 6294
36bbad1d
KH
6295 If the DISPLAY environment variable is set and nonempty,
6296 try to use X, and die with an error message if that doesn't work. */
4588ec20
JB
6297
6298#ifdef HAVE_X_WINDOWS
00d70d57 6299 if (! inhibit_window_system && ! display_arg)
d460af17 6300 {
36bbad1d 6301 char *display;
36bbad1d 6302 display = getenv ("DISPLAY");
36bbad1d 6303 display_arg = (display != 0 && *display != 0);
c1fc674b
AS
6304
6305 if (display_arg && !x_display_ok (display))
6306 {
6307 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6308 display);
6309 inhibit_window_system = 1;
6310 }
f040093a 6311 }
d460af17 6312
177c0ea7 6313 if (!inhibit_window_system && display_arg
9e4555e8
RS
6314#ifndef CANNOT_DUMP
6315 && initialized
6316#endif
6317 )
4588ec20 6318 {
111d9af3 6319 Vinitial_window_system = Qx;
4588ec20
JB
6320#ifdef HAVE_X11
6321 Vwindow_system_version = make_number (11);
039e5d71 6322#endif
8a2a6032 6323#if defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
039e5d71 6324 /* In some versions of ncurses,
6a428f77 6325 tputs crashes if we have not called tgetent.
039e5d71
KH
6326 So call tgetent. */
6327 { char b[2044]; tgetent (b, "xterm");}
4588ec20 6328#endif
5f5c8ee5 6329 adjust_frame_glyphs_initially ();
4588ec20
JB
6330 return;
6331 }
6332#endif /* HAVE_X_WINDOWS */
6333
fd2e066a 6334#ifdef HAVE_NTGUI
177c0ea7 6335 if (!inhibit_window_system)
fd2e066a 6336 {
111d9af3 6337 Vinitial_window_system = Qw32;
fd2e066a 6338 Vwindow_system_version = make_number (1);
5f5c8ee5 6339 adjust_frame_glyphs_initially ();
fd2e066a
GV
6340 return;
6341 }
6342#endif /* HAVE_NTGUI */
6343
edfda783
AR
6344#ifdef HAVE_NS
6345 if (!inhibit_window_system
6346#ifndef CANNOT_DUMP
6347 && initialized
6348#endif
6349 )
6350 {
111d9af3 6351 Vinitial_window_system = Qns;
edfda783
AR
6352 Vwindow_system_version = make_number(10);
6353 adjust_frame_glyphs_initially ();
6354 return;
6355 }
6356#endif
6357
4588ec20
JB
6358 /* If no window system has been specified, try to use the terminal. */
6359 if (! isatty (0))
6360 {
1559a86d 6361 fatal ("standard input is not a tty");
4588ec20
JB
6362 exit (1);
6363 }
6364
6152361f
JR
6365#ifdef WINDOWSNT
6366 terminal_type = "w32console";
6367#else
153a073c 6368 /* Look at the TERM variable. */
4588ec20 6369 terminal_type = (char *) getenv ("TERM");
6152361f 6370#endif
4588ec20
JB
6371 if (!terminal_type)
6372 {
5d12ff0b 6373#ifdef HAVE_WINDOW_SYSTEM
b9e590fc
RS
6374 if (! inhibit_window_system)
6375 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
5d12ff0b
RS
6376 else
6377#endif /* HAVE_WINDOW_SYSTEM */
b9e590fc 6378 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
4588ec20
JB
6379 exit (1);
6380 }
6381
0a125897 6382 {
6ed8eeff 6383 struct terminal *t;
3224dac1
KL
6384 struct frame *f = XFRAME (selected_frame);
6385
6386 /* Open a display on the controlling tty. */
6ed8eeff 6387 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
3224dac1
KL
6388
6389 /* Convert the initial frame to use the new display. */
b04f5ad2 6390 if (f->output_method != output_initial)
3224dac1 6391 abort ();
6ed8eeff
KL
6392 f->output_method = t->type;
6393 f->terminal = t;
3224dac1 6394
6ed8eeff
KL
6395 t->reference_count++;
6396 t->display_info.tty->top_frame = selected_frame;
5b65d888 6397 change_frame_size (XFRAME (selected_frame),
6ed8eeff
KL
6398 FrameRows (t->display_info.tty),
6399 FrameCols (t->display_info.tty), 0, 0, 1);
3224dac1 6400
6ed8eeff
KL
6401 /* Delete the initial terminal. */
6402 if (--initial_terminal->reference_count == 0
6403 && initial_terminal->delete_terminal_hook)
6404 (*initial_terminal->delete_terminal_hook) (initial_terminal);
2666355c
KL
6405
6406 /* Update frame parameters to reflect the new type. */
6407 Fmodify_frame_parameters
6408 (selected_frame, Fcons (Fcons (Qtty_type,
6ed8eeff 6409 Ftty_type (selected_frame)), Qnil));
657ceaa0
EZ
6410 if (t->display_info.tty->name)
6411 Fmodify_frame_parameters (selected_frame,
6412 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6413 Qnil));
6414 else
6415 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6416 Qnil));
0a125897 6417 }
3fc809e2 6418
d86c299a 6419 {
91fb7e1b 6420 struct frame *sf = SELECTED_FRAME ();
7df02016
KS
6421 int width = FRAME_TOTAL_COLS (sf);
6422 int height = FRAME_LINES (sf);
d86c299a 6423
5f5c8ee5 6424 unsigned int total_glyphs = height * (width + 2) * sizeof (struct glyph);
d86c299a 6425
5f5c8ee5
GM
6426 /* If these sizes are so big they cause overflow, just ignore the
6427 change. It's not clear what better we could do. */
6428 if (total_glyphs / sizeof (struct glyph) / height != width + 2)
1559a86d 6429 fatal ("screen size %dx%d too big", width, height);
d86c299a
RS
6430 }
6431
5f5c8ee5 6432 adjust_frame_glyphs_initially ();
91fb7e1b 6433 calculate_costs (XFRAME (selected_frame));
4588ec20 6434
5f5c8ee5
GM
6435 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6436 if (initialized
6437 && !noninteractive
2246281f 6438 && NILP (Vinitial_window_system))
2d764c78
EZ
6439 {
6440 /* For the initial frame, we don't have any way of knowing what
6441 are the foreground and background colors of the terminal. */
6442 struct frame *sf = SELECTED_FRAME();
6443
f9d2fdc4
EZ
6444 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6445 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
2d764c78
EZ
6446 call0 (intern ("tty-set-up-initial-frame-faces"));
6447 }
5f5c8ee5
GM
6448}
6449
6450
6451\f
6452/***********************************************************************
6453 Blinking cursor
6454 ***********************************************************************/
6455
c3f13540 6456DEFUN ("internal-show-cursor", Finternal_show_cursor,
5d7791b3 6457 Sinternal_show_cursor, 2, 2, 0,
7ee72033 6458 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
228299fa
GM
6459WINDOW nil means use the selected window. SHOW non-nil means
6460show a cursor in WINDOW in the next redisplay. SHOW nil means
7ee72033 6461don't show a cursor. */)
5842a27b 6462 (Lisp_Object window, Lisp_Object show)
5f5c8ee5 6463{
5f5c8ee5
GM
6464 /* Don't change cursor state while redisplaying. This could confuse
6465 output routines. */
6466 if (!redisplaying_p)
6467 {
6468 if (NILP (window))
6469 window = selected_window;
6470 else
b7826503 6471 CHECK_WINDOW (window);
177c0ea7 6472
5d7791b3 6473 XWINDOW (window)->cursor_off_p = NILP (show);
5f5c8ee5
GM
6474 }
6475
6476 return Qnil;
4588ec20 6477}
5f5c8ee5
GM
6478
6479
5d7791b3
GM
6480DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6481 Sinternal_show_cursor_p, 0, 1, 0,
bacf09b4 6482 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
7ee72033 6483WINDOW nil or omitted means report on the selected window. */)
5842a27b 6484 (Lisp_Object window)
5d7791b3
GM
6485{
6486 struct window *w;
177c0ea7 6487
5d7791b3
GM
6488 if (NILP (window))
6489 window = selected_window;
6490 else
b7826503 6491 CHECK_WINDOW (window);
177c0ea7 6492
5d7791b3 6493 w = XWINDOW (window);
177c0ea7 6494 return w->cursor_off_p ? Qnil : Qt;
5d7791b3
GM
6495}
6496
87478b52
SM
6497DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6498 Slast_nonminibuf_frame, 0, 0, 0,
6499 doc: /* Value is last nonminibuffer frame. */)
5842a27b 6500 (void)
87478b52 6501{
4958ed94 6502 Lisp_Object frame = Qnil;
87478b52 6503
4958ed94
SM
6504 if (last_nonminibuf_frame)
6505 XSETFRAME (frame, last_nonminibuf_frame);
87478b52
SM
6506
6507 return frame;
6508}
4588ec20 6509\f
5f5c8ee5
GM
6510/***********************************************************************
6511 Initialization
6512 ***********************************************************************/
6513
dfcf069d 6514void
971de7fb 6515syms_of_display (void)
4588ec20 6516{
502b9b64 6517 defsubr (&Sredraw_frame);
4588ec20 6518 defsubr (&Sredraw_display);
078b3696 6519 defsubr (&Sframe_or_buffer_changed_p);
4588ec20
JB
6520 defsubr (&Sopen_termscript);
6521 defsubr (&Sding);
7ba07a1a 6522 defsubr (&Sredisplay);
4588ec20
JB
6523 defsubr (&Ssleep_for);
6524 defsubr (&Ssend_string_to_terminal);
c3f13540 6525 defsubr (&Sinternal_show_cursor);
5d7791b3 6526 defsubr (&Sinternal_show_cursor_p);
87478b52 6527 defsubr (&Slast_nonminibuf_frame);
4588ec20 6528
2d5912c1 6529#if GLYPH_DEBUG
9b0e97aa
GM
6530 defsubr (&Sdump_redisplay_history);
6531#endif
6532
d1dad759 6533 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
078b3696
KH
6534 staticpro (&frame_and_buffer_state);
6535
d67b4f80 6536 Qdisplay_table = intern_c_string ("display-table");
9cda4f7c 6537 staticpro (&Qdisplay_table);
d67b4f80 6538 Qredisplay_dont_pause = intern_c_string ("redisplay-dont-pause");
e7067d00 6539 staticpro (&Qredisplay_dont_pause);
9cda4f7c 6540
7ee72033 6541 DEFVAR_INT ("baud-rate", &baud_rate,
bacf09b4 6542 doc: /* *The output baud rate of the terminal.
228299fa
GM
6543On most systems, changing this value will affect the amount of padding
6544and the other strategic decisions made during redisplay. */);
177c0ea7 6545
7ee72033 6546 DEFVAR_BOOL ("inverse-video", &inverse_video,
bacf09b4 6547 doc: /* *Non-nil means invert the entire frame display.
228299fa 6548This means everything is in inverse video which otherwise would not be. */);
177c0ea7 6549
7ee72033 6550 DEFVAR_BOOL ("visible-bell", &visible_bell,
bacf09b4 6551 doc: /* *Non-nil means try to flash the frame to represent a bell.
552b1b01
EZ
6552
6553See also `ring-bell-function'. */);
177c0ea7 6554
7ee72033 6555 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
bacf09b4 6556 doc: /* *Non-nil means no need to redraw entire frame after suspending.
228299fa
GM
6557A non-nil value is useful if the terminal can automatically preserve
6558Emacs's frame display when you reenter Emacs.
6559It is up to you to set this variable if your terminal can do that. */);
177c0ea7 6560
2246281f
KL
6561 DEFVAR_LISP ("initial-window-system", &Vinitial_window_system,
6562 doc: /* Name of the window system that Emacs uses for the first frame.
85565e9e
RS
6563The value is a symbol--for instance, `x' for X windows.
6564The value is nil if Emacs is using a text-only terminal. */);
177c0ea7 6565
70b8d0a4
SM
6566 DEFVAR_KBOARD ("window-system", Vwindow_system,
6567 doc: /* Name of window system through which the selected frame is displayed.
6568The value is a symbol--for instance, `x' for X windows.
6569The value is nil if the selected frame is on a text-only-terminal. */);
6570
7ee72033 6571 DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
bacf09b4 6572 doc: /* The version number of the window system in use.
c22140f6 6573For X windows, this is 11. */);
177c0ea7 6574
7ee72033 6575 DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
bacf09b4 6576 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
177c0ea7 6577
7ee72033 6578 DEFVAR_LISP ("glyph-table", &Vglyph_table,
bacf09b4 6579 doc: /* Table defining how to output a glyph code to the frame.
228299fa
GM
6580If not nil, this is a vector indexed by glyph code to define the glyph.
6581Each element can be:
6582 integer: a glyph code which this glyph is an alias for.
6583 string: output this glyph using that string (not impl. in X windows).
6584 nil: this glyph mod 524288 is the code of a character to output,
6585 and this glyph / 524288 is the face number (see `face-id') to use
6586 while outputting it. */);
4588ec20
JB
6587 Vglyph_table = Qnil;
6588
7ee72033 6589 DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
bacf09b4 6590 doc: /* Display table to use for buffers that specify none.
228299fa 6591See `buffer-display-table' for more information. */);
4588ec20
JB
6592 Vstandard_display_table = Qnil;
6593
7ee72033 6594 DEFVAR_BOOL ("redisplay-dont-pause", &redisplay_dont_pause,
bacf09b4 6595 doc: /* *Non-nil means update isn't paused when input is detected. */);
5f5c8ee5
GM
6596 redisplay_dont_pause = 0;
6597
d012c62b
KS
6598#if PERIODIC_PREEMPTION_CHECKING
6599 DEFVAR_LISP ("redisplay-preemption-period", &Vredisplay_preemption_period,
6600 doc: /* *The period in seconds between checking for input during redisplay.
6601If input is detected, redisplay is pre-empted, and the input is processed.
6602If nil, never pre-empt redisplay. */);
6603 Vredisplay_preemption_period = make_float (0.10);
6604#endif
6605
4588ec20
JB
6606#ifdef CANNOT_DUMP
6607 if (noninteractive)
6608#endif
6609 {
2246281f 6610 Vinitial_window_system = Qnil;
4588ec20
JB
6611 Vwindow_system_version = Qnil;
6612 }
6613}
6b61353c
KH
6614
6615/* arch-tag: 8d812b1f-04a2-4195-a9c4-381f8457a413
6616 (do not change this comment) */