Fix bug #10098 with assertion violation during frame resize.
[emacs.git] / src / dispnew.c
blob8abf5782a88c255df127ad4b64a499bf1ac1ba1e
1 /* Updating of data structures for redisplay.
2 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 #include <config.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <setjmp.h>
24 #include <unistd.h>
26 #include "lisp.h"
27 #include "termchar.h"
28 #include "termopts.h"
29 /* cm.h must come after dispextern.h on Windows. */
30 #include "dispextern.h"
31 #include "cm.h"
32 #include "buffer.h"
33 #include "character.h"
34 #include "keyboard.h"
35 #include "frame.h"
36 #include "termhooks.h"
37 #include "window.h"
38 #include "commands.h"
39 #include "disptab.h"
40 #include "indent.h"
41 #include "intervals.h"
42 #include "blockinput.h"
43 #include "process.h"
45 #include "syssignal.h"
47 #ifdef HAVE_X_WINDOWS
48 #include "xterm.h"
49 #endif /* HAVE_X_WINDOWS */
51 #ifdef HAVE_NTGUI
52 #include "w32term.h"
53 #endif /* HAVE_NTGUI */
55 #ifdef HAVE_NS
56 #include "nsterm.h"
57 #endif
59 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
61 #include "systime.h"
62 #include <errno.h>
64 /* Get number of chars of output now in the buffer of a stdio stream.
65 This ought to be built in in stdio, but it isn't. Some s- files
66 override this because their stdio internals differ. */
68 #ifdef __GNU_LIBRARY__
70 /* The s- file might have overridden the definition with one that
71 works for the system's C library. But we are using the GNU C
72 library, so this is the right definition for every system. */
74 #ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
75 #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
76 #else
77 #undef PENDING_OUTPUT_COUNT
78 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
79 #endif
80 #else /* not __GNU_LIBRARY__ */
81 #if !defined (PENDING_OUTPUT_COUNT) && HAVE_STDIO_EXT_H && HAVE___FPENDING
82 #include <stdio_ext.h>
83 #define PENDING_OUTPUT_COUNT(FILE) __fpending (FILE)
84 #endif
85 #ifndef PENDING_OUTPUT_COUNT
86 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
87 #endif
88 #endif /* not __GNU_LIBRARY__ */
90 #if defined (HAVE_TERM_H) && defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
91 #include <term.h> /* for tgetent */
92 #endif
94 /* Structure to pass dimensions around. Used for character bounding
95 boxes, glyph matrix dimensions and alike. */
97 struct dim
99 int width;
100 int height;
104 /* Function prototypes. */
106 static void update_frame_line (struct frame *, int);
107 static int required_matrix_height (struct window *);
108 static int required_matrix_width (struct window *);
109 static void adjust_frame_glyphs (struct frame *);
110 static void change_frame_size_1 (struct frame *, int, int, int, int, int);
111 static void increment_row_positions (struct glyph_row *, EMACS_INT, EMACS_INT);
112 static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
113 static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
114 struct window *);
115 static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
116 struct window *);
117 static void adjust_frame_message_buffer (struct frame *);
118 static void adjust_decode_mode_spec_buffer (struct frame *);
119 static void fill_up_glyph_row_with_spaces (struct glyph_row *);
120 static void clear_window_matrices (struct window *, int);
121 static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
122 static int scrolling_window (struct window *, int);
123 static int update_window_line (struct window *, int, int *);
124 static void mirror_make_current (struct window *, int);
125 #if GLYPH_DEBUG
126 static void check_matrix_pointers (struct glyph_matrix *,
127 struct glyph_matrix *);
128 #endif
129 static void mirror_line_dance (struct window *, int, int, int *, char *);
130 static int update_window_tree (struct window *, int);
131 static int update_window (struct window *, int);
132 static int update_frame_1 (struct frame *, int, int);
133 static int scrolling (struct frame *);
134 static void set_window_cursor_after_update (struct window *);
135 static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
136 static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
139 /* Define PERIODIC_PREEMPTION_CHECKING to 1, if micro-second timers
140 are supported, so we can check for input during redisplay at
141 regular intervals. */
142 #ifdef EMACS_HAS_USECS
143 #define PERIODIC_PREEMPTION_CHECKING 1
144 #else
145 #define PERIODIC_PREEMPTION_CHECKING 0
146 #endif
148 #if PERIODIC_PREEMPTION_CHECKING
150 /* Redisplay preemption timers. */
152 static EMACS_TIME preemption_period;
153 static EMACS_TIME preemption_next_check;
155 #endif
157 /* Nonzero upon entry to redisplay means do not assume anything about
158 current contents of actual terminal frame; clear and redraw it. */
160 int frame_garbaged;
162 /* Nonzero means last display completed. Zero means it was preempted. */
164 int display_completed;
166 Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
169 /* The currently selected frame. In a single-frame version, this
170 variable always equals the_only_frame. */
172 Lisp_Object selected_frame;
174 /* A frame which is not just a mini-buffer, or 0 if there are no such
175 frames. This is usually the most recent such frame that was
176 selected. In a single-frame version, this variable always holds
177 the address of the_only_frame. */
179 struct frame *last_nonminibuf_frame;
181 /* 1 means SIGWINCH happened when not safe. */
183 static int delayed_size_change;
185 /* 1 means glyph initialization has been completed at startup. */
187 static int glyphs_initialized_initially_p;
189 /* Updated window if != 0. Set by update_window. */
191 struct window *updated_window;
193 /* Glyph row updated in update_window_line, and area that is updated. */
195 struct glyph_row *updated_row;
196 int updated_area;
198 /* A glyph for a space. */
200 struct glyph space_glyph;
202 /* Counts of allocated structures. These counts serve to diagnose
203 memory leaks and double frees. */
205 static int glyph_matrix_count;
206 static int glyph_pool_count;
208 /* If non-null, the frame whose frame matrices are manipulated. If
209 null, window matrices are worked on. */
211 static struct frame *frame_matrix_frame;
213 /* Non-zero means that fonts have been loaded since the last glyph
214 matrix adjustments. Redisplay must stop, and glyph matrices must
215 be adjusted when this flag becomes non-zero during display. The
216 reason fonts can be loaded so late is that fonts of fontsets are
217 loaded on demand. Another reason is that a line contains many
218 characters displayed by zero width or very narrow glyphs of
219 variable-width fonts. */
221 int fonts_changed_p;
223 /* Convert vpos and hpos from frame to window and vice versa.
224 This may only be used for terminal frames. */
226 #if GLYPH_DEBUG
228 static int window_to_frame_vpos (struct window *, int);
229 static int window_to_frame_hpos (struct window *, int);
230 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
231 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
233 /* One element of the ring buffer containing redisplay history
234 information. */
236 struct redisplay_history
238 char trace[512 + 100];
241 /* The size of the history buffer. */
243 #define REDISPLAY_HISTORY_SIZE 30
245 /* The redisplay history buffer. */
247 static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
249 /* Next free entry in redisplay_history. */
251 static int history_idx;
253 /* A tick that's incremented each time something is added to the
254 history. */
256 static uprintmax_t history_tick;
258 static void add_frame_display_history (struct frame *, int);
260 /* Add to the redisplay history how window W has been displayed.
261 MSG is a trace containing the information how W's glyph matrix
262 has been constructed. PAUSED_P non-zero means that the update
263 has been interrupted for pending input. */
265 static void
266 add_window_display_history (struct window *w, const char *msg, int paused_p)
268 char *buf;
270 if (history_idx >= REDISPLAY_HISTORY_SIZE)
271 history_idx = 0;
272 buf = redisplay_history[history_idx].trace;
273 ++history_idx;
275 snprintf (buf, sizeof redisplay_history[0].trace,
276 "%"pMu": window %p (`%s')%s\n%s",
277 history_tick++,
279 ((BUFFERP (w->buffer)
280 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
281 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
282 : "???"),
283 paused_p ? " ***paused***" : "",
284 msg);
288 /* Add to the redisplay history that frame F has been displayed.
289 PAUSED_P non-zero means that the update has been interrupted for
290 pending input. */
292 static void
293 add_frame_display_history (struct frame *f, int paused_p)
295 char *buf;
297 if (history_idx >= REDISPLAY_HISTORY_SIZE)
298 history_idx = 0;
299 buf = redisplay_history[history_idx].trace;
300 ++history_idx;
302 sprintf (buf, "%"pMu": update frame %p%s",
303 history_tick++,
304 f, paused_p ? " ***paused***" : "");
308 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
309 Sdump_redisplay_history, 0, 0, "",
310 doc: /* Dump redisplay history to stderr. */)
311 (void)
313 int i;
315 for (i = history_idx - 1; i != history_idx; --i)
317 if (i < 0)
318 i = REDISPLAY_HISTORY_SIZE - 1;
319 fprintf (stderr, "%s\n", redisplay_history[i].trace);
322 return Qnil;
326 #else /* GLYPH_DEBUG == 0 */
328 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
329 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
331 #endif /* GLYPH_DEBUG == 0 */
334 #if defined PROFILING && !HAVE___EXECUTABLE_START
335 /* FIXME: only used to find text start for profiling. */
337 void
338 safe_bcopy (const char *from, char *to, int size)
340 abort ();
342 #endif
344 /***********************************************************************
345 Glyph Matrices
346 ***********************************************************************/
348 /* Allocate and return a glyph_matrix structure. POOL is the glyph
349 pool from which memory for the matrix should be allocated, or null
350 for window-based redisplay where no glyph pools are used. The
351 member `pool' of the glyph matrix structure returned is set to
352 POOL, the structure is otherwise zeroed. */
354 static struct glyph_matrix *
355 new_glyph_matrix (struct glyph_pool *pool)
357 struct glyph_matrix *result;
359 /* Allocate and clear. */
360 result = (struct glyph_matrix *) xmalloc (sizeof *result);
361 memset (result, 0, sizeof *result);
363 /* Increment number of allocated matrices. This count is used
364 to detect memory leaks. */
365 ++glyph_matrix_count;
367 /* Set pool and return. */
368 result->pool = pool;
369 return result;
373 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
375 The global counter glyph_matrix_count is decremented when a matrix
376 is freed. If the count gets negative, more structures were freed
377 than allocated, i.e. one matrix was freed more than once or a bogus
378 pointer was passed to this function.
380 If MATRIX->pool is null, this means that the matrix manages its own
381 glyph memory---this is done for matrices on X frames. Freeing the
382 matrix also frees the glyph memory in this case. */
384 static void
385 free_glyph_matrix (struct glyph_matrix *matrix)
387 if (matrix)
389 int i;
391 /* Detect the case that more matrices are freed than were
392 allocated. */
393 if (--glyph_matrix_count < 0)
394 abort ();
396 /* Free glyph memory if MATRIX owns it. */
397 if (matrix->pool == NULL)
398 for (i = 0; i < matrix->rows_allocated; ++i)
399 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
401 /* Free row structures and the matrix itself. */
402 xfree (matrix->rows);
403 xfree (matrix);
408 /* Return the number of glyphs to reserve for a marginal area of
409 window W. TOTAL_GLYPHS is the number of glyphs in a complete
410 display line of window W. MARGIN gives the width of the marginal
411 area in canonical character units. MARGIN should be an integer
412 or a float. */
414 static int
415 margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
417 int n;
419 if (NUMBERP (margin))
421 int width = XFASTINT (w->total_cols);
422 double d = max (0, XFLOATINT (margin));
423 d = min (width / 2 - 1, d);
424 n = (int) ((double) total_glyphs / width * d);
426 else
427 n = 0;
429 return n;
432 #if XASSERTS
433 /* Return non-zero if ROW's hash value is correct, zero if not. */
435 verify_row_hash (struct glyph_row *row)
437 return row->hash == row_hash (row);
439 #endif
441 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
442 window sizes.
444 W is null if the function is called for a frame glyph matrix.
445 Otherwise it is the window MATRIX is a member of. X and Y are the
446 indices of the first column and row of MATRIX within the frame
447 matrix, if such a matrix exists. They are zero for purely
448 window-based redisplay. DIM is the needed size of the matrix.
450 In window-based redisplay, where no frame matrices exist, glyph
451 matrices manage their own glyph storage. Otherwise, they allocate
452 storage from a common frame glyph pool which can be found in
453 MATRIX->pool.
455 The reason for this memory management strategy is to avoid complete
456 frame redraws if possible. When we allocate from a common pool, a
457 change of the location or size of a sub-matrix within the pool
458 requires a complete redisplay of the frame because we cannot easily
459 make sure that the current matrices of all windows still agree with
460 what is displayed on the screen. While this is usually fast, it
461 leads to screen flickering. */
463 static void
464 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
466 int i;
467 int new_rows;
468 int marginal_areas_changed_p = 0;
469 int header_line_changed_p = 0;
470 int header_line_p = 0;
471 int left = -1, right = -1;
472 int window_width = -1, window_height = -1;
474 /* See if W had a header line that has disappeared now, or vice versa.
475 Get W's size. */
476 if (w)
478 window_box (w, -1, 0, 0, &window_width, &window_height);
480 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
481 header_line_changed_p = header_line_p != matrix->header_line_p;
483 matrix->header_line_p = header_line_p;
485 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
486 Do nothing if MATRIX' size, position, vscroll, and marginal areas
487 haven't changed. This optimization is important because preserving
488 the matrix means preventing redisplay. */
489 if (matrix->pool == NULL)
491 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
492 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
493 xassert (left >= 0 && right >= 0);
494 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
495 || right != matrix->right_margin_glyphs);
497 if (!marginal_areas_changed_p
498 && !fonts_changed_p
499 && !header_line_changed_p
500 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
501 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
502 && matrix->window_height == window_height
503 && matrix->window_vscroll == w->vscroll
504 && matrix->window_width == window_width)
505 return;
508 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
509 if (matrix->rows_allocated < dim.height)
511 int old_alloc = matrix->rows_allocated;
512 new_rows = dim.height - matrix->rows_allocated;
513 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
514 new_rows, INT_MAX, sizeof *matrix->rows);
515 memset (matrix->rows + old_alloc, 0,
516 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
518 else
519 new_rows = 0;
521 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
522 on a frame not using window-based redisplay. Set up pointers for
523 each row into the glyph pool. */
524 if (matrix->pool)
526 xassert (matrix->pool->glyphs);
528 if (w)
530 left = margin_glyphs_to_reserve (w, dim.width,
531 w->left_margin_cols);
532 right = margin_glyphs_to_reserve (w, dim.width,
533 w->right_margin_cols);
535 else
536 left = right = 0;
538 for (i = 0; i < dim.height; ++i)
540 struct glyph_row *row = &matrix->rows[i];
542 row->glyphs[LEFT_MARGIN_AREA]
543 = (matrix->pool->glyphs
544 + (y + i) * matrix->pool->ncolumns
545 + x);
547 if (w == NULL
548 || row == matrix->rows + dim.height - 1
549 || (row == matrix->rows && matrix->header_line_p))
551 row->glyphs[TEXT_AREA]
552 = row->glyphs[LEFT_MARGIN_AREA];
553 row->glyphs[RIGHT_MARGIN_AREA]
554 = row->glyphs[TEXT_AREA] + dim.width;
555 row->glyphs[LAST_AREA]
556 = row->glyphs[RIGHT_MARGIN_AREA];
558 else
560 row->glyphs[TEXT_AREA]
561 = row->glyphs[LEFT_MARGIN_AREA] + left;
562 row->glyphs[RIGHT_MARGIN_AREA]
563 = row->glyphs[TEXT_AREA] + dim.width - left - right;
564 row->glyphs[LAST_AREA]
565 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
569 matrix->left_margin_glyphs = left;
570 matrix->right_margin_glyphs = right;
572 else
574 /* If MATRIX->pool is null, MATRIX is responsible for managing
575 its own memory. It is a window matrix for window-based redisplay.
576 Allocate glyph memory from the heap. */
577 if (dim.width > matrix->matrix_w
578 || new_rows
579 || header_line_changed_p
580 || marginal_areas_changed_p)
582 struct glyph_row *row = matrix->rows;
583 struct glyph_row *end = row + matrix->rows_allocated;
585 while (row < end)
587 row->glyphs[LEFT_MARGIN_AREA]
588 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
589 dim.width, sizeof (struct glyph));
591 /* The mode line never has marginal areas. */
592 if (row == matrix->rows + dim.height - 1
593 || (row == matrix->rows && matrix->header_line_p))
595 row->glyphs[TEXT_AREA]
596 = row->glyphs[LEFT_MARGIN_AREA];
597 row->glyphs[RIGHT_MARGIN_AREA]
598 = row->glyphs[TEXT_AREA] + dim.width;
599 row->glyphs[LAST_AREA]
600 = row->glyphs[RIGHT_MARGIN_AREA];
602 else
604 row->glyphs[TEXT_AREA]
605 = row->glyphs[LEFT_MARGIN_AREA] + left;
606 row->glyphs[RIGHT_MARGIN_AREA]
607 = row->glyphs[TEXT_AREA] + dim.width - left - right;
608 row->glyphs[LAST_AREA]
609 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
611 xassert (!row->enabled_p
612 || row->mode_line_p
613 || verify_row_hash (row));
614 ++row;
618 xassert (left >= 0 && right >= 0);
619 matrix->left_margin_glyphs = left;
620 matrix->right_margin_glyphs = right;
623 /* Number of rows to be used by MATRIX. */
624 matrix->nrows = dim.height;
625 xassert (matrix->nrows >= 0);
627 if (w)
629 if (matrix == w->current_matrix)
631 /* Mark rows in a current matrix of a window as not having
632 valid contents. It's important to not do this for
633 desired matrices. When Emacs starts, it may already be
634 building desired matrices when this function runs. */
635 if (window_width < 0)
636 window_width = window_box_width (w, -1);
638 /* Optimize the case that only the height has changed (C-x 2,
639 upper window). Invalidate all rows that are no longer part
640 of the window. */
641 if (!marginal_areas_changed_p
642 && !header_line_changed_p
643 && new_rows == 0
644 && dim.width == matrix->matrix_w
645 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
646 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
647 && matrix->window_width == window_width)
649 /* Find the last row in the window. */
650 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
651 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
653 ++i;
654 break;
657 /* Window end is invalid, if inside of the rows that
658 are invalidated below. */
659 if (INTEGERP (w->window_end_vpos)
660 && XFASTINT (w->window_end_vpos) >= i)
661 w->window_end_valid = Qnil;
663 while (i < matrix->nrows)
664 matrix->rows[i++].enabled_p = 0;
666 else
668 for (i = 0; i < matrix->nrows; ++i)
669 matrix->rows[i].enabled_p = 0;
672 else if (matrix == w->desired_matrix)
674 /* Rows in desired matrices always have to be cleared;
675 redisplay expects this is the case when it runs, so it
676 had better be the case when we adjust matrices between
677 redisplays. */
678 for (i = 0; i < matrix->nrows; ++i)
679 matrix->rows[i].enabled_p = 0;
684 /* Remember last values to be able to optimize frame redraws. */
685 matrix->matrix_x = x;
686 matrix->matrix_y = y;
687 matrix->matrix_w = dim.width;
688 matrix->matrix_h = dim.height;
690 /* Record the top y location and height of W at the time the matrix
691 was last adjusted. This is used to optimize redisplay above. */
692 if (w)
694 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
695 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
696 matrix->window_height = window_height;
697 matrix->window_width = window_width;
698 matrix->window_vscroll = w->vscroll;
703 /* Reverse the contents of rows in MATRIX between START and END. The
704 contents of the row at END - 1 end up at START, END - 2 at START +
705 1 etc. This is part of the implementation of rotate_matrix (see
706 below). */
708 static void
709 reverse_rows (struct glyph_matrix *matrix, int start, int end)
711 int i, j;
713 for (i = start, j = end - 1; i < j; ++i, --j)
715 /* Non-ISO HP/UX compiler doesn't like auto struct
716 initialization. */
717 struct glyph_row temp;
718 temp = matrix->rows[i];
719 matrix->rows[i] = matrix->rows[j];
720 matrix->rows[j] = temp;
725 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
726 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
727 indices. (Note: this does not copy glyphs, only glyph pointers in
728 row structures are moved around).
730 The algorithm used for rotating the vector was, I believe, first
731 described by Kernighan. See the vector R as consisting of two
732 sub-vectors AB, where A has length BY for BY >= 0. The result
733 after rotating is then BA. Reverse both sub-vectors to get ArBr
734 and reverse the result to get (ArBr)r which is BA. Similar for
735 rotating right. */
737 void
738 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
740 if (by < 0)
742 /* Up (rotate left, i.e. towards lower indices). */
743 by = -by;
744 reverse_rows (matrix, first, first + by);
745 reverse_rows (matrix, first + by, last);
746 reverse_rows (matrix, first, last);
748 else if (by > 0)
750 /* Down (rotate right, i.e. towards higher indices). */
751 reverse_rows (matrix, last - by, last);
752 reverse_rows (matrix, first, last - by);
753 reverse_rows (matrix, first, last);
758 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
759 with indices START <= index < END. Increment positions by DELTA/
760 DELTA_BYTES. */
762 void
763 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
764 EMACS_INT delta, EMACS_INT delta_bytes)
766 /* Check that START and END are reasonable values. */
767 xassert (start >= 0 && start <= matrix->nrows);
768 xassert (end >= 0 && end <= matrix->nrows);
769 xassert (start <= end);
771 for (; start < end; ++start)
772 increment_row_positions (matrix->rows + start, delta, delta_bytes);
776 /* Enable a range of rows in glyph matrix MATRIX. START and END are
777 the row indices of the first and last + 1 row to enable. If
778 ENABLED_P is non-zero, enabled_p flags in rows will be set to 1. */
780 void
781 enable_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end, int enabled_p)
783 xassert (start <= end);
784 xassert (start >= 0 && start < matrix->nrows);
785 xassert (end >= 0 && end <= matrix->nrows);
787 for (; start < end; ++start)
788 matrix->rows[start].enabled_p = enabled_p != 0;
792 /* Clear MATRIX.
794 This empties all rows in MATRIX by setting the enabled_p flag for
795 all rows of the matrix to zero. The function prepare_desired_row
796 will eventually really clear a row when it sees one with a zero
797 enabled_p flag.
799 Resets update hints to defaults value. The only update hint
800 currently present is the flag MATRIX->no_scrolling_p. */
802 void
803 clear_glyph_matrix (struct glyph_matrix *matrix)
805 if (matrix)
807 enable_glyph_matrix_rows (matrix, 0, matrix->nrows, 0);
808 matrix->no_scrolling_p = 0;
813 /* Shift part of the glyph matrix MATRIX of window W up or down.
814 Increment y-positions in glyph rows between START and END by DY,
815 and recompute their visible height. */
817 void
818 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
820 int min_y, max_y;
822 xassert (start <= end);
823 xassert (start >= 0 && start < matrix->nrows);
824 xassert (end >= 0 && end <= matrix->nrows);
826 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
827 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
829 for (; start < end; ++start)
831 struct glyph_row *row = &matrix->rows[start];
833 row->y += dy;
834 row->visible_height = row->height;
836 if (row->y < min_y)
837 row->visible_height -= min_y - row->y;
838 if (row->y + row->height > max_y)
839 row->visible_height -= row->y + row->height - max_y;
840 if (row->fringe_bitmap_periodic_p)
841 row->redraw_fringe_bitmaps_p = 1;
846 /* Mark all rows in current matrices of frame F as invalid. Marking
847 invalid is done by setting enabled_p to zero for all rows in a
848 current matrix. */
850 void
851 clear_current_matrices (register struct frame *f)
853 /* Clear frame current matrix, if we have one. */
854 if (f->current_matrix)
855 clear_glyph_matrix (f->current_matrix);
857 /* Clear the matrix of the menu bar window, if such a window exists.
858 The menu bar window is currently used to display menus on X when
859 no toolkit support is compiled in. */
860 if (WINDOWP (f->menu_bar_window))
861 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
863 /* Clear the matrix of the tool-bar window, if any. */
864 if (WINDOWP (f->tool_bar_window))
865 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
867 /* Clear current window matrices. */
868 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
869 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
873 /* Clear out all display lines of F for a coming redisplay. */
875 void
876 clear_desired_matrices (register struct frame *f)
878 if (f->desired_matrix)
879 clear_glyph_matrix (f->desired_matrix);
881 if (WINDOWP (f->menu_bar_window))
882 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
884 if (WINDOWP (f->tool_bar_window))
885 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
887 /* Do it for window matrices. */
888 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
889 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
893 /* Clear matrices in window tree rooted in W. If DESIRED_P is
894 non-zero clear desired matrices, otherwise clear current matrices. */
896 static void
897 clear_window_matrices (struct window *w, int desired_p)
899 while (w)
901 if (!NILP (w->hchild))
903 xassert (WINDOWP (w->hchild));
904 clear_window_matrices (XWINDOW (w->hchild), desired_p);
906 else if (!NILP (w->vchild))
908 xassert (WINDOWP (w->vchild));
909 clear_window_matrices (XWINDOW (w->vchild), desired_p);
911 else
913 if (desired_p)
914 clear_glyph_matrix (w->desired_matrix);
915 else
917 clear_glyph_matrix (w->current_matrix);
918 w->window_end_valid = Qnil;
922 w = NILP (w->next) ? 0 : XWINDOW (w->next);
928 /***********************************************************************
929 Glyph Rows
931 See dispextern.h for an overall explanation of glyph rows.
932 ***********************************************************************/
934 /* Clear glyph row ROW. Do it in a way that makes it robust against
935 changes in the glyph_row structure, i.e. addition or removal of
936 structure members. */
938 static struct glyph_row null_row;
940 void
941 clear_glyph_row (struct glyph_row *row)
943 struct glyph *p[1 + LAST_AREA];
945 /* Save pointers. */
946 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
947 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
948 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
949 p[LAST_AREA] = row->glyphs[LAST_AREA];
951 /* Clear. */
952 *row = null_row;
954 /* Restore pointers. */
955 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
956 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
957 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
958 row->glyphs[LAST_AREA] = p[LAST_AREA];
960 #if 0 /* At some point, some bit-fields of struct glyph were not set,
961 which made glyphs unequal when compared with GLYPH_EQUAL_P.
962 Redisplay outputs such glyphs, and flickering effects were
963 the result. This also depended on the contents of memory
964 returned by xmalloc. If flickering happens again, activate
965 the code below. If the flickering is gone with that, chances
966 are that the flickering has the same reason as here. */
967 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
968 #endif
972 /* Make ROW an empty, enabled row of canonical character height,
973 in window W starting at y-position Y. */
975 void
976 blank_row (struct window *w, struct glyph_row *row, int y)
978 int min_y, max_y;
980 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
981 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
983 clear_glyph_row (row);
984 row->y = y;
985 row->ascent = row->phys_ascent = 0;
986 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
987 row->visible_height = row->height;
989 if (row->y < min_y)
990 row->visible_height -= min_y - row->y;
991 if (row->y + row->height > max_y)
992 row->visible_height -= row->y + row->height - max_y;
994 row->enabled_p = 1;
998 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
999 are the amounts by which to change positions. Note that the first
1000 glyph of the text area of a row can have a buffer position even if
1001 the used count of the text area is zero. Such rows display line
1002 ends. */
1004 static void
1005 increment_row_positions (struct glyph_row *row,
1006 EMACS_INT delta, EMACS_INT delta_bytes)
1008 int area, i;
1010 /* Increment start and end positions. */
1011 MATRIX_ROW_START_CHARPOS (row) += delta;
1012 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
1013 MATRIX_ROW_END_CHARPOS (row) += delta;
1014 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
1015 CHARPOS (row->start.pos) += delta;
1016 BYTEPOS (row->start.pos) += delta_bytes;
1017 CHARPOS (row->end.pos) += delta;
1018 BYTEPOS (row->end.pos) += delta_bytes;
1020 if (!row->enabled_p)
1021 return;
1023 /* Increment positions in glyphs. */
1024 for (area = 0; area < LAST_AREA; ++area)
1025 for (i = 0; i < row->used[area]; ++i)
1026 if (BUFFERP (row->glyphs[area][i].object)
1027 && row->glyphs[area][i].charpos > 0)
1028 row->glyphs[area][i].charpos += delta;
1030 /* Capture the case of rows displaying a line end. */
1031 if (row->used[TEXT_AREA] == 0
1032 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
1033 row->glyphs[TEXT_AREA]->charpos += delta;
1037 #if 0
1038 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
1039 contents, i.e. glyph structure contents are exchanged between A and
1040 B without changing glyph pointers in A and B. */
1042 static void
1043 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
1045 int area;
1047 for (area = 0; area < LAST_AREA; ++area)
1049 /* Number of glyphs to swap. */
1050 int max_used = max (a->used[area], b->used[area]);
1052 /* Start of glyphs in area of row A. */
1053 struct glyph *glyph_a = a->glyphs[area];
1055 /* End + 1 of glyphs in area of row A. */
1056 struct glyph *glyph_a_end = a->glyphs[max_used];
1058 /* Start of glyphs in area of row B. */
1059 struct glyph *glyph_b = b->glyphs[area];
1061 while (glyph_a < glyph_a_end)
1063 /* Non-ISO HP/UX compiler doesn't like auto struct
1064 initialization. */
1065 struct glyph temp;
1066 temp = *glyph_a;
1067 *glyph_a = *glyph_b;
1068 *glyph_b = temp;
1069 ++glyph_a;
1070 ++glyph_b;
1075 #endif /* 0 */
1077 /* Exchange pointers to glyph memory between glyph rows A and B. Also
1078 exchange the used[] array and the hash values of the rows, because
1079 these should all go together for the row's hash value to be
1080 correct. */
1082 static inline void
1083 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1085 int i;
1086 unsigned hash_tem = a->hash;
1088 for (i = 0; i < LAST_AREA + 1; ++i)
1090 struct glyph *temp = a->glyphs[i];
1091 short used_tem = a->used[i];
1093 a->glyphs[i] = b->glyphs[i];
1094 b->glyphs[i] = temp;
1095 a->used[i] = b->used[i];
1096 b->used[i] = used_tem;
1098 a->hash = b->hash;
1099 b->hash = hash_tem;
1103 /* Copy glyph row structure FROM to glyph row structure TO, except
1104 that glyph pointers, the `used' counts, and the hash values in the
1105 structures are left unchanged. */
1107 static inline void
1108 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1110 struct glyph *pointers[1 + LAST_AREA];
1111 short used[1 + LAST_AREA];
1112 unsigned hashval;
1114 /* Save glyph pointers of TO. */
1115 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1116 memcpy (used, to->used, sizeof to->used);
1117 hashval = to->hash;
1119 /* Do a structure assignment. */
1120 *to = *from;
1122 /* Restore original pointers of TO. */
1123 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1124 memcpy (to->used, used, sizeof to->used);
1125 to->hash = hashval;
1129 /* Assign glyph row FROM to glyph row TO. This works like a structure
1130 assignment TO = FROM, except that glyph pointers are not copied but
1131 exchanged between TO and FROM. Pointers must be exchanged to avoid
1132 a memory leak. */
1134 static inline void
1135 assign_row (struct glyph_row *to, struct glyph_row *from)
1137 swap_glyph_pointers (to, from);
1138 copy_row_except_pointers (to, from);
1142 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1143 a row in a window matrix, is a slice of the glyph memory of the
1144 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1145 is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
1146 memory of FRAME_ROW. */
1148 #if GLYPH_DEBUG
1150 static int
1151 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1153 struct glyph *window_glyph_start = window_row->glyphs[0];
1154 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1155 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1157 return (frame_glyph_start <= window_glyph_start
1158 && window_glyph_start < frame_glyph_end);
1161 #endif /* GLYPH_DEBUG */
1163 #if 0
1165 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1166 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1167 in WINDOW_MATRIX is found satisfying the condition. */
1169 static struct glyph_row *
1170 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1171 struct glyph_matrix *frame_matrix, int row)
1173 int i;
1175 xassert (row >= 0 && row < frame_matrix->nrows);
1177 for (i = 0; i < window_matrix->nrows; ++i)
1178 if (glyph_row_slice_p (window_matrix->rows + i,
1179 frame_matrix->rows + row))
1180 break;
1182 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1185 #endif /* 0 */
1187 /* Prepare ROW for display. Desired rows are cleared lazily,
1188 i.e. they are only marked as to be cleared by setting their
1189 enabled_p flag to zero. When a row is to be displayed, a prior
1190 call to this function really clears it. */
1192 void
1193 prepare_desired_row (struct glyph_row *row)
1195 if (!row->enabled_p)
1197 int rp = row->reversed_p;
1199 clear_glyph_row (row);
1200 row->enabled_p = 1;
1201 row->reversed_p = rp;
1206 /* Return a hash code for glyph row ROW. */
1208 static int
1209 line_hash_code (struct glyph_row *row)
1211 int hash = 0;
1213 if (row->enabled_p)
1215 struct glyph *glyph = row->glyphs[TEXT_AREA];
1216 struct glyph *end = glyph + row->used[TEXT_AREA];
1218 while (glyph < end)
1220 int c = glyph->u.ch;
1221 int face_id = glyph->face_id;
1222 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1223 c -= SPACEGLYPH;
1224 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1225 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1226 ++glyph;
1229 if (hash == 0)
1230 hash = 1;
1233 return hash;
1237 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1238 the number of characters in the line. If must_write_spaces is
1239 zero, leading and trailing spaces are ignored. */
1241 static int
1242 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1244 struct glyph_row *row = matrix->rows + vpos;
1245 struct glyph *beg = row->glyphs[TEXT_AREA];
1246 struct glyph *end = beg + row->used[TEXT_AREA];
1247 int len;
1248 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1249 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1251 /* Ignore trailing and leading spaces if we can. */
1252 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1254 /* Skip from the end over trailing spaces. */
1255 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1256 --end;
1258 /* All blank line. */
1259 if (end == beg)
1260 return 0;
1262 /* Skip over leading spaces. */
1263 while (CHAR_GLYPH_SPACE_P (*beg))
1264 ++beg;
1267 /* If we don't have a glyph-table, each glyph is one character,
1268 so return the number of glyphs. */
1269 if (glyph_table_base == 0)
1270 len = end - beg;
1271 else
1273 /* Otherwise, scan the glyphs and accumulate their total length
1274 in LEN. */
1275 len = 0;
1276 while (beg < end)
1278 GLYPH g;
1280 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1282 if (GLYPH_INVALID_P (g)
1283 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1284 len += 1;
1285 else
1286 len += GLYPH_LENGTH (glyph_table_base, g);
1288 ++beg;
1292 return len;
1296 /* Test two glyph rows A and B for equality. Value is non-zero if A
1297 and B have equal contents. MOUSE_FACE_P non-zero means compare the
1298 mouse_face_p flags of A and B, too. */
1300 static inline int
1301 row_equal_p (struct glyph_row *a, struct glyph_row *b, int mouse_face_p)
1303 xassert (verify_row_hash (a));
1304 xassert (verify_row_hash (b));
1306 if (a == b)
1307 return 1;
1308 else if (a->hash != b->hash)
1309 return 0;
1310 else
1312 struct glyph *a_glyph, *b_glyph, *a_end;
1313 int area;
1315 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1316 return 0;
1318 /* Compare glyphs. */
1319 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1321 if (a->used[area] != b->used[area])
1322 return 0;
1324 a_glyph = a->glyphs[area];
1325 a_end = a_glyph + a->used[area];
1326 b_glyph = b->glyphs[area];
1328 while (a_glyph < a_end
1329 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1330 ++a_glyph, ++b_glyph;
1332 if (a_glyph != a_end)
1333 return 0;
1336 if (a->fill_line_p != b->fill_line_p
1337 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1338 || a->left_fringe_bitmap != b->left_fringe_bitmap
1339 || a->left_fringe_face_id != b->left_fringe_face_id
1340 || a->left_fringe_offset != b->left_fringe_offset
1341 || a->right_fringe_bitmap != b->right_fringe_bitmap
1342 || a->right_fringe_face_id != b->right_fringe_face_id
1343 || a->right_fringe_offset != b->right_fringe_offset
1344 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1345 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1346 || a->exact_window_width_line_p != b->exact_window_width_line_p
1347 || a->overlapped_p != b->overlapped_p
1348 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1349 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1350 || a->reversed_p != b->reversed_p
1351 /* Different partially visible characters on left margin. */
1352 || a->x != b->x
1353 /* Different height. */
1354 || a->ascent != b->ascent
1355 || a->phys_ascent != b->phys_ascent
1356 || a->phys_height != b->phys_height
1357 || a->visible_height != b->visible_height)
1358 return 0;
1361 return 1;
1366 /***********************************************************************
1367 Glyph Pool
1369 See dispextern.h for an overall explanation of glyph pools.
1370 ***********************************************************************/
1372 /* Allocate a glyph_pool structure. The structure returned is
1373 initialized with zeros. The global variable glyph_pool_count is
1374 incremented for each pool allocated. */
1376 static struct glyph_pool *
1377 new_glyph_pool (void)
1379 struct glyph_pool *result;
1381 /* Allocate a new glyph_pool and clear it. */
1382 result = (struct glyph_pool *) xmalloc (sizeof *result);
1383 memset (result, 0, sizeof *result);
1385 /* For memory leak and double deletion checking. */
1386 ++glyph_pool_count;
1388 return result;
1392 /* Free a glyph_pool structure POOL. The function may be called with
1393 a null POOL pointer. The global variable glyph_pool_count is
1394 decremented with every pool structure freed. If this count gets
1395 negative, more structures were freed than allocated, i.e. one
1396 structure must have been freed more than once or a bogus pointer
1397 was passed to free_glyph_pool. */
1399 static void
1400 free_glyph_pool (struct glyph_pool *pool)
1402 if (pool)
1404 /* More freed than allocated? */
1405 --glyph_pool_count;
1406 xassert (glyph_pool_count >= 0);
1408 xfree (pool->glyphs);
1409 xfree (pool);
1414 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1415 columns we need. This function never shrinks a pool. The only
1416 case in which this would make sense, would be when a frame's size
1417 is changed from a large value to a smaller one. But, if someone
1418 does it once, we can expect that he will do it again.
1420 Value is non-zero if the pool changed in a way which makes
1421 re-adjusting window glyph matrices necessary. */
1423 static int
1424 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1426 ptrdiff_t needed;
1427 int changed_p;
1429 changed_p = (pool->glyphs == 0
1430 || matrix_dim.height != pool->nrows
1431 || matrix_dim.width != pool->ncolumns);
1433 /* Enlarge the glyph pool. */
1434 needed = matrix_dim.width;
1435 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1436 memory_full (SIZE_MAX);
1437 needed *= matrix_dim.height;
1438 if (needed > pool->nglyphs)
1440 ptrdiff_t old_nglyphs = pool->nglyphs;
1441 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1442 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1443 memset (pool->glyphs + old_nglyphs, 0,
1444 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1447 /* Remember the number of rows and columns because (a) we use them
1448 to do sanity checks, and (b) the number of columns determines
1449 where rows in the frame matrix start---this must be available to
1450 determine pointers to rows of window sub-matrices. */
1451 pool->nrows = matrix_dim.height;
1452 pool->ncolumns = matrix_dim.width;
1454 return changed_p;
1459 /***********************************************************************
1460 Debug Code
1461 ***********************************************************************/
1463 #if GLYPH_DEBUG
1466 /* Flush standard output. This is sometimes useful to call from the debugger.
1467 XXX Maybe this should be changed to flush the current terminal instead of
1468 stdout.
1471 void flush_stdout (void) EXTERNALLY_VISIBLE;
1473 void
1474 flush_stdout (void)
1476 fflush (stdout);
1480 /* Check that no glyph pointers have been lost in MATRIX. If a
1481 pointer has been lost, e.g. by using a structure assignment between
1482 rows, at least one pointer must occur more than once in the rows of
1483 MATRIX. */
1485 void
1486 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1488 int i, j;
1490 for (i = 0; i < matrix->nrows; ++i)
1491 for (j = 0; j < matrix->nrows; ++j)
1492 xassert (i == j
1493 || (matrix->rows[i].glyphs[TEXT_AREA]
1494 != matrix->rows[j].glyphs[TEXT_AREA]));
1498 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1500 struct glyph_row *
1501 matrix_row (struct glyph_matrix *matrix, int row)
1503 xassert (matrix && matrix->rows);
1504 xassert (row >= 0 && row < matrix->nrows);
1506 /* That's really too slow for normal testing because this function
1507 is called almost everywhere. Although---it's still astonishingly
1508 fast, so it is valuable to have for debugging purposes. */
1509 #if 0
1510 check_matrix_pointer_lossage (matrix);
1511 #endif
1513 return matrix->rows + row;
1517 #if 0 /* This function makes invalid assumptions when text is
1518 partially invisible. But it might come handy for debugging
1519 nevertheless. */
1521 /* Check invariants that must hold for an up to date current matrix of
1522 window W. */
1524 static void
1525 check_matrix_invariants (struct window *w)
1527 struct glyph_matrix *matrix = w->current_matrix;
1528 int yb = window_text_bottom_y (w);
1529 struct glyph_row *row = matrix->rows;
1530 struct glyph_row *last_text_row = NULL;
1531 struct buffer *saved = current_buffer;
1532 struct buffer *buffer = XBUFFER (w->buffer);
1533 int c;
1535 /* This can sometimes happen for a fresh window. */
1536 if (matrix->nrows < 2)
1537 return;
1539 set_buffer_temp (buffer);
1541 /* Note: last row is always reserved for the mode line. */
1542 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1543 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1545 struct glyph_row *next = row + 1;
1547 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1548 last_text_row = row;
1550 /* Check that character and byte positions are in sync. */
1551 xassert (MATRIX_ROW_START_BYTEPOS (row)
1552 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1553 xassert (BYTEPOS (row->start.pos)
1554 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1556 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1557 have such a position temporarily in case of a minibuffer
1558 displaying something like `[Sole completion]' at its end. */
1559 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1561 xassert (MATRIX_ROW_END_BYTEPOS (row)
1562 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1563 xassert (BYTEPOS (row->end.pos)
1564 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1567 /* Check that end position of `row' is equal to start position
1568 of next row. */
1569 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1571 xassert (MATRIX_ROW_END_CHARPOS (row)
1572 == MATRIX_ROW_START_CHARPOS (next));
1573 xassert (MATRIX_ROW_END_BYTEPOS (row)
1574 == MATRIX_ROW_START_BYTEPOS (next));
1575 xassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1576 xassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1578 row = next;
1581 xassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1582 xassert (w->desired_matrix->rows != NULL);
1583 set_buffer_temp (saved);
1586 #endif /* 0 */
1588 #endif /* GLYPH_DEBUG != 0 */
1592 /**********************************************************************
1593 Allocating/ Adjusting Glyph Matrices
1594 **********************************************************************/
1596 /* Allocate glyph matrices over a window tree for a frame-based
1597 redisplay
1599 X and Y are column/row within the frame glyph matrix where
1600 sub-matrices for the window tree rooted at WINDOW must be
1601 allocated. DIM_ONLY_P non-zero means that the caller of this
1602 function is only interested in the result matrix dimension, and
1603 matrix adjustments should not be performed.
1605 The function returns the total width/height of the sub-matrices of
1606 the window tree. If called on a frame root window, the computation
1607 will take the mini-buffer window into account.
1609 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1611 NEW_LEAF_MATRIX set if any window in the tree did not have a
1612 glyph matrices yet, and
1614 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1615 any window in the tree will be changed or have been changed (see
1616 DIM_ONLY_P)
1618 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1619 function.
1621 Windows are arranged into chains of windows on the same level
1622 through the next fields of window structures. Such a level can be
1623 either a sequence of horizontally adjacent windows from left to
1624 right, or a sequence of vertically adjacent windows from top to
1625 bottom. Each window in a horizontal sequence can be either a leaf
1626 window or a vertical sequence; a window in a vertical sequence can
1627 be either a leaf or a horizontal sequence. All windows in a
1628 horizontal sequence have the same height, and all windows in a
1629 vertical sequence have the same width.
1631 This function uses, for historical reasons, a more general
1632 algorithm to determine glyph matrix dimensions that would be
1633 necessary.
1635 The matrix height of a horizontal sequence is determined by the
1636 maximum height of any matrix in the sequence. The matrix width of
1637 a horizontal sequence is computed by adding up matrix widths of
1638 windows in the sequence.
1640 |<------- result width ------->|
1641 +---------+----------+---------+ ---
1642 | | | | |
1643 | | | |
1644 +---------+ | | result height
1645 | +---------+
1646 | | |
1647 +----------+ ---
1649 The matrix width of a vertical sequence is the maximum matrix width
1650 of any window in the sequence. Its height is computed by adding up
1651 matrix heights of windows in the sequence.
1653 |<---- result width -->|
1654 +---------+ ---
1655 | | |
1656 | | |
1657 +---------+--+ |
1658 | | |
1659 | | result height
1661 +------------+---------+ |
1662 | | |
1663 | | |
1664 +------------+---------+ --- */
1666 /* Bit indicating that a new matrix will be allocated or has been
1667 allocated. */
1669 #define NEW_LEAF_MATRIX (1 << 0)
1671 /* Bit indicating that a matrix will or has changed its location or
1672 size. */
1674 #define CHANGED_LEAF_MATRIX (1 << 1)
1676 static struct dim
1677 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1678 int dim_only_p, int *window_change_flags)
1680 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1681 int x0 = x, y0 = y;
1682 int wmax = 0, hmax = 0;
1683 struct dim total;
1684 struct dim dim;
1685 struct window *w;
1686 int in_horz_combination_p;
1688 /* What combination is WINDOW part of? Compute this once since the
1689 result is the same for all windows in the `next' chain. The
1690 special case of a root window (parent equal to nil) is treated
1691 like a vertical combination because a root window's `next'
1692 points to the mini-buffer window, if any, which is arranged
1693 vertically below other windows. */
1694 in_horz_combination_p
1695 = (!NILP (XWINDOW (window)->parent)
1696 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1698 /* For WINDOW and all windows on the same level. */
1701 w = XWINDOW (window);
1703 /* Get the dimension of the window sub-matrix for W, depending
1704 on whether this is a combination or a leaf window. */
1705 if (!NILP (w->hchild))
1706 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
1707 dim_only_p,
1708 window_change_flags);
1709 else if (!NILP (w->vchild))
1710 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
1711 dim_only_p,
1712 window_change_flags);
1713 else
1715 /* If not already done, allocate sub-matrix structures. */
1716 if (w->desired_matrix == NULL)
1718 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1719 w->current_matrix = new_glyph_matrix (f->current_pool);
1720 *window_change_flags |= NEW_LEAF_MATRIX;
1723 /* Width and height MUST be chosen so that there are no
1724 holes in the frame matrix. */
1725 dim.width = required_matrix_width (w);
1726 dim.height = required_matrix_height (w);
1728 /* Will matrix be re-allocated? */
1729 if (x != w->desired_matrix->matrix_x
1730 || y != w->desired_matrix->matrix_y
1731 || dim.width != w->desired_matrix->matrix_w
1732 || dim.height != w->desired_matrix->matrix_h
1733 || (margin_glyphs_to_reserve (w, dim.width,
1734 w->left_margin_cols)
1735 != w->desired_matrix->left_margin_glyphs)
1736 || (margin_glyphs_to_reserve (w, dim.width,
1737 w->right_margin_cols)
1738 != w->desired_matrix->right_margin_glyphs))
1739 *window_change_flags |= CHANGED_LEAF_MATRIX;
1741 /* Actually change matrices, if allowed. Do not consider
1742 CHANGED_LEAF_MATRIX computed above here because the pool
1743 may have been changed which we don't now here. We trust
1744 that we only will be called with DIM_ONLY_P != 0 when
1745 necessary. */
1746 if (!dim_only_p)
1748 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1749 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1753 /* If we are part of a horizontal combination, advance x for
1754 windows to the right of W; otherwise advance y for windows
1755 below W. */
1756 if (in_horz_combination_p)
1757 x += dim.width;
1758 else
1759 y += dim.height;
1761 /* Remember maximum glyph matrix dimensions. */
1762 wmax = max (wmax, dim.width);
1763 hmax = max (hmax, dim.height);
1765 /* Next window on same level. */
1766 window = w->next;
1768 while (!NILP (window));
1770 /* Set `total' to the total glyph matrix dimension of this window
1771 level. In a vertical combination, the width is the width of the
1772 widest window; the height is the y we finally reached, corrected
1773 by the y we started with. In a horizontal combination, the total
1774 height is the height of the tallest window, and the width is the
1775 x we finally reached, corrected by the x we started with. */
1776 if (in_horz_combination_p)
1778 total.width = x - x0;
1779 total.height = hmax;
1781 else
1783 total.width = wmax;
1784 total.height = y - y0;
1787 return total;
1791 /* Return the required height of glyph matrices for window W. */
1793 static int
1794 required_matrix_height (struct window *w)
1796 #ifdef HAVE_WINDOW_SYSTEM
1797 struct frame *f = XFRAME (w->frame);
1799 if (FRAME_WINDOW_P (f))
1801 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1802 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1803 return (((window_pixel_height + ch_height - 1)
1804 / ch_height) * w->nrows_scale_factor
1805 /* One partially visible line at the top and
1806 bottom of the window. */
1808 /* 2 for header and mode line. */
1809 + 2);
1811 #endif /* HAVE_WINDOW_SYSTEM */
1813 return WINDOW_TOTAL_LINES (w);
1817 /* Return the required width of glyph matrices for window W. */
1819 static int
1820 required_matrix_width (struct window *w)
1822 #ifdef HAVE_WINDOW_SYSTEM
1823 struct frame *f = XFRAME (w->frame);
1824 if (FRAME_WINDOW_P (f))
1826 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1827 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1829 /* Compute number of glyphs needed in a glyph row. */
1830 return (((window_pixel_width + ch_width - 1)
1831 / ch_width) * w->ncols_scale_factor
1832 /* 2 partially visible columns in the text area. */
1834 /* One partially visible column at the right
1835 edge of each marginal area. */
1836 + 1 + 1);
1838 #endif /* HAVE_WINDOW_SYSTEM */
1840 return XINT (w->total_cols);
1844 /* Allocate window matrices for window-based redisplay. W is the
1845 window whose matrices must be allocated/reallocated. */
1847 static void
1848 allocate_matrices_for_window_redisplay (struct window *w)
1850 while (w)
1852 if (!NILP (w->vchild))
1853 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
1854 else if (!NILP (w->hchild))
1855 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
1856 else
1858 /* W is a leaf window. */
1859 struct dim dim;
1861 /* If matrices are not yet allocated, allocate them now. */
1862 if (w->desired_matrix == NULL)
1864 w->desired_matrix = new_glyph_matrix (NULL);
1865 w->current_matrix = new_glyph_matrix (NULL);
1868 dim.width = required_matrix_width (w);
1869 dim.height = required_matrix_height (w);
1870 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1871 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1874 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1879 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1880 do it for all frames; otherwise do it just for the given frame.
1881 This function must be called when a new frame is created, its size
1882 changes, or its window configuration changes. */
1884 void
1885 adjust_glyphs (struct frame *f)
1887 /* Block input so that expose events and other events that access
1888 glyph matrices are not processed while we are changing them. */
1889 BLOCK_INPUT;
1891 if (f)
1892 adjust_frame_glyphs (f);
1893 else
1895 Lisp_Object tail, lisp_frame;
1897 FOR_EACH_FRAME (tail, lisp_frame)
1898 adjust_frame_glyphs (XFRAME (lisp_frame));
1901 UNBLOCK_INPUT;
1905 /* Adjust frame glyphs when Emacs is initialized.
1907 To be called from init_display.
1909 We need a glyph matrix because redraw will happen soon.
1910 Unfortunately, window sizes on selected_frame are not yet set to
1911 meaningful values. I believe we can assume that there are only two
1912 windows on the frame---the mini-buffer and the root window. Frame
1913 height and width seem to be correct so far. So, set the sizes of
1914 windows to estimated values. */
1916 static void
1917 adjust_frame_glyphs_initially (void)
1919 struct frame *sf = SELECTED_FRAME ();
1920 struct window *root = XWINDOW (sf->root_window);
1921 struct window *mini = XWINDOW (root->next);
1922 int frame_lines = FRAME_LINES (sf);
1923 int frame_cols = FRAME_COLS (sf);
1924 int top_margin = FRAME_TOP_MARGIN (sf);
1926 /* Do it for the root window. */
1927 XSETFASTINT (root->top_line, top_margin);
1928 XSETFASTINT (root->total_lines, frame_lines - 1 - top_margin);
1929 XSETFASTINT (root->total_cols, frame_cols);
1931 /* Do it for the mini-buffer window. */
1932 XSETFASTINT (mini->top_line, frame_lines - 1);
1933 XSETFASTINT (mini->total_lines, 1);
1934 XSETFASTINT (mini->total_cols, frame_cols);
1936 adjust_frame_glyphs (sf);
1937 glyphs_initialized_initially_p = 1;
1941 /* Allocate/reallocate glyph matrices of a single frame F. */
1943 static void
1944 adjust_frame_glyphs (struct frame *f)
1946 if (FRAME_WINDOW_P (f))
1947 adjust_frame_glyphs_for_window_redisplay (f);
1948 else
1949 adjust_frame_glyphs_for_frame_redisplay (f);
1951 /* Don't forget the message buffer and the buffer for
1952 decode_mode_spec. */
1953 adjust_frame_message_buffer (f);
1954 adjust_decode_mode_spec_buffer (f);
1956 f->glyphs_initialized_p = 1;
1959 /* Return 1 if any window in the tree has nonzero window margins. See
1960 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1961 static int
1962 showing_window_margins_p (struct window *w)
1964 while (w)
1966 if (!NILP (w->hchild))
1968 if (showing_window_margins_p (XWINDOW (w->hchild)))
1969 return 1;
1971 else if (!NILP (w->vchild))
1973 if (showing_window_margins_p (XWINDOW (w->vchild)))
1974 return 1;
1976 else if (!NILP (w->left_margin_cols)
1977 || !NILP (w->right_margin_cols))
1978 return 1;
1980 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1982 return 0;
1986 /* In the window tree with root W, build current matrices of leaf
1987 windows from the frame's current matrix. */
1989 static void
1990 fake_current_matrices (Lisp_Object window)
1992 struct window *w;
1994 for (; !NILP (window); window = w->next)
1996 w = XWINDOW (window);
1998 if (!NILP (w->hchild))
1999 fake_current_matrices (w->hchild);
2000 else if (!NILP (w->vchild))
2001 fake_current_matrices (w->vchild);
2002 else
2004 int i;
2005 struct frame *f = XFRAME (w->frame);
2006 struct glyph_matrix *m = w->current_matrix;
2007 struct glyph_matrix *fm = f->current_matrix;
2009 xassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
2010 xassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
2012 for (i = 0; i < m->matrix_h; ++i)
2014 struct glyph_row *r = m->rows + i;
2015 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
2017 xassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
2018 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
2020 r->enabled_p = fr->enabled_p;
2021 if (r->enabled_p)
2023 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
2024 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
2025 r->used[TEXT_AREA] = (m->matrix_w
2026 - r->used[LEFT_MARGIN_AREA]
2027 - r->used[RIGHT_MARGIN_AREA]);
2028 r->mode_line_p = 0;
2036 /* Save away the contents of frame F's current frame matrix. Value is
2037 a glyph matrix holding the contents of F's current frame matrix. */
2039 static struct glyph_matrix *
2040 save_current_matrix (struct frame *f)
2042 int i;
2043 struct glyph_matrix *saved;
2045 saved = (struct glyph_matrix *) xmalloc (sizeof *saved);
2046 memset (saved, 0, sizeof *saved);
2047 saved->nrows = f->current_matrix->nrows;
2048 saved->rows = (struct glyph_row *) xmalloc (saved->nrows
2049 * sizeof *saved->rows);
2050 memset (saved->rows, 0, saved->nrows * sizeof *saved->rows);
2052 for (i = 0; i < saved->nrows; ++i)
2054 struct glyph_row *from = f->current_matrix->rows + i;
2055 struct glyph_row *to = saved->rows + i;
2056 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2057 to->glyphs[TEXT_AREA] = (struct glyph *) xmalloc (nbytes);
2058 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2059 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2062 return saved;
2066 /* Restore the contents of frame F's current frame matrix from SAVED,
2067 and free memory associated with SAVED. */
2069 static void
2070 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
2072 int i;
2074 for (i = 0; i < saved->nrows; ++i)
2076 struct glyph_row *from = saved->rows + i;
2077 struct glyph_row *to = f->current_matrix->rows + i;
2078 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2079 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2080 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2081 xfree (from->glyphs[TEXT_AREA]);
2084 xfree (saved->rows);
2085 xfree (saved);
2090 /* Allocate/reallocate glyph matrices of a single frame F for
2091 frame-based redisplay. */
2093 static void
2094 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
2096 struct dim matrix_dim;
2097 int pool_changed_p;
2098 int window_change_flags;
2099 int top_window_y;
2101 if (!FRAME_LIVE_P (f))
2102 return;
2104 top_window_y = FRAME_TOP_MARGIN (f);
2106 /* Allocate glyph pool structures if not already done. */
2107 if (f->desired_pool == NULL)
2109 f->desired_pool = new_glyph_pool ();
2110 f->current_pool = new_glyph_pool ();
2113 /* Allocate frames matrix structures if needed. */
2114 if (f->desired_matrix == NULL)
2116 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2117 f->current_matrix = new_glyph_matrix (f->current_pool);
2120 /* Compute window glyph matrices. (This takes the mini-buffer
2121 window into account). The result is the size of the frame glyph
2122 matrix needed. The variable window_change_flags is set to a bit
2123 mask indicating whether new matrices will be allocated or
2124 existing matrices change their size or location within the frame
2125 matrix. */
2126 window_change_flags = 0;
2127 matrix_dim
2128 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2129 0, top_window_y,
2131 &window_change_flags);
2133 /* Add in menu bar lines, if any. */
2134 matrix_dim.height += top_window_y;
2136 /* Enlarge pools as necessary. */
2137 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2138 realloc_glyph_pool (f->current_pool, matrix_dim);
2140 /* Set up glyph pointers within window matrices. Do this only if
2141 absolutely necessary since it requires a frame redraw. */
2142 if (pool_changed_p || window_change_flags)
2144 /* Do it for window matrices. */
2145 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2146 0, top_window_y, 0,
2147 &window_change_flags);
2149 /* Size of frame matrices must equal size of frame. Note
2150 that we are called for X frames with window widths NOT equal
2151 to the frame width (from CHANGE_FRAME_SIZE_1). */
2152 xassert (matrix_dim.width == FRAME_COLS (f)
2153 && matrix_dim.height == FRAME_LINES (f));
2155 /* Pointers to glyph memory in glyph rows are exchanged during
2156 the update phase of redisplay, which means in general that a
2157 frame's current matrix consists of pointers into both the
2158 desired and current glyph pool of the frame. Adjusting a
2159 matrix sets the frame matrix up so that pointers are all into
2160 the same pool. If we want to preserve glyph contents of the
2161 current matrix over a call to adjust_glyph_matrix, we must
2162 make a copy of the current glyphs, and restore the current
2163 matrix' contents from that copy. */
2164 if (display_completed
2165 && !FRAME_GARBAGED_P (f)
2166 && matrix_dim.width == f->current_matrix->matrix_w
2167 && matrix_dim.height == f->current_matrix->matrix_h
2168 /* For some reason, the frame glyph matrix gets corrupted if
2169 any of the windows contain margins. I haven't been able
2170 to hunt down the reason, but for the moment this prevents
2171 the problem from manifesting. -- cyd */
2172 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2174 struct glyph_matrix *copy = save_current_matrix (f);
2175 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2176 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2177 restore_current_matrix (f, copy);
2178 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2180 else
2182 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2183 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2184 SET_FRAME_GARBAGED (f);
2190 /* Allocate/reallocate glyph matrices of a single frame F for
2191 window-based redisplay. */
2193 static void
2194 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2196 xassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2198 /* Allocate/reallocate window matrices. */
2199 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2201 #ifdef HAVE_X_WINDOWS
2202 /* Allocate/ reallocate matrices of the dummy window used to display
2203 the menu bar under X when no X toolkit support is available. */
2204 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2206 /* Allocate a dummy window if not already done. */
2207 struct window *w;
2208 if (NILP (f->menu_bar_window))
2210 f->menu_bar_window = make_window ();
2211 w = XWINDOW (f->menu_bar_window);
2212 XSETFRAME (w->frame, f);
2213 w->pseudo_window_p = 1;
2215 else
2216 w = XWINDOW (f->menu_bar_window);
2218 /* Set window dimensions to frame dimensions and allocate or
2219 adjust glyph matrices of W. */
2220 XSETFASTINT (w->top_line, 0);
2221 XSETFASTINT (w->left_col, 0);
2222 XSETFASTINT (w->total_lines, FRAME_MENU_BAR_LINES (f));
2223 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2224 allocate_matrices_for_window_redisplay (w);
2226 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2227 #endif /* HAVE_X_WINDOWS */
2229 #ifndef USE_GTK
2231 /* Allocate/ reallocate matrices of the tool bar window. If we
2232 don't have a tool bar window yet, make one. */
2233 struct window *w;
2234 if (NILP (f->tool_bar_window))
2236 f->tool_bar_window = make_window ();
2237 w = XWINDOW (f->tool_bar_window);
2238 XSETFRAME (w->frame, f);
2239 w->pseudo_window_p = 1;
2241 else
2242 w = XWINDOW (f->tool_bar_window);
2244 XSETFASTINT (w->top_line, FRAME_MENU_BAR_LINES (f));
2245 XSETFASTINT (w->left_col, 0);
2246 XSETFASTINT (w->total_lines, FRAME_TOOL_BAR_LINES (f));
2247 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2248 allocate_matrices_for_window_redisplay (w);
2250 #endif
2254 /* Adjust/ allocate message buffer of frame F.
2256 Note that the message buffer is never freed. Since I could not
2257 find a free in 19.34, I assume that freeing it would be
2258 problematic in some way and don't do it either.
2260 (Implementation note: It should be checked if we can free it
2261 eventually without causing trouble). */
2263 static void
2264 adjust_frame_message_buffer (struct frame *f)
2266 ptrdiff_t size = FRAME_MESSAGE_BUF_SIZE (f) + 1;
2268 if (FRAME_MESSAGE_BUF (f))
2270 char *buffer = FRAME_MESSAGE_BUF (f);
2271 char *new_buffer = (char *) xrealloc (buffer, size);
2272 FRAME_MESSAGE_BUF (f) = new_buffer;
2274 else
2275 FRAME_MESSAGE_BUF (f) = (char *) xmalloc (size);
2279 /* Re-allocate buffer for decode_mode_spec on frame F. */
2281 static void
2282 adjust_decode_mode_spec_buffer (struct frame *f)
2284 f->decode_mode_spec_buffer
2285 = (char *) xrealloc (f->decode_mode_spec_buffer,
2286 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2291 /**********************************************************************
2292 Freeing Glyph Matrices
2293 **********************************************************************/
2295 /* Free glyph memory for a frame F. F may be null. This function can
2296 be called for the same frame more than once. The root window of
2297 F may be nil when this function is called. This is the case when
2298 the function is called when F is destroyed. */
2300 void
2301 free_glyphs (struct frame *f)
2303 if (f && f->glyphs_initialized_p)
2305 /* Block interrupt input so that we don't get surprised by an X
2306 event while we're in an inconsistent state. */
2307 BLOCK_INPUT;
2308 f->glyphs_initialized_p = 0;
2310 /* Release window sub-matrices. */
2311 if (!NILP (f->root_window))
2312 free_window_matrices (XWINDOW (f->root_window));
2314 /* Free the dummy window for menu bars without X toolkit and its
2315 glyph matrices. */
2316 if (!NILP (f->menu_bar_window))
2318 struct window *w = XWINDOW (f->menu_bar_window);
2319 free_glyph_matrix (w->desired_matrix);
2320 free_glyph_matrix (w->current_matrix);
2321 w->desired_matrix = w->current_matrix = NULL;
2322 f->menu_bar_window = Qnil;
2325 /* Free the tool bar window and its glyph matrices. */
2326 if (!NILP (f->tool_bar_window))
2328 struct window *w = XWINDOW (f->tool_bar_window);
2329 free_glyph_matrix (w->desired_matrix);
2330 free_glyph_matrix (w->current_matrix);
2331 w->desired_matrix = w->current_matrix = NULL;
2332 f->tool_bar_window = Qnil;
2335 /* Release frame glyph matrices. Reset fields to zero in
2336 case we are called a second time. */
2337 if (f->desired_matrix)
2339 free_glyph_matrix (f->desired_matrix);
2340 free_glyph_matrix (f->current_matrix);
2341 f->desired_matrix = f->current_matrix = NULL;
2344 /* Release glyph pools. */
2345 if (f->desired_pool)
2347 free_glyph_pool (f->desired_pool);
2348 free_glyph_pool (f->current_pool);
2349 f->desired_pool = f->current_pool = NULL;
2352 UNBLOCK_INPUT;
2357 /* Free glyph sub-matrices in the window tree rooted at W. This
2358 function may be called with a null pointer, and it may be called on
2359 the same tree more than once. */
2361 void
2362 free_window_matrices (struct window *w)
2364 while (w)
2366 if (!NILP (w->hchild))
2367 free_window_matrices (XWINDOW (w->hchild));
2368 else if (!NILP (w->vchild))
2369 free_window_matrices (XWINDOW (w->vchild));
2370 else
2372 /* This is a leaf window. Free its memory and reset fields
2373 to zero in case this function is called a second time for
2374 W. */
2375 free_glyph_matrix (w->current_matrix);
2376 free_glyph_matrix (w->desired_matrix);
2377 w->current_matrix = w->desired_matrix = NULL;
2380 /* Next window on same level. */
2381 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2386 /* Check glyph memory leaks. This function is called from
2387 shut_down_emacs. Note that frames are not destroyed when Emacs
2388 exits. We therefore free all glyph memory for all active frames
2389 explicitly and check that nothing is left allocated. */
2391 void
2392 check_glyph_memory (void)
2394 Lisp_Object tail, frame;
2396 /* Free glyph memory for all frames. */
2397 FOR_EACH_FRAME (tail, frame)
2398 free_glyphs (XFRAME (frame));
2400 /* Check that nothing is left allocated. */
2401 if (glyph_matrix_count)
2402 abort ();
2403 if (glyph_pool_count)
2404 abort ();
2409 /**********************************************************************
2410 Building a Frame Matrix
2411 **********************************************************************/
2413 /* Most of the redisplay code works on glyph matrices attached to
2414 windows. This is a good solution most of the time, but it is not
2415 suitable for terminal code. Terminal output functions cannot rely
2416 on being able to set an arbitrary terminal window. Instead they
2417 must be provided with a view of the whole frame, i.e. the whole
2418 screen. We build such a view by constructing a frame matrix from
2419 window matrices in this section.
2421 Windows that must be updated have their must_be_update_p flag set.
2422 For all such windows, their desired matrix is made part of the
2423 desired frame matrix. For other windows, their current matrix is
2424 made part of the desired frame matrix.
2426 +-----------------+----------------+
2427 | desired | desired |
2428 | | |
2429 +-----------------+----------------+
2430 | current |
2432 +----------------------------------+
2434 Desired window matrices can be made part of the frame matrix in a
2435 cheap way: We exploit the fact that the desired frame matrix and
2436 desired window matrices share their glyph memory. This is not
2437 possible for current window matrices. Their glyphs are copied to
2438 the desired frame matrix. The latter is equivalent to
2439 preserve_other_columns in the old redisplay.
2441 Used glyphs counters for frame matrix rows are the result of adding
2442 up glyph lengths of the window matrices. A line in the frame
2443 matrix is enabled, if a corresponding line in a window matrix is
2444 enabled.
2446 After building the desired frame matrix, it will be passed to
2447 terminal code, which will manipulate both the desired and current
2448 frame matrix. Changes applied to the frame's current matrix have
2449 to be visible in current window matrices afterwards, of course.
2451 This problem is solved like this:
2453 1. Window and frame matrices share glyphs. Window matrices are
2454 constructed in a way that their glyph contents ARE the glyph
2455 contents needed in a frame matrix. Thus, any modification of
2456 glyphs done in terminal code will be reflected in window matrices
2457 automatically.
2459 2. Exchanges of rows in a frame matrix done by terminal code are
2460 intercepted by hook functions so that corresponding row operations
2461 on window matrices can be performed. This is necessary because we
2462 use pointers to glyphs in glyph row structures. To satisfy the
2463 assumption of point 1 above that glyphs are updated implicitly in
2464 window matrices when they are manipulated via the frame matrix,
2465 window and frame matrix must of course agree where to find the
2466 glyphs for their rows. Possible manipulations that must be
2467 mirrored are assignments of rows of the desired frame matrix to the
2468 current frame matrix and scrolling the current frame matrix. */
2470 /* Build frame F's desired matrix from window matrices. Only windows
2471 which have the flag must_be_updated_p set have to be updated. Menu
2472 bar lines of a frame are not covered by window matrices, so make
2473 sure not to touch them in this function. */
2475 static void
2476 build_frame_matrix (struct frame *f)
2478 int i;
2480 /* F must have a frame matrix when this function is called. */
2481 xassert (!FRAME_WINDOW_P (f));
2483 /* Clear all rows in the frame matrix covered by window matrices.
2484 Menu bar lines are not covered by windows. */
2485 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2486 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2488 /* Build the matrix by walking the window tree. */
2489 build_frame_matrix_from_window_tree (f->desired_matrix,
2490 XWINDOW (FRAME_ROOT_WINDOW (f)));
2494 /* Walk a window tree, building a frame matrix MATRIX from window
2495 matrices. W is the root of a window tree. */
2497 static void
2498 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2500 while (w)
2502 if (!NILP (w->hchild))
2503 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2504 else if (!NILP (w->vchild))
2505 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2506 else
2507 build_frame_matrix_from_leaf_window (matrix, w);
2509 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2514 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2515 desired frame matrix built. W is a leaf window whose desired or
2516 current matrix is to be added to FRAME_MATRIX. W's flag
2517 must_be_updated_p determines which matrix it contributes to
2518 FRAME_MATRIX. If must_be_updated_p is non-zero, W's desired matrix
2519 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2520 Adding a desired matrix means setting up used counters and such in
2521 frame rows, while adding a current window matrix to FRAME_MATRIX
2522 means copying glyphs. The latter case corresponds to
2523 preserve_other_columns in the old redisplay. */
2525 static void
2526 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2528 struct glyph_matrix *window_matrix;
2529 int window_y, frame_y;
2530 /* If non-zero, a glyph to insert at the right border of W. */
2531 GLYPH right_border_glyph;
2533 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2535 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2536 if (w->must_be_updated_p)
2538 window_matrix = w->desired_matrix;
2540 /* Decide whether we want to add a vertical border glyph. */
2541 if (!WINDOW_RIGHTMOST_P (w))
2543 struct Lisp_Char_Table *dp = window_display_table (w);
2544 Lisp_Object gc;
2546 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2547 if (dp
2548 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc))
2549 && GLYPH_CODE_CHAR_VALID_P (gc))
2551 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2552 spec_glyph_lookup_face (w, &right_border_glyph);
2555 if (GLYPH_FACE (right_border_glyph) <= 0)
2556 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2559 else
2560 window_matrix = w->current_matrix;
2562 /* For all rows in the window matrix and corresponding rows in the
2563 frame matrix. */
2564 window_y = 0;
2565 frame_y = window_matrix->matrix_y;
2566 while (window_y < window_matrix->nrows)
2568 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2569 struct glyph_row *window_row = window_matrix->rows + window_y;
2570 int current_row_p = window_matrix == w->current_matrix;
2572 /* Fill up the frame row with spaces up to the left margin of the
2573 window row. */
2574 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2576 /* Fill up areas in the window matrix row with spaces. */
2577 fill_up_glyph_row_with_spaces (window_row);
2579 /* If only part of W's desired matrix has been built, and
2580 window_row wasn't displayed, use the corresponding current
2581 row instead. */
2582 if (window_matrix == w->desired_matrix
2583 && !window_row->enabled_p)
2585 window_row = w->current_matrix->rows + window_y;
2586 current_row_p = 1;
2589 if (current_row_p)
2591 /* Copy window row to frame row. */
2592 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2593 window_row->glyphs[0],
2594 window_matrix->matrix_w * sizeof (struct glyph));
2596 else
2598 xassert (window_row->enabled_p);
2600 /* Only when a desired row has been displayed, we want
2601 the corresponding frame row to be updated. */
2602 frame_row->enabled_p = 1;
2604 /* Maybe insert a vertical border between horizontally adjacent
2605 windows. */
2606 if (GLYPH_CHAR (right_border_glyph) != 0)
2608 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2609 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2612 #if GLYPH_DEBUG
2613 /* Window row window_y must be a slice of frame row
2614 frame_y. */
2615 xassert (glyph_row_slice_p (window_row, frame_row));
2617 /* If rows are in sync, we don't have to copy glyphs because
2618 frame and window share glyphs. */
2620 strcpy (w->current_matrix->method, w->desired_matrix->method);
2621 add_window_display_history (w, w->current_matrix->method, 0);
2622 #endif
2625 /* Set number of used glyphs in the frame matrix. Since we fill
2626 up with spaces, and visit leaf windows from left to right it
2627 can be done simply. */
2628 frame_row->used[TEXT_AREA]
2629 = window_matrix->matrix_x + window_matrix->matrix_w;
2631 /* Next row. */
2632 ++window_y;
2633 ++frame_y;
2637 /* Given a user-specified glyph, possibly including a Lisp-level face
2638 ID, return a glyph that has a realized face ID.
2639 This is used for glyphs displayed specially and not part of the text;
2640 for instance, vertical separators, truncation markers, etc. */
2642 void
2643 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2645 int lface_id = GLYPH_FACE (*glyph);
2646 /* Convert the glyph's specified face to a realized (cache) face. */
2647 if (lface_id > 0)
2649 int face_id = merge_faces (XFRAME (w->frame),
2650 Qt, lface_id, DEFAULT_FACE_ID);
2651 SET_GLYPH_FACE (*glyph, face_id);
2655 /* Add spaces to a glyph row ROW in a window matrix.
2657 Each row has the form:
2659 +---------+-----------------------------+------------+
2660 | left | text | right |
2661 +---------+-----------------------------+------------+
2663 Left and right marginal areas are optional. This function adds
2664 spaces to areas so that there are no empty holes between areas.
2665 In other words: If the right area is not empty, the text area
2666 is filled up with spaces up to the right area. If the text area
2667 is not empty, the left area is filled up.
2669 To be called for frame-based redisplay, only. */
2671 static void
2672 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2674 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2675 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2676 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2680 /* Fill area AREA of glyph row ROW with spaces. To be called for
2681 frame-based redisplay only. */
2683 static void
2684 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2686 if (row->glyphs[area] < row->glyphs[area + 1])
2688 struct glyph *end = row->glyphs[area + 1];
2689 struct glyph *text = row->glyphs[area] + row->used[area];
2691 while (text < end)
2692 *text++ = space_glyph;
2693 row->used[area] = text - row->glyphs[area];
2698 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2699 reached. In frame matrices only one area, TEXT_AREA, is used. */
2701 static void
2702 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2704 int i = row->used[TEXT_AREA];
2705 struct glyph *glyph = row->glyphs[TEXT_AREA];
2707 while (i < upto)
2708 glyph[i++] = space_glyph;
2710 row->used[TEXT_AREA] = i;
2715 /**********************************************************************
2716 Mirroring operations on frame matrices in window matrices
2717 **********************************************************************/
2719 /* Set frame being updated via frame-based redisplay to F. This
2720 function must be called before updates to make explicit that we are
2721 working on frame matrices or not. */
2723 static inline void
2724 set_frame_matrix_frame (struct frame *f)
2726 frame_matrix_frame = f;
2730 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2731 DESIRED_MATRIX is the desired matrix corresponding to
2732 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2733 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2734 frame_matrix_frame is non-null, this indicates that the exchange is
2735 done in frame matrices, and that we have to perform analogous
2736 operations in window matrices of frame_matrix_frame. */
2738 static inline void
2739 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2741 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2742 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2743 int mouse_face_p = current_row->mouse_face_p;
2745 /* Do current_row = desired_row. This exchanges glyph pointers
2746 between both rows, and does a structure assignment otherwise. */
2747 assign_row (current_row, desired_row);
2749 /* Enable current_row to mark it as valid. */
2750 current_row->enabled_p = 1;
2751 current_row->mouse_face_p = mouse_face_p;
2753 /* If we are called on frame matrices, perform analogous operations
2754 for window matrices. */
2755 if (frame_matrix_frame)
2756 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2760 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2761 W's frame which has been made current (by swapping pointers between
2762 current and desired matrix). Perform analogous operations in the
2763 matrices of leaf windows in the window tree rooted at W. */
2765 static void
2766 mirror_make_current (struct window *w, int frame_row)
2768 while (w)
2770 if (!NILP (w->hchild))
2771 mirror_make_current (XWINDOW (w->hchild), frame_row);
2772 else if (!NILP (w->vchild))
2773 mirror_make_current (XWINDOW (w->vchild), frame_row);
2774 else
2776 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2777 here because the checks performed in debug mode there
2778 will not allow the conversion. */
2779 int row = frame_row - w->desired_matrix->matrix_y;
2781 /* If FRAME_ROW is within W, assign the desired row to the
2782 current row (exchanging glyph pointers). */
2783 if (row >= 0 && row < w->desired_matrix->matrix_h)
2785 struct glyph_row *current_row
2786 = MATRIX_ROW (w->current_matrix, row);
2787 struct glyph_row *desired_row
2788 = MATRIX_ROW (w->desired_matrix, row);
2790 if (desired_row->enabled_p)
2791 assign_row (current_row, desired_row);
2792 else
2793 swap_glyph_pointers (desired_row, current_row);
2794 current_row->enabled_p = 1;
2796 /* Set the Y coordinate of the mode/header line's row.
2797 It is needed in draw_row_with_mouse_face to find the
2798 screen coordinates. (Window-based redisplay sets
2799 this in update_window, but no one seems to do that
2800 for frame-based redisplay.) */
2801 if (current_row->mode_line_p)
2802 current_row->y = row;
2806 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2811 /* Perform row dance after scrolling. We are working on the range of
2812 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2813 including) in MATRIX. COPY_FROM is a vector containing, for each
2814 row I in the range 0 <= I < NLINES, the index of the original line
2815 to move to I. This index is relative to the row range, i.e. 0 <=
2816 index < NLINES. RETAINED_P is a vector containing zero for each
2817 row 0 <= I < NLINES which is empty.
2819 This function is called from do_scrolling and do_direct_scrolling. */
2821 void
2822 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2823 int *copy_from, char *retained_p)
2825 /* A copy of original rows. */
2826 struct glyph_row *old_rows;
2828 /* Rows to assign to. */
2829 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2831 int i;
2833 /* Make a copy of the original rows. */
2834 old_rows = (struct glyph_row *) alloca (nlines * sizeof *old_rows);
2835 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2837 /* Assign new rows, maybe clear lines. */
2838 for (i = 0; i < nlines; ++i)
2840 int enabled_before_p = new_rows[i].enabled_p;
2842 xassert (i + unchanged_at_top < matrix->nrows);
2843 xassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2844 new_rows[i] = old_rows[copy_from[i]];
2845 new_rows[i].enabled_p = enabled_before_p;
2847 /* RETAINED_P is zero for empty lines. */
2848 if (!retained_p[copy_from[i]])
2849 new_rows[i].enabled_p = 0;
2852 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2853 if (frame_matrix_frame)
2854 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2855 unchanged_at_top, nlines, copy_from, retained_p);
2859 /* Synchronize glyph pointers in the current matrix of window W with
2860 the current frame matrix. */
2862 static void
2863 sync_window_with_frame_matrix_rows (struct window *w)
2865 struct frame *f = XFRAME (w->frame);
2866 struct glyph_row *window_row, *window_row_end, *frame_row;
2867 int left, right, x, width;
2869 /* Preconditions: W must be a leaf window on a tty frame. */
2870 xassert (NILP (w->hchild) && NILP (w->vchild));
2871 xassert (!FRAME_WINDOW_P (f));
2873 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2874 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2875 x = w->current_matrix->matrix_x;
2876 width = w->current_matrix->matrix_w;
2878 window_row = w->current_matrix->rows;
2879 window_row_end = window_row + w->current_matrix->nrows;
2880 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2882 for (; window_row < window_row_end; ++window_row, ++frame_row)
2884 window_row->glyphs[LEFT_MARGIN_AREA]
2885 = frame_row->glyphs[0] + x;
2886 window_row->glyphs[TEXT_AREA]
2887 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2888 window_row->glyphs[LAST_AREA]
2889 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2890 window_row->glyphs[RIGHT_MARGIN_AREA]
2891 = window_row->glyphs[LAST_AREA] - right;
2896 /* Return the window in the window tree rooted in W containing frame
2897 row ROW. Value is null if none is found. */
2899 static struct window *
2900 frame_row_to_window (struct window *w, int row)
2902 struct window *found = NULL;
2904 while (w && !found)
2906 if (!NILP (w->hchild))
2907 found = frame_row_to_window (XWINDOW (w->hchild), row);
2908 else if (!NILP (w->vchild))
2909 found = frame_row_to_window (XWINDOW (w->vchild), row);
2910 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2911 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2912 found = w;
2914 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2917 return found;
2921 /* Perform a line dance in the window tree rooted at W, after
2922 scrolling a frame matrix in mirrored_line_dance.
2924 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2925 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2926 COPY_FROM is a vector containing, for each row I in the range 0 <=
2927 I < NLINES, the index of the original line to move to I. This
2928 index is relative to the row range, i.e. 0 <= index < NLINES.
2929 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2930 which is empty. */
2932 static void
2933 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2935 while (w)
2937 if (!NILP (w->hchild))
2938 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
2939 nlines, copy_from, retained_p);
2940 else if (!NILP (w->vchild))
2941 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
2942 nlines, copy_from, retained_p);
2943 else
2945 /* W is a leaf window, and we are working on its current
2946 matrix m. */
2947 struct glyph_matrix *m = w->current_matrix;
2948 int i, sync_p = 0;
2949 struct glyph_row *old_rows;
2951 /* Make a copy of the original rows of matrix m. */
2952 old_rows = (struct glyph_row *) alloca (m->nrows * sizeof *old_rows);
2953 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2955 for (i = 0; i < nlines; ++i)
2957 /* Frame relative line assigned to. */
2958 int frame_to = i + unchanged_at_top;
2960 /* Frame relative line assigned. */
2961 int frame_from = copy_from[i] + unchanged_at_top;
2963 /* Window relative line assigned to. */
2964 int window_to = frame_to - m->matrix_y;
2966 /* Window relative line assigned. */
2967 int window_from = frame_from - m->matrix_y;
2969 /* Is assigned line inside window? */
2970 int from_inside_window_p
2971 = window_from >= 0 && window_from < m->matrix_h;
2973 /* Is assigned to line inside window? */
2974 int to_inside_window_p
2975 = window_to >= 0 && window_to < m->matrix_h;
2977 if (from_inside_window_p && to_inside_window_p)
2979 /* Enabled setting before assignment. */
2980 int enabled_before_p;
2982 /* Do the assignment. The enabled_p flag is saved
2983 over the assignment because the old redisplay did
2984 that. */
2985 enabled_before_p = m->rows[window_to].enabled_p;
2986 m->rows[window_to] = old_rows[window_from];
2987 m->rows[window_to].enabled_p = enabled_before_p;
2989 /* If frame line is empty, window line is empty, too. */
2990 if (!retained_p[copy_from[i]])
2991 m->rows[window_to].enabled_p = 0;
2993 else if (to_inside_window_p)
2995 /* A copy between windows. This is an infrequent
2996 case not worth optimizing. */
2997 struct frame *f = XFRAME (w->frame);
2998 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2999 struct window *w2;
3000 struct glyph_matrix *m2;
3001 int m2_from;
3003 w2 = frame_row_to_window (root, frame_from);
3004 /* ttn@surf.glug.org: when enabling menu bar using `emacs
3005 -nw', FROM_FRAME sometimes has no associated window.
3006 This check avoids a segfault if W2 is null. */
3007 if (w2)
3009 m2 = w2->current_matrix;
3010 m2_from = frame_from - m2->matrix_y;
3011 copy_row_except_pointers (m->rows + window_to,
3012 m2->rows + m2_from);
3014 /* If frame line is empty, window line is empty, too. */
3015 if (!retained_p[copy_from[i]])
3016 m->rows[window_to].enabled_p = 0;
3018 sync_p = 1;
3020 else if (from_inside_window_p)
3021 sync_p = 1;
3024 /* If there was a copy between windows, make sure glyph
3025 pointers are in sync with the frame matrix. */
3026 if (sync_p)
3027 sync_window_with_frame_matrix_rows (w);
3029 /* Check that no pointers are lost. */
3030 CHECK_MATRIX (m);
3033 /* Next window on same level. */
3034 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3039 #if GLYPH_DEBUG
3041 /* Check that window and frame matrices agree about their
3042 understanding where glyphs of the rows are to find. For each
3043 window in the window tree rooted at W, check that rows in the
3044 matrices of leaf window agree with their frame matrices about
3045 glyph pointers. */
3047 static void
3048 check_window_matrix_pointers (struct window *w)
3050 while (w)
3052 if (!NILP (w->hchild))
3053 check_window_matrix_pointers (XWINDOW (w->hchild));
3054 else if (!NILP (w->vchild))
3055 check_window_matrix_pointers (XWINDOW (w->vchild));
3056 else
3058 struct frame *f = XFRAME (w->frame);
3059 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
3060 check_matrix_pointers (w->current_matrix, f->current_matrix);
3063 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3068 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
3069 a window and FRAME_MATRIX is the corresponding frame matrix. For
3070 each row in WINDOW_MATRIX check that it's a slice of the
3071 corresponding frame row. If it isn't, abort. */
3073 static void
3074 check_matrix_pointers (struct glyph_matrix *window_matrix,
3075 struct glyph_matrix *frame_matrix)
3077 /* Row number in WINDOW_MATRIX. */
3078 int i = 0;
3080 /* Row number corresponding to I in FRAME_MATRIX. */
3081 int j = window_matrix->matrix_y;
3083 /* For all rows check that the row in the window matrix is a
3084 slice of the row in the frame matrix. If it isn't we didn't
3085 mirror an operation on the frame matrix correctly. */
3086 while (i < window_matrix->nrows)
3088 if (!glyph_row_slice_p (window_matrix->rows + i,
3089 frame_matrix->rows + j))
3090 abort ();
3091 ++i, ++j;
3095 #endif /* GLYPH_DEBUG != 0 */
3099 /**********************************************************************
3100 VPOS and HPOS translations
3101 **********************************************************************/
3103 #if GLYPH_DEBUG
3105 /* Translate vertical position VPOS which is relative to window W to a
3106 vertical position relative to W's frame. */
3108 static int
3109 window_to_frame_vpos (struct window *w, int vpos)
3111 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3112 xassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
3113 vpos += WINDOW_TOP_EDGE_LINE (w);
3114 xassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
3115 return vpos;
3119 /* Translate horizontal position HPOS which is relative to window W to
3120 a horizontal position relative to W's frame. */
3122 static int
3123 window_to_frame_hpos (struct window *w, int hpos)
3125 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3126 hpos += WINDOW_LEFT_EDGE_COL (w);
3127 return hpos;
3130 #endif /* GLYPH_DEBUG */
3134 /**********************************************************************
3135 Redrawing Frames
3136 **********************************************************************/
3138 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
3139 doc: /* Clear frame FRAME and output again what is supposed to appear on it. */)
3140 (Lisp_Object frame)
3142 struct frame *f;
3144 CHECK_LIVE_FRAME (frame);
3145 f = XFRAME (frame);
3147 /* Ignore redraw requests, if frame has no glyphs yet.
3148 (Implementation note: It still has to be checked why we are
3149 called so early here). */
3150 if (!glyphs_initialized_initially_p)
3151 return Qnil;
3153 update_begin (f);
3154 #ifdef MSDOS
3155 if (FRAME_MSDOS_P (f))
3156 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3157 #endif
3158 clear_frame (f);
3159 clear_current_matrices (f);
3160 update_end (f);
3161 if (FRAME_TERMCAP_P (f))
3162 fflush (FRAME_TTY (f)->output);
3163 windows_or_buffers_changed++;
3164 /* Mark all windows as inaccurate, so that every window will have
3165 its redisplay done. */
3166 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3167 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3168 f->garbaged = 0;
3169 return Qnil;
3173 /* Redraw frame F. This is nothing more than a call to the Lisp
3174 function redraw-frame. */
3176 void
3177 redraw_frame (struct frame *f)
3179 Lisp_Object frame;
3180 XSETFRAME (frame, f);
3181 Fredraw_frame (frame);
3185 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3186 doc: /* Clear and redisplay all visible frames. */)
3187 (void)
3189 Lisp_Object tail, frame;
3191 FOR_EACH_FRAME (tail, frame)
3192 if (FRAME_VISIBLE_P (XFRAME (frame)))
3193 Fredraw_frame (frame);
3195 return Qnil;
3200 /***********************************************************************
3201 Frame Update
3202 ***********************************************************************/
3204 /* Update frame F based on the data in desired matrices.
3206 If FORCE_P is non-zero, don't let redisplay be stopped by detecting
3207 pending input. If INHIBIT_HAIRY_ID_P is non-zero, don't try
3208 scrolling.
3210 Value is non-zero if redisplay was stopped due to pending input. */
3213 update_frame (struct frame *f, int force_p, int inhibit_hairy_id_p)
3215 /* 1 means display has been paused because of pending input. */
3216 int paused_p;
3217 struct window *root_window = XWINDOW (f->root_window);
3219 if (redisplay_dont_pause)
3220 force_p = 1;
3221 #if PERIODIC_PREEMPTION_CHECKING
3222 else if (NILP (Vredisplay_preemption_period))
3223 force_p = 1;
3224 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3226 EMACS_TIME tm;
3227 double p = XFLOATINT (Vredisplay_preemption_period);
3228 int sec, usec;
3230 if (detect_input_pending_ignore_squeezables ())
3232 paused_p = 1;
3233 goto do_pause;
3236 sec = (int) p;
3237 usec = (p - sec) * 1000000;
3239 EMACS_GET_TIME (tm);
3240 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3241 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3243 #endif
3245 if (FRAME_WINDOW_P (f))
3247 /* We are working on window matrix basis. All windows whose
3248 flag must_be_updated_p is set have to be updated. */
3250 /* Record that we are not working on frame matrices. */
3251 set_frame_matrix_frame (NULL);
3253 /* Update all windows in the window tree of F, maybe stopping
3254 when pending input is detected. */
3255 update_begin (f);
3257 /* Update the menu bar on X frames that don't have toolkit
3258 support. */
3259 if (WINDOWP (f->menu_bar_window))
3260 update_window (XWINDOW (f->menu_bar_window), 1);
3262 /* Update the tool-bar window, if present. */
3263 if (WINDOWP (f->tool_bar_window))
3265 struct window *w = XWINDOW (f->tool_bar_window);
3267 /* Update tool-bar window. */
3268 if (w->must_be_updated_p)
3270 Lisp_Object tem;
3272 update_window (w, 1);
3273 w->must_be_updated_p = 0;
3275 /* Swap tool-bar strings. We swap because we want to
3276 reuse strings. */
3277 tem = f->current_tool_bar_string;
3278 f->current_tool_bar_string = f->desired_tool_bar_string;
3279 f->desired_tool_bar_string = tem;
3284 /* Update windows. */
3285 paused_p = update_window_tree (root_window, force_p);
3286 update_end (f);
3288 /* This flush is a performance bottleneck under X,
3289 and it doesn't seem to be necessary anyway (in general).
3290 It is necessary when resizing the window with the mouse, or
3291 at least the fringes are not redrawn in a timely manner. ++kfs */
3292 if (f->force_flush_display_p)
3294 FRAME_RIF (f)->flush_display (f);
3295 f->force_flush_display_p = 0;
3298 else
3300 /* We are working on frame matrix basis. Set the frame on whose
3301 frame matrix we operate. */
3302 set_frame_matrix_frame (f);
3304 /* Build F's desired matrix from window matrices. */
3305 build_frame_matrix (f);
3307 /* Update the display */
3308 update_begin (f);
3309 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3310 update_end (f);
3312 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3314 if (FRAME_TTY (f)->termscript)
3315 fflush (FRAME_TTY (f)->termscript);
3316 if (FRAME_TERMCAP_P (f))
3317 fflush (FRAME_TTY (f)->output);
3320 /* Check window matrices for lost pointers. */
3321 #if GLYPH_DEBUG
3322 check_window_matrix_pointers (root_window);
3323 add_frame_display_history (f, paused_p);
3324 #endif
3327 #if PERIODIC_PREEMPTION_CHECKING
3328 do_pause:
3329 #endif
3330 /* Reset flags indicating that a window should be updated. */
3331 set_window_update_flags (root_window, 0);
3333 display_completed = !paused_p;
3334 return paused_p;
3339 /************************************************************************
3340 Window-based updates
3341 ************************************************************************/
3343 /* Perform updates in window tree rooted at W. FORCE_P non-zero means
3344 don't stop updating when input is pending. */
3346 static int
3347 update_window_tree (struct window *w, int force_p)
3349 int paused_p = 0;
3351 while (w && !paused_p)
3353 if (!NILP (w->hchild))
3354 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3355 else if (!NILP (w->vchild))
3356 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3357 else if (w->must_be_updated_p)
3358 paused_p |= update_window (w, force_p);
3360 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3363 return paused_p;
3367 /* Update window W if its flag must_be_updated_p is non-zero. If
3368 FORCE_P is non-zero, don't stop updating if input is pending. */
3370 void
3371 update_single_window (struct window *w, int force_p)
3373 if (w->must_be_updated_p)
3375 struct frame *f = XFRAME (WINDOW_FRAME (w));
3377 /* Record that this is not a frame-based redisplay. */
3378 set_frame_matrix_frame (NULL);
3380 if (redisplay_dont_pause)
3381 force_p = 1;
3382 #if PERIODIC_PREEMPTION_CHECKING
3383 else if (NILP (Vredisplay_preemption_period))
3384 force_p = 1;
3385 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3387 EMACS_TIME tm;
3388 double p = XFLOATINT (Vredisplay_preemption_period);
3389 int sec, usec;
3391 sec = (int) p;
3392 usec = (p - sec) * 1000000;
3394 EMACS_GET_TIME (tm);
3395 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3396 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3398 #endif
3400 /* Update W. */
3401 update_begin (f);
3402 update_window (w, force_p);
3403 update_end (f);
3405 /* Reset flag in W. */
3406 w->must_be_updated_p = 0;
3410 #ifdef HAVE_WINDOW_SYSTEM
3412 /* Redraw lines from the current matrix of window W that are
3413 overlapped by other rows. YB is bottom-most y-position in W. */
3415 static void
3416 redraw_overlapped_rows (struct window *w, int yb)
3418 int i;
3419 struct frame *f = XFRAME (WINDOW_FRAME (w));
3421 /* If rows overlapping others have been changed, the rows being
3422 overlapped have to be redrawn. This won't draw lines that have
3423 already been drawn in update_window_line because overlapped_p in
3424 desired rows is 0, so after row assignment overlapped_p in
3425 current rows is 0. */
3426 for (i = 0; i < w->current_matrix->nrows; ++i)
3428 struct glyph_row *row = w->current_matrix->rows + i;
3430 if (!row->enabled_p)
3431 break;
3432 else if (row->mode_line_p)
3433 continue;
3435 if (row->overlapped_p)
3437 enum glyph_row_area area;
3439 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3441 updated_row = row;
3442 updated_area = area;
3443 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3444 area == TEXT_AREA ? row->x : 0);
3445 if (row->used[area])
3446 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3447 row->used[area]);
3448 FRAME_RIF (f)->clear_end_of_line (-1);
3451 row->overlapped_p = 0;
3454 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3455 break;
3460 /* Redraw lines from the current matrix of window W that overlap
3461 others. YB is bottom-most y-position in W. */
3463 static void
3464 redraw_overlapping_rows (struct window *w, int yb)
3466 int i, bottom_y;
3467 struct glyph_row *row;
3468 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3470 for (i = 0; i < w->current_matrix->nrows; ++i)
3472 row = w->current_matrix->rows + i;
3474 if (!row->enabled_p)
3475 break;
3476 else if (row->mode_line_p)
3477 continue;
3479 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3481 if (row->overlapping_p)
3483 int overlaps = 0;
3485 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3486 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3487 overlaps |= OVERLAPS_PRED;
3488 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3489 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3490 overlaps |= OVERLAPS_SUCC;
3492 if (overlaps)
3494 if (row->used[LEFT_MARGIN_AREA])
3495 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3497 if (row->used[TEXT_AREA])
3498 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3500 if (row->used[RIGHT_MARGIN_AREA])
3501 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3503 /* Record in neighbor rows that ROW overwrites part of
3504 their display. */
3505 if (overlaps & OVERLAPS_PRED)
3506 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3507 if (overlaps & OVERLAPS_SUCC)
3508 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3512 if (bottom_y >= yb)
3513 break;
3517 #endif /* HAVE_WINDOW_SYSTEM */
3520 #if defined GLYPH_DEBUG && 0
3522 /* Check that no row in the current matrix of window W is enabled
3523 which is below what's displayed in the window. */
3525 static void
3526 check_current_matrix_flags (struct window *w)
3528 int last_seen_p = 0;
3529 int i, yb = window_text_bottom_y (w);
3531 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3533 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3534 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3535 last_seen_p = 1;
3536 else if (last_seen_p && row->enabled_p)
3537 abort ();
3541 #endif /* GLYPH_DEBUG */
3544 /* Update display of window W. FORCE_P non-zero means that we should
3545 not stop when detecting pending input. */
3547 static int
3548 update_window (struct window *w, int force_p)
3550 struct glyph_matrix *desired_matrix = w->desired_matrix;
3551 int paused_p;
3552 #if !PERIODIC_PREEMPTION_CHECKING
3553 int preempt_count = baud_rate / 2400 + 1;
3554 #endif
3555 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3556 #if GLYPH_DEBUG
3557 /* Check that W's frame doesn't have glyph matrices. */
3558 xassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3559 #endif
3561 /* Check pending input the first time so that we can quickly return. */
3562 #if !PERIODIC_PREEMPTION_CHECKING
3563 if (!force_p)
3564 detect_input_pending_ignore_squeezables ();
3565 #endif
3567 /* If forced to complete the update, or if no input is pending, do
3568 the update. */
3569 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3571 struct glyph_row *row, *end;
3572 struct glyph_row *mode_line_row;
3573 struct glyph_row *header_line_row;
3574 int yb, changed_p = 0, mouse_face_overwritten_p = 0;
3575 #if ! PERIODIC_PREEMPTION_CHECKING
3576 int n_updated = 0;
3577 #endif
3579 rif->update_window_begin_hook (w);
3580 yb = window_text_bottom_y (w);
3582 /* If window has a header line, update it before everything else.
3583 Adjust y-positions of other rows by the header line height. */
3584 row = desired_matrix->rows;
3585 end = row + desired_matrix->nrows - 1;
3587 if (row->mode_line_p)
3589 header_line_row = row;
3590 ++row;
3592 else
3593 header_line_row = NULL;
3595 /* Update the mode line, if necessary. */
3596 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3597 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3599 mode_line_row->y = yb;
3600 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3601 desired_matrix),
3602 &mouse_face_overwritten_p);
3605 /* Find first enabled row. Optimizations in redisplay_internal
3606 may lead to an update with only one row enabled. There may
3607 be also completely empty matrices. */
3608 while (row < end && !row->enabled_p)
3609 ++row;
3611 /* Try reusing part of the display by copying. */
3612 if (row < end && !desired_matrix->no_scrolling_p)
3614 int rc = scrolling_window (w, header_line_row != NULL);
3615 if (rc < 0)
3617 /* All rows were found to be equal. */
3618 paused_p = 0;
3619 goto set_cursor;
3621 else if (rc > 0)
3623 /* We've scrolled the display. */
3624 force_p = 1;
3625 changed_p = 1;
3629 /* Update the rest of the lines. */
3630 for (; row < end && (force_p || !input_pending); ++row)
3631 if (row->enabled_p)
3633 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3634 int i;
3636 /* We'll have to play a little bit with when to
3637 detect_input_pending. If it's done too often,
3638 scrolling large windows with repeated scroll-up
3639 commands will too quickly pause redisplay. */
3640 #if PERIODIC_PREEMPTION_CHECKING
3641 if (!force_p)
3643 EMACS_TIME tm, dif;
3644 EMACS_GET_TIME (tm);
3645 EMACS_SUB_TIME (dif, preemption_next_check, tm);
3646 if (EMACS_TIME_NEG_P (dif))
3648 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3649 if (detect_input_pending_ignore_squeezables ())
3650 break;
3653 #else
3654 if (!force_p && ++n_updated % preempt_count == 0)
3655 detect_input_pending_ignore_squeezables ();
3656 #endif
3657 changed_p |= update_window_line (w, vpos,
3658 &mouse_face_overwritten_p);
3660 /* Mark all rows below the last visible one in the current
3661 matrix as invalid. This is necessary because of
3662 variable line heights. Consider the case of three
3663 successive redisplays, where the first displays 5
3664 lines, the second 3 lines, and the third 5 lines again.
3665 If the second redisplay wouldn't mark rows in the
3666 current matrix invalid, the third redisplay might be
3667 tempted to optimize redisplay based on lines displayed
3668 in the first redisplay. */
3669 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3670 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3671 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3674 /* Was display preempted? */
3675 paused_p = row < end;
3677 set_cursor:
3679 /* Update the header line after scrolling because a new header
3680 line would otherwise overwrite lines at the top of the window
3681 that can be scrolled. */
3682 if (header_line_row && header_line_row->enabled_p)
3684 header_line_row->y = 0;
3685 update_window_line (w, 0, &mouse_face_overwritten_p);
3688 /* Fix the appearance of overlapping/overlapped rows. */
3689 if (!paused_p && !w->pseudo_window_p)
3691 #ifdef HAVE_WINDOW_SYSTEM
3692 if (changed_p && rif->fix_overlapping_area)
3694 redraw_overlapped_rows (w, yb);
3695 redraw_overlapping_rows (w, yb);
3697 #endif
3699 /* Make cursor visible at cursor position of W. */
3700 set_window_cursor_after_update (w);
3702 #if 0 /* Check that current matrix invariants are satisfied. This is
3703 for debugging only. See the comment of check_matrix_invariants. */
3704 IF_DEBUG (check_matrix_invariants (w));
3705 #endif
3708 #if GLYPH_DEBUG
3709 /* Remember the redisplay method used to display the matrix. */
3710 strcpy (w->current_matrix->method, w->desired_matrix->method);
3711 #endif
3713 #ifdef HAVE_WINDOW_SYSTEM
3714 update_window_fringes (w, 0);
3715 #endif
3717 /* End the update of window W. Don't set the cursor if we
3718 paused updating the display because in this case,
3719 set_window_cursor_after_update hasn't been called, and
3720 output_cursor doesn't contain the cursor location. */
3721 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3723 else
3724 paused_p = 1;
3726 #if GLYPH_DEBUG
3727 /* check_current_matrix_flags (w); */
3728 add_window_display_history (w, w->current_matrix->method, paused_p);
3729 #endif
3731 clear_glyph_matrix (desired_matrix);
3733 return paused_p;
3737 /* Update the display of area AREA in window W, row number VPOS.
3738 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3740 static void
3741 update_marginal_area (struct window *w, int area, int vpos)
3743 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3744 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3746 /* Let functions in xterm.c know what area subsequent X positions
3747 will be relative to. */
3748 updated_area = area;
3750 /* Set cursor to start of glyphs, write them, and clear to the end
3751 of the area. I don't think that something more sophisticated is
3752 necessary here, since marginal areas will not be the default. */
3753 rif->cursor_to (vpos, 0, desired_row->y, 0);
3754 if (desired_row->used[area])
3755 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3756 rif->clear_end_of_line (-1);
3760 /* Update the display of the text area of row VPOS in window W.
3761 Value is non-zero if display has changed. */
3763 static int
3764 update_text_area (struct window *w, int vpos)
3766 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3767 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3768 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3769 int changed_p = 0;
3771 /* Let functions in xterm.c know what area subsequent X positions
3772 will be relative to. */
3773 updated_area = TEXT_AREA;
3775 /* If rows are at different X or Y, or rows have different height,
3776 or the current row is marked invalid, write the entire line. */
3777 if (!current_row->enabled_p
3778 || desired_row->y != current_row->y
3779 || desired_row->ascent != current_row->ascent
3780 || desired_row->phys_ascent != current_row->phys_ascent
3781 || desired_row->phys_height != current_row->phys_height
3782 || desired_row->visible_height != current_row->visible_height
3783 || current_row->overlapped_p
3784 /* This next line is necessary for correctly redrawing
3785 mouse-face areas after scrolling and other operations.
3786 However, it causes excessive flickering when mouse is moved
3787 across the mode line. Luckily, turning it off for the mode
3788 line doesn't seem to hurt anything. -- cyd.
3789 But it is still needed for the header line. -- kfs. */
3790 || (current_row->mouse_face_p
3791 && !(current_row->mode_line_p && vpos > 0))
3792 || current_row->x != desired_row->x)
3794 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
3796 if (desired_row->used[TEXT_AREA])
3797 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3798 desired_row->used[TEXT_AREA]);
3800 /* Clear to end of window. */
3801 rif->clear_end_of_line (-1);
3802 changed_p = 1;
3804 /* This erases the cursor. We do this here because
3805 notice_overwritten_cursor cannot easily check this, which
3806 might indicate that the whole functionality of
3807 notice_overwritten_cursor would better be implemented here.
3808 On the other hand, we need notice_overwritten_cursor as long
3809 as mouse highlighting is done asynchronously outside of
3810 redisplay. */
3811 if (vpos == w->phys_cursor.vpos)
3812 w->phys_cursor_on_p = 0;
3814 else
3816 int stop, i, x;
3817 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3818 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3819 int overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3820 int desired_stop_pos = desired_row->used[TEXT_AREA];
3821 int abort_skipping = 0;
3823 /* If the desired row extends its face to the text area end, and
3824 unless the current row also does so at the same position,
3825 make sure we write at least one glyph, so that the face
3826 extension actually takes place. */
3827 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3828 && (desired_stop_pos < current_row->used[TEXT_AREA]
3829 || (desired_stop_pos == current_row->used[TEXT_AREA]
3830 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3831 --desired_stop_pos;
3833 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3834 i = 0;
3835 x = desired_row->x;
3837 /* Loop over glyphs that current and desired row may have
3838 in common. */
3839 while (i < stop)
3841 int can_skip_p = !abort_skipping;
3843 /* Skip over glyphs that both rows have in common. These
3844 don't have to be written. We can't skip if the last
3845 current glyph overlaps the glyph to its right. For
3846 example, consider a current row of `if ' with the `f' in
3847 Courier bold so that it overlaps the ` ' to its right.
3848 If the desired row is ` ', we would skip over the space
3849 after the `if' and there would remain a pixel from the
3850 `f' on the screen. */
3851 if (overlapping_glyphs_p && i > 0)
3853 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3854 int left, right;
3856 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3857 &left, &right);
3858 can_skip_p = (right == 0 && !abort_skipping);
3861 if (can_skip_p)
3863 int start_hpos = i;
3865 while (i < stop
3866 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3868 x += desired_glyph->pixel_width;
3869 ++desired_glyph, ++current_glyph, ++i;
3872 /* Consider the case that the current row contains "xxx
3873 ppp ggg" in italic Courier font, and the desired row
3874 is "xxx ggg". The character `p' has lbearing, `g'
3875 has not. The loop above will stop in front of the
3876 first `p' in the current row. If we would start
3877 writing glyphs there, we wouldn't erase the lbearing
3878 of the `p'. The rest of the lbearing problem is then
3879 taken care of by draw_glyphs. */
3880 if (overlapping_glyphs_p
3881 && i > 0
3882 && i < current_row->used[TEXT_AREA]
3883 && (current_row->used[TEXT_AREA]
3884 != desired_row->used[TEXT_AREA]))
3886 int left, right;
3888 rif->get_glyph_overhangs (current_glyph, XFRAME (w->frame),
3889 &left, &right);
3890 while (left > 0 && i > 0)
3892 --i, --desired_glyph, --current_glyph;
3893 x -= desired_glyph->pixel_width;
3894 left -= desired_glyph->pixel_width;
3897 /* Abort the skipping algorithm if we end up before
3898 our starting point, to avoid looping (bug#1070).
3899 This can happen when the lbearing is larger than
3900 the pixel width. */
3901 abort_skipping = (i < start_hpos);
3905 /* Try to avoid writing the entire rest of the desired row
3906 by looking for a resync point. This mainly prevents
3907 mode line flickering in the case the mode line is in
3908 fixed-pitch font, which it usually will be. */
3909 if (i < desired_row->used[TEXT_AREA])
3911 int start_x = x, start_hpos = i;
3912 struct glyph *start = desired_glyph;
3913 int current_x = x;
3914 int skip_first_p = !can_skip_p;
3916 /* Find the next glyph that's equal again. */
3917 while (i < stop
3918 && (skip_first_p
3919 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3920 && x == current_x)
3922 x += desired_glyph->pixel_width;
3923 current_x += current_glyph->pixel_width;
3924 ++desired_glyph, ++current_glyph, ++i;
3925 skip_first_p = 0;
3928 if (i == start_hpos || x != current_x)
3930 i = start_hpos;
3931 x = start_x;
3932 desired_glyph = start;
3933 break;
3936 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
3937 rif->write_glyphs (start, i - start_hpos);
3938 changed_p = 1;
3942 /* Write the rest. */
3943 if (i < desired_row->used[TEXT_AREA])
3945 rif->cursor_to (vpos, i, desired_row->y, x);
3946 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
3947 changed_p = 1;
3950 /* Maybe clear to end of line. */
3951 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3953 /* If new row extends to the end of the text area, nothing
3954 has to be cleared, if and only if we did a write_glyphs
3955 above. This is made sure by setting desired_stop_pos
3956 appropriately above. */
3957 xassert (i < desired_row->used[TEXT_AREA]
3958 || ((desired_row->used[TEXT_AREA]
3959 == current_row->used[TEXT_AREA])
3960 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3962 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3964 /* If old row extends to the end of the text area, clear. */
3965 if (i >= desired_row->used[TEXT_AREA])
3966 rif->cursor_to (vpos, i, desired_row->y,
3967 desired_row->pixel_width);
3968 rif->clear_end_of_line (-1);
3969 changed_p = 1;
3971 else if (desired_row->pixel_width < current_row->pixel_width)
3973 /* Otherwise clear to the end of the old row. Everything
3974 after that position should be clear already. */
3975 int xlim;
3977 if (i >= desired_row->used[TEXT_AREA])
3978 rif->cursor_to (vpos, i, desired_row->y,
3979 desired_row->pixel_width);
3981 /* If cursor is displayed at the end of the line, make sure
3982 it's cleared. Nowadays we don't have a phys_cursor_glyph
3983 with which to erase the cursor (because this method
3984 doesn't work with lbearing/rbearing), so we must do it
3985 this way. */
3986 if (vpos == w->phys_cursor.vpos
3987 && (desired_row->reversed_p
3988 ? (w->phys_cursor.hpos < 0)
3989 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3991 w->phys_cursor_on_p = 0;
3992 xlim = -1;
3994 else
3995 xlim = current_row->pixel_width;
3996 rif->clear_end_of_line (xlim);
3997 changed_p = 1;
4001 return changed_p;
4005 /* Update row VPOS in window W. Value is non-zero if display has been
4006 changed. */
4008 static int
4009 update_window_line (struct window *w, int vpos, int *mouse_face_overwritten_p)
4011 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
4012 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
4013 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4014 int changed_p = 0;
4016 /* Set the row being updated. This is important to let xterm.c
4017 know what line height values are in effect. */
4018 updated_row = desired_row;
4020 /* A row can be completely invisible in case a desired matrix was
4021 built with a vscroll and then make_cursor_line_fully_visible shifts
4022 the matrix. Make sure to make such rows current anyway, since
4023 we need the correct y-position, for example, in the current matrix. */
4024 if (desired_row->mode_line_p
4025 || desired_row->visible_height > 0)
4027 xassert (desired_row->enabled_p);
4029 /* Update display of the left margin area, if there is one. */
4030 if (!desired_row->full_width_p
4031 && !NILP (w->left_margin_cols))
4033 changed_p = 1;
4034 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
4037 /* Update the display of the text area. */
4038 if (update_text_area (w, vpos))
4040 changed_p = 1;
4041 if (current_row->mouse_face_p)
4042 *mouse_face_overwritten_p = 1;
4045 /* Update display of the right margin area, if there is one. */
4046 if (!desired_row->full_width_p
4047 && !NILP (w->right_margin_cols))
4049 changed_p = 1;
4050 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
4053 /* Draw truncation marks etc. */
4054 if (!current_row->enabled_p
4055 || desired_row->y != current_row->y
4056 || desired_row->visible_height != current_row->visible_height
4057 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
4058 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
4059 || current_row->redraw_fringe_bitmaps_p
4060 || desired_row->mode_line_p != current_row->mode_line_p
4061 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
4062 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
4063 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
4064 rif->after_update_window_line_hook (desired_row);
4067 /* Update current_row from desired_row. */
4068 make_current (w->desired_matrix, w->current_matrix, vpos);
4069 updated_row = NULL;
4070 return changed_p;
4074 /* Set the cursor after an update of window W. This function may only
4075 be called from update_window. */
4077 static void
4078 set_window_cursor_after_update (struct window *w)
4080 struct frame *f = XFRAME (w->frame);
4081 struct redisplay_interface *rif = FRAME_RIF (f);
4082 int cx, cy, vpos, hpos;
4084 /* Not intended for frame matrix updates. */
4085 xassert (FRAME_WINDOW_P (f));
4087 if (cursor_in_echo_area
4088 && !NILP (echo_area_buffer[0])
4089 /* If we are showing a message instead of the mini-buffer,
4090 show the cursor for the message instead. */
4091 && XWINDOW (minibuf_window) == w
4092 && EQ (minibuf_window, echo_area_window)
4093 /* These cases apply only to the frame that contains
4094 the active mini-buffer window. */
4095 && FRAME_HAS_MINIBUF_P (f)
4096 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4098 cx = cy = vpos = hpos = 0;
4100 if (cursor_in_echo_area >= 0)
4102 /* If the mini-buffer is several lines high, find the last
4103 line that has any text on it. Note: either all lines
4104 are enabled or none. Otherwise we wouldn't be able to
4105 determine Y. */
4106 struct glyph_row *row, *last_row;
4107 struct glyph *glyph;
4108 int yb = window_text_bottom_y (w);
4110 last_row = NULL;
4111 row = w->current_matrix->rows;
4112 while (row->enabled_p
4113 && (last_row == NULL
4114 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
4116 if (row->used[TEXT_AREA]
4117 && row->glyphs[TEXT_AREA][0].charpos >= 0)
4118 last_row = row;
4119 ++row;
4122 if (last_row)
4124 struct glyph *start = last_row->glyphs[TEXT_AREA];
4125 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
4127 while (last > start && last->charpos < 0)
4128 --last;
4130 for (glyph = start; glyph < last; ++glyph)
4132 cx += glyph->pixel_width;
4133 ++hpos;
4136 cy = last_row->y;
4137 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
4141 else
4143 cx = w->cursor.x;
4144 cy = w->cursor.y;
4145 hpos = w->cursor.hpos;
4146 vpos = w->cursor.vpos;
4149 /* Window cursor can be out of sync for horizontally split windows. */
4150 hpos = max (-1, hpos); /* -1 is for when cursor is on the left fringe */
4151 hpos = min (w->current_matrix->matrix_w - 1, hpos);
4152 vpos = max (0, vpos);
4153 vpos = min (w->current_matrix->nrows - 1, vpos);
4154 rif->cursor_to (vpos, hpos, cy, cx);
4158 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4159 tree rooted at W. */
4161 void
4162 set_window_update_flags (struct window *w, int on_p)
4164 while (w)
4166 if (!NILP (w->hchild))
4167 set_window_update_flags (XWINDOW (w->hchild), on_p);
4168 else if (!NILP (w->vchild))
4169 set_window_update_flags (XWINDOW (w->vchild), on_p);
4170 else
4171 w->must_be_updated_p = on_p;
4173 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4179 /***********************************************************************
4180 Window-Based Scrolling
4181 ***********************************************************************/
4183 /* Structure describing rows in scrolling_window. */
4185 struct row_entry
4187 /* Number of occurrences of this row in desired and current matrix. */
4188 int old_uses, new_uses;
4190 /* Vpos of row in new matrix. */
4191 int new_line_number;
4193 /* Bucket index of this row_entry in the hash table row_table. */
4194 ptrdiff_t bucket;
4196 /* The row described by this entry. */
4197 struct glyph_row *row;
4199 /* Hash collision chain. */
4200 struct row_entry *next;
4203 /* A pool to allocate row_entry structures from, and the size of the
4204 pool. The pool is reallocated in scrolling_window when we find
4205 that we need a larger one. */
4207 static struct row_entry *row_entry_pool;
4208 static ptrdiff_t row_entry_pool_size;
4210 /* Index of next free entry in row_entry_pool. */
4212 static ptrdiff_t row_entry_idx;
4214 /* The hash table used during scrolling, and the table's size. This
4215 table is used to quickly identify equal rows in the desired and
4216 current matrix. */
4218 static struct row_entry **row_table;
4219 static ptrdiff_t row_table_size;
4221 /* Vectors of pointers to row_entry structures belonging to the
4222 current and desired matrix, and the size of the vectors. */
4224 static struct row_entry **old_lines, **new_lines;
4225 static ptrdiff_t old_lines_size, new_lines_size;
4227 /* A pool to allocate run structures from, and its size. */
4229 static struct run *run_pool;
4230 static ptrdiff_t runs_size;
4232 /* A vector of runs of lines found during scrolling. */
4234 static struct run **runs;
4236 /* Add glyph row ROW to the scrolling hash table. */
4238 static inline struct row_entry *
4239 add_row_entry (struct glyph_row *row)
4241 struct row_entry *entry;
4242 ptrdiff_t i = row->hash % row_table_size;
4244 entry = row_table[i];
4245 xassert (entry || verify_row_hash (row));
4246 while (entry && !row_equal_p (entry->row, row, 1))
4247 entry = entry->next;
4249 if (entry == NULL)
4251 entry = row_entry_pool + row_entry_idx++;
4252 entry->row = row;
4253 entry->old_uses = entry->new_uses = 0;
4254 entry->new_line_number = 0;
4255 entry->bucket = i;
4256 entry->next = row_table[i];
4257 row_table[i] = entry;
4260 return entry;
4264 /* Try to reuse part of the current display of W by scrolling lines.
4265 HEADER_LINE_P non-zero means W has a header line.
4267 The algorithm is taken from Communications of the ACM, Apr78 "A
4268 Technique for Isolating Differences Between Files." It should take
4269 O(N) time.
4271 A short outline of the steps of the algorithm
4273 1. Skip lines equal at the start and end of both matrices.
4275 2. Enter rows in the current and desired matrix into a symbol
4276 table, counting how often they appear in both matrices.
4278 3. Rows that appear exactly once in both matrices serve as anchors,
4279 i.e. we assume that such lines are likely to have been moved.
4281 4. Starting from anchor lines, extend regions to be scrolled both
4282 forward and backward.
4284 Value is
4286 -1 if all rows were found to be equal.
4287 0 to indicate that we did not scroll the display, or
4288 1 if we did scroll. */
4290 static int
4291 scrolling_window (struct window *w, int header_line_p)
4293 struct glyph_matrix *desired_matrix = w->desired_matrix;
4294 struct glyph_matrix *current_matrix = w->current_matrix;
4295 int yb = window_text_bottom_y (w);
4296 ptrdiff_t i;
4297 int j, first_old, first_new, last_old, last_new;
4298 int nruns, run_idx;
4299 ptrdiff_t n;
4300 struct row_entry *entry;
4301 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4303 /* Skip over rows equal at the start. */
4304 for (i = header_line_p ? 1 : 0; i < current_matrix->nrows - 1; ++i)
4306 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4307 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4309 if (c->enabled_p
4310 && d->enabled_p
4311 && !d->redraw_fringe_bitmaps_p
4312 && c->y == d->y
4313 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4314 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4315 && row_equal_p (c, d, 1))
4317 assign_row (c, d);
4318 d->enabled_p = 0;
4320 else
4321 break;
4324 /* Give up if some rows in the desired matrix are not enabled. */
4325 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4326 return -1;
4328 first_old = first_new = i;
4330 /* Set last_new to the index + 1 of the row that reaches the
4331 bottom boundary in the desired matrix. Give up if we find a
4332 disabled row before we reach the bottom boundary. */
4333 i = first_new + 1;
4334 while (i < desired_matrix->nrows - 1)
4336 int bottom;
4338 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4339 return 0;
4340 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4341 if (bottom <= yb)
4342 ++i;
4343 if (bottom >= yb)
4344 break;
4347 last_new = i;
4349 /* Set last_old to the index + 1 of the row that reaches the bottom
4350 boundary in the current matrix. We don't look at the enabled
4351 flag here because we plan to reuse part of the display even if
4352 other parts are disabled. */
4353 i = first_old + 1;
4354 while (i < current_matrix->nrows - 1)
4356 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4357 if (bottom <= yb)
4358 ++i;
4359 if (bottom >= yb)
4360 break;
4363 last_old = i;
4365 /* Skip over rows equal at the bottom. */
4366 i = last_new;
4367 j = last_old;
4368 while (i - 1 > first_new
4369 && j - 1 > first_old
4370 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4371 && (MATRIX_ROW (current_matrix, j - 1)->y
4372 == MATRIX_ROW (desired_matrix, i - 1)->y)
4373 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4374 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4375 MATRIX_ROW (current_matrix, j - 1), 1))
4376 --i, --j;
4377 last_new = i;
4378 last_old = j;
4380 /* Nothing to do if all rows are equal. */
4381 if (last_new == first_new)
4382 return 0;
4384 /* Check for integer overflow in size calculation.
4386 If next_almost_prime checks (N) for divisibility by 2..10, then
4387 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4388 So, set next_almost_prime_increment_max to 10.
4390 It's just a coincidence that next_almost_prime_increment_max ==
4391 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4392 13, then next_almost_prime_increment_max would be 14, e.g.,
4393 because next_almost_prime (113) would be 127. */
4395 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4396 enum { next_almost_prime_increment_max = 10 };
4397 ptrdiff_t row_table_max =
4398 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4399 - next_almost_prime_increment_max);
4400 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4401 if (current_nrows_max < current_matrix->nrows)
4402 memory_full (SIZE_MAX);
4405 /* Reallocate vectors, tables etc. if necessary. */
4407 if (current_matrix->nrows > old_lines_size)
4408 old_lines = xpalloc (old_lines, &old_lines_size,
4409 current_matrix->nrows - old_lines_size,
4410 INT_MAX, sizeof *old_lines);
4412 if (desired_matrix->nrows > new_lines_size)
4413 new_lines = xpalloc (new_lines, &new_lines_size,
4414 desired_matrix->nrows - new_lines_size,
4415 INT_MAX, sizeof *new_lines);
4417 n = desired_matrix->nrows;
4418 n += current_matrix->nrows;
4419 if (row_table_size < 3 * n)
4421 ptrdiff_t size = next_almost_prime (3 * n);
4422 row_table = xnrealloc (row_table, size, sizeof *row_table);
4423 row_table_size = size;
4424 memset (row_table, 0, size * sizeof *row_table);
4427 if (n > row_entry_pool_size)
4428 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4429 n - row_entry_pool_size,
4430 -1, sizeof *row_entry_pool);
4432 if (desired_matrix->nrows > runs_size)
4434 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4435 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4436 runs_size = desired_matrix->nrows;
4439 nruns = run_idx = 0;
4440 row_entry_idx = 0;
4442 /* Add rows from the current and desired matrix to the hash table
4443 row_hash_table to be able to find equal ones quickly. */
4445 for (i = first_old; i < last_old; ++i)
4447 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4449 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4450 old_lines[i] = entry;
4451 ++entry->old_uses;
4453 else
4454 old_lines[i] = NULL;
4457 for (i = first_new; i < last_new; ++i)
4459 xassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4460 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4461 ++entry->new_uses;
4462 entry->new_line_number = i;
4463 new_lines[i] = entry;
4466 /* Identify moves based on lines that are unique and equal
4467 in both matrices. */
4468 for (i = first_old; i < last_old;)
4469 if (old_lines[i]
4470 && old_lines[i]->old_uses == 1
4471 && old_lines[i]->new_uses == 1)
4473 int p, q;
4474 int new_line = old_lines[i]->new_line_number;
4475 struct run *run = run_pool + run_idx++;
4477 /* Record move. */
4478 run->current_vpos = i;
4479 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4480 run->desired_vpos = new_line;
4481 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4482 run->nrows = 1;
4483 run->height = MATRIX_ROW (current_matrix, i)->height;
4485 /* Extend backward. */
4486 p = i - 1;
4487 q = new_line - 1;
4488 while (p > first_old
4489 && q > first_new
4490 && old_lines[p] == new_lines[q])
4492 int h = MATRIX_ROW (current_matrix, p)->height;
4493 --run->current_vpos;
4494 --run->desired_vpos;
4495 ++run->nrows;
4496 run->height += h;
4497 run->desired_y -= h;
4498 run->current_y -= h;
4499 --p, --q;
4502 /* Extend forward. */
4503 p = i + 1;
4504 q = new_line + 1;
4505 while (p < last_old
4506 && q < last_new
4507 && old_lines[p] == new_lines[q])
4509 int h = MATRIX_ROW (current_matrix, p)->height;
4510 ++run->nrows;
4511 run->height += h;
4512 ++p, ++q;
4515 /* Insert run into list of all runs. Order runs by copied
4516 pixel lines. Note that we record runs that don't have to
4517 be copied because they are already in place. This is done
4518 because we can avoid calling update_window_line in this
4519 case. */
4520 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4522 for (q = nruns; q > p; --q)
4523 runs[q] = runs[q - 1];
4524 runs[p] = run;
4525 ++nruns;
4527 i += run->nrows;
4529 else
4530 ++i;
4532 /* Do the moves. Do it in a way that we don't overwrite something
4533 we want to copy later on. This is not solvable in general
4534 because there is only one display and we don't have a way to
4535 exchange areas on this display. Example:
4537 +-----------+ +-----------+
4538 | A | | B |
4539 +-----------+ --> +-----------+
4540 | B | | A |
4541 +-----------+ +-----------+
4543 Instead, prefer bigger moves, and invalidate moves that would
4544 copy from where we copied to. */
4546 for (i = 0; i < nruns; ++i)
4547 if (runs[i]->nrows > 0)
4549 struct run *r = runs[i];
4551 /* Copy on the display. */
4552 if (r->current_y != r->desired_y)
4554 rif->clear_window_mouse_face (w);
4555 rif->scroll_run_hook (w, r);
4557 /* Invalidate runs that copy from where we copied to. */
4558 for (j = i + 1; j < nruns; ++j)
4560 struct run *p = runs[j];
4562 if ((p->current_y >= r->desired_y
4563 && p->current_y < r->desired_y + r->height)
4564 || (p->current_y + p->height >= r->desired_y
4565 && (p->current_y + p->height
4566 < r->desired_y + r->height)))
4567 p->nrows = 0;
4571 /* Assign matrix rows. */
4572 for (j = 0; j < r->nrows; ++j)
4574 struct glyph_row *from, *to;
4575 int to_overlapped_p;
4577 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4578 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4579 to_overlapped_p = to->overlapped_p;
4580 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4581 assign_row (to, from);
4582 to->enabled_p = 1, from->enabled_p = 0;
4583 to->overlapped_p = to_overlapped_p;
4587 /* Clear the hash table, for the next time. */
4588 for (i = 0; i < row_entry_idx; ++i)
4589 row_table[row_entry_pool[i].bucket] = NULL;
4591 /* Value is 1 to indicate that we scrolled the display. */
4592 return 0 < nruns;
4597 /************************************************************************
4598 Frame-Based Updates
4599 ************************************************************************/
4601 /* Update the desired frame matrix of frame F.
4603 FORCE_P non-zero means that the update should not be stopped by
4604 pending input. INHIBIT_HAIRY_ID_P non-zero means that scrolling
4605 should not be tried.
4607 Value is non-zero if update was stopped due to pending input. */
4609 static int
4610 update_frame_1 (struct frame *f, int force_p, int inhibit_id_p)
4612 /* Frame matrices to work on. */
4613 struct glyph_matrix *current_matrix = f->current_matrix;
4614 struct glyph_matrix *desired_matrix = f->desired_matrix;
4615 int i;
4616 int pause_p;
4617 int preempt_count = baud_rate / 2400 + 1;
4619 xassert (current_matrix && desired_matrix);
4621 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4622 calculate_costs (f);
4624 if (preempt_count <= 0)
4625 preempt_count = 1;
4627 #if !PERIODIC_PREEMPTION_CHECKING
4628 if (!force_p && detect_input_pending_ignore_squeezables ())
4630 pause_p = 1;
4631 goto do_pause;
4633 #endif
4635 /* If we cannot insert/delete lines, it's no use trying it. */
4636 if (!FRAME_LINE_INS_DEL_OK (f))
4637 inhibit_id_p = 1;
4639 /* See if any of the desired lines are enabled; don't compute for
4640 i/d line if just want cursor motion. */
4641 for (i = 0; i < desired_matrix->nrows; i++)
4642 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4643 break;
4645 /* Try doing i/d line, if not yet inhibited. */
4646 if (!inhibit_id_p && i < desired_matrix->nrows)
4647 force_p |= scrolling (f);
4649 /* Update the individual lines as needed. Do bottom line first. */
4650 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4651 update_frame_line (f, desired_matrix->nrows - 1);
4653 /* Now update the rest of the lines. */
4654 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4656 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4658 if (FRAME_TERMCAP_P (f))
4660 /* Flush out every so many lines.
4661 Also flush out if likely to have more than 1k buffered
4662 otherwise. I'm told that some telnet connections get
4663 really screwed by more than 1k output at once. */
4664 FILE *display_output = FRAME_TTY (f)->output;
4665 if (display_output)
4667 int outq = PENDING_OUTPUT_COUNT (display_output);
4668 if (outq > 900
4669 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4671 fflush (display_output);
4672 if (preempt_count == 1)
4674 #ifdef EMACS_OUTQSIZE
4675 if (EMACS_OUTQSIZE (0, &outq) < 0)
4676 /* Probably not a tty. Ignore the error and reset
4677 the outq count. */
4678 outq = PENDING_OUTPUT_COUNT (FRAME_TTY (f->output));
4679 #endif
4680 outq *= 10;
4681 if (baud_rate <= outq && baud_rate > 0)
4682 sleep (outq / baud_rate);
4688 #if PERIODIC_PREEMPTION_CHECKING
4689 if (!force_p)
4691 EMACS_TIME tm, dif;
4692 EMACS_GET_TIME (tm);
4693 EMACS_SUB_TIME (dif, preemption_next_check, tm);
4694 if (EMACS_TIME_NEG_P (dif))
4696 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
4697 if (detect_input_pending_ignore_squeezables ())
4698 break;
4701 #else
4702 if (!force_p && (i - 1) % preempt_count == 0)
4703 detect_input_pending_ignore_squeezables ();
4704 #endif
4706 update_frame_line (f, i);
4710 pause_p = (i < FRAME_LINES (f) - 1) ? i : 0;
4712 /* Now just clean up termcap drivers and set cursor, etc. */
4713 if (!pause_p)
4715 if ((cursor_in_echo_area
4716 /* If we are showing a message instead of the mini-buffer,
4717 show the cursor for the message instead of for the
4718 (now hidden) mini-buffer contents. */
4719 || (EQ (minibuf_window, selected_window)
4720 && EQ (minibuf_window, echo_area_window)
4721 && !NILP (echo_area_buffer[0])))
4722 /* These cases apply only to the frame that contains
4723 the active mini-buffer window. */
4724 && FRAME_HAS_MINIBUF_P (f)
4725 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4727 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4728 int row, col;
4730 if (cursor_in_echo_area < 0)
4732 /* Negative value of cursor_in_echo_area means put
4733 cursor at beginning of line. */
4734 row = top;
4735 col = 0;
4737 else
4739 /* Positive value of cursor_in_echo_area means put
4740 cursor at the end of the prompt. If the mini-buffer
4741 is several lines high, find the last line that has
4742 any text on it. */
4743 row = FRAME_LINES (f);
4746 --row;
4747 col = 0;
4749 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4751 /* Frame rows are filled up with spaces that
4752 must be ignored here. */
4753 struct glyph_row *r = MATRIX_ROW (current_matrix,
4754 row);
4755 struct glyph *start = r->glyphs[TEXT_AREA];
4756 struct glyph *last = start + r->used[TEXT_AREA];
4758 while (last > start
4759 && (last - 1)->charpos < 0)
4760 --last;
4762 col = last - start;
4765 while (row > top && col == 0);
4767 /* Make sure COL is not out of range. */
4768 if (col >= FRAME_CURSOR_X_LIMIT (f))
4770 /* If we have another row, advance cursor into it. */
4771 if (row < FRAME_LINES (f) - 1)
4773 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4774 row++;
4776 /* Otherwise move it back in range. */
4777 else
4778 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4782 cursor_to (f, row, col);
4784 else
4786 /* We have only one cursor on terminal frames. Use it to
4787 display the cursor of the selected window. */
4788 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4789 if (w->cursor.vpos >= 0
4790 /* The cursor vpos may be temporarily out of bounds
4791 in the following situation: There is one window,
4792 with the cursor in the lower half of it. The window
4793 is split, and a message causes a redisplay before
4794 a new cursor position has been computed. */
4795 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4797 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4798 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4800 if (INTEGERP (w->left_margin_cols))
4801 x += XFASTINT (w->left_margin_cols);
4803 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4804 cursor_to (f, y, x);
4809 #if !PERIODIC_PREEMPTION_CHECKING
4810 do_pause:
4811 #endif
4813 clear_desired_matrices (f);
4814 return pause_p;
4818 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4820 static int
4821 scrolling (struct frame *frame)
4823 int unchanged_at_top, unchanged_at_bottom;
4824 int window_size;
4825 int changed_lines;
4826 int *old_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4827 int *new_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4828 int *draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4829 int *old_draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4830 register int i;
4831 int free_at_end_vpos = FRAME_LINES (frame);
4832 struct glyph_matrix *current_matrix = frame->current_matrix;
4833 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4835 if (!current_matrix)
4836 abort ();
4838 /* Compute hash codes of all the lines. Also calculate number of
4839 changed lines, number of unchanged lines at the beginning, and
4840 number of unchanged lines at the end. */
4841 changed_lines = 0;
4842 unchanged_at_top = 0;
4843 unchanged_at_bottom = FRAME_LINES (frame);
4844 for (i = 0; i < FRAME_LINES (frame); i++)
4846 /* Give up on this scrolling if some old lines are not enabled. */
4847 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4848 return 0;
4849 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4850 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4852 /* This line cannot be redrawn, so don't let scrolling mess it. */
4853 new_hash[i] = old_hash[i];
4854 #define INFINITY 1000000 /* Taken from scroll.c */
4855 draw_cost[i] = INFINITY;
4857 else
4859 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4860 draw_cost[i] = line_draw_cost (desired_matrix, i);
4863 if (old_hash[i] != new_hash[i])
4865 changed_lines++;
4866 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4868 else if (i == unchanged_at_top)
4869 unchanged_at_top++;
4870 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4873 /* If changed lines are few, don't allow preemption, don't scroll. */
4874 if ((!FRAME_SCROLL_REGION_OK (frame)
4875 && changed_lines < baud_rate / 2400)
4876 || unchanged_at_bottom == FRAME_LINES (frame))
4877 return 1;
4879 window_size = (FRAME_LINES (frame) - unchanged_at_top
4880 - unchanged_at_bottom);
4882 if (FRAME_SCROLL_REGION_OK (frame))
4883 free_at_end_vpos -= unchanged_at_bottom;
4884 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4885 free_at_end_vpos = -1;
4887 /* If large window, fast terminal and few lines in common between
4888 current frame and desired frame, don't bother with i/d calc. */
4889 if (!FRAME_SCROLL_REGION_OK (frame)
4890 && window_size >= 18 && baud_rate > 2400
4891 && (window_size >=
4892 10 * scrolling_max_lines_saved (unchanged_at_top,
4893 FRAME_LINES (frame) - unchanged_at_bottom,
4894 old_hash, new_hash, draw_cost)))
4895 return 0;
4897 if (window_size < 2)
4898 return 0;
4900 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4901 draw_cost + unchanged_at_top - 1,
4902 old_draw_cost + unchanged_at_top - 1,
4903 old_hash + unchanged_at_top - 1,
4904 new_hash + unchanged_at_top - 1,
4905 free_at_end_vpos - unchanged_at_top);
4907 return 0;
4911 /* Count the number of blanks at the start of the vector of glyphs R
4912 which is LEN glyphs long. */
4914 static int
4915 count_blanks (struct glyph *r, int len)
4917 int i;
4919 for (i = 0; i < len; ++i)
4920 if (!CHAR_GLYPH_SPACE_P (r[i]))
4921 break;
4923 return i;
4927 /* Count the number of glyphs in common at the start of the glyph
4928 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4929 of STR2. Value is the number of equal glyphs equal at the start. */
4931 static int
4932 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4934 struct glyph *p1 = str1;
4935 struct glyph *p2 = str2;
4937 while (p1 < end1
4938 && p2 < end2
4939 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4940 ++p1, ++p2;
4942 return p1 - str1;
4946 /* Char insertion/deletion cost vector, from term.c */
4948 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4951 /* Perform a frame-based update on line VPOS in frame FRAME. */
4953 static void
4954 update_frame_line (struct frame *f, int vpos)
4956 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4957 int tem;
4958 int osp, nsp, begmatch, endmatch, olen, nlen;
4959 struct glyph_matrix *current_matrix = f->current_matrix;
4960 struct glyph_matrix *desired_matrix = f->desired_matrix;
4961 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4962 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4963 int must_write_whole_line_p;
4964 int write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4965 int colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4966 != FACE_TTY_DEFAULT_BG_COLOR);
4968 if (colored_spaces_p)
4969 write_spaces_p = 1;
4971 /* Current row not enabled means it has unknown contents. We must
4972 write the whole desired line in that case. */
4973 must_write_whole_line_p = !current_row->enabled_p;
4974 if (must_write_whole_line_p)
4976 obody = 0;
4977 olen = 0;
4979 else
4981 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4982 olen = current_row->used[TEXT_AREA];
4984 /* Ignore trailing spaces, if we can. */
4985 if (!write_spaces_p)
4986 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4987 olen--;
4990 current_row->enabled_p = 1;
4991 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4993 /* If desired line is empty, just clear the line. */
4994 if (!desired_row->enabled_p)
4996 nlen = 0;
4997 goto just_erase;
5000 nbody = desired_row->glyphs[TEXT_AREA];
5001 nlen = desired_row->used[TEXT_AREA];
5002 nend = nbody + nlen;
5004 /* If display line has unknown contents, write the whole line. */
5005 if (must_write_whole_line_p)
5007 /* Ignore spaces at the end, if we can. */
5008 if (!write_spaces_p)
5009 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5010 --nlen;
5012 /* Write the contents of the desired line. */
5013 if (nlen)
5015 cursor_to (f, vpos, 0);
5016 write_glyphs (f, nbody, nlen);
5019 /* Don't call clear_end_of_line if we already wrote the whole
5020 line. The cursor will not be at the right margin in that
5021 case but in the line below. */
5022 if (nlen < FRAME_TOTAL_COLS (f))
5024 cursor_to (f, vpos, nlen);
5025 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
5027 else
5028 /* Make sure we are in the right row, otherwise cursor movement
5029 with cmgoto might use `ch' in the wrong row. */
5030 cursor_to (f, vpos, 0);
5032 make_current (desired_matrix, current_matrix, vpos);
5033 return;
5036 /* Pretend trailing spaces are not there at all,
5037 unless for one reason or another we must write all spaces. */
5038 if (!write_spaces_p)
5039 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5040 nlen--;
5042 /* If there's no i/d char, quickly do the best we can without it. */
5043 if (!FRAME_CHAR_INS_DEL_OK (f))
5045 int i, j;
5047 /* Find the first glyph in desired row that doesn't agree with
5048 a glyph in the current row, and write the rest from there on. */
5049 for (i = 0; i < nlen; i++)
5051 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
5053 /* Find the end of the run of different glyphs. */
5054 j = i + 1;
5055 while (j < nlen
5056 && (j >= olen
5057 || !GLYPH_EQUAL_P (nbody + j, obody + j)
5058 || CHAR_GLYPH_PADDING_P (nbody[j])))
5059 ++j;
5061 /* Output this run of non-matching chars. */
5062 cursor_to (f, vpos, i);
5063 write_glyphs (f, nbody + i, j - i);
5064 i = j - 1;
5066 /* Now find the next non-match. */
5070 /* Clear the rest of the line, or the non-clear part of it. */
5071 if (olen > nlen)
5073 cursor_to (f, vpos, nlen);
5074 clear_end_of_line (f, olen);
5077 /* Make current row = desired row. */
5078 make_current (desired_matrix, current_matrix, vpos);
5079 return;
5082 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5083 characters in a row. */
5085 if (!olen)
5087 /* If current line is blank, skip over initial spaces, if
5088 possible, and write the rest. */
5089 if (write_spaces_p)
5090 nsp = 0;
5091 else
5092 nsp = count_blanks (nbody, nlen);
5094 if (nlen > nsp)
5096 cursor_to (f, vpos, nsp);
5097 write_glyphs (f, nbody + nsp, nlen - nsp);
5100 /* Exchange contents between current_frame and new_frame. */
5101 make_current (desired_matrix, current_matrix, vpos);
5102 return;
5105 /* Compute number of leading blanks in old and new contents. */
5106 osp = count_blanks (obody, olen);
5107 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
5109 /* Compute number of matching chars starting with first non-blank. */
5110 begmatch = count_match (obody + osp, obody + olen,
5111 nbody + nsp, nbody + nlen);
5113 /* Spaces in new match implicit space past the end of old. */
5114 /* A bug causing this to be a no-op was fixed in 18.29. */
5115 if (!write_spaces_p && osp + begmatch == olen)
5117 np1 = nbody + nsp;
5118 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
5119 ++begmatch;
5122 /* Avoid doing insert/delete char
5123 just cause number of leading spaces differs
5124 when the following text does not match. */
5125 if (begmatch == 0 && osp != nsp)
5126 osp = nsp = min (osp, nsp);
5128 /* Find matching characters at end of line */
5129 op1 = obody + olen;
5130 np1 = nbody + nlen;
5131 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5132 while (op1 > op2
5133 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
5135 op1--;
5136 np1--;
5138 endmatch = obody + olen - op1;
5140 /* tem gets the distance to insert or delete.
5141 endmatch is how many characters we save by doing so.
5142 Is it worth it? */
5144 tem = (nlen - nsp) - (olen - osp);
5145 if (endmatch && tem
5146 && (!FRAME_CHAR_INS_DEL_OK (f)
5147 || endmatch <= char_ins_del_cost (f)[tem]))
5148 endmatch = 0;
5150 /* nsp - osp is the distance to insert or delete.
5151 If that is nonzero, begmatch is known to be nonzero also.
5152 begmatch + endmatch is how much we save by doing the ins/del.
5153 Is it worth it? */
5155 if (nsp != osp
5156 && (!FRAME_CHAR_INS_DEL_OK (f)
5157 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
5159 begmatch = 0;
5160 endmatch = 0;
5161 osp = nsp = min (osp, nsp);
5164 /* Now go through the line, inserting, writing and
5165 deleting as appropriate. */
5167 if (osp > nsp)
5169 cursor_to (f, vpos, nsp);
5170 delete_glyphs (f, osp - nsp);
5172 else if (nsp > osp)
5174 /* If going to delete chars later in line
5175 and insert earlier in the line,
5176 must delete first to avoid losing data in the insert */
5177 if (endmatch && nlen < olen + nsp - osp)
5179 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5180 delete_glyphs (f, olen + nsp - osp - nlen);
5181 olen = nlen - (nsp - osp);
5183 cursor_to (f, vpos, osp);
5184 insert_glyphs (f, 0, nsp - osp);
5186 olen += nsp - osp;
5188 tem = nsp + begmatch + endmatch;
5189 if (nlen != tem || olen != tem)
5191 if (!endmatch || nlen == olen)
5193 /* If new text being written reaches right margin, there is
5194 no need to do clear-to-eol at the end of this function
5195 (and it would not be safe, since cursor is not going to
5196 be "at the margin" after the text is done). */
5197 if (nlen == FRAME_TOTAL_COLS (f))
5198 olen = 0;
5200 /* Function write_glyphs is prepared to do nothing
5201 if passed a length <= 0. Check it here to avoid
5202 unnecessary cursor movement. */
5203 if (nlen - tem > 0)
5205 cursor_to (f, vpos, nsp + begmatch);
5206 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5209 else if (nlen > olen)
5211 /* Here, we used to have the following simple code:
5212 ----------------------------------------
5213 write_glyphs (nbody + nsp + begmatch, olen - tem);
5214 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5215 ----------------------------------------
5216 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5217 is a padding glyph. */
5218 int out = olen - tem; /* Columns to be overwritten originally. */
5219 int del;
5221 cursor_to (f, vpos, nsp + begmatch);
5223 /* Calculate columns we can actually overwrite. */
5224 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5225 out--;
5226 write_glyphs (f, nbody + nsp + begmatch, out);
5228 /* If we left columns to be overwritten, we must delete them. */
5229 del = olen - tem - out;
5230 if (del > 0)
5231 delete_glyphs (f, del);
5233 /* At last, we insert columns not yet written out. */
5234 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5235 olen = nlen;
5237 else if (olen > nlen)
5239 cursor_to (f, vpos, nsp + begmatch);
5240 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5241 delete_glyphs (f, olen - nlen);
5242 olen = nlen;
5246 just_erase:
5247 /* If any unerased characters remain after the new line, erase them. */
5248 if (olen > nlen)
5250 cursor_to (f, vpos, nlen);
5251 clear_end_of_line (f, olen);
5254 /* Exchange contents between current_frame and new_frame. */
5255 make_current (desired_matrix, current_matrix, vpos);
5260 /***********************************************************************
5261 X/Y Position -> Buffer Position
5262 ***********************************************************************/
5264 /* Determine what's under window-relative pixel position (*X, *Y).
5265 Return the OBJECT (string or buffer) that's there.
5266 Return in *POS the position in that object.
5267 Adjust *X and *Y to character positions.
5268 Return in *DX and *DY the pixel coordinates of the click,
5269 relative to the top left corner of OBJECT, or relative to
5270 the top left corner of the character glyph at (*X, *Y)
5271 if OBJECT is nil.
5272 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5273 if the coordinates point to an empty area of the display. */
5275 Lisp_Object
5276 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)
5278 struct it it;
5279 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5280 struct text_pos startp;
5281 Lisp_Object string;
5282 struct glyph_row *row;
5283 #ifdef HAVE_WINDOW_SYSTEM
5284 struct image *img = 0;
5285 #endif
5286 int x0, x1, to_x;
5287 void *itdata = NULL;
5289 /* We used to set current_buffer directly here, but that does the
5290 wrong thing with `face-remapping-alist' (bug#2044). */
5291 Fset_buffer (w->buffer);
5292 itdata = bidi_shelve_cache ();
5293 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5294 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5295 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5296 start_display (&it, w, startp);
5297 /* start_display takes into account the header-line row, but IT's
5298 vpos still counts from the glyph row that includes the window's
5299 start position. Adjust for a possible header-line row. */
5300 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5302 x0 = *x;
5304 /* First, move to the beginning of the row corresponding to *Y. We
5305 need to be in that row to get the correct value of base paragraph
5306 direction for the text at (*X, *Y). */
5307 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5309 /* TO_X is the pixel position that the iterator will compute for the
5310 glyph at *X. We add it.first_visible_x because iterator
5311 positions include the hscroll. */
5312 to_x = x0 + it.first_visible_x;
5313 if (it.bidi_it.paragraph_dir == R2L)
5314 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5315 text area. This is because the iterator, even in R2L
5316 paragraphs, delivers glyphs as if they started at the left
5317 margin of the window. (When we actually produce glyphs for
5318 display, we reverse their order in PRODUCE_GLYPHS, but the
5319 iterator doesn't know about that.) The following line adjusts
5320 the pixel position to the iterator geometry, which is what
5321 move_it_* routines use. (The -1 is because in a window whose
5322 text-area width is W, the rightmost pixel position is W-1, and
5323 it should be mirrored into zero pixel position.) */
5324 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5326 /* Now move horizontally in the row to the glyph under *X. Second
5327 argument is ZV to prevent move_it_in_display_line from matching
5328 based on buffer positions. */
5329 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5330 bidi_unshelve_cache (itdata, 0);
5332 Fset_buffer (old_current_buffer);
5334 *dx = x0 + it.first_visible_x - it.current_x;
5335 *dy = *y - it.current_y;
5337 string = w->buffer;
5338 if (STRINGP (it.string))
5339 string = it.string;
5340 *pos = it.current;
5341 if (it.what == IT_COMPOSITION
5342 && it.cmp_it.nchars > 1
5343 && it.cmp_it.reversed_p)
5345 /* The current display element is a grapheme cluster in a
5346 composition. In that case, we need the position of the first
5347 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5348 it.current points to the last character of the cluster, thus
5349 we must move back to the first character of the same
5350 cluster. */
5351 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5352 if (STRINGP (it.string))
5353 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5354 else
5355 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->buffer),
5356 CHARPOS (pos->pos));
5359 #ifdef HAVE_WINDOW_SYSTEM
5360 if (it.what == IT_IMAGE)
5362 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5363 && !NILP (img->spec))
5364 *object = img->spec;
5366 #endif
5368 if (it.vpos < w->current_matrix->nrows
5369 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5370 row->enabled_p))
5372 if (it.hpos < row->used[TEXT_AREA])
5374 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5375 #ifdef HAVE_WINDOW_SYSTEM
5376 if (img)
5378 *dy -= row->ascent - glyph->ascent;
5379 *dx += glyph->slice.img.x;
5380 *dy += glyph->slice.img.y;
5381 /* Image slices positions are still relative to the entire image */
5382 *width = img->width;
5383 *height = img->height;
5385 else
5386 #endif
5388 *width = glyph->pixel_width;
5389 *height = glyph->ascent + glyph->descent;
5392 else
5394 *width = 0;
5395 *height = row->height;
5398 else
5400 *width = *height = 0;
5403 /* Add extra (default width) columns if clicked after EOL. */
5404 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5405 if (x0 > x1)
5406 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5408 *x = it.hpos;
5409 *y = it.vpos;
5411 return string;
5415 /* Value is the string under window-relative coordinates X/Y in the
5416 mode line or header line (PART says which) of window W, or nil if none.
5417 *CHARPOS is set to the position in the string returned. */
5419 Lisp_Object
5420 mode_line_string (struct window *w, enum window_part part,
5421 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5422 int *dx, int *dy, int *width, int *height)
5424 struct glyph_row *row;
5425 struct glyph *glyph, *end;
5426 int x0, y0;
5427 Lisp_Object string = Qnil;
5429 if (part == ON_MODE_LINE)
5430 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5431 else
5432 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5433 y0 = *y - row->y;
5434 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5436 if (row->mode_line_p && row->enabled_p)
5438 /* Find the glyph under X. If we find one with a string object,
5439 it's the one we were looking for. */
5440 glyph = row->glyphs[TEXT_AREA];
5441 end = glyph + row->used[TEXT_AREA];
5442 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5443 x0 -= glyph->pixel_width;
5444 *x = glyph - row->glyphs[TEXT_AREA];
5445 if (glyph < end)
5447 string = glyph->object;
5448 *charpos = glyph->charpos;
5449 *width = glyph->pixel_width;
5450 *height = glyph->ascent + glyph->descent;
5451 #ifdef HAVE_WINDOW_SYSTEM
5452 if (glyph->type == IMAGE_GLYPH)
5454 struct image *img;
5455 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5456 if (img != NULL)
5457 *object = img->spec;
5458 y0 -= row->ascent - glyph->ascent;
5460 #endif
5462 else
5464 /* Add extra (default width) columns if clicked after EOL. */
5465 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5466 *width = 0;
5467 *height = row->height;
5470 else
5472 *x = 0;
5473 x0 = 0;
5474 *width = *height = 0;
5477 *dx = x0;
5478 *dy = y0;
5480 return string;
5484 /* Value is the string under window-relative coordinates X/Y in either
5485 marginal area, or nil if none. *CHARPOS is set to the position in
5486 the string returned. */
5488 Lisp_Object
5489 marginal_area_string (struct window *w, enum window_part part,
5490 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5491 int *dx, int *dy, int *width, int *height)
5493 struct glyph_row *row = w->current_matrix->rows;
5494 struct glyph *glyph, *end;
5495 int x0, y0, i, wy = *y;
5496 int area;
5497 Lisp_Object string = Qnil;
5499 if (part == ON_LEFT_MARGIN)
5500 area = LEFT_MARGIN_AREA;
5501 else if (part == ON_RIGHT_MARGIN)
5502 area = RIGHT_MARGIN_AREA;
5503 else
5504 abort ();
5506 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5507 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5508 break;
5509 y0 = *y - row->y;
5510 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5512 if (row->enabled_p)
5514 /* Find the glyph under X. If we find one with a string object,
5515 it's the one we were looking for. */
5516 if (area == RIGHT_MARGIN_AREA)
5517 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5518 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5519 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5520 + window_box_width (w, LEFT_MARGIN_AREA)
5521 + window_box_width (w, TEXT_AREA));
5522 else
5523 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5524 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5525 : 0);
5527 glyph = row->glyphs[area];
5528 end = glyph + row->used[area];
5529 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5530 x0 -= glyph->pixel_width;
5531 *x = glyph - row->glyphs[area];
5532 if (glyph < end)
5534 string = glyph->object;
5535 *charpos = glyph->charpos;
5536 *width = glyph->pixel_width;
5537 *height = glyph->ascent + glyph->descent;
5538 #ifdef HAVE_WINDOW_SYSTEM
5539 if (glyph->type == IMAGE_GLYPH)
5541 struct image *img;
5542 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5543 if (img != NULL)
5544 *object = img->spec;
5545 y0 -= row->ascent - glyph->ascent;
5546 x0 += glyph->slice.img.x;
5547 y0 += glyph->slice.img.y;
5549 #endif
5551 else
5553 /* Add extra (default width) columns if clicked after EOL. */
5554 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5555 *width = 0;
5556 *height = row->height;
5559 else
5561 x0 = 0;
5562 *x = 0;
5563 *width = *height = 0;
5566 *dx = x0;
5567 *dy = y0;
5569 return string;
5573 /***********************************************************************
5574 Changing Frame Sizes
5575 ***********************************************************************/
5577 #ifdef SIGWINCH
5579 static void
5580 window_change_signal (int signalnum) /* If we don't have an argument, */
5581 /* some compilers complain in signal calls. */
5583 int width, height;
5584 int old_errno = errno;
5586 struct tty_display_info *tty;
5588 signal (SIGWINCH, window_change_signal);
5589 SIGNAL_THREAD_CHECK (signalnum);
5591 /* The frame size change obviously applies to a single
5592 termcap-controlled terminal, but we can't decide which.
5593 Therefore, we resize the frames corresponding to each tty.
5595 for (tty = tty_list; tty; tty = tty->next) {
5597 if (! tty->term_initted)
5598 continue;
5600 /* Suspended tty frames have tty->input == NULL avoid trying to
5601 use it. */
5602 if (!tty->input)
5603 continue;
5605 get_tty_size (fileno (tty->input), &width, &height);
5607 if (width > 5 && height > 2) {
5608 Lisp_Object tail, frame;
5610 FOR_EACH_FRAME (tail, frame)
5611 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5612 /* Record the new sizes, but don't reallocate the data
5613 structures now. Let that be done later outside of the
5614 signal handler. */
5615 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5619 errno = old_errno;
5621 #endif /* SIGWINCH */
5624 /* Do any change in frame size that was requested by a signal. SAFE
5625 non-zero means this function is called from a place where it is
5626 safe to change frame sizes while a redisplay is in progress. */
5628 void
5629 do_pending_window_change (int safe)
5631 /* If window_change_signal should have run before, run it now. */
5632 if (redisplaying_p && !safe)
5633 return;
5635 while (delayed_size_change)
5637 Lisp_Object tail, frame;
5639 delayed_size_change = 0;
5641 FOR_EACH_FRAME (tail, frame)
5643 struct frame *f = XFRAME (frame);
5645 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5646 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5647 0, 0, safe);
5653 /* Change the frame height and/or width. Values may be given as zero to
5654 indicate no change is to take place.
5656 If DELAY is non-zero, then assume we're being called from a signal
5657 handler, and queue the change for later - perhaps the next
5658 redisplay. Since this tries to resize windows, we can't call it
5659 from a signal handler.
5661 SAFE non-zero means this function is called from a place where it's
5662 safe to change frame sizes while a redisplay is in progress. */
5664 void
5665 change_frame_size (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5667 Lisp_Object tail, frame;
5669 if (FRAME_MSDOS_P (f))
5671 /* On MS-DOS, all frames use the same screen, so a change in
5672 size affects all frames. Termcap now supports multiple
5673 ttys. */
5674 FOR_EACH_FRAME (tail, frame)
5675 if (! FRAME_WINDOW_P (XFRAME (frame)))
5676 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5677 pretend, delay, safe);
5679 else
5680 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5683 static void
5684 change_frame_size_1 (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5686 int new_frame_total_cols;
5687 int count = SPECPDL_INDEX ();
5689 /* If we can't deal with the change now, queue it for later. */
5690 if (delay || (redisplaying_p && !safe))
5692 f->new_text_lines = newheight;
5693 f->new_text_cols = newwidth;
5694 delayed_size_change = 1;
5695 return;
5698 /* This size-change overrides any pending one for this frame. */
5699 f->new_text_lines = 0;
5700 f->new_text_cols = 0;
5702 /* If an argument is zero, set it to the current value. */
5703 if (newheight == 0)
5704 newheight = FRAME_LINES (f);
5705 if (newwidth == 0)
5706 newwidth = FRAME_COLS (f);
5708 /* Compute width of windows in F.
5709 This is the width of the frame without vertical scroll bars. */
5710 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5712 /* Round up to the smallest acceptable size. */
5713 check_frame_size (f, &newheight, &newwidth);
5715 /* If we're not changing the frame size, quit now. */
5716 /* Frame width may be unchanged but the text portion may change, for example,
5717 fullscreen and remove/add scroll bar. */
5718 if (newheight == FRAME_LINES (f)
5719 && newwidth == FRAME_COLS (f) // text portion unchanged
5720 && new_frame_total_cols == FRAME_TOTAL_COLS (f)) // frame width unchanged
5721 return;
5723 BLOCK_INPUT;
5725 #ifdef MSDOS
5726 /* We only can set screen dimensions to certain values supported
5727 by our video hardware. Try to find the smallest size greater
5728 or equal to the requested dimensions. */
5729 dos_set_window_size (&newheight, &newwidth);
5730 #endif
5732 if (newheight != FRAME_LINES (f))
5734 resize_frame_windows (f, newheight, 0);
5736 /* MSDOS frames cannot PRETEND, as they change frame size by
5737 manipulating video hardware. */
5738 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5739 FrameRows (FRAME_TTY (f)) = newheight;
5742 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5744 resize_frame_windows (f, new_frame_total_cols, 1);
5746 /* MSDOS frames cannot PRETEND, as they change frame size by
5747 manipulating video hardware. */
5748 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5749 FrameCols (FRAME_TTY (f)) = newwidth;
5751 if (WINDOWP (f->tool_bar_window))
5752 XSETFASTINT (XWINDOW (f->tool_bar_window)->total_cols, newwidth);
5755 FRAME_LINES (f) = newheight;
5756 SET_FRAME_COLS (f, newwidth);
5759 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5760 int text_area_x, text_area_y, text_area_width, text_area_height;
5762 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5763 &text_area_height);
5764 if (w->cursor.x >= text_area_x + text_area_width)
5765 w->cursor.hpos = w->cursor.x = 0;
5766 if (w->cursor.y >= text_area_y + text_area_height)
5767 w->cursor.vpos = w->cursor.y = 0;
5770 adjust_glyphs (f);
5771 calculate_costs (f);
5772 SET_FRAME_GARBAGED (f);
5773 f->resized_p = 1;
5775 UNBLOCK_INPUT;
5777 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5779 run_window_configuration_change_hook (f);
5781 unbind_to (count, Qnil);
5786 /***********************************************************************
5787 Terminal Related Lisp Functions
5788 ***********************************************************************/
5790 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5791 1, 1, "FOpen termscript file: ",
5792 doc: /* Start writing all terminal output to FILE as well as the terminal.
5793 FILE = nil means just close any termscript file currently open. */)
5794 (Lisp_Object file)
5796 struct tty_display_info *tty;
5798 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5799 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5800 error ("Current frame is not on a tty device");
5802 tty = CURTTY ();
5804 if (tty->termscript != 0)
5806 BLOCK_INPUT;
5807 fclose (tty->termscript);
5808 UNBLOCK_INPUT;
5810 tty->termscript = 0;
5812 if (! NILP (file))
5814 file = Fexpand_file_name (file, Qnil);
5815 tty->termscript = fopen (SSDATA (file), "w");
5816 if (tty->termscript == 0)
5817 report_file_error ("Opening termscript", Fcons (file, Qnil));
5819 return Qnil;
5823 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5824 Ssend_string_to_terminal, 1, 2, 0,
5825 doc: /* Send STRING to the terminal without alteration.
5826 Control characters in STRING will have terminal-dependent effects.
5828 Optional parameter TERMINAL specifies the tty terminal device to use.
5829 It may be a terminal object, a frame, or nil for the terminal used by
5830 the currently selected frame. In batch mode, STRING is sent to stdout
5831 when TERMINAL is nil. */)
5832 (Lisp_Object string, Lisp_Object terminal)
5834 struct terminal *t = get_terminal (terminal, 1);
5835 FILE *out;
5837 /* ??? Perhaps we should do something special for multibyte strings here. */
5838 CHECK_STRING (string);
5839 BLOCK_INPUT;
5841 if (!t)
5842 error ("Unknown terminal device");
5844 if (t->type == output_initial)
5845 out = stdout;
5846 else if (t->type != output_termcap && t->type != output_msdos_raw)
5847 error ("Device %d is not a termcap terminal device", t->id);
5848 else
5850 struct tty_display_info *tty = t->display_info.tty;
5852 if (! tty->output)
5853 error ("Terminal is currently suspended");
5855 if (tty->termscript)
5857 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5858 fflush (tty->termscript);
5860 out = tty->output;
5862 fwrite (SDATA (string), 1, SBYTES (string), out);
5863 fflush (out);
5864 UNBLOCK_INPUT;
5865 return Qnil;
5869 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5870 doc: /* Beep, or flash the screen.
5871 Also, unless an argument is given,
5872 terminate any keyboard macro currently executing. */)
5873 (Lisp_Object arg)
5875 if (!NILP (arg))
5877 if (noninteractive)
5878 putchar (07);
5879 else
5880 ring_bell (XFRAME (selected_frame));
5882 else
5883 bitch_at_user ();
5885 return Qnil;
5888 void
5889 bitch_at_user (void)
5891 if (noninteractive)
5892 putchar (07);
5893 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5894 error ("Keyboard macro terminated by a command ringing the bell");
5895 else
5896 ring_bell (XFRAME (selected_frame));
5901 /***********************************************************************
5902 Sleeping, Waiting
5903 ***********************************************************************/
5905 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5906 doc: /* Pause, without updating display, for SECONDS seconds.
5907 SECONDS may be a floating-point value, meaning that you can wait for a
5908 fraction of a second. Optional second arg MILLISECONDS specifies an
5909 additional wait period, in milliseconds; this may be useful if your
5910 Emacs was built without floating point support.
5911 \(Not all operating systems support waiting for a fraction of a second.) */)
5912 (Lisp_Object seconds, Lisp_Object milliseconds)
5914 int sec, usec;
5916 if (NILP (milliseconds))
5917 XSETINT (milliseconds, 0);
5918 else
5919 CHECK_NUMBER (milliseconds);
5920 usec = XINT (milliseconds) * 1000;
5923 double duration = extract_float (seconds);
5924 sec = (int) duration;
5925 usec += (duration - sec) * 1000000;
5928 #ifndef EMACS_HAS_USECS
5929 if (sec == 0 && usec != 0)
5930 error ("Millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
5931 #endif
5933 /* Assure that 0 <= usec < 1000000. */
5934 if (usec < 0)
5936 /* We can't rely on the rounding being correct if usec is negative. */
5937 if (-1000000 < usec)
5938 sec--, usec += 1000000;
5939 else
5940 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
5942 else
5943 sec += usec / 1000000, usec %= 1000000;
5945 if (sec < 0 || (sec == 0 && usec == 0))
5946 return Qnil;
5948 wait_reading_process_output (sec, usec, 0, 0, Qnil, NULL, 0);
5950 return Qnil;
5954 /* This is just like wait_reading_process_output, except that
5955 it does redisplay.
5957 TIMEOUT is number of seconds to wait (float or integer),
5958 or t to wait forever.
5959 READING is 1 if reading input.
5960 If DO_DISPLAY is >0 display process output while waiting.
5961 If DO_DISPLAY is >1 perform an initial redisplay before waiting.
5964 Lisp_Object
5965 sit_for (Lisp_Object timeout, int reading, int do_display)
5967 int sec, usec;
5969 swallow_events (do_display);
5971 if ((detect_input_pending_run_timers (do_display))
5972 || !NILP (Vexecuting_kbd_macro))
5973 return Qnil;
5975 if (do_display >= 2)
5976 redisplay_preserve_echo_area (2);
5978 if (INTEGERP (timeout))
5980 sec = XINT (timeout);
5981 usec = 0;
5983 else if (FLOATP (timeout))
5985 double seconds = XFLOAT_DATA (timeout);
5986 sec = (int) seconds;
5987 usec = (int) ((seconds - sec) * 1000000);
5989 else if (EQ (timeout, Qt))
5991 sec = 0;
5992 usec = 0;
5994 else
5995 wrong_type_argument (Qnumberp, timeout);
5997 if (sec == 0 && usec == 0 && !EQ (timeout, Qt))
5998 return Qt;
6000 #ifdef SIGIO
6001 gobble_input (0);
6002 #endif
6004 wait_reading_process_output (sec, usec, reading ? -1 : 1, do_display,
6005 Qnil, NULL, 0);
6007 return detect_input_pending () ? Qnil : Qt;
6011 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
6012 doc: /* Perform redisplay if no input is available.
6013 If optional arg FORCE is non-nil or `redisplay-dont-pause' is non-nil,
6014 perform a full redisplay even if input is available.
6015 Return t if redisplay was performed, nil otherwise. */)
6016 (Lisp_Object force)
6018 int count;
6020 swallow_events (1);
6021 if ((detect_input_pending_run_timers (1)
6022 && NILP (force) && !redisplay_dont_pause)
6023 || !NILP (Vexecuting_kbd_macro))
6024 return Qnil;
6026 count = SPECPDL_INDEX ();
6027 if (!NILP (force) && !redisplay_dont_pause)
6028 specbind (Qredisplay_dont_pause, Qt);
6029 redisplay_preserve_echo_area (2);
6030 unbind_to (count, Qnil);
6031 return Qt;
6036 /***********************************************************************
6037 Other Lisp Functions
6038 ***********************************************************************/
6040 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
6041 session's frames, frame names, buffers, buffer-read-only flags, and
6042 buffer-modified-flags. */
6044 static Lisp_Object frame_and_buffer_state;
6047 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
6048 Sframe_or_buffer_changed_p, 0, 1, 0,
6049 doc: /* Return non-nil if the frame and buffer state appears to have changed.
6050 VARIABLE is a variable name whose value is either nil or a state vector
6051 that will be updated to contain all frames and buffers,
6052 aside from buffers whose names start with space,
6053 along with the buffers' read-only and modified flags. This allows a fast
6054 check to see whether buffer menus might need to be recomputed.
6055 If this function returns non-nil, it updates the internal vector to reflect
6056 the current state.
6058 If VARIABLE is nil, an internal variable is used. Users should not
6059 pass nil for VARIABLE. */)
6060 (Lisp_Object variable)
6062 Lisp_Object state, tail, frame, buf;
6063 Lisp_Object *vecp, *end;
6064 int n;
6066 if (! NILP (variable))
6068 CHECK_SYMBOL (variable);
6069 state = Fsymbol_value (variable);
6070 if (! VECTORP (state))
6071 goto changed;
6073 else
6074 state = frame_and_buffer_state;
6076 vecp = XVECTOR (state)->contents;
6077 end = vecp + ASIZE (state);
6079 FOR_EACH_FRAME (tail, frame)
6081 if (vecp == end)
6082 goto changed;
6083 if (!EQ (*vecp++, frame))
6084 goto changed;
6085 if (vecp == end)
6086 goto changed;
6087 if (!EQ (*vecp++, XFRAME (frame)->name))
6088 goto changed;
6090 /* Check that the buffer info matches. */
6091 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6093 buf = XCDR (XCAR (tail));
6094 /* Ignore buffers that aren't included in buffer lists. */
6095 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6096 continue;
6097 if (vecp == end)
6098 goto changed;
6099 if (!EQ (*vecp++, buf))
6100 goto changed;
6101 if (vecp == end)
6102 goto changed;
6103 if (!EQ (*vecp++, BVAR (XBUFFER (buf), read_only)))
6104 goto changed;
6105 if (vecp == end)
6106 goto changed;
6107 if (!EQ (*vecp++, Fbuffer_modified_p (buf)))
6108 goto changed;
6110 if (vecp == end)
6111 goto changed;
6112 /* Detect deletion of a buffer at the end of the list. */
6113 if (EQ (*vecp, Qlambda))
6114 return Qnil;
6116 /* Come here if we decide the data has changed. */
6117 changed:
6118 /* Count the size we will need.
6119 Start with 1 so there is room for at least one lambda at the end. */
6120 n = 1;
6121 FOR_EACH_FRAME (tail, frame)
6122 n += 2;
6123 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6124 n += 3;
6125 /* Reallocate the vector if data has grown to need it,
6126 or if it has shrunk a lot. */
6127 if (! VECTORP (state)
6128 || n > ASIZE (state)
6129 || n + 20 < ASIZE (state) / 2)
6130 /* Add 20 extra so we grow it less often. */
6132 state = Fmake_vector (make_number (n + 20), Qlambda);
6133 if (! NILP (variable))
6134 Fset (variable, state);
6135 else
6136 frame_and_buffer_state = state;
6139 /* Record the new data in the (possibly reallocated) vector. */
6140 vecp = XVECTOR (state)->contents;
6141 FOR_EACH_FRAME (tail, frame)
6143 *vecp++ = frame;
6144 *vecp++ = XFRAME (frame)->name;
6146 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6148 buf = XCDR (XCAR (tail));
6149 /* Ignore buffers that aren't included in buffer lists. */
6150 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6151 continue;
6152 *vecp++ = buf;
6153 *vecp++ = BVAR (XBUFFER (buf), read_only);
6154 *vecp++ = Fbuffer_modified_p (buf);
6156 /* Fill up the vector with lambdas (always at least one). */
6157 *vecp++ = Qlambda;
6158 while (vecp - XVECTOR (state)->contents
6159 < ASIZE (state))
6160 *vecp++ = Qlambda;
6161 /* Make sure we didn't overflow the vector. */
6162 if (vecp - XVECTOR (state)->contents
6163 > ASIZE (state))
6164 abort ();
6165 return Qt;
6170 /***********************************************************************
6171 Initialization
6172 ***********************************************************************/
6174 /* Initialization done when Emacs fork is started, before doing stty.
6175 Determine terminal type and set terminal_driver. Then invoke its
6176 decoding routine to set up variables in the terminal package. */
6178 void
6179 init_display (void)
6181 char *terminal_type;
6183 /* Construct the space glyph. */
6184 space_glyph.type = CHAR_GLYPH;
6185 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
6186 space_glyph.charpos = -1;
6188 inverse_video = 0;
6189 cursor_in_echo_area = 0;
6190 terminal_type = (char *) 0;
6192 /* Now is the time to initialize this; it's used by init_sys_modes
6193 during startup. */
6194 Vinitial_window_system = Qnil;
6196 /* SIGWINCH needs to be handled no matter what display we start
6197 with. Otherwise newly opened tty frames will not resize
6198 automatically. */
6199 #ifdef SIGWINCH
6200 #ifndef CANNOT_DUMP
6201 if (initialized)
6202 #endif /* CANNOT_DUMP */
6203 signal (SIGWINCH, window_change_signal);
6204 #endif /* SIGWINCH */
6206 /* If running as a daemon, no need to initialize any frames/terminal. */
6207 if (IS_DAEMON)
6208 return;
6210 /* If the user wants to use a window system, we shouldn't bother
6211 initializing the terminal. This is especially important when the
6212 terminal is so dumb that emacs gives up before and doesn't bother
6213 using the window system.
6215 If the DISPLAY environment variable is set and nonempty,
6216 try to use X, and die with an error message if that doesn't work. */
6218 #ifdef HAVE_X_WINDOWS
6219 if (! inhibit_window_system && ! display_arg)
6221 char *display;
6222 display = getenv ("DISPLAY");
6223 display_arg = (display != 0 && *display != 0);
6225 if (display_arg && !x_display_ok (display))
6227 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6228 display);
6229 inhibit_window_system = 1;
6233 if (!inhibit_window_system && display_arg)
6235 Vinitial_window_system = Qx;
6236 #ifdef HAVE_X11
6237 Vwindow_system_version = make_number (11);
6238 #endif
6239 #if defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
6240 /* In some versions of ncurses,
6241 tputs crashes if we have not called tgetent.
6242 So call tgetent. */
6243 { char b[2044]; tgetent (b, "xterm");}
6244 #endif
6245 adjust_frame_glyphs_initially ();
6246 return;
6248 #endif /* HAVE_X_WINDOWS */
6250 #ifdef HAVE_NTGUI
6251 if (!inhibit_window_system)
6253 Vinitial_window_system = Qw32;
6254 Vwindow_system_version = make_number (1);
6255 adjust_frame_glyphs_initially ();
6256 return;
6258 #endif /* HAVE_NTGUI */
6260 #ifdef HAVE_NS
6261 if (!inhibit_window_system
6262 #ifndef CANNOT_DUMP
6263 && initialized
6264 #endif
6267 Vinitial_window_system = Qns;
6268 Vwindow_system_version = make_number (10);
6269 adjust_frame_glyphs_initially ();
6270 return;
6272 #endif
6274 /* If no window system has been specified, try to use the terminal. */
6275 if (! isatty (0))
6277 fatal ("standard input is not a tty");
6278 exit (1);
6281 #ifdef WINDOWSNT
6282 terminal_type = "w32console";
6283 #else
6284 /* Look at the TERM variable. */
6285 terminal_type = (char *) getenv ("TERM");
6286 #endif
6287 if (!terminal_type)
6289 #ifdef HAVE_WINDOW_SYSTEM
6290 if (! inhibit_window_system)
6291 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6292 else
6293 #endif /* HAVE_WINDOW_SYSTEM */
6294 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6295 exit (1);
6299 struct terminal *t;
6300 struct frame *f = XFRAME (selected_frame);
6302 /* Open a display on the controlling tty. */
6303 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6305 /* Convert the initial frame to use the new display. */
6306 if (f->output_method != output_initial)
6307 abort ();
6308 f->output_method = t->type;
6309 f->terminal = t;
6311 t->reference_count++;
6312 #ifdef MSDOS
6313 f->output_data.tty->display_info = &the_only_display_info;
6314 #else
6315 if (f->output_method == output_termcap)
6316 create_tty_output (f);
6317 #endif
6318 t->display_info.tty->top_frame = selected_frame;
6319 change_frame_size (XFRAME (selected_frame),
6320 FrameRows (t->display_info.tty),
6321 FrameCols (t->display_info.tty), 0, 0, 1);
6323 /* Delete the initial terminal. */
6324 if (--initial_terminal->reference_count == 0
6325 && initial_terminal->delete_terminal_hook)
6326 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6328 /* Update frame parameters to reflect the new type. */
6329 Fmodify_frame_parameters
6330 (selected_frame, Fcons (Fcons (Qtty_type,
6331 Ftty_type (selected_frame)), Qnil));
6332 if (t->display_info.tty->name)
6333 Fmodify_frame_parameters (selected_frame,
6334 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6335 Qnil));
6336 else
6337 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6338 Qnil));
6342 struct frame *sf = SELECTED_FRAME ();
6343 int width = FRAME_TOTAL_COLS (sf);
6344 int height = FRAME_LINES (sf);
6346 /* If these sizes are so big they cause overflow, just ignore the
6347 change. It's not clear what better we could do. The rest of
6348 the code assumes that (width + 2) * height * sizeof (struct glyph)
6349 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6350 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6351 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6352 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6353 < (width + 2) * height))
6354 fatal ("screen size %dx%d too big", width, height);
6357 adjust_frame_glyphs_initially ();
6358 calculate_costs (XFRAME (selected_frame));
6360 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6361 if (initialized
6362 && !noninteractive
6363 && NILP (Vinitial_window_system))
6365 /* For the initial frame, we don't have any way of knowing what
6366 are the foreground and background colors of the terminal. */
6367 struct frame *sf = SELECTED_FRAME ();
6369 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6370 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6371 call0 (intern ("tty-set-up-initial-frame-faces"));
6377 /***********************************************************************
6378 Blinking cursor
6379 ***********************************************************************/
6381 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6382 Sinternal_show_cursor, 2, 2, 0,
6383 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6384 WINDOW nil means use the selected window. SHOW non-nil means
6385 show a cursor in WINDOW in the next redisplay. SHOW nil means
6386 don't show a cursor. */)
6387 (Lisp_Object window, Lisp_Object show)
6389 /* Don't change cursor state while redisplaying. This could confuse
6390 output routines. */
6391 if (!redisplaying_p)
6393 if (NILP (window))
6394 window = selected_window;
6395 else
6396 CHECK_WINDOW (window);
6398 XWINDOW (window)->cursor_off_p = NILP (show);
6401 return Qnil;
6405 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6406 Sinternal_show_cursor_p, 0, 1, 0,
6407 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6408 WINDOW nil or omitted means report on the selected window. */)
6409 (Lisp_Object window)
6411 struct window *w;
6413 if (NILP (window))
6414 window = selected_window;
6415 else
6416 CHECK_WINDOW (window);
6418 w = XWINDOW (window);
6419 return w->cursor_off_p ? Qnil : Qt;
6422 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6423 Slast_nonminibuf_frame, 0, 0, 0,
6424 doc: /* Value is last nonminibuffer frame. */)
6425 (void)
6427 Lisp_Object frame = Qnil;
6429 if (last_nonminibuf_frame)
6430 XSETFRAME (frame, last_nonminibuf_frame);
6432 return frame;
6435 /***********************************************************************
6436 Initialization
6437 ***********************************************************************/
6439 void
6440 syms_of_display (void)
6442 defsubr (&Sredraw_frame);
6443 defsubr (&Sredraw_display);
6444 defsubr (&Sframe_or_buffer_changed_p);
6445 defsubr (&Sopen_termscript);
6446 defsubr (&Sding);
6447 defsubr (&Sredisplay);
6448 defsubr (&Ssleep_for);
6449 defsubr (&Ssend_string_to_terminal);
6450 defsubr (&Sinternal_show_cursor);
6451 defsubr (&Sinternal_show_cursor_p);
6452 defsubr (&Slast_nonminibuf_frame);
6454 #if GLYPH_DEBUG
6455 defsubr (&Sdump_redisplay_history);
6456 #endif
6458 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6459 staticpro (&frame_and_buffer_state);
6461 DEFSYM (Qdisplay_table, "display-table");
6462 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6464 DEFVAR_INT ("baud-rate", baud_rate,
6465 doc: /* *The output baud rate of the terminal.
6466 On most systems, changing this value will affect the amount of padding
6467 and the other strategic decisions made during redisplay. */);
6469 DEFVAR_BOOL ("inverse-video", inverse_video,
6470 doc: /* *Non-nil means invert the entire frame display.
6471 This means everything is in inverse video which otherwise would not be. */);
6473 DEFVAR_BOOL ("visible-bell", visible_bell,
6474 doc: /* *Non-nil means try to flash the frame to represent a bell.
6476 See also `ring-bell-function'. */);
6478 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6479 doc: /* *Non-nil means no need to redraw entire frame after suspending.
6480 A non-nil value is useful if the terminal can automatically preserve
6481 Emacs's frame display when you reenter Emacs.
6482 It is up to you to set this variable if your terminal can do that. */);
6484 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6485 doc: /* Name of the window system that Emacs uses for the first frame.
6486 The value is a symbol:
6487 nil for a termcap frame (a character-only terminal),
6488 'x' for an Emacs frame that is really an X window,
6489 'w32' for an Emacs frame that is a window on MS-Windows display,
6490 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6491 'pc' for a direct-write MS-DOS frame.
6493 Use of this variable as a boolean is deprecated. Instead,
6494 use `display-graphic-p' or any of the other `display-*-p'
6495 predicates which report frame's specific UI-related capabilities. */);
6497 DEFVAR_KBOARD ("window-system", Vwindow_system,
6498 doc: /* Name of window system through which the selected frame is displayed.
6499 The value is a symbol:
6500 nil for a termcap frame (a character-only terminal),
6501 'x' for an Emacs frame that is really an X window,
6502 'w32' for an Emacs frame that is a window on MS-Windows display,
6503 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6504 'pc' for a direct-write MS-DOS frame.
6506 Use of this variable as a boolean is deprecated. Instead,
6507 use `display-graphic-p' or any of the other `display-*-p'
6508 predicates which report frame's specific UI-related capabilities. */);
6510 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6511 doc: /* The version number of the window system in use.
6512 For X windows, this is 11. */);
6514 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6515 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6517 DEFVAR_LISP ("glyph-table", Vglyph_table,
6518 doc: /* Table defining how to output a glyph code to the frame.
6519 If not nil, this is a vector indexed by glyph code to define the glyph.
6520 Each element can be:
6521 integer: a glyph code which this glyph is an alias for.
6522 string: output this glyph using that string (not impl. in X windows).
6523 nil: this glyph mod 524288 is the code of a character to output,
6524 and this glyph / 524288 is the face number (see `face-id') to use
6525 while outputting it. */);
6526 Vglyph_table = Qnil;
6528 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6529 doc: /* Display table to use for buffers that specify none.
6530 See `buffer-display-table' for more information. */);
6531 Vstandard_display_table = Qnil;
6533 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6534 doc: /* *Non-nil means display update isn't paused when input is detected. */);
6535 redisplay_dont_pause = 1;
6537 #if PERIODIC_PREEMPTION_CHECKING
6538 DEFVAR_LISP ("redisplay-preemption-period", Vredisplay_preemption_period,
6539 doc: /* *The period in seconds between checking for input during redisplay.
6540 If input is detected, redisplay is pre-empted, and the input is processed.
6541 If nil, never pre-empt redisplay. */);
6542 Vredisplay_preemption_period = make_float (0.10);
6543 #endif
6545 #ifdef CANNOT_DUMP
6546 if (noninteractive)
6547 #endif
6549 Vinitial_window_system = Qnil;
6550 Vwindow_system_version = Qnil;