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