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