Fix bug #15064 with assertion violation due to mouse face.
[emacs.git] / src / dispnew.c
blob519659a104c278f009cea7e8eb594bbe82ed7a67
1 /* Updating of data structures for redisplay.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #define DISPEXTERN_INLINE EXTERN_INLINE
25 #include "sysstdio.h"
26 #include <unistd.h>
28 #include "lisp.h"
29 #include "termchar.h"
30 /* cm.h must come after dispextern.h on Windows. */
31 #include "dispextern.h"
32 #include "cm.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "keyboard.h"
36 #include "frame.h"
37 #include "termhooks.h"
38 #include "window.h"
39 #include "commands.h"
40 #include "disptab.h"
41 #include "indent.h"
42 #include "intervals.h"
43 #include "blockinput.h"
44 #include "process.h"
46 #include "syssignal.h"
48 #ifdef HAVE_WINDOW_SYSTEM
49 #include TERM_HEADER
50 #endif /* HAVE_WINDOW_SYSTEM */
52 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
54 #include "systime.h"
55 #include <errno.h>
57 #include <fpending.h>
59 #if defined (HAVE_TERM_H) && defined (GNU_LINUX)
60 #include <term.h> /* for tgetent */
61 #endif
63 #ifdef WINDOWSNT
64 #include "w32.h"
65 #endif
67 /* Structure to pass dimensions around. Used for character bounding
68 boxes, glyph matrix dimensions and alike. */
70 struct dim
72 int width;
73 int height;
77 /* Function prototypes. */
79 static void update_frame_line (struct frame *, int);
80 static int required_matrix_height (struct window *);
81 static int required_matrix_width (struct window *);
82 static void adjust_frame_glyphs (struct frame *);
83 static void change_frame_size_1 (struct frame *, int, int, bool, bool, bool);
84 static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
85 static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
86 static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
87 struct window *);
88 static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
89 struct window *);
90 static void adjust_decode_mode_spec_buffer (struct frame *);
91 static void fill_up_glyph_row_with_spaces (struct glyph_row *);
92 static void clear_window_matrices (struct window *, bool);
93 static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
94 static int scrolling_window (struct window *, bool);
95 static bool update_window_line (struct window *, int, bool *);
96 static void mirror_make_current (struct window *, int);
97 #ifdef GLYPH_DEBUG
98 static void check_matrix_pointers (struct glyph_matrix *,
99 struct glyph_matrix *);
100 #endif
101 static void mirror_line_dance (struct window *, int, int, int *, char *);
102 static bool update_window_tree (struct window *, bool);
103 static bool update_window (struct window *, bool);
104 static bool update_frame_1 (struct frame *, bool, bool);
105 static bool scrolling (struct frame *);
106 static void set_window_cursor_after_update (struct window *);
107 static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
108 static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
110 /* True upon entry to redisplay means do not assume anything about
111 current contents of actual terminal frame; clear and redraw it. */
113 bool frame_garbaged;
115 /* True means last display completed. False means it was preempted. */
117 bool display_completed;
119 Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
122 /* The currently selected frame. In a single-frame version, this
123 variable always equals the_only_frame. */
125 Lisp_Object selected_frame;
127 /* A frame which is not just a mini-buffer, or 0 if there are no such
128 frames. This is usually the most recent such frame that was
129 selected. In a single-frame version, this variable always holds
130 the address of the_only_frame. */
132 struct frame *last_nonminibuf_frame;
134 /* True means SIGWINCH happened when not safe. */
136 static bool delayed_size_change;
138 /* Glyph row updated in update_window_line, and area that is updated. */
140 struct glyph_row *updated_row;
141 int updated_area;
143 /* A glyph for a space. */
145 struct glyph space_glyph;
147 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
149 /* Counts of allocated structures. These counts serve to diagnose
150 memory leaks and double frees. */
152 static int glyph_matrix_count;
153 static int glyph_pool_count;
155 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
157 /* If non-null, the frame whose frame matrices are manipulated. If
158 null, window matrices are worked on. */
160 static struct frame *frame_matrix_frame;
162 /* True means that fonts have been loaded since the last glyph
163 matrix adjustments. Redisplay must stop, and glyph matrices must
164 be adjusted when this flag becomes true during display. The
165 reason fonts can be loaded so late is that fonts of fontsets are
166 loaded on demand. Another reason is that a line contains many
167 characters displayed by zero width or very narrow glyphs of
168 variable-width fonts. */
170 bool fonts_changed_p;
172 /* Convert vpos and hpos from frame to window and vice versa.
173 This may only be used for terminal frames. */
175 #ifdef GLYPH_DEBUG
177 static int window_to_frame_vpos (struct window *, int);
178 static int window_to_frame_hpos (struct window *, int);
179 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
180 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
182 /* One element of the ring buffer containing redisplay history
183 information. */
185 struct redisplay_history
187 char trace[512 + 100];
190 /* The size of the history buffer. */
192 #define REDISPLAY_HISTORY_SIZE 30
194 /* The redisplay history buffer. */
196 static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
198 /* Next free entry in redisplay_history. */
200 static int history_idx;
202 /* A tick that's incremented each time something is added to the
203 history. */
205 static uprintmax_t history_tick;
207 /* Add to the redisplay history how window W has been displayed.
208 MSG is a trace containing the information how W's glyph matrix
209 has been constructed. PAUSED_P means that the update
210 has been interrupted for pending input. */
212 static void
213 add_window_display_history (struct window *w, const char *msg, bool paused_p)
215 char *buf;
216 void *ptr = w;
218 if (history_idx >= REDISPLAY_HISTORY_SIZE)
219 history_idx = 0;
220 buf = redisplay_history[history_idx].trace;
221 ++history_idx;
223 snprintf (buf, sizeof redisplay_history[0].trace,
224 "%"pMu": window %p (`%s')%s\n%s",
225 history_tick++,
226 ptr,
227 ((BUFFERP (w->contents)
228 && STRINGP (BVAR (XBUFFER (w->contents), name)))
229 ? SSDATA (BVAR (XBUFFER (w->contents), name))
230 : "???"),
231 paused_p ? " ***paused***" : "",
232 msg);
236 /* Add to the redisplay history that frame F has been displayed.
237 PAUSED_P means that the update has been interrupted for
238 pending input. */
240 static void
241 add_frame_display_history (struct frame *f, bool paused_p)
243 char *buf;
244 void *ptr = f;
246 if (history_idx >= REDISPLAY_HISTORY_SIZE)
247 history_idx = 0;
248 buf = redisplay_history[history_idx].trace;
249 ++history_idx;
251 sprintf (buf, "%"pMu": update frame %p%s",
252 history_tick++,
253 ptr, paused_p ? " ***paused***" : "");
257 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
258 Sdump_redisplay_history, 0, 0, "",
259 doc: /* Dump redisplay history to stderr. */)
260 (void)
262 int i;
264 for (i = history_idx - 1; i != history_idx; --i)
266 if (i < 0)
267 i = REDISPLAY_HISTORY_SIZE - 1;
268 fprintf (stderr, "%s\n", redisplay_history[i].trace);
271 return Qnil;
275 #else /* not GLYPH_DEBUG */
277 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
278 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
280 #endif /* GLYPH_DEBUG */
283 #if (defined PROFILING \
284 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
285 && !HAVE___EXECUTABLE_START)
286 /* This function comes first in the Emacs executable and is used only
287 to estimate the text start for profiling. */
288 void
289 __executable_start (void)
291 emacs_abort ();
293 #endif
295 /***********************************************************************
296 Glyph Matrices
297 ***********************************************************************/
299 /* Allocate and return a glyph_matrix structure. POOL is the glyph
300 pool from which memory for the matrix should be allocated, or null
301 for window-based redisplay where no glyph pools are used. The
302 member `pool' of the glyph matrix structure returned is set to
303 POOL, the structure is otherwise zeroed. */
305 static struct glyph_matrix *
306 new_glyph_matrix (struct glyph_pool *pool)
308 struct glyph_matrix *result = xzalloc (sizeof *result);
310 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
311 /* Increment number of allocated matrices. This count is used
312 to detect memory leaks. */
313 ++glyph_matrix_count;
314 #endif
316 /* Set pool and return. */
317 result->pool = pool;
318 return result;
322 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
324 If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
325 glyph_matrix_count is decremented when a matrix is freed. If the count
326 gets negative, more structures were freed than allocated, i.e. one matrix
327 was freed more than once or a bogus pointer was passed to this function.
329 If MATRIX->pool is null, this means that the matrix manages its own
330 glyph memory---this is done for matrices on X frames. Freeing the
331 matrix also frees the glyph memory in this case. */
333 static void
334 free_glyph_matrix (struct glyph_matrix *matrix)
336 if (matrix)
338 int i;
340 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
341 /* Detect the case that more matrices are freed than were
342 allocated. */
343 --glyph_matrix_count;
344 eassert (glyph_matrix_count >= 0);
345 #endif
347 /* Free glyph memory if MATRIX owns it. */
348 if (matrix->pool == NULL)
349 for (i = 0; i < matrix->rows_allocated; ++i)
350 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
352 /* Free row structures and the matrix itself. */
353 xfree (matrix->rows);
354 xfree (matrix);
359 /* Return the number of glyphs to reserve for a marginal area of
360 window W. TOTAL_GLYPHS is the number of glyphs in a complete
361 display line of window W. MARGIN gives the width of the marginal
362 area in canonical character units. MARGIN should be an integer
363 or a float. */
365 static int
366 margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
368 int n;
370 if (NUMBERP (margin))
372 int width = w->total_cols;
373 double d = max (0, XFLOATINT (margin));
374 d = min (width / 2 - 1, d);
375 n = (int) ((double) total_glyphs / width * d);
377 else
378 n = 0;
380 return n;
383 /* Return true if ROW's hash value is correct.
384 Optimized away if ENABLE_CHECKING is not defined. */
386 static bool
387 verify_row_hash (struct glyph_row *row)
389 return row->hash == row_hash (row);
392 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
393 window sizes.
395 W is null if the function is called for a frame glyph matrix.
396 Otherwise it is the window MATRIX is a member of. X and Y are the
397 indices of the first column and row of MATRIX within the frame
398 matrix, if such a matrix exists. They are zero for purely
399 window-based redisplay. DIM is the needed size of the matrix.
401 In window-based redisplay, where no frame matrices exist, glyph
402 matrices manage their own glyph storage. Otherwise, they allocate
403 storage from a common frame glyph pool which can be found in
404 MATRIX->pool.
406 The reason for this memory management strategy is to avoid complete
407 frame redraws if possible. When we allocate from a common pool, a
408 change of the location or size of a sub-matrix within the pool
409 requires a complete redisplay of the frame because we cannot easily
410 make sure that the current matrices of all windows still agree with
411 what is displayed on the screen. While this is usually fast, it
412 leads to screen flickering. */
414 static void
415 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
417 int i;
418 int new_rows;
419 bool marginal_areas_changed_p = 0;
420 bool header_line_changed_p = 0;
421 bool header_line_p = 0;
422 int left = -1, right = -1;
423 int window_width = -1, window_height = -1;
425 /* See if W had a header line that has disappeared now, or vice versa.
426 Get W's size. */
427 if (w)
429 window_box (w, -1, 0, 0, &window_width, &window_height);
431 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
432 header_line_changed_p = header_line_p != matrix->header_line_p;
434 matrix->header_line_p = header_line_p;
436 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
437 Do nothing if MATRIX' size, position, vscroll, and marginal areas
438 haven't changed. This optimization is important because preserving
439 the matrix means preventing redisplay. */
440 if (matrix->pool == NULL)
442 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
443 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
444 eassert (left >= 0 && right >= 0);
445 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
446 || right != matrix->right_margin_glyphs);
448 if (!marginal_areas_changed_p
449 && !fonts_changed_p
450 && !header_line_changed_p
451 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
452 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
453 && matrix->window_height == window_height
454 && matrix->window_vscroll == w->vscroll
455 && matrix->window_width == window_width)
456 return;
459 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
460 if (matrix->rows_allocated < dim.height)
462 int old_alloc = matrix->rows_allocated;
463 new_rows = dim.height - matrix->rows_allocated;
464 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
465 new_rows, INT_MAX, sizeof *matrix->rows);
466 memset (matrix->rows + old_alloc, 0,
467 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
469 else
470 new_rows = 0;
472 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
473 on a frame not using window-based redisplay. Set up pointers for
474 each row into the glyph pool. */
475 if (matrix->pool)
477 eassert (matrix->pool->glyphs);
479 if (w)
481 left = margin_glyphs_to_reserve (w, dim.width,
482 w->left_margin_cols);
483 right = margin_glyphs_to_reserve (w, dim.width,
484 w->right_margin_cols);
486 else
487 left = right = 0;
489 for (i = 0; i < dim.height; ++i)
491 struct glyph_row *row = &matrix->rows[i];
493 row->glyphs[LEFT_MARGIN_AREA]
494 = (matrix->pool->glyphs
495 + (y + i) * matrix->pool->ncolumns
496 + x);
498 if (w == NULL
499 || row == matrix->rows + dim.height - 1
500 || (row == matrix->rows && matrix->header_line_p))
502 row->glyphs[TEXT_AREA]
503 = row->glyphs[LEFT_MARGIN_AREA];
504 row->glyphs[RIGHT_MARGIN_AREA]
505 = row->glyphs[TEXT_AREA] + dim.width;
506 row->glyphs[LAST_AREA]
507 = row->glyphs[RIGHT_MARGIN_AREA];
509 else
511 row->glyphs[TEXT_AREA]
512 = row->glyphs[LEFT_MARGIN_AREA] + left;
513 row->glyphs[RIGHT_MARGIN_AREA]
514 = row->glyphs[TEXT_AREA] + dim.width - left - right;
515 row->glyphs[LAST_AREA]
516 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
520 matrix->left_margin_glyphs = left;
521 matrix->right_margin_glyphs = right;
523 else
525 /* If MATRIX->pool is null, MATRIX is responsible for managing
526 its own memory. It is a window matrix for window-based redisplay.
527 Allocate glyph memory from the heap. */
528 if (dim.width > matrix->matrix_w
529 || new_rows
530 || header_line_changed_p
531 || marginal_areas_changed_p)
533 struct glyph_row *row = matrix->rows;
534 struct glyph_row *end = row + matrix->rows_allocated;
536 while (row < end)
538 row->glyphs[LEFT_MARGIN_AREA]
539 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
540 dim.width, sizeof (struct glyph));
542 /* The mode line never has marginal areas. */
543 if (row == matrix->rows + dim.height - 1
544 || (row == matrix->rows && matrix->header_line_p))
546 row->glyphs[TEXT_AREA]
547 = row->glyphs[LEFT_MARGIN_AREA];
548 row->glyphs[RIGHT_MARGIN_AREA]
549 = row->glyphs[TEXT_AREA] + dim.width;
550 row->glyphs[LAST_AREA]
551 = row->glyphs[RIGHT_MARGIN_AREA];
553 else
555 row->glyphs[TEXT_AREA]
556 = row->glyphs[LEFT_MARGIN_AREA] + left;
557 row->glyphs[RIGHT_MARGIN_AREA]
558 = row->glyphs[TEXT_AREA] + dim.width - left - right;
559 row->glyphs[LAST_AREA]
560 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
562 ++row;
566 eassert (left >= 0 && right >= 0);
567 matrix->left_margin_glyphs = left;
568 matrix->right_margin_glyphs = right;
571 /* Number of rows to be used by MATRIX. */
572 matrix->nrows = dim.height;
573 eassert (matrix->nrows >= 0);
575 if (w)
577 if (matrix == w->current_matrix)
579 /* Mark rows in a current matrix of a window as not having
580 valid contents. It's important to not do this for
581 desired matrices. When Emacs starts, it may already be
582 building desired matrices when this function runs. */
583 if (window_width < 0)
584 window_width = window_box_width (w, -1);
586 /* Optimize the case that only the height has changed (C-x 2,
587 upper window). Invalidate all rows that are no longer part
588 of the window. */
589 if (!marginal_areas_changed_p
590 && !header_line_changed_p
591 && new_rows == 0
592 && dim.width == matrix->matrix_w
593 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
594 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
595 && matrix->window_width == window_width)
597 /* Find the last row in the window. */
598 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
599 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
601 ++i;
602 break;
605 /* Window end is invalid, if inside of the rows that
606 are invalidated below. */
607 if (INTEGERP (w->window_end_vpos)
608 && XFASTINT (w->window_end_vpos) >= i)
609 w->window_end_valid = 0;
611 while (i < matrix->nrows)
612 matrix->rows[i++].enabled_p = 0;
614 else
616 for (i = 0; i < matrix->nrows; ++i)
617 matrix->rows[i].enabled_p = 0;
620 else if (matrix == w->desired_matrix)
622 /* Rows in desired matrices always have to be cleared;
623 redisplay expects this is the case when it runs, so it
624 had better be the case when we adjust matrices between
625 redisplays. */
626 for (i = 0; i < matrix->nrows; ++i)
627 matrix->rows[i].enabled_p = 0;
632 /* Remember last values to be able to optimize frame redraws. */
633 matrix->matrix_x = x;
634 matrix->matrix_y = y;
635 matrix->matrix_w = dim.width;
636 matrix->matrix_h = dim.height;
638 /* Record the top y location and height of W at the time the matrix
639 was last adjusted. This is used to optimize redisplay above. */
640 if (w)
642 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
643 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
644 matrix->window_height = window_height;
645 matrix->window_width = window_width;
646 matrix->window_vscroll = w->vscroll;
651 /* Reverse the contents of rows in MATRIX between START and END. The
652 contents of the row at END - 1 end up at START, END - 2 at START +
653 1 etc. This is part of the implementation of rotate_matrix (see
654 below). */
656 static void
657 reverse_rows (struct glyph_matrix *matrix, int start, int end)
659 int i, j;
661 for (i = start, j = end - 1; i < j; ++i, --j)
663 /* Non-ISO HP/UX compiler doesn't like auto struct
664 initialization. */
665 struct glyph_row temp;
666 temp = matrix->rows[i];
667 matrix->rows[i] = matrix->rows[j];
668 matrix->rows[j] = temp;
673 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
674 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
675 indices. (Note: this does not copy glyphs, only glyph pointers in
676 row structures are moved around).
678 The algorithm used for rotating the vector was, I believe, first
679 described by Kernighan. See the vector R as consisting of two
680 sub-vectors AB, where A has length BY for BY >= 0. The result
681 after rotating is then BA. Reverse both sub-vectors to get ArBr
682 and reverse the result to get (ArBr)r which is BA. Similar for
683 rotating right. */
685 void
686 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
688 if (by < 0)
690 /* Up (rotate left, i.e. towards lower indices). */
691 by = -by;
692 reverse_rows (matrix, first, first + by);
693 reverse_rows (matrix, first + by, last);
694 reverse_rows (matrix, first, last);
696 else if (by > 0)
698 /* Down (rotate right, i.e. towards higher indices). */
699 reverse_rows (matrix, last - by, last);
700 reverse_rows (matrix, first, last - by);
701 reverse_rows (matrix, first, last);
706 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
707 with indices START <= index < END. Increment positions by DELTA/
708 DELTA_BYTES. */
710 void
711 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
712 ptrdiff_t delta, ptrdiff_t delta_bytes)
714 /* Check that START and END are reasonable values. */
715 eassert (start >= 0 && start <= matrix->nrows);
716 eassert (end >= 0 && end <= matrix->nrows);
717 eassert (start <= end);
719 for (; start < end; ++start)
720 increment_row_positions (matrix->rows + start, delta, delta_bytes);
724 /* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
725 START and END are the row indices of the first and last + 1 row to clear. */
727 void
728 clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
730 eassert (start <= end);
731 eassert (start >= 0 && start < matrix->nrows);
732 eassert (end >= 0 && end <= matrix->nrows);
734 for (; start < end; ++start)
735 matrix->rows[start].enabled_p = 0;
739 /* Clear MATRIX.
741 Empty all rows in MATRIX by clearing their enabled_p flags.
742 The function prepare_desired_row will eventually really clear a row
743 when it sees one with a false enabled_p flag.
745 Reset update hints to default values. The only update hint
746 currently present is the flag MATRIX->no_scrolling_p. */
748 void
749 clear_glyph_matrix (struct glyph_matrix *matrix)
751 if (matrix)
753 clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
754 matrix->no_scrolling_p = 0;
759 /* Shift part of the glyph matrix MATRIX of window W up or down.
760 Increment y-positions in glyph rows between START and END by DY,
761 and recompute their visible height. */
763 void
764 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
766 int min_y, max_y;
768 eassert (start <= end);
769 eassert (start >= 0 && start < matrix->nrows);
770 eassert (end >= 0 && end <= matrix->nrows);
772 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
773 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
775 for (; start < end; ++start)
777 struct glyph_row *row = &matrix->rows[start];
779 row->y += dy;
780 row->visible_height = row->height;
782 if (row->y < min_y)
783 row->visible_height -= min_y - row->y;
784 if (row->y + row->height > max_y)
785 row->visible_height -= row->y + row->height - max_y;
786 if (row->fringe_bitmap_periodic_p)
787 row->redraw_fringe_bitmaps_p = 1;
792 /* Mark all rows in current matrices of frame F as invalid. Marking
793 invalid is done by setting enabled_p to zero for all rows in a
794 current matrix. */
796 void
797 clear_current_matrices (register struct frame *f)
799 /* Clear frame current matrix, if we have one. */
800 if (f->current_matrix)
801 clear_glyph_matrix (f->current_matrix);
803 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
804 /* Clear the matrix of the menu bar window, if such a window exists.
805 The menu bar window is currently used to display menus on X when
806 no toolkit support is compiled in. */
807 if (WINDOWP (f->menu_bar_window))
808 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
809 #endif
811 /* Clear the matrix of the tool-bar window, if any. */
812 if (WINDOWP (f->tool_bar_window))
813 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
815 /* Clear current window matrices. */
816 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
817 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
821 /* Clear out all display lines of F for a coming redisplay. */
823 void
824 clear_desired_matrices (register struct frame *f)
826 if (f->desired_matrix)
827 clear_glyph_matrix (f->desired_matrix);
829 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
830 if (WINDOWP (f->menu_bar_window))
831 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
832 #endif
834 if (WINDOWP (f->tool_bar_window))
835 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
837 /* Do it for window matrices. */
838 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
839 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
843 /* Clear matrices in window tree rooted in W. If DESIRED_P,
844 clear desired matrices, otherwise clear current matrices. */
846 static void
847 clear_window_matrices (struct window *w, bool desired_p)
849 while (w)
851 if (WINDOWP (w->contents))
852 clear_window_matrices (XWINDOW (w->contents), desired_p);
853 else
855 if (desired_p)
856 clear_glyph_matrix (w->desired_matrix);
857 else
859 clear_glyph_matrix (w->current_matrix);
860 w->window_end_valid = 0;
864 w = NILP (w->next) ? 0 : XWINDOW (w->next);
870 /***********************************************************************
871 Glyph Rows
873 See dispextern.h for an overall explanation of glyph rows.
874 ***********************************************************************/
876 /* Clear glyph row ROW. Do it in a way that makes it robust against
877 changes in the glyph_row structure, i.e. addition or removal of
878 structure members. */
880 static struct glyph_row null_row;
882 void
883 clear_glyph_row (struct glyph_row *row)
885 struct glyph *p[1 + LAST_AREA];
887 /* Save pointers. */
888 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
889 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
890 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
891 p[LAST_AREA] = row->glyphs[LAST_AREA];
893 /* Clear. */
894 *row = null_row;
896 /* Restore pointers. */
897 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
898 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
899 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
900 row->glyphs[LAST_AREA] = p[LAST_AREA];
902 #if 0 /* At some point, some bit-fields of struct glyph were not set,
903 which made glyphs unequal when compared with GLYPH_EQUAL_P.
904 Redisplay outputs such glyphs, and flickering effects were
905 the result. This also depended on the contents of memory
906 returned by xmalloc. If flickering happens again, activate
907 the code below. If the flickering is gone with that, chances
908 are that the flickering has the same reason as here. */
909 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
910 #endif
914 /* Make ROW an empty, enabled row of canonical character height,
915 in window W starting at y-position Y. */
917 void
918 blank_row (struct window *w, struct glyph_row *row, int y)
920 int min_y, max_y;
922 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
923 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
925 clear_glyph_row (row);
926 row->y = y;
927 row->ascent = row->phys_ascent = 0;
928 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
929 row->visible_height = row->height;
931 if (row->y < min_y)
932 row->visible_height -= min_y - row->y;
933 if (row->y + row->height > max_y)
934 row->visible_height -= row->y + row->height - max_y;
936 row->enabled_p = 1;
940 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
941 are the amounts by which to change positions. Note that the first
942 glyph of the text area of a row can have a buffer position even if
943 the used count of the text area is zero. Such rows display line
944 ends. */
946 static void
947 increment_row_positions (struct glyph_row *row,
948 ptrdiff_t delta, ptrdiff_t delta_bytes)
950 int area, i;
952 /* Increment start and end positions. */
953 MATRIX_ROW_START_CHARPOS (row) += delta;
954 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
955 MATRIX_ROW_END_CHARPOS (row) += delta;
956 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
957 CHARPOS (row->start.pos) += delta;
958 BYTEPOS (row->start.pos) += delta_bytes;
959 CHARPOS (row->end.pos) += delta;
960 BYTEPOS (row->end.pos) += delta_bytes;
962 if (!row->enabled_p)
963 return;
965 /* Increment positions in glyphs. */
966 for (area = 0; area < LAST_AREA; ++area)
967 for (i = 0; i < row->used[area]; ++i)
968 if (BUFFERP (row->glyphs[area][i].object)
969 && row->glyphs[area][i].charpos > 0)
970 row->glyphs[area][i].charpos += delta;
972 /* Capture the case of rows displaying a line end. */
973 if (row->used[TEXT_AREA] == 0
974 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
975 row->glyphs[TEXT_AREA]->charpos += delta;
979 #if 0
980 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
981 contents, i.e. glyph structure contents are exchanged between A and
982 B without changing glyph pointers in A and B. */
984 static void
985 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
987 int area;
989 for (area = 0; area < LAST_AREA; ++area)
991 /* Number of glyphs to swap. */
992 int max_used = max (a->used[area], b->used[area]);
994 /* Start of glyphs in area of row A. */
995 struct glyph *glyph_a = a->glyphs[area];
997 /* End + 1 of glyphs in area of row A. */
998 struct glyph *glyph_a_end = a->glyphs[max_used];
1000 /* Start of glyphs in area of row B. */
1001 struct glyph *glyph_b = b->glyphs[area];
1003 while (glyph_a < glyph_a_end)
1005 /* Non-ISO HP/UX compiler doesn't like auto struct
1006 initialization. */
1007 struct glyph temp;
1008 temp = *glyph_a;
1009 *glyph_a = *glyph_b;
1010 *glyph_b = temp;
1011 ++glyph_a;
1012 ++glyph_b;
1017 #endif /* 0 */
1019 /* Exchange pointers to glyph memory between glyph rows A and B. Also
1020 exchange the used[] array and the hash values of the rows, because
1021 these should all go together for the row's hash value to be
1022 correct. */
1024 static void
1025 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1027 int i;
1028 unsigned hash_tem = a->hash;
1030 for (i = 0; i < LAST_AREA + 1; ++i)
1032 struct glyph *temp = a->glyphs[i];
1034 a->glyphs[i] = b->glyphs[i];
1035 b->glyphs[i] = temp;
1036 if (i < LAST_AREA)
1038 short used_tem = a->used[i];
1040 a->used[i] = b->used[i];
1041 b->used[i] = used_tem;
1044 a->hash = b->hash;
1045 b->hash = hash_tem;
1049 /* Copy glyph row structure FROM to glyph row structure TO, except
1050 that glyph pointers, the `used' counts, and the hash values in the
1051 structures are left unchanged. */
1053 static void
1054 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1056 struct glyph *pointers[1 + LAST_AREA];
1057 short used[LAST_AREA];
1058 unsigned hashval;
1060 /* Save glyph pointers of TO. */
1061 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1062 memcpy (used, to->used, sizeof to->used);
1063 hashval = to->hash;
1065 /* Do a structure assignment. */
1066 *to = *from;
1068 /* Restore original pointers of TO. */
1069 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1070 memcpy (to->used, used, sizeof to->used);
1071 to->hash = hashval;
1075 /* Assign glyph row FROM to glyph row TO. This works like a structure
1076 assignment TO = FROM, except that glyph pointers are not copied but
1077 exchanged between TO and FROM. Pointers must be exchanged to avoid
1078 a memory leak. */
1080 static void
1081 assign_row (struct glyph_row *to, struct glyph_row *from)
1083 swap_glyph_pointers (to, from);
1084 copy_row_except_pointers (to, from);
1088 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1089 a row in a window matrix, is a slice of the glyph memory of the
1090 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1091 is true if the glyph memory of WINDOW_ROW is part of the glyph
1092 memory of FRAME_ROW. */
1094 #ifdef GLYPH_DEBUG
1096 static bool
1097 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1099 struct glyph *window_glyph_start = window_row->glyphs[0];
1100 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1101 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1103 return (frame_glyph_start <= window_glyph_start
1104 && window_glyph_start < frame_glyph_end);
1107 #endif /* GLYPH_DEBUG */
1109 #if 0
1111 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1112 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1113 in WINDOW_MATRIX is found satisfying the condition. */
1115 static struct glyph_row *
1116 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1117 struct glyph_matrix *frame_matrix, int row)
1119 int i;
1121 eassert (row >= 0 && row < frame_matrix->nrows);
1123 for (i = 0; i < window_matrix->nrows; ++i)
1124 if (glyph_row_slice_p (window_matrix->rows + i,
1125 frame_matrix->rows + row))
1126 break;
1128 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1131 #endif /* 0 */
1133 /* Prepare ROW for display. Desired rows are cleared lazily,
1134 i.e. they are only marked as to be cleared by setting their
1135 enabled_p flag to zero. When a row is to be displayed, a prior
1136 call to this function really clears it. */
1138 void
1139 prepare_desired_row (struct glyph_row *row)
1141 if (!row->enabled_p)
1143 bool rp = row->reversed_p;
1145 clear_glyph_row (row);
1146 row->enabled_p = 1;
1147 row->reversed_p = rp;
1152 /* Return a hash code for glyph row ROW. */
1154 static int
1155 line_hash_code (struct glyph_row *row)
1157 int hash = 0;
1159 if (row->enabled_p)
1161 struct glyph *glyph = row->glyphs[TEXT_AREA];
1162 struct glyph *end = glyph + row->used[TEXT_AREA];
1164 while (glyph < end)
1166 int c = glyph->u.ch;
1167 int face_id = glyph->face_id;
1168 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1169 c -= SPACEGLYPH;
1170 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1171 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1172 ++glyph;
1175 if (hash == 0)
1176 hash = 1;
1179 return hash;
1183 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1184 the number of characters in the line. If must_write_spaces is
1185 zero, leading and trailing spaces are ignored. */
1187 static int
1188 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1190 struct glyph_row *row = matrix->rows + vpos;
1191 struct glyph *beg = row->glyphs[TEXT_AREA];
1192 struct glyph *end = beg + row->used[TEXT_AREA];
1193 int len;
1194 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1195 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1197 /* Ignore trailing and leading spaces if we can. */
1198 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1200 /* Skip from the end over trailing spaces. */
1201 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1202 --end;
1204 /* All blank line. */
1205 if (end == beg)
1206 return 0;
1208 /* Skip over leading spaces. */
1209 while (CHAR_GLYPH_SPACE_P (*beg))
1210 ++beg;
1213 /* If we don't have a glyph-table, each glyph is one character,
1214 so return the number of glyphs. */
1215 if (glyph_table_base == 0)
1216 len = end - beg;
1217 else
1219 /* Otherwise, scan the glyphs and accumulate their total length
1220 in LEN. */
1221 len = 0;
1222 while (beg < end)
1224 GLYPH g;
1226 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1228 if (GLYPH_INVALID_P (g)
1229 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1230 len += 1;
1231 else
1232 len += GLYPH_LENGTH (glyph_table_base, g);
1234 ++beg;
1238 return len;
1242 /* Return true if the glyph rows A and B have equal contents.
1243 MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too. */
1245 static bool
1246 row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
1248 eassert (verify_row_hash (a));
1249 eassert (verify_row_hash (b));
1251 if (a == b)
1252 return 1;
1253 else if (a->hash != b->hash)
1254 return 0;
1255 else
1257 struct glyph *a_glyph, *b_glyph, *a_end;
1258 int area;
1260 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1261 return 0;
1263 /* Compare glyphs. */
1264 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1266 if (a->used[area] != b->used[area])
1267 return 0;
1269 a_glyph = a->glyphs[area];
1270 a_end = a_glyph + a->used[area];
1271 b_glyph = b->glyphs[area];
1273 while (a_glyph < a_end
1274 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1275 ++a_glyph, ++b_glyph;
1277 if (a_glyph != a_end)
1278 return 0;
1281 if (a->fill_line_p != b->fill_line_p
1282 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1283 || a->left_fringe_bitmap != b->left_fringe_bitmap
1284 || a->left_fringe_face_id != b->left_fringe_face_id
1285 || a->left_fringe_offset != b->left_fringe_offset
1286 || a->right_fringe_bitmap != b->right_fringe_bitmap
1287 || a->right_fringe_face_id != b->right_fringe_face_id
1288 || a->right_fringe_offset != b->right_fringe_offset
1289 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1290 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1291 || a->exact_window_width_line_p != b->exact_window_width_line_p
1292 || a->overlapped_p != b->overlapped_p
1293 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1294 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1295 || a->reversed_p != b->reversed_p
1296 /* Different partially visible characters on left margin. */
1297 || a->x != b->x
1298 /* Different height. */
1299 || a->ascent != b->ascent
1300 || a->phys_ascent != b->phys_ascent
1301 || a->phys_height != b->phys_height
1302 || a->visible_height != b->visible_height)
1303 return 0;
1306 return 1;
1311 /***********************************************************************
1312 Glyph Pool
1314 See dispextern.h for an overall explanation of glyph pools.
1315 ***********************************************************************/
1317 /* Allocate a glyph_pool structure. The structure returned is initialized
1318 with zeros. If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global
1319 variable glyph_pool_count is incremented for each pool allocated. */
1321 static struct glyph_pool *
1322 new_glyph_pool (void)
1324 struct glyph_pool *result = xzalloc (sizeof *result);
1326 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1327 /* For memory leak and double deletion checking. */
1328 ++glyph_pool_count;
1329 #endif
1331 return result;
1335 /* Free a glyph_pool structure POOL. The function may be called with
1336 a null POOL pointer. If GLYPH_DEBUG and ENABLE_CHECKING are in effect,
1337 global variable glyph_pool_count is decremented with every pool structure
1338 freed. If this count gets negative, more structures were freed than
1339 allocated, i.e. one structure must have been freed more than once or
1340 a bogus pointer was passed to free_glyph_pool. */
1342 static void
1343 free_glyph_pool (struct glyph_pool *pool)
1345 if (pool)
1347 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1348 /* More freed than allocated? */
1349 --glyph_pool_count;
1350 eassert (glyph_pool_count >= 0);
1351 #endif
1352 xfree (pool->glyphs);
1353 xfree (pool);
1358 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1359 columns we need. This function never shrinks a pool. The only
1360 case in which this would make sense, would be when a frame's size
1361 is changed from a large value to a smaller one. But, if someone
1362 does it once, we can expect that he will do it again.
1364 Return true if the pool changed in a way which makes
1365 re-adjusting window glyph matrices necessary. */
1367 static bool
1368 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1370 ptrdiff_t needed;
1371 bool changed_p;
1373 changed_p = (pool->glyphs == 0
1374 || matrix_dim.height != pool->nrows
1375 || matrix_dim.width != pool->ncolumns);
1377 /* Enlarge the glyph pool. */
1378 needed = matrix_dim.width;
1379 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1380 memory_full (SIZE_MAX);
1381 needed *= matrix_dim.height;
1382 if (needed > pool->nglyphs)
1384 ptrdiff_t old_nglyphs = pool->nglyphs;
1385 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1386 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1387 memset (pool->glyphs + old_nglyphs, 0,
1388 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1391 /* Remember the number of rows and columns because (a) we use them
1392 to do sanity checks, and (b) the number of columns determines
1393 where rows in the frame matrix start---this must be available to
1394 determine pointers to rows of window sub-matrices. */
1395 pool->nrows = matrix_dim.height;
1396 pool->ncolumns = matrix_dim.width;
1398 return changed_p;
1403 /***********************************************************************
1404 Debug Code
1405 ***********************************************************************/
1407 #ifdef GLYPH_DEBUG
1410 /* Flush standard output. This is sometimes useful to call from the debugger.
1411 XXX Maybe this should be changed to flush the current terminal instead of
1412 stdout.
1415 void flush_stdout (void) EXTERNALLY_VISIBLE;
1417 void
1418 flush_stdout (void)
1420 fflush (stdout);
1424 /* Check that no glyph pointers have been lost in MATRIX. If a
1425 pointer has been lost, e.g. by using a structure assignment between
1426 rows, at least one pointer must occur more than once in the rows of
1427 MATRIX. */
1429 void
1430 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1432 int i, j;
1434 for (i = 0; i < matrix->nrows; ++i)
1435 for (j = 0; j < matrix->nrows; ++j)
1436 eassert (i == j
1437 || (matrix->rows[i].glyphs[TEXT_AREA]
1438 != matrix->rows[j].glyphs[TEXT_AREA]));
1442 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1444 struct glyph_row *
1445 matrix_row (struct glyph_matrix *matrix, int row)
1447 eassert (matrix && matrix->rows);
1448 eassert (row >= 0 && row < matrix->nrows);
1450 /* That's really too slow for normal testing because this function
1451 is called almost everywhere. Although---it's still astonishingly
1452 fast, so it is valuable to have for debugging purposes. */
1453 #if 0
1454 check_matrix_pointer_lossage (matrix);
1455 #endif
1457 return matrix->rows + row;
1461 #if 0 /* This function makes invalid assumptions when text is
1462 partially invisible. But it might come handy for debugging
1463 nevertheless. */
1465 /* Check invariants that must hold for an up to date current matrix of
1466 window W. */
1468 static void
1469 check_matrix_invariants (struct window *w)
1471 struct glyph_matrix *matrix = w->current_matrix;
1472 int yb = window_text_bottom_y (w);
1473 struct glyph_row *row = matrix->rows;
1474 struct glyph_row *last_text_row = NULL;
1475 struct buffer *saved = current_buffer;
1476 struct buffer *buffer = XBUFFER (w->contents);
1477 int c;
1479 /* This can sometimes happen for a fresh window. */
1480 if (matrix->nrows < 2)
1481 return;
1483 set_buffer_temp (buffer);
1485 /* Note: last row is always reserved for the mode line. */
1486 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1487 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1489 struct glyph_row *next = row + 1;
1491 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1492 last_text_row = row;
1494 /* Check that character and byte positions are in sync. */
1495 eassert (MATRIX_ROW_START_BYTEPOS (row)
1496 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1497 eassert (BYTEPOS (row->start.pos)
1498 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1500 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1501 have such a position temporarily in case of a minibuffer
1502 displaying something like `[Sole completion]' at its end. */
1503 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1505 eassert (MATRIX_ROW_END_BYTEPOS (row)
1506 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1507 eassert (BYTEPOS (row->end.pos)
1508 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1511 /* Check that end position of `row' is equal to start position
1512 of next row. */
1513 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1515 eassert (MATRIX_ROW_END_CHARPOS (row)
1516 == MATRIX_ROW_START_CHARPOS (next));
1517 eassert (MATRIX_ROW_END_BYTEPOS (row)
1518 == MATRIX_ROW_START_BYTEPOS (next));
1519 eassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1520 eassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1522 row = next;
1525 eassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1526 eassert (w->desired_matrix->rows != NULL);
1527 set_buffer_temp (saved);
1530 #endif /* 0 */
1532 #endif /* GLYPH_DEBUG */
1536 /**********************************************************************
1537 Allocating/ Adjusting Glyph Matrices
1538 **********************************************************************/
1540 /* Allocate glyph matrices over a window tree for a frame-based
1541 redisplay
1543 X and Y are column/row within the frame glyph matrix where
1544 sub-matrices for the window tree rooted at WINDOW must be
1545 allocated. DIM_ONLY_P means that the caller of this
1546 function is only interested in the result matrix dimension, and
1547 matrix adjustments should not be performed.
1549 The function returns the total width/height of the sub-matrices of
1550 the window tree. If called on a frame root window, the computation
1551 will take the mini-buffer window into account.
1553 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1555 NEW_LEAF_MATRIX set if any window in the tree did not have a
1556 glyph matrices yet, and
1558 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1559 any window in the tree will be changed or have been changed (see
1560 DIM_ONLY_P)
1562 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1563 function.
1565 Windows are arranged into chains of windows on the same level
1566 through the next fields of window structures. Such a level can be
1567 either a sequence of horizontally adjacent windows from left to
1568 right, or a sequence of vertically adjacent windows from top to
1569 bottom. Each window in a horizontal sequence can be either a leaf
1570 window or a vertical sequence; a window in a vertical sequence can
1571 be either a leaf or a horizontal sequence. All windows in a
1572 horizontal sequence have the same height, and all windows in a
1573 vertical sequence have the same width.
1575 This function uses, for historical reasons, a more general
1576 algorithm to determine glyph matrix dimensions that would be
1577 necessary.
1579 The matrix height of a horizontal sequence is determined by the
1580 maximum height of any matrix in the sequence. The matrix width of
1581 a horizontal sequence is computed by adding up matrix widths of
1582 windows in the sequence.
1584 |<------- result width ------->|
1585 +---------+----------+---------+ ---
1586 | | | | |
1587 | | | |
1588 +---------+ | | result height
1589 | +---------+
1590 | | |
1591 +----------+ ---
1593 The matrix width of a vertical sequence is the maximum matrix width
1594 of any window in the sequence. Its height is computed by adding up
1595 matrix heights of windows in the sequence.
1597 |<---- result width -->|
1598 +---------+ ---
1599 | | |
1600 | | |
1601 +---------+--+ |
1602 | | |
1603 | | result height
1605 +------------+---------+ |
1606 | | |
1607 | | |
1608 +------------+---------+ --- */
1610 /* Bit indicating that a new matrix will be allocated or has been
1611 allocated. */
1613 #define NEW_LEAF_MATRIX (1 << 0)
1615 /* Bit indicating that a matrix will or has changed its location or
1616 size. */
1618 #define CHANGED_LEAF_MATRIX (1 << 1)
1620 static struct dim
1621 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1622 bool dim_only_p, int *window_change_flags)
1624 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1625 int x0 = x, y0 = y;
1626 int wmax = 0, hmax = 0;
1627 struct dim total;
1628 struct dim dim;
1629 struct window *w;
1630 bool in_horz_combination_p;
1632 /* What combination is WINDOW part of? Compute this once since the
1633 result is the same for all windows in the `next' chain. The
1634 special case of a root window (parent equal to nil) is treated
1635 like a vertical combination because a root window's `next'
1636 points to the mini-buffer window, if any, which is arranged
1637 vertically below other windows. */
1638 in_horz_combination_p
1639 = (!NILP (XWINDOW (window)->parent)
1640 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (XWINDOW (window)->parent)));
1642 /* For WINDOW and all windows on the same level. */
1645 w = XWINDOW (window);
1647 /* Get the dimension of the window sub-matrix for W, depending
1648 on whether this is a combination or a leaf window. */
1649 if (WINDOWP (w->contents))
1650 dim = allocate_matrices_for_frame_redisplay (w->contents, x, y,
1651 dim_only_p,
1652 window_change_flags);
1653 else
1655 /* If not already done, allocate sub-matrix structures. */
1656 if (w->desired_matrix == NULL)
1658 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1659 w->current_matrix = new_glyph_matrix (f->current_pool);
1660 *window_change_flags |= NEW_LEAF_MATRIX;
1663 /* Width and height MUST be chosen so that there are no
1664 holes in the frame matrix. */
1665 dim.width = required_matrix_width (w);
1666 dim.height = required_matrix_height (w);
1668 /* Will matrix be re-allocated? */
1669 if (x != w->desired_matrix->matrix_x
1670 || y != w->desired_matrix->matrix_y
1671 || dim.width != w->desired_matrix->matrix_w
1672 || dim.height != w->desired_matrix->matrix_h
1673 || (margin_glyphs_to_reserve (w, dim.width,
1674 w->left_margin_cols)
1675 != w->desired_matrix->left_margin_glyphs)
1676 || (margin_glyphs_to_reserve (w, dim.width,
1677 w->right_margin_cols)
1678 != w->desired_matrix->right_margin_glyphs))
1679 *window_change_flags |= CHANGED_LEAF_MATRIX;
1681 /* Actually change matrices, if allowed. Do not consider
1682 CHANGED_LEAF_MATRIX computed above here because the pool
1683 may have been changed which we don't now here. We trust
1684 that we only will be called with DIM_ONLY_P when
1685 necessary. */
1686 if (!dim_only_p)
1688 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1689 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1693 /* If we are part of a horizontal combination, advance x for
1694 windows to the right of W; otherwise advance y for windows
1695 below W. */
1696 if (in_horz_combination_p)
1697 x += dim.width;
1698 else
1699 y += dim.height;
1701 /* Remember maximum glyph matrix dimensions. */
1702 wmax = max (wmax, dim.width);
1703 hmax = max (hmax, dim.height);
1705 /* Next window on same level. */
1706 window = w->next;
1708 while (!NILP (window));
1710 /* Set `total' to the total glyph matrix dimension of this window
1711 level. In a vertical combination, the width is the width of the
1712 widest window; the height is the y we finally reached, corrected
1713 by the y we started with. In a horizontal combination, the total
1714 height is the height of the tallest window, and the width is the
1715 x we finally reached, corrected by the x we started with. */
1716 if (in_horz_combination_p)
1718 total.width = x - x0;
1719 total.height = hmax;
1721 else
1723 total.width = wmax;
1724 total.height = y - y0;
1727 return total;
1731 /* Return the required height of glyph matrices for window W. */
1733 static int
1734 required_matrix_height (struct window *w)
1736 #ifdef HAVE_WINDOW_SYSTEM
1737 struct frame *f = XFRAME (w->frame);
1739 if (FRAME_WINDOW_P (f))
1741 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1742 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1743 return (((window_pixel_height + ch_height - 1)
1744 / ch_height) * w->nrows_scale_factor
1745 /* One partially visible line at the top and
1746 bottom of the window. */
1748 /* 2 for header and mode line. */
1749 + 2);
1751 #endif /* HAVE_WINDOW_SYSTEM */
1753 return WINDOW_TOTAL_LINES (w);
1757 /* Return the required width of glyph matrices for window W. */
1759 static int
1760 required_matrix_width (struct window *w)
1762 #ifdef HAVE_WINDOW_SYSTEM
1763 struct frame *f = XFRAME (w->frame);
1764 if (FRAME_WINDOW_P (f))
1766 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1767 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1769 /* Compute number of glyphs needed in a glyph row. */
1770 return (((window_pixel_width + ch_width - 1)
1771 / ch_width) * w->ncols_scale_factor
1772 /* 2 partially visible columns in the text area. */
1774 /* One partially visible column at the right
1775 edge of each marginal area. */
1776 + 1 + 1);
1778 #endif /* HAVE_WINDOW_SYSTEM */
1780 return w->total_cols;
1784 /* Allocate window matrices for window-based redisplay. W is the
1785 window whose matrices must be allocated/reallocated. */
1787 static void
1788 allocate_matrices_for_window_redisplay (struct window *w)
1790 while (w)
1792 if (WINDOWP (w->contents))
1793 allocate_matrices_for_window_redisplay (XWINDOW (w->contents));
1794 else
1796 /* W is a leaf window. */
1797 struct dim dim;
1799 /* If matrices are not yet allocated, allocate them now. */
1800 if (w->desired_matrix == NULL)
1802 w->desired_matrix = new_glyph_matrix (NULL);
1803 w->current_matrix = new_glyph_matrix (NULL);
1806 dim.width = required_matrix_width (w);
1807 dim.height = required_matrix_height (w);
1808 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1809 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1812 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1817 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1818 do it for all frames; otherwise do it just for the given frame.
1819 This function must be called when a new frame is created, its size
1820 changes, or its window configuration changes. */
1822 void
1823 adjust_glyphs (struct frame *f)
1825 /* Block input so that expose events and other events that access
1826 glyph matrices are not processed while we are changing them. */
1827 block_input ();
1829 if (f)
1830 adjust_frame_glyphs (f);
1831 else
1833 Lisp_Object tail, lisp_frame;
1835 FOR_EACH_FRAME (tail, lisp_frame)
1836 adjust_frame_glyphs (XFRAME (lisp_frame));
1839 unblock_input ();
1842 /* Allocate/reallocate glyph matrices of a single frame F. */
1844 static void
1845 adjust_frame_glyphs (struct frame *f)
1847 if (FRAME_WINDOW_P (f))
1848 adjust_frame_glyphs_for_window_redisplay (f);
1849 else
1850 adjust_frame_glyphs_for_frame_redisplay (f);
1852 /* Don't forget the buffer for decode_mode_spec. */
1853 adjust_decode_mode_spec_buffer (f);
1855 f->glyphs_initialized_p = 1;
1858 /* Return true if any window in the tree has nonzero window margins. See
1859 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1860 static bool
1861 showing_window_margins_p (struct window *w)
1863 while (w)
1865 if (WINDOWP (w->contents))
1867 if (showing_window_margins_p (XWINDOW (w->contents)))
1868 return 1;
1870 else if (!NILP (w->left_margin_cols) || !NILP (w->right_margin_cols))
1871 return 1;
1873 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1875 return 0;
1879 /* In the window tree with root W, build current matrices of leaf
1880 windows from the frame's current matrix. */
1882 static void
1883 fake_current_matrices (Lisp_Object window)
1885 struct window *w;
1887 for (; !NILP (window); window = w->next)
1889 w = XWINDOW (window);
1891 if (WINDOWP (w->contents))
1892 fake_current_matrices (w->contents);
1893 else
1895 int i;
1896 struct frame *f = XFRAME (w->frame);
1897 struct glyph_matrix *m = w->current_matrix;
1898 struct glyph_matrix *fm = f->current_matrix;
1900 eassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
1901 eassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
1903 for (i = 0; i < m->matrix_h; ++i)
1905 struct glyph_row *r = m->rows + i;
1906 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
1908 eassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
1909 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
1911 r->enabled_p = fr->enabled_p;
1912 if (r->enabled_p)
1914 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
1915 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
1916 r->used[TEXT_AREA] = (m->matrix_w
1917 - r->used[LEFT_MARGIN_AREA]
1918 - r->used[RIGHT_MARGIN_AREA]);
1919 r->mode_line_p = 0;
1927 /* Save away the contents of frame F's current frame matrix. Value is
1928 a glyph matrix holding the contents of F's current frame matrix. */
1930 static struct glyph_matrix *
1931 save_current_matrix (struct frame *f)
1933 int i;
1934 struct glyph_matrix *saved = xzalloc (sizeof *saved);
1935 saved->nrows = f->current_matrix->nrows;
1936 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
1938 for (i = 0; i < saved->nrows; ++i)
1940 struct glyph_row *from = f->current_matrix->rows + i;
1941 struct glyph_row *to = saved->rows + i;
1942 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1943 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
1944 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1945 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1948 return saved;
1952 /* Restore the contents of frame F's current frame matrix from SAVED,
1953 and free memory associated with SAVED. */
1955 static void
1956 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
1958 int i;
1960 for (i = 0; i < saved->nrows; ++i)
1962 struct glyph_row *from = saved->rows + i;
1963 struct glyph_row *to = f->current_matrix->rows + i;
1964 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1965 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1966 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1967 xfree (from->glyphs[TEXT_AREA]);
1970 xfree (saved->rows);
1971 xfree (saved);
1976 /* Allocate/reallocate glyph matrices of a single frame F for
1977 frame-based redisplay. */
1979 static void
1980 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
1982 struct dim matrix_dim;
1983 bool pool_changed_p;
1984 int window_change_flags;
1985 int top_window_y;
1987 if (!FRAME_LIVE_P (f))
1988 return;
1990 top_window_y = FRAME_TOP_MARGIN (f);
1992 /* Allocate glyph pool structures if not already done. */
1993 if (f->desired_pool == NULL)
1995 f->desired_pool = new_glyph_pool ();
1996 f->current_pool = new_glyph_pool ();
1999 /* Allocate frames matrix structures if needed. */
2000 if (f->desired_matrix == NULL)
2002 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2003 f->current_matrix = new_glyph_matrix (f->current_pool);
2006 /* Compute window glyph matrices. (This takes the mini-buffer
2007 window into account). The result is the size of the frame glyph
2008 matrix needed. The variable window_change_flags is set to a bit
2009 mask indicating whether new matrices will be allocated or
2010 existing matrices change their size or location within the frame
2011 matrix. */
2012 window_change_flags = 0;
2013 matrix_dim
2014 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2015 0, top_window_y,
2017 &window_change_flags);
2019 /* Add in menu bar lines, if any. */
2020 matrix_dim.height += top_window_y;
2022 /* Enlarge pools as necessary. */
2023 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2024 realloc_glyph_pool (f->current_pool, matrix_dim);
2026 /* Set up glyph pointers within window matrices. Do this only if
2027 absolutely necessary since it requires a frame redraw. */
2028 if (pool_changed_p || window_change_flags)
2030 /* Do it for window matrices. */
2031 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2032 0, top_window_y, 0,
2033 &window_change_flags);
2035 /* Size of frame matrices must equal size of frame. Note
2036 that we are called for X frames with window widths NOT equal
2037 to the frame width (from CHANGE_FRAME_SIZE_1). */
2038 eassert (matrix_dim.width == FRAME_COLS (f)
2039 && matrix_dim.height == FRAME_LINES (f));
2041 /* Pointers to glyph memory in glyph rows are exchanged during
2042 the update phase of redisplay, which means in general that a
2043 frame's current matrix consists of pointers into both the
2044 desired and current glyph pool of the frame. Adjusting a
2045 matrix sets the frame matrix up so that pointers are all into
2046 the same pool. If we want to preserve glyph contents of the
2047 current matrix over a call to adjust_glyph_matrix, we must
2048 make a copy of the current glyphs, and restore the current
2049 matrix' contents from that copy. */
2050 if (display_completed
2051 && !FRAME_GARBAGED_P (f)
2052 && matrix_dim.width == f->current_matrix->matrix_w
2053 && matrix_dim.height == f->current_matrix->matrix_h
2054 /* For some reason, the frame glyph matrix gets corrupted if
2055 any of the windows contain margins. I haven't been able
2056 to hunt down the reason, but for the moment this prevents
2057 the problem from manifesting. -- cyd */
2058 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2060 struct glyph_matrix *copy = save_current_matrix (f);
2061 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2062 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2063 restore_current_matrix (f, copy);
2064 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2066 else
2068 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2069 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2070 SET_FRAME_GARBAGED (f);
2076 /* Allocate/reallocate glyph matrices of a single frame F for
2077 window-based redisplay. */
2079 static void
2080 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2082 eassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2084 /* Allocate/reallocate window matrices. */
2085 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2087 #ifdef HAVE_X_WINDOWS
2088 /* Allocate/ reallocate matrices of the dummy window used to display
2089 the menu bar under X when no X toolkit support is available. */
2090 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2092 /* Allocate a dummy window if not already done. */
2093 struct window *w;
2094 if (NILP (f->menu_bar_window))
2096 Lisp_Object frame;
2097 fset_menu_bar_window (f, make_window ());
2098 w = XWINDOW (f->menu_bar_window);
2099 XSETFRAME (frame, f);
2100 wset_frame (w, frame);
2101 w->pseudo_window_p = 1;
2103 else
2104 w = XWINDOW (f->menu_bar_window);
2106 /* Set window dimensions to frame dimensions and allocate or
2107 adjust glyph matrices of W. */
2108 w->top_line = 0;
2109 w->left_col = 0;
2110 w->total_lines = FRAME_MENU_BAR_LINES (f);
2111 w->total_cols = FRAME_TOTAL_COLS (f);
2112 allocate_matrices_for_window_redisplay (w);
2114 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2115 #endif /* HAVE_X_WINDOWS */
2117 #ifndef USE_GTK
2119 /* Allocate/ reallocate matrices of the tool bar window. If we
2120 don't have a tool bar window yet, make one. */
2121 struct window *w;
2122 if (NILP (f->tool_bar_window))
2124 Lisp_Object frame;
2125 fset_tool_bar_window (f, make_window ());
2126 w = XWINDOW (f->tool_bar_window);
2127 XSETFRAME (frame, f);
2128 wset_frame (w, frame);
2129 w->pseudo_window_p = 1;
2131 else
2132 w = XWINDOW (f->tool_bar_window);
2134 w->top_line = FRAME_MENU_BAR_LINES (f);
2135 w->left_col = 0;
2136 w->total_lines = FRAME_TOOL_BAR_LINES (f);
2137 w->total_cols = FRAME_TOTAL_COLS (f);
2138 allocate_matrices_for_window_redisplay (w);
2140 #endif
2144 /* Re-allocate buffer for decode_mode_spec on frame F. */
2146 static void
2147 adjust_decode_mode_spec_buffer (struct frame *f)
2149 f->decode_mode_spec_buffer = xrealloc (f->decode_mode_spec_buffer,
2150 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2155 /**********************************************************************
2156 Freeing Glyph Matrices
2157 **********************************************************************/
2159 /* Free glyph memory for a frame F. F may be null. This function can
2160 be called for the same frame more than once. The root window of
2161 F may be nil when this function is called. This is the case when
2162 the function is called when F is destroyed. */
2164 void
2165 free_glyphs (struct frame *f)
2167 if (f && f->glyphs_initialized_p)
2169 /* Block interrupt input so that we don't get surprised by an X
2170 event while we're in an inconsistent state. */
2171 block_input ();
2172 f->glyphs_initialized_p = 0;
2174 /* Release window sub-matrices. */
2175 if (!NILP (f->root_window))
2176 free_window_matrices (XWINDOW (f->root_window));
2178 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2179 /* Free the dummy window for menu bars without X toolkit and its
2180 glyph matrices. */
2181 if (!NILP (f->menu_bar_window))
2183 struct window *w = XWINDOW (f->menu_bar_window);
2184 free_glyph_matrix (w->desired_matrix);
2185 free_glyph_matrix (w->current_matrix);
2186 w->desired_matrix = w->current_matrix = NULL;
2187 fset_menu_bar_window (f, Qnil);
2189 #endif
2191 /* Free the tool bar window and its glyph matrices. */
2192 if (!NILP (f->tool_bar_window))
2194 struct window *w = XWINDOW (f->tool_bar_window);
2195 free_glyph_matrix (w->desired_matrix);
2196 free_glyph_matrix (w->current_matrix);
2197 w->desired_matrix = w->current_matrix = NULL;
2198 fset_tool_bar_window (f, Qnil);
2201 /* Release frame glyph matrices. Reset fields to zero in
2202 case we are called a second time. */
2203 if (f->desired_matrix)
2205 free_glyph_matrix (f->desired_matrix);
2206 free_glyph_matrix (f->current_matrix);
2207 f->desired_matrix = f->current_matrix = NULL;
2210 /* Release glyph pools. */
2211 if (f->desired_pool)
2213 free_glyph_pool (f->desired_pool);
2214 free_glyph_pool (f->current_pool);
2215 f->desired_pool = f->current_pool = NULL;
2218 unblock_input ();
2223 /* Free glyph sub-matrices in the window tree rooted at W. This
2224 function may be called with a null pointer, and it may be called on
2225 the same tree more than once. */
2227 void
2228 free_window_matrices (struct window *w)
2230 while (w)
2232 if (WINDOWP (w->contents))
2233 free_window_matrices (XWINDOW (w->contents));
2234 else
2236 /* This is a leaf window. Free its memory and reset fields
2237 to zero in case this function is called a second time for
2238 W. */
2239 free_glyph_matrix (w->current_matrix);
2240 free_glyph_matrix (w->desired_matrix);
2241 w->current_matrix = w->desired_matrix = NULL;
2244 /* Next window on same level. */
2245 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2250 /* Check glyph memory leaks. This function is called from
2251 shut_down_emacs. Note that frames are not destroyed when Emacs
2252 exits. We therefore free all glyph memory for all active frames
2253 explicitly and check that nothing is left allocated. */
2255 void
2256 check_glyph_memory (void)
2258 Lisp_Object tail, frame;
2260 /* Free glyph memory for all frames. */
2261 FOR_EACH_FRAME (tail, frame)
2262 free_glyphs (XFRAME (frame));
2264 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2265 /* Check that nothing is left allocated. */
2266 eassert (glyph_matrix_count == 0);
2267 eassert (glyph_pool_count == 0);
2268 #endif
2273 /**********************************************************************
2274 Building a Frame Matrix
2275 **********************************************************************/
2277 /* Most of the redisplay code works on glyph matrices attached to
2278 windows. This is a good solution most of the time, but it is not
2279 suitable for terminal code. Terminal output functions cannot rely
2280 on being able to set an arbitrary terminal window. Instead they
2281 must be provided with a view of the whole frame, i.e. the whole
2282 screen. We build such a view by constructing a frame matrix from
2283 window matrices in this section.
2285 Windows that must be updated have their must_be_updated_p flag set.
2286 For all such windows, their desired matrix is made part of the
2287 desired frame matrix. For other windows, their current matrix is
2288 made part of the desired frame matrix.
2290 +-----------------+----------------+
2291 | desired | desired |
2292 | | |
2293 +-----------------+----------------+
2294 | current |
2296 +----------------------------------+
2298 Desired window matrices can be made part of the frame matrix in a
2299 cheap way: We exploit the fact that the desired frame matrix and
2300 desired window matrices share their glyph memory. This is not
2301 possible for current window matrices. Their glyphs are copied to
2302 the desired frame matrix. The latter is equivalent to
2303 preserve_other_columns in the old redisplay.
2305 Used glyphs counters for frame matrix rows are the result of adding
2306 up glyph lengths of the window matrices. A line in the frame
2307 matrix is enabled, if a corresponding line in a window matrix is
2308 enabled.
2310 After building the desired frame matrix, it will be passed to
2311 terminal code, which will manipulate both the desired and current
2312 frame matrix. Changes applied to the frame's current matrix have
2313 to be visible in current window matrices afterwards, of course.
2315 This problem is solved like this:
2317 1. Window and frame matrices share glyphs. Window matrices are
2318 constructed in a way that their glyph contents ARE the glyph
2319 contents needed in a frame matrix. Thus, any modification of
2320 glyphs done in terminal code will be reflected in window matrices
2321 automatically.
2323 2. Exchanges of rows in a frame matrix done by terminal code are
2324 intercepted by hook functions so that corresponding row operations
2325 on window matrices can be performed. This is necessary because we
2326 use pointers to glyphs in glyph row structures. To satisfy the
2327 assumption of point 1 above that glyphs are updated implicitly in
2328 window matrices when they are manipulated via the frame matrix,
2329 window and frame matrix must of course agree where to find the
2330 glyphs for their rows. Possible manipulations that must be
2331 mirrored are assignments of rows of the desired frame matrix to the
2332 current frame matrix and scrolling the current frame matrix. */
2334 /* Build frame F's desired matrix from window matrices. Only windows
2335 which have the flag must_be_updated_p set have to be updated. Menu
2336 bar lines of a frame are not covered by window matrices, so make
2337 sure not to touch them in this function. */
2339 static void
2340 build_frame_matrix (struct frame *f)
2342 int i;
2344 /* F must have a frame matrix when this function is called. */
2345 eassert (!FRAME_WINDOW_P (f));
2347 /* Clear all rows in the frame matrix covered by window matrices.
2348 Menu bar lines are not covered by windows. */
2349 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2350 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2352 /* Build the matrix by walking the window tree. */
2353 build_frame_matrix_from_window_tree (f->desired_matrix,
2354 XWINDOW (FRAME_ROOT_WINDOW (f)));
2358 /* Walk a window tree, building a frame matrix MATRIX from window
2359 matrices. W is the root of a window tree. */
2361 static void
2362 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2364 while (w)
2366 if (WINDOWP (w->contents))
2367 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->contents));
2368 else
2369 build_frame_matrix_from_leaf_window (matrix, w);
2371 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2376 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2377 desired frame matrix built. W is a leaf window whose desired or
2378 current matrix is to be added to FRAME_MATRIX. W's flag
2379 must_be_updated_p determines which matrix it contributes to
2380 FRAME_MATRIX. If W->must_be_updated_p, W's desired matrix
2381 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2382 Adding a desired matrix means setting up used counters and such in
2383 frame rows, while adding a current window matrix to FRAME_MATRIX
2384 means copying glyphs. The latter case corresponds to
2385 preserve_other_columns in the old redisplay. */
2387 static void
2388 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2390 struct glyph_matrix *window_matrix;
2391 int window_y, frame_y;
2392 /* If non-zero, a glyph to insert at the right border of W. */
2393 GLYPH right_border_glyph;
2395 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2397 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2398 if (w->must_be_updated_p)
2400 window_matrix = w->desired_matrix;
2402 /* Decide whether we want to add a vertical border glyph. */
2403 if (!WINDOW_RIGHTMOST_P (w))
2405 struct Lisp_Char_Table *dp = window_display_table (w);
2406 Lisp_Object gc;
2408 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2409 if (dp
2410 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc)))
2412 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2413 spec_glyph_lookup_face (w, &right_border_glyph);
2416 if (GLYPH_FACE (right_border_glyph) <= 0)
2417 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2420 else
2421 window_matrix = w->current_matrix;
2423 /* For all rows in the window matrix and corresponding rows in the
2424 frame matrix. */
2425 window_y = 0;
2426 frame_y = window_matrix->matrix_y;
2427 while (window_y < window_matrix->nrows)
2429 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2430 struct glyph_row *window_row = window_matrix->rows + window_y;
2431 bool current_row_p = window_matrix == w->current_matrix;
2433 /* Fill up the frame row with spaces up to the left margin of the
2434 window row. */
2435 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2437 /* Fill up areas in the window matrix row with spaces. */
2438 fill_up_glyph_row_with_spaces (window_row);
2440 /* If only part of W's desired matrix has been built, and
2441 window_row wasn't displayed, use the corresponding current
2442 row instead. */
2443 if (window_matrix == w->desired_matrix
2444 && !window_row->enabled_p)
2446 window_row = w->current_matrix->rows + window_y;
2447 current_row_p = 1;
2450 if (current_row_p)
2452 /* Copy window row to frame row. */
2453 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2454 window_row->glyphs[0],
2455 window_matrix->matrix_w * sizeof (struct glyph));
2457 else
2459 eassert (window_row->enabled_p);
2461 /* Only when a desired row has been displayed, we want
2462 the corresponding frame row to be updated. */
2463 frame_row->enabled_p = 1;
2465 /* Maybe insert a vertical border between horizontally adjacent
2466 windows. */
2467 if (GLYPH_CHAR (right_border_glyph) != 0)
2469 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2470 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2473 #ifdef GLYPH_DEBUG
2474 /* Window row window_y must be a slice of frame row
2475 frame_y. */
2476 eassert (glyph_row_slice_p (window_row, frame_row));
2478 /* If rows are in sync, we don't have to copy glyphs because
2479 frame and window share glyphs. */
2481 strcpy (w->current_matrix->method, w->desired_matrix->method);
2482 add_window_display_history (w, w->current_matrix->method, 0);
2483 #endif
2486 /* Set number of used glyphs in the frame matrix. Since we fill
2487 up with spaces, and visit leaf windows from left to right it
2488 can be done simply. */
2489 frame_row->used[TEXT_AREA]
2490 = window_matrix->matrix_x + window_matrix->matrix_w;
2492 /* Next row. */
2493 ++window_y;
2494 ++frame_y;
2498 /* Given a user-specified glyph, possibly including a Lisp-level face
2499 ID, return a glyph that has a realized face ID.
2500 This is used for glyphs displayed specially and not part of the text;
2501 for instance, vertical separators, truncation markers, etc. */
2503 void
2504 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2506 int lface_id = GLYPH_FACE (*glyph);
2507 /* Convert the glyph's specified face to a realized (cache) face. */
2508 if (lface_id > 0)
2510 int face_id = merge_faces (XFRAME (w->frame),
2511 Qt, lface_id, DEFAULT_FACE_ID);
2512 SET_GLYPH_FACE (*glyph, face_id);
2516 /* Add spaces to a glyph row ROW in a window matrix.
2518 Each row has the form:
2520 +---------+-----------------------------+------------+
2521 | left | text | right |
2522 +---------+-----------------------------+------------+
2524 Left and right marginal areas are optional. This function adds
2525 spaces to areas so that there are no empty holes between areas.
2526 In other words: If the right area is not empty, the text area
2527 is filled up with spaces up to the right area. If the text area
2528 is not empty, the left area is filled up.
2530 To be called for frame-based redisplay, only. */
2532 static void
2533 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2535 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2536 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2537 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2541 /* Fill area AREA of glyph row ROW with spaces. To be called for
2542 frame-based redisplay only. */
2544 static void
2545 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2547 if (row->glyphs[area] < row->glyphs[area + 1])
2549 struct glyph *end = row->glyphs[area + 1];
2550 struct glyph *text = row->glyphs[area] + row->used[area];
2552 while (text < end)
2553 *text++ = space_glyph;
2554 row->used[area] = text - row->glyphs[area];
2559 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2560 reached. In frame matrices only one area, TEXT_AREA, is used. */
2562 static void
2563 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2565 int i = row->used[TEXT_AREA];
2566 struct glyph *glyph = row->glyphs[TEXT_AREA];
2568 while (i < upto)
2569 glyph[i++] = space_glyph;
2571 row->used[TEXT_AREA] = i;
2576 /**********************************************************************
2577 Mirroring operations on frame matrices in window matrices
2578 **********************************************************************/
2580 /* Set frame being updated via frame-based redisplay to F. This
2581 function must be called before updates to make explicit that we are
2582 working on frame matrices or not. */
2584 static void
2585 set_frame_matrix_frame (struct frame *f)
2587 frame_matrix_frame = f;
2591 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2592 DESIRED_MATRIX is the desired matrix corresponding to
2593 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2594 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2595 frame_matrix_frame is non-null, this indicates that the exchange is
2596 done in frame matrices, and that we have to perform analogous
2597 operations in window matrices of frame_matrix_frame. */
2599 static void
2600 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2602 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2603 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2604 bool mouse_face_p = current_row->mouse_face_p;
2606 /* Do current_row = desired_row. This exchanges glyph pointers
2607 between both rows, and does a structure assignment otherwise. */
2608 assign_row (current_row, desired_row);
2610 /* Enable current_row to mark it as valid. */
2611 current_row->enabled_p = 1;
2612 current_row->mouse_face_p = mouse_face_p;
2614 /* If we are called on frame matrices, perform analogous operations
2615 for window matrices. */
2616 if (frame_matrix_frame)
2617 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2621 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2622 W's frame which has been made current (by swapping pointers between
2623 current and desired matrix). Perform analogous operations in the
2624 matrices of leaf windows in the window tree rooted at W. */
2626 static void
2627 mirror_make_current (struct window *w, int frame_row)
2629 while (w)
2631 if (WINDOWP (w->contents))
2632 mirror_make_current (XWINDOW (w->contents), frame_row);
2633 else
2635 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2636 here because the checks performed in debug mode there
2637 will not allow the conversion. */
2638 int row = frame_row - w->desired_matrix->matrix_y;
2640 /* If FRAME_ROW is within W, assign the desired row to the
2641 current row (exchanging glyph pointers). */
2642 if (row >= 0 && row < w->desired_matrix->matrix_h)
2644 struct glyph_row *current_row
2645 = MATRIX_ROW (w->current_matrix, row);
2646 struct glyph_row *desired_row
2647 = MATRIX_ROW (w->desired_matrix, row);
2649 if (desired_row->enabled_p)
2650 assign_row (current_row, desired_row);
2651 else
2652 swap_glyph_pointers (desired_row, current_row);
2653 current_row->enabled_p = 1;
2655 /* Set the Y coordinate of the mode/header line's row.
2656 It is needed in draw_row_with_mouse_face to find the
2657 screen coordinates. (Window-based redisplay sets
2658 this in update_window, but no one seems to do that
2659 for frame-based redisplay.) */
2660 if (current_row->mode_line_p)
2661 current_row->y = row;
2665 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2670 /* Perform row dance after scrolling. We are working on the range of
2671 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2672 including) in MATRIX. COPY_FROM is a vector containing, for each
2673 row I in the range 0 <= I < NLINES, the index of the original line
2674 to move to I. This index is relative to the row range, i.e. 0 <=
2675 index < NLINES. RETAINED_P is a vector containing zero for each
2676 row 0 <= I < NLINES which is empty.
2678 This function is called from do_scrolling and do_direct_scrolling. */
2680 void
2681 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2682 int *copy_from, char *retained_p)
2684 /* A copy of original rows. */
2685 struct glyph_row *old_rows;
2687 /* Rows to assign to. */
2688 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2690 int i;
2692 /* Make a copy of the original rows. */
2693 old_rows = alloca (nlines * sizeof *old_rows);
2694 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2696 /* Assign new rows, maybe clear lines. */
2697 for (i = 0; i < nlines; ++i)
2699 bool enabled_before_p = new_rows[i].enabled_p;
2701 eassert (i + unchanged_at_top < matrix->nrows);
2702 eassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2703 new_rows[i] = old_rows[copy_from[i]];
2704 new_rows[i].enabled_p = enabled_before_p;
2706 /* RETAINED_P is zero for empty lines. */
2707 if (!retained_p[copy_from[i]])
2708 new_rows[i].enabled_p = 0;
2711 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2712 if (frame_matrix_frame)
2713 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2714 unchanged_at_top, nlines, copy_from, retained_p);
2718 /* Synchronize glyph pointers in the current matrix of window W with
2719 the current frame matrix. */
2721 static void
2722 sync_window_with_frame_matrix_rows (struct window *w)
2724 struct frame *f = XFRAME (w->frame);
2725 struct glyph_row *window_row, *window_row_end, *frame_row;
2726 int left, right, x, width;
2728 /* Preconditions: W must be a live window on a tty frame. */
2729 eassert (BUFFERP (w->contents));
2730 eassert (!FRAME_WINDOW_P (f));
2732 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2733 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2734 x = w->current_matrix->matrix_x;
2735 width = w->current_matrix->matrix_w;
2737 window_row = w->current_matrix->rows;
2738 window_row_end = window_row + w->current_matrix->nrows;
2739 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2741 for (; window_row < window_row_end; ++window_row, ++frame_row)
2743 window_row->glyphs[LEFT_MARGIN_AREA]
2744 = frame_row->glyphs[0] + x;
2745 window_row->glyphs[TEXT_AREA]
2746 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2747 window_row->glyphs[LAST_AREA]
2748 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2749 window_row->glyphs[RIGHT_MARGIN_AREA]
2750 = window_row->glyphs[LAST_AREA] - right;
2755 /* Return the window in the window tree rooted in W containing frame
2756 row ROW. Value is null if none is found. */
2758 static struct window *
2759 frame_row_to_window (struct window *w, int row)
2761 struct window *found = NULL;
2763 while (w && !found)
2765 if (WINDOWP (w->contents))
2766 found = frame_row_to_window (XWINDOW (w->contents), row);
2767 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2768 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2769 found = w;
2771 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2774 return found;
2778 /* Perform a line dance in the window tree rooted at W, after
2779 scrolling a frame matrix in mirrored_line_dance.
2781 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2782 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2783 COPY_FROM is a vector containing, for each row I in the range 0 <=
2784 I < NLINES, the index of the original line to move to I. This
2785 index is relative to the row range, i.e. 0 <= index < NLINES.
2786 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2787 which is empty. */
2789 static void
2790 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2792 while (w)
2794 if (WINDOWP (w->contents))
2795 mirror_line_dance (XWINDOW (w->contents), unchanged_at_top,
2796 nlines, copy_from, retained_p);
2797 else
2799 /* W is a leaf window, and we are working on its current
2800 matrix m. */
2801 struct glyph_matrix *m = w->current_matrix;
2802 int i;
2803 bool sync_p = 0;
2804 struct glyph_row *old_rows;
2806 /* Make a copy of the original rows of matrix m. */
2807 old_rows = alloca (m->nrows * sizeof *old_rows);
2808 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2810 for (i = 0; i < nlines; ++i)
2812 /* Frame relative line assigned to. */
2813 int frame_to = i + unchanged_at_top;
2815 /* Frame relative line assigned. */
2816 int frame_from = copy_from[i] + unchanged_at_top;
2818 /* Window relative line assigned to. */
2819 int window_to = frame_to - m->matrix_y;
2821 /* Window relative line assigned. */
2822 int window_from = frame_from - m->matrix_y;
2824 /* Is assigned line inside window? */
2825 bool from_inside_window_p
2826 = window_from >= 0 && window_from < m->matrix_h;
2828 /* Is assigned to line inside window? */
2829 bool to_inside_window_p
2830 = window_to >= 0 && window_to < m->matrix_h;
2832 if (from_inside_window_p && to_inside_window_p)
2834 /* Do the assignment. The enabled_p flag is saved
2835 over the assignment because the old redisplay did
2836 that. */
2837 bool enabled_before_p = m->rows[window_to].enabled_p;
2838 m->rows[window_to] = old_rows[window_from];
2839 m->rows[window_to].enabled_p = enabled_before_p;
2841 /* If frame line is empty, window line is empty, too. */
2842 if (!retained_p[copy_from[i]])
2843 m->rows[window_to].enabled_p = 0;
2845 else if (to_inside_window_p)
2847 /* A copy between windows. This is an infrequent
2848 case not worth optimizing. */
2849 struct frame *f = XFRAME (w->frame);
2850 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2851 struct window *w2;
2852 struct glyph_matrix *m2;
2853 int m2_from;
2855 w2 = frame_row_to_window (root, frame_from);
2856 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2857 -nw', FROM_FRAME sometimes has no associated window.
2858 This check avoids a segfault if W2 is null. */
2859 if (w2)
2861 m2 = w2->current_matrix;
2862 m2_from = frame_from - m2->matrix_y;
2863 copy_row_except_pointers (m->rows + window_to,
2864 m2->rows + m2_from);
2866 /* If frame line is empty, window line is empty, too. */
2867 if (!retained_p[copy_from[i]])
2868 m->rows[window_to].enabled_p = 0;
2870 sync_p = 1;
2872 else if (from_inside_window_p)
2873 sync_p = 1;
2876 /* If there was a copy between windows, make sure glyph
2877 pointers are in sync with the frame matrix. */
2878 if (sync_p)
2879 sync_window_with_frame_matrix_rows (w);
2881 /* Check that no pointers are lost. */
2882 CHECK_MATRIX (m);
2885 /* Next window on same level. */
2886 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2891 #ifdef GLYPH_DEBUG
2893 /* Check that window and frame matrices agree about their
2894 understanding where glyphs of the rows are to find. For each
2895 window in the window tree rooted at W, check that rows in the
2896 matrices of leaf window agree with their frame matrices about
2897 glyph pointers. */
2899 static void
2900 check_window_matrix_pointers (struct window *w)
2902 while (w)
2904 if (WINDOWP (w->contents))
2905 check_window_matrix_pointers (XWINDOW (w->contents));
2906 else
2908 struct frame *f = XFRAME (w->frame);
2909 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
2910 check_matrix_pointers (w->current_matrix, f->current_matrix);
2913 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2918 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
2919 a window and FRAME_MATRIX is the corresponding frame matrix. For
2920 each row in WINDOW_MATRIX check that it's a slice of the
2921 corresponding frame row. If it isn't, abort. */
2923 static void
2924 check_matrix_pointers (struct glyph_matrix *window_matrix,
2925 struct glyph_matrix *frame_matrix)
2927 /* Row number in WINDOW_MATRIX. */
2928 int i = 0;
2930 /* Row number corresponding to I in FRAME_MATRIX. */
2931 int j = window_matrix->matrix_y;
2933 /* For all rows check that the row in the window matrix is a
2934 slice of the row in the frame matrix. If it isn't we didn't
2935 mirror an operation on the frame matrix correctly. */
2936 while (i < window_matrix->nrows)
2938 if (!glyph_row_slice_p (window_matrix->rows + i,
2939 frame_matrix->rows + j))
2940 emacs_abort ();
2941 ++i, ++j;
2945 #endif /* GLYPH_DEBUG */
2949 /**********************************************************************
2950 VPOS and HPOS translations
2951 **********************************************************************/
2953 #ifdef GLYPH_DEBUG
2955 /* Translate vertical position VPOS which is relative to window W to a
2956 vertical position relative to W's frame. */
2958 static int
2959 window_to_frame_vpos (struct window *w, int vpos)
2961 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
2962 eassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
2963 vpos += WINDOW_TOP_EDGE_LINE (w);
2964 eassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
2965 return vpos;
2969 /* Translate horizontal position HPOS which is relative to window W to
2970 a horizontal position relative to W's frame. */
2972 static int
2973 window_to_frame_hpos (struct window *w, int hpos)
2975 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
2976 hpos += WINDOW_LEFT_EDGE_COL (w);
2977 return hpos;
2980 #endif /* GLYPH_DEBUG */
2984 /**********************************************************************
2985 Redrawing Frames
2986 **********************************************************************/
2988 /* Redraw frame F. */
2990 void
2991 redraw_frame (struct frame *f)
2993 /* Error if F has no glyphs. */
2994 eassert (f->glyphs_initialized_p);
2995 update_begin (f);
2996 #ifdef MSDOS
2997 if (FRAME_MSDOS_P (f))
2998 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
2999 #endif
3000 clear_frame (f);
3001 clear_current_matrices (f);
3002 update_end (f);
3003 if (FRAME_TERMCAP_P (f))
3004 fflush (FRAME_TTY (f)->output);
3005 windows_or_buffers_changed++;
3006 /* Mark all windows as inaccurate, so that every window will have
3007 its redisplay done. */
3008 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3009 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3010 f->garbaged = 0;
3013 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 0, 1, 0,
3014 doc: /* Clear frame FRAME and output again what is supposed to appear on it.
3015 If FRAME is omitted or nil, the selected frame is used. */)
3016 (Lisp_Object frame)
3018 redraw_frame (decode_live_frame (frame));
3019 return Qnil;
3022 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3023 doc: /* Clear and redisplay all visible frames. */)
3024 (void)
3026 Lisp_Object tail, frame;
3028 FOR_EACH_FRAME (tail, frame)
3029 if (FRAME_VISIBLE_P (XFRAME (frame)))
3030 redraw_frame (XFRAME (frame));
3032 return Qnil;
3037 /***********************************************************************
3038 Frame Update
3039 ***********************************************************************/
3041 /* Update frame F based on the data in desired matrices.
3043 If FORCE_P, don't let redisplay be stopped by detecting pending input.
3044 If INHIBIT_HAIRY_ID_P, don't try scrolling.
3046 Value is true if redisplay was stopped due to pending input. */
3048 bool
3049 update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
3051 /* True means display has been paused because of pending input. */
3052 bool paused_p;
3053 struct window *root_window = XWINDOW (f->root_window);
3055 if (redisplay_dont_pause)
3056 force_p = 1;
3057 else if (!force_p && detect_input_pending_ignore_squeezables ())
3059 paused_p = 1;
3060 goto do_pause;
3063 if (FRAME_WINDOW_P (f))
3065 /* We are working on window matrix basis. All windows whose
3066 flag must_be_updated_p is set have to be updated. */
3068 /* Record that we are not working on frame matrices. */
3069 set_frame_matrix_frame (NULL);
3071 /* Update all windows in the window tree of F, maybe stopping
3072 when pending input is detected. */
3073 update_begin (f);
3075 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
3076 /* Update the menu bar on X frames that don't have toolkit
3077 support. */
3078 if (WINDOWP (f->menu_bar_window))
3079 update_window (XWINDOW (f->menu_bar_window), 1);
3080 #endif
3082 /* Update the tool-bar window, if present. */
3083 if (WINDOWP (f->tool_bar_window))
3085 struct window *w = XWINDOW (f->tool_bar_window);
3087 /* Update tool-bar window. */
3088 if (w->must_be_updated_p)
3090 Lisp_Object tem;
3092 update_window (w, 1);
3093 w->must_be_updated_p = 0;
3095 /* Swap tool-bar strings. We swap because we want to
3096 reuse strings. */
3097 tem = f->current_tool_bar_string;
3098 fset_current_tool_bar_string (f, f->desired_tool_bar_string);
3099 fset_desired_tool_bar_string (f, tem);
3104 /* Update windows. */
3105 paused_p = update_window_tree (root_window, force_p);
3106 update_end (f);
3108 /* This flush is a performance bottleneck under X,
3109 and it doesn't seem to be necessary anyway (in general).
3110 It is necessary when resizing the window with the mouse, or
3111 at least the fringes are not redrawn in a timely manner. ++kfs */
3112 if (f->force_flush_display_p)
3114 FRAME_RIF (f)->flush_display (f);
3115 f->force_flush_display_p = 0;
3118 else
3120 /* We are working on frame matrix basis. Set the frame on whose
3121 frame matrix we operate. */
3122 set_frame_matrix_frame (f);
3124 /* Build F's desired matrix from window matrices. */
3125 build_frame_matrix (f);
3127 /* Update the display */
3128 update_begin (f);
3129 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3130 update_end (f);
3132 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3134 if (FRAME_TTY (f)->termscript)
3135 fflush (FRAME_TTY (f)->termscript);
3136 if (FRAME_TERMCAP_P (f))
3137 fflush (FRAME_TTY (f)->output);
3140 /* Check window matrices for lost pointers. */
3141 #ifdef GLYPH_DEBUG
3142 check_window_matrix_pointers (root_window);
3143 add_frame_display_history (f, paused_p);
3144 #endif
3147 do_pause:
3148 /* Reset flags indicating that a window should be updated. */
3149 set_window_update_flags (root_window, 0);
3151 display_completed = !paused_p;
3152 return paused_p;
3157 /************************************************************************
3158 Window-based updates
3159 ************************************************************************/
3161 /* Perform updates in window tree rooted at W.
3162 If FORCE_P, don't stop updating if input is pending. */
3164 static bool
3165 update_window_tree (struct window *w, bool force_p)
3167 bool paused_p = 0;
3169 while (w && !paused_p)
3171 if (WINDOWP (w->contents))
3172 paused_p |= update_window_tree (XWINDOW (w->contents), force_p);
3173 else if (w->must_be_updated_p)
3174 paused_p |= update_window (w, force_p);
3176 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3179 return paused_p;
3183 /* Update window W if its flag must_be_updated_p is set.
3184 If FORCE_P, don't stop updating if input is pending. */
3186 void
3187 update_single_window (struct window *w, bool force_p)
3189 if (w->must_be_updated_p)
3191 struct frame *f = XFRAME (WINDOW_FRAME (w));
3193 /* Record that this is not a frame-based redisplay. */
3194 set_frame_matrix_frame (NULL);
3196 if (redisplay_dont_pause)
3197 force_p = 1;
3199 /* Update W. */
3200 update_begin (f);
3201 update_window (w, force_p);
3202 update_end (f);
3204 /* Reset flag in W. */
3205 w->must_be_updated_p = 0;
3209 #ifdef HAVE_WINDOW_SYSTEM
3211 /* Redraw lines from the current matrix of window W that are
3212 overlapped by other rows. YB is bottom-most y-position in W. */
3214 static void
3215 redraw_overlapped_rows (struct window *w, int yb)
3217 int i;
3218 struct frame *f = XFRAME (WINDOW_FRAME (w));
3220 /* If rows overlapping others have been changed, the rows being
3221 overlapped have to be redrawn. This won't draw lines that have
3222 already been drawn in update_window_line because overlapped_p in
3223 desired rows is 0, so after row assignment overlapped_p in
3224 current rows is 0. */
3225 for (i = 0; i < w->current_matrix->nrows; ++i)
3227 struct glyph_row *row = w->current_matrix->rows + i;
3229 if (!row->enabled_p)
3230 break;
3231 else if (row->mode_line_p)
3232 continue;
3234 if (row->overlapped_p)
3236 enum glyph_row_area area;
3238 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3240 updated_row = row;
3241 updated_area = area;
3242 FRAME_RIF (f)->cursor_to (w, i, 0, row->y,
3243 area == TEXT_AREA ? row->x : 0);
3244 if (row->used[area])
3245 FRAME_RIF (f)->write_glyphs (w, row->glyphs[area],
3246 row->used[area]);
3247 FRAME_RIF (f)->clear_end_of_line (w, -1);
3250 row->overlapped_p = 0;
3253 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3254 break;
3259 /* Redraw lines from the current matrix of window W that overlap
3260 others. YB is bottom-most y-position in W. */
3262 static void
3263 redraw_overlapping_rows (struct window *w, int yb)
3265 int i, bottom_y;
3266 struct glyph_row *row;
3267 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3269 for (i = 0; i < w->current_matrix->nrows; ++i)
3271 row = w->current_matrix->rows + i;
3273 if (!row->enabled_p)
3274 break;
3275 else if (row->mode_line_p)
3276 continue;
3278 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3280 if (row->overlapping_p)
3282 int overlaps = 0;
3284 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3285 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3286 overlaps |= OVERLAPS_PRED;
3287 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3288 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3289 overlaps |= OVERLAPS_SUCC;
3291 if (overlaps)
3293 if (row->used[LEFT_MARGIN_AREA])
3294 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3296 if (row->used[TEXT_AREA])
3297 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3299 if (row->used[RIGHT_MARGIN_AREA])
3300 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3302 /* Record in neighbor rows that ROW overwrites part of
3303 their display. */
3304 if (overlaps & OVERLAPS_PRED)
3305 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3306 if (overlaps & OVERLAPS_SUCC)
3307 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3311 if (bottom_y >= yb)
3312 break;
3316 #endif /* HAVE_WINDOW_SYSTEM */
3319 #if defined GLYPH_DEBUG && 0
3321 /* Check that no row in the current matrix of window W is enabled
3322 which is below what's displayed in the window. */
3324 static void
3325 check_current_matrix_flags (struct window *w)
3327 bool last_seen_p = 0;
3328 int i, yb = window_text_bottom_y (w);
3330 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3332 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3333 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3334 last_seen_p = 1;
3335 else if (last_seen_p && row->enabled_p)
3336 emacs_abort ();
3340 #endif /* GLYPH_DEBUG */
3343 /* Update display of window W.
3344 If FORCE_P, don't stop updating when input is pending. */
3346 static bool
3347 update_window (struct window *w, bool force_p)
3349 struct glyph_matrix *desired_matrix = w->desired_matrix;
3350 bool paused_p;
3351 int preempt_count = baud_rate / 2400 + 1;
3352 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3353 #ifdef GLYPH_DEBUG
3354 /* Check that W's frame doesn't have glyph matrices. */
3355 eassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3356 #endif
3358 /* Check pending input the first time so that we can quickly return. */
3359 if (!force_p)
3360 detect_input_pending_ignore_squeezables ();
3362 /* If forced to complete the update, or if no input is pending, do
3363 the update. */
3364 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3366 struct glyph_row *row, *end;
3367 struct glyph_row *mode_line_row;
3368 struct glyph_row *header_line_row;
3369 int yb;
3370 bool changed_p = 0, mouse_face_overwritten_p = 0;
3371 int n_updated = 0;
3373 rif->update_window_begin_hook (w);
3374 yb = window_text_bottom_y (w);
3375 row = MATRIX_ROW (desired_matrix, 0);
3376 end = MATRIX_MODE_LINE_ROW (desired_matrix);
3378 /* Take note of the header line, if there is one. We will
3379 update it below, after updating all of the window's lines. */
3380 if (row->mode_line_p)
3382 header_line_row = row;
3383 ++row;
3385 else
3386 header_line_row = NULL;
3388 /* Update the mode line, if necessary. */
3389 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3390 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3392 mode_line_row->y = yb;
3393 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3394 desired_matrix),
3395 &mouse_face_overwritten_p);
3398 /* Find first enabled row. Optimizations in redisplay_internal
3399 may lead to an update with only one row enabled. There may
3400 be also completely empty matrices. */
3401 while (row < end && !row->enabled_p)
3402 ++row;
3404 /* Try reusing part of the display by copying. */
3405 if (row < end && !desired_matrix->no_scrolling_p)
3407 int rc = scrolling_window (w, header_line_row != NULL);
3408 if (rc < 0)
3410 /* All rows were found to be equal. */
3411 paused_p = 0;
3412 goto set_cursor;
3414 else if (rc > 0)
3416 /* We've scrolled the display. */
3417 force_p = 1;
3418 changed_p = 1;
3422 /* Update the rest of the lines. */
3423 for (; row < end && (force_p || !input_pending); ++row)
3424 /* scrolling_window resets the enabled_p flag of the rows it
3425 reuses from current_matrix. */
3426 if (row->enabled_p)
3428 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3429 int i;
3431 /* We'll have to play a little bit with when to
3432 detect_input_pending. If it's done too often,
3433 scrolling large windows with repeated scroll-up
3434 commands will too quickly pause redisplay. */
3435 if (!force_p && ++n_updated % preempt_count == 0)
3436 detect_input_pending_ignore_squeezables ();
3437 changed_p |= update_window_line (w, vpos,
3438 &mouse_face_overwritten_p);
3440 /* Mark all rows below the last visible one in the current
3441 matrix as invalid. This is necessary because of
3442 variable line heights. Consider the case of three
3443 successive redisplays, where the first displays 5
3444 lines, the second 3 lines, and the third 5 lines again.
3445 If the second redisplay wouldn't mark rows in the
3446 current matrix invalid, the third redisplay might be
3447 tempted to optimize redisplay based on lines displayed
3448 in the first redisplay. */
3449 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3450 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3451 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3454 /* Was display preempted? */
3455 paused_p = row < end;
3457 set_cursor:
3459 /* Update the header line after scrolling because a new header
3460 line would otherwise overwrite lines at the top of the window
3461 that can be scrolled. */
3462 if (header_line_row && header_line_row->enabled_p)
3464 header_line_row->y = 0;
3465 update_window_line (w, 0, &mouse_face_overwritten_p);
3468 /* Fix the appearance of overlapping/overlapped rows. */
3469 if (!paused_p && !w->pseudo_window_p)
3471 #ifdef HAVE_WINDOW_SYSTEM
3472 if (changed_p && rif->fix_overlapping_area)
3474 redraw_overlapped_rows (w, yb);
3475 redraw_overlapping_rows (w, yb);
3477 #endif
3479 /* Make cursor visible at cursor position of W. */
3480 set_window_cursor_after_update (w);
3482 #if 0 /* Check that current matrix invariants are satisfied. This is
3483 for debugging only. See the comment of check_matrix_invariants. */
3484 IF_DEBUG (check_matrix_invariants (w));
3485 #endif
3488 #ifdef GLYPH_DEBUG
3489 /* Remember the redisplay method used to display the matrix. */
3490 strcpy (w->current_matrix->method, w->desired_matrix->method);
3491 #endif
3493 #ifdef HAVE_WINDOW_SYSTEM
3494 update_window_fringes (w, 0);
3495 #endif
3497 /* End the update of window W. Don't set the cursor if we
3498 paused updating the display because in this case,
3499 set_window_cursor_after_update hasn't been called, and
3500 output_cursor doesn't contain the cursor location. */
3501 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3503 else
3504 paused_p = 1;
3506 #ifdef GLYPH_DEBUG
3507 /* check_current_matrix_flags (w); */
3508 add_window_display_history (w, w->current_matrix->method, paused_p);
3509 #endif
3511 clear_glyph_matrix (desired_matrix);
3513 return paused_p;
3517 /* Update the display of area AREA in window W, row number VPOS.
3518 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3520 static void
3521 update_marginal_area (struct window *w, int area, int vpos)
3523 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3524 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3526 /* Let functions in xterm.c know what area subsequent X positions
3527 will be relative to. */
3528 updated_area = area;
3530 /* Set cursor to start of glyphs, write them, and clear to the end
3531 of the area. I don't think that something more sophisticated is
3532 necessary here, since marginal areas will not be the default. */
3533 rif->cursor_to (w, vpos, 0, desired_row->y, 0);
3534 if (desired_row->used[area])
3535 rif->write_glyphs (w, desired_row->glyphs[area], desired_row->used[area]);
3536 rif->clear_end_of_line (w, -1);
3540 /* Update the display of the text area of row VPOS in window W.
3541 Value is true if display has changed. */
3543 static bool
3544 update_text_area (struct window *w, int vpos)
3546 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3547 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3548 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3549 bool changed_p = 0;
3551 /* Let functions in xterm.c know what area subsequent X positions
3552 will be relative to. */
3553 updated_area = TEXT_AREA;
3555 /* If rows are at different X or Y, or rows have different height,
3556 or the current row is marked invalid, write the entire line. */
3557 if (!current_row->enabled_p
3558 || desired_row->y != current_row->y
3559 || desired_row->ascent != current_row->ascent
3560 || desired_row->phys_ascent != current_row->phys_ascent
3561 || desired_row->phys_height != current_row->phys_height
3562 || desired_row->visible_height != current_row->visible_height
3563 || current_row->overlapped_p
3564 /* This next line is necessary for correctly redrawing
3565 mouse-face areas after scrolling and other operations.
3566 However, it causes excessive flickering when mouse is moved
3567 across the mode line. Luckily, turning it off for the mode
3568 line doesn't seem to hurt anything. -- cyd.
3569 But it is still needed for the header line. -- kfs. */
3570 || (current_row->mouse_face_p
3571 && !(current_row->mode_line_p && vpos > 0))
3572 || current_row->x != desired_row->x)
3574 rif->cursor_to (w, vpos, 0, desired_row->y, desired_row->x);
3576 if (desired_row->used[TEXT_AREA])
3577 rif->write_glyphs (w, desired_row->glyphs[TEXT_AREA],
3578 desired_row->used[TEXT_AREA]);
3580 /* Clear to end of window. */
3581 rif->clear_end_of_line (w, -1);
3582 changed_p = 1;
3584 /* This erases the cursor. We do this here because
3585 notice_overwritten_cursor cannot easily check this, which
3586 might indicate that the whole functionality of
3587 notice_overwritten_cursor would better be implemented here.
3588 On the other hand, we need notice_overwritten_cursor as long
3589 as mouse highlighting is done asynchronously outside of
3590 redisplay. */
3591 if (vpos == w->phys_cursor.vpos)
3592 w->phys_cursor_on_p = 0;
3594 else
3596 int stop, i, x;
3597 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3598 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3599 bool overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3600 int desired_stop_pos = desired_row->used[TEXT_AREA];
3601 bool abort_skipping = 0;
3603 /* If the desired row extends its face to the text area end, and
3604 unless the current row also does so at the same position,
3605 make sure we write at least one glyph, so that the face
3606 extension actually takes place. */
3607 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3608 && (desired_stop_pos < current_row->used[TEXT_AREA]
3609 || (desired_stop_pos == current_row->used[TEXT_AREA]
3610 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3611 --desired_stop_pos;
3613 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3614 i = 0;
3615 x = desired_row->x;
3617 /* Loop over glyphs that current and desired row may have
3618 in common. */
3619 while (i < stop)
3621 bool can_skip_p = !abort_skipping;
3623 /* Skip over glyphs that both rows have in common. These
3624 don't have to be written. We can't skip if the last
3625 current glyph overlaps the glyph to its right. For
3626 example, consider a current row of `if ' with the `f' in
3627 Courier bold so that it overlaps the ` ' to its right.
3628 If the desired row is ` ', we would skip over the space
3629 after the `if' and there would remain a pixel from the
3630 `f' on the screen. */
3631 if (overlapping_glyphs_p && i > 0)
3633 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3634 int left, right;
3636 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3637 &left, &right);
3638 can_skip_p = (right == 0 && !abort_skipping);
3641 if (can_skip_p)
3643 int start_hpos = i;
3645 while (i < stop
3646 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3648 x += desired_glyph->pixel_width;
3649 ++desired_glyph, ++current_glyph, ++i;
3652 /* Consider the case that the current row contains "xxx
3653 ppp ggg" in italic Courier font, and the desired row
3654 is "xxx ggg". The character `p' has lbearing, `g'
3655 has not. The loop above will stop in front of the
3656 first `p' in the current row. If we would start
3657 writing glyphs there, we wouldn't erase the lbearing
3658 of the `p'. The rest of the lbearing problem is then
3659 taken care of by draw_glyphs. */
3660 if (overlapping_glyphs_p
3661 && i > 0
3662 && i < current_row->used[TEXT_AREA]
3663 && (current_row->used[TEXT_AREA]
3664 != desired_row->used[TEXT_AREA]))
3666 int left, right;
3668 rif->get_glyph_overhangs (current_glyph,
3669 XFRAME (w->frame),
3670 &left, &right);
3671 while (left > 0 && i > 0)
3673 --i, --desired_glyph, --current_glyph;
3674 x -= desired_glyph->pixel_width;
3675 left -= desired_glyph->pixel_width;
3678 /* Abort the skipping algorithm if we end up before
3679 our starting point, to avoid looping (bug#1070).
3680 This can happen when the lbearing is larger than
3681 the pixel width. */
3682 abort_skipping = (i < start_hpos);
3686 /* Try to avoid writing the entire rest of the desired row
3687 by looking for a resync point. This mainly prevents
3688 mode line flickering in the case the mode line is in
3689 fixed-pitch font, which it usually will be. */
3690 if (i < desired_row->used[TEXT_AREA])
3692 int start_x = x, start_hpos = i;
3693 struct glyph *start = desired_glyph;
3694 int current_x = x;
3695 bool skip_first_p = !can_skip_p;
3697 /* Find the next glyph that's equal again. */
3698 while (i < stop
3699 && (skip_first_p
3700 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3701 && x == current_x)
3703 x += desired_glyph->pixel_width;
3704 current_x += current_glyph->pixel_width;
3705 ++desired_glyph, ++current_glyph, ++i;
3706 skip_first_p = 0;
3709 if (i == start_hpos || x != current_x)
3711 i = start_hpos;
3712 x = start_x;
3713 desired_glyph = start;
3714 break;
3717 rif->cursor_to (w, vpos, start_hpos, desired_row->y, start_x);
3718 rif->write_glyphs (w, start, i - start_hpos);
3719 changed_p = 1;
3723 /* Write the rest. */
3724 if (i < desired_row->used[TEXT_AREA])
3726 rif->cursor_to (w, vpos, i, desired_row->y, x);
3727 rif->write_glyphs (w, desired_glyph, desired_row->used[TEXT_AREA] - i);
3728 changed_p = 1;
3731 /* Maybe clear to end of line. */
3732 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3734 /* If new row extends to the end of the text area, nothing
3735 has to be cleared, if and only if we did a write_glyphs
3736 above. This is made sure by setting desired_stop_pos
3737 appropriately above. */
3738 eassert (i < desired_row->used[TEXT_AREA]
3739 || ((desired_row->used[TEXT_AREA]
3740 == current_row->used[TEXT_AREA])
3741 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3743 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3745 /* If old row extends to the end of the text area, clear. */
3746 if (i >= desired_row->used[TEXT_AREA])
3747 rif->cursor_to (w, vpos, i, desired_row->y,
3748 desired_row->pixel_width);
3749 rif->clear_end_of_line (w, -1);
3750 changed_p = 1;
3752 else if (desired_row->pixel_width < current_row->pixel_width)
3754 /* Otherwise clear to the end of the old row. Everything
3755 after that position should be clear already. */
3756 int xlim;
3758 if (i >= desired_row->used[TEXT_AREA])
3759 rif->cursor_to (w, vpos, i, desired_row->y,
3760 desired_row->pixel_width);
3762 /* If cursor is displayed at the end of the line, make sure
3763 it's cleared. Nowadays we don't have a phys_cursor_glyph
3764 with which to erase the cursor (because this method
3765 doesn't work with lbearing/rbearing), so we must do it
3766 this way. */
3767 if (vpos == w->phys_cursor.vpos
3768 && (desired_row->reversed_p
3769 ? (w->phys_cursor.hpos < 0)
3770 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3772 w->phys_cursor_on_p = 0;
3773 xlim = -1;
3775 else
3776 xlim = current_row->pixel_width;
3777 rif->clear_end_of_line (w, xlim);
3778 changed_p = 1;
3782 return changed_p;
3786 /* Update row VPOS in window W. Value is true if display has been changed. */
3788 static bool
3789 update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
3791 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3792 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3793 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3794 bool changed_p = 0;
3796 /* Set the row being updated. This is important to let xterm.c
3797 know what line height values are in effect. */
3798 updated_row = desired_row;
3800 /* A row can be completely invisible in case a desired matrix was
3801 built with a vscroll and then make_cursor_line_fully_visible shifts
3802 the matrix. Make sure to make such rows current anyway, since
3803 we need the correct y-position, for example, in the current matrix. */
3804 if (desired_row->mode_line_p
3805 || desired_row->visible_height > 0)
3807 eassert (desired_row->enabled_p);
3809 /* Update display of the left margin area, if there is one. */
3810 if (!desired_row->full_width_p
3811 && !NILP (w->left_margin_cols))
3813 changed_p = 1;
3814 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
3815 /* Setting this flag will ensure the vertical border, if
3816 any, between this window and the one on its left will be
3817 redrawn. This is necessary because updating the left
3818 margin area can potentially draw over the border. */
3819 current_row->redraw_fringe_bitmaps_p = 1;
3822 /* Update the display of the text area. */
3823 if (update_text_area (w, vpos))
3825 changed_p = 1;
3826 if (current_row->mouse_face_p)
3827 *mouse_face_overwritten_p = 1;
3830 /* Update display of the right margin area, if there is one. */
3831 if (!desired_row->full_width_p
3832 && !NILP (w->right_margin_cols))
3834 changed_p = 1;
3835 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
3838 /* Draw truncation marks etc. */
3839 if (!current_row->enabled_p
3840 || desired_row->y != current_row->y
3841 || desired_row->visible_height != current_row->visible_height
3842 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
3843 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
3844 || current_row->redraw_fringe_bitmaps_p
3845 || desired_row->mode_line_p != current_row->mode_line_p
3846 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
3847 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
3848 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
3849 rif->after_update_window_line_hook (w, desired_row);
3852 /* Update current_row from desired_row. */
3853 make_current (w->desired_matrix, w->current_matrix, vpos);
3854 updated_row = NULL;
3855 return changed_p;
3859 /* Set the cursor after an update of window W. This function may only
3860 be called from update_window. */
3862 static void
3863 set_window_cursor_after_update (struct window *w)
3865 struct frame *f = XFRAME (w->frame);
3866 struct redisplay_interface *rif = FRAME_RIF (f);
3867 int cx, cy, vpos, hpos;
3869 /* Not intended for frame matrix updates. */
3870 eassert (FRAME_WINDOW_P (f));
3872 if (cursor_in_echo_area
3873 && !NILP (echo_area_buffer[0])
3874 /* If we are showing a message instead of the mini-buffer,
3875 show the cursor for the message instead. */
3876 && XWINDOW (minibuf_window) == w
3877 && EQ (minibuf_window, echo_area_window)
3878 /* These cases apply only to the frame that contains
3879 the active mini-buffer window. */
3880 && FRAME_HAS_MINIBUF_P (f)
3881 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
3883 cx = cy = vpos = hpos = 0;
3885 if (cursor_in_echo_area >= 0)
3887 /* If the mini-buffer is several lines high, find the last
3888 line that has any text on it. Note: either all lines
3889 are enabled or none. Otherwise we wouldn't be able to
3890 determine Y. */
3891 struct glyph_row *row, *last_row;
3892 struct glyph *glyph;
3893 int yb = window_text_bottom_y (w);
3895 last_row = NULL;
3896 row = w->current_matrix->rows;
3897 while (row->enabled_p
3898 && (last_row == NULL
3899 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
3901 if (row->used[TEXT_AREA]
3902 && row->glyphs[TEXT_AREA][0].charpos >= 0)
3903 last_row = row;
3904 ++row;
3907 if (last_row)
3909 struct glyph *start = last_row->glyphs[TEXT_AREA];
3910 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
3912 while (last > start && last->charpos < 0)
3913 --last;
3915 for (glyph = start; glyph < last; ++glyph)
3917 cx += glyph->pixel_width;
3918 ++hpos;
3921 cy = last_row->y;
3922 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
3926 else
3928 cx = w->cursor.x;
3929 cy = w->cursor.y;
3930 hpos = w->cursor.hpos;
3931 vpos = w->cursor.vpos;
3934 /* Window cursor can be out of sync for horizontally split windows.
3935 Horizontal position is -1 when cursor is on the left fringe. */
3936 hpos = clip_to_bounds (-1, hpos, w->current_matrix->matrix_w - 1);
3937 vpos = clip_to_bounds (0, vpos, w->current_matrix->nrows - 1);
3938 rif->cursor_to (w, vpos, hpos, cy, cx);
3942 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
3943 tree rooted at W. */
3945 void
3946 set_window_update_flags (struct window *w, bool on_p)
3948 while (w)
3950 if (WINDOWP (w->contents))
3951 set_window_update_flags (XWINDOW (w->contents), on_p);
3952 else
3953 w->must_be_updated_p = on_p;
3955 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3961 /***********************************************************************
3962 Window-Based Scrolling
3963 ***********************************************************************/
3965 /* Structure describing rows in scrolling_window. */
3967 struct row_entry
3969 /* Number of occurrences of this row in desired and current matrix. */
3970 int old_uses, new_uses;
3972 /* Vpos of row in new matrix. */
3973 int new_line_number;
3975 /* Bucket index of this row_entry in the hash table row_table. */
3976 ptrdiff_t bucket;
3978 /* The row described by this entry. */
3979 struct glyph_row *row;
3981 /* Hash collision chain. */
3982 struct row_entry *next;
3985 /* A pool to allocate row_entry structures from, and the size of the
3986 pool. The pool is reallocated in scrolling_window when we find
3987 that we need a larger one. */
3989 static struct row_entry *row_entry_pool;
3990 static ptrdiff_t row_entry_pool_size;
3992 /* Index of next free entry in row_entry_pool. */
3994 static ptrdiff_t row_entry_idx;
3996 /* The hash table used during scrolling, and the table's size. This
3997 table is used to quickly identify equal rows in the desired and
3998 current matrix. */
4000 static struct row_entry **row_table;
4001 static ptrdiff_t row_table_size;
4003 /* Vectors of pointers to row_entry structures belonging to the
4004 current and desired matrix, and the size of the vectors. */
4006 static struct row_entry **old_lines, **new_lines;
4007 static ptrdiff_t old_lines_size, new_lines_size;
4009 /* A pool to allocate run structures from, and its size. */
4011 static struct run *run_pool;
4012 static ptrdiff_t runs_size;
4014 /* A vector of runs of lines found during scrolling. */
4016 static struct run **runs;
4018 /* Add glyph row ROW to the scrolling hash table. */
4020 static struct row_entry *
4021 add_row_entry (struct glyph_row *row)
4023 struct row_entry *entry;
4024 ptrdiff_t i = row->hash % row_table_size;
4026 entry = row_table[i];
4027 eassert (entry || verify_row_hash (row));
4028 while (entry && !row_equal_p (entry->row, row, 1))
4029 entry = entry->next;
4031 if (entry == NULL)
4033 entry = row_entry_pool + row_entry_idx++;
4034 entry->row = row;
4035 entry->old_uses = entry->new_uses = 0;
4036 entry->new_line_number = 0;
4037 entry->bucket = i;
4038 entry->next = row_table[i];
4039 row_table[i] = entry;
4042 return entry;
4046 /* Try to reuse part of the current display of W by scrolling lines.
4047 HEADER_LINE_P means W has a header line.
4049 The algorithm is taken from Communications of the ACM, Apr78 "A
4050 Technique for Isolating Differences Between Files." It should take
4051 O(N) time.
4053 A short outline of the steps of the algorithm
4055 1. Skip lines equal at the start and end of both matrices.
4057 2. Enter rows in the current and desired matrix into a symbol
4058 table, counting how often they appear in both matrices.
4060 3. Rows that appear exactly once in both matrices serve as anchors,
4061 i.e. we assume that such lines are likely to have been moved.
4063 4. Starting from anchor lines, extend regions to be scrolled both
4064 forward and backward.
4066 Value is
4068 -1 if all rows were found to be equal.
4069 0 to indicate that we did not scroll the display, or
4070 1 if we did scroll. */
4072 static int
4073 scrolling_window (struct window *w, bool header_line_p)
4075 struct glyph_matrix *desired_matrix = w->desired_matrix;
4076 struct glyph_matrix *current_matrix = w->current_matrix;
4077 int yb = window_text_bottom_y (w);
4078 ptrdiff_t i;
4079 int j, first_old, first_new, last_old, last_new;
4080 int nruns, run_idx;
4081 ptrdiff_t n;
4082 struct row_entry *entry;
4083 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4085 /* Skip over rows equal at the start. */
4086 for (i = header_line_p; i < current_matrix->nrows - 1; ++i)
4088 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4089 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4091 if (c->enabled_p
4092 && d->enabled_p
4093 && !d->redraw_fringe_bitmaps_p
4094 && c->y == d->y
4095 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4096 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4097 && row_equal_p (c, d, 1))
4099 assign_row (c, d);
4100 d->enabled_p = 0;
4102 else
4103 break;
4106 /* Give up if some rows in the desired matrix are not enabled. */
4107 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4108 return -1;
4110 first_old = first_new = i;
4112 /* Set last_new to the index + 1 of the row that reaches the
4113 bottom boundary in the desired matrix. Give up if we find a
4114 disabled row before we reach the bottom boundary. */
4115 i = first_new + 1;
4116 while (i < desired_matrix->nrows - 1)
4118 int bottom;
4120 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4121 return 0;
4122 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4123 if (bottom <= yb)
4124 ++i;
4125 if (bottom >= yb)
4126 break;
4129 last_new = i;
4131 /* Set last_old to the index + 1 of the row that reaches the bottom
4132 boundary in the current matrix. We don't look at the enabled
4133 flag here because we plan to reuse part of the display even if
4134 other parts are disabled. */
4135 i = first_old + 1;
4136 while (i < current_matrix->nrows - 1)
4138 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4139 if (bottom <= yb)
4140 ++i;
4141 if (bottom >= yb)
4142 break;
4145 last_old = i;
4147 /* Skip over rows equal at the bottom. */
4148 i = last_new;
4149 j = last_old;
4150 while (i - 1 > first_new
4151 && j - 1 > first_old
4152 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4153 && (MATRIX_ROW (current_matrix, j - 1)->y
4154 == MATRIX_ROW (desired_matrix, i - 1)->y)
4155 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4156 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4157 MATRIX_ROW (current_matrix, j - 1), 1))
4158 --i, --j;
4159 last_new = i;
4160 last_old = j;
4162 /* Nothing to do if all rows are equal. */
4163 if (last_new == first_new)
4164 return 0;
4166 /* Check for integer overflow in size calculation.
4168 If next_almost_prime checks (N) for divisibility by 2..10, then
4169 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4170 So, set next_almost_prime_increment_max to 10.
4172 It's just a coincidence that next_almost_prime_increment_max ==
4173 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4174 13, then next_almost_prime_increment_max would be 14, e.g.,
4175 because next_almost_prime (113) would be 127. */
4177 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4178 enum { next_almost_prime_increment_max = 10 };
4179 ptrdiff_t row_table_max =
4180 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4181 - next_almost_prime_increment_max);
4182 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4183 if (current_nrows_max < current_matrix->nrows)
4184 memory_full (SIZE_MAX);
4187 /* Reallocate vectors, tables etc. if necessary. */
4189 if (current_matrix->nrows > old_lines_size)
4190 old_lines = xpalloc (old_lines, &old_lines_size,
4191 current_matrix->nrows - old_lines_size,
4192 INT_MAX, sizeof *old_lines);
4194 if (desired_matrix->nrows > new_lines_size)
4195 new_lines = xpalloc (new_lines, &new_lines_size,
4196 desired_matrix->nrows - new_lines_size,
4197 INT_MAX, sizeof *new_lines);
4199 n = desired_matrix->nrows;
4200 n += current_matrix->nrows;
4201 if (row_table_size < 3 * n)
4203 ptrdiff_t size = next_almost_prime (3 * n);
4204 row_table = xnrealloc (row_table, size, sizeof *row_table);
4205 row_table_size = size;
4206 memset (row_table, 0, size * sizeof *row_table);
4209 if (n > row_entry_pool_size)
4210 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4211 n - row_entry_pool_size,
4212 -1, sizeof *row_entry_pool);
4214 if (desired_matrix->nrows > runs_size)
4216 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4217 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4218 runs_size = desired_matrix->nrows;
4221 nruns = run_idx = 0;
4222 row_entry_idx = 0;
4224 /* Add rows from the current and desired matrix to the hash table
4225 row_hash_table to be able to find equal ones quickly. */
4227 for (i = first_old; i < last_old; ++i)
4229 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4231 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4232 old_lines[i] = entry;
4233 ++entry->old_uses;
4235 else
4236 old_lines[i] = NULL;
4239 for (i = first_new; i < last_new; ++i)
4241 eassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4242 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4243 ++entry->new_uses;
4244 entry->new_line_number = i;
4245 new_lines[i] = entry;
4248 /* Identify moves based on lines that are unique and equal
4249 in both matrices. */
4250 for (i = first_old; i < last_old;)
4251 if (old_lines[i]
4252 && old_lines[i]->old_uses == 1
4253 && old_lines[i]->new_uses == 1)
4255 int p, q;
4256 int new_line = old_lines[i]->new_line_number;
4257 struct run *run = run_pool + run_idx++;
4259 /* Record move. */
4260 run->current_vpos = i;
4261 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4262 run->desired_vpos = new_line;
4263 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4264 run->nrows = 1;
4265 run->height = MATRIX_ROW (current_matrix, i)->height;
4267 /* Extend backward. */
4268 p = i - 1;
4269 q = new_line - 1;
4270 while (p > first_old
4271 && q > first_new
4272 && old_lines[p] == new_lines[q])
4274 int h = MATRIX_ROW (current_matrix, p)->height;
4275 --run->current_vpos;
4276 --run->desired_vpos;
4277 ++run->nrows;
4278 run->height += h;
4279 run->desired_y -= h;
4280 run->current_y -= h;
4281 --p, --q;
4284 /* Extend forward. */
4285 p = i + 1;
4286 q = new_line + 1;
4287 while (p < last_old
4288 && q < last_new
4289 && old_lines[p] == new_lines[q])
4291 int h = MATRIX_ROW (current_matrix, p)->height;
4292 ++run->nrows;
4293 run->height += h;
4294 ++p, ++q;
4297 /* Insert run into list of all runs. Order runs by copied
4298 pixel lines. Note that we record runs that don't have to
4299 be copied because they are already in place. This is done
4300 because we can avoid calling update_window_line in this
4301 case. */
4302 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4304 for (q = nruns; q > p; --q)
4305 runs[q] = runs[q - 1];
4306 runs[p] = run;
4307 ++nruns;
4309 i += run->nrows;
4311 else
4312 ++i;
4314 /* Do the moves. Do it in a way that we don't overwrite something
4315 we want to copy later on. This is not solvable in general
4316 because there is only one display and we don't have a way to
4317 exchange areas on this display. Example:
4319 +-----------+ +-----------+
4320 | A | | B |
4321 +-----------+ --> +-----------+
4322 | B | | A |
4323 +-----------+ +-----------+
4325 Instead, prefer bigger moves, and invalidate moves that would
4326 copy from where we copied to. */
4328 for (i = 0; i < nruns; ++i)
4329 if (runs[i]->nrows > 0)
4331 struct run *r = runs[i];
4333 /* Copy on the display. */
4334 if (r->current_y != r->desired_y)
4336 rif->clear_window_mouse_face (w);
4337 rif->scroll_run_hook (w, r);
4340 /* Truncate runs that copy to where we copied to, and
4341 invalidate runs that copy from where we copied to. */
4342 for (j = nruns - 1; j > i; --j)
4344 struct run *p = runs[j];
4345 bool truncated_p = 0;
4347 if (p->nrows > 0
4348 && p->desired_y < r->desired_y + r->height
4349 && p->desired_y + p->height > r->desired_y)
4351 if (p->desired_y < r->desired_y)
4353 p->nrows = r->desired_vpos - p->desired_vpos;
4354 p->height = r->desired_y - p->desired_y;
4355 truncated_p = 1;
4357 else
4359 int nrows_copied = (r->desired_vpos + r->nrows
4360 - p->desired_vpos);
4362 if (p->nrows <= nrows_copied)
4363 p->nrows = 0;
4364 else
4366 int height_copied = (r->desired_y + r->height
4367 - p->desired_y);
4369 p->current_vpos += nrows_copied;
4370 p->desired_vpos += nrows_copied;
4371 p->nrows -= nrows_copied;
4372 p->current_y += height_copied;
4373 p->desired_y += height_copied;
4374 p->height -= height_copied;
4375 truncated_p = 1;
4380 if (r->current_y != r->desired_y
4381 /* The condition below is equivalent to
4382 ((p->current_y >= r->desired_y
4383 && p->current_y < r->desired_y + r->height)
4384 || (p->current_y + p->height > r->desired_y
4385 && (p->current_y + p->height
4386 <= r->desired_y + r->height)))
4387 because we have 0 < p->height <= r->height. */
4388 && p->current_y < r->desired_y + r->height
4389 && p->current_y + p->height > r->desired_y)
4390 p->nrows = 0;
4392 /* Reorder runs by copied pixel lines if truncated. */
4393 if (truncated_p && p->nrows > 0)
4395 int k = nruns - 1;
4397 while (runs[k]->nrows == 0 || runs[k]->height < p->height)
4398 k--;
4399 memmove (runs + j, runs + j + 1, (k - j) * sizeof (*runs));
4400 runs[k] = p;
4404 /* Assign matrix rows. */
4405 for (j = 0; j < r->nrows; ++j)
4407 struct glyph_row *from, *to;
4408 bool to_overlapped_p;
4410 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4411 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4412 to_overlapped_p = to->overlapped_p;
4413 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4414 assign_row (to, from);
4415 /* The above `assign_row' actually does swap, so if we had
4416 an overlap in the copy destination of two runs, then
4417 the second run would assign a previously disabled bogus
4418 row. But thanks to the truncation code in the
4419 preceding for-loop, we no longer have such an overlap,
4420 and thus the assigned row should always be enabled. */
4421 eassert (to->enabled_p);
4422 from->enabled_p = 0;
4423 to->overlapped_p = to_overlapped_p;
4427 /* Clear the hash table, for the next time. */
4428 for (i = 0; i < row_entry_idx; ++i)
4429 row_table[row_entry_pool[i].bucket] = NULL;
4431 /* Value is 1 to indicate that we scrolled the display. */
4432 return nruns > 0;
4437 /************************************************************************
4438 Frame-Based Updates
4439 ************************************************************************/
4441 /* Update the desired frame matrix of frame F.
4443 FORCE_P means that the update should not be stopped by pending input.
4444 INHIBIT_HAIRY_ID_P means that scrolling should not be tried.
4446 Value is true if update was stopped due to pending input. */
4448 static bool
4449 update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p)
4451 /* Frame matrices to work on. */
4452 struct glyph_matrix *current_matrix = f->current_matrix;
4453 struct glyph_matrix *desired_matrix = f->desired_matrix;
4454 int i;
4455 bool pause_p;
4456 int preempt_count = baud_rate / 2400 + 1;
4458 eassert (current_matrix && desired_matrix);
4460 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4461 calculate_costs (f);
4463 if (preempt_count <= 0)
4464 preempt_count = 1;
4466 if (!force_p && detect_input_pending_ignore_squeezables ())
4468 pause_p = 1;
4469 goto do_pause;
4472 /* If we cannot insert/delete lines, it's no use trying it. */
4473 if (!FRAME_LINE_INS_DEL_OK (f))
4474 inhibit_id_p = 1;
4476 /* See if any of the desired lines are enabled; don't compute for
4477 i/d line if just want cursor motion. */
4478 for (i = 0; i < desired_matrix->nrows; i++)
4479 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4480 break;
4482 /* Try doing i/d line, if not yet inhibited. */
4483 if (!inhibit_id_p && i < desired_matrix->nrows)
4484 force_p |= scrolling (f);
4486 /* Update the individual lines as needed. Do bottom line first. */
4487 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4488 update_frame_line (f, desired_matrix->nrows - 1);
4490 /* Now update the rest of the lines. */
4491 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4493 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4495 if (FRAME_TERMCAP_P (f))
4497 /* Flush out every so many lines.
4498 Also flush out if likely to have more than 1k buffered
4499 otherwise. I'm told that some telnet connections get
4500 really screwed by more than 1k output at once. */
4501 FILE *display_output = FRAME_TTY (f)->output;
4502 if (display_output)
4504 ptrdiff_t outq = __fpending (display_output);
4505 if (outq > 900
4506 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4507 fflush (display_output);
4511 if (!force_p && (i - 1) % preempt_count == 0)
4512 detect_input_pending_ignore_squeezables ();
4514 update_frame_line (f, i);
4518 lint_assume (0 <= FRAME_LINES (f));
4519 pause_p = 0 < i && i < FRAME_LINES (f) - 1;
4521 /* Now just clean up termcap drivers and set cursor, etc. */
4522 if (!pause_p)
4524 if ((cursor_in_echo_area
4525 /* If we are showing a message instead of the mini-buffer,
4526 show the cursor for the message instead of for the
4527 (now hidden) mini-buffer contents. */
4528 || (EQ (minibuf_window, selected_window)
4529 && EQ (minibuf_window, echo_area_window)
4530 && !NILP (echo_area_buffer[0])))
4531 /* These cases apply only to the frame that contains
4532 the active mini-buffer window. */
4533 && FRAME_HAS_MINIBUF_P (f)
4534 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4536 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4537 int row, col;
4539 if (cursor_in_echo_area < 0)
4541 /* Negative value of cursor_in_echo_area means put
4542 cursor at beginning of line. */
4543 row = top;
4544 col = 0;
4546 else
4548 /* Positive value of cursor_in_echo_area means put
4549 cursor at the end of the prompt. If the mini-buffer
4550 is several lines high, find the last line that has
4551 any text on it. */
4552 row = FRAME_LINES (f);
4555 --row;
4556 col = 0;
4558 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4560 /* Frame rows are filled up with spaces that
4561 must be ignored here. */
4562 struct glyph_row *r = MATRIX_ROW (current_matrix,
4563 row);
4564 struct glyph *start = r->glyphs[TEXT_AREA];
4565 struct glyph *last = start + r->used[TEXT_AREA];
4567 while (last > start
4568 && (last - 1)->charpos < 0)
4569 --last;
4571 col = last - start;
4574 while (row > top && col == 0);
4576 /* Make sure COL is not out of range. */
4577 if (col >= FRAME_CURSOR_X_LIMIT (f))
4579 /* If we have another row, advance cursor into it. */
4580 if (row < FRAME_LINES (f) - 1)
4582 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4583 row++;
4585 /* Otherwise move it back in range. */
4586 else
4587 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4591 cursor_to (f, row, col);
4593 else
4595 /* We have only one cursor on terminal frames. Use it to
4596 display the cursor of the selected window. */
4597 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4598 if (w->cursor.vpos >= 0
4599 /* The cursor vpos may be temporarily out of bounds
4600 in the following situation: There is one window,
4601 with the cursor in the lower half of it. The window
4602 is split, and a message causes a redisplay before
4603 a new cursor position has been computed. */
4604 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4606 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4607 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4609 if (INTEGERP (w->left_margin_cols))
4610 x += XFASTINT (w->left_margin_cols);
4612 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4613 cursor_to (f, y, x);
4618 do_pause:
4620 clear_desired_matrices (f);
4621 return pause_p;
4625 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4627 static bool
4628 scrolling (struct frame *frame)
4630 int unchanged_at_top, unchanged_at_bottom;
4631 int window_size;
4632 int changed_lines;
4633 int *old_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4634 int *new_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4635 int *draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4636 int *old_draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4637 register int i;
4638 int free_at_end_vpos = FRAME_LINES (frame);
4639 struct glyph_matrix *current_matrix = frame->current_matrix;
4640 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4642 if (!current_matrix)
4643 emacs_abort ();
4645 /* Compute hash codes of all the lines. Also calculate number of
4646 changed lines, number of unchanged lines at the beginning, and
4647 number of unchanged lines at the end. */
4648 changed_lines = 0;
4649 unchanged_at_top = 0;
4650 unchanged_at_bottom = FRAME_LINES (frame);
4651 for (i = 0; i < FRAME_LINES (frame); i++)
4653 /* Give up on this scrolling if some old lines are not enabled. */
4654 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4655 return 0;
4656 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4657 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4659 /* This line cannot be redrawn, so don't let scrolling mess it. */
4660 new_hash[i] = old_hash[i];
4661 #define INFINITY 1000000 /* Taken from scroll.c */
4662 draw_cost[i] = INFINITY;
4664 else
4666 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4667 draw_cost[i] = line_draw_cost (desired_matrix, i);
4670 if (old_hash[i] != new_hash[i])
4672 changed_lines++;
4673 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4675 else if (i == unchanged_at_top)
4676 unchanged_at_top++;
4677 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4680 /* If changed lines are few, don't allow preemption, don't scroll. */
4681 if ((!FRAME_SCROLL_REGION_OK (frame)
4682 && changed_lines < baud_rate / 2400)
4683 || unchanged_at_bottom == FRAME_LINES (frame))
4684 return 1;
4686 window_size = (FRAME_LINES (frame) - unchanged_at_top
4687 - unchanged_at_bottom);
4689 if (FRAME_SCROLL_REGION_OK (frame))
4690 free_at_end_vpos -= unchanged_at_bottom;
4691 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4692 free_at_end_vpos = -1;
4694 /* If large window, fast terminal and few lines in common between
4695 current frame and desired frame, don't bother with i/d calc. */
4696 if (!FRAME_SCROLL_REGION_OK (frame)
4697 && window_size >= 18 && baud_rate > 2400
4698 && (window_size >=
4699 10 * scrolling_max_lines_saved (unchanged_at_top,
4700 FRAME_LINES (frame) - unchanged_at_bottom,
4701 old_hash, new_hash, draw_cost)))
4702 return 0;
4704 if (window_size < 2)
4705 return 0;
4707 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4708 draw_cost + unchanged_at_top - 1,
4709 old_draw_cost + unchanged_at_top - 1,
4710 old_hash + unchanged_at_top - 1,
4711 new_hash + unchanged_at_top - 1,
4712 free_at_end_vpos - unchanged_at_top);
4714 return 0;
4718 /* Count the number of blanks at the start of the vector of glyphs R
4719 which is LEN glyphs long. */
4721 static int
4722 count_blanks (struct glyph *r, int len)
4724 int i;
4726 for (i = 0; i < len; ++i)
4727 if (!CHAR_GLYPH_SPACE_P (r[i]))
4728 break;
4730 return i;
4734 /* Count the number of glyphs in common at the start of the glyph
4735 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4736 of STR2. Value is the number of equal glyphs equal at the start. */
4738 static int
4739 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4741 struct glyph *p1 = str1;
4742 struct glyph *p2 = str2;
4744 while (p1 < end1
4745 && p2 < end2
4746 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4747 ++p1, ++p2;
4749 return p1 - str1;
4753 /* Char insertion/deletion cost vector, from term.c */
4755 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4758 /* Perform a frame-based update on line VPOS in frame FRAME. */
4760 static void
4761 update_frame_line (struct frame *f, int vpos)
4763 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4764 int tem;
4765 int osp, nsp, begmatch, endmatch, olen, nlen;
4766 struct glyph_matrix *current_matrix = f->current_matrix;
4767 struct glyph_matrix *desired_matrix = f->desired_matrix;
4768 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4769 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4770 bool must_write_whole_line_p;
4771 bool write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4772 bool colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4773 != FACE_TTY_DEFAULT_BG_COLOR);
4775 if (colored_spaces_p)
4776 write_spaces_p = 1;
4778 /* Current row not enabled means it has unknown contents. We must
4779 write the whole desired line in that case. */
4780 must_write_whole_line_p = !current_row->enabled_p;
4781 if (must_write_whole_line_p)
4783 obody = 0;
4784 olen = 0;
4786 else
4788 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4789 olen = current_row->used[TEXT_AREA];
4791 /* Ignore trailing spaces, if we can. */
4792 if (!write_spaces_p)
4793 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4794 olen--;
4797 current_row->enabled_p = 1;
4798 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4800 /* If desired line is empty, just clear the line. */
4801 if (!desired_row->enabled_p)
4803 nlen = 0;
4804 goto just_erase;
4807 nbody = desired_row->glyphs[TEXT_AREA];
4808 nlen = desired_row->used[TEXT_AREA];
4809 nend = nbody + nlen;
4811 /* If display line has unknown contents, write the whole line. */
4812 if (must_write_whole_line_p)
4814 /* Ignore spaces at the end, if we can. */
4815 if (!write_spaces_p)
4816 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4817 --nlen;
4819 /* Write the contents of the desired line. */
4820 if (nlen)
4822 cursor_to (f, vpos, 0);
4823 write_glyphs (f, nbody, nlen);
4826 /* Don't call clear_end_of_line if we already wrote the whole
4827 line. The cursor will not be at the right margin in that
4828 case but in the line below. */
4829 if (nlen < FRAME_TOTAL_COLS (f))
4831 cursor_to (f, vpos, nlen);
4832 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
4834 else
4835 /* Make sure we are in the right row, otherwise cursor movement
4836 with cmgoto might use `ch' in the wrong row. */
4837 cursor_to (f, vpos, 0);
4839 make_current (desired_matrix, current_matrix, vpos);
4840 return;
4843 /* Pretend trailing spaces are not there at all,
4844 unless for one reason or another we must write all spaces. */
4845 if (!write_spaces_p)
4846 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4847 nlen--;
4849 /* If there's no i/d char, quickly do the best we can without it. */
4850 if (!FRAME_CHAR_INS_DEL_OK (f))
4852 int i, j;
4854 /* Find the first glyph in desired row that doesn't agree with
4855 a glyph in the current row, and write the rest from there on. */
4856 for (i = 0; i < nlen; i++)
4858 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
4860 /* Find the end of the run of different glyphs. */
4861 j = i + 1;
4862 while (j < nlen
4863 && (j >= olen
4864 || !GLYPH_EQUAL_P (nbody + j, obody + j)
4865 || CHAR_GLYPH_PADDING_P (nbody[j])))
4866 ++j;
4868 /* Output this run of non-matching chars. */
4869 cursor_to (f, vpos, i);
4870 write_glyphs (f, nbody + i, j - i);
4871 i = j - 1;
4873 /* Now find the next non-match. */
4877 /* Clear the rest of the line, or the non-clear part of it. */
4878 if (olen > nlen)
4880 cursor_to (f, vpos, nlen);
4881 clear_end_of_line (f, olen);
4884 /* Make current row = desired row. */
4885 make_current (desired_matrix, current_matrix, vpos);
4886 return;
4889 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
4890 characters in a row. */
4892 if (!olen)
4894 /* If current line is blank, skip over initial spaces, if
4895 possible, and write the rest. */
4896 if (write_spaces_p)
4897 nsp = 0;
4898 else
4899 nsp = count_blanks (nbody, nlen);
4901 if (nlen > nsp)
4903 cursor_to (f, vpos, nsp);
4904 write_glyphs (f, nbody + nsp, nlen - nsp);
4907 /* Exchange contents between current_frame and new_frame. */
4908 make_current (desired_matrix, current_matrix, vpos);
4909 return;
4912 /* Compute number of leading blanks in old and new contents. */
4913 osp = count_blanks (obody, olen);
4914 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
4916 /* Compute number of matching chars starting with first non-blank. */
4917 begmatch = count_match (obody + osp, obody + olen,
4918 nbody + nsp, nbody + nlen);
4920 /* Spaces in new match implicit space past the end of old. */
4921 /* A bug causing this to be a no-op was fixed in 18.29. */
4922 if (!write_spaces_p && osp + begmatch == olen)
4924 np1 = nbody + nsp;
4925 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
4926 ++begmatch;
4929 /* Avoid doing insert/delete char
4930 just cause number of leading spaces differs
4931 when the following text does not match. */
4932 if (begmatch == 0 && osp != nsp)
4933 osp = nsp = min (osp, nsp);
4935 /* Find matching characters at end of line */
4936 op1 = obody + olen;
4937 np1 = nbody + nlen;
4938 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
4939 while (op1 > op2
4940 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
4942 op1--;
4943 np1--;
4945 endmatch = obody + olen - op1;
4947 /* tem gets the distance to insert or delete.
4948 endmatch is how many characters we save by doing so.
4949 Is it worth it? */
4951 tem = (nlen - nsp) - (olen - osp);
4952 if (endmatch && tem
4953 && (!FRAME_CHAR_INS_DEL_OK (f)
4954 || endmatch <= char_ins_del_cost (f)[tem]))
4955 endmatch = 0;
4957 /* nsp - osp is the distance to insert or delete.
4958 If that is nonzero, begmatch is known to be nonzero also.
4959 begmatch + endmatch is how much we save by doing the ins/del.
4960 Is it worth it? */
4962 if (nsp != osp
4963 && (!FRAME_CHAR_INS_DEL_OK (f)
4964 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
4966 begmatch = 0;
4967 endmatch = 0;
4968 osp = nsp = min (osp, nsp);
4971 /* Now go through the line, inserting, writing and
4972 deleting as appropriate. */
4974 if (osp > nsp)
4976 cursor_to (f, vpos, nsp);
4977 delete_glyphs (f, osp - nsp);
4979 else if (nsp > osp)
4981 /* If going to delete chars later in line
4982 and insert earlier in the line,
4983 must delete first to avoid losing data in the insert */
4984 if (endmatch && nlen < olen + nsp - osp)
4986 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
4987 delete_glyphs (f, olen + nsp - osp - nlen);
4988 olen = nlen - (nsp - osp);
4990 cursor_to (f, vpos, osp);
4991 insert_glyphs (f, 0, nsp - osp);
4993 olen += nsp - osp;
4995 tem = nsp + begmatch + endmatch;
4996 if (nlen != tem || olen != tem)
4998 if (!endmatch || nlen == olen)
5000 /* If new text being written reaches right margin, there is
5001 no need to do clear-to-eol at the end of this function
5002 (and it would not be safe, since cursor is not going to
5003 be "at the margin" after the text is done). */
5004 if (nlen == FRAME_TOTAL_COLS (f))
5005 olen = 0;
5007 /* Function write_glyphs is prepared to do nothing
5008 if passed a length <= 0. Check it here to avoid
5009 unnecessary cursor movement. */
5010 if (nlen - tem > 0)
5012 cursor_to (f, vpos, nsp + begmatch);
5013 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5016 else if (nlen > olen)
5018 /* Here, we used to have the following simple code:
5019 ----------------------------------------
5020 write_glyphs (nbody + nsp + begmatch, olen - tem);
5021 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5022 ----------------------------------------
5023 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5024 is a padding glyph. */
5025 int out = olen - tem; /* Columns to be overwritten originally. */
5026 int del;
5028 cursor_to (f, vpos, nsp + begmatch);
5030 /* Calculate columns we can actually overwrite. */
5031 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5032 out--;
5033 write_glyphs (f, nbody + nsp + begmatch, out);
5035 /* If we left columns to be overwritten, we must delete them. */
5036 del = olen - tem - out;
5037 if (del > 0)
5038 delete_glyphs (f, del);
5040 /* At last, we insert columns not yet written out. */
5041 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5042 olen = nlen;
5044 else if (olen > nlen)
5046 cursor_to (f, vpos, nsp + begmatch);
5047 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5048 delete_glyphs (f, olen - nlen);
5049 olen = nlen;
5053 just_erase:
5054 /* If any unerased characters remain after the new line, erase them. */
5055 if (olen > nlen)
5057 cursor_to (f, vpos, nlen);
5058 clear_end_of_line (f, olen);
5061 /* Exchange contents between current_frame and new_frame. */
5062 make_current (desired_matrix, current_matrix, vpos);
5067 /***********************************************************************
5068 X/Y Position -> Buffer Position
5069 ***********************************************************************/
5071 /* Determine what's under window-relative pixel position (*X, *Y).
5072 Return the OBJECT (string or buffer) that's there.
5073 Return in *POS the position in that object.
5074 Adjust *X and *Y to character positions.
5075 Return in *DX and *DY the pixel coordinates of the click,
5076 relative to the top left corner of OBJECT, or relative to
5077 the top left corner of the character glyph at (*X, *Y)
5078 if OBJECT is nil.
5079 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5080 if the coordinates point to an empty area of the display. */
5082 Lisp_Object
5083 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)
5085 struct it it;
5086 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5087 struct text_pos startp;
5088 Lisp_Object string;
5089 struct glyph_row *row;
5090 #ifdef HAVE_WINDOW_SYSTEM
5091 struct image *img = 0;
5092 #endif
5093 int x0, x1, to_x;
5094 void *itdata = NULL;
5096 /* We used to set current_buffer directly here, but that does the
5097 wrong thing with `face-remapping-alist' (bug#2044). */
5098 Fset_buffer (w->contents);
5099 itdata = bidi_shelve_cache ();
5100 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5101 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5102 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5103 start_display (&it, w, startp);
5104 /* start_display takes into account the header-line row, but IT's
5105 vpos still counts from the glyph row that includes the window's
5106 start position. Adjust for a possible header-line row. */
5107 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w);
5109 x0 = *x;
5111 /* First, move to the beginning of the row corresponding to *Y. We
5112 need to be in that row to get the correct value of base paragraph
5113 direction for the text at (*X, *Y). */
5114 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5116 /* TO_X is the pixel position that the iterator will compute for the
5117 glyph at *X. We add it.first_visible_x because iterator
5118 positions include the hscroll. */
5119 to_x = x0 + it.first_visible_x;
5120 if (it.bidi_it.paragraph_dir == R2L)
5121 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5122 text area. This is because the iterator, even in R2L
5123 paragraphs, delivers glyphs as if they started at the left
5124 margin of the window. (When we actually produce glyphs for
5125 display, we reverse their order in PRODUCE_GLYPHS, but the
5126 iterator doesn't know about that.) The following line adjusts
5127 the pixel position to the iterator geometry, which is what
5128 move_it_* routines use. (The -1 is because in a window whose
5129 text-area width is W, the rightmost pixel position is W-1, and
5130 it should be mirrored into zero pixel position.) */
5131 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5133 /* Now move horizontally in the row to the glyph under *X. Second
5134 argument is ZV to prevent move_it_in_display_line from matching
5135 based on buffer positions. */
5136 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5137 bidi_unshelve_cache (itdata, 0);
5139 Fset_buffer (old_current_buffer);
5141 *dx = x0 + it.first_visible_x - it.current_x;
5142 *dy = *y - it.current_y;
5144 string = w->contents;
5145 if (STRINGP (it.string))
5146 string = it.string;
5147 *pos = it.current;
5148 if (it.what == IT_COMPOSITION
5149 && it.cmp_it.nchars > 1
5150 && it.cmp_it.reversed_p)
5152 /* The current display element is a grapheme cluster in a
5153 composition. In that case, we need the position of the first
5154 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5155 it.current points to the last character of the cluster, thus
5156 we must move back to the first character of the same
5157 cluster. */
5158 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5159 if (STRINGP (it.string))
5160 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5161 else
5162 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->contents),
5163 CHARPOS (pos->pos));
5166 #ifdef HAVE_WINDOW_SYSTEM
5167 if (it.what == IT_IMAGE)
5169 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5170 && !NILP (img->spec))
5171 *object = img->spec;
5173 #endif
5175 if (it.vpos < w->current_matrix->nrows
5176 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5177 row->enabled_p))
5179 if (it.hpos < row->used[TEXT_AREA])
5181 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5182 #ifdef HAVE_WINDOW_SYSTEM
5183 if (img)
5185 *dy -= row->ascent - glyph->ascent;
5186 *dx += glyph->slice.img.x;
5187 *dy += glyph->slice.img.y;
5188 /* Image slices positions are still relative to the entire image */
5189 *width = img->width;
5190 *height = img->height;
5192 else
5193 #endif
5195 *width = glyph->pixel_width;
5196 *height = glyph->ascent + glyph->descent;
5199 else
5201 *width = 0;
5202 *height = row->height;
5205 else
5207 *width = *height = 0;
5210 /* Add extra (default width) columns if clicked after EOL. */
5211 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5212 if (x0 > x1)
5213 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5215 *x = it.hpos;
5216 *y = it.vpos;
5218 return string;
5222 /* Value is the string under window-relative coordinates X/Y in the
5223 mode line or header line (PART says which) of window W, or nil if none.
5224 *CHARPOS is set to the position in the string returned. */
5226 Lisp_Object
5227 mode_line_string (struct window *w, enum window_part part,
5228 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5229 int *dx, int *dy, int *width, int *height)
5231 struct glyph_row *row;
5232 struct glyph *glyph, *end;
5233 int x0, y0;
5234 Lisp_Object string = Qnil;
5236 if (part == ON_MODE_LINE)
5237 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5238 else
5239 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5240 y0 = *y - row->y;
5241 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5243 if (row->mode_line_p && row->enabled_p)
5245 /* Find the glyph under X. If we find one with a string object,
5246 it's the one we were looking for. */
5247 glyph = row->glyphs[TEXT_AREA];
5248 end = glyph + row->used[TEXT_AREA];
5249 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5250 x0 -= glyph->pixel_width;
5251 *x = glyph - row->glyphs[TEXT_AREA];
5252 if (glyph < end)
5254 string = glyph->object;
5255 *charpos = glyph->charpos;
5256 *width = glyph->pixel_width;
5257 *height = glyph->ascent + glyph->descent;
5258 #ifdef HAVE_WINDOW_SYSTEM
5259 if (glyph->type == IMAGE_GLYPH)
5261 struct image *img;
5262 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5263 if (img != NULL)
5264 *object = img->spec;
5265 y0 -= row->ascent - glyph->ascent;
5267 #endif
5269 else
5271 /* Add extra (default width) columns if clicked after EOL. */
5272 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5273 *width = 0;
5274 *height = row->height;
5277 else
5279 *x = 0;
5280 x0 = 0;
5281 *width = *height = 0;
5284 *dx = x0;
5285 *dy = y0;
5287 return string;
5291 /* Value is the string under window-relative coordinates X/Y in either
5292 marginal area, or nil if none. *CHARPOS is set to the position in
5293 the string returned. */
5295 Lisp_Object
5296 marginal_area_string (struct window *w, enum window_part part,
5297 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5298 int *dx, int *dy, int *width, int *height)
5300 struct glyph_row *row = w->current_matrix->rows;
5301 struct glyph *glyph, *end;
5302 int x0, y0, i, wy = *y;
5303 int area;
5304 Lisp_Object string = Qnil;
5306 if (part == ON_LEFT_MARGIN)
5307 area = LEFT_MARGIN_AREA;
5308 else if (part == ON_RIGHT_MARGIN)
5309 area = RIGHT_MARGIN_AREA;
5310 else
5311 emacs_abort ();
5313 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5314 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5315 break;
5316 y0 = *y - row->y;
5317 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5319 if (row->enabled_p)
5321 /* Find the glyph under X. If we find one with a string object,
5322 it's the one we were looking for. */
5323 if (area == RIGHT_MARGIN_AREA)
5324 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5325 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5326 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5327 + window_box_width (w, LEFT_MARGIN_AREA)
5328 + window_box_width (w, TEXT_AREA));
5329 else
5330 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5331 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5332 : 0);
5334 glyph = row->glyphs[area];
5335 end = glyph + row->used[area];
5336 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5337 x0 -= glyph->pixel_width;
5338 *x = glyph - row->glyphs[area];
5339 if (glyph < end)
5341 string = glyph->object;
5342 *charpos = glyph->charpos;
5343 *width = glyph->pixel_width;
5344 *height = glyph->ascent + glyph->descent;
5345 #ifdef HAVE_WINDOW_SYSTEM
5346 if (glyph->type == IMAGE_GLYPH)
5348 struct image *img;
5349 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5350 if (img != NULL)
5351 *object = img->spec;
5352 y0 -= row->ascent - glyph->ascent;
5353 x0 += glyph->slice.img.x;
5354 y0 += glyph->slice.img.y;
5356 #endif
5358 else
5360 /* Add extra (default width) columns if clicked after EOL. */
5361 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5362 *width = 0;
5363 *height = row->height;
5366 else
5368 x0 = 0;
5369 *x = 0;
5370 *width = *height = 0;
5373 *dx = x0;
5374 *dy = y0;
5376 return string;
5380 /***********************************************************************
5381 Changing Frame Sizes
5382 ***********************************************************************/
5384 #ifdef SIGWINCH
5386 static void deliver_window_change_signal (int);
5388 static void
5389 handle_window_change_signal (int sig)
5391 int width, height;
5392 struct tty_display_info *tty;
5394 /* The frame size change obviously applies to a single
5395 termcap-controlled terminal, but we can't decide which.
5396 Therefore, we resize the frames corresponding to each tty.
5398 for (tty = tty_list; tty; tty = tty->next) {
5400 if (! tty->term_initted)
5401 continue;
5403 /* Suspended tty frames have tty->input == NULL avoid trying to
5404 use it. */
5405 if (!tty->input)
5406 continue;
5408 get_tty_size (fileno (tty->input), &width, &height);
5410 if (width > 5 && height > 2) {
5411 Lisp_Object tail, frame;
5413 FOR_EACH_FRAME (tail, frame)
5414 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5415 /* Record the new sizes, but don't reallocate the data
5416 structures now. Let that be done later outside of the
5417 signal handler. */
5418 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5423 static void
5424 deliver_window_change_signal (int sig)
5426 deliver_process_signal (sig, handle_window_change_signal);
5428 #endif /* SIGWINCH */
5431 /* Do any change in frame size that was requested by a signal.
5432 SAFE means this function is called from a place where it is
5433 safe to change frame sizes while a redisplay is in progress. */
5435 void
5436 do_pending_window_change (bool safe)
5438 /* If window change signal handler should have run before, run it now. */
5439 if (redisplaying_p && !safe)
5440 return;
5442 while (delayed_size_change)
5444 Lisp_Object tail, frame;
5446 delayed_size_change = 0;
5448 FOR_EACH_FRAME (tail, frame)
5450 struct frame *f = XFRAME (frame);
5452 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5453 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5454 0, 0, safe);
5460 /* Change the frame height and/or width. Values may be given as zero to
5461 indicate no change is to take place.
5463 If DELAY, assume we're being called from a signal handler, and
5464 queue the change for later - perhaps the next redisplay.
5465 Since this tries to resize windows, we can't call it
5466 from a signal handler.
5468 SAFE means this function is called from a place where it's
5469 safe to change frame sizes while a redisplay is in progress. */
5471 void
5472 change_frame_size (struct frame *f, int newheight, int newwidth,
5473 bool pretend, bool delay, bool safe)
5475 Lisp_Object tail, frame;
5477 if (FRAME_MSDOS_P (f))
5479 /* On MS-DOS, all frames use the same screen, so a change in
5480 size affects all frames. Termcap now supports multiple
5481 ttys. */
5482 FOR_EACH_FRAME (tail, frame)
5483 if (! FRAME_WINDOW_P (XFRAME (frame)))
5484 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5485 pretend, delay, safe);
5487 else
5488 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5491 static void
5492 change_frame_size_1 (struct frame *f, int newheight, int newwidth,
5493 bool pretend, bool delay, bool safe)
5495 int new_frame_total_cols;
5496 ptrdiff_t count = SPECPDL_INDEX ();
5498 /* If we can't deal with the change now, queue it for later. */
5499 if (delay || (redisplaying_p && !safe))
5501 f->new_text_lines = newheight;
5502 f->new_text_cols = newwidth;
5503 delayed_size_change = 1;
5504 return;
5507 /* This size-change overrides any pending one for this frame. */
5508 f->new_text_lines = 0;
5509 f->new_text_cols = 0;
5511 /* If an argument is zero, set it to the current value. */
5512 if (newheight == 0)
5513 newheight = FRAME_LINES (f);
5514 if (newwidth == 0)
5515 newwidth = FRAME_COLS (f);
5517 /* Compute width of windows in F. */
5518 /* Round up to the smallest acceptable size. */
5519 check_frame_size (f, &newheight, &newwidth);
5521 /* This is the width of the frame with vertical scroll bars and fringe
5522 columns. Do this after rounding - see discussion of bug#9723. */
5523 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5525 /* If we're not changing the frame size, quit now. */
5526 /* Frame width may be unchanged but the text portion may change, for
5527 example, fullscreen and remove/add scroll bar. */
5528 if (newheight == FRAME_LINES (f)
5529 /* Text portion unchanged? */
5530 && newwidth == FRAME_COLS (f)
5531 /* Frame width unchanged? */
5532 && new_frame_total_cols == FRAME_TOTAL_COLS (f))
5533 return;
5535 block_input ();
5537 #ifdef MSDOS
5538 /* We only can set screen dimensions to certain values supported
5539 by our video hardware. Try to find the smallest size greater
5540 or equal to the requested dimensions. */
5541 dos_set_window_size (&newheight, &newwidth);
5542 #endif
5544 if (newheight != FRAME_LINES (f))
5546 resize_frame_windows (f, newheight, 0);
5548 /* MSDOS frames cannot PRETEND, as they change frame size by
5549 manipulating video hardware. */
5550 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5551 FrameRows (FRAME_TTY (f)) = newheight;
5554 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5556 resize_frame_windows (f, new_frame_total_cols, 1);
5558 /* MSDOS frames cannot PRETEND, as they change frame size by
5559 manipulating video hardware. */
5560 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5561 FrameCols (FRAME_TTY (f)) = newwidth;
5563 if (WINDOWP (f->tool_bar_window))
5564 XWINDOW (f->tool_bar_window)->total_cols = newwidth;
5567 FRAME_LINES (f) = newheight;
5568 SET_FRAME_COLS (f, newwidth);
5571 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5572 int text_area_x, text_area_y, text_area_width, text_area_height;
5574 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5575 &text_area_height);
5576 if (w->cursor.x >= text_area_x + text_area_width)
5577 w->cursor.hpos = w->cursor.x = 0;
5578 if (w->cursor.y >= text_area_y + text_area_height)
5579 w->cursor.vpos = w->cursor.y = 0;
5582 adjust_glyphs (f);
5583 calculate_costs (f);
5584 SET_FRAME_GARBAGED (f);
5585 f->resized_p = 1;
5587 unblock_input ();
5589 record_unwind_current_buffer ();
5591 run_window_configuration_change_hook (f);
5593 unbind_to (count, Qnil);
5598 /***********************************************************************
5599 Terminal Related Lisp Functions
5600 ***********************************************************************/
5602 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5603 1, 1, "FOpen termscript file: ",
5604 doc: /* Start writing all terminal output to FILE as well as the terminal.
5605 FILE = nil means just close any termscript file currently open. */)
5606 (Lisp_Object file)
5608 struct tty_display_info *tty;
5610 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5611 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5612 error ("Current frame is not on a tty device");
5614 tty = CURTTY ();
5616 if (tty->termscript != 0)
5618 block_input ();
5619 fclose (tty->termscript);
5620 tty->termscript = 0;
5621 unblock_input ();
5624 if (! NILP (file))
5626 file = Fexpand_file_name (file, Qnil);
5627 tty->termscript = emacs_fopen (SSDATA (file), "w");
5628 if (tty->termscript == 0)
5629 report_file_error ("Opening termscript", file);
5631 return Qnil;
5635 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5636 Ssend_string_to_terminal, 1, 2, 0,
5637 doc: /* Send STRING to the terminal without alteration.
5638 Control characters in STRING will have terminal-dependent effects.
5640 Optional parameter TERMINAL specifies the tty terminal device to use.
5641 It may be a terminal object, a frame, or nil for the terminal used by
5642 the currently selected frame. In batch mode, STRING is sent to stdout
5643 when TERMINAL is nil. */)
5644 (Lisp_Object string, Lisp_Object terminal)
5646 struct terminal *t = get_terminal (terminal, 1);
5647 FILE *out;
5649 /* ??? Perhaps we should do something special for multibyte strings here. */
5650 CHECK_STRING (string);
5651 block_input ();
5653 if (!t)
5654 error ("Unknown terminal device");
5656 if (t->type == output_initial)
5657 out = stdout;
5658 else if (t->type != output_termcap && t->type != output_msdos_raw)
5659 error ("Device %d is not a termcap terminal device", t->id);
5660 else
5662 struct tty_display_info *tty = t->display_info.tty;
5664 if (! tty->output)
5665 error ("Terminal is currently suspended");
5667 if (tty->termscript)
5669 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5670 fflush (tty->termscript);
5672 out = tty->output;
5674 fwrite (SDATA (string), 1, SBYTES (string), out);
5675 fflush (out);
5676 unblock_input ();
5677 return Qnil;
5681 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5682 doc: /* Beep, or flash the screen.
5683 Also, unless an argument is given,
5684 terminate any keyboard macro currently executing. */)
5685 (Lisp_Object arg)
5687 if (!NILP (arg))
5689 if (noninteractive)
5690 putchar (07);
5691 else
5692 ring_bell (XFRAME (selected_frame));
5694 else
5695 bitch_at_user ();
5697 return Qnil;
5700 void
5701 bitch_at_user (void)
5703 if (noninteractive)
5704 putchar (07);
5705 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5707 const char *msg
5708 = "Keyboard macro terminated by a command ringing the bell";
5709 Fsignal (Quser_error, list1 (build_string (msg)));
5711 else
5712 ring_bell (XFRAME (selected_frame));
5717 /***********************************************************************
5718 Sleeping, Waiting
5719 ***********************************************************************/
5721 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5722 doc: /* Pause, without updating display, for SECONDS seconds.
5723 SECONDS may be a floating-point value, meaning that you can wait for a
5724 fraction of a second. Optional second arg MILLISECONDS specifies an
5725 additional wait period, in milliseconds; this is for backwards compatibility.
5726 \(Not all operating systems support waiting for a fraction of a second.) */)
5727 (Lisp_Object seconds, Lisp_Object milliseconds)
5729 double duration = extract_float (seconds);
5731 if (!NILP (milliseconds))
5733 CHECK_NUMBER (milliseconds);
5734 duration += XINT (milliseconds) / 1000.0;
5737 if (duration > 0)
5739 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (duration);
5740 wait_reading_process_output (min (EMACS_SECS (t), WAIT_READING_MAX),
5741 EMACS_NSECS (t), 0, 0, Qnil, NULL, 0);
5744 return Qnil;
5748 /* This is just like wait_reading_process_output, except that
5749 it does redisplay.
5751 TIMEOUT is number of seconds to wait (float or integer),
5752 or t to wait forever.
5753 READING is true if reading input.
5754 If DISPLAY_OPTION is >0 display process output while waiting.
5755 If DISPLAY_OPTION is >1 perform an initial redisplay before waiting.
5758 Lisp_Object
5759 sit_for (Lisp_Object timeout, bool reading, int display_option)
5761 intmax_t sec;
5762 int nsec;
5763 bool do_display = display_option > 0;
5765 swallow_events (do_display);
5767 if ((detect_input_pending_run_timers (do_display))
5768 || !NILP (Vexecuting_kbd_macro))
5769 return Qnil;
5771 if (display_option > 1)
5772 redisplay_preserve_echo_area (2);
5774 if (INTEGERP (timeout))
5776 sec = XINT (timeout);
5777 if (sec <= 0)
5778 return Qt;
5779 nsec = 0;
5781 else if (FLOATP (timeout))
5783 double seconds = XFLOAT_DATA (timeout);
5784 if (! (0 < seconds))
5785 return Qt;
5786 else
5788 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (seconds);
5789 sec = min (EMACS_SECS (t), WAIT_READING_MAX);
5790 nsec = EMACS_NSECS (t);
5793 else if (EQ (timeout, Qt))
5795 sec = 0;
5796 nsec = 0;
5798 else
5799 wrong_type_argument (Qnumberp, timeout);
5802 #ifdef USABLE_SIGIO
5803 gobble_input ();
5804 #endif
5806 wait_reading_process_output (sec, nsec, reading ? -1 : 1, do_display,
5807 Qnil, NULL, 0);
5809 return detect_input_pending () ? Qnil : Qt;
5813 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
5814 doc: /* Perform redisplay.
5815 Optional arg FORCE, if non-nil, prevents redisplay from being
5816 preempted by arriving input, even if `redisplay-dont-pause' is nil.
5817 If `redisplay-dont-pause' is non-nil (the default), redisplay is never
5818 preempted by arriving input, so FORCE does nothing.
5820 Return t if redisplay was performed, nil if redisplay was preempted
5821 immediately by pending input. */)
5822 (Lisp_Object force)
5824 ptrdiff_t count;
5826 swallow_events (1);
5827 if ((detect_input_pending_run_timers (1)
5828 && NILP (force) && !redisplay_dont_pause)
5829 || !NILP (Vexecuting_kbd_macro))
5830 return Qnil;
5832 count = SPECPDL_INDEX ();
5833 if (!NILP (force) && !redisplay_dont_pause)
5834 specbind (Qredisplay_dont_pause, Qt);
5835 redisplay_preserve_echo_area (2);
5836 unbind_to (count, Qnil);
5837 return Qt;
5842 /***********************************************************************
5843 Other Lisp Functions
5844 ***********************************************************************/
5846 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
5847 session's frames, frame names, buffers, buffer-read-only flags, and
5848 buffer-modified-flags. */
5850 static Lisp_Object frame_and_buffer_state;
5853 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
5854 Sframe_or_buffer_changed_p, 0, 1, 0,
5855 doc: /* Return non-nil if the frame and buffer state appears to have changed.
5856 VARIABLE is a variable name whose value is either nil or a state vector
5857 that will be updated to contain all frames and buffers,
5858 aside from buffers whose names start with space,
5859 along with the buffers' read-only and modified flags. This allows a fast
5860 check to see whether buffer menus might need to be recomputed.
5861 If this function returns non-nil, it updates the internal vector to reflect
5862 the current state.
5864 If VARIABLE is nil, an internal variable is used. Users should not
5865 pass nil for VARIABLE. */)
5866 (Lisp_Object variable)
5868 Lisp_Object state, tail, frame, buf;
5869 ptrdiff_t n, idx;
5871 if (! NILP (variable))
5873 CHECK_SYMBOL (variable);
5874 state = Fsymbol_value (variable);
5875 if (! VECTORP (state))
5876 goto changed;
5878 else
5879 state = frame_and_buffer_state;
5881 idx = 0;
5882 FOR_EACH_FRAME (tail, frame)
5884 if (idx == ASIZE (state))
5885 goto changed;
5886 if (!EQ (AREF (state, idx++), frame))
5887 goto changed;
5888 if (idx == ASIZE (state))
5889 goto changed;
5890 if (!EQ (AREF (state, idx++), XFRAME (frame)->name))
5891 goto changed;
5893 /* Check that the buffer info matches. */
5894 FOR_EACH_LIVE_BUFFER (tail, buf)
5896 /* Ignore buffers that aren't included in buffer lists. */
5897 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5898 continue;
5899 if (idx == ASIZE (state))
5900 goto changed;
5901 if (!EQ (AREF (state, idx++), buf))
5902 goto changed;
5903 if (idx == ASIZE (state))
5904 goto changed;
5905 if (!EQ (AREF (state, idx++), BVAR (XBUFFER (buf), read_only)))
5906 goto changed;
5907 if (idx == ASIZE (state))
5908 goto changed;
5909 if (!EQ (AREF (state, idx++), Fbuffer_modified_p (buf)))
5910 goto changed;
5912 if (idx == ASIZE (state))
5913 goto changed;
5914 /* Detect deletion of a buffer at the end of the list. */
5915 if (EQ (AREF (state, idx), Qlambda))
5916 return Qnil;
5918 /* Come here if we decide the data has changed. */
5919 changed:
5920 /* Count the size we will need.
5921 Start with 1 so there is room for at least one lambda at the end. */
5922 n = 1;
5923 FOR_EACH_FRAME (tail, frame)
5924 n += 2;
5925 FOR_EACH_LIVE_BUFFER (tail, buf)
5926 n += 3;
5927 /* Reallocate the vector if data has grown to need it,
5928 or if it has shrunk a lot. */
5929 if (! VECTORP (state)
5930 || n > ASIZE (state)
5931 || n + 20 < ASIZE (state) / 2)
5932 /* Add 20 extra so we grow it less often. */
5934 state = Fmake_vector (make_number (n + 20), Qlambda);
5935 if (! NILP (variable))
5936 Fset (variable, state);
5937 else
5938 frame_and_buffer_state = state;
5941 /* Record the new data in the (possibly reallocated) vector. */
5942 idx = 0;
5943 FOR_EACH_FRAME (tail, frame)
5945 ASET (state, idx, frame);
5946 idx++;
5947 ASET (state, idx, XFRAME (frame)->name);
5948 idx++;
5950 FOR_EACH_LIVE_BUFFER (tail, buf)
5952 /* Ignore buffers that aren't included in buffer lists. */
5953 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5954 continue;
5955 ASET (state, idx, buf);
5956 idx++;
5957 ASET (state, idx, BVAR (XBUFFER (buf), read_only));
5958 idx++;
5959 ASET (state, idx, Fbuffer_modified_p (buf));
5960 idx++;
5962 /* Fill up the vector with lambdas (always at least one). */
5963 ASET (state, idx, Qlambda);
5964 idx++;
5965 while (idx < ASIZE (state))
5967 ASET (state, idx, Qlambda);
5968 idx++;
5970 /* Make sure we didn't overflow the vector. */
5971 eassert (idx <= ASIZE (state));
5972 return Qt;
5977 /***********************************************************************
5978 Initialization
5979 ***********************************************************************/
5981 /* Initialization done when Emacs fork is started, before doing stty.
5982 Determine terminal type and set terminal_driver. Then invoke its
5983 decoding routine to set up variables in the terminal package. */
5985 void
5986 init_display (void)
5988 char *terminal_type;
5990 /* Construct the space glyph. */
5991 space_glyph.type = CHAR_GLYPH;
5992 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
5993 space_glyph.charpos = -1;
5995 inverse_video = 0;
5996 cursor_in_echo_area = 0;
5998 /* Now is the time to initialize this; it's used by init_sys_modes
5999 during startup. */
6000 Vinitial_window_system = Qnil;
6002 /* SIGWINCH needs to be handled no matter what display we start
6003 with. Otherwise newly opened tty frames will not resize
6004 automatically. */
6005 #ifdef SIGWINCH
6006 #ifndef CANNOT_DUMP
6007 if (initialized)
6008 #endif /* CANNOT_DUMP */
6010 struct sigaction action;
6011 emacs_sigaction_init (&action, deliver_window_change_signal);
6012 sigaction (SIGWINCH, &action, 0);
6014 #endif /* SIGWINCH */
6016 /* If running as a daemon, no need to initialize any frames/terminal. */
6017 if (IS_DAEMON)
6018 return;
6020 /* If the user wants to use a window system, we shouldn't bother
6021 initializing the terminal. This is especially important when the
6022 terminal is so dumb that emacs gives up before and doesn't bother
6023 using the window system.
6025 If the DISPLAY environment variable is set and nonempty,
6026 try to use X, and die with an error message if that doesn't work. */
6028 #ifdef HAVE_X_WINDOWS
6029 if (! inhibit_window_system && ! display_arg)
6031 char *display;
6032 display = getenv ("DISPLAY");
6033 display_arg = (display != 0 && *display != 0);
6035 if (display_arg && !x_display_ok (display))
6037 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6038 display);
6039 inhibit_window_system = 1;
6043 if (!inhibit_window_system && display_arg)
6045 Vinitial_window_system = Qx;
6046 #ifdef HAVE_X11
6047 Vwindow_system_version = make_number (11);
6048 #endif
6049 #ifdef USE_NCURSES
6050 /* In some versions of ncurses,
6051 tputs crashes if we have not called tgetent.
6052 So call tgetent. */
6053 { char b[2044]; tgetent (b, "xterm");}
6054 #endif
6055 return;
6057 #endif /* HAVE_X_WINDOWS */
6059 #ifdef HAVE_NTGUI
6060 if (!inhibit_window_system)
6062 Vinitial_window_system = Qw32;
6063 Vwindow_system_version = make_number (1);
6064 return;
6066 #endif /* HAVE_NTGUI */
6068 #ifdef HAVE_NS
6069 if (!inhibit_window_system
6070 #ifndef CANNOT_DUMP
6071 && initialized
6072 #endif
6075 Vinitial_window_system = Qns;
6076 Vwindow_system_version = make_number (10);
6077 return;
6079 #endif
6081 /* If no window system has been specified, try to use the terminal. */
6082 if (! isatty (0))
6083 fatal ("standard input is not a tty");
6085 #ifdef WINDOWSNT
6086 terminal_type = "w32console";
6087 #else
6088 terminal_type = getenv ("TERM");
6089 #endif
6090 if (!terminal_type)
6092 #ifdef HAVE_WINDOW_SYSTEM
6093 if (! inhibit_window_system)
6094 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6095 else
6096 #endif /* HAVE_WINDOW_SYSTEM */
6097 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6098 exit (1);
6102 struct terminal *t;
6103 struct frame *f = XFRAME (selected_frame);
6105 init_foreground_group ();
6107 /* Open a display on the controlling tty. */
6108 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6110 /* Convert the initial frame to use the new display. */
6111 if (f->output_method != output_initial)
6112 emacs_abort ();
6113 f->output_method = t->type;
6114 f->terminal = t;
6116 t->reference_count++;
6117 #ifdef MSDOS
6118 f->output_data.tty->display_info = &the_only_display_info;
6119 #else
6120 if (f->output_method == output_termcap)
6121 create_tty_output (f);
6122 #endif
6123 t->display_info.tty->top_frame = selected_frame;
6124 change_frame_size (XFRAME (selected_frame),
6125 FrameRows (t->display_info.tty),
6126 FrameCols (t->display_info.tty), 0, 0, 1);
6128 /* Delete the initial terminal. */
6129 if (--initial_terminal->reference_count == 0
6130 && initial_terminal->delete_terminal_hook)
6131 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6133 /* Update frame parameters to reflect the new type. */
6134 Fmodify_frame_parameters
6135 (selected_frame, list1 (Fcons (Qtty_type,
6136 Ftty_type (selected_frame))));
6137 if (t->display_info.tty->name)
6138 Fmodify_frame_parameters
6139 (selected_frame,
6140 list1 (Fcons (Qtty, build_string (t->display_info.tty->name))));
6141 else
6142 Fmodify_frame_parameters (selected_frame, list1 (Fcons (Qtty, Qnil)));
6146 struct frame *sf = SELECTED_FRAME ();
6147 int width = FRAME_TOTAL_COLS (sf);
6148 int height = FRAME_LINES (sf);
6150 /* If these sizes are so big they cause overflow, just ignore the
6151 change. It's not clear what better we could do. The rest of
6152 the code assumes that (width + 2) * height * sizeof (struct glyph)
6153 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6154 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6155 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6156 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6157 < (width + 2) * height))
6158 fatal ("screen size %dx%d too big", width, height);
6161 calculate_costs (XFRAME (selected_frame));
6163 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6164 if (initialized
6165 && !noninteractive
6166 && NILP (Vinitial_window_system))
6168 /* For the initial frame, we don't have any way of knowing what
6169 are the foreground and background colors of the terminal. */
6170 struct frame *sf = SELECTED_FRAME ();
6172 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6173 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6174 call0 (intern ("tty-set-up-initial-frame-faces"));
6180 /***********************************************************************
6181 Blinking cursor
6182 ***********************************************************************/
6184 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6185 Sinternal_show_cursor, 2, 2, 0,
6186 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6187 WINDOW nil means use the selected window. SHOW non-nil means
6188 show a cursor in WINDOW in the next redisplay. SHOW nil means
6189 don't show a cursor. */)
6190 (Lisp_Object window, Lisp_Object show)
6192 /* Don't change cursor state while redisplaying. This could confuse
6193 output routines. */
6194 if (!redisplaying_p)
6195 decode_any_window (window)->cursor_off_p = NILP (show);
6196 return Qnil;
6200 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6201 Sinternal_show_cursor_p, 0, 1, 0,
6202 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6203 WINDOW nil or omitted means report on the selected window. */)
6204 (Lisp_Object window)
6206 return decode_any_window (window)->cursor_off_p ? Qnil : Qt;
6209 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6210 Slast_nonminibuf_frame, 0, 0, 0,
6211 doc: /* Value is last nonminibuffer frame. */)
6212 (void)
6214 Lisp_Object frame = Qnil;
6216 if (last_nonminibuf_frame)
6217 XSETFRAME (frame, last_nonminibuf_frame);
6219 return frame;
6222 /***********************************************************************
6223 Initialization
6224 ***********************************************************************/
6226 void
6227 syms_of_display (void)
6229 defsubr (&Sredraw_frame);
6230 defsubr (&Sredraw_display);
6231 defsubr (&Sframe_or_buffer_changed_p);
6232 defsubr (&Sopen_termscript);
6233 defsubr (&Sding);
6234 defsubr (&Sredisplay);
6235 defsubr (&Ssleep_for);
6236 defsubr (&Ssend_string_to_terminal);
6237 defsubr (&Sinternal_show_cursor);
6238 defsubr (&Sinternal_show_cursor_p);
6239 defsubr (&Slast_nonminibuf_frame);
6241 #ifdef GLYPH_DEBUG
6242 defsubr (&Sdump_redisplay_history);
6243 #endif
6245 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6246 staticpro (&frame_and_buffer_state);
6248 DEFSYM (Qdisplay_table, "display-table");
6249 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6251 DEFVAR_INT ("baud-rate", baud_rate,
6252 doc: /* The output baud rate of the terminal.
6253 On most systems, changing this value will affect the amount of padding
6254 and the other strategic decisions made during redisplay. */);
6256 DEFVAR_BOOL ("inverse-video", inverse_video,
6257 doc: /* Non-nil means invert the entire frame display.
6258 This means everything is in inverse video which otherwise would not be. */);
6260 DEFVAR_BOOL ("visible-bell", visible_bell,
6261 doc: /* Non-nil means try to flash the frame to represent a bell.
6263 See also `ring-bell-function'. */);
6265 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6266 doc: /* Non-nil means no need to redraw entire frame after suspending.
6267 A non-nil value is useful if the terminal can automatically preserve
6268 Emacs's frame display when you reenter Emacs.
6269 It is up to you to set this variable if your terminal can do that. */);
6271 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6272 doc: /* Name of the window system that Emacs uses for the first frame.
6273 The value is a symbol:
6274 nil for a termcap frame (a character-only terminal),
6275 'x' for an Emacs frame that is really an X window,
6276 'w32' for an Emacs frame that is a window on MS-Windows display,
6277 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6278 'pc' for a direct-write MS-DOS frame.
6280 Use of this variable as a boolean is deprecated. Instead,
6281 use `display-graphic-p' or any of the other `display-*-p'
6282 predicates which report frame's specific UI-related capabilities. */);
6284 DEFVAR_KBOARD ("window-system", Vwindow_system,
6285 doc: /* Name of window system through which the selected frame is displayed.
6286 The value is a symbol:
6287 nil for a termcap frame (a character-only terminal),
6288 'x' for an Emacs frame that is really an X window,
6289 'w32' for an Emacs frame that is a window on MS-Windows display,
6290 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6291 'pc' for a direct-write MS-DOS frame.
6293 Use of this variable as a boolean is deprecated. Instead,
6294 use `display-graphic-p' or any of the other `display-*-p'
6295 predicates which report frame's specific UI-related capabilities. */);
6297 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6298 doc: /* The version number of the window system in use.
6299 For X windows, this is 11. */);
6301 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6302 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6304 DEFVAR_LISP ("glyph-table", Vglyph_table,
6305 doc: /* Table defining how to output a glyph code to the frame.
6306 If not nil, this is a vector indexed by glyph code to define the glyph.
6307 Each element can be:
6308 integer: a glyph code which this glyph is an alias for.
6309 string: output this glyph using that string (not impl. in X windows).
6310 nil: this glyph mod 524288 is the code of a character to output,
6311 and this glyph / 524288 is the face number (see `face-id') to use
6312 while outputting it. */);
6313 Vglyph_table = Qnil;
6315 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6316 doc: /* Display table to use for buffers that specify none.
6317 See `buffer-display-table' for more information. */);
6318 Vstandard_display_table = Qnil;
6320 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6321 doc: /* Non-nil means display update isn't paused when input is detected. */);
6322 redisplay_dont_pause = 1;
6324 #ifdef CANNOT_DUMP
6325 if (noninteractive)
6326 #endif
6328 Vinitial_window_system = Qnil;
6329 Vwindow_system_version = Qnil;