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