* src/window.c (get_phys_cursor_glyph): Fix Lisp_Object/int mixup.
[emacs.git] / src / dispnew.c
blobd6bf6666ee20e2d93016bf5f8bb33eaac85bce3c
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 || verify_row_hash (row));
612 ++row;
616 xassert (left >= 0 && right >= 0);
617 matrix->left_margin_glyphs = left;
618 matrix->right_margin_glyphs = right;
621 /* Number of rows to be used by MATRIX. */
622 matrix->nrows = dim.height;
623 xassert (matrix->nrows >= 0);
625 if (w)
627 if (matrix == w->current_matrix)
629 /* Mark rows in a current matrix of a window as not having
630 valid contents. It's important to not do this for
631 desired matrices. When Emacs starts, it may already be
632 building desired matrices when this function runs. */
633 if (window_width < 0)
634 window_width = window_box_width (w, -1);
636 /* Optimize the case that only the height has changed (C-x 2,
637 upper window). Invalidate all rows that are no longer part
638 of the window. */
639 if (!marginal_areas_changed_p
640 && !header_line_changed_p
641 && new_rows == 0
642 && dim.width == matrix->matrix_w
643 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
644 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
645 && matrix->window_width == window_width)
647 /* Find the last row in the window. */
648 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
649 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
651 ++i;
652 break;
655 /* Window end is invalid, if inside of the rows that
656 are invalidated below. */
657 if (INTEGERP (w->window_end_vpos)
658 && XFASTINT (w->window_end_vpos) >= i)
659 w->window_end_valid = Qnil;
661 while (i < matrix->nrows)
662 matrix->rows[i++].enabled_p = 0;
664 else
666 for (i = 0; i < matrix->nrows; ++i)
667 matrix->rows[i].enabled_p = 0;
670 else if (matrix == w->desired_matrix)
672 /* Rows in desired matrices always have to be cleared;
673 redisplay expects this is the case when it runs, so it
674 had better be the case when we adjust matrices between
675 redisplays. */
676 for (i = 0; i < matrix->nrows; ++i)
677 matrix->rows[i].enabled_p = 0;
682 /* Remember last values to be able to optimize frame redraws. */
683 matrix->matrix_x = x;
684 matrix->matrix_y = y;
685 matrix->matrix_w = dim.width;
686 matrix->matrix_h = dim.height;
688 /* Record the top y location and height of W at the time the matrix
689 was last adjusted. This is used to optimize redisplay above. */
690 if (w)
692 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
693 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
694 matrix->window_height = window_height;
695 matrix->window_width = window_width;
696 matrix->window_vscroll = w->vscroll;
701 /* Reverse the contents of rows in MATRIX between START and END. The
702 contents of the row at END - 1 end up at START, END - 2 at START +
703 1 etc. This is part of the implementation of rotate_matrix (see
704 below). */
706 static void
707 reverse_rows (struct glyph_matrix *matrix, int start, int end)
709 int i, j;
711 for (i = start, j = end - 1; i < j; ++i, --j)
713 /* Non-ISO HP/UX compiler doesn't like auto struct
714 initialization. */
715 struct glyph_row temp;
716 temp = matrix->rows[i];
717 matrix->rows[i] = matrix->rows[j];
718 matrix->rows[j] = temp;
723 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
724 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
725 indices. (Note: this does not copy glyphs, only glyph pointers in
726 row structures are moved around).
728 The algorithm used for rotating the vector was, I believe, first
729 described by Kernighan. See the vector R as consisting of two
730 sub-vectors AB, where A has length BY for BY >= 0. The result
731 after rotating is then BA. Reverse both sub-vectors to get ArBr
732 and reverse the result to get (ArBr)r which is BA. Similar for
733 rotating right. */
735 void
736 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
738 if (by < 0)
740 /* Up (rotate left, i.e. towards lower indices). */
741 by = -by;
742 reverse_rows (matrix, first, first + by);
743 reverse_rows (matrix, first + by, last);
744 reverse_rows (matrix, first, last);
746 else if (by > 0)
748 /* Down (rotate right, i.e. towards higher indices). */
749 reverse_rows (matrix, last - by, last);
750 reverse_rows (matrix, first, last - by);
751 reverse_rows (matrix, first, last);
756 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
757 with indices START <= index < END. Increment positions by DELTA/
758 DELTA_BYTES. */
760 void
761 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
762 EMACS_INT delta, EMACS_INT delta_bytes)
764 /* Check that START and END are reasonable values. */
765 xassert (start >= 0 && start <= matrix->nrows);
766 xassert (end >= 0 && end <= matrix->nrows);
767 xassert (start <= end);
769 for (; start < end; ++start)
770 increment_row_positions (matrix->rows + start, delta, delta_bytes);
774 /* Enable a range of rows in glyph matrix MATRIX. START and END are
775 the row indices of the first and last + 1 row to enable. If
776 ENABLED_P is non-zero, enabled_p flags in rows will be set to 1. */
778 void
779 enable_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end, int enabled_p)
781 xassert (start <= end);
782 xassert (start >= 0 && start < matrix->nrows);
783 xassert (end >= 0 && end <= matrix->nrows);
785 for (; start < end; ++start)
786 matrix->rows[start].enabled_p = enabled_p != 0;
790 /* Clear MATRIX.
792 This empties all rows in MATRIX by setting the enabled_p flag for
793 all rows of the matrix to zero. The function prepare_desired_row
794 will eventually really clear a row when it sees one with a zero
795 enabled_p flag.
797 Resets update hints to defaults value. The only update hint
798 currently present is the flag MATRIX->no_scrolling_p. */
800 void
801 clear_glyph_matrix (struct glyph_matrix *matrix)
803 if (matrix)
805 enable_glyph_matrix_rows (matrix, 0, matrix->nrows, 0);
806 matrix->no_scrolling_p = 0;
811 /* Shift part of the glyph matrix MATRIX of window W up or down.
812 Increment y-positions in glyph rows between START and END by DY,
813 and recompute their visible height. */
815 void
816 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
818 int min_y, max_y;
820 xassert (start <= end);
821 xassert (start >= 0 && start < matrix->nrows);
822 xassert (end >= 0 && end <= matrix->nrows);
824 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
825 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
827 for (; start < end; ++start)
829 struct glyph_row *row = &matrix->rows[start];
831 row->y += dy;
832 row->visible_height = row->height;
834 if (row->y < min_y)
835 row->visible_height -= min_y - row->y;
836 if (row->y + row->height > max_y)
837 row->visible_height -= row->y + row->height - max_y;
838 if (row->fringe_bitmap_periodic_p)
839 row->redraw_fringe_bitmaps_p = 1;
844 /* Mark all rows in current matrices of frame F as invalid. Marking
845 invalid is done by setting enabled_p to zero for all rows in a
846 current matrix. */
848 void
849 clear_current_matrices (register struct frame *f)
851 /* Clear frame current matrix, if we have one. */
852 if (f->current_matrix)
853 clear_glyph_matrix (f->current_matrix);
855 /* Clear the matrix of the menu bar window, if such a window exists.
856 The menu bar window is currently used to display menus on X when
857 no toolkit support is compiled in. */
858 if (WINDOWP (f->menu_bar_window))
859 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
861 /* Clear the matrix of the tool-bar window, if any. */
862 if (WINDOWP (f->tool_bar_window))
863 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
865 /* Clear current window matrices. */
866 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
867 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
871 /* Clear out all display lines of F for a coming redisplay. */
873 void
874 clear_desired_matrices (register struct frame *f)
876 if (f->desired_matrix)
877 clear_glyph_matrix (f->desired_matrix);
879 if (WINDOWP (f->menu_bar_window))
880 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
882 if (WINDOWP (f->tool_bar_window))
883 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
885 /* Do it for window matrices. */
886 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
887 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
891 /* Clear matrices in window tree rooted in W. If DESIRED_P is
892 non-zero clear desired matrices, otherwise clear current matrices. */
894 static void
895 clear_window_matrices (struct window *w, int desired_p)
897 while (w)
899 if (!NILP (w->hchild))
901 xassert (WINDOWP (w->hchild));
902 clear_window_matrices (XWINDOW (w->hchild), desired_p);
904 else if (!NILP (w->vchild))
906 xassert (WINDOWP (w->vchild));
907 clear_window_matrices (XWINDOW (w->vchild), desired_p);
909 else
911 if (desired_p)
912 clear_glyph_matrix (w->desired_matrix);
913 else
915 clear_glyph_matrix (w->current_matrix);
916 w->window_end_valid = Qnil;
920 w = NILP (w->next) ? 0 : XWINDOW (w->next);
926 /***********************************************************************
927 Glyph Rows
929 See dispextern.h for an overall explanation of glyph rows.
930 ***********************************************************************/
932 /* Clear glyph row ROW. Do it in a way that makes it robust against
933 changes in the glyph_row structure, i.e. addition or removal of
934 structure members. */
936 static struct glyph_row null_row;
938 void
939 clear_glyph_row (struct glyph_row *row)
941 struct glyph *p[1 + LAST_AREA];
943 /* Save pointers. */
944 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
945 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
946 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
947 p[LAST_AREA] = row->glyphs[LAST_AREA];
949 /* Clear. */
950 *row = null_row;
952 /* Restore pointers. */
953 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
954 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
955 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
956 row->glyphs[LAST_AREA] = p[LAST_AREA];
958 #if 0 /* At some point, some bit-fields of struct glyph were not set,
959 which made glyphs unequal when compared with GLYPH_EQUAL_P.
960 Redisplay outputs such glyphs, and flickering effects were
961 the result. This also depended on the contents of memory
962 returned by xmalloc. If flickering happens again, activate
963 the code below. If the flickering is gone with that, chances
964 are that the flickering has the same reason as here. */
965 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
966 #endif
970 /* Make ROW an empty, enabled row of canonical character height,
971 in window W starting at y-position Y. */
973 void
974 blank_row (struct window *w, struct glyph_row *row, int y)
976 int min_y, max_y;
978 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
979 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
981 clear_glyph_row (row);
982 row->y = y;
983 row->ascent = row->phys_ascent = 0;
984 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
985 row->visible_height = row->height;
987 if (row->y < min_y)
988 row->visible_height -= min_y - row->y;
989 if (row->y + row->height > max_y)
990 row->visible_height -= row->y + row->height - max_y;
992 row->enabled_p = 1;
996 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
997 are the amounts by which to change positions. Note that the first
998 glyph of the text area of a row can have a buffer position even if
999 the used count of the text area is zero. Such rows display line
1000 ends. */
1002 static void
1003 increment_row_positions (struct glyph_row *row,
1004 EMACS_INT delta, EMACS_INT delta_bytes)
1006 int area, i;
1008 /* Increment start and end positions. */
1009 MATRIX_ROW_START_CHARPOS (row) += delta;
1010 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
1011 MATRIX_ROW_END_CHARPOS (row) += delta;
1012 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
1013 CHARPOS (row->start.pos) += delta;
1014 BYTEPOS (row->start.pos) += delta_bytes;
1015 CHARPOS (row->end.pos) += delta;
1016 BYTEPOS (row->end.pos) += delta_bytes;
1018 if (!row->enabled_p)
1019 return;
1021 /* Increment positions in glyphs. */
1022 for (area = 0; area < LAST_AREA; ++area)
1023 for (i = 0; i < row->used[area]; ++i)
1024 if (BUFFERP (row->glyphs[area][i].object)
1025 && row->glyphs[area][i].charpos > 0)
1026 row->glyphs[area][i].charpos += delta;
1028 /* Capture the case of rows displaying a line end. */
1029 if (row->used[TEXT_AREA] == 0
1030 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
1031 row->glyphs[TEXT_AREA]->charpos += delta;
1035 #if 0
1036 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
1037 contents, i.e. glyph structure contents are exchanged between A and
1038 B without changing glyph pointers in A and B. */
1040 static void
1041 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
1043 int area;
1045 for (area = 0; area < LAST_AREA; ++area)
1047 /* Number of glyphs to swap. */
1048 int max_used = max (a->used[area], b->used[area]);
1050 /* Start of glyphs in area of row A. */
1051 struct glyph *glyph_a = a->glyphs[area];
1053 /* End + 1 of glyphs in area of row A. */
1054 struct glyph *glyph_a_end = a->glyphs[max_used];
1056 /* Start of glyphs in area of row B. */
1057 struct glyph *glyph_b = b->glyphs[area];
1059 while (glyph_a < glyph_a_end)
1061 /* Non-ISO HP/UX compiler doesn't like auto struct
1062 initialization. */
1063 struct glyph temp;
1064 temp = *glyph_a;
1065 *glyph_a = *glyph_b;
1066 *glyph_b = temp;
1067 ++glyph_a;
1068 ++glyph_b;
1073 #endif /* 0 */
1075 /* Exchange pointers to glyph memory between glyph rows A and B. Also
1076 exchange the used[] array and the hash values of the rows, because
1077 these should all go together for the row's hash value to be
1078 correct. */
1080 static inline void
1081 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1083 int i;
1084 unsigned hash_tem = a->hash;
1086 for (i = 0; i < LAST_AREA + 1; ++i)
1088 struct glyph *temp = a->glyphs[i];
1089 short used_tem = a->used[i];
1091 a->glyphs[i] = b->glyphs[i];
1092 b->glyphs[i] = temp;
1093 a->used[i] = b->used[i];
1094 b->used[i] = used_tem;
1096 a->hash = b->hash;
1097 b->hash = hash_tem;
1101 /* Copy glyph row structure FROM to glyph row structure TO, except
1102 that glyph pointers, the `used' counts, and the hash values in the
1103 structures are left unchanged. */
1105 static inline void
1106 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1108 struct glyph *pointers[1 + LAST_AREA];
1109 short used[1 + LAST_AREA];
1110 unsigned hashval;
1112 /* Save glyph pointers of TO. */
1113 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1114 memcpy (used, to->used, sizeof to->used);
1115 hashval = to->hash;
1117 /* Do a structure assignment. */
1118 *to = *from;
1120 /* Restore original pointers of TO. */
1121 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1122 memcpy (to->used, used, sizeof to->used);
1123 to->hash = hashval;
1127 /* Assign glyph row FROM to glyph row TO. This works like a structure
1128 assignment TO = FROM, except that glyph pointers are not copied but
1129 exchanged between TO and FROM. Pointers must be exchanged to avoid
1130 a memory leak. */
1132 static inline void
1133 assign_row (struct glyph_row *to, struct glyph_row *from)
1135 swap_glyph_pointers (to, from);
1136 copy_row_except_pointers (to, from);
1140 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1141 a row in a window matrix, is a slice of the glyph memory of the
1142 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1143 is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
1144 memory of FRAME_ROW. */
1146 #if GLYPH_DEBUG
1148 static int
1149 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1151 struct glyph *window_glyph_start = window_row->glyphs[0];
1152 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1153 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1155 return (frame_glyph_start <= window_glyph_start
1156 && window_glyph_start < frame_glyph_end);
1159 #endif /* GLYPH_DEBUG */
1161 #if 0
1163 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1164 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1165 in WINDOW_MATRIX is found satisfying the condition. */
1167 static struct glyph_row *
1168 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1169 struct glyph_matrix *frame_matrix, int row)
1171 int i;
1173 xassert (row >= 0 && row < frame_matrix->nrows);
1175 for (i = 0; i < window_matrix->nrows; ++i)
1176 if (glyph_row_slice_p (window_matrix->rows + i,
1177 frame_matrix->rows + row))
1178 break;
1180 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1183 #endif /* 0 */
1185 /* Prepare ROW for display. Desired rows are cleared lazily,
1186 i.e. they are only marked as to be cleared by setting their
1187 enabled_p flag to zero. When a row is to be displayed, a prior
1188 call to this function really clears it. */
1190 void
1191 prepare_desired_row (struct glyph_row *row)
1193 if (!row->enabled_p)
1195 int rp = row->reversed_p;
1197 clear_glyph_row (row);
1198 row->enabled_p = 1;
1199 row->reversed_p = rp;
1204 /* Return a hash code for glyph row ROW. */
1206 static int
1207 line_hash_code (struct glyph_row *row)
1209 int hash = 0;
1211 if (row->enabled_p)
1213 struct glyph *glyph = row->glyphs[TEXT_AREA];
1214 struct glyph *end = glyph + row->used[TEXT_AREA];
1216 while (glyph < end)
1218 int c = glyph->u.ch;
1219 int face_id = glyph->face_id;
1220 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1221 c -= SPACEGLYPH;
1222 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1223 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1224 ++glyph;
1227 if (hash == 0)
1228 hash = 1;
1231 return hash;
1235 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1236 the number of characters in the line. If must_write_spaces is
1237 zero, leading and trailing spaces are ignored. */
1239 static int
1240 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1242 struct glyph_row *row = matrix->rows + vpos;
1243 struct glyph *beg = row->glyphs[TEXT_AREA];
1244 struct glyph *end = beg + row->used[TEXT_AREA];
1245 int len;
1246 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1247 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1249 /* Ignore trailing and leading spaces if we can. */
1250 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1252 /* Skip from the end over trailing spaces. */
1253 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1254 --end;
1256 /* All blank line. */
1257 if (end == beg)
1258 return 0;
1260 /* Skip over leading spaces. */
1261 while (CHAR_GLYPH_SPACE_P (*beg))
1262 ++beg;
1265 /* If we don't have a glyph-table, each glyph is one character,
1266 so return the number of glyphs. */
1267 if (glyph_table_base == 0)
1268 len = end - beg;
1269 else
1271 /* Otherwise, scan the glyphs and accumulate their total length
1272 in LEN. */
1273 len = 0;
1274 while (beg < end)
1276 GLYPH g;
1278 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1280 if (GLYPH_INVALID_P (g)
1281 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1282 len += 1;
1283 else
1284 len += GLYPH_LENGTH (glyph_table_base, g);
1286 ++beg;
1290 return len;
1294 /* Test two glyph rows A and B for equality. Value is non-zero if A
1295 and B have equal contents. MOUSE_FACE_P non-zero means compare the
1296 mouse_face_p flags of A and B, too. */
1298 static inline int
1299 row_equal_p (struct glyph_row *a, struct glyph_row *b, int mouse_face_p)
1301 xassert (verify_row_hash (a));
1302 xassert (verify_row_hash (b));
1304 if (a == b)
1305 return 1;
1306 else if (a->hash != b->hash)
1307 return 0;
1308 else
1310 struct glyph *a_glyph, *b_glyph, *a_end;
1311 int area;
1313 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1314 return 0;
1316 /* Compare glyphs. */
1317 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1319 if (a->used[area] != b->used[area])
1320 return 0;
1322 a_glyph = a->glyphs[area];
1323 a_end = a_glyph + a->used[area];
1324 b_glyph = b->glyphs[area];
1326 while (a_glyph < a_end
1327 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1328 ++a_glyph, ++b_glyph;
1330 if (a_glyph != a_end)
1331 return 0;
1334 if (a->fill_line_p != b->fill_line_p
1335 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1336 || a->left_fringe_bitmap != b->left_fringe_bitmap
1337 || a->left_fringe_face_id != b->left_fringe_face_id
1338 || a->left_fringe_offset != b->left_fringe_offset
1339 || a->right_fringe_bitmap != b->right_fringe_bitmap
1340 || a->right_fringe_face_id != b->right_fringe_face_id
1341 || a->right_fringe_offset != b->right_fringe_offset
1342 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1343 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1344 || a->exact_window_width_line_p != b->exact_window_width_line_p
1345 || a->overlapped_p != b->overlapped_p
1346 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1347 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1348 || a->reversed_p != b->reversed_p
1349 /* Different partially visible characters on left margin. */
1350 || a->x != b->x
1351 /* Different height. */
1352 || a->ascent != b->ascent
1353 || a->phys_ascent != b->phys_ascent
1354 || a->phys_height != b->phys_height
1355 || a->visible_height != b->visible_height)
1356 return 0;
1359 return 1;
1364 /***********************************************************************
1365 Glyph Pool
1367 See dispextern.h for an overall explanation of glyph pools.
1368 ***********************************************************************/
1370 /* Allocate a glyph_pool structure. The structure returned is
1371 initialized with zeros. The global variable glyph_pool_count is
1372 incremented for each pool allocated. */
1374 static struct glyph_pool *
1375 new_glyph_pool (void)
1377 struct glyph_pool *result;
1379 /* Allocate a new glyph_pool and clear it. */
1380 result = (struct glyph_pool *) xmalloc (sizeof *result);
1381 memset (result, 0, sizeof *result);
1383 /* For memory leak and double deletion checking. */
1384 ++glyph_pool_count;
1386 return result;
1390 /* Free a glyph_pool structure POOL. The function may be called with
1391 a null POOL pointer. The global variable glyph_pool_count is
1392 decremented with every pool structure freed. If this count gets
1393 negative, more structures were freed than allocated, i.e. one
1394 structure must have been freed more than once or a bogus pointer
1395 was passed to free_glyph_pool. */
1397 static void
1398 free_glyph_pool (struct glyph_pool *pool)
1400 if (pool)
1402 /* More freed than allocated? */
1403 --glyph_pool_count;
1404 xassert (glyph_pool_count >= 0);
1406 xfree (pool->glyphs);
1407 xfree (pool);
1412 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1413 columns we need. This function never shrinks a pool. The only
1414 case in which this would make sense, would be when a frame's size
1415 is changed from a large value to a smaller one. But, if someone
1416 does it once, we can expect that he will do it again.
1418 Value is non-zero if the pool changed in a way which makes
1419 re-adjusting window glyph matrices necessary. */
1421 static int
1422 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1424 ptrdiff_t needed;
1425 int changed_p;
1427 changed_p = (pool->glyphs == 0
1428 || matrix_dim.height != pool->nrows
1429 || matrix_dim.width != pool->ncolumns);
1431 /* Enlarge the glyph pool. */
1432 needed = matrix_dim.width;
1433 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1434 memory_full (SIZE_MAX);
1435 needed *= matrix_dim.height;
1436 if (needed > pool->nglyphs)
1438 ptrdiff_t old_nglyphs = pool->nglyphs;
1439 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1440 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1441 memset (pool->glyphs + old_nglyphs, 0,
1442 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1445 /* Remember the number of rows and columns because (a) we use them
1446 to do sanity checks, and (b) the number of columns determines
1447 where rows in the frame matrix start---this must be available to
1448 determine pointers to rows of window sub-matrices. */
1449 pool->nrows = matrix_dim.height;
1450 pool->ncolumns = matrix_dim.width;
1452 return changed_p;
1457 /***********************************************************************
1458 Debug Code
1459 ***********************************************************************/
1461 #if GLYPH_DEBUG
1464 /* Flush standard output. This is sometimes useful to call from the debugger.
1465 XXX Maybe this should be changed to flush the current terminal instead of
1466 stdout.
1469 void flush_stdout (void) EXTERNALLY_VISIBLE;
1471 void
1472 flush_stdout (void)
1474 fflush (stdout);
1478 /* Check that no glyph pointers have been lost in MATRIX. If a
1479 pointer has been lost, e.g. by using a structure assignment between
1480 rows, at least one pointer must occur more than once in the rows of
1481 MATRIX. */
1483 void
1484 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1486 int i, j;
1488 for (i = 0; i < matrix->nrows; ++i)
1489 for (j = 0; j < matrix->nrows; ++j)
1490 xassert (i == j
1491 || (matrix->rows[i].glyphs[TEXT_AREA]
1492 != matrix->rows[j].glyphs[TEXT_AREA]));
1496 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1498 struct glyph_row *
1499 matrix_row (struct glyph_matrix *matrix, int row)
1501 xassert (matrix && matrix->rows);
1502 xassert (row >= 0 && row < matrix->nrows);
1504 /* That's really too slow for normal testing because this function
1505 is called almost everywhere. Although---it's still astonishingly
1506 fast, so it is valuable to have for debugging purposes. */
1507 #if 0
1508 check_matrix_pointer_lossage (matrix);
1509 #endif
1511 return matrix->rows + row;
1515 #if 0 /* This function makes invalid assumptions when text is
1516 partially invisible. But it might come handy for debugging
1517 nevertheless. */
1519 /* Check invariants that must hold for an up to date current matrix of
1520 window W. */
1522 static void
1523 check_matrix_invariants (struct window *w)
1525 struct glyph_matrix *matrix = w->current_matrix;
1526 int yb = window_text_bottom_y (w);
1527 struct glyph_row *row = matrix->rows;
1528 struct glyph_row *last_text_row = NULL;
1529 struct buffer *saved = current_buffer;
1530 struct buffer *buffer = XBUFFER (w->buffer);
1531 int c;
1533 /* This can sometimes happen for a fresh window. */
1534 if (matrix->nrows < 2)
1535 return;
1537 set_buffer_temp (buffer);
1539 /* Note: last row is always reserved for the mode line. */
1540 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1541 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1543 struct glyph_row *next = row + 1;
1545 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1546 last_text_row = row;
1548 /* Check that character and byte positions are in sync. */
1549 xassert (MATRIX_ROW_START_BYTEPOS (row)
1550 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1551 xassert (BYTEPOS (row->start.pos)
1552 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1554 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1555 have such a position temporarily in case of a minibuffer
1556 displaying something like `[Sole completion]' at its end. */
1557 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1559 xassert (MATRIX_ROW_END_BYTEPOS (row)
1560 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1561 xassert (BYTEPOS (row->end.pos)
1562 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1565 /* Check that end position of `row' is equal to start position
1566 of next row. */
1567 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1569 xassert (MATRIX_ROW_END_CHARPOS (row)
1570 == MATRIX_ROW_START_CHARPOS (next));
1571 xassert (MATRIX_ROW_END_BYTEPOS (row)
1572 == MATRIX_ROW_START_BYTEPOS (next));
1573 xassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1574 xassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1576 row = next;
1579 xassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1580 xassert (w->desired_matrix->rows != NULL);
1581 set_buffer_temp (saved);
1584 #endif /* 0 */
1586 #endif /* GLYPH_DEBUG != 0 */
1590 /**********************************************************************
1591 Allocating/ Adjusting Glyph Matrices
1592 **********************************************************************/
1594 /* Allocate glyph matrices over a window tree for a frame-based
1595 redisplay
1597 X and Y are column/row within the frame glyph matrix where
1598 sub-matrices for the window tree rooted at WINDOW must be
1599 allocated. DIM_ONLY_P non-zero means that the caller of this
1600 function is only interested in the result matrix dimension, and
1601 matrix adjustments should not be performed.
1603 The function returns the total width/height of the sub-matrices of
1604 the window tree. If called on a frame root window, the computation
1605 will take the mini-buffer window into account.
1607 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1609 NEW_LEAF_MATRIX set if any window in the tree did not have a
1610 glyph matrices yet, and
1612 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1613 any window in the tree will be changed or have been changed (see
1614 DIM_ONLY_P)
1616 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1617 function.
1619 Windows are arranged into chains of windows on the same level
1620 through the next fields of window structures. Such a level can be
1621 either a sequence of horizontally adjacent windows from left to
1622 right, or a sequence of vertically adjacent windows from top to
1623 bottom. Each window in a horizontal sequence can be either a leaf
1624 window or a vertical sequence; a window in a vertical sequence can
1625 be either a leaf or a horizontal sequence. All windows in a
1626 horizontal sequence have the same height, and all windows in a
1627 vertical sequence have the same width.
1629 This function uses, for historical reasons, a more general
1630 algorithm to determine glyph matrix dimensions that would be
1631 necessary.
1633 The matrix height of a horizontal sequence is determined by the
1634 maximum height of any matrix in the sequence. The matrix width of
1635 a horizontal sequence is computed by adding up matrix widths of
1636 windows in the sequence.
1638 |<------- result width ------->|
1639 +---------+----------+---------+ ---
1640 | | | | |
1641 | | | |
1642 +---------+ | | result height
1643 | +---------+
1644 | | |
1645 +----------+ ---
1647 The matrix width of a vertical sequence is the maximum matrix width
1648 of any window in the sequence. Its height is computed by adding up
1649 matrix heights of windows in the sequence.
1651 |<---- result width -->|
1652 +---------+ ---
1653 | | |
1654 | | |
1655 +---------+--+ |
1656 | | |
1657 | | result height
1659 +------------+---------+ |
1660 | | |
1661 | | |
1662 +------------+---------+ --- */
1664 /* Bit indicating that a new matrix will be allocated or has been
1665 allocated. */
1667 #define NEW_LEAF_MATRIX (1 << 0)
1669 /* Bit indicating that a matrix will or has changed its location or
1670 size. */
1672 #define CHANGED_LEAF_MATRIX (1 << 1)
1674 static struct dim
1675 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1676 int dim_only_p, int *window_change_flags)
1678 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1679 int x0 = x, y0 = y;
1680 int wmax = 0, hmax = 0;
1681 struct dim total;
1682 struct dim dim;
1683 struct window *w;
1684 int in_horz_combination_p;
1686 /* What combination is WINDOW part of? Compute this once since the
1687 result is the same for all windows in the `next' chain. The
1688 special case of a root window (parent equal to nil) is treated
1689 like a vertical combination because a root window's `next'
1690 points to the mini-buffer window, if any, which is arranged
1691 vertically below other windows. */
1692 in_horz_combination_p
1693 = (!NILP (XWINDOW (window)->parent)
1694 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1696 /* For WINDOW and all windows on the same level. */
1699 w = XWINDOW (window);
1701 /* Get the dimension of the window sub-matrix for W, depending
1702 on whether this is a combination or a leaf window. */
1703 if (!NILP (w->hchild))
1704 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
1705 dim_only_p,
1706 window_change_flags);
1707 else if (!NILP (w->vchild))
1708 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
1709 dim_only_p,
1710 window_change_flags);
1711 else
1713 /* If not already done, allocate sub-matrix structures. */
1714 if (w->desired_matrix == NULL)
1716 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1717 w->current_matrix = new_glyph_matrix (f->current_pool);
1718 *window_change_flags |= NEW_LEAF_MATRIX;
1721 /* Width and height MUST be chosen so that there are no
1722 holes in the frame matrix. */
1723 dim.width = required_matrix_width (w);
1724 dim.height = required_matrix_height (w);
1726 /* Will matrix be re-allocated? */
1727 if (x != w->desired_matrix->matrix_x
1728 || y != w->desired_matrix->matrix_y
1729 || dim.width != w->desired_matrix->matrix_w
1730 || dim.height != w->desired_matrix->matrix_h
1731 || (margin_glyphs_to_reserve (w, dim.width,
1732 w->left_margin_cols)
1733 != w->desired_matrix->left_margin_glyphs)
1734 || (margin_glyphs_to_reserve (w, dim.width,
1735 w->right_margin_cols)
1736 != w->desired_matrix->right_margin_glyphs))
1737 *window_change_flags |= CHANGED_LEAF_MATRIX;
1739 /* Actually change matrices, if allowed. Do not consider
1740 CHANGED_LEAF_MATRIX computed above here because the pool
1741 may have been changed which we don't now here. We trust
1742 that we only will be called with DIM_ONLY_P != 0 when
1743 necessary. */
1744 if (!dim_only_p)
1746 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1747 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1751 /* If we are part of a horizontal combination, advance x for
1752 windows to the right of W; otherwise advance y for windows
1753 below W. */
1754 if (in_horz_combination_p)
1755 x += dim.width;
1756 else
1757 y += dim.height;
1759 /* Remember maximum glyph matrix dimensions. */
1760 wmax = max (wmax, dim.width);
1761 hmax = max (hmax, dim.height);
1763 /* Next window on same level. */
1764 window = w->next;
1766 while (!NILP (window));
1768 /* Set `total' to the total glyph matrix dimension of this window
1769 level. In a vertical combination, the width is the width of the
1770 widest window; the height is the y we finally reached, corrected
1771 by the y we started with. In a horizontal combination, the total
1772 height is the height of the tallest window, and the width is the
1773 x we finally reached, corrected by the x we started with. */
1774 if (in_horz_combination_p)
1776 total.width = x - x0;
1777 total.height = hmax;
1779 else
1781 total.width = wmax;
1782 total.height = y - y0;
1785 return total;
1789 /* Return the required height of glyph matrices for window W. */
1791 static int
1792 required_matrix_height (struct window *w)
1794 #ifdef HAVE_WINDOW_SYSTEM
1795 struct frame *f = XFRAME (w->frame);
1797 if (FRAME_WINDOW_P (f))
1799 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1800 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1801 return (((window_pixel_height + ch_height - 1)
1802 / ch_height) * w->nrows_scale_factor
1803 /* One partially visible line at the top and
1804 bottom of the window. */
1806 /* 2 for header and mode line. */
1807 + 2);
1809 #endif /* HAVE_WINDOW_SYSTEM */
1811 return WINDOW_TOTAL_LINES (w);
1815 /* Return the required width of glyph matrices for window W. */
1817 static int
1818 required_matrix_width (struct window *w)
1820 #ifdef HAVE_WINDOW_SYSTEM
1821 struct frame *f = XFRAME (w->frame);
1822 if (FRAME_WINDOW_P (f))
1824 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1825 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1827 /* Compute number of glyphs needed in a glyph row. */
1828 return (((window_pixel_width + ch_width - 1)
1829 / ch_width) * w->ncols_scale_factor
1830 /* 2 partially visible columns in the text area. */
1832 /* One partially visible column at the right
1833 edge of each marginal area. */
1834 + 1 + 1);
1836 #endif /* HAVE_WINDOW_SYSTEM */
1838 return XINT (w->total_cols);
1842 /* Allocate window matrices for window-based redisplay. W is the
1843 window whose matrices must be allocated/reallocated. */
1845 static void
1846 allocate_matrices_for_window_redisplay (struct window *w)
1848 while (w)
1850 if (!NILP (w->vchild))
1851 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
1852 else if (!NILP (w->hchild))
1853 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
1854 else
1856 /* W is a leaf window. */
1857 struct dim dim;
1859 /* If matrices are not yet allocated, allocate them now. */
1860 if (w->desired_matrix == NULL)
1862 w->desired_matrix = new_glyph_matrix (NULL);
1863 w->current_matrix = new_glyph_matrix (NULL);
1866 dim.width = required_matrix_width (w);
1867 dim.height = required_matrix_height (w);
1868 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1869 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1872 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1877 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1878 do it for all frames; otherwise do it just for the given frame.
1879 This function must be called when a new frame is created, its size
1880 changes, or its window configuration changes. */
1882 void
1883 adjust_glyphs (struct frame *f)
1885 /* Block input so that expose events and other events that access
1886 glyph matrices are not processed while we are changing them. */
1887 BLOCK_INPUT;
1889 if (f)
1890 adjust_frame_glyphs (f);
1891 else
1893 Lisp_Object tail, lisp_frame;
1895 FOR_EACH_FRAME (tail, lisp_frame)
1896 adjust_frame_glyphs (XFRAME (lisp_frame));
1899 UNBLOCK_INPUT;
1903 /* Adjust frame glyphs when Emacs is initialized.
1905 To be called from init_display.
1907 We need a glyph matrix because redraw will happen soon.
1908 Unfortunately, window sizes on selected_frame are not yet set to
1909 meaningful values. I believe we can assume that there are only two
1910 windows on the frame---the mini-buffer and the root window. Frame
1911 height and width seem to be correct so far. So, set the sizes of
1912 windows to estimated values. */
1914 static void
1915 adjust_frame_glyphs_initially (void)
1917 struct frame *sf = SELECTED_FRAME ();
1918 struct window *root = XWINDOW (sf->root_window);
1919 struct window *mini = XWINDOW (root->next);
1920 int frame_lines = FRAME_LINES (sf);
1921 int frame_cols = FRAME_COLS (sf);
1922 int top_margin = FRAME_TOP_MARGIN (sf);
1924 /* Do it for the root window. */
1925 XSETFASTINT (root->top_line, top_margin);
1926 XSETFASTINT (root->total_lines, frame_lines - 1 - top_margin);
1927 XSETFASTINT (root->total_cols, frame_cols);
1929 /* Do it for the mini-buffer window. */
1930 XSETFASTINT (mini->top_line, frame_lines - 1);
1931 XSETFASTINT (mini->total_lines, 1);
1932 XSETFASTINT (mini->total_cols, frame_cols);
1934 adjust_frame_glyphs (sf);
1935 glyphs_initialized_initially_p = 1;
1939 /* Allocate/reallocate glyph matrices of a single frame F. */
1941 static void
1942 adjust_frame_glyphs (struct frame *f)
1944 if (FRAME_WINDOW_P (f))
1945 adjust_frame_glyphs_for_window_redisplay (f);
1946 else
1947 adjust_frame_glyphs_for_frame_redisplay (f);
1949 /* Don't forget the message buffer and the buffer for
1950 decode_mode_spec. */
1951 adjust_frame_message_buffer (f);
1952 adjust_decode_mode_spec_buffer (f);
1954 f->glyphs_initialized_p = 1;
1957 /* Return 1 if any window in the tree has nonzero window margins. See
1958 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1959 static int
1960 showing_window_margins_p (struct window *w)
1962 while (w)
1964 if (!NILP (w->hchild))
1966 if (showing_window_margins_p (XWINDOW (w->hchild)))
1967 return 1;
1969 else if (!NILP (w->vchild))
1971 if (showing_window_margins_p (XWINDOW (w->vchild)))
1972 return 1;
1974 else if (!NILP (w->left_margin_cols)
1975 || !NILP (w->right_margin_cols))
1976 return 1;
1978 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1980 return 0;
1984 /* In the window tree with root W, build current matrices of leaf
1985 windows from the frame's current matrix. */
1987 static void
1988 fake_current_matrices (Lisp_Object window)
1990 struct window *w;
1992 for (; !NILP (window); window = w->next)
1994 w = XWINDOW (window);
1996 if (!NILP (w->hchild))
1997 fake_current_matrices (w->hchild);
1998 else if (!NILP (w->vchild))
1999 fake_current_matrices (w->vchild);
2000 else
2002 int i;
2003 struct frame *f = XFRAME (w->frame);
2004 struct glyph_matrix *m = w->current_matrix;
2005 struct glyph_matrix *fm = f->current_matrix;
2007 xassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
2008 xassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
2010 for (i = 0; i < m->matrix_h; ++i)
2012 struct glyph_row *r = m->rows + i;
2013 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
2015 xassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
2016 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
2018 r->enabled_p = fr->enabled_p;
2019 if (r->enabled_p)
2021 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
2022 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
2023 r->used[TEXT_AREA] = (m->matrix_w
2024 - r->used[LEFT_MARGIN_AREA]
2025 - r->used[RIGHT_MARGIN_AREA]);
2026 r->mode_line_p = 0;
2034 /* Save away the contents of frame F's current frame matrix. Value is
2035 a glyph matrix holding the contents of F's current frame matrix. */
2037 static struct glyph_matrix *
2038 save_current_matrix (struct frame *f)
2040 int i;
2041 struct glyph_matrix *saved;
2043 saved = (struct glyph_matrix *) xmalloc (sizeof *saved);
2044 memset (saved, 0, sizeof *saved);
2045 saved->nrows = f->current_matrix->nrows;
2046 saved->rows = (struct glyph_row *) xmalloc (saved->nrows
2047 * sizeof *saved->rows);
2048 memset (saved->rows, 0, saved->nrows * sizeof *saved->rows);
2050 for (i = 0; i < saved->nrows; ++i)
2052 struct glyph_row *from = f->current_matrix->rows + i;
2053 struct glyph_row *to = saved->rows + i;
2054 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2055 to->glyphs[TEXT_AREA] = (struct glyph *) xmalloc (nbytes);
2056 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2057 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2060 return saved;
2064 /* Restore the contents of frame F's current frame matrix from SAVED,
2065 and free memory associated with SAVED. */
2067 static void
2068 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
2070 int i;
2072 for (i = 0; i < saved->nrows; ++i)
2074 struct glyph_row *from = saved->rows + i;
2075 struct glyph_row *to = f->current_matrix->rows + i;
2076 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2077 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2078 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2079 xfree (from->glyphs[TEXT_AREA]);
2082 xfree (saved->rows);
2083 xfree (saved);
2088 /* Allocate/reallocate glyph matrices of a single frame F for
2089 frame-based redisplay. */
2091 static void
2092 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
2094 struct dim matrix_dim;
2095 int pool_changed_p;
2096 int window_change_flags;
2097 int top_window_y;
2099 if (!FRAME_LIVE_P (f))
2100 return;
2102 top_window_y = FRAME_TOP_MARGIN (f);
2104 /* Allocate glyph pool structures if not already done. */
2105 if (f->desired_pool == NULL)
2107 f->desired_pool = new_glyph_pool ();
2108 f->current_pool = new_glyph_pool ();
2111 /* Allocate frames matrix structures if needed. */
2112 if (f->desired_matrix == NULL)
2114 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2115 f->current_matrix = new_glyph_matrix (f->current_pool);
2118 /* Compute window glyph matrices. (This takes the mini-buffer
2119 window into account). The result is the size of the frame glyph
2120 matrix needed. The variable window_change_flags is set to a bit
2121 mask indicating whether new matrices will be allocated or
2122 existing matrices change their size or location within the frame
2123 matrix. */
2124 window_change_flags = 0;
2125 matrix_dim
2126 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2127 0, top_window_y,
2129 &window_change_flags);
2131 /* Add in menu bar lines, if any. */
2132 matrix_dim.height += top_window_y;
2134 /* Enlarge pools as necessary. */
2135 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2136 realloc_glyph_pool (f->current_pool, matrix_dim);
2138 /* Set up glyph pointers within window matrices. Do this only if
2139 absolutely necessary since it requires a frame redraw. */
2140 if (pool_changed_p || window_change_flags)
2142 /* Do it for window matrices. */
2143 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2144 0, top_window_y, 0,
2145 &window_change_flags);
2147 /* Size of frame matrices must equal size of frame. Note
2148 that we are called for X frames with window widths NOT equal
2149 to the frame width (from CHANGE_FRAME_SIZE_1). */
2150 xassert (matrix_dim.width == FRAME_COLS (f)
2151 && matrix_dim.height == FRAME_LINES (f));
2153 /* Pointers to glyph memory in glyph rows are exchanged during
2154 the update phase of redisplay, which means in general that a
2155 frame's current matrix consists of pointers into both the
2156 desired and current glyph pool of the frame. Adjusting a
2157 matrix sets the frame matrix up so that pointers are all into
2158 the same pool. If we want to preserve glyph contents of the
2159 current matrix over a call to adjust_glyph_matrix, we must
2160 make a copy of the current glyphs, and restore the current
2161 matrix' contents from that copy. */
2162 if (display_completed
2163 && !FRAME_GARBAGED_P (f)
2164 && matrix_dim.width == f->current_matrix->matrix_w
2165 && matrix_dim.height == f->current_matrix->matrix_h
2166 /* For some reason, the frame glyph matrix gets corrupted if
2167 any of the windows contain margins. I haven't been able
2168 to hunt down the reason, but for the moment this prevents
2169 the problem from manifesting. -- cyd */
2170 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2172 struct glyph_matrix *copy = save_current_matrix (f);
2173 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2174 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2175 restore_current_matrix (f, copy);
2176 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2178 else
2180 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2181 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2182 SET_FRAME_GARBAGED (f);
2188 /* Allocate/reallocate glyph matrices of a single frame F for
2189 window-based redisplay. */
2191 static void
2192 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2194 xassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2196 /* Allocate/reallocate window matrices. */
2197 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2199 #ifdef HAVE_X_WINDOWS
2200 /* Allocate/ reallocate matrices of the dummy window used to display
2201 the menu bar under X when no X toolkit support is available. */
2202 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2204 /* Allocate a dummy window if not already done. */
2205 struct window *w;
2206 if (NILP (f->menu_bar_window))
2208 f->menu_bar_window = make_window ();
2209 w = XWINDOW (f->menu_bar_window);
2210 XSETFRAME (w->frame, f);
2211 w->pseudo_window_p = 1;
2213 else
2214 w = XWINDOW (f->menu_bar_window);
2216 /* Set window dimensions to frame dimensions and allocate or
2217 adjust glyph matrices of W. */
2218 XSETFASTINT (w->top_line, 0);
2219 XSETFASTINT (w->left_col, 0);
2220 XSETFASTINT (w->total_lines, FRAME_MENU_BAR_LINES (f));
2221 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2222 allocate_matrices_for_window_redisplay (w);
2224 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2225 #endif /* HAVE_X_WINDOWS */
2227 #ifndef USE_GTK
2229 /* Allocate/ reallocate matrices of the tool bar window. If we
2230 don't have a tool bar window yet, make one. */
2231 struct window *w;
2232 if (NILP (f->tool_bar_window))
2234 f->tool_bar_window = make_window ();
2235 w = XWINDOW (f->tool_bar_window);
2236 XSETFRAME (w->frame, f);
2237 w->pseudo_window_p = 1;
2239 else
2240 w = XWINDOW (f->tool_bar_window);
2242 XSETFASTINT (w->top_line, FRAME_MENU_BAR_LINES (f));
2243 XSETFASTINT (w->left_col, 0);
2244 XSETFASTINT (w->total_lines, FRAME_TOOL_BAR_LINES (f));
2245 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2246 allocate_matrices_for_window_redisplay (w);
2248 #endif
2252 /* Adjust/ allocate message buffer of frame F.
2254 Note that the message buffer is never freed. Since I could not
2255 find a free in 19.34, I assume that freeing it would be
2256 problematic in some way and don't do it either.
2258 (Implementation note: It should be checked if we can free it
2259 eventually without causing trouble). */
2261 static void
2262 adjust_frame_message_buffer (struct frame *f)
2264 ptrdiff_t size = FRAME_MESSAGE_BUF_SIZE (f) + 1;
2266 if (FRAME_MESSAGE_BUF (f))
2268 char *buffer = FRAME_MESSAGE_BUF (f);
2269 char *new_buffer = (char *) xrealloc (buffer, size);
2270 FRAME_MESSAGE_BUF (f) = new_buffer;
2272 else
2273 FRAME_MESSAGE_BUF (f) = (char *) xmalloc (size);
2277 /* Re-allocate buffer for decode_mode_spec on frame F. */
2279 static void
2280 adjust_decode_mode_spec_buffer (struct frame *f)
2282 f->decode_mode_spec_buffer
2283 = (char *) xrealloc (f->decode_mode_spec_buffer,
2284 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2289 /**********************************************************************
2290 Freeing Glyph Matrices
2291 **********************************************************************/
2293 /* Free glyph memory for a frame F. F may be null. This function can
2294 be called for the same frame more than once. The root window of
2295 F may be nil when this function is called. This is the case when
2296 the function is called when F is destroyed. */
2298 void
2299 free_glyphs (struct frame *f)
2301 if (f && f->glyphs_initialized_p)
2303 /* Block interrupt input so that we don't get surprised by an X
2304 event while we're in an inconsistent state. */
2305 BLOCK_INPUT;
2306 f->glyphs_initialized_p = 0;
2308 /* Release window sub-matrices. */
2309 if (!NILP (f->root_window))
2310 free_window_matrices (XWINDOW (f->root_window));
2312 /* Free the dummy window for menu bars without X toolkit and its
2313 glyph matrices. */
2314 if (!NILP (f->menu_bar_window))
2316 struct window *w = XWINDOW (f->menu_bar_window);
2317 free_glyph_matrix (w->desired_matrix);
2318 free_glyph_matrix (w->current_matrix);
2319 w->desired_matrix = w->current_matrix = NULL;
2320 f->menu_bar_window = Qnil;
2323 /* Free the tool bar window and its glyph matrices. */
2324 if (!NILP (f->tool_bar_window))
2326 struct window *w = XWINDOW (f->tool_bar_window);
2327 free_glyph_matrix (w->desired_matrix);
2328 free_glyph_matrix (w->current_matrix);
2329 w->desired_matrix = w->current_matrix = NULL;
2330 f->tool_bar_window = Qnil;
2333 /* Release frame glyph matrices. Reset fields to zero in
2334 case we are called a second time. */
2335 if (f->desired_matrix)
2337 free_glyph_matrix (f->desired_matrix);
2338 free_glyph_matrix (f->current_matrix);
2339 f->desired_matrix = f->current_matrix = NULL;
2342 /* Release glyph pools. */
2343 if (f->desired_pool)
2345 free_glyph_pool (f->desired_pool);
2346 free_glyph_pool (f->current_pool);
2347 f->desired_pool = f->current_pool = NULL;
2350 UNBLOCK_INPUT;
2355 /* Free glyph sub-matrices in the window tree rooted at W. This
2356 function may be called with a null pointer, and it may be called on
2357 the same tree more than once. */
2359 void
2360 free_window_matrices (struct window *w)
2362 while (w)
2364 if (!NILP (w->hchild))
2365 free_window_matrices (XWINDOW (w->hchild));
2366 else if (!NILP (w->vchild))
2367 free_window_matrices (XWINDOW (w->vchild));
2368 else
2370 /* This is a leaf window. Free its memory and reset fields
2371 to zero in case this function is called a second time for
2372 W. */
2373 free_glyph_matrix (w->current_matrix);
2374 free_glyph_matrix (w->desired_matrix);
2375 w->current_matrix = w->desired_matrix = NULL;
2378 /* Next window on same level. */
2379 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2384 /* Check glyph memory leaks. This function is called from
2385 shut_down_emacs. Note that frames are not destroyed when Emacs
2386 exits. We therefore free all glyph memory for all active frames
2387 explicitly and check that nothing is left allocated. */
2389 void
2390 check_glyph_memory (void)
2392 Lisp_Object tail, frame;
2394 /* Free glyph memory for all frames. */
2395 FOR_EACH_FRAME (tail, frame)
2396 free_glyphs (XFRAME (frame));
2398 /* Check that nothing is left allocated. */
2399 if (glyph_matrix_count)
2400 abort ();
2401 if (glyph_pool_count)
2402 abort ();
2407 /**********************************************************************
2408 Building a Frame Matrix
2409 **********************************************************************/
2411 /* Most of the redisplay code works on glyph matrices attached to
2412 windows. This is a good solution most of the time, but it is not
2413 suitable for terminal code. Terminal output functions cannot rely
2414 on being able to set an arbitrary terminal window. Instead they
2415 must be provided with a view of the whole frame, i.e. the whole
2416 screen. We build such a view by constructing a frame matrix from
2417 window matrices in this section.
2419 Windows that must be updated have their must_be_update_p flag set.
2420 For all such windows, their desired matrix is made part of the
2421 desired frame matrix. For other windows, their current matrix is
2422 made part of the desired frame matrix.
2424 +-----------------+----------------+
2425 | desired | desired |
2426 | | |
2427 +-----------------+----------------+
2428 | current |
2430 +----------------------------------+
2432 Desired window matrices can be made part of the frame matrix in a
2433 cheap way: We exploit the fact that the desired frame matrix and
2434 desired window matrices share their glyph memory. This is not
2435 possible for current window matrices. Their glyphs are copied to
2436 the desired frame matrix. The latter is equivalent to
2437 preserve_other_columns in the old redisplay.
2439 Used glyphs counters for frame matrix rows are the result of adding
2440 up glyph lengths of the window matrices. A line in the frame
2441 matrix is enabled, if a corresponding line in a window matrix is
2442 enabled.
2444 After building the desired frame matrix, it will be passed to
2445 terminal code, which will manipulate both the desired and current
2446 frame matrix. Changes applied to the frame's current matrix have
2447 to be visible in current window matrices afterwards, of course.
2449 This problem is solved like this:
2451 1. Window and frame matrices share glyphs. Window matrices are
2452 constructed in a way that their glyph contents ARE the glyph
2453 contents needed in a frame matrix. Thus, any modification of
2454 glyphs done in terminal code will be reflected in window matrices
2455 automatically.
2457 2. Exchanges of rows in a frame matrix done by terminal code are
2458 intercepted by hook functions so that corresponding row operations
2459 on window matrices can be performed. This is necessary because we
2460 use pointers to glyphs in glyph row structures. To satisfy the
2461 assumption of point 1 above that glyphs are updated implicitly in
2462 window matrices when they are manipulated via the frame matrix,
2463 window and frame matrix must of course agree where to find the
2464 glyphs for their rows. Possible manipulations that must be
2465 mirrored are assignments of rows of the desired frame matrix to the
2466 current frame matrix and scrolling the current frame matrix. */
2468 /* Build frame F's desired matrix from window matrices. Only windows
2469 which have the flag must_be_updated_p set have to be updated. Menu
2470 bar lines of a frame are not covered by window matrices, so make
2471 sure not to touch them in this function. */
2473 static void
2474 build_frame_matrix (struct frame *f)
2476 int i;
2478 /* F must have a frame matrix when this function is called. */
2479 xassert (!FRAME_WINDOW_P (f));
2481 /* Clear all rows in the frame matrix covered by window matrices.
2482 Menu bar lines are not covered by windows. */
2483 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2484 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2486 /* Build the matrix by walking the window tree. */
2487 build_frame_matrix_from_window_tree (f->desired_matrix,
2488 XWINDOW (FRAME_ROOT_WINDOW (f)));
2492 /* Walk a window tree, building a frame matrix MATRIX from window
2493 matrices. W is the root of a window tree. */
2495 static void
2496 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2498 while (w)
2500 if (!NILP (w->hchild))
2501 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2502 else if (!NILP (w->vchild))
2503 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2504 else
2505 build_frame_matrix_from_leaf_window (matrix, w);
2507 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2512 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2513 desired frame matrix built. W is a leaf window whose desired or
2514 current matrix is to be added to FRAME_MATRIX. W's flag
2515 must_be_updated_p determines which matrix it contributes to
2516 FRAME_MATRIX. If must_be_updated_p is non-zero, W's desired matrix
2517 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2518 Adding a desired matrix means setting up used counters and such in
2519 frame rows, while adding a current window matrix to FRAME_MATRIX
2520 means copying glyphs. The latter case corresponds to
2521 preserve_other_columns in the old redisplay. */
2523 static void
2524 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2526 struct glyph_matrix *window_matrix;
2527 int window_y, frame_y;
2528 /* If non-zero, a glyph to insert at the right border of W. */
2529 GLYPH right_border_glyph;
2531 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2533 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2534 if (w->must_be_updated_p)
2536 window_matrix = w->desired_matrix;
2538 /* Decide whether we want to add a vertical border glyph. */
2539 if (!WINDOW_RIGHTMOST_P (w))
2541 struct Lisp_Char_Table *dp = window_display_table (w);
2542 Lisp_Object gc;
2544 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2545 if (dp
2546 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc))
2547 && GLYPH_CODE_CHAR_VALID_P (gc))
2549 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2550 spec_glyph_lookup_face (w, &right_border_glyph);
2553 if (GLYPH_FACE (right_border_glyph) <= 0)
2554 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2557 else
2558 window_matrix = w->current_matrix;
2560 /* For all rows in the window matrix and corresponding rows in the
2561 frame matrix. */
2562 window_y = 0;
2563 frame_y = window_matrix->matrix_y;
2564 while (window_y < window_matrix->nrows)
2566 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2567 struct glyph_row *window_row = window_matrix->rows + window_y;
2568 int current_row_p = window_matrix == w->current_matrix;
2570 /* Fill up the frame row with spaces up to the left margin of the
2571 window row. */
2572 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2574 /* Fill up areas in the window matrix row with spaces. */
2575 fill_up_glyph_row_with_spaces (window_row);
2577 /* If only part of W's desired matrix has been built, and
2578 window_row wasn't displayed, use the corresponding current
2579 row instead. */
2580 if (window_matrix == w->desired_matrix
2581 && !window_row->enabled_p)
2583 window_row = w->current_matrix->rows + window_y;
2584 current_row_p = 1;
2587 if (current_row_p)
2589 /* Copy window row to frame row. */
2590 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2591 window_row->glyphs[0],
2592 window_matrix->matrix_w * sizeof (struct glyph));
2594 else
2596 xassert (window_row->enabled_p);
2598 /* Only when a desired row has been displayed, we want
2599 the corresponding frame row to be updated. */
2600 frame_row->enabled_p = 1;
2602 /* Maybe insert a vertical border between horizontally adjacent
2603 windows. */
2604 if (GLYPH_CHAR (right_border_glyph) != 0)
2606 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2607 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2610 #if GLYPH_DEBUG
2611 /* Window row window_y must be a slice of frame row
2612 frame_y. */
2613 xassert (glyph_row_slice_p (window_row, frame_row));
2615 /* If rows are in sync, we don't have to copy glyphs because
2616 frame and window share glyphs. */
2618 strcpy (w->current_matrix->method, w->desired_matrix->method);
2619 add_window_display_history (w, w->current_matrix->method, 0);
2620 #endif
2623 /* Set number of used glyphs in the frame matrix. Since we fill
2624 up with spaces, and visit leaf windows from left to right it
2625 can be done simply. */
2626 frame_row->used[TEXT_AREA]
2627 = window_matrix->matrix_x + window_matrix->matrix_w;
2629 /* Next row. */
2630 ++window_y;
2631 ++frame_y;
2635 /* Given a user-specified glyph, possibly including a Lisp-level face
2636 ID, return a glyph that has a realized face ID.
2637 This is used for glyphs displayed specially and not part of the text;
2638 for instance, vertical separators, truncation markers, etc. */
2640 void
2641 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2643 int lface_id = GLYPH_FACE (*glyph);
2644 /* Convert the glyph's specified face to a realized (cache) face. */
2645 if (lface_id > 0)
2647 int face_id = merge_faces (XFRAME (w->frame),
2648 Qt, lface_id, DEFAULT_FACE_ID);
2649 SET_GLYPH_FACE (*glyph, face_id);
2653 /* Add spaces to a glyph row ROW in a window matrix.
2655 Each row has the form:
2657 +---------+-----------------------------+------------+
2658 | left | text | right |
2659 +---------+-----------------------------+------------+
2661 Left and right marginal areas are optional. This function adds
2662 spaces to areas so that there are no empty holes between areas.
2663 In other words: If the right area is not empty, the text area
2664 is filled up with spaces up to the right area. If the text area
2665 is not empty, the left area is filled up.
2667 To be called for frame-based redisplay, only. */
2669 static void
2670 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2672 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2673 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2674 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2678 /* Fill area AREA of glyph row ROW with spaces. To be called for
2679 frame-based redisplay only. */
2681 static void
2682 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2684 if (row->glyphs[area] < row->glyphs[area + 1])
2686 struct glyph *end = row->glyphs[area + 1];
2687 struct glyph *text = row->glyphs[area] + row->used[area];
2689 while (text < end)
2690 *text++ = space_glyph;
2691 row->used[area] = text - row->glyphs[area];
2696 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2697 reached. In frame matrices only one area, TEXT_AREA, is used. */
2699 static void
2700 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2702 int i = row->used[TEXT_AREA];
2703 struct glyph *glyph = row->glyphs[TEXT_AREA];
2705 while (i < upto)
2706 glyph[i++] = space_glyph;
2708 row->used[TEXT_AREA] = i;
2713 /**********************************************************************
2714 Mirroring operations on frame matrices in window matrices
2715 **********************************************************************/
2717 /* Set frame being updated via frame-based redisplay to F. This
2718 function must be called before updates to make explicit that we are
2719 working on frame matrices or not. */
2721 static inline void
2722 set_frame_matrix_frame (struct frame *f)
2724 frame_matrix_frame = f;
2728 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2729 DESIRED_MATRIX is the desired matrix corresponding to
2730 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2731 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2732 frame_matrix_frame is non-null, this indicates that the exchange is
2733 done in frame matrices, and that we have to perform analogous
2734 operations in window matrices of frame_matrix_frame. */
2736 static inline void
2737 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2739 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2740 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2741 int mouse_face_p = current_row->mouse_face_p;
2743 /* Do current_row = desired_row. This exchanges glyph pointers
2744 between both rows, and does a structure assignment otherwise. */
2745 assign_row (current_row, desired_row);
2747 /* Enable current_row to mark it as valid. */
2748 current_row->enabled_p = 1;
2749 current_row->mouse_face_p = mouse_face_p;
2751 /* If we are called on frame matrices, perform analogous operations
2752 for window matrices. */
2753 if (frame_matrix_frame)
2754 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2758 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2759 W's frame which has been made current (by swapping pointers between
2760 current and desired matrix). Perform analogous operations in the
2761 matrices of leaf windows in the window tree rooted at W. */
2763 static void
2764 mirror_make_current (struct window *w, int frame_row)
2766 while (w)
2768 if (!NILP (w->hchild))
2769 mirror_make_current (XWINDOW (w->hchild), frame_row);
2770 else if (!NILP (w->vchild))
2771 mirror_make_current (XWINDOW (w->vchild), frame_row);
2772 else
2774 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2775 here because the checks performed in debug mode there
2776 will not allow the conversion. */
2777 int row = frame_row - w->desired_matrix->matrix_y;
2779 /* If FRAME_ROW is within W, assign the desired row to the
2780 current row (exchanging glyph pointers). */
2781 if (row >= 0 && row < w->desired_matrix->matrix_h)
2783 struct glyph_row *current_row
2784 = MATRIX_ROW (w->current_matrix, row);
2785 struct glyph_row *desired_row
2786 = MATRIX_ROW (w->desired_matrix, row);
2788 if (desired_row->enabled_p)
2789 assign_row (current_row, desired_row);
2790 else
2791 swap_glyph_pointers (desired_row, current_row);
2792 current_row->enabled_p = 1;
2794 /* Set the Y coordinate of the mode/header line's row.
2795 It is needed in draw_row_with_mouse_face to find the
2796 screen coordinates. (Window-based redisplay sets
2797 this in update_window, but no one seems to do that
2798 for frame-based redisplay.) */
2799 if (current_row->mode_line_p)
2800 current_row->y = row;
2804 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2809 /* Perform row dance after scrolling. We are working on the range of
2810 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2811 including) in MATRIX. COPY_FROM is a vector containing, for each
2812 row I in the range 0 <= I < NLINES, the index of the original line
2813 to move to I. This index is relative to the row range, i.e. 0 <=
2814 index < NLINES. RETAINED_P is a vector containing zero for each
2815 row 0 <= I < NLINES which is empty.
2817 This function is called from do_scrolling and do_direct_scrolling. */
2819 void
2820 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2821 int *copy_from, char *retained_p)
2823 /* A copy of original rows. */
2824 struct glyph_row *old_rows;
2826 /* Rows to assign to. */
2827 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2829 int i;
2831 /* Make a copy of the original rows. */
2832 old_rows = (struct glyph_row *) alloca (nlines * sizeof *old_rows);
2833 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2835 /* Assign new rows, maybe clear lines. */
2836 for (i = 0; i < nlines; ++i)
2838 int enabled_before_p = new_rows[i].enabled_p;
2840 xassert (i + unchanged_at_top < matrix->nrows);
2841 xassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2842 new_rows[i] = old_rows[copy_from[i]];
2843 new_rows[i].enabled_p = enabled_before_p;
2845 /* RETAINED_P is zero for empty lines. */
2846 if (!retained_p[copy_from[i]])
2847 new_rows[i].enabled_p = 0;
2850 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2851 if (frame_matrix_frame)
2852 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2853 unchanged_at_top, nlines, copy_from, retained_p);
2857 /* Synchronize glyph pointers in the current matrix of window W with
2858 the current frame matrix. */
2860 static void
2861 sync_window_with_frame_matrix_rows (struct window *w)
2863 struct frame *f = XFRAME (w->frame);
2864 struct glyph_row *window_row, *window_row_end, *frame_row;
2865 int left, right, x, width;
2867 /* Preconditions: W must be a leaf window on a tty frame. */
2868 xassert (NILP (w->hchild) && NILP (w->vchild));
2869 xassert (!FRAME_WINDOW_P (f));
2871 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2872 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2873 x = w->current_matrix->matrix_x;
2874 width = w->current_matrix->matrix_w;
2876 window_row = w->current_matrix->rows;
2877 window_row_end = window_row + w->current_matrix->nrows;
2878 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2880 for (; window_row < window_row_end; ++window_row, ++frame_row)
2882 window_row->glyphs[LEFT_MARGIN_AREA]
2883 = frame_row->glyphs[0] + x;
2884 window_row->glyphs[TEXT_AREA]
2885 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2886 window_row->glyphs[LAST_AREA]
2887 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2888 window_row->glyphs[RIGHT_MARGIN_AREA]
2889 = window_row->glyphs[LAST_AREA] - right;
2894 /* Return the window in the window tree rooted in W containing frame
2895 row ROW. Value is null if none is found. */
2897 static struct window *
2898 frame_row_to_window (struct window *w, int row)
2900 struct window *found = NULL;
2902 while (w && !found)
2904 if (!NILP (w->hchild))
2905 found = frame_row_to_window (XWINDOW (w->hchild), row);
2906 else if (!NILP (w->vchild))
2907 found = frame_row_to_window (XWINDOW (w->vchild), row);
2908 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2909 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2910 found = w;
2912 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2915 return found;
2919 /* Perform a line dance in the window tree rooted at W, after
2920 scrolling a frame matrix in mirrored_line_dance.
2922 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2923 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2924 COPY_FROM is a vector containing, for each row I in the range 0 <=
2925 I < NLINES, the index of the original line to move to I. This
2926 index is relative to the row range, i.e. 0 <= index < NLINES.
2927 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2928 which is empty. */
2930 static void
2931 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2933 while (w)
2935 if (!NILP (w->hchild))
2936 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
2937 nlines, copy_from, retained_p);
2938 else if (!NILP (w->vchild))
2939 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
2940 nlines, copy_from, retained_p);
2941 else
2943 /* W is a leaf window, and we are working on its current
2944 matrix m. */
2945 struct glyph_matrix *m = w->current_matrix;
2946 int i, sync_p = 0;
2947 struct glyph_row *old_rows;
2949 /* Make a copy of the original rows of matrix m. */
2950 old_rows = (struct glyph_row *) alloca (m->nrows * sizeof *old_rows);
2951 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2953 for (i = 0; i < nlines; ++i)
2955 /* Frame relative line assigned to. */
2956 int frame_to = i + unchanged_at_top;
2958 /* Frame relative line assigned. */
2959 int frame_from = copy_from[i] + unchanged_at_top;
2961 /* Window relative line assigned to. */
2962 int window_to = frame_to - m->matrix_y;
2964 /* Window relative line assigned. */
2965 int window_from = frame_from - m->matrix_y;
2967 /* Is assigned line inside window? */
2968 int from_inside_window_p
2969 = window_from >= 0 && window_from < m->matrix_h;
2971 /* Is assigned to line inside window? */
2972 int to_inside_window_p
2973 = window_to >= 0 && window_to < m->matrix_h;
2975 if (from_inside_window_p && to_inside_window_p)
2977 /* Enabled setting before assignment. */
2978 int enabled_before_p;
2980 /* Do the assignment. The enabled_p flag is saved
2981 over the assignment because the old redisplay did
2982 that. */
2983 enabled_before_p = m->rows[window_to].enabled_p;
2984 m->rows[window_to] = old_rows[window_from];
2985 m->rows[window_to].enabled_p = enabled_before_p;
2987 /* If frame line is empty, window line is empty, too. */
2988 if (!retained_p[copy_from[i]])
2989 m->rows[window_to].enabled_p = 0;
2991 else if (to_inside_window_p)
2993 /* A copy between windows. This is an infrequent
2994 case not worth optimizing. */
2995 struct frame *f = XFRAME (w->frame);
2996 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2997 struct window *w2;
2998 struct glyph_matrix *m2;
2999 int m2_from;
3001 w2 = frame_row_to_window (root, frame_from);
3002 /* ttn@surf.glug.org: when enabling menu bar using `emacs
3003 -nw', FROM_FRAME sometimes has no associated window.
3004 This check avoids a segfault if W2 is null. */
3005 if (w2)
3007 m2 = w2->current_matrix;
3008 m2_from = frame_from - m2->matrix_y;
3009 copy_row_except_pointers (m->rows + window_to,
3010 m2->rows + m2_from);
3012 /* If frame line is empty, window line is empty, too. */
3013 if (!retained_p[copy_from[i]])
3014 m->rows[window_to].enabled_p = 0;
3016 sync_p = 1;
3018 else if (from_inside_window_p)
3019 sync_p = 1;
3022 /* If there was a copy between windows, make sure glyph
3023 pointers are in sync with the frame matrix. */
3024 if (sync_p)
3025 sync_window_with_frame_matrix_rows (w);
3027 /* Check that no pointers are lost. */
3028 CHECK_MATRIX (m);
3031 /* Next window on same level. */
3032 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3037 #if GLYPH_DEBUG
3039 /* Check that window and frame matrices agree about their
3040 understanding where glyphs of the rows are to find. For each
3041 window in the window tree rooted at W, check that rows in the
3042 matrices of leaf window agree with their frame matrices about
3043 glyph pointers. */
3045 static void
3046 check_window_matrix_pointers (struct window *w)
3048 while (w)
3050 if (!NILP (w->hchild))
3051 check_window_matrix_pointers (XWINDOW (w->hchild));
3052 else if (!NILP (w->vchild))
3053 check_window_matrix_pointers (XWINDOW (w->vchild));
3054 else
3056 struct frame *f = XFRAME (w->frame);
3057 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
3058 check_matrix_pointers (w->current_matrix, f->current_matrix);
3061 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3066 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
3067 a window and FRAME_MATRIX is the corresponding frame matrix. For
3068 each row in WINDOW_MATRIX check that it's a slice of the
3069 corresponding frame row. If it isn't, abort. */
3071 static void
3072 check_matrix_pointers (struct glyph_matrix *window_matrix,
3073 struct glyph_matrix *frame_matrix)
3075 /* Row number in WINDOW_MATRIX. */
3076 int i = 0;
3078 /* Row number corresponding to I in FRAME_MATRIX. */
3079 int j = window_matrix->matrix_y;
3081 /* For all rows check that the row in the window matrix is a
3082 slice of the row in the frame matrix. If it isn't we didn't
3083 mirror an operation on the frame matrix correctly. */
3084 while (i < window_matrix->nrows)
3086 if (!glyph_row_slice_p (window_matrix->rows + i,
3087 frame_matrix->rows + j))
3088 abort ();
3089 ++i, ++j;
3093 #endif /* GLYPH_DEBUG != 0 */
3097 /**********************************************************************
3098 VPOS and HPOS translations
3099 **********************************************************************/
3101 #if GLYPH_DEBUG
3103 /* Translate vertical position VPOS which is relative to window W to a
3104 vertical position relative to W's frame. */
3106 static int
3107 window_to_frame_vpos (struct window *w, int vpos)
3109 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3110 xassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
3111 vpos += WINDOW_TOP_EDGE_LINE (w);
3112 xassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
3113 return vpos;
3117 /* Translate horizontal position HPOS which is relative to window W to
3118 a horizontal position relative to W's frame. */
3120 static int
3121 window_to_frame_hpos (struct window *w, int hpos)
3123 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3124 hpos += WINDOW_LEFT_EDGE_COL (w);
3125 return hpos;
3128 #endif /* GLYPH_DEBUG */
3132 /**********************************************************************
3133 Redrawing Frames
3134 **********************************************************************/
3136 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
3137 doc: /* Clear frame FRAME and output again what is supposed to appear on it. */)
3138 (Lisp_Object frame)
3140 struct frame *f;
3142 CHECK_LIVE_FRAME (frame);
3143 f = XFRAME (frame);
3145 /* Ignore redraw requests, if frame has no glyphs yet.
3146 (Implementation note: It still has to be checked why we are
3147 called so early here). */
3148 if (!glyphs_initialized_initially_p)
3149 return Qnil;
3151 update_begin (f);
3152 #ifdef MSDOS
3153 if (FRAME_MSDOS_P (f))
3154 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3155 #endif
3156 clear_frame (f);
3157 clear_current_matrices (f);
3158 update_end (f);
3159 if (FRAME_TERMCAP_P (f))
3160 fflush (FRAME_TTY (f)->output);
3161 windows_or_buffers_changed++;
3162 /* Mark all windows as inaccurate, so that every window will have
3163 its redisplay done. */
3164 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3165 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3166 f->garbaged = 0;
3167 return Qnil;
3171 /* Redraw frame F. This is nothing more than a call to the Lisp
3172 function redraw-frame. */
3174 void
3175 redraw_frame (struct frame *f)
3177 Lisp_Object frame;
3178 XSETFRAME (frame, f);
3179 Fredraw_frame (frame);
3183 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3184 doc: /* Clear and redisplay all visible frames. */)
3185 (void)
3187 Lisp_Object tail, frame;
3189 FOR_EACH_FRAME (tail, frame)
3190 if (FRAME_VISIBLE_P (XFRAME (frame)))
3191 Fredraw_frame (frame);
3193 return Qnil;
3198 /***********************************************************************
3199 Frame Update
3200 ***********************************************************************/
3202 /* Update frame F based on the data in desired matrices.
3204 If FORCE_P is non-zero, don't let redisplay be stopped by detecting
3205 pending input. If INHIBIT_HAIRY_ID_P is non-zero, don't try
3206 scrolling.
3208 Value is non-zero if redisplay was stopped due to pending input. */
3211 update_frame (struct frame *f, int force_p, int inhibit_hairy_id_p)
3213 /* 1 means display has been paused because of pending input. */
3214 int paused_p;
3215 struct window *root_window = XWINDOW (f->root_window);
3217 if (redisplay_dont_pause)
3218 force_p = 1;
3219 #if PERIODIC_PREEMPTION_CHECKING
3220 else if (NILP (Vredisplay_preemption_period))
3221 force_p = 1;
3222 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3224 EMACS_TIME tm;
3225 double p = XFLOATINT (Vredisplay_preemption_period);
3226 int sec, usec;
3228 if (detect_input_pending_ignore_squeezables ())
3230 paused_p = 1;
3231 goto do_pause;
3234 sec = (int) p;
3235 usec = (p - sec) * 1000000;
3237 EMACS_GET_TIME (tm);
3238 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3239 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3241 #endif
3243 if (FRAME_WINDOW_P (f))
3245 /* We are working on window matrix basis. All windows whose
3246 flag must_be_updated_p is set have to be updated. */
3248 /* Record that we are not working on frame matrices. */
3249 set_frame_matrix_frame (NULL);
3251 /* Update all windows in the window tree of F, maybe stopping
3252 when pending input is detected. */
3253 update_begin (f);
3255 /* Update the menu bar on X frames that don't have toolkit
3256 support. */
3257 if (WINDOWP (f->menu_bar_window))
3258 update_window (XWINDOW (f->menu_bar_window), 1);
3260 /* Update the tool-bar window, if present. */
3261 if (WINDOWP (f->tool_bar_window))
3263 struct window *w = XWINDOW (f->tool_bar_window);
3265 /* Update tool-bar window. */
3266 if (w->must_be_updated_p)
3268 Lisp_Object tem;
3270 update_window (w, 1);
3271 w->must_be_updated_p = 0;
3273 /* Swap tool-bar strings. We swap because we want to
3274 reuse strings. */
3275 tem = f->current_tool_bar_string;
3276 f->current_tool_bar_string = f->desired_tool_bar_string;
3277 f->desired_tool_bar_string = tem;
3282 /* Update windows. */
3283 paused_p = update_window_tree (root_window, force_p);
3284 update_end (f);
3286 /* This flush is a performance bottleneck under X,
3287 and it doesn't seem to be necessary anyway (in general).
3288 It is necessary when resizing the window with the mouse, or
3289 at least the fringes are not redrawn in a timely manner. ++kfs */
3290 if (f->force_flush_display_p)
3292 FRAME_RIF (f)->flush_display (f);
3293 f->force_flush_display_p = 0;
3296 else
3298 /* We are working on frame matrix basis. Set the frame on whose
3299 frame matrix we operate. */
3300 set_frame_matrix_frame (f);
3302 /* Build F's desired matrix from window matrices. */
3303 build_frame_matrix (f);
3305 /* Update the display */
3306 update_begin (f);
3307 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3308 update_end (f);
3310 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3312 if (FRAME_TTY (f)->termscript)
3313 fflush (FRAME_TTY (f)->termscript);
3314 if (FRAME_TERMCAP_P (f))
3315 fflush (FRAME_TTY (f)->output);
3318 /* Check window matrices for lost pointers. */
3319 #if GLYPH_DEBUG
3320 check_window_matrix_pointers (root_window);
3321 add_frame_display_history (f, paused_p);
3322 #endif
3325 #if PERIODIC_PREEMPTION_CHECKING
3326 do_pause:
3327 #endif
3328 /* Reset flags indicating that a window should be updated. */
3329 set_window_update_flags (root_window, 0);
3331 display_completed = !paused_p;
3332 return paused_p;
3337 /************************************************************************
3338 Window-based updates
3339 ************************************************************************/
3341 /* Perform updates in window tree rooted at W. FORCE_P non-zero means
3342 don't stop updating when input is pending. */
3344 static int
3345 update_window_tree (struct window *w, int force_p)
3347 int paused_p = 0;
3349 while (w && !paused_p)
3351 if (!NILP (w->hchild))
3352 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3353 else if (!NILP (w->vchild))
3354 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3355 else if (w->must_be_updated_p)
3356 paused_p |= update_window (w, force_p);
3358 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3361 return paused_p;
3365 /* Update window W if its flag must_be_updated_p is non-zero. If
3366 FORCE_P is non-zero, don't stop updating if input is pending. */
3368 void
3369 update_single_window (struct window *w, int force_p)
3371 if (w->must_be_updated_p)
3373 struct frame *f = XFRAME (WINDOW_FRAME (w));
3375 /* Record that this is not a frame-based redisplay. */
3376 set_frame_matrix_frame (NULL);
3378 if (redisplay_dont_pause)
3379 force_p = 1;
3380 #if PERIODIC_PREEMPTION_CHECKING
3381 else if (NILP (Vredisplay_preemption_period))
3382 force_p = 1;
3383 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3385 EMACS_TIME tm;
3386 double p = XFLOATINT (Vredisplay_preemption_period);
3387 int sec, usec;
3389 sec = (int) p;
3390 usec = (p - sec) * 1000000;
3392 EMACS_GET_TIME (tm);
3393 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3394 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3396 #endif
3398 /* Update W. */
3399 update_begin (f);
3400 update_window (w, force_p);
3401 update_end (f);
3403 /* Reset flag in W. */
3404 w->must_be_updated_p = 0;
3408 #ifdef HAVE_WINDOW_SYSTEM
3410 /* Redraw lines from the current matrix of window W that are
3411 overlapped by other rows. YB is bottom-most y-position in W. */
3413 static void
3414 redraw_overlapped_rows (struct window *w, int yb)
3416 int i;
3417 struct frame *f = XFRAME (WINDOW_FRAME (w));
3419 /* If rows overlapping others have been changed, the rows being
3420 overlapped have to be redrawn. This won't draw lines that have
3421 already been drawn in update_window_line because overlapped_p in
3422 desired rows is 0, so after row assignment overlapped_p in
3423 current rows is 0. */
3424 for (i = 0; i < w->current_matrix->nrows; ++i)
3426 struct glyph_row *row = w->current_matrix->rows + i;
3428 if (!row->enabled_p)
3429 break;
3430 else if (row->mode_line_p)
3431 continue;
3433 if (row->overlapped_p)
3435 enum glyph_row_area area;
3437 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3439 updated_row = row;
3440 updated_area = area;
3441 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3442 area == TEXT_AREA ? row->x : 0);
3443 if (row->used[area])
3444 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3445 row->used[area]);
3446 FRAME_RIF (f)->clear_end_of_line (-1);
3449 row->overlapped_p = 0;
3452 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3453 break;
3458 /* Redraw lines from the current matrix of window W that overlap
3459 others. YB is bottom-most y-position in W. */
3461 static void
3462 redraw_overlapping_rows (struct window *w, int yb)
3464 int i, bottom_y;
3465 struct glyph_row *row;
3466 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3468 for (i = 0; i < w->current_matrix->nrows; ++i)
3470 row = w->current_matrix->rows + i;
3472 if (!row->enabled_p)
3473 break;
3474 else if (row->mode_line_p)
3475 continue;
3477 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3479 if (row->overlapping_p)
3481 int overlaps = 0;
3483 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3484 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3485 overlaps |= OVERLAPS_PRED;
3486 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3487 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3488 overlaps |= OVERLAPS_SUCC;
3490 if (overlaps)
3492 if (row->used[LEFT_MARGIN_AREA])
3493 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3495 if (row->used[TEXT_AREA])
3496 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3498 if (row->used[RIGHT_MARGIN_AREA])
3499 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3501 /* Record in neighbor rows that ROW overwrites part of
3502 their display. */
3503 if (overlaps & OVERLAPS_PRED)
3504 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3505 if (overlaps & OVERLAPS_SUCC)
3506 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3510 if (bottom_y >= yb)
3511 break;
3515 #endif /* HAVE_WINDOW_SYSTEM */
3518 #if defined GLYPH_DEBUG && 0
3520 /* Check that no row in the current matrix of window W is enabled
3521 which is below what's displayed in the window. */
3523 static void
3524 check_current_matrix_flags (struct window *w)
3526 int last_seen_p = 0;
3527 int i, yb = window_text_bottom_y (w);
3529 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3531 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3532 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3533 last_seen_p = 1;
3534 else if (last_seen_p && row->enabled_p)
3535 abort ();
3539 #endif /* GLYPH_DEBUG */
3542 /* Update display of window W. FORCE_P non-zero means that we should
3543 not stop when detecting pending input. */
3545 static int
3546 update_window (struct window *w, int force_p)
3548 struct glyph_matrix *desired_matrix = w->desired_matrix;
3549 int paused_p;
3550 #if !PERIODIC_PREEMPTION_CHECKING
3551 int preempt_count = baud_rate / 2400 + 1;
3552 #endif
3553 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3554 #if GLYPH_DEBUG
3555 /* Check that W's frame doesn't have glyph matrices. */
3556 xassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3557 #endif
3559 /* Check pending input the first time so that we can quickly return. */
3560 #if !PERIODIC_PREEMPTION_CHECKING
3561 if (!force_p)
3562 detect_input_pending_ignore_squeezables ();
3563 #endif
3565 /* If forced to complete the update, or if no input is pending, do
3566 the update. */
3567 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3569 struct glyph_row *row, *end;
3570 struct glyph_row *mode_line_row;
3571 struct glyph_row *header_line_row;
3572 int yb, changed_p = 0, mouse_face_overwritten_p = 0;
3573 #if ! PERIODIC_PREEMPTION_CHECKING
3574 int n_updated = 0;
3575 #endif
3577 rif->update_window_begin_hook (w);
3578 yb = window_text_bottom_y (w);
3580 /* If window has a header line, update it before everything else.
3581 Adjust y-positions of other rows by the header line height. */
3582 row = desired_matrix->rows;
3583 end = row + desired_matrix->nrows - 1;
3585 if (row->mode_line_p)
3587 header_line_row = row;
3588 ++row;
3590 else
3591 header_line_row = NULL;
3593 /* Update the mode line, if necessary. */
3594 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3595 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3597 mode_line_row->y = yb;
3598 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3599 desired_matrix),
3600 &mouse_face_overwritten_p);
3603 /* Find first enabled row. Optimizations in redisplay_internal
3604 may lead to an update with only one row enabled. There may
3605 be also completely empty matrices. */
3606 while (row < end && !row->enabled_p)
3607 ++row;
3609 /* Try reusing part of the display by copying. */
3610 if (row < end && !desired_matrix->no_scrolling_p)
3612 int rc = scrolling_window (w, header_line_row != NULL);
3613 if (rc < 0)
3615 /* All rows were found to be equal. */
3616 paused_p = 0;
3617 goto set_cursor;
3619 else if (rc > 0)
3621 /* We've scrolled the display. */
3622 force_p = 1;
3623 changed_p = 1;
3627 /* Update the rest of the lines. */
3628 for (; row < end && (force_p || !input_pending); ++row)
3629 if (row->enabled_p)
3631 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3632 int i;
3634 /* We'll have to play a little bit with when to
3635 detect_input_pending. If it's done too often,
3636 scrolling large windows with repeated scroll-up
3637 commands will too quickly pause redisplay. */
3638 #if PERIODIC_PREEMPTION_CHECKING
3639 if (!force_p)
3641 EMACS_TIME tm, dif;
3642 EMACS_GET_TIME (tm);
3643 EMACS_SUB_TIME (dif, preemption_next_check, tm);
3644 if (EMACS_TIME_NEG_P (dif))
3646 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3647 if (detect_input_pending_ignore_squeezables ())
3648 break;
3651 #else
3652 if (!force_p && ++n_updated % preempt_count == 0)
3653 detect_input_pending_ignore_squeezables ();
3654 #endif
3655 changed_p |= update_window_line (w, vpos,
3656 &mouse_face_overwritten_p);
3658 /* Mark all rows below the last visible one in the current
3659 matrix as invalid. This is necessary because of
3660 variable line heights. Consider the case of three
3661 successive redisplays, where the first displays 5
3662 lines, the second 3 lines, and the third 5 lines again.
3663 If the second redisplay wouldn't mark rows in the
3664 current matrix invalid, the third redisplay might be
3665 tempted to optimize redisplay based on lines displayed
3666 in the first redisplay. */
3667 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3668 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3669 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3672 /* Was display preempted? */
3673 paused_p = row < end;
3675 set_cursor:
3677 /* Update the header line after scrolling because a new header
3678 line would otherwise overwrite lines at the top of the window
3679 that can be scrolled. */
3680 if (header_line_row && header_line_row->enabled_p)
3682 header_line_row->y = 0;
3683 update_window_line (w, 0, &mouse_face_overwritten_p);
3686 /* Fix the appearance of overlapping/overlapped rows. */
3687 if (!paused_p && !w->pseudo_window_p)
3689 #ifdef HAVE_WINDOW_SYSTEM
3690 if (changed_p && rif->fix_overlapping_area)
3692 redraw_overlapped_rows (w, yb);
3693 redraw_overlapping_rows (w, yb);
3695 #endif
3697 /* Make cursor visible at cursor position of W. */
3698 set_window_cursor_after_update (w);
3700 #if 0 /* Check that current matrix invariants are satisfied. This is
3701 for debugging only. See the comment of check_matrix_invariants. */
3702 IF_DEBUG (check_matrix_invariants (w));
3703 #endif
3706 #if GLYPH_DEBUG
3707 /* Remember the redisplay method used to display the matrix. */
3708 strcpy (w->current_matrix->method, w->desired_matrix->method);
3709 #endif
3711 #ifdef HAVE_WINDOW_SYSTEM
3712 update_window_fringes (w, 0);
3713 #endif
3715 /* End the update of window W. Don't set the cursor if we
3716 paused updating the display because in this case,
3717 set_window_cursor_after_update hasn't been called, and
3718 output_cursor doesn't contain the cursor location. */
3719 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3721 else
3722 paused_p = 1;
3724 #if GLYPH_DEBUG
3725 /* check_current_matrix_flags (w); */
3726 add_window_display_history (w, w->current_matrix->method, paused_p);
3727 #endif
3729 clear_glyph_matrix (desired_matrix);
3731 return paused_p;
3735 /* Update the display of area AREA in window W, row number VPOS.
3736 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3738 static void
3739 update_marginal_area (struct window *w, int area, int vpos)
3741 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3742 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3744 /* Let functions in xterm.c know what area subsequent X positions
3745 will be relative to. */
3746 updated_area = area;
3748 /* Set cursor to start of glyphs, write them, and clear to the end
3749 of the area. I don't think that something more sophisticated is
3750 necessary here, since marginal areas will not be the default. */
3751 rif->cursor_to (vpos, 0, desired_row->y, 0);
3752 if (desired_row->used[area])
3753 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3754 rif->clear_end_of_line (-1);
3758 /* Update the display of the text area of row VPOS in window W.
3759 Value is non-zero if display has changed. */
3761 static int
3762 update_text_area (struct window *w, int vpos)
3764 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3765 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3766 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3767 int changed_p = 0;
3769 /* Let functions in xterm.c know what area subsequent X positions
3770 will be relative to. */
3771 updated_area = TEXT_AREA;
3773 /* If rows are at different X or Y, or rows have different height,
3774 or the current row is marked invalid, write the entire line. */
3775 if (!current_row->enabled_p
3776 || desired_row->y != current_row->y
3777 || desired_row->ascent != current_row->ascent
3778 || desired_row->phys_ascent != current_row->phys_ascent
3779 || desired_row->phys_height != current_row->phys_height
3780 || desired_row->visible_height != current_row->visible_height
3781 || current_row->overlapped_p
3782 /* This next line is necessary for correctly redrawing
3783 mouse-face areas after scrolling and other operations.
3784 However, it causes excessive flickering when mouse is moved
3785 across the mode line. Luckily, turning it off for the mode
3786 line doesn't seem to hurt anything. -- cyd.
3787 But it is still needed for the header line. -- kfs. */
3788 || (current_row->mouse_face_p
3789 && !(current_row->mode_line_p && vpos > 0))
3790 || current_row->x != desired_row->x)
3792 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
3794 if (desired_row->used[TEXT_AREA])
3795 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3796 desired_row->used[TEXT_AREA]);
3798 /* Clear to end of window. */
3799 rif->clear_end_of_line (-1);
3800 changed_p = 1;
3802 /* This erases the cursor. We do this here because
3803 notice_overwritten_cursor cannot easily check this, which
3804 might indicate that the whole functionality of
3805 notice_overwritten_cursor would better be implemented here.
3806 On the other hand, we need notice_overwritten_cursor as long
3807 as mouse highlighting is done asynchronously outside of
3808 redisplay. */
3809 if (vpos == w->phys_cursor.vpos)
3810 w->phys_cursor_on_p = 0;
3812 else
3814 int stop, i, x;
3815 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3816 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3817 int overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3818 int desired_stop_pos = desired_row->used[TEXT_AREA];
3819 int abort_skipping = 0;
3821 /* If the desired row extends its face to the text area end, and
3822 unless the current row also does so at the same position,
3823 make sure we write at least one glyph, so that the face
3824 extension actually takes place. */
3825 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3826 && (desired_stop_pos < current_row->used[TEXT_AREA]
3827 || (desired_stop_pos == current_row->used[TEXT_AREA]
3828 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3829 --desired_stop_pos;
3831 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3832 i = 0;
3833 x = desired_row->x;
3835 /* Loop over glyphs that current and desired row may have
3836 in common. */
3837 while (i < stop)
3839 int can_skip_p = !abort_skipping;
3841 /* Skip over glyphs that both rows have in common. These
3842 don't have to be written. We can't skip if the last
3843 current glyph overlaps the glyph to its right. For
3844 example, consider a current row of `if ' with the `f' in
3845 Courier bold so that it overlaps the ` ' to its right.
3846 If the desired row is ` ', we would skip over the space
3847 after the `if' and there would remain a pixel from the
3848 `f' on the screen. */
3849 if (overlapping_glyphs_p && i > 0)
3851 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3852 int left, right;
3854 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3855 &left, &right);
3856 can_skip_p = (right == 0 && !abort_skipping);
3859 if (can_skip_p)
3861 int start_hpos = i;
3863 while (i < stop
3864 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3866 x += desired_glyph->pixel_width;
3867 ++desired_glyph, ++current_glyph, ++i;
3870 /* Consider the case that the current row contains "xxx
3871 ppp ggg" in italic Courier font, and the desired row
3872 is "xxx ggg". The character `p' has lbearing, `g'
3873 has not. The loop above will stop in front of the
3874 first `p' in the current row. If we would start
3875 writing glyphs there, we wouldn't erase the lbearing
3876 of the `p'. The rest of the lbearing problem is then
3877 taken care of by draw_glyphs. */
3878 if (overlapping_glyphs_p
3879 && i > 0
3880 && i < current_row->used[TEXT_AREA]
3881 && (current_row->used[TEXT_AREA]
3882 != desired_row->used[TEXT_AREA]))
3884 int left, right;
3886 rif->get_glyph_overhangs (current_glyph, XFRAME (w->frame),
3887 &left, &right);
3888 while (left > 0 && i > 0)
3890 --i, --desired_glyph, --current_glyph;
3891 x -= desired_glyph->pixel_width;
3892 left -= desired_glyph->pixel_width;
3895 /* Abort the skipping algorithm if we end up before
3896 our starting point, to avoid looping (bug#1070).
3897 This can happen when the lbearing is larger than
3898 the pixel width. */
3899 abort_skipping = (i < start_hpos);
3903 /* Try to avoid writing the entire rest of the desired row
3904 by looking for a resync point. This mainly prevents
3905 mode line flickering in the case the mode line is in
3906 fixed-pitch font, which it usually will be. */
3907 if (i < desired_row->used[TEXT_AREA])
3909 int start_x = x, start_hpos = i;
3910 struct glyph *start = desired_glyph;
3911 int current_x = x;
3912 int skip_first_p = !can_skip_p;
3914 /* Find the next glyph that's equal again. */
3915 while (i < stop
3916 && (skip_first_p
3917 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3918 && x == current_x)
3920 x += desired_glyph->pixel_width;
3921 current_x += current_glyph->pixel_width;
3922 ++desired_glyph, ++current_glyph, ++i;
3923 skip_first_p = 0;
3926 if (i == start_hpos || x != current_x)
3928 i = start_hpos;
3929 x = start_x;
3930 desired_glyph = start;
3931 break;
3934 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
3935 rif->write_glyphs (start, i - start_hpos);
3936 changed_p = 1;
3940 /* Write the rest. */
3941 if (i < desired_row->used[TEXT_AREA])
3943 rif->cursor_to (vpos, i, desired_row->y, x);
3944 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
3945 changed_p = 1;
3948 /* Maybe clear to end of line. */
3949 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3951 /* If new row extends to the end of the text area, nothing
3952 has to be cleared, if and only if we did a write_glyphs
3953 above. This is made sure by setting desired_stop_pos
3954 appropriately above. */
3955 xassert (i < desired_row->used[TEXT_AREA]
3956 || ((desired_row->used[TEXT_AREA]
3957 == current_row->used[TEXT_AREA])
3958 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3960 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3962 /* If old row extends to the end of the text area, clear. */
3963 if (i >= desired_row->used[TEXT_AREA])
3964 rif->cursor_to (vpos, i, desired_row->y,
3965 desired_row->pixel_width);
3966 rif->clear_end_of_line (-1);
3967 changed_p = 1;
3969 else if (desired_row->pixel_width < current_row->pixel_width)
3971 /* Otherwise clear to the end of the old row. Everything
3972 after that position should be clear already. */
3973 int xlim;
3975 if (i >= desired_row->used[TEXT_AREA])
3976 rif->cursor_to (vpos, i, desired_row->y,
3977 desired_row->pixel_width);
3979 /* If cursor is displayed at the end of the line, make sure
3980 it's cleared. Nowadays we don't have a phys_cursor_glyph
3981 with which to erase the cursor (because this method
3982 doesn't work with lbearing/rbearing), so we must do it
3983 this way. */
3984 if (vpos == w->phys_cursor.vpos
3985 && (desired_row->reversed_p
3986 ? (w->phys_cursor.hpos < 0)
3987 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3989 w->phys_cursor_on_p = 0;
3990 xlim = -1;
3992 else
3993 xlim = current_row->pixel_width;
3994 rif->clear_end_of_line (xlim);
3995 changed_p = 1;
3999 return changed_p;
4003 /* Update row VPOS in window W. Value is non-zero if display has been
4004 changed. */
4006 static int
4007 update_window_line (struct window *w, int vpos, int *mouse_face_overwritten_p)
4009 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
4010 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
4011 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4012 int changed_p = 0;
4014 /* Set the row being updated. This is important to let xterm.c
4015 know what line height values are in effect. */
4016 updated_row = desired_row;
4018 /* A row can be completely invisible in case a desired matrix was
4019 built with a vscroll and then make_cursor_line_fully_visible shifts
4020 the matrix. Make sure to make such rows current anyway, since
4021 we need the correct y-position, for example, in the current matrix. */
4022 if (desired_row->mode_line_p
4023 || desired_row->visible_height > 0)
4025 xassert (desired_row->enabled_p);
4027 /* Update display of the left margin area, if there is one. */
4028 if (!desired_row->full_width_p
4029 && !NILP (w->left_margin_cols))
4031 changed_p = 1;
4032 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
4035 /* Update the display of the text area. */
4036 if (update_text_area (w, vpos))
4038 changed_p = 1;
4039 if (current_row->mouse_face_p)
4040 *mouse_face_overwritten_p = 1;
4043 /* Update display of the right margin area, if there is one. */
4044 if (!desired_row->full_width_p
4045 && !NILP (w->right_margin_cols))
4047 changed_p = 1;
4048 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
4051 /* Draw truncation marks etc. */
4052 if (!current_row->enabled_p
4053 || desired_row->y != current_row->y
4054 || desired_row->visible_height != current_row->visible_height
4055 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
4056 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
4057 || current_row->redraw_fringe_bitmaps_p
4058 || desired_row->mode_line_p != current_row->mode_line_p
4059 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
4060 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
4061 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
4062 rif->after_update_window_line_hook (desired_row);
4065 /* Update current_row from desired_row. */
4066 make_current (w->desired_matrix, w->current_matrix, vpos);
4067 updated_row = NULL;
4068 return changed_p;
4072 /* Set the cursor after an update of window W. This function may only
4073 be called from update_window. */
4075 static void
4076 set_window_cursor_after_update (struct window *w)
4078 struct frame *f = XFRAME (w->frame);
4079 struct redisplay_interface *rif = FRAME_RIF (f);
4080 int cx, cy, vpos, hpos;
4082 /* Not intended for frame matrix updates. */
4083 xassert (FRAME_WINDOW_P (f));
4085 if (cursor_in_echo_area
4086 && !NILP (echo_area_buffer[0])
4087 /* If we are showing a message instead of the mini-buffer,
4088 show the cursor for the message instead. */
4089 && XWINDOW (minibuf_window) == w
4090 && EQ (minibuf_window, echo_area_window)
4091 /* These cases apply only to the frame that contains
4092 the active mini-buffer window. */
4093 && FRAME_HAS_MINIBUF_P (f)
4094 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4096 cx = cy = vpos = hpos = 0;
4098 if (cursor_in_echo_area >= 0)
4100 /* If the mini-buffer is several lines high, find the last
4101 line that has any text on it. Note: either all lines
4102 are enabled or none. Otherwise we wouldn't be able to
4103 determine Y. */
4104 struct glyph_row *row, *last_row;
4105 struct glyph *glyph;
4106 int yb = window_text_bottom_y (w);
4108 last_row = NULL;
4109 row = w->current_matrix->rows;
4110 while (row->enabled_p
4111 && (last_row == NULL
4112 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
4114 if (row->used[TEXT_AREA]
4115 && row->glyphs[TEXT_AREA][0].charpos >= 0)
4116 last_row = row;
4117 ++row;
4120 if (last_row)
4122 struct glyph *start = last_row->glyphs[TEXT_AREA];
4123 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
4125 while (last > start && last->charpos < 0)
4126 --last;
4128 for (glyph = start; glyph < last; ++glyph)
4130 cx += glyph->pixel_width;
4131 ++hpos;
4134 cy = last_row->y;
4135 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
4139 else
4141 cx = w->cursor.x;
4142 cy = w->cursor.y;
4143 hpos = w->cursor.hpos;
4144 vpos = w->cursor.vpos;
4147 /* Window cursor can be out of sync for horizontally split windows. */
4148 hpos = max (-1, hpos); /* -1 is for when cursor is on the left fringe */
4149 hpos = min (w->current_matrix->matrix_w - 1, hpos);
4150 vpos = max (0, vpos);
4151 vpos = min (w->current_matrix->nrows - 1, vpos);
4152 rif->cursor_to (vpos, hpos, cy, cx);
4156 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4157 tree rooted at W. */
4159 void
4160 set_window_update_flags (struct window *w, int on_p)
4162 while (w)
4164 if (!NILP (w->hchild))
4165 set_window_update_flags (XWINDOW (w->hchild), on_p);
4166 else if (!NILP (w->vchild))
4167 set_window_update_flags (XWINDOW (w->vchild), on_p);
4168 else
4169 w->must_be_updated_p = on_p;
4171 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4177 /***********************************************************************
4178 Window-Based Scrolling
4179 ***********************************************************************/
4181 /* Structure describing rows in scrolling_window. */
4183 struct row_entry
4185 /* Number of occurrences of this row in desired and current matrix. */
4186 int old_uses, new_uses;
4188 /* Vpos of row in new matrix. */
4189 int new_line_number;
4191 /* Bucket index of this row_entry in the hash table row_table. */
4192 ptrdiff_t bucket;
4194 /* The row described by this entry. */
4195 struct glyph_row *row;
4197 /* Hash collision chain. */
4198 struct row_entry *next;
4201 /* A pool to allocate row_entry structures from, and the size of the
4202 pool. The pool is reallocated in scrolling_window when we find
4203 that we need a larger one. */
4205 static struct row_entry *row_entry_pool;
4206 static ptrdiff_t row_entry_pool_size;
4208 /* Index of next free entry in row_entry_pool. */
4210 static ptrdiff_t row_entry_idx;
4212 /* The hash table used during scrolling, and the table's size. This
4213 table is used to quickly identify equal rows in the desired and
4214 current matrix. */
4216 static struct row_entry **row_table;
4217 static ptrdiff_t row_table_size;
4219 /* Vectors of pointers to row_entry structures belonging to the
4220 current and desired matrix, and the size of the vectors. */
4222 static struct row_entry **old_lines, **new_lines;
4223 static ptrdiff_t old_lines_size, new_lines_size;
4225 /* A pool to allocate run structures from, and its size. */
4227 static struct run *run_pool;
4228 static ptrdiff_t runs_size;
4230 /* A vector of runs of lines found during scrolling. */
4232 static struct run **runs;
4234 /* Add glyph row ROW to the scrolling hash table. */
4236 static inline struct row_entry *
4237 add_row_entry (struct glyph_row *row)
4239 struct row_entry *entry;
4240 ptrdiff_t i = row->hash % row_table_size;
4242 entry = row_table[i];
4243 xassert (entry || verify_row_hash (row));
4244 while (entry && !row_equal_p (entry->row, row, 1))
4245 entry = entry->next;
4247 if (entry == NULL)
4249 entry = row_entry_pool + row_entry_idx++;
4250 entry->row = row;
4251 entry->old_uses = entry->new_uses = 0;
4252 entry->new_line_number = 0;
4253 entry->bucket = i;
4254 entry->next = row_table[i];
4255 row_table[i] = entry;
4258 return entry;
4262 /* Try to reuse part of the current display of W by scrolling lines.
4263 HEADER_LINE_P non-zero means W has a header line.
4265 The algorithm is taken from Communications of the ACM, Apr78 "A
4266 Technique for Isolating Differences Between Files." It should take
4267 O(N) time.
4269 A short outline of the steps of the algorithm
4271 1. Skip lines equal at the start and end of both matrices.
4273 2. Enter rows in the current and desired matrix into a symbol
4274 table, counting how often they appear in both matrices.
4276 3. Rows that appear exactly once in both matrices serve as anchors,
4277 i.e. we assume that such lines are likely to have been moved.
4279 4. Starting from anchor lines, extend regions to be scrolled both
4280 forward and backward.
4282 Value is
4284 -1 if all rows were found to be equal.
4285 0 to indicate that we did not scroll the display, or
4286 1 if we did scroll. */
4288 static int
4289 scrolling_window (struct window *w, int header_line_p)
4291 struct glyph_matrix *desired_matrix = w->desired_matrix;
4292 struct glyph_matrix *current_matrix = w->current_matrix;
4293 int yb = window_text_bottom_y (w);
4294 ptrdiff_t i;
4295 int j, first_old, first_new, last_old, last_new;
4296 int nruns, run_idx;
4297 ptrdiff_t n;
4298 struct row_entry *entry;
4299 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4301 /* Skip over rows equal at the start. */
4302 for (i = header_line_p ? 1 : 0; i < current_matrix->nrows - 1; ++i)
4304 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4305 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4307 if (c->enabled_p
4308 && d->enabled_p
4309 && !d->redraw_fringe_bitmaps_p
4310 && c->y == d->y
4311 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4312 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4313 && row_equal_p (c, d, 1))
4315 assign_row (c, d);
4316 d->enabled_p = 0;
4318 else
4319 break;
4322 /* Give up if some rows in the desired matrix are not enabled. */
4323 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4324 return -1;
4326 first_old = first_new = i;
4328 /* Set last_new to the index + 1 of the row that reaches the
4329 bottom boundary in the desired matrix. Give up if we find a
4330 disabled row before we reach the bottom boundary. */
4331 i = first_new + 1;
4332 while (i < desired_matrix->nrows - 1)
4334 int bottom;
4336 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4337 return 0;
4338 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4339 if (bottom <= yb)
4340 ++i;
4341 if (bottom >= yb)
4342 break;
4345 last_new = i;
4347 /* Set last_old to the index + 1 of the row that reaches the bottom
4348 boundary in the current matrix. We don't look at the enabled
4349 flag here because we plan to reuse part of the display even if
4350 other parts are disabled. */
4351 i = first_old + 1;
4352 while (i < current_matrix->nrows - 1)
4354 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4355 if (bottom <= yb)
4356 ++i;
4357 if (bottom >= yb)
4358 break;
4361 last_old = i;
4363 /* Skip over rows equal at the bottom. */
4364 i = last_new;
4365 j = last_old;
4366 while (i - 1 > first_new
4367 && j - 1 > first_old
4368 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4369 && (MATRIX_ROW (current_matrix, j - 1)->y
4370 == MATRIX_ROW (desired_matrix, i - 1)->y)
4371 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4372 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4373 MATRIX_ROW (current_matrix, j - 1), 1))
4374 --i, --j;
4375 last_new = i;
4376 last_old = j;
4378 /* Nothing to do if all rows are equal. */
4379 if (last_new == first_new)
4380 return 0;
4382 /* Check for integer overflow in size calculation.
4384 If next_almost_prime checks (N) for divisibility by 2..10, then
4385 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4386 So, set next_almost_prime_increment_max to 10.
4388 It's just a coincidence that next_almost_prime_increment_max ==
4389 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4390 13, then next_almost_prime_increment_max would be 14, e.g.,
4391 because next_almost_prime (113) would be 127. */
4393 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4394 enum { next_almost_prime_increment_max = 10 };
4395 ptrdiff_t row_table_max =
4396 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4397 - next_almost_prime_increment_max);
4398 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4399 if (current_nrows_max < current_matrix->nrows)
4400 memory_full (SIZE_MAX);
4403 /* Reallocate vectors, tables etc. if necessary. */
4405 if (current_matrix->nrows > old_lines_size)
4406 old_lines = xpalloc (old_lines, &old_lines_size,
4407 current_matrix->nrows - old_lines_size,
4408 INT_MAX, sizeof *old_lines);
4410 if (desired_matrix->nrows > new_lines_size)
4411 new_lines = xpalloc (new_lines, &new_lines_size,
4412 desired_matrix->nrows - new_lines_size,
4413 INT_MAX, sizeof *new_lines);
4415 n = desired_matrix->nrows;
4416 n += current_matrix->nrows;
4417 if (row_table_size < 3 * n)
4419 ptrdiff_t size = next_almost_prime (3 * n);
4420 row_table = xnrealloc (row_table, size, sizeof *row_table);
4421 row_table_size = size;
4422 memset (row_table, 0, size * sizeof *row_table);
4425 if (n > row_entry_pool_size)
4426 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4427 n - row_entry_pool_size,
4428 -1, sizeof *row_entry_pool);
4430 if (desired_matrix->nrows > runs_size)
4432 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4433 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4434 runs_size = desired_matrix->nrows;
4437 nruns = run_idx = 0;
4438 row_entry_idx = 0;
4440 /* Add rows from the current and desired matrix to the hash table
4441 row_hash_table to be able to find equal ones quickly. */
4443 for (i = first_old; i < last_old; ++i)
4445 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4447 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4448 old_lines[i] = entry;
4449 ++entry->old_uses;
4451 else
4452 old_lines[i] = NULL;
4455 for (i = first_new; i < last_new; ++i)
4457 xassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4458 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4459 ++entry->new_uses;
4460 entry->new_line_number = i;
4461 new_lines[i] = entry;
4464 /* Identify moves based on lines that are unique and equal
4465 in both matrices. */
4466 for (i = first_old; i < last_old;)
4467 if (old_lines[i]
4468 && old_lines[i]->old_uses == 1
4469 && old_lines[i]->new_uses == 1)
4471 int p, q;
4472 int new_line = old_lines[i]->new_line_number;
4473 struct run *run = run_pool + run_idx++;
4475 /* Record move. */
4476 run->current_vpos = i;
4477 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4478 run->desired_vpos = new_line;
4479 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4480 run->nrows = 1;
4481 run->height = MATRIX_ROW (current_matrix, i)->height;
4483 /* Extend backward. */
4484 p = i - 1;
4485 q = new_line - 1;
4486 while (p > first_old
4487 && q > first_new
4488 && old_lines[p] == new_lines[q])
4490 int h = MATRIX_ROW (current_matrix, p)->height;
4491 --run->current_vpos;
4492 --run->desired_vpos;
4493 ++run->nrows;
4494 run->height += h;
4495 run->desired_y -= h;
4496 run->current_y -= h;
4497 --p, --q;
4500 /* Extend forward. */
4501 p = i + 1;
4502 q = new_line + 1;
4503 while (p < last_old
4504 && q < last_new
4505 && old_lines[p] == new_lines[q])
4507 int h = MATRIX_ROW (current_matrix, p)->height;
4508 ++run->nrows;
4509 run->height += h;
4510 ++p, ++q;
4513 /* Insert run into list of all runs. Order runs by copied
4514 pixel lines. Note that we record runs that don't have to
4515 be copied because they are already in place. This is done
4516 because we can avoid calling update_window_line in this
4517 case. */
4518 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4520 for (q = nruns; q > p; --q)
4521 runs[q] = runs[q - 1];
4522 runs[p] = run;
4523 ++nruns;
4525 i += run->nrows;
4527 else
4528 ++i;
4530 /* Do the moves. Do it in a way that we don't overwrite something
4531 we want to copy later on. This is not solvable in general
4532 because there is only one display and we don't have a way to
4533 exchange areas on this display. Example:
4535 +-----------+ +-----------+
4536 | A | | B |
4537 +-----------+ --> +-----------+
4538 | B | | A |
4539 +-----------+ +-----------+
4541 Instead, prefer bigger moves, and invalidate moves that would
4542 copy from where we copied to. */
4544 for (i = 0; i < nruns; ++i)
4545 if (runs[i]->nrows > 0)
4547 struct run *r = runs[i];
4549 /* Copy on the display. */
4550 if (r->current_y != r->desired_y)
4552 rif->clear_window_mouse_face (w);
4553 rif->scroll_run_hook (w, r);
4555 /* Invalidate runs that copy from where we copied to. */
4556 for (j = i + 1; j < nruns; ++j)
4558 struct run *p = runs[j];
4560 if ((p->current_y >= r->desired_y
4561 && p->current_y < r->desired_y + r->height)
4562 || (p->current_y + p->height >= r->desired_y
4563 && (p->current_y + p->height
4564 < r->desired_y + r->height)))
4565 p->nrows = 0;
4569 /* Assign matrix rows. */
4570 for (j = 0; j < r->nrows; ++j)
4572 struct glyph_row *from, *to;
4573 int to_overlapped_p;
4575 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4576 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4577 to_overlapped_p = to->overlapped_p;
4578 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4579 assign_row (to, from);
4580 to->enabled_p = 1, from->enabled_p = 0;
4581 to->overlapped_p = to_overlapped_p;
4585 /* Clear the hash table, for the next time. */
4586 for (i = 0; i < row_entry_idx; ++i)
4587 row_table[row_entry_pool[i].bucket] = NULL;
4589 /* Value is 1 to indicate that we scrolled the display. */
4590 return 0 < nruns;
4595 /************************************************************************
4596 Frame-Based Updates
4597 ************************************************************************/
4599 /* Update the desired frame matrix of frame F.
4601 FORCE_P non-zero means that the update should not be stopped by
4602 pending input. INHIBIT_HAIRY_ID_P non-zero means that scrolling
4603 should not be tried.
4605 Value is non-zero if update was stopped due to pending input. */
4607 static int
4608 update_frame_1 (struct frame *f, int force_p, int inhibit_id_p)
4610 /* Frame matrices to work on. */
4611 struct glyph_matrix *current_matrix = f->current_matrix;
4612 struct glyph_matrix *desired_matrix = f->desired_matrix;
4613 int i;
4614 int pause_p;
4615 int preempt_count = baud_rate / 2400 + 1;
4617 xassert (current_matrix && desired_matrix);
4619 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4620 calculate_costs (f);
4622 if (preempt_count <= 0)
4623 preempt_count = 1;
4625 #if !PERIODIC_PREEMPTION_CHECKING
4626 if (!force_p && detect_input_pending_ignore_squeezables ())
4628 pause_p = 1;
4629 goto do_pause;
4631 #endif
4633 /* If we cannot insert/delete lines, it's no use trying it. */
4634 if (!FRAME_LINE_INS_DEL_OK (f))
4635 inhibit_id_p = 1;
4637 /* See if any of the desired lines are enabled; don't compute for
4638 i/d line if just want cursor motion. */
4639 for (i = 0; i < desired_matrix->nrows; i++)
4640 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4641 break;
4643 /* Try doing i/d line, if not yet inhibited. */
4644 if (!inhibit_id_p && i < desired_matrix->nrows)
4645 force_p |= scrolling (f);
4647 /* Update the individual lines as needed. Do bottom line first. */
4648 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4649 update_frame_line (f, desired_matrix->nrows - 1);
4651 /* Now update the rest of the lines. */
4652 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4654 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4656 if (FRAME_TERMCAP_P (f))
4658 /* Flush out every so many lines.
4659 Also flush out if likely to have more than 1k buffered
4660 otherwise. I'm told that some telnet connections get
4661 really screwed by more than 1k output at once. */
4662 FILE *display_output = FRAME_TTY (f)->output;
4663 if (display_output)
4665 int outq = PENDING_OUTPUT_COUNT (display_output);
4666 if (outq > 900
4667 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4669 fflush (display_output);
4670 if (preempt_count == 1)
4672 #ifdef EMACS_OUTQSIZE
4673 if (EMACS_OUTQSIZE (0, &outq) < 0)
4674 /* Probably not a tty. Ignore the error and reset
4675 the outq count. */
4676 outq = PENDING_OUTPUT_COUNT (FRAME_TTY (f->output));
4677 #endif
4678 outq *= 10;
4679 if (baud_rate <= outq && baud_rate > 0)
4680 sleep (outq / baud_rate);
4686 #if PERIODIC_PREEMPTION_CHECKING
4687 if (!force_p)
4689 EMACS_TIME tm, dif;
4690 EMACS_GET_TIME (tm);
4691 EMACS_SUB_TIME (dif, preemption_next_check, tm);
4692 if (EMACS_TIME_NEG_P (dif))
4694 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
4695 if (detect_input_pending_ignore_squeezables ())
4696 break;
4699 #else
4700 if (!force_p && (i - 1) % preempt_count == 0)
4701 detect_input_pending_ignore_squeezables ();
4702 #endif
4704 update_frame_line (f, i);
4708 pause_p = (i < FRAME_LINES (f) - 1) ? i : 0;
4710 /* Now just clean up termcap drivers and set cursor, etc. */
4711 if (!pause_p)
4713 if ((cursor_in_echo_area
4714 /* If we are showing a message instead of the mini-buffer,
4715 show the cursor for the message instead of for the
4716 (now hidden) mini-buffer contents. */
4717 || (EQ (minibuf_window, selected_window)
4718 && EQ (minibuf_window, echo_area_window)
4719 && !NILP (echo_area_buffer[0])))
4720 /* These cases apply only to the frame that contains
4721 the active mini-buffer window. */
4722 && FRAME_HAS_MINIBUF_P (f)
4723 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4725 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4726 int row, col;
4728 if (cursor_in_echo_area < 0)
4730 /* Negative value of cursor_in_echo_area means put
4731 cursor at beginning of line. */
4732 row = top;
4733 col = 0;
4735 else
4737 /* Positive value of cursor_in_echo_area means put
4738 cursor at the end of the prompt. If the mini-buffer
4739 is several lines high, find the last line that has
4740 any text on it. */
4741 row = FRAME_LINES (f);
4744 --row;
4745 col = 0;
4747 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4749 /* Frame rows are filled up with spaces that
4750 must be ignored here. */
4751 struct glyph_row *r = MATRIX_ROW (current_matrix,
4752 row);
4753 struct glyph *start = r->glyphs[TEXT_AREA];
4754 struct glyph *last = start + r->used[TEXT_AREA];
4756 while (last > start
4757 && (last - 1)->charpos < 0)
4758 --last;
4760 col = last - start;
4763 while (row > top && col == 0);
4765 /* Make sure COL is not out of range. */
4766 if (col >= FRAME_CURSOR_X_LIMIT (f))
4768 /* If we have another row, advance cursor into it. */
4769 if (row < FRAME_LINES (f) - 1)
4771 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4772 row++;
4774 /* Otherwise move it back in range. */
4775 else
4776 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4780 cursor_to (f, row, col);
4782 else
4784 /* We have only one cursor on terminal frames. Use it to
4785 display the cursor of the selected window. */
4786 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4787 if (w->cursor.vpos >= 0
4788 /* The cursor vpos may be temporarily out of bounds
4789 in the following situation: There is one window,
4790 with the cursor in the lower half of it. The window
4791 is split, and a message causes a redisplay before
4792 a new cursor position has been computed. */
4793 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4795 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4796 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4798 if (INTEGERP (w->left_margin_cols))
4799 x += XFASTINT (w->left_margin_cols);
4801 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4802 cursor_to (f, y, x);
4807 #if !PERIODIC_PREEMPTION_CHECKING
4808 do_pause:
4809 #endif
4811 clear_desired_matrices (f);
4812 return pause_p;
4816 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4818 static int
4819 scrolling (struct frame *frame)
4821 int unchanged_at_top, unchanged_at_bottom;
4822 int window_size;
4823 int changed_lines;
4824 int *old_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4825 int *new_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4826 int *draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4827 int *old_draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4828 register int i;
4829 int free_at_end_vpos = FRAME_LINES (frame);
4830 struct glyph_matrix *current_matrix = frame->current_matrix;
4831 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4833 if (!current_matrix)
4834 abort ();
4836 /* Compute hash codes of all the lines. Also calculate number of
4837 changed lines, number of unchanged lines at the beginning, and
4838 number of unchanged lines at the end. */
4839 changed_lines = 0;
4840 unchanged_at_top = 0;
4841 unchanged_at_bottom = FRAME_LINES (frame);
4842 for (i = 0; i < FRAME_LINES (frame); i++)
4844 /* Give up on this scrolling if some old lines are not enabled. */
4845 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4846 return 0;
4847 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4848 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4850 /* This line cannot be redrawn, so don't let scrolling mess it. */
4851 new_hash[i] = old_hash[i];
4852 #define INFINITY 1000000 /* Taken from scroll.c */
4853 draw_cost[i] = INFINITY;
4855 else
4857 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4858 draw_cost[i] = line_draw_cost (desired_matrix, i);
4861 if (old_hash[i] != new_hash[i])
4863 changed_lines++;
4864 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4866 else if (i == unchanged_at_top)
4867 unchanged_at_top++;
4868 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4871 /* If changed lines are few, don't allow preemption, don't scroll. */
4872 if ((!FRAME_SCROLL_REGION_OK (frame)
4873 && changed_lines < baud_rate / 2400)
4874 || unchanged_at_bottom == FRAME_LINES (frame))
4875 return 1;
4877 window_size = (FRAME_LINES (frame) - unchanged_at_top
4878 - unchanged_at_bottom);
4880 if (FRAME_SCROLL_REGION_OK (frame))
4881 free_at_end_vpos -= unchanged_at_bottom;
4882 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4883 free_at_end_vpos = -1;
4885 /* If large window, fast terminal and few lines in common between
4886 current frame and desired frame, don't bother with i/d calc. */
4887 if (!FRAME_SCROLL_REGION_OK (frame)
4888 && window_size >= 18 && baud_rate > 2400
4889 && (window_size >=
4890 10 * scrolling_max_lines_saved (unchanged_at_top,
4891 FRAME_LINES (frame) - unchanged_at_bottom,
4892 old_hash, new_hash, draw_cost)))
4893 return 0;
4895 if (window_size < 2)
4896 return 0;
4898 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4899 draw_cost + unchanged_at_top - 1,
4900 old_draw_cost + unchanged_at_top - 1,
4901 old_hash + unchanged_at_top - 1,
4902 new_hash + unchanged_at_top - 1,
4903 free_at_end_vpos - unchanged_at_top);
4905 return 0;
4909 /* Count the number of blanks at the start of the vector of glyphs R
4910 which is LEN glyphs long. */
4912 static int
4913 count_blanks (struct glyph *r, int len)
4915 int i;
4917 for (i = 0; i < len; ++i)
4918 if (!CHAR_GLYPH_SPACE_P (r[i]))
4919 break;
4921 return i;
4925 /* Count the number of glyphs in common at the start of the glyph
4926 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4927 of STR2. Value is the number of equal glyphs equal at the start. */
4929 static int
4930 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4932 struct glyph *p1 = str1;
4933 struct glyph *p2 = str2;
4935 while (p1 < end1
4936 && p2 < end2
4937 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4938 ++p1, ++p2;
4940 return p1 - str1;
4944 /* Char insertion/deletion cost vector, from term.c */
4946 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4949 /* Perform a frame-based update on line VPOS in frame FRAME. */
4951 static void
4952 update_frame_line (struct frame *f, int vpos)
4954 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4955 int tem;
4956 int osp, nsp, begmatch, endmatch, olen, nlen;
4957 struct glyph_matrix *current_matrix = f->current_matrix;
4958 struct glyph_matrix *desired_matrix = f->desired_matrix;
4959 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4960 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4961 int must_write_whole_line_p;
4962 int write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4963 int colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4964 != FACE_TTY_DEFAULT_BG_COLOR);
4966 if (colored_spaces_p)
4967 write_spaces_p = 1;
4969 /* Current row not enabled means it has unknown contents. We must
4970 write the whole desired line in that case. */
4971 must_write_whole_line_p = !current_row->enabled_p;
4972 if (must_write_whole_line_p)
4974 obody = 0;
4975 olen = 0;
4977 else
4979 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4980 olen = current_row->used[TEXT_AREA];
4982 /* Ignore trailing spaces, if we can. */
4983 if (!write_spaces_p)
4984 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4985 olen--;
4988 current_row->enabled_p = 1;
4989 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4991 /* If desired line is empty, just clear the line. */
4992 if (!desired_row->enabled_p)
4994 nlen = 0;
4995 goto just_erase;
4998 nbody = desired_row->glyphs[TEXT_AREA];
4999 nlen = desired_row->used[TEXT_AREA];
5000 nend = nbody + nlen;
5002 /* If display line has unknown contents, write the whole line. */
5003 if (must_write_whole_line_p)
5005 /* Ignore spaces at the end, if we can. */
5006 if (!write_spaces_p)
5007 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5008 --nlen;
5010 /* Write the contents of the desired line. */
5011 if (nlen)
5013 cursor_to (f, vpos, 0);
5014 write_glyphs (f, nbody, nlen);
5017 /* Don't call clear_end_of_line if we already wrote the whole
5018 line. The cursor will not be at the right margin in that
5019 case but in the line below. */
5020 if (nlen < FRAME_TOTAL_COLS (f))
5022 cursor_to (f, vpos, nlen);
5023 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
5025 else
5026 /* Make sure we are in the right row, otherwise cursor movement
5027 with cmgoto might use `ch' in the wrong row. */
5028 cursor_to (f, vpos, 0);
5030 make_current (desired_matrix, current_matrix, vpos);
5031 return;
5034 /* Pretend trailing spaces are not there at all,
5035 unless for one reason or another we must write all spaces. */
5036 if (!write_spaces_p)
5037 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5038 nlen--;
5040 /* If there's no i/d char, quickly do the best we can without it. */
5041 if (!FRAME_CHAR_INS_DEL_OK (f))
5043 int i, j;
5045 /* Find the first glyph in desired row that doesn't agree with
5046 a glyph in the current row, and write the rest from there on. */
5047 for (i = 0; i < nlen; i++)
5049 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
5051 /* Find the end of the run of different glyphs. */
5052 j = i + 1;
5053 while (j < nlen
5054 && (j >= olen
5055 || !GLYPH_EQUAL_P (nbody + j, obody + j)
5056 || CHAR_GLYPH_PADDING_P (nbody[j])))
5057 ++j;
5059 /* Output this run of non-matching chars. */
5060 cursor_to (f, vpos, i);
5061 write_glyphs (f, nbody + i, j - i);
5062 i = j - 1;
5064 /* Now find the next non-match. */
5068 /* Clear the rest of the line, or the non-clear part of it. */
5069 if (olen > nlen)
5071 cursor_to (f, vpos, nlen);
5072 clear_end_of_line (f, olen);
5075 /* Make current row = desired row. */
5076 make_current (desired_matrix, current_matrix, vpos);
5077 return;
5080 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5081 characters in a row. */
5083 if (!olen)
5085 /* If current line is blank, skip over initial spaces, if
5086 possible, and write the rest. */
5087 if (write_spaces_p)
5088 nsp = 0;
5089 else
5090 nsp = count_blanks (nbody, nlen);
5092 if (nlen > nsp)
5094 cursor_to (f, vpos, nsp);
5095 write_glyphs (f, nbody + nsp, nlen - nsp);
5098 /* Exchange contents between current_frame and new_frame. */
5099 make_current (desired_matrix, current_matrix, vpos);
5100 return;
5103 /* Compute number of leading blanks in old and new contents. */
5104 osp = count_blanks (obody, olen);
5105 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
5107 /* Compute number of matching chars starting with first non-blank. */
5108 begmatch = count_match (obody + osp, obody + olen,
5109 nbody + nsp, nbody + nlen);
5111 /* Spaces in new match implicit space past the end of old. */
5112 /* A bug causing this to be a no-op was fixed in 18.29. */
5113 if (!write_spaces_p && osp + begmatch == olen)
5115 np1 = nbody + nsp;
5116 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
5117 ++begmatch;
5120 /* Avoid doing insert/delete char
5121 just cause number of leading spaces differs
5122 when the following text does not match. */
5123 if (begmatch == 0 && osp != nsp)
5124 osp = nsp = min (osp, nsp);
5126 /* Find matching characters at end of line */
5127 op1 = obody + olen;
5128 np1 = nbody + nlen;
5129 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5130 while (op1 > op2
5131 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
5133 op1--;
5134 np1--;
5136 endmatch = obody + olen - op1;
5138 /* tem gets the distance to insert or delete.
5139 endmatch is how many characters we save by doing so.
5140 Is it worth it? */
5142 tem = (nlen - nsp) - (olen - osp);
5143 if (endmatch && tem
5144 && (!FRAME_CHAR_INS_DEL_OK (f)
5145 || endmatch <= char_ins_del_cost (f)[tem]))
5146 endmatch = 0;
5148 /* nsp - osp is the distance to insert or delete.
5149 If that is nonzero, begmatch is known to be nonzero also.
5150 begmatch + endmatch is how much we save by doing the ins/del.
5151 Is it worth it? */
5153 if (nsp != osp
5154 && (!FRAME_CHAR_INS_DEL_OK (f)
5155 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
5157 begmatch = 0;
5158 endmatch = 0;
5159 osp = nsp = min (osp, nsp);
5162 /* Now go through the line, inserting, writing and
5163 deleting as appropriate. */
5165 if (osp > nsp)
5167 cursor_to (f, vpos, nsp);
5168 delete_glyphs (f, osp - nsp);
5170 else if (nsp > osp)
5172 /* If going to delete chars later in line
5173 and insert earlier in the line,
5174 must delete first to avoid losing data in the insert */
5175 if (endmatch && nlen < olen + nsp - osp)
5177 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5178 delete_glyphs (f, olen + nsp - osp - nlen);
5179 olen = nlen - (nsp - osp);
5181 cursor_to (f, vpos, osp);
5182 insert_glyphs (f, 0, nsp - osp);
5184 olen += nsp - osp;
5186 tem = nsp + begmatch + endmatch;
5187 if (nlen != tem || olen != tem)
5189 if (!endmatch || nlen == olen)
5191 /* If new text being written reaches right margin, there is
5192 no need to do clear-to-eol at the end of this function
5193 (and it would not be safe, since cursor is not going to
5194 be "at the margin" after the text is done). */
5195 if (nlen == FRAME_TOTAL_COLS (f))
5196 olen = 0;
5198 /* Function write_glyphs is prepared to do nothing
5199 if passed a length <= 0. Check it here to avoid
5200 unnecessary cursor movement. */
5201 if (nlen - tem > 0)
5203 cursor_to (f, vpos, nsp + begmatch);
5204 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5207 else if (nlen > olen)
5209 /* Here, we used to have the following simple code:
5210 ----------------------------------------
5211 write_glyphs (nbody + nsp + begmatch, olen - tem);
5212 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5213 ----------------------------------------
5214 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5215 is a padding glyph. */
5216 int out = olen - tem; /* Columns to be overwritten originally. */
5217 int del;
5219 cursor_to (f, vpos, nsp + begmatch);
5221 /* Calculate columns we can actually overwrite. */
5222 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5223 out--;
5224 write_glyphs (f, nbody + nsp + begmatch, out);
5226 /* If we left columns to be overwritten, we must delete them. */
5227 del = olen - tem - out;
5228 if (del > 0)
5229 delete_glyphs (f, del);
5231 /* At last, we insert columns not yet written out. */
5232 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5233 olen = nlen;
5235 else if (olen > nlen)
5237 cursor_to (f, vpos, nsp + begmatch);
5238 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5239 delete_glyphs (f, olen - nlen);
5240 olen = nlen;
5244 just_erase:
5245 /* If any unerased characters remain after the new line, erase them. */
5246 if (olen > nlen)
5248 cursor_to (f, vpos, nlen);
5249 clear_end_of_line (f, olen);
5252 /* Exchange contents between current_frame and new_frame. */
5253 make_current (desired_matrix, current_matrix, vpos);
5258 /***********************************************************************
5259 X/Y Position -> Buffer Position
5260 ***********************************************************************/
5262 /* Determine what's under window-relative pixel position (*X, *Y).
5263 Return the OBJECT (string or buffer) that's there.
5264 Return in *POS the position in that object.
5265 Adjust *X and *Y to character positions.
5266 Return in *DX and *DY the pixel coordinates of the click,
5267 relative to the top left corner of OBJECT, or relative to
5268 the top left corner of the character glyph at (*X, *Y)
5269 if OBJECT is nil.
5270 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5271 if the coordinates point to an empty area of the display. */
5273 Lisp_Object
5274 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)
5276 struct it it;
5277 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5278 struct text_pos startp;
5279 Lisp_Object string;
5280 struct glyph_row *row;
5281 #ifdef HAVE_WINDOW_SYSTEM
5282 struct image *img = 0;
5283 #endif
5284 int x0, x1, to_x;
5285 void *itdata = NULL;
5287 /* We used to set current_buffer directly here, but that does the
5288 wrong thing with `face-remapping-alist' (bug#2044). */
5289 Fset_buffer (w->buffer);
5290 itdata = bidi_shelve_cache ();
5291 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5292 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5293 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5294 start_display (&it, w, startp);
5295 /* start_display takes into account the header-line row, but IT's
5296 vpos still counts from the glyph row that includes the window's
5297 start position. Adjust for a possible header-line row. */
5298 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5300 x0 = *x;
5302 /* First, move to the beginning of the row corresponding to *Y. We
5303 need to be in that row to get the correct value of base paragraph
5304 direction for the text at (*X, *Y). */
5305 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5307 /* TO_X is the pixel position that the iterator will compute for the
5308 glyph at *X. We add it.first_visible_x because iterator
5309 positions include the hscroll. */
5310 to_x = x0 + it.first_visible_x;
5311 if (it.bidi_it.paragraph_dir == R2L)
5312 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5313 text area. This is because the iterator, even in R2L
5314 paragraphs, delivers glyphs as if they started at the left
5315 margin of the window. (When we actually produce glyphs for
5316 display, we reverse their order in PRODUCE_GLYPHS, but the
5317 iterator doesn't know about that.) The following line adjusts
5318 the pixel position to the iterator geometry, which is what
5319 move_it_* routines use. (The -1 is because in a window whose
5320 text-area width is W, the rightmost pixel position is W-1, and
5321 it should be mirrored into zero pixel position.) */
5322 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5324 /* Now move horizontally in the row to the glyph under *X. Second
5325 argument is ZV to prevent move_it_in_display_line from matching
5326 based on buffer positions. */
5327 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5328 bidi_unshelve_cache (itdata, 0);
5330 Fset_buffer (old_current_buffer);
5332 *dx = x0 + it.first_visible_x - it.current_x;
5333 *dy = *y - it.current_y;
5335 string = w->buffer;
5336 if (STRINGP (it.string))
5337 string = it.string;
5338 *pos = it.current;
5339 if (it.what == IT_COMPOSITION
5340 && it.cmp_it.nchars > 1
5341 && it.cmp_it.reversed_p)
5343 /* The current display element is a grapheme cluster in a
5344 composition. In that case, we need the position of the first
5345 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5346 it.current points to the last character of the cluster, thus
5347 we must move back to the first character of the same
5348 cluster. */
5349 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5350 if (STRINGP (it.string))
5351 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5352 else
5353 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->buffer),
5354 CHARPOS (pos->pos));
5357 #ifdef HAVE_WINDOW_SYSTEM
5358 if (it.what == IT_IMAGE)
5360 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5361 && !NILP (img->spec))
5362 *object = img->spec;
5364 #endif
5366 if (it.vpos < w->current_matrix->nrows
5367 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5368 row->enabled_p))
5370 if (it.hpos < row->used[TEXT_AREA])
5372 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5373 #ifdef HAVE_WINDOW_SYSTEM
5374 if (img)
5376 *dy -= row->ascent - glyph->ascent;
5377 *dx += glyph->slice.img.x;
5378 *dy += glyph->slice.img.y;
5379 /* Image slices positions are still relative to the entire image */
5380 *width = img->width;
5381 *height = img->height;
5383 else
5384 #endif
5386 *width = glyph->pixel_width;
5387 *height = glyph->ascent + glyph->descent;
5390 else
5392 *width = 0;
5393 *height = row->height;
5396 else
5398 *width = *height = 0;
5401 /* Add extra (default width) columns if clicked after EOL. */
5402 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5403 if (x0 > x1)
5404 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5406 *x = it.hpos;
5407 *y = it.vpos;
5409 return string;
5413 /* Value is the string under window-relative coordinates X/Y in the
5414 mode line or header line (PART says which) of window W, or nil if none.
5415 *CHARPOS is set to the position in the string returned. */
5417 Lisp_Object
5418 mode_line_string (struct window *w, enum window_part part,
5419 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5420 int *dx, int *dy, int *width, int *height)
5422 struct glyph_row *row;
5423 struct glyph *glyph, *end;
5424 int x0, y0;
5425 Lisp_Object string = Qnil;
5427 if (part == ON_MODE_LINE)
5428 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5429 else
5430 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5431 y0 = *y - row->y;
5432 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5434 if (row->mode_line_p && row->enabled_p)
5436 /* Find the glyph under X. If we find one with a string object,
5437 it's the one we were looking for. */
5438 glyph = row->glyphs[TEXT_AREA];
5439 end = glyph + row->used[TEXT_AREA];
5440 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5441 x0 -= glyph->pixel_width;
5442 *x = glyph - row->glyphs[TEXT_AREA];
5443 if (glyph < end)
5445 string = glyph->object;
5446 *charpos = glyph->charpos;
5447 *width = glyph->pixel_width;
5448 *height = glyph->ascent + glyph->descent;
5449 #ifdef HAVE_WINDOW_SYSTEM
5450 if (glyph->type == IMAGE_GLYPH)
5452 struct image *img;
5453 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5454 if (img != NULL)
5455 *object = img->spec;
5456 y0 -= row->ascent - glyph->ascent;
5458 #endif
5460 else
5462 /* Add extra (default width) columns if clicked after EOL. */
5463 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5464 *width = 0;
5465 *height = row->height;
5468 else
5470 *x = 0;
5471 x0 = 0;
5472 *width = *height = 0;
5475 *dx = x0;
5476 *dy = y0;
5478 return string;
5482 /* Value is the string under window-relative coordinates X/Y in either
5483 marginal area, or nil if none. *CHARPOS is set to the position in
5484 the string returned. */
5486 Lisp_Object
5487 marginal_area_string (struct window *w, enum window_part part,
5488 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5489 int *dx, int *dy, int *width, int *height)
5491 struct glyph_row *row = w->current_matrix->rows;
5492 struct glyph *glyph, *end;
5493 int x0, y0, i, wy = *y;
5494 int area;
5495 Lisp_Object string = Qnil;
5497 if (part == ON_LEFT_MARGIN)
5498 area = LEFT_MARGIN_AREA;
5499 else if (part == ON_RIGHT_MARGIN)
5500 area = RIGHT_MARGIN_AREA;
5501 else
5502 abort ();
5504 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5505 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5506 break;
5507 y0 = *y - row->y;
5508 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5510 if (row->enabled_p)
5512 /* Find the glyph under X. If we find one with a string object,
5513 it's the one we were looking for. */
5514 if (area == RIGHT_MARGIN_AREA)
5515 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5516 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5517 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5518 + window_box_width (w, LEFT_MARGIN_AREA)
5519 + window_box_width (w, TEXT_AREA));
5520 else
5521 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5522 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5523 : 0);
5525 glyph = row->glyphs[area];
5526 end = glyph + row->used[area];
5527 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5528 x0 -= glyph->pixel_width;
5529 *x = glyph - row->glyphs[area];
5530 if (glyph < end)
5532 string = glyph->object;
5533 *charpos = glyph->charpos;
5534 *width = glyph->pixel_width;
5535 *height = glyph->ascent + glyph->descent;
5536 #ifdef HAVE_WINDOW_SYSTEM
5537 if (glyph->type == IMAGE_GLYPH)
5539 struct image *img;
5540 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5541 if (img != NULL)
5542 *object = img->spec;
5543 y0 -= row->ascent - glyph->ascent;
5544 x0 += glyph->slice.img.x;
5545 y0 += glyph->slice.img.y;
5547 #endif
5549 else
5551 /* Add extra (default width) columns if clicked after EOL. */
5552 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5553 *width = 0;
5554 *height = row->height;
5557 else
5559 x0 = 0;
5560 *x = 0;
5561 *width = *height = 0;
5564 *dx = x0;
5565 *dy = y0;
5567 return string;
5571 /***********************************************************************
5572 Changing Frame Sizes
5573 ***********************************************************************/
5575 #ifdef SIGWINCH
5577 static void
5578 window_change_signal (int signalnum) /* If we don't have an argument, */
5579 /* some compilers complain in signal calls. */
5581 int width, height;
5582 int old_errno = errno;
5584 struct tty_display_info *tty;
5586 signal (SIGWINCH, window_change_signal);
5587 SIGNAL_THREAD_CHECK (signalnum);
5589 /* The frame size change obviously applies to a single
5590 termcap-controlled terminal, but we can't decide which.
5591 Therefore, we resize the frames corresponding to each tty.
5593 for (tty = tty_list; tty; tty = tty->next) {
5595 if (! tty->term_initted)
5596 continue;
5598 /* Suspended tty frames have tty->input == NULL avoid trying to
5599 use it. */
5600 if (!tty->input)
5601 continue;
5603 get_tty_size (fileno (tty->input), &width, &height);
5605 if (width > 5 && height > 2) {
5606 Lisp_Object tail, frame;
5608 FOR_EACH_FRAME (tail, frame)
5609 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5610 /* Record the new sizes, but don't reallocate the data
5611 structures now. Let that be done later outside of the
5612 signal handler. */
5613 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5617 errno = old_errno;
5619 #endif /* SIGWINCH */
5622 /* Do any change in frame size that was requested by a signal. SAFE
5623 non-zero means this function is called from a place where it is
5624 safe to change frame sizes while a redisplay is in progress. */
5626 void
5627 do_pending_window_change (int safe)
5629 /* If window_change_signal should have run before, run it now. */
5630 if (redisplaying_p && !safe)
5631 return;
5633 while (delayed_size_change)
5635 Lisp_Object tail, frame;
5637 delayed_size_change = 0;
5639 FOR_EACH_FRAME (tail, frame)
5641 struct frame *f = XFRAME (frame);
5643 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5644 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5645 0, 0, safe);
5651 /* Change the frame height and/or width. Values may be given as zero to
5652 indicate no change is to take place.
5654 If DELAY is non-zero, then assume we're being called from a signal
5655 handler, and queue the change for later - perhaps the next
5656 redisplay. Since this tries to resize windows, we can't call it
5657 from a signal handler.
5659 SAFE non-zero means this function is called from a place where it's
5660 safe to change frame sizes while a redisplay is in progress. */
5662 void
5663 change_frame_size (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5665 Lisp_Object tail, frame;
5667 if (FRAME_MSDOS_P (f))
5669 /* On MS-DOS, all frames use the same screen, so a change in
5670 size affects all frames. Termcap now supports multiple
5671 ttys. */
5672 FOR_EACH_FRAME (tail, frame)
5673 if (! FRAME_WINDOW_P (XFRAME (frame)))
5674 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5675 pretend, delay, safe);
5677 else
5678 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5681 static void
5682 change_frame_size_1 (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5684 int new_frame_total_cols;
5685 int count = SPECPDL_INDEX ();
5687 /* If we can't deal with the change now, queue it for later. */
5688 if (delay || (redisplaying_p && !safe))
5690 f->new_text_lines = newheight;
5691 f->new_text_cols = newwidth;
5692 delayed_size_change = 1;
5693 return;
5696 /* This size-change overrides any pending one for this frame. */
5697 f->new_text_lines = 0;
5698 f->new_text_cols = 0;
5700 /* If an argument is zero, set it to the current value. */
5701 if (newheight == 0)
5702 newheight = FRAME_LINES (f);
5703 if (newwidth == 0)
5704 newwidth = FRAME_COLS (f);
5706 /* Compute width of windows in F.
5707 This is the width of the frame without vertical scroll bars. */
5708 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5710 /* Round up to the smallest acceptable size. */
5711 check_frame_size (f, &newheight, &newwidth);
5713 /* If we're not changing the frame size, quit now. */
5714 /* Frame width may be unchanged but the text portion may change, for example,
5715 fullscreen and remove/add scroll bar. */
5716 if (newheight == FRAME_LINES (f)
5717 && newwidth == FRAME_COLS (f) // text portion unchanged
5718 && new_frame_total_cols == FRAME_TOTAL_COLS (f)) // frame width unchanged
5719 return;
5721 BLOCK_INPUT;
5723 #ifdef MSDOS
5724 /* We only can set screen dimensions to certain values supported
5725 by our video hardware. Try to find the smallest size greater
5726 or equal to the requested dimensions. */
5727 dos_set_window_size (&newheight, &newwidth);
5728 #endif
5730 if (newheight != FRAME_LINES (f))
5732 resize_frame_windows (f, newheight, 0);
5734 /* MSDOS frames cannot PRETEND, as they change frame size by
5735 manipulating video hardware. */
5736 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5737 FrameRows (FRAME_TTY (f)) = newheight;
5740 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5742 resize_frame_windows (f, new_frame_total_cols, 1);
5744 /* MSDOS frames cannot PRETEND, as they change frame size by
5745 manipulating video hardware. */
5746 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5747 FrameCols (FRAME_TTY (f)) = newwidth;
5749 if (WINDOWP (f->tool_bar_window))
5750 XSETFASTINT (XWINDOW (f->tool_bar_window)->total_cols, newwidth);
5753 FRAME_LINES (f) = newheight;
5754 SET_FRAME_COLS (f, newwidth);
5757 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5758 int text_area_x, text_area_y, text_area_width, text_area_height;
5760 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5761 &text_area_height);
5762 if (w->cursor.x >= text_area_x + text_area_width)
5763 w->cursor.hpos = w->cursor.x = 0;
5764 if (w->cursor.y >= text_area_y + text_area_height)
5765 w->cursor.vpos = w->cursor.y = 0;
5768 adjust_glyphs (f);
5769 calculate_costs (f);
5770 SET_FRAME_GARBAGED (f);
5771 f->resized_p = 1;
5773 UNBLOCK_INPUT;
5775 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5777 run_window_configuration_change_hook (f);
5779 unbind_to (count, Qnil);
5784 /***********************************************************************
5785 Terminal Related Lisp Functions
5786 ***********************************************************************/
5788 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5789 1, 1, "FOpen termscript file: ",
5790 doc: /* Start writing all terminal output to FILE as well as the terminal.
5791 FILE = nil means just close any termscript file currently open. */)
5792 (Lisp_Object file)
5794 struct tty_display_info *tty;
5796 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5797 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5798 error ("Current frame is not on a tty device");
5800 tty = CURTTY ();
5802 if (tty->termscript != 0)
5804 BLOCK_INPUT;
5805 fclose (tty->termscript);
5806 UNBLOCK_INPUT;
5808 tty->termscript = 0;
5810 if (! NILP (file))
5812 file = Fexpand_file_name (file, Qnil);
5813 tty->termscript = fopen (SSDATA (file), "w");
5814 if (tty->termscript == 0)
5815 report_file_error ("Opening termscript", Fcons (file, Qnil));
5817 return Qnil;
5821 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5822 Ssend_string_to_terminal, 1, 2, 0,
5823 doc: /* Send STRING to the terminal without alteration.
5824 Control characters in STRING will have terminal-dependent effects.
5826 Optional parameter TERMINAL specifies the tty terminal device to use.
5827 It may be a terminal object, a frame, or nil for the terminal used by
5828 the currently selected frame. In batch mode, STRING is sent to stdout
5829 when TERMINAL is nil. */)
5830 (Lisp_Object string, Lisp_Object terminal)
5832 struct terminal *t = get_terminal (terminal, 1);
5833 FILE *out;
5835 /* ??? Perhaps we should do something special for multibyte strings here. */
5836 CHECK_STRING (string);
5837 BLOCK_INPUT;
5839 if (!t)
5840 error ("Unknown terminal device");
5842 if (t->type == output_initial)
5843 out = stdout;
5844 else if (t->type != output_termcap && t->type != output_msdos_raw)
5845 error ("Device %d is not a termcap terminal device", t->id);
5846 else
5848 struct tty_display_info *tty = t->display_info.tty;
5850 if (! tty->output)
5851 error ("Terminal is currently suspended");
5853 if (tty->termscript)
5855 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5856 fflush (tty->termscript);
5858 out = tty->output;
5860 fwrite (SDATA (string), 1, SBYTES (string), out);
5861 fflush (out);
5862 UNBLOCK_INPUT;
5863 return Qnil;
5867 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5868 doc: /* Beep, or flash the screen.
5869 Also, unless an argument is given,
5870 terminate any keyboard macro currently executing. */)
5871 (Lisp_Object arg)
5873 if (!NILP (arg))
5875 if (noninteractive)
5876 putchar (07);
5877 else
5878 ring_bell (XFRAME (selected_frame));
5880 else
5881 bitch_at_user ();
5883 return Qnil;
5886 void
5887 bitch_at_user (void)
5889 if (noninteractive)
5890 putchar (07);
5891 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5892 error ("Keyboard macro terminated by a command ringing the bell");
5893 else
5894 ring_bell (XFRAME (selected_frame));
5899 /***********************************************************************
5900 Sleeping, Waiting
5901 ***********************************************************************/
5903 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5904 doc: /* Pause, without updating display, for SECONDS seconds.
5905 SECONDS may be a floating-point value, meaning that you can wait for a
5906 fraction of a second. Optional second arg MILLISECONDS specifies an
5907 additional wait period, in milliseconds; this may be useful if your
5908 Emacs was built without floating point support.
5909 \(Not all operating systems support waiting for a fraction of a second.) */)
5910 (Lisp_Object seconds, Lisp_Object milliseconds)
5912 int sec, usec;
5914 if (NILP (milliseconds))
5915 XSETINT (milliseconds, 0);
5916 else
5917 CHECK_NUMBER (milliseconds);
5918 usec = XINT (milliseconds) * 1000;
5921 double duration = extract_float (seconds);
5922 sec = (int) duration;
5923 usec += (duration - sec) * 1000000;
5926 #ifndef EMACS_HAS_USECS
5927 if (sec == 0 && usec != 0)
5928 error ("Millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
5929 #endif
5931 /* Assure that 0 <= usec < 1000000. */
5932 if (usec < 0)
5934 /* We can't rely on the rounding being correct if usec is negative. */
5935 if (-1000000 < usec)
5936 sec--, usec += 1000000;
5937 else
5938 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
5940 else
5941 sec += usec / 1000000, usec %= 1000000;
5943 if (sec < 0 || (sec == 0 && usec == 0))
5944 return Qnil;
5946 wait_reading_process_output (sec, usec, 0, 0, Qnil, NULL, 0);
5948 return Qnil;
5952 /* This is just like wait_reading_process_output, except that
5953 it does redisplay.
5955 TIMEOUT is number of seconds to wait (float or integer),
5956 or t to wait forever.
5957 READING is 1 if reading input.
5958 If DO_DISPLAY is >0 display process output while waiting.
5959 If DO_DISPLAY is >1 perform an initial redisplay before waiting.
5962 Lisp_Object
5963 sit_for (Lisp_Object timeout, int reading, int do_display)
5965 int sec, usec;
5967 swallow_events (do_display);
5969 if ((detect_input_pending_run_timers (do_display))
5970 || !NILP (Vexecuting_kbd_macro))
5971 return Qnil;
5973 if (do_display >= 2)
5974 redisplay_preserve_echo_area (2);
5976 if (INTEGERP (timeout))
5978 sec = XINT (timeout);
5979 usec = 0;
5981 else if (FLOATP (timeout))
5983 double seconds = XFLOAT_DATA (timeout);
5984 sec = (int) seconds;
5985 usec = (int) ((seconds - sec) * 1000000);
5987 else if (EQ (timeout, Qt))
5989 sec = 0;
5990 usec = 0;
5992 else
5993 wrong_type_argument (Qnumberp, timeout);
5995 if (sec == 0 && usec == 0 && !EQ (timeout, Qt))
5996 return Qt;
5998 #ifdef SIGIO
5999 gobble_input (0);
6000 #endif
6002 wait_reading_process_output (sec, usec, reading ? -1 : 1, do_display,
6003 Qnil, NULL, 0);
6005 return detect_input_pending () ? Qnil : Qt;
6009 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
6010 doc: /* Perform redisplay if no input is available.
6011 If optional arg FORCE is non-nil or `redisplay-dont-pause' is non-nil,
6012 perform a full redisplay even if input is available.
6013 Return t if redisplay was performed, nil otherwise. */)
6014 (Lisp_Object force)
6016 int count;
6018 swallow_events (1);
6019 if ((detect_input_pending_run_timers (1)
6020 && NILP (force) && !redisplay_dont_pause)
6021 || !NILP (Vexecuting_kbd_macro))
6022 return Qnil;
6024 count = SPECPDL_INDEX ();
6025 if (!NILP (force) && !redisplay_dont_pause)
6026 specbind (Qredisplay_dont_pause, Qt);
6027 redisplay_preserve_echo_area (2);
6028 unbind_to (count, Qnil);
6029 return Qt;
6034 /***********************************************************************
6035 Other Lisp Functions
6036 ***********************************************************************/
6038 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
6039 session's frames, frame names, buffers, buffer-read-only flags, and
6040 buffer-modified-flags. */
6042 static Lisp_Object frame_and_buffer_state;
6045 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
6046 Sframe_or_buffer_changed_p, 0, 1, 0,
6047 doc: /* Return non-nil if the frame and buffer state appears to have changed.
6048 VARIABLE is a variable name whose value is either nil or a state vector
6049 that will be updated to contain all frames and buffers,
6050 aside from buffers whose names start with space,
6051 along with the buffers' read-only and modified flags. This allows a fast
6052 check to see whether buffer menus might need to be recomputed.
6053 If this function returns non-nil, it updates the internal vector to reflect
6054 the current state.
6056 If VARIABLE is nil, an internal variable is used. Users should not
6057 pass nil for VARIABLE. */)
6058 (Lisp_Object variable)
6060 Lisp_Object state, tail, frame, buf;
6061 Lisp_Object *vecp, *end;
6062 int n;
6064 if (! NILP (variable))
6066 CHECK_SYMBOL (variable);
6067 state = Fsymbol_value (variable);
6068 if (! VECTORP (state))
6069 goto changed;
6071 else
6072 state = frame_and_buffer_state;
6074 vecp = XVECTOR (state)->contents;
6075 end = vecp + ASIZE (state);
6077 FOR_EACH_FRAME (tail, frame)
6079 if (vecp == end)
6080 goto changed;
6081 if (!EQ (*vecp++, frame))
6082 goto changed;
6083 if (vecp == end)
6084 goto changed;
6085 if (!EQ (*vecp++, XFRAME (frame)->name))
6086 goto changed;
6088 /* Check that the buffer info matches. */
6089 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6091 buf = XCDR (XCAR (tail));
6092 /* Ignore buffers that aren't included in buffer lists. */
6093 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6094 continue;
6095 if (vecp == end)
6096 goto changed;
6097 if (!EQ (*vecp++, buf))
6098 goto changed;
6099 if (vecp == end)
6100 goto changed;
6101 if (!EQ (*vecp++, BVAR (XBUFFER (buf), read_only)))
6102 goto changed;
6103 if (vecp == end)
6104 goto changed;
6105 if (!EQ (*vecp++, Fbuffer_modified_p (buf)))
6106 goto changed;
6108 if (vecp == end)
6109 goto changed;
6110 /* Detect deletion of a buffer at the end of the list. */
6111 if (EQ (*vecp, Qlambda))
6112 return Qnil;
6114 /* Come here if we decide the data has changed. */
6115 changed:
6116 /* Count the size we will need.
6117 Start with 1 so there is room for at least one lambda at the end. */
6118 n = 1;
6119 FOR_EACH_FRAME (tail, frame)
6120 n += 2;
6121 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6122 n += 3;
6123 /* Reallocate the vector if data has grown to need it,
6124 or if it has shrunk a lot. */
6125 if (! VECTORP (state)
6126 || n > ASIZE (state)
6127 || n + 20 < ASIZE (state) / 2)
6128 /* Add 20 extra so we grow it less often. */
6130 state = Fmake_vector (make_number (n + 20), Qlambda);
6131 if (! NILP (variable))
6132 Fset (variable, state);
6133 else
6134 frame_and_buffer_state = state;
6137 /* Record the new data in the (possibly reallocated) vector. */
6138 vecp = XVECTOR (state)->contents;
6139 FOR_EACH_FRAME (tail, frame)
6141 *vecp++ = frame;
6142 *vecp++ = XFRAME (frame)->name;
6144 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6146 buf = XCDR (XCAR (tail));
6147 /* Ignore buffers that aren't included in buffer lists. */
6148 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6149 continue;
6150 *vecp++ = buf;
6151 *vecp++ = BVAR (XBUFFER (buf), read_only);
6152 *vecp++ = Fbuffer_modified_p (buf);
6154 /* Fill up the vector with lambdas (always at least one). */
6155 *vecp++ = Qlambda;
6156 while (vecp - XVECTOR (state)->contents
6157 < ASIZE (state))
6158 *vecp++ = Qlambda;
6159 /* Make sure we didn't overflow the vector. */
6160 if (vecp - XVECTOR (state)->contents
6161 > ASIZE (state))
6162 abort ();
6163 return Qt;
6168 /***********************************************************************
6169 Initialization
6170 ***********************************************************************/
6172 /* Initialization done when Emacs fork is started, before doing stty.
6173 Determine terminal type and set terminal_driver. Then invoke its
6174 decoding routine to set up variables in the terminal package. */
6176 void
6177 init_display (void)
6179 char *terminal_type;
6181 /* Construct the space glyph. */
6182 space_glyph.type = CHAR_GLYPH;
6183 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
6184 space_glyph.charpos = -1;
6186 inverse_video = 0;
6187 cursor_in_echo_area = 0;
6188 terminal_type = (char *) 0;
6190 /* Now is the time to initialize this; it's used by init_sys_modes
6191 during startup. */
6192 Vinitial_window_system = Qnil;
6194 /* SIGWINCH needs to be handled no matter what display we start
6195 with. Otherwise newly opened tty frames will not resize
6196 automatically. */
6197 #ifdef SIGWINCH
6198 #ifndef CANNOT_DUMP
6199 if (initialized)
6200 #endif /* CANNOT_DUMP */
6201 signal (SIGWINCH, window_change_signal);
6202 #endif /* SIGWINCH */
6204 /* If running as a daemon, no need to initialize any frames/terminal. */
6205 if (IS_DAEMON)
6206 return;
6208 /* If the user wants to use a window system, we shouldn't bother
6209 initializing the terminal. This is especially important when the
6210 terminal is so dumb that emacs gives up before and doesn't bother
6211 using the window system.
6213 If the DISPLAY environment variable is set and nonempty,
6214 try to use X, and die with an error message if that doesn't work. */
6216 #ifdef HAVE_X_WINDOWS
6217 if (! inhibit_window_system && ! display_arg)
6219 char *display;
6220 display = getenv ("DISPLAY");
6221 display_arg = (display != 0 && *display != 0);
6223 if (display_arg && !x_display_ok (display))
6225 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6226 display);
6227 inhibit_window_system = 1;
6231 if (!inhibit_window_system && display_arg)
6233 Vinitial_window_system = Qx;
6234 #ifdef HAVE_X11
6235 Vwindow_system_version = make_number (11);
6236 #endif
6237 #if defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
6238 /* In some versions of ncurses,
6239 tputs crashes if we have not called tgetent.
6240 So call tgetent. */
6241 { char b[2044]; tgetent (b, "xterm");}
6242 #endif
6243 adjust_frame_glyphs_initially ();
6244 return;
6246 #endif /* HAVE_X_WINDOWS */
6248 #ifdef HAVE_NTGUI
6249 if (!inhibit_window_system)
6251 Vinitial_window_system = Qw32;
6252 Vwindow_system_version = make_number (1);
6253 adjust_frame_glyphs_initially ();
6254 return;
6256 #endif /* HAVE_NTGUI */
6258 #ifdef HAVE_NS
6259 if (!inhibit_window_system
6260 #ifndef CANNOT_DUMP
6261 && initialized
6262 #endif
6265 Vinitial_window_system = Qns;
6266 Vwindow_system_version = make_number (10);
6267 adjust_frame_glyphs_initially ();
6268 return;
6270 #endif
6272 /* If no window system has been specified, try to use the terminal. */
6273 if (! isatty (0))
6275 fatal ("standard input is not a tty");
6276 exit (1);
6279 #ifdef WINDOWSNT
6280 terminal_type = "w32console";
6281 #else
6282 /* Look at the TERM variable. */
6283 terminal_type = (char *) getenv ("TERM");
6284 #endif
6285 if (!terminal_type)
6287 #ifdef HAVE_WINDOW_SYSTEM
6288 if (! inhibit_window_system)
6289 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6290 else
6291 #endif /* HAVE_WINDOW_SYSTEM */
6292 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6293 exit (1);
6297 struct terminal *t;
6298 struct frame *f = XFRAME (selected_frame);
6300 /* Open a display on the controlling tty. */
6301 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6303 /* Convert the initial frame to use the new display. */
6304 if (f->output_method != output_initial)
6305 abort ();
6306 f->output_method = t->type;
6307 f->terminal = t;
6309 t->reference_count++;
6310 #ifdef MSDOS
6311 f->output_data.tty->display_info = &the_only_display_info;
6312 #else
6313 if (f->output_method == output_termcap)
6314 create_tty_output (f);
6315 #endif
6316 t->display_info.tty->top_frame = selected_frame;
6317 change_frame_size (XFRAME (selected_frame),
6318 FrameRows (t->display_info.tty),
6319 FrameCols (t->display_info.tty), 0, 0, 1);
6321 /* Delete the initial terminal. */
6322 if (--initial_terminal->reference_count == 0
6323 && initial_terminal->delete_terminal_hook)
6324 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6326 /* Update frame parameters to reflect the new type. */
6327 Fmodify_frame_parameters
6328 (selected_frame, Fcons (Fcons (Qtty_type,
6329 Ftty_type (selected_frame)), Qnil));
6330 if (t->display_info.tty->name)
6331 Fmodify_frame_parameters (selected_frame,
6332 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6333 Qnil));
6334 else
6335 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6336 Qnil));
6340 struct frame *sf = SELECTED_FRAME ();
6341 int width = FRAME_TOTAL_COLS (sf);
6342 int height = FRAME_LINES (sf);
6344 /* If these sizes are so big they cause overflow, just ignore the
6345 change. It's not clear what better we could do. The rest of
6346 the code assumes that (width + 2) * height * sizeof (struct glyph)
6347 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6348 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6349 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6350 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6351 < (width + 2) * height))
6352 fatal ("screen size %dx%d too big", width, height);
6355 adjust_frame_glyphs_initially ();
6356 calculate_costs (XFRAME (selected_frame));
6358 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6359 if (initialized
6360 && !noninteractive
6361 && NILP (Vinitial_window_system))
6363 /* For the initial frame, we don't have any way of knowing what
6364 are the foreground and background colors of the terminal. */
6365 struct frame *sf = SELECTED_FRAME ();
6367 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6368 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6369 call0 (intern ("tty-set-up-initial-frame-faces"));
6375 /***********************************************************************
6376 Blinking cursor
6377 ***********************************************************************/
6379 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6380 Sinternal_show_cursor, 2, 2, 0,
6381 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6382 WINDOW nil means use the selected window. SHOW non-nil means
6383 show a cursor in WINDOW in the next redisplay. SHOW nil means
6384 don't show a cursor. */)
6385 (Lisp_Object window, Lisp_Object show)
6387 /* Don't change cursor state while redisplaying. This could confuse
6388 output routines. */
6389 if (!redisplaying_p)
6391 if (NILP (window))
6392 window = selected_window;
6393 else
6394 CHECK_WINDOW (window);
6396 XWINDOW (window)->cursor_off_p = NILP (show);
6399 return Qnil;
6403 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6404 Sinternal_show_cursor_p, 0, 1, 0,
6405 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6406 WINDOW nil or omitted means report on the selected window. */)
6407 (Lisp_Object window)
6409 struct window *w;
6411 if (NILP (window))
6412 window = selected_window;
6413 else
6414 CHECK_WINDOW (window);
6416 w = XWINDOW (window);
6417 return w->cursor_off_p ? Qnil : Qt;
6420 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6421 Slast_nonminibuf_frame, 0, 0, 0,
6422 doc: /* Value is last nonminibuffer frame. */)
6423 (void)
6425 Lisp_Object frame = Qnil;
6427 if (last_nonminibuf_frame)
6428 XSETFRAME (frame, last_nonminibuf_frame);
6430 return frame;
6433 /***********************************************************************
6434 Initialization
6435 ***********************************************************************/
6437 void
6438 syms_of_display (void)
6440 defsubr (&Sredraw_frame);
6441 defsubr (&Sredraw_display);
6442 defsubr (&Sframe_or_buffer_changed_p);
6443 defsubr (&Sopen_termscript);
6444 defsubr (&Sding);
6445 defsubr (&Sredisplay);
6446 defsubr (&Ssleep_for);
6447 defsubr (&Ssend_string_to_terminal);
6448 defsubr (&Sinternal_show_cursor);
6449 defsubr (&Sinternal_show_cursor_p);
6450 defsubr (&Slast_nonminibuf_frame);
6452 #if GLYPH_DEBUG
6453 defsubr (&Sdump_redisplay_history);
6454 #endif
6456 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6457 staticpro (&frame_and_buffer_state);
6459 DEFSYM (Qdisplay_table, "display-table");
6460 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6462 DEFVAR_INT ("baud-rate", baud_rate,
6463 doc: /* *The output baud rate of the terminal.
6464 On most systems, changing this value will affect the amount of padding
6465 and the other strategic decisions made during redisplay. */);
6467 DEFVAR_BOOL ("inverse-video", inverse_video,
6468 doc: /* *Non-nil means invert the entire frame display.
6469 This means everything is in inverse video which otherwise would not be. */);
6471 DEFVAR_BOOL ("visible-bell", visible_bell,
6472 doc: /* *Non-nil means try to flash the frame to represent a bell.
6474 See also `ring-bell-function'. */);
6476 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6477 doc: /* *Non-nil means no need to redraw entire frame after suspending.
6478 A non-nil value is useful if the terminal can automatically preserve
6479 Emacs's frame display when you reenter Emacs.
6480 It is up to you to set this variable if your terminal can do that. */);
6482 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6483 doc: /* Name of the window system that Emacs uses for the first frame.
6484 The value is a symbol:
6485 nil for a termcap frame (a character-only terminal),
6486 'x' for an Emacs frame that is really an X window,
6487 'w32' for an Emacs frame that is a window on MS-Windows display,
6488 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6489 'pc' for a direct-write MS-DOS frame.
6491 Use of this variable as a boolean is deprecated. Instead,
6492 use `display-graphic-p' or any of the other `display-*-p'
6493 predicates which report frame's specific UI-related capabilities. */);
6495 DEFVAR_KBOARD ("window-system", Vwindow_system,
6496 doc: /* Name of window system through which the selected frame is displayed.
6497 The value is a symbol:
6498 nil for a termcap frame (a character-only terminal),
6499 'x' for an Emacs frame that is really an X window,
6500 'w32' for an Emacs frame that is a window on MS-Windows display,
6501 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6502 'pc' for a direct-write MS-DOS frame.
6504 Use of this variable as a boolean is deprecated. Instead,
6505 use `display-graphic-p' or any of the other `display-*-p'
6506 predicates which report frame's specific UI-related capabilities. */);
6508 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6509 doc: /* The version number of the window system in use.
6510 For X windows, this is 11. */);
6512 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6513 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6515 DEFVAR_LISP ("glyph-table", Vglyph_table,
6516 doc: /* Table defining how to output a glyph code to the frame.
6517 If not nil, this is a vector indexed by glyph code to define the glyph.
6518 Each element can be:
6519 integer: a glyph code which this glyph is an alias for.
6520 string: output this glyph using that string (not impl. in X windows).
6521 nil: this glyph mod 524288 is the code of a character to output,
6522 and this glyph / 524288 is the face number (see `face-id') to use
6523 while outputting it. */);
6524 Vglyph_table = Qnil;
6526 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6527 doc: /* Display table to use for buffers that specify none.
6528 See `buffer-display-table' for more information. */);
6529 Vstandard_display_table = Qnil;
6531 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6532 doc: /* *Non-nil means display update isn't paused when input is detected. */);
6533 redisplay_dont_pause = 1;
6535 #if PERIODIC_PREEMPTION_CHECKING
6536 DEFVAR_LISP ("redisplay-preemption-period", Vredisplay_preemption_period,
6537 doc: /* *The period in seconds between checking for input during redisplay.
6538 If input is detected, redisplay is pre-empted, and the input is processed.
6539 If nil, never pre-empt redisplay. */);
6540 Vredisplay_preemption_period = make_float (0.10);
6541 #endif
6543 #ifdef CANNOT_DUMP
6544 if (noninteractive)
6545 #endif
6547 Vinitial_window_system = Qnil;
6548 Vwindow_system_version = Qnil;