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