Fix typos.
[emacs.git] / src / dispnew.c
blob7b594c5b4e82d269265d4c71a5810c117241e83c
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 int area, k;
438 unsigned row_hash = 0;
440 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
441 for (k = 0; k < row->used[area]; ++k)
442 row_hash = ((((row_hash << 4) + (row_hash >> 24)) & 0x0fffffff)
443 + row->glyphs[area][k].u.val
444 + row->glyphs[area][k].face_id
445 + row->glyphs[area][k].padding_p
446 + (row->glyphs[area][k].type << 2));
448 return row_hash == row->hash;
450 #endif
452 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
453 window sizes.
455 W is null if the function is called for a frame glyph matrix.
456 Otherwise it is the window MATRIX is a member of. X and Y are the
457 indices of the first column and row of MATRIX within the frame
458 matrix, if such a matrix exists. They are zero for purely
459 window-based redisplay. DIM is the needed size of the matrix.
461 In window-based redisplay, where no frame matrices exist, glyph
462 matrices manage their own glyph storage. Otherwise, they allocate
463 storage from a common frame glyph pool which can be found in
464 MATRIX->pool.
466 The reason for this memory management strategy is to avoid complete
467 frame redraws if possible. When we allocate from a common pool, a
468 change of the location or size of a sub-matrix within the pool
469 requires a complete redisplay of the frame because we cannot easily
470 make sure that the current matrices of all windows still agree with
471 what is displayed on the screen. While this is usually fast, it
472 leads to screen flickering. */
474 static void
475 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
477 int i;
478 int new_rows;
479 int marginal_areas_changed_p = 0;
480 int header_line_changed_p = 0;
481 int header_line_p = 0;
482 int left = -1, right = -1;
483 int window_width = -1, window_height = -1;
485 /* See if W had a header line that has disappeared now, or vice versa.
486 Get W's size. */
487 if (w)
489 window_box (w, -1, 0, 0, &window_width, &window_height);
491 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
492 header_line_changed_p = header_line_p != matrix->header_line_p;
494 matrix->header_line_p = header_line_p;
496 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
497 Do nothing if MATRIX' size, position, vscroll, and marginal areas
498 haven't changed. This optimization is important because preserving
499 the matrix means preventing redisplay. */
500 if (matrix->pool == NULL)
502 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
503 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
504 xassert (left >= 0 && right >= 0);
505 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
506 || right != matrix->right_margin_glyphs);
508 if (!marginal_areas_changed_p
509 && !fonts_changed_p
510 && !header_line_changed_p
511 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
512 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
513 && matrix->window_height == window_height
514 && matrix->window_vscroll == w->vscroll
515 && matrix->window_width == window_width)
516 return;
519 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
520 if (matrix->rows_allocated < dim.height)
522 int old_alloc = matrix->rows_allocated;
523 new_rows = dim.height - matrix->rows_allocated;
524 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
525 new_rows, INT_MAX, sizeof *matrix->rows);
526 memset (matrix->rows + old_alloc, 0,
527 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
529 else
530 new_rows = 0;
532 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
533 on a frame not using window-based redisplay. Set up pointers for
534 each row into the glyph pool. */
535 if (matrix->pool)
537 xassert (matrix->pool->glyphs);
539 if (w)
541 left = margin_glyphs_to_reserve (w, dim.width,
542 w->left_margin_cols);
543 right = margin_glyphs_to_reserve (w, dim.width,
544 w->right_margin_cols);
546 else
547 left = right = 0;
549 for (i = 0; i < dim.height; ++i)
551 struct glyph_row *row = &matrix->rows[i];
553 row->glyphs[LEFT_MARGIN_AREA]
554 = (matrix->pool->glyphs
555 + (y + i) * matrix->pool->ncolumns
556 + x);
558 if (w == NULL
559 || row == matrix->rows + dim.height - 1
560 || (row == matrix->rows && matrix->header_line_p))
562 row->glyphs[TEXT_AREA]
563 = row->glyphs[LEFT_MARGIN_AREA];
564 row->glyphs[RIGHT_MARGIN_AREA]
565 = row->glyphs[TEXT_AREA] + dim.width;
566 row->glyphs[LAST_AREA]
567 = row->glyphs[RIGHT_MARGIN_AREA];
569 else
571 row->glyphs[TEXT_AREA]
572 = row->glyphs[LEFT_MARGIN_AREA] + left;
573 row->glyphs[RIGHT_MARGIN_AREA]
574 = row->glyphs[TEXT_AREA] + dim.width - left - right;
575 row->glyphs[LAST_AREA]
576 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
580 matrix->left_margin_glyphs = left;
581 matrix->right_margin_glyphs = right;
583 else
585 /* If MATRIX->pool is null, MATRIX is responsible for managing
586 its own memory. It is a window matrix for window-based redisplay.
587 Allocate glyph memory from the heap. */
588 if (dim.width > matrix->matrix_w
589 || new_rows
590 || header_line_changed_p
591 || marginal_areas_changed_p)
593 struct glyph_row *row = matrix->rows;
594 struct glyph_row *end = row + matrix->rows_allocated;
596 while (row < end)
598 row->glyphs[LEFT_MARGIN_AREA]
599 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
600 dim.width, sizeof (struct glyph));
602 /* The mode line never has marginal areas. */
603 if (row == matrix->rows + dim.height - 1
604 || (row == matrix->rows && matrix->header_line_p))
606 row->glyphs[TEXT_AREA]
607 = row->glyphs[LEFT_MARGIN_AREA];
608 row->glyphs[RIGHT_MARGIN_AREA]
609 = row->glyphs[TEXT_AREA] + dim.width;
610 row->glyphs[LAST_AREA]
611 = row->glyphs[RIGHT_MARGIN_AREA];
613 else
615 row->glyphs[TEXT_AREA]
616 = row->glyphs[LEFT_MARGIN_AREA] + left;
617 row->glyphs[RIGHT_MARGIN_AREA]
618 = row->glyphs[TEXT_AREA] + dim.width - left - right;
619 row->glyphs[LAST_AREA]
620 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
622 xassert (!row->enabled_p || verify_row_hash (row));
623 ++row;
627 xassert (left >= 0 && right >= 0);
628 matrix->left_margin_glyphs = left;
629 matrix->right_margin_glyphs = right;
632 /* Number of rows to be used by MATRIX. */
633 matrix->nrows = dim.height;
634 xassert (matrix->nrows >= 0);
636 if (w)
638 if (matrix == w->current_matrix)
640 /* Mark rows in a current matrix of a window as not having
641 valid contents. It's important to not do this for
642 desired matrices. When Emacs starts, it may already be
643 building desired matrices when this function runs. */
644 if (window_width < 0)
645 window_width = window_box_width (w, -1);
647 /* Optimize the case that only the height has changed (C-x 2,
648 upper window). Invalidate all rows that are no longer part
649 of the window. */
650 if (!marginal_areas_changed_p
651 && !header_line_changed_p
652 && new_rows == 0
653 && dim.width == matrix->matrix_w
654 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
655 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
656 && matrix->window_width == window_width)
658 /* Find the last row in the window. */
659 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
660 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
662 ++i;
663 break;
666 /* Window end is invalid, if inside of the rows that
667 are invalidated below. */
668 if (INTEGERP (w->window_end_vpos)
669 && XFASTINT (w->window_end_vpos) >= i)
670 w->window_end_valid = Qnil;
672 while (i < matrix->nrows)
673 matrix->rows[i++].enabled_p = 0;
675 else
677 for (i = 0; i < matrix->nrows; ++i)
678 matrix->rows[i].enabled_p = 0;
681 else if (matrix == w->desired_matrix)
683 /* Rows in desired matrices always have to be cleared;
684 redisplay expects this is the case when it runs, so it
685 had better be the case when we adjust matrices between
686 redisplays. */
687 for (i = 0; i < matrix->nrows; ++i)
688 matrix->rows[i].enabled_p = 0;
693 /* Remember last values to be able to optimize frame redraws. */
694 matrix->matrix_x = x;
695 matrix->matrix_y = y;
696 matrix->matrix_w = dim.width;
697 matrix->matrix_h = dim.height;
699 /* Record the top y location and height of W at the time the matrix
700 was last adjusted. This is used to optimize redisplay above. */
701 if (w)
703 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
704 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
705 matrix->window_height = window_height;
706 matrix->window_width = window_width;
707 matrix->window_vscroll = w->vscroll;
712 /* Reverse the contents of rows in MATRIX between START and END. The
713 contents of the row at END - 1 end up at START, END - 2 at START +
714 1 etc. This is part of the implementation of rotate_matrix (see
715 below). */
717 static void
718 reverse_rows (struct glyph_matrix *matrix, int start, int end)
720 int i, j;
722 for (i = start, j = end - 1; i < j; ++i, --j)
724 /* Non-ISO HP/UX compiler doesn't like auto struct
725 initialization. */
726 struct glyph_row temp;
727 temp = matrix->rows[i];
728 matrix->rows[i] = matrix->rows[j];
729 matrix->rows[j] = temp;
734 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
735 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
736 indices. (Note: this does not copy glyphs, only glyph pointers in
737 row structures are moved around).
739 The algorithm used for rotating the vector was, I believe, first
740 described by Kernighan. See the vector R as consisting of two
741 sub-vectors AB, where A has length BY for BY >= 0. The result
742 after rotating is then BA. Reverse both sub-vectors to get ArBr
743 and reverse the result to get (ArBr)r which is BA. Similar for
744 rotating right. */
746 void
747 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
749 if (by < 0)
751 /* Up (rotate left, i.e. towards lower indices). */
752 by = -by;
753 reverse_rows (matrix, first, first + by);
754 reverse_rows (matrix, first + by, last);
755 reverse_rows (matrix, first, last);
757 else if (by > 0)
759 /* Down (rotate right, i.e. towards higher indices). */
760 reverse_rows (matrix, last - by, last);
761 reverse_rows (matrix, first, last - by);
762 reverse_rows (matrix, first, last);
767 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
768 with indices START <= index < END. Increment positions by DELTA/
769 DELTA_BYTES. */
771 void
772 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
773 EMACS_INT delta, EMACS_INT delta_bytes)
775 /* Check that START and END are reasonable values. */
776 xassert (start >= 0 && start <= matrix->nrows);
777 xassert (end >= 0 && end <= matrix->nrows);
778 xassert (start <= end);
780 for (; start < end; ++start)
781 increment_row_positions (matrix->rows + start, delta, delta_bytes);
785 /* Enable a range of rows in glyph matrix MATRIX. START and END are
786 the row indices of the first and last + 1 row to enable. If
787 ENABLED_P is non-zero, enabled_p flags in rows will be set to 1. */
789 void
790 enable_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end, int enabled_p)
792 xassert (start <= end);
793 xassert (start >= 0 && start < matrix->nrows);
794 xassert (end >= 0 && end <= matrix->nrows);
796 for (; start < end; ++start)
797 matrix->rows[start].enabled_p = enabled_p != 0;
801 /* Clear MATRIX.
803 This empties all rows in MATRIX by setting the enabled_p flag for
804 all rows of the matrix to zero. The function prepare_desired_row
805 will eventually really clear a row when it sees one with a zero
806 enabled_p flag.
808 Resets update hints to defaults value. The only update hint
809 currently present is the flag MATRIX->no_scrolling_p. */
811 void
812 clear_glyph_matrix (struct glyph_matrix *matrix)
814 if (matrix)
816 enable_glyph_matrix_rows (matrix, 0, matrix->nrows, 0);
817 matrix->no_scrolling_p = 0;
822 /* Shift part of the glyph matrix MATRIX of window W up or down.
823 Increment y-positions in glyph rows between START and END by DY,
824 and recompute their visible height. */
826 void
827 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
829 int min_y, max_y;
831 xassert (start <= end);
832 xassert (start >= 0 && start < matrix->nrows);
833 xassert (end >= 0 && end <= matrix->nrows);
835 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
836 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
838 for (; start < end; ++start)
840 struct glyph_row *row = &matrix->rows[start];
842 row->y += dy;
843 row->visible_height = row->height;
845 if (row->y < min_y)
846 row->visible_height -= min_y - row->y;
847 if (row->y + row->height > max_y)
848 row->visible_height -= row->y + row->height - max_y;
849 if (row->fringe_bitmap_periodic_p)
850 row->redraw_fringe_bitmaps_p = 1;
855 /* Mark all rows in current matrices of frame F as invalid. Marking
856 invalid is done by setting enabled_p to zero for all rows in a
857 current matrix. */
859 void
860 clear_current_matrices (register struct frame *f)
862 /* Clear frame current matrix, if we have one. */
863 if (f->current_matrix)
864 clear_glyph_matrix (f->current_matrix);
866 /* Clear the matrix of the menu bar window, if such a window exists.
867 The menu bar window is currently used to display menus on X when
868 no toolkit support is compiled in. */
869 if (WINDOWP (f->menu_bar_window))
870 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
872 /* Clear the matrix of the tool-bar window, if any. */
873 if (WINDOWP (f->tool_bar_window))
874 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
876 /* Clear current window matrices. */
877 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
878 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
882 /* Clear out all display lines of F for a coming redisplay. */
884 void
885 clear_desired_matrices (register struct frame *f)
887 if (f->desired_matrix)
888 clear_glyph_matrix (f->desired_matrix);
890 if (WINDOWP (f->menu_bar_window))
891 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
893 if (WINDOWP (f->tool_bar_window))
894 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
896 /* Do it for window matrices. */
897 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
898 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
902 /* Clear matrices in window tree rooted in W. If DESIRED_P is
903 non-zero clear desired matrices, otherwise clear current matrices. */
905 static void
906 clear_window_matrices (struct window *w, int desired_p)
908 while (w)
910 if (!NILP (w->hchild))
912 xassert (WINDOWP (w->hchild));
913 clear_window_matrices (XWINDOW (w->hchild), desired_p);
915 else if (!NILP (w->vchild))
917 xassert (WINDOWP (w->vchild));
918 clear_window_matrices (XWINDOW (w->vchild), desired_p);
920 else
922 if (desired_p)
923 clear_glyph_matrix (w->desired_matrix);
924 else
926 clear_glyph_matrix (w->current_matrix);
927 w->window_end_valid = Qnil;
931 w = NILP (w->next) ? 0 : XWINDOW (w->next);
937 /***********************************************************************
938 Glyph Rows
940 See dispextern.h for an overall explanation of glyph rows.
941 ***********************************************************************/
943 /* Clear glyph row ROW. Do it in a way that makes it robust against
944 changes in the glyph_row structure, i.e. addition or removal of
945 structure members. */
947 static struct glyph_row null_row;
949 void
950 clear_glyph_row (struct glyph_row *row)
952 struct glyph *p[1 + LAST_AREA];
954 /* Save pointers. */
955 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
956 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
957 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
958 p[LAST_AREA] = row->glyphs[LAST_AREA];
960 /* Clear. */
961 *row = null_row;
963 /* Restore pointers. */
964 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
965 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
966 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
967 row->glyphs[LAST_AREA] = p[LAST_AREA];
969 #if 0 /* At some point, some bit-fields of struct glyph were not set,
970 which made glyphs unequal when compared with GLYPH_EQUAL_P.
971 Redisplay outputs such glyphs, and flickering effects were
972 the result. This also depended on the contents of memory
973 returned by xmalloc. If flickering happens again, activate
974 the code below. If the flickering is gone with that, chances
975 are that the flickering has the same reason as here. */
976 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
977 #endif
981 /* Make ROW an empty, enabled row of canonical character height,
982 in window W starting at y-position Y. */
984 void
985 blank_row (struct window *w, struct glyph_row *row, int y)
987 int min_y, max_y;
989 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
990 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
992 clear_glyph_row (row);
993 row->y = y;
994 row->ascent = row->phys_ascent = 0;
995 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
996 row->visible_height = row->height;
998 if (row->y < min_y)
999 row->visible_height -= min_y - row->y;
1000 if (row->y + row->height > max_y)
1001 row->visible_height -= row->y + row->height - max_y;
1003 row->enabled_p = 1;
1007 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
1008 are the amounts by which to change positions. Note that the first
1009 glyph of the text area of a row can have a buffer position even if
1010 the used count of the text area is zero. Such rows display line
1011 ends. */
1013 static void
1014 increment_row_positions (struct glyph_row *row,
1015 EMACS_INT delta, EMACS_INT delta_bytes)
1017 int area, i;
1019 /* Increment start and end positions. */
1020 MATRIX_ROW_START_CHARPOS (row) += delta;
1021 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
1022 MATRIX_ROW_END_CHARPOS (row) += delta;
1023 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
1024 CHARPOS (row->start.pos) += delta;
1025 BYTEPOS (row->start.pos) += delta_bytes;
1026 CHARPOS (row->end.pos) += delta;
1027 BYTEPOS (row->end.pos) += delta_bytes;
1029 if (!row->enabled_p)
1030 return;
1032 /* Increment positions in glyphs. */
1033 for (area = 0; area < LAST_AREA; ++area)
1034 for (i = 0; i < row->used[area]; ++i)
1035 if (BUFFERP (row->glyphs[area][i].object)
1036 && row->glyphs[area][i].charpos > 0)
1037 row->glyphs[area][i].charpos += delta;
1039 /* Capture the case of rows displaying a line end. */
1040 if (row->used[TEXT_AREA] == 0
1041 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
1042 row->glyphs[TEXT_AREA]->charpos += delta;
1046 #if 0
1047 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
1048 contents, i.e. glyph structure contents are exchanged between A and
1049 B without changing glyph pointers in A and B. */
1051 static void
1052 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
1054 int area;
1056 for (area = 0; area < LAST_AREA; ++area)
1058 /* Number of glyphs to swap. */
1059 int max_used = max (a->used[area], b->used[area]);
1061 /* Start of glyphs in area of row A. */
1062 struct glyph *glyph_a = a->glyphs[area];
1064 /* End + 1 of glyphs in area of row A. */
1065 struct glyph *glyph_a_end = a->glyphs[max_used];
1067 /* Start of glyphs in area of row B. */
1068 struct glyph *glyph_b = b->glyphs[area];
1070 while (glyph_a < glyph_a_end)
1072 /* Non-ISO HP/UX compiler doesn't like auto struct
1073 initialization. */
1074 struct glyph temp;
1075 temp = *glyph_a;
1076 *glyph_a = *glyph_b;
1077 *glyph_b = temp;
1078 ++glyph_a;
1079 ++glyph_b;
1084 #endif /* 0 */
1086 /* Exchange pointers to glyph memory between glyph rows A and B. */
1088 static inline void
1089 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1091 int i;
1092 for (i = 0; i < LAST_AREA + 1; ++i)
1094 struct glyph *temp = a->glyphs[i];
1095 a->glyphs[i] = b->glyphs[i];
1096 b->glyphs[i] = temp;
1101 /* Copy glyph row structure FROM to glyph row structure TO, except
1102 that glyph pointers in the structures are left unchanged. */
1104 static inline void
1105 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1107 struct glyph *pointers[1 + LAST_AREA];
1109 /* Save glyph pointers of TO. */
1110 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1112 /* Do a structure assignment. */
1113 *to = *from;
1115 /* Restore original pointers of TO. */
1116 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1120 /* Assign glyph row FROM to glyph row TO. This works like a structure
1121 assignment TO = FROM, except that glyph pointers are not copied but
1122 exchanged between TO and FROM. Pointers must be exchanged to avoid
1123 a memory leak. */
1125 static inline void
1126 assign_row (struct glyph_row *to, struct glyph_row *from)
1128 swap_glyph_pointers (to, from);
1129 copy_row_except_pointers (to, from);
1133 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1134 a row in a window matrix, is a slice of the glyph memory of the
1135 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1136 is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
1137 memory of FRAME_ROW. */
1139 #if GLYPH_DEBUG
1141 static int
1142 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1144 struct glyph *window_glyph_start = window_row->glyphs[0];
1145 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1146 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1148 return (frame_glyph_start <= window_glyph_start
1149 && window_glyph_start < frame_glyph_end);
1152 #endif /* GLYPH_DEBUG */
1154 #if 0
1156 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1157 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1158 in WINDOW_MATRIX is found satisfying the condition. */
1160 static struct glyph_row *
1161 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1162 struct glyph_matrix *frame_matrix, int row)
1164 int i;
1166 xassert (row >= 0 && row < frame_matrix->nrows);
1168 for (i = 0; i < window_matrix->nrows; ++i)
1169 if (glyph_row_slice_p (window_matrix->rows + i,
1170 frame_matrix->rows + row))
1171 break;
1173 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1176 #endif /* 0 */
1178 /* Prepare ROW for display. Desired rows are cleared lazily,
1179 i.e. they are only marked as to be cleared by setting their
1180 enabled_p flag to zero. When a row is to be displayed, a prior
1181 call to this function really clears it. */
1183 void
1184 prepare_desired_row (struct glyph_row *row)
1186 if (!row->enabled_p)
1188 int rp = row->reversed_p;
1190 clear_glyph_row (row);
1191 row->enabled_p = 1;
1192 row->reversed_p = rp;
1197 /* Return a hash code for glyph row ROW. */
1199 static int
1200 line_hash_code (struct glyph_row *row)
1202 int hash = 0;
1204 if (row->enabled_p)
1206 struct glyph *glyph = row->glyphs[TEXT_AREA];
1207 struct glyph *end = glyph + row->used[TEXT_AREA];
1209 while (glyph < end)
1211 int c = glyph->u.ch;
1212 int face_id = glyph->face_id;
1213 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1214 c -= SPACEGLYPH;
1215 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1216 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1217 ++glyph;
1220 if (hash == 0)
1221 hash = 1;
1224 return hash;
1228 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1229 the number of characters in the line. If must_write_spaces is
1230 zero, leading and trailing spaces are ignored. */
1232 static int
1233 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1235 struct glyph_row *row = matrix->rows + vpos;
1236 struct glyph *beg = row->glyphs[TEXT_AREA];
1237 struct glyph *end = beg + row->used[TEXT_AREA];
1238 int len;
1239 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1240 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1242 /* Ignore trailing and leading spaces if we can. */
1243 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1245 /* Skip from the end over trailing spaces. */
1246 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1247 --end;
1249 /* All blank line. */
1250 if (end == beg)
1251 return 0;
1253 /* Skip over leading spaces. */
1254 while (CHAR_GLYPH_SPACE_P (*beg))
1255 ++beg;
1258 /* If we don't have a glyph-table, each glyph is one character,
1259 so return the number of glyphs. */
1260 if (glyph_table_base == 0)
1261 len = end - beg;
1262 else
1264 /* Otherwise, scan the glyphs and accumulate their total length
1265 in LEN. */
1266 len = 0;
1267 while (beg < end)
1269 GLYPH g;
1271 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1273 if (GLYPH_INVALID_P (g)
1274 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1275 len += 1;
1276 else
1277 len += GLYPH_LENGTH (glyph_table_base, g);
1279 ++beg;
1283 return len;
1287 /* Test two glyph rows A and B for equality. Value is non-zero if A
1288 and B have equal contents. MOUSE_FACE_P non-zero means compare the
1289 mouse_face_p flags of A and B, too. */
1291 static inline int
1292 row_equal_p (struct glyph_row *a, struct glyph_row *b, int mouse_face_p)
1294 xassert (verify_row_hash (a));
1295 xassert (verify_row_hash (b));
1297 if (a == b)
1298 return 1;
1299 else if (a->hash != b->hash)
1300 return 0;
1301 else
1303 struct glyph *a_glyph, *b_glyph, *a_end;
1304 int area;
1306 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1307 return 0;
1309 /* Compare glyphs. */
1310 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1312 if (a->used[area] != b->used[area])
1313 return 0;
1315 a_glyph = a->glyphs[area];
1316 a_end = a_glyph + a->used[area];
1317 b_glyph = b->glyphs[area];
1319 while (a_glyph < a_end
1320 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1321 ++a_glyph, ++b_glyph;
1323 if (a_glyph != a_end)
1324 return 0;
1327 if (a->fill_line_p != b->fill_line_p
1328 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1329 || a->left_fringe_bitmap != b->left_fringe_bitmap
1330 || a->left_fringe_face_id != b->left_fringe_face_id
1331 || a->left_fringe_offset != b->left_fringe_offset
1332 || a->right_fringe_bitmap != b->right_fringe_bitmap
1333 || a->right_fringe_face_id != b->right_fringe_face_id
1334 || a->right_fringe_offset != b->right_fringe_offset
1335 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1336 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1337 || a->exact_window_width_line_p != b->exact_window_width_line_p
1338 || a->overlapped_p != b->overlapped_p
1339 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1340 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1341 || a->reversed_p != b->reversed_p
1342 /* Different partially visible characters on left margin. */
1343 || a->x != b->x
1344 /* Different height. */
1345 || a->ascent != b->ascent
1346 || a->phys_ascent != b->phys_ascent
1347 || a->phys_height != b->phys_height
1348 || a->visible_height != b->visible_height)
1349 return 0;
1352 return 1;
1357 /***********************************************************************
1358 Glyph Pool
1360 See dispextern.h for an overall explanation of glyph pools.
1361 ***********************************************************************/
1363 /* Allocate a glyph_pool structure. The structure returned is
1364 initialized with zeros. The global variable glyph_pool_count is
1365 incremented for each pool allocated. */
1367 static struct glyph_pool *
1368 new_glyph_pool (void)
1370 struct glyph_pool *result;
1372 /* Allocate a new glyph_pool and clear it. */
1373 result = (struct glyph_pool *) xmalloc (sizeof *result);
1374 memset (result, 0, sizeof *result);
1376 /* For memory leak and double deletion checking. */
1377 ++glyph_pool_count;
1379 return result;
1383 /* Free a glyph_pool structure POOL. The function may be called with
1384 a null POOL pointer. The global variable glyph_pool_count is
1385 decremented with every pool structure freed. If this count gets
1386 negative, more structures were freed than allocated, i.e. one
1387 structure must have been freed more than once or a bogus pointer
1388 was passed to free_glyph_pool. */
1390 static void
1391 free_glyph_pool (struct glyph_pool *pool)
1393 if (pool)
1395 /* More freed than allocated? */
1396 --glyph_pool_count;
1397 xassert (glyph_pool_count >= 0);
1399 xfree (pool->glyphs);
1400 xfree (pool);
1405 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1406 columns we need. This function never shrinks a pool. The only
1407 case in which this would make sense, would be when a frame's size
1408 is changed from a large value to a smaller one. But, if someone
1409 does it once, we can expect that he will do it again.
1411 Value is non-zero if the pool changed in a way which makes
1412 re-adjusting window glyph matrices necessary. */
1414 static int
1415 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1417 ptrdiff_t needed;
1418 int changed_p;
1420 changed_p = (pool->glyphs == 0
1421 || matrix_dim.height != pool->nrows
1422 || matrix_dim.width != pool->ncolumns);
1424 /* Enlarge the glyph pool. */
1425 needed = matrix_dim.width;
1426 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1427 memory_full (SIZE_MAX);
1428 needed *= matrix_dim.height;
1429 if (needed > pool->nglyphs)
1431 ptrdiff_t old_nglyphs = pool->nglyphs;
1432 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1433 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1434 memset (pool->glyphs + old_nglyphs, 0,
1435 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1438 /* Remember the number of rows and columns because (a) we use them
1439 to do sanity checks, and (b) the number of columns determines
1440 where rows in the frame matrix start---this must be available to
1441 determine pointers to rows of window sub-matrices. */
1442 pool->nrows = matrix_dim.height;
1443 pool->ncolumns = matrix_dim.width;
1445 return changed_p;
1450 /***********************************************************************
1451 Debug Code
1452 ***********************************************************************/
1454 #if GLYPH_DEBUG
1457 /* Flush standard output. This is sometimes useful to call from the debugger.
1458 XXX Maybe this should be changed to flush the current terminal instead of
1459 stdout.
1462 void flush_stdout (void) EXTERNALLY_VISIBLE;
1464 void
1465 flush_stdout (void)
1467 fflush (stdout);
1471 /* Check that no glyph pointers have been lost in MATRIX. If a
1472 pointer has been lost, e.g. by using a structure assignment between
1473 rows, at least one pointer must occur more than once in the rows of
1474 MATRIX. */
1476 void
1477 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1479 int i, j;
1481 for (i = 0; i < matrix->nrows; ++i)
1482 for (j = 0; j < matrix->nrows; ++j)
1483 xassert (i == j
1484 || (matrix->rows[i].glyphs[TEXT_AREA]
1485 != matrix->rows[j].glyphs[TEXT_AREA]));
1489 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1491 struct glyph_row *
1492 matrix_row (struct glyph_matrix *matrix, int row)
1494 xassert (matrix && matrix->rows);
1495 xassert (row >= 0 && row < matrix->nrows);
1497 /* That's really too slow for normal testing because this function
1498 is called almost everywhere. Although---it's still astonishingly
1499 fast, so it is valuable to have for debugging purposes. */
1500 #if 0
1501 check_matrix_pointer_lossage (matrix);
1502 #endif
1504 return matrix->rows + row;
1508 #if 0 /* This function makes invalid assumptions when text is
1509 partially invisible. But it might come handy for debugging
1510 nevertheless. */
1512 /* Check invariants that must hold for an up to date current matrix of
1513 window W. */
1515 static void
1516 check_matrix_invariants (struct window *w)
1518 struct glyph_matrix *matrix = w->current_matrix;
1519 int yb = window_text_bottom_y (w);
1520 struct glyph_row *row = matrix->rows;
1521 struct glyph_row *last_text_row = NULL;
1522 struct buffer *saved = current_buffer;
1523 struct buffer *buffer = XBUFFER (w->buffer);
1524 int c;
1526 /* This can sometimes happen for a fresh window. */
1527 if (matrix->nrows < 2)
1528 return;
1530 set_buffer_temp (buffer);
1532 /* Note: last row is always reserved for the mode line. */
1533 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1534 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1536 struct glyph_row *next = row + 1;
1538 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1539 last_text_row = row;
1541 /* Check that character and byte positions are in sync. */
1542 xassert (MATRIX_ROW_START_BYTEPOS (row)
1543 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1544 xassert (BYTEPOS (row->start.pos)
1545 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1547 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1548 have such a position temporarily in case of a minibuffer
1549 displaying something like `[Sole completion]' at its end. */
1550 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1552 xassert (MATRIX_ROW_END_BYTEPOS (row)
1553 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1554 xassert (BYTEPOS (row->end.pos)
1555 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1558 /* Check that end position of `row' is equal to start position
1559 of next row. */
1560 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1562 xassert (MATRIX_ROW_END_CHARPOS (row)
1563 == MATRIX_ROW_START_CHARPOS (next));
1564 xassert (MATRIX_ROW_END_BYTEPOS (row)
1565 == MATRIX_ROW_START_BYTEPOS (next));
1566 xassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1567 xassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1569 row = next;
1572 xassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1573 xassert (w->desired_matrix->rows != NULL);
1574 set_buffer_temp (saved);
1577 #endif /* 0 */
1579 #endif /* GLYPH_DEBUG != 0 */
1583 /**********************************************************************
1584 Allocating/ Adjusting Glyph Matrices
1585 **********************************************************************/
1587 /* Allocate glyph matrices over a window tree for a frame-based
1588 redisplay
1590 X and Y are column/row within the frame glyph matrix where
1591 sub-matrices for the window tree rooted at WINDOW must be
1592 allocated. DIM_ONLY_P non-zero means that the caller of this
1593 function is only interested in the result matrix dimension, and
1594 matrix adjustments should not be performed.
1596 The function returns the total width/height of the sub-matrices of
1597 the window tree. If called on a frame root window, the computation
1598 will take the mini-buffer window into account.
1600 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1602 NEW_LEAF_MATRIX set if any window in the tree did not have a
1603 glyph matrices yet, and
1605 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1606 any window in the tree will be changed or have been changed (see
1607 DIM_ONLY_P)
1609 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1610 function.
1612 Windows are arranged into chains of windows on the same level
1613 through the next fields of window structures. Such a level can be
1614 either a sequence of horizontally adjacent windows from left to
1615 right, or a sequence of vertically adjacent windows from top to
1616 bottom. Each window in a horizontal sequence can be either a leaf
1617 window or a vertical sequence; a window in a vertical sequence can
1618 be either a leaf or a horizontal sequence. All windows in a
1619 horizontal sequence have the same height, and all windows in a
1620 vertical sequence have the same width.
1622 This function uses, for historical reasons, a more general
1623 algorithm to determine glyph matrix dimensions that would be
1624 necessary.
1626 The matrix height of a horizontal sequence is determined by the
1627 maximum height of any matrix in the sequence. The matrix width of
1628 a horizontal sequence is computed by adding up matrix widths of
1629 windows in the sequence.
1631 |<------- result width ------->|
1632 +---------+----------+---------+ ---
1633 | | | | |
1634 | | | |
1635 +---------+ | | result height
1636 | +---------+
1637 | | |
1638 +----------+ ---
1640 The matrix width of a vertical sequence is the maximum matrix width
1641 of any window in the sequence. Its height is computed by adding up
1642 matrix heights of windows in the sequence.
1644 |<---- result width -->|
1645 +---------+ ---
1646 | | |
1647 | | |
1648 +---------+--+ |
1649 | | |
1650 | | result height
1652 +------------+---------+ |
1653 | | |
1654 | | |
1655 +------------+---------+ --- */
1657 /* Bit indicating that a new matrix will be allocated or has been
1658 allocated. */
1660 #define NEW_LEAF_MATRIX (1 << 0)
1662 /* Bit indicating that a matrix will or has changed its location or
1663 size. */
1665 #define CHANGED_LEAF_MATRIX (1 << 1)
1667 static struct dim
1668 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1669 int dim_only_p, int *window_change_flags)
1671 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1672 int x0 = x, y0 = y;
1673 int wmax = 0, hmax = 0;
1674 struct dim total;
1675 struct dim dim;
1676 struct window *w;
1677 int in_horz_combination_p;
1679 /* What combination is WINDOW part of? Compute this once since the
1680 result is the same for all windows in the `next' chain. The
1681 special case of a root window (parent equal to nil) is treated
1682 like a vertical combination because a root window's `next'
1683 points to the mini-buffer window, if any, which is arranged
1684 vertically below other windows. */
1685 in_horz_combination_p
1686 = (!NILP (XWINDOW (window)->parent)
1687 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1689 /* For WINDOW and all windows on the same level. */
1692 w = XWINDOW (window);
1694 /* Get the dimension of the window sub-matrix for W, depending
1695 on whether this is a combination or a leaf window. */
1696 if (!NILP (w->hchild))
1697 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
1698 dim_only_p,
1699 window_change_flags);
1700 else if (!NILP (w->vchild))
1701 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
1702 dim_only_p,
1703 window_change_flags);
1704 else
1706 /* If not already done, allocate sub-matrix structures. */
1707 if (w->desired_matrix == NULL)
1709 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1710 w->current_matrix = new_glyph_matrix (f->current_pool);
1711 *window_change_flags |= NEW_LEAF_MATRIX;
1714 /* Width and height MUST be chosen so that there are no
1715 holes in the frame matrix. */
1716 dim.width = required_matrix_width (w);
1717 dim.height = required_matrix_height (w);
1719 /* Will matrix be re-allocated? */
1720 if (x != w->desired_matrix->matrix_x
1721 || y != w->desired_matrix->matrix_y
1722 || dim.width != w->desired_matrix->matrix_w
1723 || dim.height != w->desired_matrix->matrix_h
1724 || (margin_glyphs_to_reserve (w, dim.width,
1725 w->left_margin_cols)
1726 != w->desired_matrix->left_margin_glyphs)
1727 || (margin_glyphs_to_reserve (w, dim.width,
1728 w->right_margin_cols)
1729 != w->desired_matrix->right_margin_glyphs))
1730 *window_change_flags |= CHANGED_LEAF_MATRIX;
1732 /* Actually change matrices, if allowed. Do not consider
1733 CHANGED_LEAF_MATRIX computed above here because the pool
1734 may have been changed which we don't now here. We trust
1735 that we only will be called with DIM_ONLY_P != 0 when
1736 necessary. */
1737 if (!dim_only_p)
1739 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1740 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1744 /* If we are part of a horizontal combination, advance x for
1745 windows to the right of W; otherwise advance y for windows
1746 below W. */
1747 if (in_horz_combination_p)
1748 x += dim.width;
1749 else
1750 y += dim.height;
1752 /* Remember maximum glyph matrix dimensions. */
1753 wmax = max (wmax, dim.width);
1754 hmax = max (hmax, dim.height);
1756 /* Next window on same level. */
1757 window = w->next;
1759 while (!NILP (window));
1761 /* Set `total' to the total glyph matrix dimension of this window
1762 level. In a vertical combination, the width is the width of the
1763 widest window; the height is the y we finally reached, corrected
1764 by the y we started with. In a horizontal combination, the total
1765 height is the height of the tallest window, and the width is the
1766 x we finally reached, corrected by the x we started with. */
1767 if (in_horz_combination_p)
1769 total.width = x - x0;
1770 total.height = hmax;
1772 else
1774 total.width = wmax;
1775 total.height = y - y0;
1778 return total;
1782 /* Return the required height of glyph matrices for window W. */
1784 static int
1785 required_matrix_height (struct window *w)
1787 #ifdef HAVE_WINDOW_SYSTEM
1788 struct frame *f = XFRAME (w->frame);
1790 if (FRAME_WINDOW_P (f))
1792 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1793 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1794 return (((window_pixel_height + ch_height - 1)
1795 / ch_height) * w->nrows_scale_factor
1796 /* One partially visible line at the top and
1797 bottom of the window. */
1799 /* 2 for header and mode line. */
1800 + 2);
1802 #endif /* HAVE_WINDOW_SYSTEM */
1804 return WINDOW_TOTAL_LINES (w);
1808 /* Return the required width of glyph matrices for window W. */
1810 static int
1811 required_matrix_width (struct window *w)
1813 #ifdef HAVE_WINDOW_SYSTEM
1814 struct frame *f = XFRAME (w->frame);
1815 if (FRAME_WINDOW_P (f))
1817 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1818 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1820 /* Compute number of glyphs needed in a glyph row. */
1821 return (((window_pixel_width + ch_width - 1)
1822 / ch_width) * w->ncols_scale_factor
1823 /* 2 partially visible columns in the text area. */
1825 /* One partially visible column at the right
1826 edge of each marginal area. */
1827 + 1 + 1);
1829 #endif /* HAVE_WINDOW_SYSTEM */
1831 return XINT (w->total_cols);
1835 /* Allocate window matrices for window-based redisplay. W is the
1836 window whose matrices must be allocated/reallocated. */
1838 static void
1839 allocate_matrices_for_window_redisplay (struct window *w)
1841 while (w)
1843 if (!NILP (w->vchild))
1844 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
1845 else if (!NILP (w->hchild))
1846 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
1847 else
1849 /* W is a leaf window. */
1850 struct dim dim;
1852 /* If matrices are not yet allocated, allocate them now. */
1853 if (w->desired_matrix == NULL)
1855 w->desired_matrix = new_glyph_matrix (NULL);
1856 w->current_matrix = new_glyph_matrix (NULL);
1859 dim.width = required_matrix_width (w);
1860 dim.height = required_matrix_height (w);
1861 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1862 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1865 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1870 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1871 do it for all frames; otherwise do it just for the given frame.
1872 This function must be called when a new frame is created, its size
1873 changes, or its window configuration changes. */
1875 void
1876 adjust_glyphs (struct frame *f)
1878 /* Block input so that expose events and other events that access
1879 glyph matrices are not processed while we are changing them. */
1880 BLOCK_INPUT;
1882 if (f)
1883 adjust_frame_glyphs (f);
1884 else
1886 Lisp_Object tail, lisp_frame;
1888 FOR_EACH_FRAME (tail, lisp_frame)
1889 adjust_frame_glyphs (XFRAME (lisp_frame));
1892 UNBLOCK_INPUT;
1896 /* Adjust frame glyphs when Emacs is initialized.
1898 To be called from init_display.
1900 We need a glyph matrix because redraw will happen soon.
1901 Unfortunately, window sizes on selected_frame are not yet set to
1902 meaningful values. I believe we can assume that there are only two
1903 windows on the frame---the mini-buffer and the root window. Frame
1904 height and width seem to be correct so far. So, set the sizes of
1905 windows to estimated values. */
1907 static void
1908 adjust_frame_glyphs_initially (void)
1910 struct frame *sf = SELECTED_FRAME ();
1911 struct window *root = XWINDOW (sf->root_window);
1912 struct window *mini = XWINDOW (root->next);
1913 int frame_lines = FRAME_LINES (sf);
1914 int frame_cols = FRAME_COLS (sf);
1915 int top_margin = FRAME_TOP_MARGIN (sf);
1917 /* Do it for the root window. */
1918 XSETFASTINT (root->top_line, top_margin);
1919 XSETFASTINT (root->total_lines, frame_lines - 1 - top_margin);
1920 XSETFASTINT (root->total_cols, frame_cols);
1922 /* Do it for the mini-buffer window. */
1923 XSETFASTINT (mini->top_line, frame_lines - 1);
1924 XSETFASTINT (mini->total_lines, 1);
1925 XSETFASTINT (mini->total_cols, frame_cols);
1927 adjust_frame_glyphs (sf);
1928 glyphs_initialized_initially_p = 1;
1932 /* Allocate/reallocate glyph matrices of a single frame F. */
1934 static void
1935 adjust_frame_glyphs (struct frame *f)
1937 if (FRAME_WINDOW_P (f))
1938 adjust_frame_glyphs_for_window_redisplay (f);
1939 else
1940 adjust_frame_glyphs_for_frame_redisplay (f);
1942 /* Don't forget the message buffer and the buffer for
1943 decode_mode_spec. */
1944 adjust_frame_message_buffer (f);
1945 adjust_decode_mode_spec_buffer (f);
1947 f->glyphs_initialized_p = 1;
1950 /* Return 1 if any window in the tree has nonzero window margins. See
1951 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1952 static int
1953 showing_window_margins_p (struct window *w)
1955 while (w)
1957 if (!NILP (w->hchild))
1959 if (showing_window_margins_p (XWINDOW (w->hchild)))
1960 return 1;
1962 else if (!NILP (w->vchild))
1964 if (showing_window_margins_p (XWINDOW (w->vchild)))
1965 return 1;
1967 else if (!NILP (w->left_margin_cols)
1968 || !NILP (w->right_margin_cols))
1969 return 1;
1971 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1973 return 0;
1977 /* In the window tree with root W, build current matrices of leaf
1978 windows from the frame's current matrix. */
1980 static void
1981 fake_current_matrices (Lisp_Object window)
1983 struct window *w;
1985 for (; !NILP (window); window = w->next)
1987 w = XWINDOW (window);
1989 if (!NILP (w->hchild))
1990 fake_current_matrices (w->hchild);
1991 else if (!NILP (w->vchild))
1992 fake_current_matrices (w->vchild);
1993 else
1995 int i;
1996 struct frame *f = XFRAME (w->frame);
1997 struct glyph_matrix *m = w->current_matrix;
1998 struct glyph_matrix *fm = f->current_matrix;
2000 xassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
2001 xassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
2003 for (i = 0; i < m->matrix_h; ++i)
2005 struct glyph_row *r = m->rows + i;
2006 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
2008 xassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
2009 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
2011 r->enabled_p = fr->enabled_p;
2012 if (r->enabled_p)
2014 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
2015 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
2016 r->used[TEXT_AREA] = (m->matrix_w
2017 - r->used[LEFT_MARGIN_AREA]
2018 - r->used[RIGHT_MARGIN_AREA]);
2019 r->mode_line_p = 0;
2027 /* Save away the contents of frame F's current frame matrix. Value is
2028 a glyph matrix holding the contents of F's current frame matrix. */
2030 static struct glyph_matrix *
2031 save_current_matrix (struct frame *f)
2033 int i;
2034 struct glyph_matrix *saved;
2036 saved = (struct glyph_matrix *) xmalloc (sizeof *saved);
2037 memset (saved, 0, sizeof *saved);
2038 saved->nrows = f->current_matrix->nrows;
2039 saved->rows = (struct glyph_row *) xmalloc (saved->nrows
2040 * sizeof *saved->rows);
2041 memset (saved->rows, 0, saved->nrows * sizeof *saved->rows);
2043 for (i = 0; i < saved->nrows; ++i)
2045 struct glyph_row *from = f->current_matrix->rows + i;
2046 struct glyph_row *to = saved->rows + i;
2047 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2048 to->glyphs[TEXT_AREA] = (struct glyph *) xmalloc (nbytes);
2049 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2050 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2053 return saved;
2057 /* Restore the contents of frame F's current frame matrix from SAVED,
2058 and free memory associated with SAVED. */
2060 static void
2061 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
2063 int i;
2065 for (i = 0; i < saved->nrows; ++i)
2067 struct glyph_row *from = saved->rows + i;
2068 struct glyph_row *to = f->current_matrix->rows + i;
2069 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2070 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2071 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2072 xfree (from->glyphs[TEXT_AREA]);
2075 xfree (saved->rows);
2076 xfree (saved);
2081 /* Allocate/reallocate glyph matrices of a single frame F for
2082 frame-based redisplay. */
2084 static void
2085 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
2087 struct dim matrix_dim;
2088 int pool_changed_p;
2089 int window_change_flags;
2090 int top_window_y;
2092 if (!FRAME_LIVE_P (f))
2093 return;
2095 top_window_y = FRAME_TOP_MARGIN (f);
2097 /* Allocate glyph pool structures if not already done. */
2098 if (f->desired_pool == NULL)
2100 f->desired_pool = new_glyph_pool ();
2101 f->current_pool = new_glyph_pool ();
2104 /* Allocate frames matrix structures if needed. */
2105 if (f->desired_matrix == NULL)
2107 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2108 f->current_matrix = new_glyph_matrix (f->current_pool);
2111 /* Compute window glyph matrices. (This takes the mini-buffer
2112 window into account). The result is the size of the frame glyph
2113 matrix needed. The variable window_change_flags is set to a bit
2114 mask indicating whether new matrices will be allocated or
2115 existing matrices change their size or location within the frame
2116 matrix. */
2117 window_change_flags = 0;
2118 matrix_dim
2119 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2120 0, top_window_y,
2122 &window_change_flags);
2124 /* Add in menu bar lines, if any. */
2125 matrix_dim.height += top_window_y;
2127 /* Enlarge pools as necessary. */
2128 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2129 realloc_glyph_pool (f->current_pool, matrix_dim);
2131 /* Set up glyph pointers within window matrices. Do this only if
2132 absolutely necessary since it requires a frame redraw. */
2133 if (pool_changed_p || window_change_flags)
2135 /* Do it for window matrices. */
2136 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2137 0, top_window_y, 0,
2138 &window_change_flags);
2140 /* Size of frame matrices must equal size of frame. Note
2141 that we are called for X frames with window widths NOT equal
2142 to the frame width (from CHANGE_FRAME_SIZE_1). */
2143 xassert (matrix_dim.width == FRAME_COLS (f)
2144 && matrix_dim.height == FRAME_LINES (f));
2146 /* Pointers to glyph memory in glyph rows are exchanged during
2147 the update phase of redisplay, which means in general that a
2148 frame's current matrix consists of pointers into both the
2149 desired and current glyph pool of the frame. Adjusting a
2150 matrix sets the frame matrix up so that pointers are all into
2151 the same pool. If we want to preserve glyph contents of the
2152 current matrix over a call to adjust_glyph_matrix, we must
2153 make a copy of the current glyphs, and restore the current
2154 matrix' contents from that copy. */
2155 if (display_completed
2156 && !FRAME_GARBAGED_P (f)
2157 && matrix_dim.width == f->current_matrix->matrix_w
2158 && matrix_dim.height == f->current_matrix->matrix_h
2159 /* For some reason, the frame glyph matrix gets corrupted if
2160 any of the windows contain margins. I haven't been able
2161 to hunt down the reason, but for the moment this prevents
2162 the problem from manifesting. -- cyd */
2163 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2165 struct glyph_matrix *copy = save_current_matrix (f);
2166 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2167 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2168 restore_current_matrix (f, copy);
2169 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2171 else
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 SET_FRAME_GARBAGED (f);
2181 /* Allocate/reallocate glyph matrices of a single frame F for
2182 window-based redisplay. */
2184 static void
2185 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2187 xassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2189 /* Allocate/reallocate window matrices. */
2190 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2192 #ifdef HAVE_X_WINDOWS
2193 /* Allocate/ reallocate matrices of the dummy window used to display
2194 the menu bar under X when no X toolkit support is available. */
2195 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2197 /* Allocate a dummy window if not already done. */
2198 struct window *w;
2199 if (NILP (f->menu_bar_window))
2201 f->menu_bar_window = make_window ();
2202 w = XWINDOW (f->menu_bar_window);
2203 XSETFRAME (w->frame, f);
2204 w->pseudo_window_p = 1;
2206 else
2207 w = XWINDOW (f->menu_bar_window);
2209 /* Set window dimensions to frame dimensions and allocate or
2210 adjust glyph matrices of W. */
2211 XSETFASTINT (w->top_line, 0);
2212 XSETFASTINT (w->left_col, 0);
2213 XSETFASTINT (w->total_lines, FRAME_MENU_BAR_LINES (f));
2214 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2215 allocate_matrices_for_window_redisplay (w);
2217 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2218 #endif /* HAVE_X_WINDOWS */
2220 #ifndef USE_GTK
2222 /* Allocate/ reallocate matrices of the tool bar window. If we
2223 don't have a tool bar window yet, make one. */
2224 struct window *w;
2225 if (NILP (f->tool_bar_window))
2227 f->tool_bar_window = make_window ();
2228 w = XWINDOW (f->tool_bar_window);
2229 XSETFRAME (w->frame, f);
2230 w->pseudo_window_p = 1;
2232 else
2233 w = XWINDOW (f->tool_bar_window);
2235 XSETFASTINT (w->top_line, FRAME_MENU_BAR_LINES (f));
2236 XSETFASTINT (w->left_col, 0);
2237 XSETFASTINT (w->total_lines, FRAME_TOOL_BAR_LINES (f));
2238 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2239 allocate_matrices_for_window_redisplay (w);
2241 #endif
2245 /* Adjust/ allocate message buffer of frame F.
2247 Note that the message buffer is never freed. Since I could not
2248 find a free in 19.34, I assume that freeing it would be
2249 problematic in some way and don't do it either.
2251 (Implementation note: It should be checked if we can free it
2252 eventually without causing trouble). */
2254 static void
2255 adjust_frame_message_buffer (struct frame *f)
2257 ptrdiff_t size = FRAME_MESSAGE_BUF_SIZE (f) + 1;
2259 if (FRAME_MESSAGE_BUF (f))
2261 char *buffer = FRAME_MESSAGE_BUF (f);
2262 char *new_buffer = (char *) xrealloc (buffer, size);
2263 FRAME_MESSAGE_BUF (f) = new_buffer;
2265 else
2266 FRAME_MESSAGE_BUF (f) = (char *) xmalloc (size);
2270 /* Re-allocate buffer for decode_mode_spec on frame F. */
2272 static void
2273 adjust_decode_mode_spec_buffer (struct frame *f)
2275 f->decode_mode_spec_buffer
2276 = (char *) xrealloc (f->decode_mode_spec_buffer,
2277 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2282 /**********************************************************************
2283 Freeing Glyph Matrices
2284 **********************************************************************/
2286 /* Free glyph memory for a frame F. F may be null. This function can
2287 be called for the same frame more than once. The root window of
2288 F may be nil when this function is called. This is the case when
2289 the function is called when F is destroyed. */
2291 void
2292 free_glyphs (struct frame *f)
2294 if (f && f->glyphs_initialized_p)
2296 /* Block interrupt input so that we don't get surprised by an X
2297 event while we're in an inconsistent state. */
2298 BLOCK_INPUT;
2299 f->glyphs_initialized_p = 0;
2301 /* Release window sub-matrices. */
2302 if (!NILP (f->root_window))
2303 free_window_matrices (XWINDOW (f->root_window));
2305 /* Free the dummy window for menu bars without X toolkit and its
2306 glyph matrices. */
2307 if (!NILP (f->menu_bar_window))
2309 struct window *w = XWINDOW (f->menu_bar_window);
2310 free_glyph_matrix (w->desired_matrix);
2311 free_glyph_matrix (w->current_matrix);
2312 w->desired_matrix = w->current_matrix = NULL;
2313 f->menu_bar_window = Qnil;
2316 /* Free the tool bar window and its glyph matrices. */
2317 if (!NILP (f->tool_bar_window))
2319 struct window *w = XWINDOW (f->tool_bar_window);
2320 free_glyph_matrix (w->desired_matrix);
2321 free_glyph_matrix (w->current_matrix);
2322 w->desired_matrix = w->current_matrix = NULL;
2323 f->tool_bar_window = Qnil;
2326 /* Release frame glyph matrices. Reset fields to zero in
2327 case we are called a second time. */
2328 if (f->desired_matrix)
2330 free_glyph_matrix (f->desired_matrix);
2331 free_glyph_matrix (f->current_matrix);
2332 f->desired_matrix = f->current_matrix = NULL;
2335 /* Release glyph pools. */
2336 if (f->desired_pool)
2338 free_glyph_pool (f->desired_pool);
2339 free_glyph_pool (f->current_pool);
2340 f->desired_pool = f->current_pool = NULL;
2343 UNBLOCK_INPUT;
2348 /* Free glyph sub-matrices in the window tree rooted at W. This
2349 function may be called with a null pointer, and it may be called on
2350 the same tree more than once. */
2352 void
2353 free_window_matrices (struct window *w)
2355 while (w)
2357 if (!NILP (w->hchild))
2358 free_window_matrices (XWINDOW (w->hchild));
2359 else if (!NILP (w->vchild))
2360 free_window_matrices (XWINDOW (w->vchild));
2361 else
2363 /* This is a leaf window. Free its memory and reset fields
2364 to zero in case this function is called a second time for
2365 W. */
2366 free_glyph_matrix (w->current_matrix);
2367 free_glyph_matrix (w->desired_matrix);
2368 w->current_matrix = w->desired_matrix = NULL;
2371 /* Next window on same level. */
2372 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2377 /* Check glyph memory leaks. This function is called from
2378 shut_down_emacs. Note that frames are not destroyed when Emacs
2379 exits. We therefore free all glyph memory for all active frames
2380 explicitly and check that nothing is left allocated. */
2382 void
2383 check_glyph_memory (void)
2385 Lisp_Object tail, frame;
2387 /* Free glyph memory for all frames. */
2388 FOR_EACH_FRAME (tail, frame)
2389 free_glyphs (XFRAME (frame));
2391 /* Check that nothing is left allocated. */
2392 if (glyph_matrix_count)
2393 abort ();
2394 if (glyph_pool_count)
2395 abort ();
2400 /**********************************************************************
2401 Building a Frame Matrix
2402 **********************************************************************/
2404 /* Most of the redisplay code works on glyph matrices attached to
2405 windows. This is a good solution most of the time, but it is not
2406 suitable for terminal code. Terminal output functions cannot rely
2407 on being able to set an arbitrary terminal window. Instead they
2408 must be provided with a view of the whole frame, i.e. the whole
2409 screen. We build such a view by constructing a frame matrix from
2410 window matrices in this section.
2412 Windows that must be updated have their must_be_update_p flag set.
2413 For all such windows, their desired matrix is made part of the
2414 desired frame matrix. For other windows, their current matrix is
2415 made part of the desired frame matrix.
2417 +-----------------+----------------+
2418 | desired | desired |
2419 | | |
2420 +-----------------+----------------+
2421 | current |
2423 +----------------------------------+
2425 Desired window matrices can be made part of the frame matrix in a
2426 cheap way: We exploit the fact that the desired frame matrix and
2427 desired window matrices share their glyph memory. This is not
2428 possible for current window matrices. Their glyphs are copied to
2429 the desired frame matrix. The latter is equivalent to
2430 preserve_other_columns in the old redisplay.
2432 Used glyphs counters for frame matrix rows are the result of adding
2433 up glyph lengths of the window matrices. A line in the frame
2434 matrix is enabled, if a corresponding line in a window matrix is
2435 enabled.
2437 After building the desired frame matrix, it will be passed to
2438 terminal code, which will manipulate both the desired and current
2439 frame matrix. Changes applied to the frame's current matrix have
2440 to be visible in current window matrices afterwards, of course.
2442 This problem is solved like this:
2444 1. Window and frame matrices share glyphs. Window matrices are
2445 constructed in a way that their glyph contents ARE the glyph
2446 contents needed in a frame matrix. Thus, any modification of
2447 glyphs done in terminal code will be reflected in window matrices
2448 automatically.
2450 2. Exchanges of rows in a frame matrix done by terminal code are
2451 intercepted by hook functions so that corresponding row operations
2452 on window matrices can be performed. This is necessary because we
2453 use pointers to glyphs in glyph row structures. To satisfy the
2454 assumption of point 1 above that glyphs are updated implicitly in
2455 window matrices when they are manipulated via the frame matrix,
2456 window and frame matrix must of course agree where to find the
2457 glyphs for their rows. Possible manipulations that must be
2458 mirrored are assignments of rows of the desired frame matrix to the
2459 current frame matrix and scrolling the current frame matrix. */
2461 /* Build frame F's desired matrix from window matrices. Only windows
2462 which have the flag must_be_updated_p set have to be updated. Menu
2463 bar lines of a frame are not covered by window matrices, so make
2464 sure not to touch them in this function. */
2466 static void
2467 build_frame_matrix (struct frame *f)
2469 int i;
2471 /* F must have a frame matrix when this function is called. */
2472 xassert (!FRAME_WINDOW_P (f));
2474 /* Clear all rows in the frame matrix covered by window matrices.
2475 Menu bar lines are not covered by windows. */
2476 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2477 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2479 /* Build the matrix by walking the window tree. */
2480 build_frame_matrix_from_window_tree (f->desired_matrix,
2481 XWINDOW (FRAME_ROOT_WINDOW (f)));
2485 /* Walk a window tree, building a frame matrix MATRIX from window
2486 matrices. W is the root of a window tree. */
2488 static void
2489 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2491 while (w)
2493 if (!NILP (w->hchild))
2494 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2495 else if (!NILP (w->vchild))
2496 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2497 else
2498 build_frame_matrix_from_leaf_window (matrix, w);
2500 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2505 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2506 desired frame matrix built. W is a leaf window whose desired or
2507 current matrix is to be added to FRAME_MATRIX. W's flag
2508 must_be_updated_p determines which matrix it contributes to
2509 FRAME_MATRIX. If must_be_updated_p is non-zero, W's desired matrix
2510 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2511 Adding a desired matrix means setting up used counters and such in
2512 frame rows, while adding a current window matrix to FRAME_MATRIX
2513 means copying glyphs. The latter case corresponds to
2514 preserve_other_columns in the old redisplay. */
2516 static void
2517 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2519 struct glyph_matrix *window_matrix;
2520 int window_y, frame_y;
2521 /* If non-zero, a glyph to insert at the right border of W. */
2522 GLYPH right_border_glyph;
2524 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2526 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2527 if (w->must_be_updated_p)
2529 window_matrix = w->desired_matrix;
2531 /* Decide whether we want to add a vertical border glyph. */
2532 if (!WINDOW_RIGHTMOST_P (w))
2534 struct Lisp_Char_Table *dp = window_display_table (w);
2535 Lisp_Object gc;
2537 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2538 if (dp
2539 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc))
2540 && GLYPH_CODE_CHAR_VALID_P (gc))
2542 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2543 spec_glyph_lookup_face (w, &right_border_glyph);
2546 if (GLYPH_FACE (right_border_glyph) <= 0)
2547 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2550 else
2551 window_matrix = w->current_matrix;
2553 /* For all rows in the window matrix and corresponding rows in the
2554 frame matrix. */
2555 window_y = 0;
2556 frame_y = window_matrix->matrix_y;
2557 while (window_y < window_matrix->nrows)
2559 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2560 struct glyph_row *window_row = window_matrix->rows + window_y;
2561 int current_row_p = window_matrix == w->current_matrix;
2563 /* Fill up the frame row with spaces up to the left margin of the
2564 window row. */
2565 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2567 /* Fill up areas in the window matrix row with spaces. */
2568 fill_up_glyph_row_with_spaces (window_row);
2570 /* If only part of W's desired matrix has been built, and
2571 window_row wasn't displayed, use the corresponding current
2572 row instead. */
2573 if (window_matrix == w->desired_matrix
2574 && !window_row->enabled_p)
2576 window_row = w->current_matrix->rows + window_y;
2577 current_row_p = 1;
2580 if (current_row_p)
2582 /* Copy window row to frame row. */
2583 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2584 window_row->glyphs[0],
2585 window_matrix->matrix_w * sizeof (struct glyph));
2587 else
2589 xassert (window_row->enabled_p);
2591 /* Only when a desired row has been displayed, we want
2592 the corresponding frame row to be updated. */
2593 frame_row->enabled_p = 1;
2595 /* Maybe insert a vertical border between horizontally adjacent
2596 windows. */
2597 if (GLYPH_CHAR (right_border_glyph) != 0)
2599 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2600 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2603 #if GLYPH_DEBUG
2604 /* Window row window_y must be a slice of frame row
2605 frame_y. */
2606 xassert (glyph_row_slice_p (window_row, frame_row));
2608 /* If rows are in sync, we don't have to copy glyphs because
2609 frame and window share glyphs. */
2611 strcpy (w->current_matrix->method, w->desired_matrix->method);
2612 add_window_display_history (w, w->current_matrix->method, 0);
2613 #endif
2616 /* Set number of used glyphs in the frame matrix. Since we fill
2617 up with spaces, and visit leaf windows from left to right it
2618 can be done simply. */
2619 frame_row->used[TEXT_AREA]
2620 = window_matrix->matrix_x + window_matrix->matrix_w;
2622 /* Next row. */
2623 ++window_y;
2624 ++frame_y;
2628 /* Given a user-specified glyph, possibly including a Lisp-level face
2629 ID, return a glyph that has a realized face ID.
2630 This is used for glyphs displayed specially and not part of the text;
2631 for instance, vertical separators, truncation markers, etc. */
2633 void
2634 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2636 int lface_id = GLYPH_FACE (*glyph);
2637 /* Convert the glyph's specified face to a realized (cache) face. */
2638 if (lface_id > 0)
2640 int face_id = merge_faces (XFRAME (w->frame),
2641 Qt, lface_id, DEFAULT_FACE_ID);
2642 SET_GLYPH_FACE (*glyph, face_id);
2646 /* Add spaces to a glyph row ROW in a window matrix.
2648 Each row has the form:
2650 +---------+-----------------------------+------------+
2651 | left | text | right |
2652 +---------+-----------------------------+------------+
2654 Left and right marginal areas are optional. This function adds
2655 spaces to areas so that there are no empty holes between areas.
2656 In other words: If the right area is not empty, the text area
2657 is filled up with spaces up to the right area. If the text area
2658 is not empty, the left area is filled up.
2660 To be called for frame-based redisplay, only. */
2662 static void
2663 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2665 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2666 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2667 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2671 /* Fill area AREA of glyph row ROW with spaces. To be called for
2672 frame-based redisplay only. */
2674 static void
2675 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2677 if (row->glyphs[area] < row->glyphs[area + 1])
2679 struct glyph *end = row->glyphs[area + 1];
2680 struct glyph *text = row->glyphs[area] + row->used[area];
2682 while (text < end)
2683 *text++ = space_glyph;
2684 row->used[area] = text - row->glyphs[area];
2689 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2690 reached. In frame matrices only one area, TEXT_AREA, is used. */
2692 static void
2693 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2695 int i = row->used[TEXT_AREA];
2696 struct glyph *glyph = row->glyphs[TEXT_AREA];
2698 while (i < upto)
2699 glyph[i++] = space_glyph;
2701 row->used[TEXT_AREA] = i;
2706 /**********************************************************************
2707 Mirroring operations on frame matrices in window matrices
2708 **********************************************************************/
2710 /* Set frame being updated via frame-based redisplay to F. This
2711 function must be called before updates to make explicit that we are
2712 working on frame matrices or not. */
2714 static inline void
2715 set_frame_matrix_frame (struct frame *f)
2717 frame_matrix_frame = f;
2721 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2722 DESIRED_MATRIX is the desired matrix corresponding to
2723 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2724 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2725 frame_matrix_frame is non-null, this indicates that the exchange is
2726 done in frame matrices, and that we have to perform analogous
2727 operations in window matrices of frame_matrix_frame. */
2729 static inline void
2730 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2732 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2733 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2734 int mouse_face_p = current_row->mouse_face_p;
2736 /* Do current_row = desired_row. This exchanges glyph pointers
2737 between both rows, and does a structure assignment otherwise. */
2738 assign_row (current_row, desired_row);
2740 /* Enable current_row to mark it as valid. */
2741 current_row->enabled_p = 1;
2742 current_row->mouse_face_p = mouse_face_p;
2744 /* If we are called on frame matrices, perform analogous operations
2745 for window matrices. */
2746 if (frame_matrix_frame)
2747 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2751 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2752 W's frame which has been made current (by swapping pointers between
2753 current and desired matrix). Perform analogous operations in the
2754 matrices of leaf windows in the window tree rooted at W. */
2756 static void
2757 mirror_make_current (struct window *w, int frame_row)
2759 while (w)
2761 if (!NILP (w->hchild))
2762 mirror_make_current (XWINDOW (w->hchild), frame_row);
2763 else if (!NILP (w->vchild))
2764 mirror_make_current (XWINDOW (w->vchild), frame_row);
2765 else
2767 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2768 here because the checks performed in debug mode there
2769 will not allow the conversion. */
2770 int row = frame_row - w->desired_matrix->matrix_y;
2772 /* If FRAME_ROW is within W, assign the desired row to the
2773 current row (exchanging glyph pointers). */
2774 if (row >= 0 && row < w->desired_matrix->matrix_h)
2776 struct glyph_row *current_row
2777 = MATRIX_ROW (w->current_matrix, row);
2778 struct glyph_row *desired_row
2779 = MATRIX_ROW (w->desired_matrix, row);
2781 if (desired_row->enabled_p)
2782 assign_row (current_row, desired_row);
2783 else
2784 swap_glyph_pointers (desired_row, current_row);
2785 current_row->enabled_p = 1;
2787 /* Set the Y coordinate of the mode/header line's row.
2788 It is needed in draw_row_with_mouse_face to find the
2789 screen coordinates. (Window-based redisplay sets
2790 this in update_window, but no one seems to do that
2791 for frame-based redisplay.) */
2792 if (current_row->mode_line_p)
2793 current_row->y = row;
2797 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2802 /* Perform row dance after scrolling. We are working on the range of
2803 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2804 including) in MATRIX. COPY_FROM is a vector containing, for each
2805 row I in the range 0 <= I < NLINES, the index of the original line
2806 to move to I. This index is relative to the row range, i.e. 0 <=
2807 index < NLINES. RETAINED_P is a vector containing zero for each
2808 row 0 <= I < NLINES which is empty.
2810 This function is called from do_scrolling and do_direct_scrolling. */
2812 void
2813 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2814 int *copy_from, char *retained_p)
2816 /* A copy of original rows. */
2817 struct glyph_row *old_rows;
2819 /* Rows to assign to. */
2820 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2822 int i;
2824 /* Make a copy of the original rows. */
2825 old_rows = (struct glyph_row *) alloca (nlines * sizeof *old_rows);
2826 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2828 /* Assign new rows, maybe clear lines. */
2829 for (i = 0; i < nlines; ++i)
2831 int enabled_before_p = new_rows[i].enabled_p;
2833 xassert (i + unchanged_at_top < matrix->nrows);
2834 xassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2835 new_rows[i] = old_rows[copy_from[i]];
2836 new_rows[i].enabled_p = enabled_before_p;
2838 /* RETAINED_P is zero for empty lines. */
2839 if (!retained_p[copy_from[i]])
2840 new_rows[i].enabled_p = 0;
2843 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2844 if (frame_matrix_frame)
2845 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2846 unchanged_at_top, nlines, copy_from, retained_p);
2850 /* Synchronize glyph pointers in the current matrix of window W with
2851 the current frame matrix. */
2853 static void
2854 sync_window_with_frame_matrix_rows (struct window *w)
2856 struct frame *f = XFRAME (w->frame);
2857 struct glyph_row *window_row, *window_row_end, *frame_row;
2858 int left, right, x, width;
2860 /* Preconditions: W must be a leaf window on a tty frame. */
2861 xassert (NILP (w->hchild) && NILP (w->vchild));
2862 xassert (!FRAME_WINDOW_P (f));
2864 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2865 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2866 x = w->current_matrix->matrix_x;
2867 width = w->current_matrix->matrix_w;
2869 window_row = w->current_matrix->rows;
2870 window_row_end = window_row + w->current_matrix->nrows;
2871 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2873 for (; window_row < window_row_end; ++window_row, ++frame_row)
2875 window_row->glyphs[LEFT_MARGIN_AREA]
2876 = frame_row->glyphs[0] + x;
2877 window_row->glyphs[TEXT_AREA]
2878 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2879 window_row->glyphs[LAST_AREA]
2880 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2881 window_row->glyphs[RIGHT_MARGIN_AREA]
2882 = window_row->glyphs[LAST_AREA] - right;
2887 /* Return the window in the window tree rooted in W containing frame
2888 row ROW. Value is null if none is found. */
2890 static struct window *
2891 frame_row_to_window (struct window *w, int row)
2893 struct window *found = NULL;
2895 while (w && !found)
2897 if (!NILP (w->hchild))
2898 found = frame_row_to_window (XWINDOW (w->hchild), row);
2899 else if (!NILP (w->vchild))
2900 found = frame_row_to_window (XWINDOW (w->vchild), row);
2901 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2902 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2903 found = w;
2905 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2908 return found;
2912 /* Perform a line dance in the window tree rooted at W, after
2913 scrolling a frame matrix in mirrored_line_dance.
2915 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2916 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2917 COPY_FROM is a vector containing, for each row I in the range 0 <=
2918 I < NLINES, the index of the original line to move to I. This
2919 index is relative to the row range, i.e. 0 <= index < NLINES.
2920 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2921 which is empty. */
2923 static void
2924 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2926 while (w)
2928 if (!NILP (w->hchild))
2929 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
2930 nlines, copy_from, retained_p);
2931 else if (!NILP (w->vchild))
2932 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
2933 nlines, copy_from, retained_p);
2934 else
2936 /* W is a leaf window, and we are working on its current
2937 matrix m. */
2938 struct glyph_matrix *m = w->current_matrix;
2939 int i, sync_p = 0;
2940 struct glyph_row *old_rows;
2942 /* Make a copy of the original rows of matrix m. */
2943 old_rows = (struct glyph_row *) alloca (m->nrows * sizeof *old_rows);
2944 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2946 for (i = 0; i < nlines; ++i)
2948 /* Frame relative line assigned to. */
2949 int frame_to = i + unchanged_at_top;
2951 /* Frame relative line assigned. */
2952 int frame_from = copy_from[i] + unchanged_at_top;
2954 /* Window relative line assigned to. */
2955 int window_to = frame_to - m->matrix_y;
2957 /* Window relative line assigned. */
2958 int window_from = frame_from - m->matrix_y;
2960 /* Is assigned line inside window? */
2961 int from_inside_window_p
2962 = window_from >= 0 && window_from < m->matrix_h;
2964 /* Is assigned to line inside window? */
2965 int to_inside_window_p
2966 = window_to >= 0 && window_to < m->matrix_h;
2968 if (from_inside_window_p && to_inside_window_p)
2970 /* Enabled setting before assignment. */
2971 int enabled_before_p;
2973 /* Do the assignment. The enabled_p flag is saved
2974 over the assignment because the old redisplay did
2975 that. */
2976 enabled_before_p = m->rows[window_to].enabled_p;
2977 m->rows[window_to] = old_rows[window_from];
2978 m->rows[window_to].enabled_p = enabled_before_p;
2980 /* If frame line is empty, window line is empty, too. */
2981 if (!retained_p[copy_from[i]])
2982 m->rows[window_to].enabled_p = 0;
2984 else if (to_inside_window_p)
2986 /* A copy between windows. This is an infrequent
2987 case not worth optimizing. */
2988 struct frame *f = XFRAME (w->frame);
2989 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2990 struct window *w2;
2991 struct glyph_matrix *m2;
2992 int m2_from;
2994 w2 = frame_row_to_window (root, frame_from);
2995 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2996 -nw', FROM_FRAME sometimes has no associated window.
2997 This check avoids a segfault if W2 is null. */
2998 if (w2)
3000 m2 = w2->current_matrix;
3001 m2_from = frame_from - m2->matrix_y;
3002 copy_row_except_pointers (m->rows + window_to,
3003 m2->rows + m2_from);
3005 /* If frame line is empty, window line is empty, too. */
3006 if (!retained_p[copy_from[i]])
3007 m->rows[window_to].enabled_p = 0;
3009 sync_p = 1;
3011 else if (from_inside_window_p)
3012 sync_p = 1;
3015 /* If there was a copy between windows, make sure glyph
3016 pointers are in sync with the frame matrix. */
3017 if (sync_p)
3018 sync_window_with_frame_matrix_rows (w);
3020 /* Check that no pointers are lost. */
3021 CHECK_MATRIX (m);
3024 /* Next window on same level. */
3025 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3030 #if GLYPH_DEBUG
3032 /* Check that window and frame matrices agree about their
3033 understanding where glyphs of the rows are to find. For each
3034 window in the window tree rooted at W, check that rows in the
3035 matrices of leaf window agree with their frame matrices about
3036 glyph pointers. */
3038 static void
3039 check_window_matrix_pointers (struct window *w)
3041 while (w)
3043 if (!NILP (w->hchild))
3044 check_window_matrix_pointers (XWINDOW (w->hchild));
3045 else if (!NILP (w->vchild))
3046 check_window_matrix_pointers (XWINDOW (w->vchild));
3047 else
3049 struct frame *f = XFRAME (w->frame);
3050 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
3051 check_matrix_pointers (w->current_matrix, f->current_matrix);
3054 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3059 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
3060 a window and FRAME_MATRIX is the corresponding frame matrix. For
3061 each row in WINDOW_MATRIX check that it's a slice of the
3062 corresponding frame row. If it isn't, abort. */
3064 static void
3065 check_matrix_pointers (struct glyph_matrix *window_matrix,
3066 struct glyph_matrix *frame_matrix)
3068 /* Row number in WINDOW_MATRIX. */
3069 int i = 0;
3071 /* Row number corresponding to I in FRAME_MATRIX. */
3072 int j = window_matrix->matrix_y;
3074 /* For all rows check that the row in the window matrix is a
3075 slice of the row in the frame matrix. If it isn't we didn't
3076 mirror an operation on the frame matrix correctly. */
3077 while (i < window_matrix->nrows)
3079 if (!glyph_row_slice_p (window_matrix->rows + i,
3080 frame_matrix->rows + j))
3081 abort ();
3082 ++i, ++j;
3086 #endif /* GLYPH_DEBUG != 0 */
3090 /**********************************************************************
3091 VPOS and HPOS translations
3092 **********************************************************************/
3094 #if GLYPH_DEBUG
3096 /* Translate vertical position VPOS which is relative to window W to a
3097 vertical position relative to W's frame. */
3099 static int
3100 window_to_frame_vpos (struct window *w, int vpos)
3102 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3103 xassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
3104 vpos += WINDOW_TOP_EDGE_LINE (w);
3105 xassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
3106 return vpos;
3110 /* Translate horizontal position HPOS which is relative to window W to
3111 a horizontal position relative to W's frame. */
3113 static int
3114 window_to_frame_hpos (struct window *w, int hpos)
3116 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3117 hpos += WINDOW_LEFT_EDGE_COL (w);
3118 return hpos;
3121 #endif /* GLYPH_DEBUG */
3125 /**********************************************************************
3126 Redrawing Frames
3127 **********************************************************************/
3129 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
3130 doc: /* Clear frame FRAME and output again what is supposed to appear on it. */)
3131 (Lisp_Object frame)
3133 struct frame *f;
3135 CHECK_LIVE_FRAME (frame);
3136 f = XFRAME (frame);
3138 /* Ignore redraw requests, if frame has no glyphs yet.
3139 (Implementation note: It still has to be checked why we are
3140 called so early here). */
3141 if (!glyphs_initialized_initially_p)
3142 return Qnil;
3144 update_begin (f);
3145 #ifdef MSDOS
3146 if (FRAME_MSDOS_P (f))
3147 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3148 #endif
3149 clear_frame (f);
3150 clear_current_matrices (f);
3151 update_end (f);
3152 if (FRAME_TERMCAP_P (f))
3153 fflush (FRAME_TTY (f)->output);
3154 windows_or_buffers_changed++;
3155 /* Mark all windows as inaccurate, so that every window will have
3156 its redisplay done. */
3157 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3158 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3159 f->garbaged = 0;
3160 return Qnil;
3164 /* Redraw frame F. This is nothing more than a call to the Lisp
3165 function redraw-frame. */
3167 void
3168 redraw_frame (struct frame *f)
3170 Lisp_Object frame;
3171 XSETFRAME (frame, f);
3172 Fredraw_frame (frame);
3176 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3177 doc: /* Clear and redisplay all visible frames. */)
3178 (void)
3180 Lisp_Object tail, frame;
3182 FOR_EACH_FRAME (tail, frame)
3183 if (FRAME_VISIBLE_P (XFRAME (frame)))
3184 Fredraw_frame (frame);
3186 return Qnil;
3191 /***********************************************************************
3192 Frame Update
3193 ***********************************************************************/
3195 /* Update frame F based on the data in desired matrices.
3197 If FORCE_P is non-zero, don't let redisplay be stopped by detecting
3198 pending input. If INHIBIT_HAIRY_ID_P is non-zero, don't try
3199 scrolling.
3201 Value is non-zero if redisplay was stopped due to pending input. */
3204 update_frame (struct frame *f, int force_p, int inhibit_hairy_id_p)
3206 /* 1 means display has been paused because of pending input. */
3207 int paused_p;
3208 struct window *root_window = XWINDOW (f->root_window);
3210 if (redisplay_dont_pause)
3211 force_p = 1;
3212 #if PERIODIC_PREEMPTION_CHECKING
3213 else if (NILP (Vredisplay_preemption_period))
3214 force_p = 1;
3215 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3217 EMACS_TIME tm;
3218 double p = XFLOATINT (Vredisplay_preemption_period);
3219 int sec, usec;
3221 if (detect_input_pending_ignore_squeezables ())
3223 paused_p = 1;
3224 goto do_pause;
3227 sec = (int) p;
3228 usec = (p - sec) * 1000000;
3230 EMACS_GET_TIME (tm);
3231 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3232 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3234 #endif
3236 if (FRAME_WINDOW_P (f))
3238 /* We are working on window matrix basis. All windows whose
3239 flag must_be_updated_p is set have to be updated. */
3241 /* Record that we are not working on frame matrices. */
3242 set_frame_matrix_frame (NULL);
3244 /* Update all windows in the window tree of F, maybe stopping
3245 when pending input is detected. */
3246 update_begin (f);
3248 /* Update the menu bar on X frames that don't have toolkit
3249 support. */
3250 if (WINDOWP (f->menu_bar_window))
3251 update_window (XWINDOW (f->menu_bar_window), 1);
3253 /* Update the tool-bar window, if present. */
3254 if (WINDOWP (f->tool_bar_window))
3256 struct window *w = XWINDOW (f->tool_bar_window);
3258 /* Update tool-bar window. */
3259 if (w->must_be_updated_p)
3261 Lisp_Object tem;
3263 update_window (w, 1);
3264 w->must_be_updated_p = 0;
3266 /* Swap tool-bar strings. We swap because we want to
3267 reuse strings. */
3268 tem = f->current_tool_bar_string;
3269 f->current_tool_bar_string = f->desired_tool_bar_string;
3270 f->desired_tool_bar_string = tem;
3275 /* Update windows. */
3276 paused_p = update_window_tree (root_window, force_p);
3277 update_end (f);
3279 /* This flush is a performance bottleneck under X,
3280 and it doesn't seem to be necessary anyway (in general).
3281 It is necessary when resizing the window with the mouse, or
3282 at least the fringes are not redrawn in a timely manner. ++kfs */
3283 if (f->force_flush_display_p)
3285 FRAME_RIF (f)->flush_display (f);
3286 f->force_flush_display_p = 0;
3289 else
3291 /* We are working on frame matrix basis. Set the frame on whose
3292 frame matrix we operate. */
3293 set_frame_matrix_frame (f);
3295 /* Build F's desired matrix from window matrices. */
3296 build_frame_matrix (f);
3298 /* Update the display */
3299 update_begin (f);
3300 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3301 update_end (f);
3303 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3305 if (FRAME_TTY (f)->termscript)
3306 fflush (FRAME_TTY (f)->termscript);
3307 if (FRAME_TERMCAP_P (f))
3308 fflush (FRAME_TTY (f)->output);
3311 /* Check window matrices for lost pointers. */
3312 #if GLYPH_DEBUG
3313 check_window_matrix_pointers (root_window);
3314 add_frame_display_history (f, paused_p);
3315 #endif
3318 #if PERIODIC_PREEMPTION_CHECKING
3319 do_pause:
3320 #endif
3321 /* Reset flags indicating that a window should be updated. */
3322 set_window_update_flags (root_window, 0);
3324 display_completed = !paused_p;
3325 return paused_p;
3330 /************************************************************************
3331 Window-based updates
3332 ************************************************************************/
3334 /* Perform updates in window tree rooted at W. FORCE_P non-zero means
3335 don't stop updating when input is pending. */
3337 static int
3338 update_window_tree (struct window *w, int force_p)
3340 int paused_p = 0;
3342 while (w && !paused_p)
3344 if (!NILP (w->hchild))
3345 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3346 else if (!NILP (w->vchild))
3347 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3348 else if (w->must_be_updated_p)
3349 paused_p |= update_window (w, force_p);
3351 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3354 return paused_p;
3358 /* Update window W if its flag must_be_updated_p is non-zero. If
3359 FORCE_P is non-zero, don't stop updating if input is pending. */
3361 void
3362 update_single_window (struct window *w, int force_p)
3364 if (w->must_be_updated_p)
3366 struct frame *f = XFRAME (WINDOW_FRAME (w));
3368 /* Record that this is not a frame-based redisplay. */
3369 set_frame_matrix_frame (NULL);
3371 if (redisplay_dont_pause)
3372 force_p = 1;
3373 #if PERIODIC_PREEMPTION_CHECKING
3374 else if (NILP (Vredisplay_preemption_period))
3375 force_p = 1;
3376 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3378 EMACS_TIME tm;
3379 double p = XFLOATINT (Vredisplay_preemption_period);
3380 int sec, usec;
3382 sec = (int) p;
3383 usec = (p - sec) * 1000000;
3385 EMACS_GET_TIME (tm);
3386 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3387 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3389 #endif
3391 /* Update W. */
3392 update_begin (f);
3393 update_window (w, force_p);
3394 update_end (f);
3396 /* Reset flag in W. */
3397 w->must_be_updated_p = 0;
3401 #ifdef HAVE_WINDOW_SYSTEM
3403 /* Redraw lines from the current matrix of window W that are
3404 overlapped by other rows. YB is bottom-most y-position in W. */
3406 static void
3407 redraw_overlapped_rows (struct window *w, int yb)
3409 int i;
3410 struct frame *f = XFRAME (WINDOW_FRAME (w));
3412 /* If rows overlapping others have been changed, the rows being
3413 overlapped have to be redrawn. This won't draw lines that have
3414 already been drawn in update_window_line because overlapped_p in
3415 desired rows is 0, so after row assignment overlapped_p in
3416 current rows is 0. */
3417 for (i = 0; i < w->current_matrix->nrows; ++i)
3419 struct glyph_row *row = w->current_matrix->rows + i;
3421 if (!row->enabled_p)
3422 break;
3423 else if (row->mode_line_p)
3424 continue;
3426 if (row->overlapped_p)
3428 enum glyph_row_area area;
3430 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3432 updated_row = row;
3433 updated_area = area;
3434 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3435 area == TEXT_AREA ? row->x : 0);
3436 if (row->used[area])
3437 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3438 row->used[area]);
3439 FRAME_RIF (f)->clear_end_of_line (-1);
3442 row->overlapped_p = 0;
3445 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3446 break;
3451 /* Redraw lines from the current matrix of window W that overlap
3452 others. YB is bottom-most y-position in W. */
3454 static void
3455 redraw_overlapping_rows (struct window *w, int yb)
3457 int i, bottom_y;
3458 struct glyph_row *row;
3459 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3461 for (i = 0; i < w->current_matrix->nrows; ++i)
3463 row = w->current_matrix->rows + i;
3465 if (!row->enabled_p)
3466 break;
3467 else if (row->mode_line_p)
3468 continue;
3470 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3472 if (row->overlapping_p)
3474 int overlaps = 0;
3476 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3477 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3478 overlaps |= OVERLAPS_PRED;
3479 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3480 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3481 overlaps |= OVERLAPS_SUCC;
3483 if (overlaps)
3485 if (row->used[LEFT_MARGIN_AREA])
3486 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3488 if (row->used[TEXT_AREA])
3489 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3491 if (row->used[RIGHT_MARGIN_AREA])
3492 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3494 /* Record in neighbour rows that ROW overwrites part of
3495 their display. */
3496 if (overlaps & OVERLAPS_PRED)
3497 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3498 if (overlaps & OVERLAPS_SUCC)
3499 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3503 if (bottom_y >= yb)
3504 break;
3508 #endif /* HAVE_WINDOW_SYSTEM */
3511 #if defined GLYPH_DEBUG && 0
3513 /* Check that no row in the current matrix of window W is enabled
3514 which is below what's displayed in the window. */
3516 static void
3517 check_current_matrix_flags (struct window *w)
3519 int last_seen_p = 0;
3520 int i, yb = window_text_bottom_y (w);
3522 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3524 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3525 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3526 last_seen_p = 1;
3527 else if (last_seen_p && row->enabled_p)
3528 abort ();
3532 #endif /* GLYPH_DEBUG */
3535 /* Update display of window W. FORCE_P non-zero means that we should
3536 not stop when detecting pending input. */
3538 static int
3539 update_window (struct window *w, int force_p)
3541 struct glyph_matrix *desired_matrix = w->desired_matrix;
3542 int paused_p;
3543 #if !PERIODIC_PREEMPTION_CHECKING
3544 int preempt_count = baud_rate / 2400 + 1;
3545 #endif
3546 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3547 #if GLYPH_DEBUG
3548 /* Check that W's frame doesn't have glyph matrices. */
3549 xassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3550 #endif
3552 /* Check pending input the first time so that we can quickly return. */
3553 #if !PERIODIC_PREEMPTION_CHECKING
3554 if (!force_p)
3555 detect_input_pending_ignore_squeezables ();
3556 #endif
3558 /* If forced to complete the update, or if no input is pending, do
3559 the update. */
3560 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3562 struct glyph_row *row, *end;
3563 struct glyph_row *mode_line_row;
3564 struct glyph_row *header_line_row;
3565 int yb, changed_p = 0, mouse_face_overwritten_p = 0;
3566 #if ! PERIODIC_PREEMPTION_CHECKING
3567 int n_updated = 0;
3568 #endif
3570 rif->update_window_begin_hook (w);
3571 yb = window_text_bottom_y (w);
3573 /* If window has a header line, update it before everything else.
3574 Adjust y-positions of other rows by the header line height. */
3575 row = desired_matrix->rows;
3576 end = row + desired_matrix->nrows - 1;
3578 if (row->mode_line_p)
3580 header_line_row = row;
3581 ++row;
3583 else
3584 header_line_row = NULL;
3586 /* Update the mode line, if necessary. */
3587 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3588 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3590 mode_line_row->y = yb;
3591 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3592 desired_matrix),
3593 &mouse_face_overwritten_p);
3596 /* Find first enabled row. Optimizations in redisplay_internal
3597 may lead to an update with only one row enabled. There may
3598 be also completely empty matrices. */
3599 while (row < end && !row->enabled_p)
3600 ++row;
3602 /* Try reusing part of the display by copying. */
3603 if (row < end && !desired_matrix->no_scrolling_p)
3605 int rc = scrolling_window (w, header_line_row != NULL);
3606 if (rc < 0)
3608 /* All rows were found to be equal. */
3609 paused_p = 0;
3610 goto set_cursor;
3612 else if (rc > 0)
3614 /* We've scrolled the display. */
3615 force_p = 1;
3616 changed_p = 1;
3620 /* Update the rest of the lines. */
3621 for (; row < end && (force_p || !input_pending); ++row)
3622 if (row->enabled_p)
3624 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3625 int i;
3627 /* We'll have to play a little bit with when to
3628 detect_input_pending. If it's done too often,
3629 scrolling large windows with repeated scroll-up
3630 commands will too quickly pause redisplay. */
3631 #if PERIODIC_PREEMPTION_CHECKING
3632 if (!force_p)
3634 EMACS_TIME tm, dif;
3635 EMACS_GET_TIME (tm);
3636 EMACS_SUB_TIME (dif, preemption_next_check, tm);
3637 if (EMACS_TIME_NEG_P (dif))
3639 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3640 if (detect_input_pending_ignore_squeezables ())
3641 break;
3644 #else
3645 if (!force_p && ++n_updated % preempt_count == 0)
3646 detect_input_pending_ignore_squeezables ();
3647 #endif
3648 changed_p |= update_window_line (w, vpos,
3649 &mouse_face_overwritten_p);
3651 /* Mark all rows below the last visible one in the current
3652 matrix as invalid. This is necessary because of
3653 variable line heights. Consider the case of three
3654 successive redisplays, where the first displays 5
3655 lines, the second 3 lines, and the third 5 lines again.
3656 If the second redisplay wouldn't mark rows in the
3657 current matrix invalid, the third redisplay might be
3658 tempted to optimize redisplay based on lines displayed
3659 in the first redisplay. */
3660 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3661 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3662 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3665 /* Was display preempted? */
3666 paused_p = row < end;
3668 set_cursor:
3670 /* Update the header line after scrolling because a new header
3671 line would otherwise overwrite lines at the top of the window
3672 that can be scrolled. */
3673 if (header_line_row && header_line_row->enabled_p)
3675 header_line_row->y = 0;
3676 update_window_line (w, 0, &mouse_face_overwritten_p);
3679 /* Fix the appearance of overlapping/overlapped rows. */
3680 if (!paused_p && !w->pseudo_window_p)
3682 #ifdef HAVE_WINDOW_SYSTEM
3683 if (changed_p && rif->fix_overlapping_area)
3685 redraw_overlapped_rows (w, yb);
3686 redraw_overlapping_rows (w, yb);
3688 #endif
3690 /* Make cursor visible at cursor position of W. */
3691 set_window_cursor_after_update (w);
3693 #if 0 /* Check that current matrix invariants are satisfied. This is
3694 for debugging only. See the comment of check_matrix_invariants. */
3695 IF_DEBUG (check_matrix_invariants (w));
3696 #endif
3699 #if GLYPH_DEBUG
3700 /* Remember the redisplay method used to display the matrix. */
3701 strcpy (w->current_matrix->method, w->desired_matrix->method);
3702 #endif
3704 #ifdef HAVE_WINDOW_SYSTEM
3705 update_window_fringes (w, 0);
3706 #endif
3708 /* End the update of window W. Don't set the cursor if we
3709 paused updating the display because in this case,
3710 set_window_cursor_after_update hasn't been called, and
3711 output_cursor doesn't contain the cursor location. */
3712 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3714 else
3715 paused_p = 1;
3717 #if GLYPH_DEBUG
3718 /* check_current_matrix_flags (w); */
3719 add_window_display_history (w, w->current_matrix->method, paused_p);
3720 #endif
3722 clear_glyph_matrix (desired_matrix);
3724 return paused_p;
3728 /* Update the display of area AREA in window W, row number VPOS.
3729 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3731 static void
3732 update_marginal_area (struct window *w, int area, int vpos)
3734 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3735 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3737 /* Let functions in xterm.c know what area subsequent X positions
3738 will be relative to. */
3739 updated_area = area;
3741 /* Set cursor to start of glyphs, write them, and clear to the end
3742 of the area. I don't think that something more sophisticated is
3743 necessary here, since marginal areas will not be the default. */
3744 rif->cursor_to (vpos, 0, desired_row->y, 0);
3745 if (desired_row->used[area])
3746 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3747 rif->clear_end_of_line (-1);
3751 /* Update the display of the text area of row VPOS in window W.
3752 Value is non-zero if display has changed. */
3754 static int
3755 update_text_area (struct window *w, int vpos)
3757 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3758 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3759 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3760 int changed_p = 0;
3762 /* Let functions in xterm.c know what area subsequent X positions
3763 will be relative to. */
3764 updated_area = TEXT_AREA;
3766 /* If rows are at different X or Y, or rows have different height,
3767 or the current row is marked invalid, write the entire line. */
3768 if (!current_row->enabled_p
3769 || desired_row->y != current_row->y
3770 || desired_row->ascent != current_row->ascent
3771 || desired_row->phys_ascent != current_row->phys_ascent
3772 || desired_row->phys_height != current_row->phys_height
3773 || desired_row->visible_height != current_row->visible_height
3774 || current_row->overlapped_p
3775 /* This next line is necessary for correctly redrawing
3776 mouse-face areas after scrolling and other operations.
3777 However, it causes excessive flickering when mouse is moved
3778 across the mode line. Luckily, turning it off for the mode
3779 line doesn't seem to hurt anything. -- cyd.
3780 But it is still needed for the header line. -- kfs. */
3781 || (current_row->mouse_face_p
3782 && !(current_row->mode_line_p && vpos > 0))
3783 || current_row->x != desired_row->x)
3785 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
3787 if (desired_row->used[TEXT_AREA])
3788 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3789 desired_row->used[TEXT_AREA]);
3791 /* Clear to end of window. */
3792 rif->clear_end_of_line (-1);
3793 changed_p = 1;
3795 /* This erases the cursor. We do this here because
3796 notice_overwritten_cursor cannot easily check this, which
3797 might indicate that the whole functionality of
3798 notice_overwritten_cursor would better be implemented here.
3799 On the other hand, we need notice_overwritten_cursor as long
3800 as mouse highlighting is done asynchronously outside of
3801 redisplay. */
3802 if (vpos == w->phys_cursor.vpos)
3803 w->phys_cursor_on_p = 0;
3805 else
3807 int stop, i, x;
3808 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3809 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3810 int overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3811 int desired_stop_pos = desired_row->used[TEXT_AREA];
3812 int abort_skipping = 0;
3814 /* If the desired row extends its face to the text area end, and
3815 unless the current row also does so at the same position,
3816 make sure we write at least one glyph, so that the face
3817 extension actually takes place. */
3818 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3819 && (desired_stop_pos < current_row->used[TEXT_AREA]
3820 || (desired_stop_pos == current_row->used[TEXT_AREA]
3821 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3822 --desired_stop_pos;
3824 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3825 i = 0;
3826 x = desired_row->x;
3828 /* Loop over glyphs that current and desired row may have
3829 in common. */
3830 while (i < stop)
3832 int can_skip_p = !abort_skipping;
3834 /* Skip over glyphs that both rows have in common. These
3835 don't have to be written. We can't skip if the last
3836 current glyph overlaps the glyph to its right. For
3837 example, consider a current row of `if ' with the `f' in
3838 Courier bold so that it overlaps the ` ' to its right.
3839 If the desired row is ` ', we would skip over the space
3840 after the `if' and there would remain a pixel from the
3841 `f' on the screen. */
3842 if (overlapping_glyphs_p && i > 0)
3844 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3845 int left, right;
3847 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3848 &left, &right);
3849 can_skip_p = (right == 0 && !abort_skipping);
3852 if (can_skip_p)
3854 int start_hpos = i;
3856 while (i < stop
3857 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3859 x += desired_glyph->pixel_width;
3860 ++desired_glyph, ++current_glyph, ++i;
3863 /* Consider the case that the current row contains "xxx
3864 ppp ggg" in italic Courier font, and the desired row
3865 is "xxx ggg". The character `p' has lbearing, `g'
3866 has not. The loop above will stop in front of the
3867 first `p' in the current row. If we would start
3868 writing glyphs there, we wouldn't erase the lbearing
3869 of the `p'. The rest of the lbearing problem is then
3870 taken care of by draw_glyphs. */
3871 if (overlapping_glyphs_p
3872 && i > 0
3873 && i < current_row->used[TEXT_AREA]
3874 && (current_row->used[TEXT_AREA]
3875 != desired_row->used[TEXT_AREA]))
3877 int left, right;
3879 rif->get_glyph_overhangs (current_glyph, XFRAME (w->frame),
3880 &left, &right);
3881 while (left > 0 && i > 0)
3883 --i, --desired_glyph, --current_glyph;
3884 x -= desired_glyph->pixel_width;
3885 left -= desired_glyph->pixel_width;
3888 /* Abort the skipping algorithm if we end up before
3889 our starting point, to avoid looping (bug#1070).
3890 This can happen when the lbearing is larger than
3891 the pixel width. */
3892 abort_skipping = (i < start_hpos);
3896 /* Try to avoid writing the entire rest of the desired row
3897 by looking for a resync point. This mainly prevents
3898 mode line flickering in the case the mode line is in
3899 fixed-pitch font, which it usually will be. */
3900 if (i < desired_row->used[TEXT_AREA])
3902 int start_x = x, start_hpos = i;
3903 struct glyph *start = desired_glyph;
3904 int current_x = x;
3905 int skip_first_p = !can_skip_p;
3907 /* Find the next glyph that's equal again. */
3908 while (i < stop
3909 && (skip_first_p
3910 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3911 && x == current_x)
3913 x += desired_glyph->pixel_width;
3914 current_x += current_glyph->pixel_width;
3915 ++desired_glyph, ++current_glyph, ++i;
3916 skip_first_p = 0;
3919 if (i == start_hpos || x != current_x)
3921 i = start_hpos;
3922 x = start_x;
3923 desired_glyph = start;
3924 break;
3927 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
3928 rif->write_glyphs (start, i - start_hpos);
3929 changed_p = 1;
3933 /* Write the rest. */
3934 if (i < desired_row->used[TEXT_AREA])
3936 rif->cursor_to (vpos, i, desired_row->y, x);
3937 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
3938 changed_p = 1;
3941 /* Maybe clear to end of line. */
3942 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3944 /* If new row extends to the end of the text area, nothing
3945 has to be cleared, if and only if we did a write_glyphs
3946 above. This is made sure by setting desired_stop_pos
3947 appropriately above. */
3948 xassert (i < desired_row->used[TEXT_AREA]
3949 || ((desired_row->used[TEXT_AREA]
3950 == current_row->used[TEXT_AREA])
3951 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3953 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3955 /* If old row extends to the end of the text area, clear. */
3956 if (i >= desired_row->used[TEXT_AREA])
3957 rif->cursor_to (vpos, i, desired_row->y,
3958 desired_row->pixel_width);
3959 rif->clear_end_of_line (-1);
3960 changed_p = 1;
3962 else if (desired_row->pixel_width < current_row->pixel_width)
3964 /* Otherwise clear to the end of the old row. Everything
3965 after that position should be clear already. */
3966 int xlim;
3968 if (i >= desired_row->used[TEXT_AREA])
3969 rif->cursor_to (vpos, i, desired_row->y,
3970 desired_row->pixel_width);
3972 /* If cursor is displayed at the end of the line, make sure
3973 it's cleared. Nowadays we don't have a phys_cursor_glyph
3974 with which to erase the cursor (because this method
3975 doesn't work with lbearing/rbearing), so we must do it
3976 this way. */
3977 if (vpos == w->phys_cursor.vpos
3978 && (desired_row->reversed_p
3979 ? (w->phys_cursor.hpos < 0)
3980 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3982 w->phys_cursor_on_p = 0;
3983 xlim = -1;
3985 else
3986 xlim = current_row->pixel_width;
3987 rif->clear_end_of_line (xlim);
3988 changed_p = 1;
3992 return changed_p;
3996 /* Update row VPOS in window W. Value is non-zero if display has been
3997 changed. */
3999 static int
4000 update_window_line (struct window *w, int vpos, int *mouse_face_overwritten_p)
4002 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
4003 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
4004 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4005 int changed_p = 0;
4007 /* Set the row being updated. This is important to let xterm.c
4008 know what line height values are in effect. */
4009 updated_row = desired_row;
4011 /* A row can be completely invisible in case a desired matrix was
4012 built with a vscroll and then make_cursor_line_fully_visible shifts
4013 the matrix. Make sure to make such rows current anyway, since
4014 we need the correct y-position, for example, in the current matrix. */
4015 if (desired_row->mode_line_p
4016 || desired_row->visible_height > 0)
4018 xassert (desired_row->enabled_p);
4020 /* Update display of the left margin area, if there is one. */
4021 if (!desired_row->full_width_p
4022 && !NILP (w->left_margin_cols))
4024 changed_p = 1;
4025 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
4028 /* Update the display of the text area. */
4029 if (update_text_area (w, vpos))
4031 changed_p = 1;
4032 if (current_row->mouse_face_p)
4033 *mouse_face_overwritten_p = 1;
4036 /* Update display of the right margin area, if there is one. */
4037 if (!desired_row->full_width_p
4038 && !NILP (w->right_margin_cols))
4040 changed_p = 1;
4041 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
4044 /* Draw truncation marks etc. */
4045 if (!current_row->enabled_p
4046 || desired_row->y != current_row->y
4047 || desired_row->visible_height != current_row->visible_height
4048 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
4049 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
4050 || current_row->redraw_fringe_bitmaps_p
4051 || desired_row->mode_line_p != current_row->mode_line_p
4052 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
4053 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
4054 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
4055 rif->after_update_window_line_hook (desired_row);
4058 /* Update current_row from desired_row. */
4059 make_current (w->desired_matrix, w->current_matrix, vpos);
4060 updated_row = NULL;
4061 return changed_p;
4065 /* Set the cursor after an update of window W. This function may only
4066 be called from update_window. */
4068 static void
4069 set_window_cursor_after_update (struct window *w)
4071 struct frame *f = XFRAME (w->frame);
4072 struct redisplay_interface *rif = FRAME_RIF (f);
4073 int cx, cy, vpos, hpos;
4075 /* Not intended for frame matrix updates. */
4076 xassert (FRAME_WINDOW_P (f));
4078 if (cursor_in_echo_area
4079 && !NILP (echo_area_buffer[0])
4080 /* If we are showing a message instead of the mini-buffer,
4081 show the cursor for the message instead. */
4082 && XWINDOW (minibuf_window) == w
4083 && EQ (minibuf_window, echo_area_window)
4084 /* These cases apply only to the frame that contains
4085 the active mini-buffer window. */
4086 && FRAME_HAS_MINIBUF_P (f)
4087 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4089 cx = cy = vpos = hpos = 0;
4091 if (cursor_in_echo_area >= 0)
4093 /* If the mini-buffer is several lines high, find the last
4094 line that has any text on it. Note: either all lines
4095 are enabled or none. Otherwise we wouldn't be able to
4096 determine Y. */
4097 struct glyph_row *row, *last_row;
4098 struct glyph *glyph;
4099 int yb = window_text_bottom_y (w);
4101 last_row = NULL;
4102 row = w->current_matrix->rows;
4103 while (row->enabled_p
4104 && (last_row == NULL
4105 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
4107 if (row->used[TEXT_AREA]
4108 && row->glyphs[TEXT_AREA][0].charpos >= 0)
4109 last_row = row;
4110 ++row;
4113 if (last_row)
4115 struct glyph *start = last_row->glyphs[TEXT_AREA];
4116 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
4118 while (last > start && last->charpos < 0)
4119 --last;
4121 for (glyph = start; glyph < last; ++glyph)
4123 cx += glyph->pixel_width;
4124 ++hpos;
4127 cy = last_row->y;
4128 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
4132 else
4134 cx = w->cursor.x;
4135 cy = w->cursor.y;
4136 hpos = w->cursor.hpos;
4137 vpos = w->cursor.vpos;
4140 /* Window cursor can be out of sync for horizontally split windows. */
4141 hpos = max (-1, hpos); /* -1 is for when cursor is on the left fringe */
4142 hpos = min (w->current_matrix->matrix_w - 1, hpos);
4143 vpos = max (0, vpos);
4144 vpos = min (w->current_matrix->nrows - 1, vpos);
4145 rif->cursor_to (vpos, hpos, cy, cx);
4149 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4150 tree rooted at W. */
4152 void
4153 set_window_update_flags (struct window *w, int on_p)
4155 while (w)
4157 if (!NILP (w->hchild))
4158 set_window_update_flags (XWINDOW (w->hchild), on_p);
4159 else if (!NILP (w->vchild))
4160 set_window_update_flags (XWINDOW (w->vchild), on_p);
4161 else
4162 w->must_be_updated_p = on_p;
4164 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4170 /***********************************************************************
4171 Window-Based Scrolling
4172 ***********************************************************************/
4174 /* Structure describing rows in scrolling_window. */
4176 struct row_entry
4178 /* Number of occurrences of this row in desired and current matrix. */
4179 int old_uses, new_uses;
4181 /* Vpos of row in new matrix. */
4182 int new_line_number;
4184 /* Bucket index of this row_entry in the hash table row_table. */
4185 ptrdiff_t bucket;
4187 /* The row described by this entry. */
4188 struct glyph_row *row;
4190 /* Hash collision chain. */
4191 struct row_entry *next;
4194 /* A pool to allocate row_entry structures from, and the size of the
4195 pool. The pool is reallocated in scrolling_window when we find
4196 that we need a larger one. */
4198 static struct row_entry *row_entry_pool;
4199 static ptrdiff_t row_entry_pool_size;
4201 /* Index of next free entry in row_entry_pool. */
4203 static ptrdiff_t row_entry_idx;
4205 /* The hash table used during scrolling, and the table's size. This
4206 table is used to quickly identify equal rows in the desired and
4207 current matrix. */
4209 static struct row_entry **row_table;
4210 static ptrdiff_t row_table_size;
4212 /* Vectors of pointers to row_entry structures belonging to the
4213 current and desired matrix, and the size of the vectors. */
4215 static struct row_entry **old_lines, **new_lines;
4216 static ptrdiff_t old_lines_size, new_lines_size;
4218 /* A pool to allocate run structures from, and its size. */
4220 static struct run *run_pool;
4221 static ptrdiff_t runs_size;
4223 /* A vector of runs of lines found during scrolling. */
4225 static struct run **runs;
4227 /* Add glyph row ROW to the scrolling hash table. */
4229 static inline struct row_entry *
4230 add_row_entry (struct glyph_row *row)
4232 struct row_entry *entry;
4233 ptrdiff_t i = row->hash % row_table_size;
4235 entry = row_table[i];
4236 while (entry && !row_equal_p (entry->row, row, 1))
4237 entry = entry->next;
4239 if (entry == NULL)
4241 entry = row_entry_pool + row_entry_idx++;
4242 entry->row = row;
4243 entry->old_uses = entry->new_uses = 0;
4244 entry->new_line_number = 0;
4245 entry->bucket = i;
4246 entry->next = row_table[i];
4247 row_table[i] = entry;
4250 return entry;
4254 /* Try to reuse part of the current display of W by scrolling lines.
4255 HEADER_LINE_P non-zero means W has a header line.
4257 The algorithm is taken from Communications of the ACM, Apr78 "A
4258 Technique for Isolating Differences Between Files." It should take
4259 O(N) time.
4261 A short outline of the steps of the algorithm
4263 1. Skip lines equal at the start and end of both matrices.
4265 2. Enter rows in the current and desired matrix into a symbol
4266 table, counting how often they appear in both matrices.
4268 3. Rows that appear exactly once in both matrices serve as anchors,
4269 i.e. we assume that such lines are likely to have been moved.
4271 4. Starting from anchor lines, extend regions to be scrolled both
4272 forward and backward.
4274 Value is
4276 -1 if all rows were found to be equal.
4277 0 to indicate that we did not scroll the display, or
4278 1 if we did scroll. */
4280 static int
4281 scrolling_window (struct window *w, int header_line_p)
4283 struct glyph_matrix *desired_matrix = w->desired_matrix;
4284 struct glyph_matrix *current_matrix = w->current_matrix;
4285 int yb = window_text_bottom_y (w);
4286 ptrdiff_t i;
4287 int j, first_old, first_new, last_old, last_new;
4288 int nruns, run_idx;
4289 ptrdiff_t n;
4290 struct row_entry *entry;
4291 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4293 /* Skip over rows equal at the start. */
4294 for (i = header_line_p ? 1 : 0; i < current_matrix->nrows - 1; ++i)
4296 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4297 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4299 if (c->enabled_p
4300 && d->enabled_p
4301 && !d->redraw_fringe_bitmaps_p
4302 && c->y == d->y
4303 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4304 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4305 && row_equal_p (c, d, 1))
4307 assign_row (c, d);
4308 d->enabled_p = 0;
4310 else
4311 break;
4314 /* Give up if some rows in the desired matrix are not enabled. */
4315 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4316 return -1;
4318 first_old = first_new = i;
4320 /* Set last_new to the index + 1 of the row that reaches the
4321 bottom boundary in the desired matrix. Give up if we find a
4322 disabled row before we reach the bottom boundary. */
4323 i = first_new + 1;
4324 while (i < desired_matrix->nrows - 1)
4326 int bottom;
4328 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4329 return 0;
4330 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4331 if (bottom <= yb)
4332 ++i;
4333 if (bottom >= yb)
4334 break;
4337 last_new = i;
4339 /* Set last_old to the index + 1 of the row that reaches the bottom
4340 boundary in the current matrix. We don't look at the enabled
4341 flag here because we plan to reuse part of the display even if
4342 other parts are disabled. */
4343 i = first_old + 1;
4344 while (i < current_matrix->nrows - 1)
4346 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4347 if (bottom <= yb)
4348 ++i;
4349 if (bottom >= yb)
4350 break;
4353 last_old = i;
4355 /* Skip over rows equal at the bottom. */
4356 i = last_new;
4357 j = last_old;
4358 while (i - 1 > first_new
4359 && j - 1 > first_old
4360 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4361 && (MATRIX_ROW (current_matrix, j - 1)->y
4362 == MATRIX_ROW (desired_matrix, i - 1)->y)
4363 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4364 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4365 MATRIX_ROW (current_matrix, j - 1), 1))
4366 --i, --j;
4367 last_new = i;
4368 last_old = j;
4370 /* Nothing to do if all rows are equal. */
4371 if (last_new == first_new)
4372 return 0;
4374 /* Check for integer overflow in size calculation.
4376 If next_almost_prime checks (N) for divisibility by 2..10, then
4377 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4378 So, set next_almost_prime_increment_max to 10.
4380 It's just a coincidence that next_almost_prime_increment_max ==
4381 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4382 13, then next_almost_prime_increment_max would be 14, e.g.,
4383 because next_almost_prime (113) would be 127. */
4385 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4386 enum { next_almost_prime_increment_max = 10 };
4387 ptrdiff_t row_table_max =
4388 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4389 - next_almost_prime_increment_max);
4390 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4391 if (current_nrows_max < current_matrix->nrows)
4392 memory_full (SIZE_MAX);
4395 /* Reallocate vectors, tables etc. if necessary. */
4397 if (current_matrix->nrows > old_lines_size)
4398 old_lines = xpalloc (old_lines, &old_lines_size,
4399 current_matrix->nrows - old_lines_size,
4400 INT_MAX, sizeof *old_lines);
4402 if (desired_matrix->nrows > new_lines_size)
4403 new_lines = xpalloc (new_lines, &new_lines_size,
4404 desired_matrix->nrows - new_lines_size,
4405 INT_MAX, sizeof *new_lines);
4407 n = desired_matrix->nrows;
4408 n += current_matrix->nrows;
4409 if (row_table_size < 3 * n)
4411 ptrdiff_t size = next_almost_prime (3 * n);
4412 row_table = xnrealloc (row_table, size, sizeof *row_table);
4413 row_table_size = size;
4414 memset (row_table, 0, size * sizeof *row_table);
4417 if (n > row_entry_pool_size)
4418 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4419 n - row_entry_pool_size,
4420 -1, sizeof *row_entry_pool);
4422 if (desired_matrix->nrows > runs_size)
4424 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4425 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4426 runs_size = desired_matrix->nrows;
4429 nruns = run_idx = 0;
4430 row_entry_idx = 0;
4432 /* Add rows from the current and desired matrix to the hash table
4433 row_hash_table to be able to find equal ones quickly. */
4435 for (i = first_old; i < last_old; ++i)
4437 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4439 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4440 old_lines[i] = entry;
4441 ++entry->old_uses;
4443 else
4444 old_lines[i] = NULL;
4447 for (i = first_new; i < last_new; ++i)
4449 xassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4450 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4451 ++entry->new_uses;
4452 entry->new_line_number = i;
4453 new_lines[i] = entry;
4456 /* Identify moves based on lines that are unique and equal
4457 in both matrices. */
4458 for (i = first_old; i < last_old;)
4459 if (old_lines[i]
4460 && old_lines[i]->old_uses == 1
4461 && old_lines[i]->new_uses == 1)
4463 int p, q;
4464 int new_line = old_lines[i]->new_line_number;
4465 struct run *run = run_pool + run_idx++;
4467 /* Record move. */
4468 run->current_vpos = i;
4469 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4470 run->desired_vpos = new_line;
4471 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4472 run->nrows = 1;
4473 run->height = MATRIX_ROW (current_matrix, i)->height;
4475 /* Extend backward. */
4476 p = i - 1;
4477 q = new_line - 1;
4478 while (p > first_old
4479 && q > first_new
4480 && old_lines[p] == new_lines[q])
4482 int h = MATRIX_ROW (current_matrix, p)->height;
4483 --run->current_vpos;
4484 --run->desired_vpos;
4485 ++run->nrows;
4486 run->height += h;
4487 run->desired_y -= h;
4488 run->current_y -= h;
4489 --p, --q;
4492 /* Extend forward. */
4493 p = i + 1;
4494 q = new_line + 1;
4495 while (p < last_old
4496 && q < last_new
4497 && old_lines[p] == new_lines[q])
4499 int h = MATRIX_ROW (current_matrix, p)->height;
4500 ++run->nrows;
4501 run->height += h;
4502 ++p, ++q;
4505 /* Insert run into list of all runs. Order runs by copied
4506 pixel lines. Note that we record runs that don't have to
4507 be copied because they are already in place. This is done
4508 because we can avoid calling update_window_line in this
4509 case. */
4510 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4512 for (q = nruns; q > p; --q)
4513 runs[q] = runs[q - 1];
4514 runs[p] = run;
4515 ++nruns;
4517 i += run->nrows;
4519 else
4520 ++i;
4522 /* Do the moves. Do it in a way that we don't overwrite something
4523 we want to copy later on. This is not solvable in general
4524 because there is only one display and we don't have a way to
4525 exchange areas on this display. Example:
4527 +-----------+ +-----------+
4528 | A | | B |
4529 +-----------+ --> +-----------+
4530 | B | | A |
4531 +-----------+ +-----------+
4533 Instead, prefer bigger moves, and invalidate moves that would
4534 copy from where we copied to. */
4536 for (i = 0; i < nruns; ++i)
4537 if (runs[i]->nrows > 0)
4539 struct run *r = runs[i];
4541 /* Copy on the display. */
4542 if (r->current_y != r->desired_y)
4544 rif->clear_window_mouse_face (w);
4545 rif->scroll_run_hook (w, r);
4547 /* Invalidate runs that copy from where we copied to. */
4548 for (j = i + 1; j < nruns; ++j)
4550 struct run *p = runs[j];
4552 if ((p->current_y >= r->desired_y
4553 && p->current_y < r->desired_y + r->height)
4554 || (p->current_y + p->height >= r->desired_y
4555 && (p->current_y + p->height
4556 < r->desired_y + r->height)))
4557 p->nrows = 0;
4561 /* Assign matrix rows. */
4562 for (j = 0; j < r->nrows; ++j)
4564 struct glyph_row *from, *to;
4565 int to_overlapped_p;
4567 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4568 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4569 to_overlapped_p = to->overlapped_p;
4570 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4571 assign_row (to, from);
4572 to->enabled_p = 1, from->enabled_p = 0;
4573 to->overlapped_p = to_overlapped_p;
4577 /* Clear the hash table, for the next time. */
4578 for (i = 0; i < row_entry_idx; ++i)
4579 row_table[row_entry_pool[i].bucket] = NULL;
4581 /* Value is 1 to indicate that we scrolled the display. */
4582 return 0 < nruns;
4587 /************************************************************************
4588 Frame-Based Updates
4589 ************************************************************************/
4591 /* Update the desired frame matrix of frame F.
4593 FORCE_P non-zero means that the update should not be stopped by
4594 pending input. INHIBIT_HAIRY_ID_P non-zero means that scrolling
4595 should not be tried.
4597 Value is non-zero if update was stopped due to pending input. */
4599 static int
4600 update_frame_1 (struct frame *f, int force_p, int inhibit_id_p)
4602 /* Frame matrices to work on. */
4603 struct glyph_matrix *current_matrix = f->current_matrix;
4604 struct glyph_matrix *desired_matrix = f->desired_matrix;
4605 int i;
4606 int pause_p;
4607 int preempt_count = baud_rate / 2400 + 1;
4609 xassert (current_matrix && desired_matrix);
4611 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4612 calculate_costs (f);
4614 if (preempt_count <= 0)
4615 preempt_count = 1;
4617 #if !PERIODIC_PREEMPTION_CHECKING
4618 if (!force_p && detect_input_pending_ignore_squeezables ())
4620 pause_p = 1;
4621 goto do_pause;
4623 #endif
4625 /* If we cannot insert/delete lines, it's no use trying it. */
4626 if (!FRAME_LINE_INS_DEL_OK (f))
4627 inhibit_id_p = 1;
4629 /* See if any of the desired lines are enabled; don't compute for
4630 i/d line if just want cursor motion. */
4631 for (i = 0; i < desired_matrix->nrows; i++)
4632 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4633 break;
4635 /* Try doing i/d line, if not yet inhibited. */
4636 if (!inhibit_id_p && i < desired_matrix->nrows)
4637 force_p |= scrolling (f);
4639 /* Update the individual lines as needed. Do bottom line first. */
4640 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4641 update_frame_line (f, desired_matrix->nrows - 1);
4643 /* Now update the rest of the lines. */
4644 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4646 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4648 if (FRAME_TERMCAP_P (f))
4650 /* Flush out every so many lines.
4651 Also flush out if likely to have more than 1k buffered
4652 otherwise. I'm told that some telnet connections get
4653 really screwed by more than 1k output at once. */
4654 FILE *display_output = FRAME_TTY (f)->output;
4655 if (display_output)
4657 int outq = PENDING_OUTPUT_COUNT (display_output);
4658 if (outq > 900
4659 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4661 fflush (display_output);
4662 if (preempt_count == 1)
4664 #ifdef EMACS_OUTQSIZE
4665 if (EMACS_OUTQSIZE (0, &outq) < 0)
4666 /* Probably not a tty. Ignore the error and reset
4667 the outq count. */
4668 outq = PENDING_OUTPUT_COUNT (FRAME_TTY (f->output));
4669 #endif
4670 outq *= 10;
4671 if (baud_rate <= outq && baud_rate > 0)
4672 sleep (outq / baud_rate);
4678 #if PERIODIC_PREEMPTION_CHECKING
4679 if (!force_p)
4681 EMACS_TIME tm, dif;
4682 EMACS_GET_TIME (tm);
4683 EMACS_SUB_TIME (dif, preemption_next_check, tm);
4684 if (EMACS_TIME_NEG_P (dif))
4686 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
4687 if (detect_input_pending_ignore_squeezables ())
4688 break;
4691 #else
4692 if (!force_p && (i - 1) % preempt_count == 0)
4693 detect_input_pending_ignore_squeezables ();
4694 #endif
4696 update_frame_line (f, i);
4700 pause_p = (i < FRAME_LINES (f) - 1) ? i : 0;
4702 /* Now just clean up termcap drivers and set cursor, etc. */
4703 if (!pause_p)
4705 if ((cursor_in_echo_area
4706 /* If we are showing a message instead of the mini-buffer,
4707 show the cursor for the message instead of for the
4708 (now hidden) mini-buffer contents. */
4709 || (EQ (minibuf_window, selected_window)
4710 && EQ (minibuf_window, echo_area_window)
4711 && !NILP (echo_area_buffer[0])))
4712 /* These cases apply only to the frame that contains
4713 the active mini-buffer window. */
4714 && FRAME_HAS_MINIBUF_P (f)
4715 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4717 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4718 int row, col;
4720 if (cursor_in_echo_area < 0)
4722 /* Negative value of cursor_in_echo_area means put
4723 cursor at beginning of line. */
4724 row = top;
4725 col = 0;
4727 else
4729 /* Positive value of cursor_in_echo_area means put
4730 cursor at the end of the prompt. If the mini-buffer
4731 is several lines high, find the last line that has
4732 any text on it. */
4733 row = FRAME_LINES (f);
4736 --row;
4737 col = 0;
4739 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4741 /* Frame rows are filled up with spaces that
4742 must be ignored here. */
4743 struct glyph_row *r = MATRIX_ROW (current_matrix,
4744 row);
4745 struct glyph *start = r->glyphs[TEXT_AREA];
4746 struct glyph *last = start + r->used[TEXT_AREA];
4748 while (last > start
4749 && (last - 1)->charpos < 0)
4750 --last;
4752 col = last - start;
4755 while (row > top && col == 0);
4757 /* Make sure COL is not out of range. */
4758 if (col >= FRAME_CURSOR_X_LIMIT (f))
4760 /* If we have another row, advance cursor into it. */
4761 if (row < FRAME_LINES (f) - 1)
4763 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4764 row++;
4766 /* Otherwise move it back in range. */
4767 else
4768 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4772 cursor_to (f, row, col);
4774 else
4776 /* We have only one cursor on terminal frames. Use it to
4777 display the cursor of the selected window. */
4778 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4779 if (w->cursor.vpos >= 0
4780 /* The cursor vpos may be temporarily out of bounds
4781 in the following situation: There is one window,
4782 with the cursor in the lower half of it. The window
4783 is split, and a message causes a redisplay before
4784 a new cursor position has been computed. */
4785 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4787 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4788 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4790 if (INTEGERP (w->left_margin_cols))
4791 x += XFASTINT (w->left_margin_cols);
4793 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4794 cursor_to (f, y, x);
4799 #if !PERIODIC_PREEMPTION_CHECKING
4800 do_pause:
4801 #endif
4803 clear_desired_matrices (f);
4804 return pause_p;
4808 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4810 static int
4811 scrolling (struct frame *frame)
4813 int unchanged_at_top, unchanged_at_bottom;
4814 int window_size;
4815 int changed_lines;
4816 int *old_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4817 int *new_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4818 int *draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4819 int *old_draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4820 register int i;
4821 int free_at_end_vpos = FRAME_LINES (frame);
4822 struct glyph_matrix *current_matrix = frame->current_matrix;
4823 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4825 if (!current_matrix)
4826 abort ();
4828 /* Compute hash codes of all the lines. Also calculate number of
4829 changed lines, number of unchanged lines at the beginning, and
4830 number of unchanged lines at the end. */
4831 changed_lines = 0;
4832 unchanged_at_top = 0;
4833 unchanged_at_bottom = FRAME_LINES (frame);
4834 for (i = 0; i < FRAME_LINES (frame); i++)
4836 /* Give up on this scrolling if some old lines are not enabled. */
4837 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4838 return 0;
4839 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4840 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4842 /* This line cannot be redrawn, so don't let scrolling mess it. */
4843 new_hash[i] = old_hash[i];
4844 #define INFINITY 1000000 /* Taken from scroll.c */
4845 draw_cost[i] = INFINITY;
4847 else
4849 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4850 draw_cost[i] = line_draw_cost (desired_matrix, i);
4853 if (old_hash[i] != new_hash[i])
4855 changed_lines++;
4856 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4858 else if (i == unchanged_at_top)
4859 unchanged_at_top++;
4860 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4863 /* If changed lines are few, don't allow preemption, don't scroll. */
4864 if ((!FRAME_SCROLL_REGION_OK (frame)
4865 && changed_lines < baud_rate / 2400)
4866 || unchanged_at_bottom == FRAME_LINES (frame))
4867 return 1;
4869 window_size = (FRAME_LINES (frame) - unchanged_at_top
4870 - unchanged_at_bottom);
4872 if (FRAME_SCROLL_REGION_OK (frame))
4873 free_at_end_vpos -= unchanged_at_bottom;
4874 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4875 free_at_end_vpos = -1;
4877 /* If large window, fast terminal and few lines in common between
4878 current frame and desired frame, don't bother with i/d calc. */
4879 if (!FRAME_SCROLL_REGION_OK (frame)
4880 && window_size >= 18 && baud_rate > 2400
4881 && (window_size >=
4882 10 * scrolling_max_lines_saved (unchanged_at_top,
4883 FRAME_LINES (frame) - unchanged_at_bottom,
4884 old_hash, new_hash, draw_cost)))
4885 return 0;
4887 if (window_size < 2)
4888 return 0;
4890 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4891 draw_cost + unchanged_at_top - 1,
4892 old_draw_cost + unchanged_at_top - 1,
4893 old_hash + unchanged_at_top - 1,
4894 new_hash + unchanged_at_top - 1,
4895 free_at_end_vpos - unchanged_at_top);
4897 return 0;
4901 /* Count the number of blanks at the start of the vector of glyphs R
4902 which is LEN glyphs long. */
4904 static int
4905 count_blanks (struct glyph *r, int len)
4907 int i;
4909 for (i = 0; i < len; ++i)
4910 if (!CHAR_GLYPH_SPACE_P (r[i]))
4911 break;
4913 return i;
4917 /* Count the number of glyphs in common at the start of the glyph
4918 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4919 of STR2. Value is the number of equal glyphs equal at the start. */
4921 static int
4922 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4924 struct glyph *p1 = str1;
4925 struct glyph *p2 = str2;
4927 while (p1 < end1
4928 && p2 < end2
4929 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4930 ++p1, ++p2;
4932 return p1 - str1;
4936 /* Char insertion/deletion cost vector, from term.c */
4938 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4941 /* Perform a frame-based update on line VPOS in frame FRAME. */
4943 static void
4944 update_frame_line (struct frame *f, int vpos)
4946 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4947 int tem;
4948 int osp, nsp, begmatch, endmatch, olen, nlen;
4949 struct glyph_matrix *current_matrix = f->current_matrix;
4950 struct glyph_matrix *desired_matrix = f->desired_matrix;
4951 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4952 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4953 int must_write_whole_line_p;
4954 int write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4955 int colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4956 != FACE_TTY_DEFAULT_BG_COLOR);
4958 if (colored_spaces_p)
4959 write_spaces_p = 1;
4961 /* Current row not enabled means it has unknown contents. We must
4962 write the whole desired line in that case. */
4963 must_write_whole_line_p = !current_row->enabled_p;
4964 if (must_write_whole_line_p)
4966 obody = 0;
4967 olen = 0;
4969 else
4971 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4972 olen = current_row->used[TEXT_AREA];
4974 /* Ignore trailing spaces, if we can. */
4975 if (!write_spaces_p)
4976 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4977 olen--;
4980 current_row->enabled_p = 1;
4981 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4983 /* If desired line is empty, just clear the line. */
4984 if (!desired_row->enabled_p)
4986 nlen = 0;
4987 goto just_erase;
4990 nbody = desired_row->glyphs[TEXT_AREA];
4991 nlen = desired_row->used[TEXT_AREA];
4992 nend = nbody + nlen;
4994 /* If display line has unknown contents, write the whole line. */
4995 if (must_write_whole_line_p)
4997 /* Ignore spaces at the end, if we can. */
4998 if (!write_spaces_p)
4999 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5000 --nlen;
5002 /* Write the contents of the desired line. */
5003 if (nlen)
5005 cursor_to (f, vpos, 0);
5006 write_glyphs (f, nbody, nlen);
5009 /* Don't call clear_end_of_line if we already wrote the whole
5010 line. The cursor will not be at the right margin in that
5011 case but in the line below. */
5012 if (nlen < FRAME_TOTAL_COLS (f))
5014 cursor_to (f, vpos, nlen);
5015 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
5017 else
5018 /* Make sure we are in the right row, otherwise cursor movement
5019 with cmgoto might use `ch' in the wrong row. */
5020 cursor_to (f, vpos, 0);
5022 make_current (desired_matrix, current_matrix, vpos);
5023 return;
5026 /* Pretend trailing spaces are not there at all,
5027 unless for one reason or another we must write all spaces. */
5028 if (!write_spaces_p)
5029 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5030 nlen--;
5032 /* If there's no i/d char, quickly do the best we can without it. */
5033 if (!FRAME_CHAR_INS_DEL_OK (f))
5035 int i, j;
5037 /* Find the first glyph in desired row that doesn't agree with
5038 a glyph in the current row, and write the rest from there on. */
5039 for (i = 0; i < nlen; i++)
5041 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
5043 /* Find the end of the run of different glyphs. */
5044 j = i + 1;
5045 while (j < nlen
5046 && (j >= olen
5047 || !GLYPH_EQUAL_P (nbody + j, obody + j)
5048 || CHAR_GLYPH_PADDING_P (nbody[j])))
5049 ++j;
5051 /* Output this run of non-matching chars. */
5052 cursor_to (f, vpos, i);
5053 write_glyphs (f, nbody + i, j - i);
5054 i = j - 1;
5056 /* Now find the next non-match. */
5060 /* Clear the rest of the line, or the non-clear part of it. */
5061 if (olen > nlen)
5063 cursor_to (f, vpos, nlen);
5064 clear_end_of_line (f, olen);
5067 /* Make current row = desired row. */
5068 make_current (desired_matrix, current_matrix, vpos);
5069 return;
5072 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5073 characters in a row. */
5075 if (!olen)
5077 /* If current line is blank, skip over initial spaces, if
5078 possible, and write the rest. */
5079 if (write_spaces_p)
5080 nsp = 0;
5081 else
5082 nsp = count_blanks (nbody, nlen);
5084 if (nlen > nsp)
5086 cursor_to (f, vpos, nsp);
5087 write_glyphs (f, nbody + nsp, nlen - nsp);
5090 /* Exchange contents between current_frame and new_frame. */
5091 make_current (desired_matrix, current_matrix, vpos);
5092 return;
5095 /* Compute number of leading blanks in old and new contents. */
5096 osp = count_blanks (obody, olen);
5097 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
5099 /* Compute number of matching chars starting with first non-blank. */
5100 begmatch = count_match (obody + osp, obody + olen,
5101 nbody + nsp, nbody + nlen);
5103 /* Spaces in new match implicit space past the end of old. */
5104 /* A bug causing this to be a no-op was fixed in 18.29. */
5105 if (!write_spaces_p && osp + begmatch == olen)
5107 np1 = nbody + nsp;
5108 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
5109 ++begmatch;
5112 /* Avoid doing insert/delete char
5113 just cause number of leading spaces differs
5114 when the following text does not match. */
5115 if (begmatch == 0 && osp != nsp)
5116 osp = nsp = min (osp, nsp);
5118 /* Find matching characters at end of line */
5119 op1 = obody + olen;
5120 np1 = nbody + nlen;
5121 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5122 while (op1 > op2
5123 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
5125 op1--;
5126 np1--;
5128 endmatch = obody + olen - op1;
5130 /* tem gets the distance to insert or delete.
5131 endmatch is how many characters we save by doing so.
5132 Is it worth it? */
5134 tem = (nlen - nsp) - (olen - osp);
5135 if (endmatch && tem
5136 && (!FRAME_CHAR_INS_DEL_OK (f)
5137 || endmatch <= char_ins_del_cost (f)[tem]))
5138 endmatch = 0;
5140 /* nsp - osp is the distance to insert or delete.
5141 If that is nonzero, begmatch is known to be nonzero also.
5142 begmatch + endmatch is how much we save by doing the ins/del.
5143 Is it worth it? */
5145 if (nsp != osp
5146 && (!FRAME_CHAR_INS_DEL_OK (f)
5147 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
5149 begmatch = 0;
5150 endmatch = 0;
5151 osp = nsp = min (osp, nsp);
5154 /* Now go through the line, inserting, writing and
5155 deleting as appropriate. */
5157 if (osp > nsp)
5159 cursor_to (f, vpos, nsp);
5160 delete_glyphs (f, osp - nsp);
5162 else if (nsp > osp)
5164 /* If going to delete chars later in line
5165 and insert earlier in the line,
5166 must delete first to avoid losing data in the insert */
5167 if (endmatch && nlen < olen + nsp - osp)
5169 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5170 delete_glyphs (f, olen + nsp - osp - nlen);
5171 olen = nlen - (nsp - osp);
5173 cursor_to (f, vpos, osp);
5174 insert_glyphs (f, 0, nsp - osp);
5176 olen += nsp - osp;
5178 tem = nsp + begmatch + endmatch;
5179 if (nlen != tem || olen != tem)
5181 if (!endmatch || nlen == olen)
5183 /* If new text being written reaches right margin, there is
5184 no need to do clear-to-eol at the end of this function
5185 (and it would not be safe, since cursor is not going to
5186 be "at the margin" after the text is done). */
5187 if (nlen == FRAME_TOTAL_COLS (f))
5188 olen = 0;
5190 /* Function write_glyphs is prepared to do nothing
5191 if passed a length <= 0. Check it here to avoid
5192 unnecessary cursor movement. */
5193 if (nlen - tem > 0)
5195 cursor_to (f, vpos, nsp + begmatch);
5196 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5199 else if (nlen > olen)
5201 /* Here, we used to have the following simple code:
5202 ----------------------------------------
5203 write_glyphs (nbody + nsp + begmatch, olen - tem);
5204 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5205 ----------------------------------------
5206 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5207 is a padding glyph. */
5208 int out = olen - tem; /* Columns to be overwritten originally. */
5209 int del;
5211 cursor_to (f, vpos, nsp + begmatch);
5213 /* Calculate columns we can actually overwrite. */
5214 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5215 out--;
5216 write_glyphs (f, nbody + nsp + begmatch, out);
5218 /* If we left columns to be overwritten, we must delete them. */
5219 del = olen - tem - out;
5220 if (del > 0)
5221 delete_glyphs (f, del);
5223 /* At last, we insert columns not yet written out. */
5224 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5225 olen = nlen;
5227 else if (olen > nlen)
5229 cursor_to (f, vpos, nsp + begmatch);
5230 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5231 delete_glyphs (f, olen - nlen);
5232 olen = nlen;
5236 just_erase:
5237 /* If any unerased characters remain after the new line, erase them. */
5238 if (olen > nlen)
5240 cursor_to (f, vpos, nlen);
5241 clear_end_of_line (f, olen);
5244 /* Exchange contents between current_frame and new_frame. */
5245 make_current (desired_matrix, current_matrix, vpos);
5250 /***********************************************************************
5251 X/Y Position -> Buffer Position
5252 ***********************************************************************/
5254 /* Determine what's under window-relative pixel position (*X, *Y).
5255 Return the OBJECT (string or buffer) that's there.
5256 Return in *POS the position in that object.
5257 Adjust *X and *Y to character positions.
5258 Return in *DX and *DY the pixel coordinates of the click,
5259 relative to the top left corner of OBJECT, or relative to
5260 the top left corner of the character glyph at (*X, *Y)
5261 if OBJECT is nil.
5262 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5263 if the coordinates point to an empty area of the display. */
5265 Lisp_Object
5266 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)
5268 struct it it;
5269 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5270 struct text_pos startp;
5271 Lisp_Object string;
5272 struct glyph_row *row;
5273 #ifdef HAVE_WINDOW_SYSTEM
5274 struct image *img = 0;
5275 #endif
5276 int x0, x1, to_x;
5277 void *itdata = NULL;
5279 /* We used to set current_buffer directly here, but that does the
5280 wrong thing with `face-remapping-alist' (bug#2044). */
5281 Fset_buffer (w->buffer);
5282 itdata = bidi_shelve_cache ();
5283 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5284 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5285 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5286 start_display (&it, w, startp);
5287 /* start_display takes into account the header-line row, but IT's
5288 vpos still counts from the glyph row that includes the window's
5289 start position. Adjust for a possible header-line row. */
5290 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
5292 x0 = *x;
5294 /* First, move to the beginning of the row corresponding to *Y. We
5295 need to be in that row to get the correct value of base paragraph
5296 direction for the text at (*X, *Y). */
5297 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5299 /* TO_X is the pixel position that the iterator will compute for the
5300 glyph at *X. We add it.first_visible_x because iterator
5301 positions include the hscroll. */
5302 to_x = x0 + it.first_visible_x;
5303 if (it.bidi_it.paragraph_dir == R2L)
5304 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5305 text area. This is because the iterator, even in R2L
5306 paragraphs, delivers glyphs as if they started at the left
5307 margin of the window. (When we actually produce glyphs for
5308 display, we reverse their order in PRODUCE_GLYPHS, but the
5309 iterator doesn't know about that.) The following line adjusts
5310 the pixel position to the iterator geometry, which is what
5311 move_it_* routines use. (The -1 is because in a window whose
5312 text-area width is W, the rightmost pixel position is W-1, and
5313 it should be mirrored into zero pixel position.) */
5314 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5316 /* Now move horizontally in the row to the glyph under *X. Second
5317 argument is ZV to prevent move_it_in_display_line from matching
5318 based on buffer positions. */
5319 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5320 bidi_unshelve_cache (itdata, 0);
5322 Fset_buffer (old_current_buffer);
5324 *dx = x0 + it.first_visible_x - it.current_x;
5325 *dy = *y - it.current_y;
5327 string = w->buffer;
5328 if (STRINGP (it.string))
5329 string = it.string;
5330 *pos = it.current;
5331 if (it.what == IT_COMPOSITION
5332 && it.cmp_it.nchars > 1
5333 && it.cmp_it.reversed_p)
5335 /* The current display element is a grapheme cluster in a
5336 composition. In that case, we need the position of the first
5337 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5338 it.current points to the last character of the cluster, thus
5339 we must move back to the first character of the same
5340 cluster. */
5341 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5342 if (STRINGP (it.string))
5343 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5344 else
5345 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->buffer),
5346 CHARPOS (pos->pos));
5349 #ifdef HAVE_WINDOW_SYSTEM
5350 if (it.what == IT_IMAGE)
5352 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5353 && !NILP (img->spec))
5354 *object = img->spec;
5356 #endif
5358 if (it.vpos < w->current_matrix->nrows
5359 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5360 row->enabled_p))
5362 if (it.hpos < row->used[TEXT_AREA])
5364 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5365 #ifdef HAVE_WINDOW_SYSTEM
5366 if (img)
5368 *dy -= row->ascent - glyph->ascent;
5369 *dx += glyph->slice.img.x;
5370 *dy += glyph->slice.img.y;
5371 /* Image slices positions are still relative to the entire image */
5372 *width = img->width;
5373 *height = img->height;
5375 else
5376 #endif
5378 *width = glyph->pixel_width;
5379 *height = glyph->ascent + glyph->descent;
5382 else
5384 *width = 0;
5385 *height = row->height;
5388 else
5390 *width = *height = 0;
5393 /* Add extra (default width) columns if clicked after EOL. */
5394 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5395 if (x0 > x1)
5396 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5398 *x = it.hpos;
5399 *y = it.vpos;
5401 return string;
5405 /* Value is the string under window-relative coordinates X/Y in the
5406 mode line or header line (PART says which) of window W, or nil if none.
5407 *CHARPOS is set to the position in the string returned. */
5409 Lisp_Object
5410 mode_line_string (struct window *w, enum window_part part,
5411 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5412 int *dx, int *dy, int *width, int *height)
5414 struct glyph_row *row;
5415 struct glyph *glyph, *end;
5416 int x0, y0;
5417 Lisp_Object string = Qnil;
5419 if (part == ON_MODE_LINE)
5420 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5421 else
5422 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5423 y0 = *y - row->y;
5424 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5426 if (row->mode_line_p && row->enabled_p)
5428 /* Find the glyph under X. If we find one with a string object,
5429 it's the one we were looking for. */
5430 glyph = row->glyphs[TEXT_AREA];
5431 end = glyph + row->used[TEXT_AREA];
5432 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5433 x0 -= glyph->pixel_width;
5434 *x = glyph - row->glyphs[TEXT_AREA];
5435 if (glyph < end)
5437 string = glyph->object;
5438 *charpos = glyph->charpos;
5439 *width = glyph->pixel_width;
5440 *height = glyph->ascent + glyph->descent;
5441 #ifdef HAVE_WINDOW_SYSTEM
5442 if (glyph->type == IMAGE_GLYPH)
5444 struct image *img;
5445 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5446 if (img != NULL)
5447 *object = img->spec;
5448 y0 -= row->ascent - glyph->ascent;
5450 #endif
5452 else
5454 /* Add extra (default width) columns if clicked after EOL. */
5455 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5456 *width = 0;
5457 *height = row->height;
5460 else
5462 *x = 0;
5463 x0 = 0;
5464 *width = *height = 0;
5467 *dx = x0;
5468 *dy = y0;
5470 return string;
5474 /* Value is the string under window-relative coordinates X/Y in either
5475 marginal area, or nil if none. *CHARPOS is set to the position in
5476 the string returned. */
5478 Lisp_Object
5479 marginal_area_string (struct window *w, enum window_part part,
5480 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5481 int *dx, int *dy, int *width, int *height)
5483 struct glyph_row *row = w->current_matrix->rows;
5484 struct glyph *glyph, *end;
5485 int x0, y0, i, wy = *y;
5486 int area;
5487 Lisp_Object string = Qnil;
5489 if (part == ON_LEFT_MARGIN)
5490 area = LEFT_MARGIN_AREA;
5491 else if (part == ON_RIGHT_MARGIN)
5492 area = RIGHT_MARGIN_AREA;
5493 else
5494 abort ();
5496 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5497 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5498 break;
5499 y0 = *y - row->y;
5500 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5502 if (row->enabled_p)
5504 /* Find the glyph under X. If we find one with a string object,
5505 it's the one we were looking for. */
5506 if (area == RIGHT_MARGIN_AREA)
5507 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5508 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5509 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5510 + window_box_width (w, LEFT_MARGIN_AREA)
5511 + window_box_width (w, TEXT_AREA));
5512 else
5513 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5514 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5515 : 0);
5517 glyph = row->glyphs[area];
5518 end = glyph + row->used[area];
5519 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5520 x0 -= glyph->pixel_width;
5521 *x = glyph - row->glyphs[area];
5522 if (glyph < end)
5524 string = glyph->object;
5525 *charpos = glyph->charpos;
5526 *width = glyph->pixel_width;
5527 *height = glyph->ascent + glyph->descent;
5528 #ifdef HAVE_WINDOW_SYSTEM
5529 if (glyph->type == IMAGE_GLYPH)
5531 struct image *img;
5532 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5533 if (img != NULL)
5534 *object = img->spec;
5535 y0 -= row->ascent - glyph->ascent;
5536 x0 += glyph->slice.img.x;
5537 y0 += glyph->slice.img.y;
5539 #endif
5541 else
5543 /* Add extra (default width) columns if clicked after EOL. */
5544 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5545 *width = 0;
5546 *height = row->height;
5549 else
5551 x0 = 0;
5552 *x = 0;
5553 *width = *height = 0;
5556 *dx = x0;
5557 *dy = y0;
5559 return string;
5563 /***********************************************************************
5564 Changing Frame Sizes
5565 ***********************************************************************/
5567 #ifdef SIGWINCH
5569 static void
5570 window_change_signal (int signalnum) /* If we don't have an argument, */
5571 /* some compilers complain in signal calls. */
5573 int width, height;
5574 int old_errno = errno;
5576 struct tty_display_info *tty;
5578 signal (SIGWINCH, window_change_signal);
5579 SIGNAL_THREAD_CHECK (signalnum);
5581 /* The frame size change obviously applies to a single
5582 termcap-controlled terminal, but we can't decide which.
5583 Therefore, we resize the frames corresponding to each tty.
5585 for (tty = tty_list; tty; tty = tty->next) {
5587 if (! tty->term_initted)
5588 continue;
5590 /* Suspended tty frames have tty->input == NULL avoid trying to
5591 use it. */
5592 if (!tty->input)
5593 continue;
5595 get_tty_size (fileno (tty->input), &width, &height);
5597 if (width > 5 && height > 2) {
5598 Lisp_Object tail, frame;
5600 FOR_EACH_FRAME (tail, frame)
5601 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5602 /* Record the new sizes, but don't reallocate the data
5603 structures now. Let that be done later outside of the
5604 signal handler. */
5605 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5609 errno = old_errno;
5611 #endif /* SIGWINCH */
5614 /* Do any change in frame size that was requested by a signal. SAFE
5615 non-zero means this function is called from a place where it is
5616 safe to change frame sizes while a redisplay is in progress. */
5618 void
5619 do_pending_window_change (int safe)
5621 /* If window_change_signal should have run before, run it now. */
5622 if (redisplaying_p && !safe)
5623 return;
5625 while (delayed_size_change)
5627 Lisp_Object tail, frame;
5629 delayed_size_change = 0;
5631 FOR_EACH_FRAME (tail, frame)
5633 struct frame *f = XFRAME (frame);
5635 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5636 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5637 0, 0, safe);
5643 /* Change the frame height and/or width. Values may be given as zero to
5644 indicate no change is to take place.
5646 If DELAY is non-zero, then assume we're being called from a signal
5647 handler, and queue the change for later - perhaps the next
5648 redisplay. Since this tries to resize windows, we can't call it
5649 from a signal handler.
5651 SAFE non-zero means this function is called from a place where it's
5652 safe to change frame sizes while a redisplay is in progress. */
5654 void
5655 change_frame_size (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5657 Lisp_Object tail, frame;
5659 if (FRAME_MSDOS_P (f))
5661 /* On MS-DOS, all frames use the same screen, so a change in
5662 size affects all frames. Termcap now supports multiple
5663 ttys. */
5664 FOR_EACH_FRAME (tail, frame)
5665 if (! FRAME_WINDOW_P (XFRAME (frame)))
5666 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5667 pretend, delay, safe);
5669 else
5670 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5673 static void
5674 change_frame_size_1 (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5676 int new_frame_total_cols;
5677 int count = SPECPDL_INDEX ();
5679 /* If we can't deal with the change now, queue it for later. */
5680 if (delay || (redisplaying_p && !safe))
5682 f->new_text_lines = newheight;
5683 f->new_text_cols = newwidth;
5684 delayed_size_change = 1;
5685 return;
5688 /* This size-change overrides any pending one for this frame. */
5689 f->new_text_lines = 0;
5690 f->new_text_cols = 0;
5692 /* If an argument is zero, set it to the current value. */
5693 if (newheight == 0)
5694 newheight = FRAME_LINES (f);
5695 if (newwidth == 0)
5696 newwidth = FRAME_COLS (f);
5698 /* Compute width of windows in F.
5699 This is the width of the frame without vertical scroll bars. */
5700 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5702 /* Round up to the smallest acceptable size. */
5703 check_frame_size (f, &newheight, &newwidth);
5705 /* If we're not changing the frame size, quit now. */
5706 /* Frame width may be unchanged but the text portion may change, for example,
5707 fullscreen and remove/add scroll bar. */
5708 if (newheight == FRAME_LINES (f)
5709 && newwidth == FRAME_COLS (f) // text portion unchanged
5710 && new_frame_total_cols == FRAME_TOTAL_COLS (f)) // frame width unchanged
5711 return;
5713 BLOCK_INPUT;
5715 #ifdef MSDOS
5716 /* We only can set screen dimensions to certain values supported
5717 by our video hardware. Try to find the smallest size greater
5718 or equal to the requested dimensions. */
5719 dos_set_window_size (&newheight, &newwidth);
5720 #endif
5722 if (newheight != FRAME_LINES (f))
5724 resize_frame_windows (f, newheight, 0);
5726 /* MSDOS frames cannot PRETEND, as they change frame size by
5727 manipulating video hardware. */
5728 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5729 FrameRows (FRAME_TTY (f)) = newheight;
5732 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5734 resize_frame_windows (f, new_frame_total_cols, 1);
5736 /* MSDOS frames cannot PRETEND, as they change frame size by
5737 manipulating video hardware. */
5738 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5739 FrameCols (FRAME_TTY (f)) = newwidth;
5741 if (WINDOWP (f->tool_bar_window))
5742 XSETFASTINT (XWINDOW (f->tool_bar_window)->total_cols, newwidth);
5745 FRAME_LINES (f) = newheight;
5746 SET_FRAME_COLS (f, newwidth);
5749 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5750 int text_area_x, text_area_y, text_area_width, text_area_height;
5752 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5753 &text_area_height);
5754 if (w->cursor.x >= text_area_x + text_area_width)
5755 w->cursor.hpos = w->cursor.x = 0;
5756 if (w->cursor.y >= text_area_y + text_area_height)
5757 w->cursor.vpos = w->cursor.y = 0;
5760 adjust_glyphs (f);
5761 calculate_costs (f);
5762 SET_FRAME_GARBAGED (f);
5763 f->resized_p = 1;
5765 UNBLOCK_INPUT;
5767 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5769 run_window_configuration_change_hook (f);
5771 unbind_to (count, Qnil);
5776 /***********************************************************************
5777 Terminal Related Lisp Functions
5778 ***********************************************************************/
5780 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5781 1, 1, "FOpen termscript file: ",
5782 doc: /* Start writing all terminal output to FILE as well as the terminal.
5783 FILE = nil means just close any termscript file currently open. */)
5784 (Lisp_Object file)
5786 struct tty_display_info *tty;
5788 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5789 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5790 error ("Current frame is not on a tty device");
5792 tty = CURTTY ();
5794 if (tty->termscript != 0)
5796 BLOCK_INPUT;
5797 fclose (tty->termscript);
5798 UNBLOCK_INPUT;
5800 tty->termscript = 0;
5802 if (! NILP (file))
5804 file = Fexpand_file_name (file, Qnil);
5805 tty->termscript = fopen (SSDATA (file), "w");
5806 if (tty->termscript == 0)
5807 report_file_error ("Opening termscript", Fcons (file, Qnil));
5809 return Qnil;
5813 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5814 Ssend_string_to_terminal, 1, 2, 0,
5815 doc: /* Send STRING to the terminal without alteration.
5816 Control characters in STRING will have terminal-dependent effects.
5818 Optional parameter TERMINAL specifies the tty terminal device to use.
5819 It may be a terminal object, a frame, or nil for the terminal used by
5820 the currently selected frame. In batch mode, STRING is sent to stdout
5821 when TERMINAL is nil. */)
5822 (Lisp_Object string, Lisp_Object terminal)
5824 struct terminal *t = get_terminal (terminal, 1);
5825 FILE *out;
5827 /* ??? Perhaps we should do something special for multibyte strings here. */
5828 CHECK_STRING (string);
5829 BLOCK_INPUT;
5831 if (!t)
5832 error ("Unknown terminal device");
5834 if (t->type == output_initial)
5835 out = stdout;
5836 else if (t->type != output_termcap && t->type != output_msdos_raw)
5837 error ("Device %d is not a termcap terminal device", t->id);
5838 else
5840 struct tty_display_info *tty = t->display_info.tty;
5842 if (! tty->output)
5843 error ("Terminal is currently suspended");
5845 if (tty->termscript)
5847 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5848 fflush (tty->termscript);
5850 out = tty->output;
5852 fwrite (SDATA (string), 1, SBYTES (string), out);
5853 fflush (out);
5854 UNBLOCK_INPUT;
5855 return Qnil;
5859 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5860 doc: /* Beep, or flash the screen.
5861 Also, unless an argument is given,
5862 terminate any keyboard macro currently executing. */)
5863 (Lisp_Object arg)
5865 if (!NILP (arg))
5867 if (noninteractive)
5868 putchar (07);
5869 else
5870 ring_bell (XFRAME (selected_frame));
5872 else
5873 bitch_at_user ();
5875 return Qnil;
5878 void
5879 bitch_at_user (void)
5881 if (noninteractive)
5882 putchar (07);
5883 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5884 error ("Keyboard macro terminated by a command ringing the bell");
5885 else
5886 ring_bell (XFRAME (selected_frame));
5891 /***********************************************************************
5892 Sleeping, Waiting
5893 ***********************************************************************/
5895 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5896 doc: /* Pause, without updating display, for SECONDS seconds.
5897 SECONDS may be a floating-point value, meaning that you can wait for a
5898 fraction of a second. Optional second arg MILLISECONDS specifies an
5899 additional wait period, in milliseconds; this may be useful if your
5900 Emacs was built without floating point support.
5901 \(Not all operating systems support waiting for a fraction of a second.) */)
5902 (Lisp_Object seconds, Lisp_Object milliseconds)
5904 int sec, usec;
5906 if (NILP (milliseconds))
5907 XSETINT (milliseconds, 0);
5908 else
5909 CHECK_NUMBER (milliseconds);
5910 usec = XINT (milliseconds) * 1000;
5913 double duration = extract_float (seconds);
5914 sec = (int) duration;
5915 usec += (duration - sec) * 1000000;
5918 #ifndef EMACS_HAS_USECS
5919 if (sec == 0 && usec != 0)
5920 error ("Millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
5921 #endif
5923 /* Assure that 0 <= usec < 1000000. */
5924 if (usec < 0)
5926 /* We can't rely on the rounding being correct if usec is negative. */
5927 if (-1000000 < usec)
5928 sec--, usec += 1000000;
5929 else
5930 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
5932 else
5933 sec += usec / 1000000, usec %= 1000000;
5935 if (sec < 0 || (sec == 0 && usec == 0))
5936 return Qnil;
5938 wait_reading_process_output (sec, usec, 0, 0, Qnil, NULL, 0);
5940 return Qnil;
5944 /* This is just like wait_reading_process_output, except that
5945 it does redisplay.
5947 TIMEOUT is number of seconds to wait (float or integer),
5948 or t to wait forever.
5949 READING is 1 if reading input.
5950 If DO_DISPLAY is >0 display process output while waiting.
5951 If DO_DISPLAY is >1 perform an initial redisplay before waiting.
5954 Lisp_Object
5955 sit_for (Lisp_Object timeout, int reading, int do_display)
5957 int sec, usec;
5959 swallow_events (do_display);
5961 if ((detect_input_pending_run_timers (do_display))
5962 || !NILP (Vexecuting_kbd_macro))
5963 return Qnil;
5965 if (do_display >= 2)
5966 redisplay_preserve_echo_area (2);
5968 if (INTEGERP (timeout))
5970 sec = XINT (timeout);
5971 usec = 0;
5973 else if (FLOATP (timeout))
5975 double seconds = XFLOAT_DATA (timeout);
5976 sec = (int) seconds;
5977 usec = (int) ((seconds - sec) * 1000000);
5979 else if (EQ (timeout, Qt))
5981 sec = 0;
5982 usec = 0;
5984 else
5985 wrong_type_argument (Qnumberp, timeout);
5987 if (sec == 0 && usec == 0 && !EQ (timeout, Qt))
5988 return Qt;
5990 #ifdef SIGIO
5991 gobble_input (0);
5992 #endif
5994 wait_reading_process_output (sec, usec, reading ? -1 : 1, do_display,
5995 Qnil, NULL, 0);
5997 return detect_input_pending () ? Qnil : Qt;
6001 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
6002 doc: /* Perform redisplay if no input is available.
6003 If optional arg FORCE is non-nil or `redisplay-dont-pause' is non-nil,
6004 perform a full redisplay even if input is available.
6005 Return t if redisplay was performed, nil otherwise. */)
6006 (Lisp_Object force)
6008 int count;
6010 swallow_events (1);
6011 if ((detect_input_pending_run_timers (1)
6012 && NILP (force) && !redisplay_dont_pause)
6013 || !NILP (Vexecuting_kbd_macro))
6014 return Qnil;
6016 count = SPECPDL_INDEX ();
6017 if (!NILP (force) && !redisplay_dont_pause)
6018 specbind (Qredisplay_dont_pause, Qt);
6019 redisplay_preserve_echo_area (2);
6020 unbind_to (count, Qnil);
6021 return Qt;
6026 /***********************************************************************
6027 Other Lisp Functions
6028 ***********************************************************************/
6030 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
6031 session's frames, frame names, buffers, buffer-read-only flags, and
6032 buffer-modified-flags. */
6034 static Lisp_Object frame_and_buffer_state;
6037 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
6038 Sframe_or_buffer_changed_p, 0, 1, 0,
6039 doc: /* Return non-nil if the frame and buffer state appears to have changed.
6040 VARIABLE is a variable name whose value is either nil or a state vector
6041 that will be updated to contain all frames and buffers,
6042 aside from buffers whose names start with space,
6043 along with the buffers' read-only and modified flags. This allows a fast
6044 check to see whether buffer menus might need to be recomputed.
6045 If this function returns non-nil, it updates the internal vector to reflect
6046 the current state.
6048 If VARIABLE is nil, an internal variable is used. Users should not
6049 pass nil for VARIABLE. */)
6050 (Lisp_Object variable)
6052 Lisp_Object state, tail, frame, buf;
6053 Lisp_Object *vecp, *end;
6054 int n;
6056 if (! NILP (variable))
6058 CHECK_SYMBOL (variable);
6059 state = Fsymbol_value (variable);
6060 if (! VECTORP (state))
6061 goto changed;
6063 else
6064 state = frame_and_buffer_state;
6066 vecp = XVECTOR (state)->contents;
6067 end = vecp + ASIZE (state);
6069 FOR_EACH_FRAME (tail, frame)
6071 if (vecp == end)
6072 goto changed;
6073 if (!EQ (*vecp++, frame))
6074 goto changed;
6075 if (vecp == end)
6076 goto changed;
6077 if (!EQ (*vecp++, XFRAME (frame)->name))
6078 goto changed;
6080 /* Check that the buffer info matches. */
6081 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6083 buf = XCDR (XCAR (tail));
6084 /* Ignore buffers that aren't included in buffer lists. */
6085 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6086 continue;
6087 if (vecp == end)
6088 goto changed;
6089 if (!EQ (*vecp++, buf))
6090 goto changed;
6091 if (vecp == end)
6092 goto changed;
6093 if (!EQ (*vecp++, BVAR (XBUFFER (buf), read_only)))
6094 goto changed;
6095 if (vecp == end)
6096 goto changed;
6097 if (!EQ (*vecp++, Fbuffer_modified_p (buf)))
6098 goto changed;
6100 if (vecp == end)
6101 goto changed;
6102 /* Detect deletion of a buffer at the end of the list. */
6103 if (EQ (*vecp, Qlambda))
6104 return Qnil;
6106 /* Come here if we decide the data has changed. */
6107 changed:
6108 /* Count the size we will need.
6109 Start with 1 so there is room for at least one lambda at the end. */
6110 n = 1;
6111 FOR_EACH_FRAME (tail, frame)
6112 n += 2;
6113 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6114 n += 3;
6115 /* Reallocate the vector if data has grown to need it,
6116 or if it has shrunk a lot. */
6117 if (! VECTORP (state)
6118 || n > ASIZE (state)
6119 || n + 20 < ASIZE (state) / 2)
6120 /* Add 20 extra so we grow it less often. */
6122 state = Fmake_vector (make_number (n + 20), Qlambda);
6123 if (! NILP (variable))
6124 Fset (variable, state);
6125 else
6126 frame_and_buffer_state = state;
6129 /* Record the new data in the (possibly reallocated) vector. */
6130 vecp = XVECTOR (state)->contents;
6131 FOR_EACH_FRAME (tail, frame)
6133 *vecp++ = frame;
6134 *vecp++ = XFRAME (frame)->name;
6136 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6138 buf = XCDR (XCAR (tail));
6139 /* Ignore buffers that aren't included in buffer lists. */
6140 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6141 continue;
6142 *vecp++ = buf;
6143 *vecp++ = BVAR (XBUFFER (buf), read_only);
6144 *vecp++ = Fbuffer_modified_p (buf);
6146 /* Fill up the vector with lambdas (always at least one). */
6147 *vecp++ = Qlambda;
6148 while (vecp - XVECTOR (state)->contents
6149 < ASIZE (state))
6150 *vecp++ = Qlambda;
6151 /* Make sure we didn't overflow the vector. */
6152 if (vecp - XVECTOR (state)->contents
6153 > ASIZE (state))
6154 abort ();
6155 return Qt;
6160 /***********************************************************************
6161 Initialization
6162 ***********************************************************************/
6164 /* Initialization done when Emacs fork is started, before doing stty.
6165 Determine terminal type and set terminal_driver. Then invoke its
6166 decoding routine to set up variables in the terminal package. */
6168 void
6169 init_display (void)
6171 char *terminal_type;
6173 /* Construct the space glyph. */
6174 space_glyph.type = CHAR_GLYPH;
6175 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
6176 space_glyph.charpos = -1;
6178 inverse_video = 0;
6179 cursor_in_echo_area = 0;
6180 terminal_type = (char *) 0;
6182 /* Now is the time to initialize this; it's used by init_sys_modes
6183 during startup. */
6184 Vinitial_window_system = Qnil;
6186 /* SIGWINCH needs to be handled no matter what display we start
6187 with. Otherwise newly opened tty frames will not resize
6188 automatically. */
6189 #ifdef SIGWINCH
6190 #ifndef CANNOT_DUMP
6191 if (initialized)
6192 #endif /* CANNOT_DUMP */
6193 signal (SIGWINCH, window_change_signal);
6194 #endif /* SIGWINCH */
6196 /* If running as a daemon, no need to initialize any frames/terminal. */
6197 if (IS_DAEMON)
6198 return;
6200 /* If the user wants to use a window system, we shouldn't bother
6201 initializing the terminal. This is especially important when the
6202 terminal is so dumb that emacs gives up before and doesn't bother
6203 using the window system.
6205 If the DISPLAY environment variable is set and nonempty,
6206 try to use X, and die with an error message if that doesn't work. */
6208 #ifdef HAVE_X_WINDOWS
6209 if (! inhibit_window_system && ! display_arg)
6211 char *display;
6212 display = getenv ("DISPLAY");
6213 display_arg = (display != 0 && *display != 0);
6215 if (display_arg && !x_display_ok (display))
6217 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6218 display);
6219 inhibit_window_system = 1;
6223 if (!inhibit_window_system && display_arg)
6225 Vinitial_window_system = Qx;
6226 #ifdef HAVE_X11
6227 Vwindow_system_version = make_number (11);
6228 #endif
6229 #if defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
6230 /* In some versions of ncurses,
6231 tputs crashes if we have not called tgetent.
6232 So call tgetent. */
6233 { char b[2044]; tgetent (b, "xterm");}
6234 #endif
6235 adjust_frame_glyphs_initially ();
6236 return;
6238 #endif /* HAVE_X_WINDOWS */
6240 #ifdef HAVE_NTGUI
6241 if (!inhibit_window_system)
6243 Vinitial_window_system = Qw32;
6244 Vwindow_system_version = make_number (1);
6245 adjust_frame_glyphs_initially ();
6246 return;
6248 #endif /* HAVE_NTGUI */
6250 #ifdef HAVE_NS
6251 if (!inhibit_window_system
6252 #ifndef CANNOT_DUMP
6253 && initialized
6254 #endif
6257 Vinitial_window_system = Qns;
6258 Vwindow_system_version = make_number (10);
6259 adjust_frame_glyphs_initially ();
6260 return;
6262 #endif
6264 /* If no window system has been specified, try to use the terminal. */
6265 if (! isatty (0))
6267 fatal ("standard input is not a tty");
6268 exit (1);
6271 #ifdef WINDOWSNT
6272 terminal_type = "w32console";
6273 #else
6274 /* Look at the TERM variable. */
6275 terminal_type = (char *) getenv ("TERM");
6276 #endif
6277 if (!terminal_type)
6279 #ifdef HAVE_WINDOW_SYSTEM
6280 if (! inhibit_window_system)
6281 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6282 else
6283 #endif /* HAVE_WINDOW_SYSTEM */
6284 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6285 exit (1);
6289 struct terminal *t;
6290 struct frame *f = XFRAME (selected_frame);
6292 /* Open a display on the controlling tty. */
6293 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6295 /* Convert the initial frame to use the new display. */
6296 if (f->output_method != output_initial)
6297 abort ();
6298 f->output_method = t->type;
6299 f->terminal = t;
6301 t->reference_count++;
6302 #ifdef MSDOS
6303 f->output_data.tty->display_info = &the_only_display_info;
6304 #else
6305 if (f->output_method == output_termcap)
6306 create_tty_output (f);
6307 #endif
6308 t->display_info.tty->top_frame = selected_frame;
6309 change_frame_size (XFRAME (selected_frame),
6310 FrameRows (t->display_info.tty),
6311 FrameCols (t->display_info.tty), 0, 0, 1);
6313 /* Delete the initial terminal. */
6314 if (--initial_terminal->reference_count == 0
6315 && initial_terminal->delete_terminal_hook)
6316 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6318 /* Update frame parameters to reflect the new type. */
6319 Fmodify_frame_parameters
6320 (selected_frame, Fcons (Fcons (Qtty_type,
6321 Ftty_type (selected_frame)), Qnil));
6322 if (t->display_info.tty->name)
6323 Fmodify_frame_parameters (selected_frame,
6324 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6325 Qnil));
6326 else
6327 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6328 Qnil));
6332 struct frame *sf = SELECTED_FRAME ();
6333 int width = FRAME_TOTAL_COLS (sf);
6334 int height = FRAME_LINES (sf);
6336 /* If these sizes are so big they cause overflow, just ignore the
6337 change. It's not clear what better we could do. The rest of
6338 the code assumes that (width + 2) * height * sizeof (struct glyph)
6339 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6340 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6341 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6342 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6343 < (width + 2) * height))
6344 fatal ("screen size %dx%d too big", width, height);
6347 adjust_frame_glyphs_initially ();
6348 calculate_costs (XFRAME (selected_frame));
6350 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6351 if (initialized
6352 && !noninteractive
6353 && NILP (Vinitial_window_system))
6355 /* For the initial frame, we don't have any way of knowing what
6356 are the foreground and background colors of the terminal. */
6357 struct frame *sf = SELECTED_FRAME ();
6359 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6360 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6361 call0 (intern ("tty-set-up-initial-frame-faces"));
6367 /***********************************************************************
6368 Blinking cursor
6369 ***********************************************************************/
6371 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6372 Sinternal_show_cursor, 2, 2, 0,
6373 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6374 WINDOW nil means use the selected window. SHOW non-nil means
6375 show a cursor in WINDOW in the next redisplay. SHOW nil means
6376 don't show a cursor. */)
6377 (Lisp_Object window, Lisp_Object show)
6379 /* Don't change cursor state while redisplaying. This could confuse
6380 output routines. */
6381 if (!redisplaying_p)
6383 if (NILP (window))
6384 window = selected_window;
6385 else
6386 CHECK_WINDOW (window);
6388 XWINDOW (window)->cursor_off_p = NILP (show);
6391 return Qnil;
6395 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6396 Sinternal_show_cursor_p, 0, 1, 0,
6397 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6398 WINDOW nil or omitted means report on the selected window. */)
6399 (Lisp_Object window)
6401 struct window *w;
6403 if (NILP (window))
6404 window = selected_window;
6405 else
6406 CHECK_WINDOW (window);
6408 w = XWINDOW (window);
6409 return w->cursor_off_p ? Qnil : Qt;
6412 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6413 Slast_nonminibuf_frame, 0, 0, 0,
6414 doc: /* Value is last nonminibuffer frame. */)
6415 (void)
6417 Lisp_Object frame = Qnil;
6419 if (last_nonminibuf_frame)
6420 XSETFRAME (frame, last_nonminibuf_frame);
6422 return frame;
6425 /***********************************************************************
6426 Initialization
6427 ***********************************************************************/
6429 void
6430 syms_of_display (void)
6432 defsubr (&Sredraw_frame);
6433 defsubr (&Sredraw_display);
6434 defsubr (&Sframe_or_buffer_changed_p);
6435 defsubr (&Sopen_termscript);
6436 defsubr (&Sding);
6437 defsubr (&Sredisplay);
6438 defsubr (&Ssleep_for);
6439 defsubr (&Ssend_string_to_terminal);
6440 defsubr (&Sinternal_show_cursor);
6441 defsubr (&Sinternal_show_cursor_p);
6442 defsubr (&Slast_nonminibuf_frame);
6444 #if GLYPH_DEBUG
6445 defsubr (&Sdump_redisplay_history);
6446 #endif
6448 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6449 staticpro (&frame_and_buffer_state);
6451 DEFSYM (Qdisplay_table, "display-table");
6452 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6454 DEFVAR_INT ("baud-rate", baud_rate,
6455 doc: /* *The output baud rate of the terminal.
6456 On most systems, changing this value will affect the amount of padding
6457 and the other strategic decisions made during redisplay. */);
6459 DEFVAR_BOOL ("inverse-video", inverse_video,
6460 doc: /* *Non-nil means invert the entire frame display.
6461 This means everything is in inverse video which otherwise would not be. */);
6463 DEFVAR_BOOL ("visible-bell", visible_bell,
6464 doc: /* *Non-nil means try to flash the frame to represent a bell.
6466 See also `ring-bell-function'. */);
6468 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6469 doc: /* *Non-nil means no need to redraw entire frame after suspending.
6470 A non-nil value is useful if the terminal can automatically preserve
6471 Emacs's frame display when you reenter Emacs.
6472 It is up to you to set this variable if your terminal can do that. */);
6474 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6475 doc: /* Name of the window system that Emacs uses for the first frame.
6476 The value is a symbol:
6477 nil for a termcap frame (a character-only terminal),
6478 'x' for an Emacs frame that is really an X window,
6479 'w32' for an Emacs frame that is a window on MS-Windows display,
6480 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6481 'pc' for a direct-write MS-DOS frame.
6483 Use of this variable as a boolean is deprecated. Instead,
6484 use `display-graphic-p' or any of the other `display-*-p'
6485 predicates which report frame's specific UI-related capabilities. */);
6487 DEFVAR_KBOARD ("window-system", Vwindow_system,
6488 doc: /* Name of window system through which the selected frame is displayed.
6489 The value is a symbol:
6490 nil for a termcap frame (a character-only terminal),
6491 'x' for an Emacs frame that is really an X window,
6492 'w32' for an Emacs frame that is a window on MS-Windows display,
6493 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6494 'pc' for a direct-write MS-DOS frame.
6496 Use of this variable as a boolean is deprecated. Instead,
6497 use `display-graphic-p' or any of the other `display-*-p'
6498 predicates which report frame's specific UI-related capabilities. */);
6500 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6501 doc: /* The version number of the window system in use.
6502 For X windows, this is 11. */);
6504 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6505 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6507 DEFVAR_LISP ("glyph-table", Vglyph_table,
6508 doc: /* Table defining how to output a glyph code to the frame.
6509 If not nil, this is a vector indexed by glyph code to define the glyph.
6510 Each element can be:
6511 integer: a glyph code which this glyph is an alias for.
6512 string: output this glyph using that string (not impl. in X windows).
6513 nil: this glyph mod 524288 is the code of a character to output,
6514 and this glyph / 524288 is the face number (see `face-id') to use
6515 while outputting it. */);
6516 Vglyph_table = Qnil;
6518 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6519 doc: /* Display table to use for buffers that specify none.
6520 See `buffer-display-table' for more information. */);
6521 Vstandard_display_table = Qnil;
6523 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6524 doc: /* *Non-nil means display update isn't paused when input is detected. */);
6525 redisplay_dont_pause = 1;
6527 #if PERIODIC_PREEMPTION_CHECKING
6528 DEFVAR_LISP ("redisplay-preemption-period", Vredisplay_preemption_period,
6529 doc: /* *The period in seconds between checking for input during redisplay.
6530 If input is detected, redisplay is pre-empted, and the input is processed.
6531 If nil, never pre-empt redisplay. */);
6532 Vredisplay_preemption_period = make_float (0.10);
6533 #endif
6535 #ifdef CANNOT_DUMP
6536 if (noninteractive)
6537 #endif
6539 Vinitial_window_system = Qnil;
6540 Vwindow_system_version = Qnil;