* frame.h (struct frame): Put menu_bar_window under #ifdef
[emacs.git] / src / dispnew.c
blob41d4844707e164a4b8fdef06973c7fb80bd737f1
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 <stdio.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 /* Updated window if != 0. Set by update_window. */
140 struct window *updated_window;
142 /* Glyph row updated in update_window_line, and area that is updated. */
144 struct glyph_row *updated_row;
145 int updated_area;
147 /* A glyph for a space. */
149 struct glyph space_glyph;
151 /* Counts of allocated structures. These counts serve to diagnose
152 memory leaks and double frees. */
154 static int glyph_matrix_count;
155 static int glyph_pool_count;
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;
217 if (history_idx >= REDISPLAY_HISTORY_SIZE)
218 history_idx = 0;
219 buf = redisplay_history[history_idx].trace;
220 ++history_idx;
222 snprintf (buf, sizeof redisplay_history[0].trace,
223 "%"pMu": window %p (`%s')%s\n%s",
224 history_tick++,
226 ((BUFFERP (w->buffer)
227 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
228 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
229 : "???"),
230 paused_p ? " ***paused***" : "",
231 msg);
235 /* Add to the redisplay history that frame F has been displayed.
236 PAUSED_P means that the update has been interrupted for
237 pending input. */
239 static void
240 add_frame_display_history (struct frame *f, bool paused_p)
242 char *buf;
244 if (history_idx >= REDISPLAY_HISTORY_SIZE)
245 history_idx = 0;
246 buf = redisplay_history[history_idx].trace;
247 ++history_idx;
249 sprintf (buf, "%"pMu": update frame %p%s",
250 history_tick++,
251 f, paused_p ? " ***paused***" : "");
255 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
256 Sdump_redisplay_history, 0, 0, "",
257 doc: /* Dump redisplay history to stderr. */)
258 (void)
260 int i;
262 for (i = history_idx - 1; i != history_idx; --i)
264 if (i < 0)
265 i = REDISPLAY_HISTORY_SIZE - 1;
266 fprintf (stderr, "%s\n", redisplay_history[i].trace);
269 return Qnil;
273 #else /* not GLYPH_DEBUG */
275 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
276 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
278 #endif /* GLYPH_DEBUG */
281 #if (defined PROFILING \
282 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
283 && !HAVE___EXECUTABLE_START)
284 /* This function comes first in the Emacs executable and is used only
285 to estimate the text start for profiling. */
286 void
287 __executable_start (void)
289 emacs_abort ();
291 #endif
293 /***********************************************************************
294 Glyph Matrices
295 ***********************************************************************/
297 /* Allocate and return a glyph_matrix structure. POOL is the glyph
298 pool from which memory for the matrix should be allocated, or null
299 for window-based redisplay where no glyph pools are used. The
300 member `pool' of the glyph matrix structure returned is set to
301 POOL, the structure is otherwise zeroed. */
303 static struct glyph_matrix *
304 new_glyph_matrix (struct glyph_pool *pool)
306 struct glyph_matrix *result = xzalloc (sizeof *result);
308 /* Increment number of allocated matrices. This count is used
309 to detect memory leaks. */
310 ++glyph_matrix_count;
312 /* Set pool and return. */
313 result->pool = pool;
314 return result;
318 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
320 The global counter glyph_matrix_count is decremented when a matrix
321 is freed. If the count gets negative, more structures were freed
322 than allocated, i.e. one matrix was freed more than once or a bogus
323 pointer was passed to this function.
325 If MATRIX->pool is null, this means that the matrix manages its own
326 glyph memory---this is done for matrices on X frames. Freeing the
327 matrix also frees the glyph memory in this case. */
329 static void
330 free_glyph_matrix (struct glyph_matrix *matrix)
332 if (matrix)
334 int i;
336 /* Detect the case that more matrices are freed than were
337 allocated. */
338 if (--glyph_matrix_count < 0)
339 emacs_abort ();
341 /* Free glyph memory if MATRIX owns it. */
342 if (matrix->pool == NULL)
343 for (i = 0; i < matrix->rows_allocated; ++i)
344 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
346 /* Free row structures and the matrix itself. */
347 xfree (matrix->rows);
348 xfree (matrix);
353 /* Return the number of glyphs to reserve for a marginal area of
354 window W. TOTAL_GLYPHS is the number of glyphs in a complete
355 display line of window W. MARGIN gives the width of the marginal
356 area in canonical character units. MARGIN should be an integer
357 or a float. */
359 static int
360 margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
362 int n;
364 if (NUMBERP (margin))
366 int width = w->total_cols;
367 double d = max (0, XFLOATINT (margin));
368 d = min (width / 2 - 1, d);
369 n = (int) ((double) total_glyphs / width * d);
371 else
372 n = 0;
374 return n;
377 /* Return true if ROW's hash value is correct.
378 Optimized away if ENABLE_CHECKING is not defined. */
380 static bool
381 verify_row_hash (struct glyph_row *row)
383 return row->hash == row_hash (row);
386 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
387 window sizes.
389 W is null if the function is called for a frame glyph matrix.
390 Otherwise it is the window MATRIX is a member of. X and Y are the
391 indices of the first column and row of MATRIX within the frame
392 matrix, if such a matrix exists. They are zero for purely
393 window-based redisplay. DIM is the needed size of the matrix.
395 In window-based redisplay, where no frame matrices exist, glyph
396 matrices manage their own glyph storage. Otherwise, they allocate
397 storage from a common frame glyph pool which can be found in
398 MATRIX->pool.
400 The reason for this memory management strategy is to avoid complete
401 frame redraws if possible. When we allocate from a common pool, a
402 change of the location or size of a sub-matrix within the pool
403 requires a complete redisplay of the frame because we cannot easily
404 make sure that the current matrices of all windows still agree with
405 what is displayed on the screen. While this is usually fast, it
406 leads to screen flickering. */
408 static void
409 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
411 int i;
412 int new_rows;
413 bool marginal_areas_changed_p = 0;
414 bool header_line_changed_p = 0;
415 bool header_line_p = 0;
416 int left = -1, right = -1;
417 int window_width = -1, window_height = -1;
419 /* See if W had a header line that has disappeared now, or vice versa.
420 Get W's size. */
421 if (w)
423 window_box (w, -1, 0, 0, &window_width, &window_height);
425 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
426 header_line_changed_p = header_line_p != matrix->header_line_p;
428 matrix->header_line_p = header_line_p;
430 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
431 Do nothing if MATRIX' size, position, vscroll, and marginal areas
432 haven't changed. This optimization is important because preserving
433 the matrix means preventing redisplay. */
434 if (matrix->pool == NULL)
436 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
437 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
438 eassert (left >= 0 && right >= 0);
439 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
440 || right != matrix->right_margin_glyphs);
442 if (!marginal_areas_changed_p
443 && !fonts_changed_p
444 && !header_line_changed_p
445 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
446 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
447 && matrix->window_height == window_height
448 && matrix->window_vscroll == w->vscroll
449 && matrix->window_width == window_width)
450 return;
453 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
454 if (matrix->rows_allocated < dim.height)
456 int old_alloc = matrix->rows_allocated;
457 new_rows = dim.height - matrix->rows_allocated;
458 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
459 new_rows, INT_MAX, sizeof *matrix->rows);
460 memset (matrix->rows + old_alloc, 0,
461 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
463 else
464 new_rows = 0;
466 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
467 on a frame not using window-based redisplay. Set up pointers for
468 each row into the glyph pool. */
469 if (matrix->pool)
471 eassert (matrix->pool->glyphs);
473 if (w)
475 left = margin_glyphs_to_reserve (w, dim.width,
476 w->left_margin_cols);
477 right = margin_glyphs_to_reserve (w, dim.width,
478 w->right_margin_cols);
480 else
481 left = right = 0;
483 for (i = 0; i < dim.height; ++i)
485 struct glyph_row *row = &matrix->rows[i];
487 row->glyphs[LEFT_MARGIN_AREA]
488 = (matrix->pool->glyphs
489 + (y + i) * matrix->pool->ncolumns
490 + x);
492 if (w == NULL
493 || row == matrix->rows + dim.height - 1
494 || (row == matrix->rows && matrix->header_line_p))
496 row->glyphs[TEXT_AREA]
497 = row->glyphs[LEFT_MARGIN_AREA];
498 row->glyphs[RIGHT_MARGIN_AREA]
499 = row->glyphs[TEXT_AREA] + dim.width;
500 row->glyphs[LAST_AREA]
501 = row->glyphs[RIGHT_MARGIN_AREA];
503 else
505 row->glyphs[TEXT_AREA]
506 = row->glyphs[LEFT_MARGIN_AREA] + left;
507 row->glyphs[RIGHT_MARGIN_AREA]
508 = row->glyphs[TEXT_AREA] + dim.width - left - right;
509 row->glyphs[LAST_AREA]
510 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
514 matrix->left_margin_glyphs = left;
515 matrix->right_margin_glyphs = right;
517 else
519 /* If MATRIX->pool is null, MATRIX is responsible for managing
520 its own memory. It is a window matrix for window-based redisplay.
521 Allocate glyph memory from the heap. */
522 if (dim.width > matrix->matrix_w
523 || new_rows
524 || header_line_changed_p
525 || marginal_areas_changed_p)
527 struct glyph_row *row = matrix->rows;
528 struct glyph_row *end = row + matrix->rows_allocated;
530 while (row < end)
532 row->glyphs[LEFT_MARGIN_AREA]
533 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
534 dim.width, sizeof (struct glyph));
536 /* The mode line never has marginal areas. */
537 if (row == matrix->rows + dim.height - 1
538 || (row == matrix->rows && matrix->header_line_p))
540 row->glyphs[TEXT_AREA]
541 = row->glyphs[LEFT_MARGIN_AREA];
542 row->glyphs[RIGHT_MARGIN_AREA]
543 = row->glyphs[TEXT_AREA] + dim.width;
544 row->glyphs[LAST_AREA]
545 = row->glyphs[RIGHT_MARGIN_AREA];
547 else
549 row->glyphs[TEXT_AREA]
550 = row->glyphs[LEFT_MARGIN_AREA] + left;
551 row->glyphs[RIGHT_MARGIN_AREA]
552 = row->glyphs[TEXT_AREA] + dim.width - left - right;
553 row->glyphs[LAST_AREA]
554 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
556 ++row;
560 eassert (left >= 0 && right >= 0);
561 matrix->left_margin_glyphs = left;
562 matrix->right_margin_glyphs = right;
565 /* Number of rows to be used by MATRIX. */
566 matrix->nrows = dim.height;
567 eassert (matrix->nrows >= 0);
569 if (w)
571 if (matrix == w->current_matrix)
573 /* Mark rows in a current matrix of a window as not having
574 valid contents. It's important to not do this for
575 desired matrices. When Emacs starts, it may already be
576 building desired matrices when this function runs. */
577 if (window_width < 0)
578 window_width = window_box_width (w, -1);
580 /* Optimize the case that only the height has changed (C-x 2,
581 upper window). Invalidate all rows that are no longer part
582 of the window. */
583 if (!marginal_areas_changed_p
584 && !header_line_changed_p
585 && new_rows == 0
586 && dim.width == matrix->matrix_w
587 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
588 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
589 && matrix->window_width == window_width)
591 /* Find the last row in the window. */
592 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
593 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
595 ++i;
596 break;
599 /* Window end is invalid, if inside of the rows that
600 are invalidated below. */
601 if (INTEGERP (w->window_end_vpos)
602 && XFASTINT (w->window_end_vpos) >= i)
603 w->window_end_valid = 0;
605 while (i < matrix->nrows)
606 matrix->rows[i++].enabled_p = 0;
608 else
610 for (i = 0; i < matrix->nrows; ++i)
611 matrix->rows[i].enabled_p = 0;
614 else if (matrix == w->desired_matrix)
616 /* Rows in desired matrices always have to be cleared;
617 redisplay expects this is the case when it runs, so it
618 had better be the case when we adjust matrices between
619 redisplays. */
620 for (i = 0; i < matrix->nrows; ++i)
621 matrix->rows[i].enabled_p = 0;
626 /* Remember last values to be able to optimize frame redraws. */
627 matrix->matrix_x = x;
628 matrix->matrix_y = y;
629 matrix->matrix_w = dim.width;
630 matrix->matrix_h = dim.height;
632 /* Record the top y location and height of W at the time the matrix
633 was last adjusted. This is used to optimize redisplay above. */
634 if (w)
636 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
637 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
638 matrix->window_height = window_height;
639 matrix->window_width = window_width;
640 matrix->window_vscroll = w->vscroll;
645 /* Reverse the contents of rows in MATRIX between START and END. The
646 contents of the row at END - 1 end up at START, END - 2 at START +
647 1 etc. This is part of the implementation of rotate_matrix (see
648 below). */
650 static void
651 reverse_rows (struct glyph_matrix *matrix, int start, int end)
653 int i, j;
655 for (i = start, j = end - 1; i < j; ++i, --j)
657 /* Non-ISO HP/UX compiler doesn't like auto struct
658 initialization. */
659 struct glyph_row temp;
660 temp = matrix->rows[i];
661 matrix->rows[i] = matrix->rows[j];
662 matrix->rows[j] = temp;
667 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
668 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
669 indices. (Note: this does not copy glyphs, only glyph pointers in
670 row structures are moved around).
672 The algorithm used for rotating the vector was, I believe, first
673 described by Kernighan. See the vector R as consisting of two
674 sub-vectors AB, where A has length BY for BY >= 0. The result
675 after rotating is then BA. Reverse both sub-vectors to get ArBr
676 and reverse the result to get (ArBr)r which is BA. Similar for
677 rotating right. */
679 void
680 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
682 if (by < 0)
684 /* Up (rotate left, i.e. towards lower indices). */
685 by = -by;
686 reverse_rows (matrix, first, first + by);
687 reverse_rows (matrix, first + by, last);
688 reverse_rows (matrix, first, last);
690 else if (by > 0)
692 /* Down (rotate right, i.e. towards higher indices). */
693 reverse_rows (matrix, last - by, last);
694 reverse_rows (matrix, first, last - by);
695 reverse_rows (matrix, first, last);
700 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
701 with indices START <= index < END. Increment positions by DELTA/
702 DELTA_BYTES. */
704 void
705 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
706 ptrdiff_t delta, ptrdiff_t delta_bytes)
708 /* Check that START and END are reasonable values. */
709 eassert (start >= 0 && start <= matrix->nrows);
710 eassert (end >= 0 && end <= matrix->nrows);
711 eassert (start <= end);
713 for (; start < end; ++start)
714 increment_row_positions (matrix->rows + start, delta, delta_bytes);
718 /* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
719 START and END are the row indices of the first and last + 1 row to clear. */
721 void
722 clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
724 eassert (start <= end);
725 eassert (start >= 0 && start < matrix->nrows);
726 eassert (end >= 0 && end <= matrix->nrows);
728 for (; start < end; ++start)
729 matrix->rows[start].enabled_p = 0;
733 /* Clear MATRIX.
735 Empty all rows in MATRIX by clearing their enabled_p flags.
736 The function prepare_desired_row will eventually really clear a row
737 when it sees one with a false enabled_p flag.
739 Reset update hints to default values. The only update hint
740 currently present is the flag MATRIX->no_scrolling_p. */
742 void
743 clear_glyph_matrix (struct glyph_matrix *matrix)
745 if (matrix)
747 clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
748 matrix->no_scrolling_p = 0;
753 /* Shift part of the glyph matrix MATRIX of window W up or down.
754 Increment y-positions in glyph rows between START and END by DY,
755 and recompute their visible height. */
757 void
758 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
760 int min_y, max_y;
762 eassert (start <= end);
763 eassert (start >= 0 && start < matrix->nrows);
764 eassert (end >= 0 && end <= matrix->nrows);
766 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
767 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
769 for (; start < end; ++start)
771 struct glyph_row *row = &matrix->rows[start];
773 row->y += dy;
774 row->visible_height = row->height;
776 if (row->y < min_y)
777 row->visible_height -= min_y - row->y;
778 if (row->y + row->height > max_y)
779 row->visible_height -= row->y + row->height - max_y;
780 if (row->fringe_bitmap_periodic_p)
781 row->redraw_fringe_bitmaps_p = 1;
786 /* Mark all rows in current matrices of frame F as invalid. Marking
787 invalid is done by setting enabled_p to zero for all rows in a
788 current matrix. */
790 void
791 clear_current_matrices (register struct frame *f)
793 /* Clear frame current matrix, if we have one. */
794 if (f->current_matrix)
795 clear_glyph_matrix (f->current_matrix);
797 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
798 /* Clear the matrix of the menu bar window, if such a window exists.
799 The menu bar window is currently used to display menus on X when
800 no toolkit support is compiled in. */
801 if (WINDOWP (f->menu_bar_window))
802 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
803 #endif
805 /* Clear the matrix of the tool-bar window, if any. */
806 if (WINDOWP (f->tool_bar_window))
807 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
809 /* Clear current window matrices. */
810 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
811 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
815 /* Clear out all display lines of F for a coming redisplay. */
817 void
818 clear_desired_matrices (register struct frame *f)
820 if (f->desired_matrix)
821 clear_glyph_matrix (f->desired_matrix);
823 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
824 if (WINDOWP (f->menu_bar_window))
825 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
826 #endif
828 if (WINDOWP (f->tool_bar_window))
829 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
831 /* Do it for window matrices. */
832 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
833 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
837 /* Clear matrices in window tree rooted in W. If DESIRED_P,
838 clear desired matrices, otherwise clear current matrices. */
840 static void
841 clear_window_matrices (struct window *w, bool desired_p)
843 while (w)
845 if (!NILP (w->hchild))
847 eassert (WINDOWP (w->hchild));
848 clear_window_matrices (XWINDOW (w->hchild), desired_p);
850 else if (!NILP (w->vchild))
852 eassert (WINDOWP (w->vchild));
853 clear_window_matrices (XWINDOW (w->vchild), desired_p);
855 else
857 if (desired_p)
858 clear_glyph_matrix (w->desired_matrix);
859 else
861 clear_glyph_matrix (w->current_matrix);
862 w->window_end_valid = 0;
866 w = NILP (w->next) ? 0 : XWINDOW (w->next);
872 /***********************************************************************
873 Glyph Rows
875 See dispextern.h for an overall explanation of glyph rows.
876 ***********************************************************************/
878 /* Clear glyph row ROW. Do it in a way that makes it robust against
879 changes in the glyph_row structure, i.e. addition or removal of
880 structure members. */
882 static struct glyph_row null_row;
884 void
885 clear_glyph_row (struct glyph_row *row)
887 struct glyph *p[1 + LAST_AREA];
889 /* Save pointers. */
890 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
891 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
892 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
893 p[LAST_AREA] = row->glyphs[LAST_AREA];
895 /* Clear. */
896 *row = null_row;
898 /* Restore pointers. */
899 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
900 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
901 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
902 row->glyphs[LAST_AREA] = p[LAST_AREA];
904 #if 0 /* At some point, some bit-fields of struct glyph were not set,
905 which made glyphs unequal when compared with GLYPH_EQUAL_P.
906 Redisplay outputs such glyphs, and flickering effects were
907 the result. This also depended on the contents of memory
908 returned by xmalloc. If flickering happens again, activate
909 the code below. If the flickering is gone with that, chances
910 are that the flickering has the same reason as here. */
911 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
912 #endif
916 /* Make ROW an empty, enabled row of canonical character height,
917 in window W starting at y-position Y. */
919 void
920 blank_row (struct window *w, struct glyph_row *row, int y)
922 int min_y, max_y;
924 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
925 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
927 clear_glyph_row (row);
928 row->y = y;
929 row->ascent = row->phys_ascent = 0;
930 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
931 row->visible_height = row->height;
933 if (row->y < min_y)
934 row->visible_height -= min_y - row->y;
935 if (row->y + row->height > max_y)
936 row->visible_height -= row->y + row->height - max_y;
938 row->enabled_p = 1;
942 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
943 are the amounts by which to change positions. Note that the first
944 glyph of the text area of a row can have a buffer position even if
945 the used count of the text area is zero. Such rows display line
946 ends. */
948 static void
949 increment_row_positions (struct glyph_row *row,
950 ptrdiff_t delta, ptrdiff_t delta_bytes)
952 int area, i;
954 /* Increment start and end positions. */
955 MATRIX_ROW_START_CHARPOS (row) += delta;
956 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
957 MATRIX_ROW_END_CHARPOS (row) += delta;
958 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
959 CHARPOS (row->start.pos) += delta;
960 BYTEPOS (row->start.pos) += delta_bytes;
961 CHARPOS (row->end.pos) += delta;
962 BYTEPOS (row->end.pos) += delta_bytes;
964 if (!row->enabled_p)
965 return;
967 /* Increment positions in glyphs. */
968 for (area = 0; area < LAST_AREA; ++area)
969 for (i = 0; i < row->used[area]; ++i)
970 if (BUFFERP (row->glyphs[area][i].object)
971 && row->glyphs[area][i].charpos > 0)
972 row->glyphs[area][i].charpos += delta;
974 /* Capture the case of rows displaying a line end. */
975 if (row->used[TEXT_AREA] == 0
976 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
977 row->glyphs[TEXT_AREA]->charpos += delta;
981 #if 0
982 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
983 contents, i.e. glyph structure contents are exchanged between A and
984 B without changing glyph pointers in A and B. */
986 static void
987 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
989 int area;
991 for (area = 0; area < LAST_AREA; ++area)
993 /* Number of glyphs to swap. */
994 int max_used = max (a->used[area], b->used[area]);
996 /* Start of glyphs in area of row A. */
997 struct glyph *glyph_a = a->glyphs[area];
999 /* End + 1 of glyphs in area of row A. */
1000 struct glyph *glyph_a_end = a->glyphs[max_used];
1002 /* Start of glyphs in area of row B. */
1003 struct glyph *glyph_b = b->glyphs[area];
1005 while (glyph_a < glyph_a_end)
1007 /* Non-ISO HP/UX compiler doesn't like auto struct
1008 initialization. */
1009 struct glyph temp;
1010 temp = *glyph_a;
1011 *glyph_a = *glyph_b;
1012 *glyph_b = temp;
1013 ++glyph_a;
1014 ++glyph_b;
1019 #endif /* 0 */
1021 /* Exchange pointers to glyph memory between glyph rows A and B. Also
1022 exchange the used[] array and the hash values of the rows, because
1023 these should all go together for the row's hash value to be
1024 correct. */
1026 static void
1027 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1029 int i;
1030 unsigned hash_tem = a->hash;
1032 for (i = 0; i < LAST_AREA + 1; ++i)
1034 struct glyph *temp = a->glyphs[i];
1036 a->glyphs[i] = b->glyphs[i];
1037 b->glyphs[i] = temp;
1038 if (i < LAST_AREA)
1040 short used_tem = a->used[i];
1042 a->used[i] = b->used[i];
1043 b->used[i] = used_tem;
1046 a->hash = b->hash;
1047 b->hash = hash_tem;
1051 /* Copy glyph row structure FROM to glyph row structure TO, except
1052 that glyph pointers, the `used' counts, and the hash values in the
1053 structures are left unchanged. */
1055 static void
1056 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1058 struct glyph *pointers[1 + LAST_AREA];
1059 short used[LAST_AREA];
1060 unsigned hashval;
1062 /* Save glyph pointers of TO. */
1063 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1064 memcpy (used, to->used, sizeof to->used);
1065 hashval = to->hash;
1067 /* Do a structure assignment. */
1068 *to = *from;
1070 /* Restore original pointers of TO. */
1071 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1072 memcpy (to->used, used, sizeof to->used);
1073 to->hash = hashval;
1077 /* Assign glyph row FROM to glyph row TO. This works like a structure
1078 assignment TO = FROM, except that glyph pointers are not copied but
1079 exchanged between TO and FROM. Pointers must be exchanged to avoid
1080 a memory leak. */
1082 static void
1083 assign_row (struct glyph_row *to, struct glyph_row *from)
1085 swap_glyph_pointers (to, from);
1086 copy_row_except_pointers (to, from);
1090 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1091 a row in a window matrix, is a slice of the glyph memory of the
1092 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1093 is true if the glyph memory of WINDOW_ROW is part of the glyph
1094 memory of FRAME_ROW. */
1096 #ifdef GLYPH_DEBUG
1098 static bool
1099 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1101 struct glyph *window_glyph_start = window_row->glyphs[0];
1102 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1103 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1105 return (frame_glyph_start <= window_glyph_start
1106 && window_glyph_start < frame_glyph_end);
1109 #endif /* GLYPH_DEBUG */
1111 #if 0
1113 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1114 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1115 in WINDOW_MATRIX is found satisfying the condition. */
1117 static struct glyph_row *
1118 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1119 struct glyph_matrix *frame_matrix, int row)
1121 int i;
1123 eassert (row >= 0 && row < frame_matrix->nrows);
1125 for (i = 0; i < window_matrix->nrows; ++i)
1126 if (glyph_row_slice_p (window_matrix->rows + i,
1127 frame_matrix->rows + row))
1128 break;
1130 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1133 #endif /* 0 */
1135 /* Prepare ROW for display. Desired rows are cleared lazily,
1136 i.e. they are only marked as to be cleared by setting their
1137 enabled_p flag to zero. When a row is to be displayed, a prior
1138 call to this function really clears it. */
1140 void
1141 prepare_desired_row (struct glyph_row *row)
1143 if (!row->enabled_p)
1145 bool rp = row->reversed_p;
1147 clear_glyph_row (row);
1148 row->enabled_p = 1;
1149 row->reversed_p = rp;
1154 /* Return a hash code for glyph row ROW. */
1156 static int
1157 line_hash_code (struct glyph_row *row)
1159 int hash = 0;
1161 if (row->enabled_p)
1163 struct glyph *glyph = row->glyphs[TEXT_AREA];
1164 struct glyph *end = glyph + row->used[TEXT_AREA];
1166 while (glyph < end)
1168 int c = glyph->u.ch;
1169 int face_id = glyph->face_id;
1170 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1171 c -= SPACEGLYPH;
1172 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1173 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1174 ++glyph;
1177 if (hash == 0)
1178 hash = 1;
1181 return hash;
1185 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1186 the number of characters in the line. If must_write_spaces is
1187 zero, leading and trailing spaces are ignored. */
1189 static int
1190 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1192 struct glyph_row *row = matrix->rows + vpos;
1193 struct glyph *beg = row->glyphs[TEXT_AREA];
1194 struct glyph *end = beg + row->used[TEXT_AREA];
1195 int len;
1196 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1197 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1199 /* Ignore trailing and leading spaces if we can. */
1200 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1202 /* Skip from the end over trailing spaces. */
1203 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1204 --end;
1206 /* All blank line. */
1207 if (end == beg)
1208 return 0;
1210 /* Skip over leading spaces. */
1211 while (CHAR_GLYPH_SPACE_P (*beg))
1212 ++beg;
1215 /* If we don't have a glyph-table, each glyph is one character,
1216 so return the number of glyphs. */
1217 if (glyph_table_base == 0)
1218 len = end - beg;
1219 else
1221 /* Otherwise, scan the glyphs and accumulate their total length
1222 in LEN. */
1223 len = 0;
1224 while (beg < end)
1226 GLYPH g;
1228 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1230 if (GLYPH_INVALID_P (g)
1231 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1232 len += 1;
1233 else
1234 len += GLYPH_LENGTH (glyph_table_base, g);
1236 ++beg;
1240 return len;
1244 /* Return true if the glyph rows A and B have equal contents.
1245 MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too. */
1247 static bool
1248 row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
1250 eassert (verify_row_hash (a));
1251 eassert (verify_row_hash (b));
1253 if (a == b)
1254 return 1;
1255 else if (a->hash != b->hash)
1256 return 0;
1257 else
1259 struct glyph *a_glyph, *b_glyph, *a_end;
1260 int area;
1262 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1263 return 0;
1265 /* Compare glyphs. */
1266 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1268 if (a->used[area] != b->used[area])
1269 return 0;
1271 a_glyph = a->glyphs[area];
1272 a_end = a_glyph + a->used[area];
1273 b_glyph = b->glyphs[area];
1275 while (a_glyph < a_end
1276 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1277 ++a_glyph, ++b_glyph;
1279 if (a_glyph != a_end)
1280 return 0;
1283 if (a->fill_line_p != b->fill_line_p
1284 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1285 || a->left_fringe_bitmap != b->left_fringe_bitmap
1286 || a->left_fringe_face_id != b->left_fringe_face_id
1287 || a->left_fringe_offset != b->left_fringe_offset
1288 || a->right_fringe_bitmap != b->right_fringe_bitmap
1289 || a->right_fringe_face_id != b->right_fringe_face_id
1290 || a->right_fringe_offset != b->right_fringe_offset
1291 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1292 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1293 || a->exact_window_width_line_p != b->exact_window_width_line_p
1294 || a->overlapped_p != b->overlapped_p
1295 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1296 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1297 || a->reversed_p != b->reversed_p
1298 /* Different partially visible characters on left margin. */
1299 || a->x != b->x
1300 /* Different height. */
1301 || a->ascent != b->ascent
1302 || a->phys_ascent != b->phys_ascent
1303 || a->phys_height != b->phys_height
1304 || a->visible_height != b->visible_height)
1305 return 0;
1308 return 1;
1313 /***********************************************************************
1314 Glyph Pool
1316 See dispextern.h for an overall explanation of glyph pools.
1317 ***********************************************************************/
1319 /* Allocate a glyph_pool structure. The structure returned is
1320 initialized with zeros. The global variable glyph_pool_count is
1321 incremented for each pool allocated. */
1323 static struct glyph_pool *
1324 new_glyph_pool (void)
1326 struct glyph_pool *result = xzalloc (sizeof *result);
1328 /* For memory leak and double deletion checking. */
1329 ++glyph_pool_count;
1331 return result;
1335 /* Free a glyph_pool structure POOL. The function may be called with
1336 a null POOL pointer. The global variable glyph_pool_count is
1337 decremented with every pool structure freed. If this count gets
1338 negative, more structures were freed than allocated, i.e. one
1339 structure must have been freed more than once or a bogus pointer
1340 was passed to free_glyph_pool. */
1342 static void
1343 free_glyph_pool (struct glyph_pool *pool)
1345 if (pool)
1347 /* More freed than allocated? */
1348 --glyph_pool_count;
1349 eassert (glyph_pool_count >= 0);
1351 xfree (pool->glyphs);
1352 xfree (pool);
1357 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1358 columns we need. This function never shrinks a pool. The only
1359 case in which this would make sense, would be when a frame's size
1360 is changed from a large value to a smaller one. But, if someone
1361 does it once, we can expect that he will do it again.
1363 Return true if the pool changed in a way which makes
1364 re-adjusting window glyph matrices necessary. */
1366 static bool
1367 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1369 ptrdiff_t needed;
1370 bool changed_p;
1372 changed_p = (pool->glyphs == 0
1373 || matrix_dim.height != pool->nrows
1374 || matrix_dim.width != pool->ncolumns);
1376 /* Enlarge the glyph pool. */
1377 needed = matrix_dim.width;
1378 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1379 memory_full (SIZE_MAX);
1380 needed *= matrix_dim.height;
1381 if (needed > pool->nglyphs)
1383 ptrdiff_t old_nglyphs = pool->nglyphs;
1384 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1385 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1386 memset (pool->glyphs + old_nglyphs, 0,
1387 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1390 /* Remember the number of rows and columns because (a) we use them
1391 to do sanity checks, and (b) the number of columns determines
1392 where rows in the frame matrix start---this must be available to
1393 determine pointers to rows of window sub-matrices. */
1394 pool->nrows = matrix_dim.height;
1395 pool->ncolumns = matrix_dim.width;
1397 return changed_p;
1402 /***********************************************************************
1403 Debug Code
1404 ***********************************************************************/
1406 #ifdef GLYPH_DEBUG
1409 /* Flush standard output. This is sometimes useful to call from the debugger.
1410 XXX Maybe this should be changed to flush the current terminal instead of
1411 stdout.
1414 void flush_stdout (void) EXTERNALLY_VISIBLE;
1416 void
1417 flush_stdout (void)
1419 fflush (stdout);
1423 /* Check that no glyph pointers have been lost in MATRIX. If a
1424 pointer has been lost, e.g. by using a structure assignment between
1425 rows, at least one pointer must occur more than once in the rows of
1426 MATRIX. */
1428 void
1429 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1431 int i, j;
1433 for (i = 0; i < matrix->nrows; ++i)
1434 for (j = 0; j < matrix->nrows; ++j)
1435 eassert (i == j
1436 || (matrix->rows[i].glyphs[TEXT_AREA]
1437 != matrix->rows[j].glyphs[TEXT_AREA]));
1441 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1443 struct glyph_row *
1444 matrix_row (struct glyph_matrix *matrix, int row)
1446 eassert (matrix && matrix->rows);
1447 eassert (row >= 0 && row < matrix->nrows);
1449 /* That's really too slow for normal testing because this function
1450 is called almost everywhere. Although---it's still astonishingly
1451 fast, so it is valuable to have for debugging purposes. */
1452 #if 0
1453 check_matrix_pointer_lossage (matrix);
1454 #endif
1456 return matrix->rows + row;
1460 #if 0 /* This function makes invalid assumptions when text is
1461 partially invisible. But it might come handy for debugging
1462 nevertheless. */
1464 /* Check invariants that must hold for an up to date current matrix of
1465 window W. */
1467 static void
1468 check_matrix_invariants (struct window *w)
1470 struct glyph_matrix *matrix = w->current_matrix;
1471 int yb = window_text_bottom_y (w);
1472 struct glyph_row *row = matrix->rows;
1473 struct glyph_row *last_text_row = NULL;
1474 struct buffer *saved = current_buffer;
1475 struct buffer *buffer = XBUFFER (w->buffer);
1476 int c;
1478 /* This can sometimes happen for a fresh window. */
1479 if (matrix->nrows < 2)
1480 return;
1482 set_buffer_temp (buffer);
1484 /* Note: last row is always reserved for the mode line. */
1485 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1486 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1488 struct glyph_row *next = row + 1;
1490 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1491 last_text_row = row;
1493 /* Check that character and byte positions are in sync. */
1494 eassert (MATRIX_ROW_START_BYTEPOS (row)
1495 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1496 eassert (BYTEPOS (row->start.pos)
1497 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1499 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1500 have such a position temporarily in case of a minibuffer
1501 displaying something like `[Sole completion]' at its end. */
1502 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1504 eassert (MATRIX_ROW_END_BYTEPOS (row)
1505 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1506 eassert (BYTEPOS (row->end.pos)
1507 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1510 /* Check that end position of `row' is equal to start position
1511 of next row. */
1512 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1514 eassert (MATRIX_ROW_END_CHARPOS (row)
1515 == MATRIX_ROW_START_CHARPOS (next));
1516 eassert (MATRIX_ROW_END_BYTEPOS (row)
1517 == MATRIX_ROW_START_BYTEPOS (next));
1518 eassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1519 eassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1521 row = next;
1524 eassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1525 eassert (w->desired_matrix->rows != NULL);
1526 set_buffer_temp (saved);
1529 #endif /* 0 */
1531 #endif /* GLYPH_DEBUG */
1535 /**********************************************************************
1536 Allocating/ Adjusting Glyph Matrices
1537 **********************************************************************/
1539 /* Allocate glyph matrices over a window tree for a frame-based
1540 redisplay
1542 X and Y are column/row within the frame glyph matrix where
1543 sub-matrices for the window tree rooted at WINDOW must be
1544 allocated. DIM_ONLY_P means that the caller of this
1545 function is only interested in the result matrix dimension, and
1546 matrix adjustments should not be performed.
1548 The function returns the total width/height of the sub-matrices of
1549 the window tree. If called on a frame root window, the computation
1550 will take the mini-buffer window into account.
1552 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1554 NEW_LEAF_MATRIX set if any window in the tree did not have a
1555 glyph matrices yet, and
1557 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1558 any window in the tree will be changed or have been changed (see
1559 DIM_ONLY_P)
1561 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1562 function.
1564 Windows are arranged into chains of windows on the same level
1565 through the next fields of window structures. Such a level can be
1566 either a sequence of horizontally adjacent windows from left to
1567 right, or a sequence of vertically adjacent windows from top to
1568 bottom. Each window in a horizontal sequence can be either a leaf
1569 window or a vertical sequence; a window in a vertical sequence can
1570 be either a leaf or a horizontal sequence. All windows in a
1571 horizontal sequence have the same height, and all windows in a
1572 vertical sequence have the same width.
1574 This function uses, for historical reasons, a more general
1575 algorithm to determine glyph matrix dimensions that would be
1576 necessary.
1578 The matrix height of a horizontal sequence is determined by the
1579 maximum height of any matrix in the sequence. The matrix width of
1580 a horizontal sequence is computed by adding up matrix widths of
1581 windows in the sequence.
1583 |<------- result width ------->|
1584 +---------+----------+---------+ ---
1585 | | | | |
1586 | | | |
1587 +---------+ | | result height
1588 | +---------+
1589 | | |
1590 +----------+ ---
1592 The matrix width of a vertical sequence is the maximum matrix width
1593 of any window in the sequence. Its height is computed by adding up
1594 matrix heights of windows in the sequence.
1596 |<---- result width -->|
1597 +---------+ ---
1598 | | |
1599 | | |
1600 +---------+--+ |
1601 | | |
1602 | | result height
1604 +------------+---------+ |
1605 | | |
1606 | | |
1607 +------------+---------+ --- */
1609 /* Bit indicating that a new matrix will be allocated or has been
1610 allocated. */
1612 #define NEW_LEAF_MATRIX (1 << 0)
1614 /* Bit indicating that a matrix will or has changed its location or
1615 size. */
1617 #define CHANGED_LEAF_MATRIX (1 << 1)
1619 static struct dim
1620 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1621 bool dim_only_p, int *window_change_flags)
1623 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1624 int x0 = x, y0 = y;
1625 int wmax = 0, hmax = 0;
1626 struct dim total;
1627 struct dim dim;
1628 struct window *w;
1629 bool in_horz_combination_p;
1631 /* What combination is WINDOW part of? Compute this once since the
1632 result is the same for all windows in the `next' chain. The
1633 special case of a root window (parent equal to nil) is treated
1634 like a vertical combination because a root window's `next'
1635 points to the mini-buffer window, if any, which is arranged
1636 vertically below other windows. */
1637 in_horz_combination_p
1638 = (!NILP (XWINDOW (window)->parent)
1639 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1641 /* For WINDOW and all windows on the same level. */
1644 w = XWINDOW (window);
1646 /* Get the dimension of the window sub-matrix for W, depending
1647 on whether this is a combination or a leaf window. */
1648 if (!NILP (w->hchild))
1649 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
1650 dim_only_p,
1651 window_change_flags);
1652 else if (!NILP (w->vchild))
1653 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
1654 dim_only_p,
1655 window_change_flags);
1656 else
1658 /* If not already done, allocate sub-matrix structures. */
1659 if (w->desired_matrix == NULL)
1661 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1662 w->current_matrix = new_glyph_matrix (f->current_pool);
1663 *window_change_flags |= NEW_LEAF_MATRIX;
1666 /* Width and height MUST be chosen so that there are no
1667 holes in the frame matrix. */
1668 dim.width = required_matrix_width (w);
1669 dim.height = required_matrix_height (w);
1671 /* Will matrix be re-allocated? */
1672 if (x != w->desired_matrix->matrix_x
1673 || y != w->desired_matrix->matrix_y
1674 || dim.width != w->desired_matrix->matrix_w
1675 || dim.height != w->desired_matrix->matrix_h
1676 || (margin_glyphs_to_reserve (w, dim.width,
1677 w->left_margin_cols)
1678 != w->desired_matrix->left_margin_glyphs)
1679 || (margin_glyphs_to_reserve (w, dim.width,
1680 w->right_margin_cols)
1681 != w->desired_matrix->right_margin_glyphs))
1682 *window_change_flags |= CHANGED_LEAF_MATRIX;
1684 /* Actually change matrices, if allowed. Do not consider
1685 CHANGED_LEAF_MATRIX computed above here because the pool
1686 may have been changed which we don't now here. We trust
1687 that we only will be called with DIM_ONLY_P when
1688 necessary. */
1689 if (!dim_only_p)
1691 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1692 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1696 /* If we are part of a horizontal combination, advance x for
1697 windows to the right of W; otherwise advance y for windows
1698 below W. */
1699 if (in_horz_combination_p)
1700 x += dim.width;
1701 else
1702 y += dim.height;
1704 /* Remember maximum glyph matrix dimensions. */
1705 wmax = max (wmax, dim.width);
1706 hmax = max (hmax, dim.height);
1708 /* Next window on same level. */
1709 window = w->next;
1711 while (!NILP (window));
1713 /* Set `total' to the total glyph matrix dimension of this window
1714 level. In a vertical combination, the width is the width of the
1715 widest window; the height is the y we finally reached, corrected
1716 by the y we started with. In a horizontal combination, the total
1717 height is the height of the tallest window, and the width is the
1718 x we finally reached, corrected by the x we started with. */
1719 if (in_horz_combination_p)
1721 total.width = x - x0;
1722 total.height = hmax;
1724 else
1726 total.width = wmax;
1727 total.height = y - y0;
1730 return total;
1734 /* Return the required height of glyph matrices for window W. */
1736 static int
1737 required_matrix_height (struct window *w)
1739 #ifdef HAVE_WINDOW_SYSTEM
1740 struct frame *f = XFRAME (w->frame);
1742 if (FRAME_WINDOW_P (f))
1744 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1745 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1746 return (((window_pixel_height + ch_height - 1)
1747 / ch_height) * w->nrows_scale_factor
1748 /* One partially visible line at the top and
1749 bottom of the window. */
1751 /* 2 for header and mode line. */
1752 + 2);
1754 #endif /* HAVE_WINDOW_SYSTEM */
1756 return WINDOW_TOTAL_LINES (w);
1760 /* Return the required width of glyph matrices for window W. */
1762 static int
1763 required_matrix_width (struct window *w)
1765 #ifdef HAVE_WINDOW_SYSTEM
1766 struct frame *f = XFRAME (w->frame);
1767 if (FRAME_WINDOW_P (f))
1769 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1770 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1772 /* Compute number of glyphs needed in a glyph row. */
1773 return (((window_pixel_width + ch_width - 1)
1774 / ch_width) * w->ncols_scale_factor
1775 /* 2 partially visible columns in the text area. */
1777 /* One partially visible column at the right
1778 edge of each marginal area. */
1779 + 1 + 1);
1781 #endif /* HAVE_WINDOW_SYSTEM */
1783 return w->total_cols;
1787 /* Allocate window matrices for window-based redisplay. W is the
1788 window whose matrices must be allocated/reallocated. */
1790 static void
1791 allocate_matrices_for_window_redisplay (struct window *w)
1793 while (w)
1795 if (!NILP (w->vchild))
1796 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
1797 else if (!NILP (w->hchild))
1798 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
1799 else
1801 /* W is a leaf window. */
1802 struct dim dim;
1804 /* If matrices are not yet allocated, allocate them now. */
1805 if (w->desired_matrix == NULL)
1807 w->desired_matrix = new_glyph_matrix (NULL);
1808 w->current_matrix = new_glyph_matrix (NULL);
1811 dim.width = required_matrix_width (w);
1812 dim.height = required_matrix_height (w);
1813 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1814 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1817 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1822 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1823 do it for all frames; otherwise do it just for the given frame.
1824 This function must be called when a new frame is created, its size
1825 changes, or its window configuration changes. */
1827 void
1828 adjust_glyphs (struct frame *f)
1830 /* Block input so that expose events and other events that access
1831 glyph matrices are not processed while we are changing them. */
1832 block_input ();
1834 if (f)
1835 adjust_frame_glyphs (f);
1836 else
1838 Lisp_Object tail, lisp_frame;
1840 FOR_EACH_FRAME (tail, lisp_frame)
1841 adjust_frame_glyphs (XFRAME (lisp_frame));
1844 unblock_input ();
1847 /* Allocate/reallocate glyph matrices of a single frame F. */
1849 static void
1850 adjust_frame_glyphs (struct frame *f)
1852 if (FRAME_WINDOW_P (f))
1853 adjust_frame_glyphs_for_window_redisplay (f);
1854 else
1855 adjust_frame_glyphs_for_frame_redisplay (f);
1857 /* Don't forget the buffer for decode_mode_spec. */
1858 adjust_decode_mode_spec_buffer (f);
1860 f->glyphs_initialized_p = 1;
1863 /* Return true if any window in the tree has nonzero window margins. See
1864 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1865 static bool
1866 showing_window_margins_p (struct window *w)
1868 while (w)
1870 if (!NILP (w->hchild))
1872 if (showing_window_margins_p (XWINDOW (w->hchild)))
1873 return 1;
1875 else if (!NILP (w->vchild))
1877 if (showing_window_margins_p (XWINDOW (w->vchild)))
1878 return 1;
1880 else if (!NILP (w->left_margin_cols)
1881 || !NILP (w->right_margin_cols))
1882 return 1;
1884 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1886 return 0;
1890 /* In the window tree with root W, build current matrices of leaf
1891 windows from the frame's current matrix. */
1893 static void
1894 fake_current_matrices (Lisp_Object window)
1896 struct window *w;
1898 for (; !NILP (window); window = w->next)
1900 w = XWINDOW (window);
1902 if (!NILP (w->hchild))
1903 fake_current_matrices (w->hchild);
1904 else if (!NILP (w->vchild))
1905 fake_current_matrices (w->vchild);
1906 else
1908 int i;
1909 struct frame *f = XFRAME (w->frame);
1910 struct glyph_matrix *m = w->current_matrix;
1911 struct glyph_matrix *fm = f->current_matrix;
1913 eassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
1914 eassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
1916 for (i = 0; i < m->matrix_h; ++i)
1918 struct glyph_row *r = m->rows + i;
1919 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
1921 eassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
1922 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
1924 r->enabled_p = fr->enabled_p;
1925 if (r->enabled_p)
1927 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
1928 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
1929 r->used[TEXT_AREA] = (m->matrix_w
1930 - r->used[LEFT_MARGIN_AREA]
1931 - r->used[RIGHT_MARGIN_AREA]);
1932 r->mode_line_p = 0;
1940 /* Save away the contents of frame F's current frame matrix. Value is
1941 a glyph matrix holding the contents of F's current frame matrix. */
1943 static struct glyph_matrix *
1944 save_current_matrix (struct frame *f)
1946 int i;
1947 struct glyph_matrix *saved = xzalloc (sizeof *saved);
1948 saved->nrows = f->current_matrix->nrows;
1949 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
1951 for (i = 0; i < saved->nrows; ++i)
1953 struct glyph_row *from = f->current_matrix->rows + i;
1954 struct glyph_row *to = saved->rows + i;
1955 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1956 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
1957 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1958 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1961 return saved;
1965 /* Restore the contents of frame F's current frame matrix from SAVED,
1966 and free memory associated with SAVED. */
1968 static void
1969 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
1971 int i;
1973 for (i = 0; i < saved->nrows; ++i)
1975 struct glyph_row *from = saved->rows + i;
1976 struct glyph_row *to = f->current_matrix->rows + i;
1977 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1978 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1979 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1980 xfree (from->glyphs[TEXT_AREA]);
1983 xfree (saved->rows);
1984 xfree (saved);
1989 /* Allocate/reallocate glyph matrices of a single frame F for
1990 frame-based redisplay. */
1992 static void
1993 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
1995 struct dim matrix_dim;
1996 bool pool_changed_p;
1997 int window_change_flags;
1998 int top_window_y;
2000 if (!FRAME_LIVE_P (f))
2001 return;
2003 top_window_y = FRAME_TOP_MARGIN (f);
2005 /* Allocate glyph pool structures if not already done. */
2006 if (f->desired_pool == NULL)
2008 f->desired_pool = new_glyph_pool ();
2009 f->current_pool = new_glyph_pool ();
2012 /* Allocate frames matrix structures if needed. */
2013 if (f->desired_matrix == NULL)
2015 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2016 f->current_matrix = new_glyph_matrix (f->current_pool);
2019 /* Compute window glyph matrices. (This takes the mini-buffer
2020 window into account). The result is the size of the frame glyph
2021 matrix needed. The variable window_change_flags is set to a bit
2022 mask indicating whether new matrices will be allocated or
2023 existing matrices change their size or location within the frame
2024 matrix. */
2025 window_change_flags = 0;
2026 matrix_dim
2027 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2028 0, top_window_y,
2030 &window_change_flags);
2032 /* Add in menu bar lines, if any. */
2033 matrix_dim.height += top_window_y;
2035 /* Enlarge pools as necessary. */
2036 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2037 realloc_glyph_pool (f->current_pool, matrix_dim);
2039 /* Set up glyph pointers within window matrices. Do this only if
2040 absolutely necessary since it requires a frame redraw. */
2041 if (pool_changed_p || window_change_flags)
2043 /* Do it for window matrices. */
2044 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2045 0, top_window_y, 0,
2046 &window_change_flags);
2048 /* Size of frame matrices must equal size of frame. Note
2049 that we are called for X frames with window widths NOT equal
2050 to the frame width (from CHANGE_FRAME_SIZE_1). */
2051 eassert (matrix_dim.width == FRAME_COLS (f)
2052 && matrix_dim.height == FRAME_LINES (f));
2054 /* Pointers to glyph memory in glyph rows are exchanged during
2055 the update phase of redisplay, which means in general that a
2056 frame's current matrix consists of pointers into both the
2057 desired and current glyph pool of the frame. Adjusting a
2058 matrix sets the frame matrix up so that pointers are all into
2059 the same pool. If we want to preserve glyph contents of the
2060 current matrix over a call to adjust_glyph_matrix, we must
2061 make a copy of the current glyphs, and restore the current
2062 matrix' contents from that copy. */
2063 if (display_completed
2064 && !FRAME_GARBAGED_P (f)
2065 && matrix_dim.width == f->current_matrix->matrix_w
2066 && matrix_dim.height == f->current_matrix->matrix_h
2067 /* For some reason, the frame glyph matrix gets corrupted if
2068 any of the windows contain margins. I haven't been able
2069 to hunt down the reason, but for the moment this prevents
2070 the problem from manifesting. -- cyd */
2071 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2073 struct glyph_matrix *copy = save_current_matrix (f);
2074 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2075 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2076 restore_current_matrix (f, copy);
2077 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2079 else
2081 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2082 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2083 SET_FRAME_GARBAGED (f);
2089 /* Allocate/reallocate glyph matrices of a single frame F for
2090 window-based redisplay. */
2092 static void
2093 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2095 eassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2097 /* Allocate/reallocate window matrices. */
2098 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2100 #ifdef HAVE_X_WINDOWS
2101 /* Allocate/ reallocate matrices of the dummy window used to display
2102 the menu bar under X when no X toolkit support is available. */
2103 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2105 /* Allocate a dummy window if not already done. */
2106 struct window *w;
2107 if (NILP (f->menu_bar_window))
2109 Lisp_Object frame;
2110 fset_menu_bar_window (f, make_window ());
2111 w = XWINDOW (f->menu_bar_window);
2112 XSETFRAME (frame, f);
2113 wset_frame (w, frame);
2114 w->pseudo_window_p = 1;
2116 else
2117 w = XWINDOW (f->menu_bar_window);
2119 /* Set window dimensions to frame dimensions and allocate or
2120 adjust glyph matrices of W. */
2121 w->top_line = 0;
2122 w->left_col = 0;
2123 w->total_lines = FRAME_MENU_BAR_LINES (f);
2124 w->total_cols = FRAME_TOTAL_COLS (f);
2125 allocate_matrices_for_window_redisplay (w);
2127 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2128 #endif /* HAVE_X_WINDOWS */
2130 #ifndef USE_GTK
2132 /* Allocate/ reallocate matrices of the tool bar window. If we
2133 don't have a tool bar window yet, make one. */
2134 struct window *w;
2135 if (NILP (f->tool_bar_window))
2137 Lisp_Object frame;
2138 fset_tool_bar_window (f, make_window ());
2139 w = XWINDOW (f->tool_bar_window);
2140 XSETFRAME (frame, f);
2141 wset_frame (w, frame);
2142 w->pseudo_window_p = 1;
2144 else
2145 w = XWINDOW (f->tool_bar_window);
2147 w->top_line = FRAME_MENU_BAR_LINES (f);
2148 w->left_col = 0;
2149 w->total_lines = FRAME_TOOL_BAR_LINES (f);
2150 w->total_cols = FRAME_TOTAL_COLS (f);
2151 allocate_matrices_for_window_redisplay (w);
2153 #endif
2157 /* Re-allocate buffer for decode_mode_spec on frame F. */
2159 static void
2160 adjust_decode_mode_spec_buffer (struct frame *f)
2162 f->decode_mode_spec_buffer = xrealloc (f->decode_mode_spec_buffer,
2163 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2168 /**********************************************************************
2169 Freeing Glyph Matrices
2170 **********************************************************************/
2172 /* Free glyph memory for a frame F. F may be null. This function can
2173 be called for the same frame more than once. The root window of
2174 F may be nil when this function is called. This is the case when
2175 the function is called when F is destroyed. */
2177 void
2178 free_glyphs (struct frame *f)
2180 if (f && f->glyphs_initialized_p)
2182 /* Block interrupt input so that we don't get surprised by an X
2183 event while we're in an inconsistent state. */
2184 block_input ();
2185 f->glyphs_initialized_p = 0;
2187 /* Release window sub-matrices. */
2188 if (!NILP (f->root_window))
2189 free_window_matrices (XWINDOW (f->root_window));
2191 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2192 /* Free the dummy window for menu bars without X toolkit and its
2193 glyph matrices. */
2194 if (!NILP (f->menu_bar_window))
2196 struct window *w = XWINDOW (f->menu_bar_window);
2197 free_glyph_matrix (w->desired_matrix);
2198 free_glyph_matrix (w->current_matrix);
2199 w->desired_matrix = w->current_matrix = NULL;
2200 fset_menu_bar_window (f, Qnil);
2202 #endif
2204 /* Free the tool bar window and its glyph matrices. */
2205 if (!NILP (f->tool_bar_window))
2207 struct window *w = XWINDOW (f->tool_bar_window);
2208 free_glyph_matrix (w->desired_matrix);
2209 free_glyph_matrix (w->current_matrix);
2210 w->desired_matrix = w->current_matrix = NULL;
2211 fset_tool_bar_window (f, Qnil);
2214 /* Release frame glyph matrices. Reset fields to zero in
2215 case we are called a second time. */
2216 if (f->desired_matrix)
2218 free_glyph_matrix (f->desired_matrix);
2219 free_glyph_matrix (f->current_matrix);
2220 f->desired_matrix = f->current_matrix = NULL;
2223 /* Release glyph pools. */
2224 if (f->desired_pool)
2226 free_glyph_pool (f->desired_pool);
2227 free_glyph_pool (f->current_pool);
2228 f->desired_pool = f->current_pool = NULL;
2231 unblock_input ();
2236 /* Free glyph sub-matrices in the window tree rooted at W. This
2237 function may be called with a null pointer, and it may be called on
2238 the same tree more than once. */
2240 void
2241 free_window_matrices (struct window *w)
2243 while (w)
2245 if (!NILP (w->hchild))
2246 free_window_matrices (XWINDOW (w->hchild));
2247 else if (!NILP (w->vchild))
2248 free_window_matrices (XWINDOW (w->vchild));
2249 else
2251 /* This is a leaf window. Free its memory and reset fields
2252 to zero in case this function is called a second time for
2253 W. */
2254 free_glyph_matrix (w->current_matrix);
2255 free_glyph_matrix (w->desired_matrix);
2256 w->current_matrix = w->desired_matrix = NULL;
2259 /* Next window on same level. */
2260 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2265 /* Check glyph memory leaks. This function is called from
2266 shut_down_emacs. Note that frames are not destroyed when Emacs
2267 exits. We therefore free all glyph memory for all active frames
2268 explicitly and check that nothing is left allocated. */
2270 void
2271 check_glyph_memory (void)
2273 Lisp_Object tail, frame;
2275 /* Free glyph memory for all frames. */
2276 FOR_EACH_FRAME (tail, frame)
2277 free_glyphs (XFRAME (frame));
2279 /* Check that nothing is left allocated. */
2280 if (glyph_matrix_count)
2281 emacs_abort ();
2282 if (glyph_pool_count)
2283 emacs_abort ();
2288 /**********************************************************************
2289 Building a Frame Matrix
2290 **********************************************************************/
2292 /* Most of the redisplay code works on glyph matrices attached to
2293 windows. This is a good solution most of the time, but it is not
2294 suitable for terminal code. Terminal output functions cannot rely
2295 on being able to set an arbitrary terminal window. Instead they
2296 must be provided with a view of the whole frame, i.e. the whole
2297 screen. We build such a view by constructing a frame matrix from
2298 window matrices in this section.
2300 Windows that must be updated have their must_be_update_p flag set.
2301 For all such windows, their desired matrix is made part of the
2302 desired frame matrix. For other windows, their current matrix is
2303 made part of the desired frame matrix.
2305 +-----------------+----------------+
2306 | desired | desired |
2307 | | |
2308 +-----------------+----------------+
2309 | current |
2311 +----------------------------------+
2313 Desired window matrices can be made part of the frame matrix in a
2314 cheap way: We exploit the fact that the desired frame matrix and
2315 desired window matrices share their glyph memory. This is not
2316 possible for current window matrices. Their glyphs are copied to
2317 the desired frame matrix. The latter is equivalent to
2318 preserve_other_columns in the old redisplay.
2320 Used glyphs counters for frame matrix rows are the result of adding
2321 up glyph lengths of the window matrices. A line in the frame
2322 matrix is enabled, if a corresponding line in a window matrix is
2323 enabled.
2325 After building the desired frame matrix, it will be passed to
2326 terminal code, which will manipulate both the desired and current
2327 frame matrix. Changes applied to the frame's current matrix have
2328 to be visible in current window matrices afterwards, of course.
2330 This problem is solved like this:
2332 1. Window and frame matrices share glyphs. Window matrices are
2333 constructed in a way that their glyph contents ARE the glyph
2334 contents needed in a frame matrix. Thus, any modification of
2335 glyphs done in terminal code will be reflected in window matrices
2336 automatically.
2338 2. Exchanges of rows in a frame matrix done by terminal code are
2339 intercepted by hook functions so that corresponding row operations
2340 on window matrices can be performed. This is necessary because we
2341 use pointers to glyphs in glyph row structures. To satisfy the
2342 assumption of point 1 above that glyphs are updated implicitly in
2343 window matrices when they are manipulated via the frame matrix,
2344 window and frame matrix must of course agree where to find the
2345 glyphs for their rows. Possible manipulations that must be
2346 mirrored are assignments of rows of the desired frame matrix to the
2347 current frame matrix and scrolling the current frame matrix. */
2349 /* Build frame F's desired matrix from window matrices. Only windows
2350 which have the flag must_be_updated_p set have to be updated. Menu
2351 bar lines of a frame are not covered by window matrices, so make
2352 sure not to touch them in this function. */
2354 static void
2355 build_frame_matrix (struct frame *f)
2357 int i;
2359 /* F must have a frame matrix when this function is called. */
2360 eassert (!FRAME_WINDOW_P (f));
2362 /* Clear all rows in the frame matrix covered by window matrices.
2363 Menu bar lines are not covered by windows. */
2364 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2365 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2367 /* Build the matrix by walking the window tree. */
2368 build_frame_matrix_from_window_tree (f->desired_matrix,
2369 XWINDOW (FRAME_ROOT_WINDOW (f)));
2373 /* Walk a window tree, building a frame matrix MATRIX from window
2374 matrices. W is the root of a window tree. */
2376 static void
2377 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2379 while (w)
2381 if (!NILP (w->hchild))
2382 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2383 else if (!NILP (w->vchild))
2384 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2385 else
2386 build_frame_matrix_from_leaf_window (matrix, w);
2388 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2393 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2394 desired frame matrix built. W is a leaf window whose desired or
2395 current matrix is to be added to FRAME_MATRIX. W's flag
2396 must_be_updated_p determines which matrix it contributes to
2397 FRAME_MATRIX. If W->must_be_updated_p, W's desired matrix
2398 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2399 Adding a desired matrix means setting up used counters and such in
2400 frame rows, while adding a current window matrix to FRAME_MATRIX
2401 means copying glyphs. The latter case corresponds to
2402 preserve_other_columns in the old redisplay. */
2404 static void
2405 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2407 struct glyph_matrix *window_matrix;
2408 int window_y, frame_y;
2409 /* If non-zero, a glyph to insert at the right border of W. */
2410 GLYPH right_border_glyph;
2412 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2414 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2415 if (w->must_be_updated_p)
2417 window_matrix = w->desired_matrix;
2419 /* Decide whether we want to add a vertical border glyph. */
2420 if (!WINDOW_RIGHTMOST_P (w))
2422 struct Lisp_Char_Table *dp = window_display_table (w);
2423 Lisp_Object gc;
2425 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2426 if (dp
2427 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc)))
2429 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2430 spec_glyph_lookup_face (w, &right_border_glyph);
2433 if (GLYPH_FACE (right_border_glyph) <= 0)
2434 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2437 else
2438 window_matrix = w->current_matrix;
2440 /* For all rows in the window matrix and corresponding rows in the
2441 frame matrix. */
2442 window_y = 0;
2443 frame_y = window_matrix->matrix_y;
2444 while (window_y < window_matrix->nrows)
2446 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2447 struct glyph_row *window_row = window_matrix->rows + window_y;
2448 bool current_row_p = window_matrix == w->current_matrix;
2450 /* Fill up the frame row with spaces up to the left margin of the
2451 window row. */
2452 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2454 /* Fill up areas in the window matrix row with spaces. */
2455 fill_up_glyph_row_with_spaces (window_row);
2457 /* If only part of W's desired matrix has been built, and
2458 window_row wasn't displayed, use the corresponding current
2459 row instead. */
2460 if (window_matrix == w->desired_matrix
2461 && !window_row->enabled_p)
2463 window_row = w->current_matrix->rows + window_y;
2464 current_row_p = 1;
2467 if (current_row_p)
2469 /* Copy window row to frame row. */
2470 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2471 window_row->glyphs[0],
2472 window_matrix->matrix_w * sizeof (struct glyph));
2474 else
2476 eassert (window_row->enabled_p);
2478 /* Only when a desired row has been displayed, we want
2479 the corresponding frame row to be updated. */
2480 frame_row->enabled_p = 1;
2482 /* Maybe insert a vertical border between horizontally adjacent
2483 windows. */
2484 if (GLYPH_CHAR (right_border_glyph) != 0)
2486 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2487 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2490 #ifdef GLYPH_DEBUG
2491 /* Window row window_y must be a slice of frame row
2492 frame_y. */
2493 eassert (glyph_row_slice_p (window_row, frame_row));
2495 /* If rows are in sync, we don't have to copy glyphs because
2496 frame and window share glyphs. */
2498 strcpy (w->current_matrix->method, w->desired_matrix->method);
2499 add_window_display_history (w, w->current_matrix->method, 0);
2500 #endif
2503 /* Set number of used glyphs in the frame matrix. Since we fill
2504 up with spaces, and visit leaf windows from left to right it
2505 can be done simply. */
2506 frame_row->used[TEXT_AREA]
2507 = window_matrix->matrix_x + window_matrix->matrix_w;
2509 /* Next row. */
2510 ++window_y;
2511 ++frame_y;
2515 /* Given a user-specified glyph, possibly including a Lisp-level face
2516 ID, return a glyph that has a realized face ID.
2517 This is used for glyphs displayed specially and not part of the text;
2518 for instance, vertical separators, truncation markers, etc. */
2520 void
2521 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2523 int lface_id = GLYPH_FACE (*glyph);
2524 /* Convert the glyph's specified face to a realized (cache) face. */
2525 if (lface_id > 0)
2527 int face_id = merge_faces (XFRAME (w->frame),
2528 Qt, lface_id, DEFAULT_FACE_ID);
2529 SET_GLYPH_FACE (*glyph, face_id);
2533 /* Add spaces to a glyph row ROW in a window matrix.
2535 Each row has the form:
2537 +---------+-----------------------------+------------+
2538 | left | text | right |
2539 +---------+-----------------------------+------------+
2541 Left and right marginal areas are optional. This function adds
2542 spaces to areas so that there are no empty holes between areas.
2543 In other words: If the right area is not empty, the text area
2544 is filled up with spaces up to the right area. If the text area
2545 is not empty, the left area is filled up.
2547 To be called for frame-based redisplay, only. */
2549 static void
2550 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2552 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2553 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2554 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2558 /* Fill area AREA of glyph row ROW with spaces. To be called for
2559 frame-based redisplay only. */
2561 static void
2562 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2564 if (row->glyphs[area] < row->glyphs[area + 1])
2566 struct glyph *end = row->glyphs[area + 1];
2567 struct glyph *text = row->glyphs[area] + row->used[area];
2569 while (text < end)
2570 *text++ = space_glyph;
2571 row->used[area] = text - row->glyphs[area];
2576 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2577 reached. In frame matrices only one area, TEXT_AREA, is used. */
2579 static void
2580 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2582 int i = row->used[TEXT_AREA];
2583 struct glyph *glyph = row->glyphs[TEXT_AREA];
2585 while (i < upto)
2586 glyph[i++] = space_glyph;
2588 row->used[TEXT_AREA] = i;
2593 /**********************************************************************
2594 Mirroring operations on frame matrices in window matrices
2595 **********************************************************************/
2597 /* Set frame being updated via frame-based redisplay to F. This
2598 function must be called before updates to make explicit that we are
2599 working on frame matrices or not. */
2601 static void
2602 set_frame_matrix_frame (struct frame *f)
2604 frame_matrix_frame = f;
2608 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2609 DESIRED_MATRIX is the desired matrix corresponding to
2610 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2611 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2612 frame_matrix_frame is non-null, this indicates that the exchange is
2613 done in frame matrices, and that we have to perform analogous
2614 operations in window matrices of frame_matrix_frame. */
2616 static void
2617 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2619 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2620 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2621 bool mouse_face_p = current_row->mouse_face_p;
2623 /* Do current_row = desired_row. This exchanges glyph pointers
2624 between both rows, and does a structure assignment otherwise. */
2625 assign_row (current_row, desired_row);
2627 /* Enable current_row to mark it as valid. */
2628 current_row->enabled_p = 1;
2629 current_row->mouse_face_p = mouse_face_p;
2631 /* If we are called on frame matrices, perform analogous operations
2632 for window matrices. */
2633 if (frame_matrix_frame)
2634 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2638 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2639 W's frame which has been made current (by swapping pointers between
2640 current and desired matrix). Perform analogous operations in the
2641 matrices of leaf windows in the window tree rooted at W. */
2643 static void
2644 mirror_make_current (struct window *w, int frame_row)
2646 while (w)
2648 if (!NILP (w->hchild))
2649 mirror_make_current (XWINDOW (w->hchild), frame_row);
2650 else if (!NILP (w->vchild))
2651 mirror_make_current (XWINDOW (w->vchild), frame_row);
2652 else
2654 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2655 here because the checks performed in debug mode there
2656 will not allow the conversion. */
2657 int row = frame_row - w->desired_matrix->matrix_y;
2659 /* If FRAME_ROW is within W, assign the desired row to the
2660 current row (exchanging glyph pointers). */
2661 if (row >= 0 && row < w->desired_matrix->matrix_h)
2663 struct glyph_row *current_row
2664 = MATRIX_ROW (w->current_matrix, row);
2665 struct glyph_row *desired_row
2666 = MATRIX_ROW (w->desired_matrix, row);
2668 if (desired_row->enabled_p)
2669 assign_row (current_row, desired_row);
2670 else
2671 swap_glyph_pointers (desired_row, current_row);
2672 current_row->enabled_p = 1;
2674 /* Set the Y coordinate of the mode/header line's row.
2675 It is needed in draw_row_with_mouse_face to find the
2676 screen coordinates. (Window-based redisplay sets
2677 this in update_window, but no one seems to do that
2678 for frame-based redisplay.) */
2679 if (current_row->mode_line_p)
2680 current_row->y = row;
2684 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2689 /* Perform row dance after scrolling. We are working on the range of
2690 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2691 including) in MATRIX. COPY_FROM is a vector containing, for each
2692 row I in the range 0 <= I < NLINES, the index of the original line
2693 to move to I. This index is relative to the row range, i.e. 0 <=
2694 index < NLINES. RETAINED_P is a vector containing zero for each
2695 row 0 <= I < NLINES which is empty.
2697 This function is called from do_scrolling and do_direct_scrolling. */
2699 void
2700 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2701 int *copy_from, char *retained_p)
2703 /* A copy of original rows. */
2704 struct glyph_row *old_rows;
2706 /* Rows to assign to. */
2707 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2709 int i;
2711 /* Make a copy of the original rows. */
2712 old_rows = alloca (nlines * sizeof *old_rows);
2713 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2715 /* Assign new rows, maybe clear lines. */
2716 for (i = 0; i < nlines; ++i)
2718 bool enabled_before_p = new_rows[i].enabled_p;
2720 eassert (i + unchanged_at_top < matrix->nrows);
2721 eassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2722 new_rows[i] = old_rows[copy_from[i]];
2723 new_rows[i].enabled_p = enabled_before_p;
2725 /* RETAINED_P is zero for empty lines. */
2726 if (!retained_p[copy_from[i]])
2727 new_rows[i].enabled_p = 0;
2730 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2731 if (frame_matrix_frame)
2732 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2733 unchanged_at_top, nlines, copy_from, retained_p);
2737 /* Synchronize glyph pointers in the current matrix of window W with
2738 the current frame matrix. */
2740 static void
2741 sync_window_with_frame_matrix_rows (struct window *w)
2743 struct frame *f = XFRAME (w->frame);
2744 struct glyph_row *window_row, *window_row_end, *frame_row;
2745 int left, right, x, width;
2747 /* Preconditions: W must be a leaf window on a tty frame. */
2748 eassert (NILP (w->hchild) && NILP (w->vchild));
2749 eassert (!FRAME_WINDOW_P (f));
2751 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2752 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2753 x = w->current_matrix->matrix_x;
2754 width = w->current_matrix->matrix_w;
2756 window_row = w->current_matrix->rows;
2757 window_row_end = window_row + w->current_matrix->nrows;
2758 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2760 for (; window_row < window_row_end; ++window_row, ++frame_row)
2762 window_row->glyphs[LEFT_MARGIN_AREA]
2763 = frame_row->glyphs[0] + x;
2764 window_row->glyphs[TEXT_AREA]
2765 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2766 window_row->glyphs[LAST_AREA]
2767 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2768 window_row->glyphs[RIGHT_MARGIN_AREA]
2769 = window_row->glyphs[LAST_AREA] - right;
2774 /* Return the window in the window tree rooted in W containing frame
2775 row ROW. Value is null if none is found. */
2777 static struct window *
2778 frame_row_to_window (struct window *w, int row)
2780 struct window *found = NULL;
2782 while (w && !found)
2784 if (!NILP (w->hchild))
2785 found = frame_row_to_window (XWINDOW (w->hchild), row);
2786 else if (!NILP (w->vchild))
2787 found = frame_row_to_window (XWINDOW (w->vchild), row);
2788 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2789 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2790 found = w;
2792 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2795 return found;
2799 /* Perform a line dance in the window tree rooted at W, after
2800 scrolling a frame matrix in mirrored_line_dance.
2802 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2803 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2804 COPY_FROM is a vector containing, for each row I in the range 0 <=
2805 I < NLINES, the index of the original line to move to I. This
2806 index is relative to the row range, i.e. 0 <= index < NLINES.
2807 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2808 which is empty. */
2810 static void
2811 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2813 while (w)
2815 if (!NILP (w->hchild))
2816 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
2817 nlines, copy_from, retained_p);
2818 else if (!NILP (w->vchild))
2819 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
2820 nlines, copy_from, retained_p);
2821 else
2823 /* W is a leaf window, and we are working on its current
2824 matrix m. */
2825 struct glyph_matrix *m = w->current_matrix;
2826 int i;
2827 bool sync_p = 0;
2828 struct glyph_row *old_rows;
2830 /* Make a copy of the original rows of matrix m. */
2831 old_rows = alloca (m->nrows * sizeof *old_rows);
2832 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2834 for (i = 0; i < nlines; ++i)
2836 /* Frame relative line assigned to. */
2837 int frame_to = i + unchanged_at_top;
2839 /* Frame relative line assigned. */
2840 int frame_from = copy_from[i] + unchanged_at_top;
2842 /* Window relative line assigned to. */
2843 int window_to = frame_to - m->matrix_y;
2845 /* Window relative line assigned. */
2846 int window_from = frame_from - m->matrix_y;
2848 /* Is assigned line inside window? */
2849 bool from_inside_window_p
2850 = window_from >= 0 && window_from < m->matrix_h;
2852 /* Is assigned to line inside window? */
2853 bool to_inside_window_p
2854 = window_to >= 0 && window_to < m->matrix_h;
2856 if (from_inside_window_p && to_inside_window_p)
2858 /* Do the assignment. The enabled_p flag is saved
2859 over the assignment because the old redisplay did
2860 that. */
2861 bool enabled_before_p = m->rows[window_to].enabled_p;
2862 m->rows[window_to] = old_rows[window_from];
2863 m->rows[window_to].enabled_p = enabled_before_p;
2865 /* If frame line is empty, window line is empty, too. */
2866 if (!retained_p[copy_from[i]])
2867 m->rows[window_to].enabled_p = 0;
2869 else if (to_inside_window_p)
2871 /* A copy between windows. This is an infrequent
2872 case not worth optimizing. */
2873 struct frame *f = XFRAME (w->frame);
2874 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2875 struct window *w2;
2876 struct glyph_matrix *m2;
2877 int m2_from;
2879 w2 = frame_row_to_window (root, frame_from);
2880 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2881 -nw', FROM_FRAME sometimes has no associated window.
2882 This check avoids a segfault if W2 is null. */
2883 if (w2)
2885 m2 = w2->current_matrix;
2886 m2_from = frame_from - m2->matrix_y;
2887 copy_row_except_pointers (m->rows + window_to,
2888 m2->rows + m2_from);
2890 /* If frame line is empty, window line is empty, too. */
2891 if (!retained_p[copy_from[i]])
2892 m->rows[window_to].enabled_p = 0;
2894 sync_p = 1;
2896 else if (from_inside_window_p)
2897 sync_p = 1;
2900 /* If there was a copy between windows, make sure glyph
2901 pointers are in sync with the frame matrix. */
2902 if (sync_p)
2903 sync_window_with_frame_matrix_rows (w);
2905 /* Check that no pointers are lost. */
2906 CHECK_MATRIX (m);
2909 /* Next window on same level. */
2910 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2915 #ifdef GLYPH_DEBUG
2917 /* Check that window and frame matrices agree about their
2918 understanding where glyphs of the rows are to find. For each
2919 window in the window tree rooted at W, check that rows in the
2920 matrices of leaf window agree with their frame matrices about
2921 glyph pointers. */
2923 static void
2924 check_window_matrix_pointers (struct window *w)
2926 while (w)
2928 if (!NILP (w->hchild))
2929 check_window_matrix_pointers (XWINDOW (w->hchild));
2930 else if (!NILP (w->vchild))
2931 check_window_matrix_pointers (XWINDOW (w->vchild));
2932 else
2934 struct frame *f = XFRAME (w->frame);
2935 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
2936 check_matrix_pointers (w->current_matrix, f->current_matrix);
2939 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2944 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
2945 a window and FRAME_MATRIX is the corresponding frame matrix. For
2946 each row in WINDOW_MATRIX check that it's a slice of the
2947 corresponding frame row. If it isn't, abort. */
2949 static void
2950 check_matrix_pointers (struct glyph_matrix *window_matrix,
2951 struct glyph_matrix *frame_matrix)
2953 /* Row number in WINDOW_MATRIX. */
2954 int i = 0;
2956 /* Row number corresponding to I in FRAME_MATRIX. */
2957 int j = window_matrix->matrix_y;
2959 /* For all rows check that the row in the window matrix is a
2960 slice of the row in the frame matrix. If it isn't we didn't
2961 mirror an operation on the frame matrix correctly. */
2962 while (i < window_matrix->nrows)
2964 if (!glyph_row_slice_p (window_matrix->rows + i,
2965 frame_matrix->rows + j))
2966 emacs_abort ();
2967 ++i, ++j;
2971 #endif /* GLYPH_DEBUG */
2975 /**********************************************************************
2976 VPOS and HPOS translations
2977 **********************************************************************/
2979 #ifdef GLYPH_DEBUG
2981 /* Translate vertical position VPOS which is relative to window W to a
2982 vertical position relative to W's frame. */
2984 static int
2985 window_to_frame_vpos (struct window *w, int vpos)
2987 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
2988 eassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
2989 vpos += WINDOW_TOP_EDGE_LINE (w);
2990 eassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
2991 return vpos;
2995 /* Translate horizontal position HPOS which is relative to window W to
2996 a horizontal position relative to W's frame. */
2998 static int
2999 window_to_frame_hpos (struct window *w, int hpos)
3001 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3002 hpos += WINDOW_LEFT_EDGE_COL (w);
3003 return hpos;
3006 #endif /* GLYPH_DEBUG */
3010 /**********************************************************************
3011 Redrawing Frames
3012 **********************************************************************/
3014 /* Redraw frame F. */
3016 void
3017 redraw_frame (struct frame *f)
3019 /* Error if F has no glyphs. */
3020 eassert (f->glyphs_initialized_p);
3021 update_begin (f);
3022 #ifdef MSDOS
3023 if (FRAME_MSDOS_P (f))
3024 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3025 #endif
3026 clear_frame (f);
3027 clear_current_matrices (f);
3028 update_end (f);
3029 if (FRAME_TERMCAP_P (f))
3030 fflush (FRAME_TTY (f)->output);
3031 windows_or_buffers_changed++;
3032 /* Mark all windows as inaccurate, so that every window will have
3033 its redisplay done. */
3034 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3035 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3036 f->garbaged = 0;
3039 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 0, 1, 0,
3040 doc: /* Clear frame FRAME and output again what is supposed to appear on it.
3041 If FRAME is omitted or nil, the selected frame is used. */)
3042 (Lisp_Object frame)
3044 redraw_frame (decode_live_frame (frame));
3045 return Qnil;
3048 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3049 doc: /* Clear and redisplay all visible frames. */)
3050 (void)
3052 Lisp_Object tail, frame;
3054 FOR_EACH_FRAME (tail, frame)
3055 if (FRAME_VISIBLE_P (XFRAME (frame)))
3056 redraw_frame (XFRAME (frame));
3058 return Qnil;
3063 /***********************************************************************
3064 Frame Update
3065 ***********************************************************************/
3067 /* Update frame F based on the data in desired matrices.
3069 If FORCE_P, don't let redisplay be stopped by detecting pending input.
3070 If INHIBIT_HAIRY_ID_P, don't try scrolling.
3072 Value is true if redisplay was stopped due to pending input. */
3074 bool
3075 update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
3077 /* True means display has been paused because of pending input. */
3078 bool paused_p;
3079 struct window *root_window = XWINDOW (f->root_window);
3081 if (redisplay_dont_pause)
3082 force_p = 1;
3083 else if (!force_p && detect_input_pending_ignore_squeezables ())
3085 paused_p = 1;
3086 goto do_pause;
3089 if (FRAME_WINDOW_P (f))
3091 /* We are working on window matrix basis. All windows whose
3092 flag must_be_updated_p is set have to be updated. */
3094 /* Record that we are not working on frame matrices. */
3095 set_frame_matrix_frame (NULL);
3097 /* Update all windows in the window tree of F, maybe stopping
3098 when pending input is detected. */
3099 update_begin (f);
3101 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
3102 /* Update the menu bar on X frames that don't have toolkit
3103 support. */
3104 if (WINDOWP (f->menu_bar_window))
3105 update_window (XWINDOW (f->menu_bar_window), 1);
3106 #endif
3108 /* Update the tool-bar window, if present. */
3109 if (WINDOWP (f->tool_bar_window))
3111 struct window *w = XWINDOW (f->tool_bar_window);
3113 /* Update tool-bar window. */
3114 if (w->must_be_updated_p)
3116 Lisp_Object tem;
3118 update_window (w, 1);
3119 w->must_be_updated_p = 0;
3121 /* Swap tool-bar strings. We swap because we want to
3122 reuse strings. */
3123 tem = f->current_tool_bar_string;
3124 fset_current_tool_bar_string (f, f->desired_tool_bar_string);
3125 fset_desired_tool_bar_string (f, tem);
3130 /* Update windows. */
3131 paused_p = update_window_tree (root_window, force_p);
3132 update_end (f);
3134 /* This flush is a performance bottleneck under X,
3135 and it doesn't seem to be necessary anyway (in general).
3136 It is necessary when resizing the window with the mouse, or
3137 at least the fringes are not redrawn in a timely manner. ++kfs */
3138 if (f->force_flush_display_p)
3140 FRAME_RIF (f)->flush_display (f);
3141 f->force_flush_display_p = 0;
3144 else
3146 /* We are working on frame matrix basis. Set the frame on whose
3147 frame matrix we operate. */
3148 set_frame_matrix_frame (f);
3150 /* Build F's desired matrix from window matrices. */
3151 build_frame_matrix (f);
3153 /* Update the display */
3154 update_begin (f);
3155 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3156 update_end (f);
3158 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3160 if (FRAME_TTY (f)->termscript)
3161 fflush (FRAME_TTY (f)->termscript);
3162 if (FRAME_TERMCAP_P (f))
3163 fflush (FRAME_TTY (f)->output);
3166 /* Check window matrices for lost pointers. */
3167 #ifdef GLYPH_DEBUG
3168 check_window_matrix_pointers (root_window);
3169 add_frame_display_history (f, paused_p);
3170 #endif
3173 do_pause:
3174 /* Reset flags indicating that a window should be updated. */
3175 set_window_update_flags (root_window, 0);
3177 display_completed = !paused_p;
3178 return paused_p;
3183 /************************************************************************
3184 Window-based updates
3185 ************************************************************************/
3187 /* Perform updates in window tree rooted at W.
3188 If FORCE_P, don't stop updating if input is pending. */
3190 static bool
3191 update_window_tree (struct window *w, bool force_p)
3193 bool paused_p = 0;
3195 while (w && !paused_p)
3197 if (!NILP (w->hchild))
3198 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3199 else if (!NILP (w->vchild))
3200 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3201 else if (w->must_be_updated_p)
3202 paused_p |= update_window (w, force_p);
3204 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3207 return paused_p;
3211 /* Update window W if its flag must_be_updated_p is set.
3212 If FORCE_P, don't stop updating if input is pending. */
3214 void
3215 update_single_window (struct window *w, bool force_p)
3217 if (w->must_be_updated_p)
3219 struct frame *f = XFRAME (WINDOW_FRAME (w));
3221 /* Record that this is not a frame-based redisplay. */
3222 set_frame_matrix_frame (NULL);
3224 if (redisplay_dont_pause)
3225 force_p = 1;
3227 /* Update W. */
3228 update_begin (f);
3229 update_window (w, force_p);
3230 update_end (f);
3232 /* Reset flag in W. */
3233 w->must_be_updated_p = 0;
3237 #ifdef HAVE_WINDOW_SYSTEM
3239 /* Redraw lines from the current matrix of window W that are
3240 overlapped by other rows. YB is bottom-most y-position in W. */
3242 static void
3243 redraw_overlapped_rows (struct window *w, int yb)
3245 int i;
3246 struct frame *f = XFRAME (WINDOW_FRAME (w));
3248 /* If rows overlapping others have been changed, the rows being
3249 overlapped have to be redrawn. This won't draw lines that have
3250 already been drawn in update_window_line because overlapped_p in
3251 desired rows is 0, so after row assignment overlapped_p in
3252 current rows is 0. */
3253 for (i = 0; i < w->current_matrix->nrows; ++i)
3255 struct glyph_row *row = w->current_matrix->rows + i;
3257 if (!row->enabled_p)
3258 break;
3259 else if (row->mode_line_p)
3260 continue;
3262 if (row->overlapped_p)
3264 enum glyph_row_area area;
3266 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3268 updated_row = row;
3269 updated_area = area;
3270 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3271 area == TEXT_AREA ? row->x : 0);
3272 if (row->used[area])
3273 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3274 row->used[area]);
3275 FRAME_RIF (f)->clear_end_of_line (-1);
3278 row->overlapped_p = 0;
3281 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3282 break;
3287 /* Redraw lines from the current matrix of window W that overlap
3288 others. YB is bottom-most y-position in W. */
3290 static void
3291 redraw_overlapping_rows (struct window *w, int yb)
3293 int i, bottom_y;
3294 struct glyph_row *row;
3295 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3297 for (i = 0; i < w->current_matrix->nrows; ++i)
3299 row = w->current_matrix->rows + i;
3301 if (!row->enabled_p)
3302 break;
3303 else if (row->mode_line_p)
3304 continue;
3306 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3308 if (row->overlapping_p)
3310 int overlaps = 0;
3312 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3313 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3314 overlaps |= OVERLAPS_PRED;
3315 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3316 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3317 overlaps |= OVERLAPS_SUCC;
3319 if (overlaps)
3321 if (row->used[LEFT_MARGIN_AREA])
3322 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3324 if (row->used[TEXT_AREA])
3325 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3327 if (row->used[RIGHT_MARGIN_AREA])
3328 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3330 /* Record in neighbor rows that ROW overwrites part of
3331 their display. */
3332 if (overlaps & OVERLAPS_PRED)
3333 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3334 if (overlaps & OVERLAPS_SUCC)
3335 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3339 if (bottom_y >= yb)
3340 break;
3344 #endif /* HAVE_WINDOW_SYSTEM */
3347 #if defined GLYPH_DEBUG && 0
3349 /* Check that no row in the current matrix of window W is enabled
3350 which is below what's displayed in the window. */
3352 static void
3353 check_current_matrix_flags (struct window *w)
3355 bool last_seen_p = 0;
3356 int i, yb = window_text_bottom_y (w);
3358 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3360 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3361 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3362 last_seen_p = 1;
3363 else if (last_seen_p && row->enabled_p)
3364 emacs_abort ();
3368 #endif /* GLYPH_DEBUG */
3371 /* Update display of window W.
3372 If FORCE_P, don't stop updating when input is pending. */
3374 static bool
3375 update_window (struct window *w, bool force_p)
3377 struct glyph_matrix *desired_matrix = w->desired_matrix;
3378 bool paused_p;
3379 int preempt_count = baud_rate / 2400 + 1;
3380 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3381 #ifdef GLYPH_DEBUG
3382 /* Check that W's frame doesn't have glyph matrices. */
3383 eassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3384 #endif
3386 /* Check pending input the first time so that we can quickly return. */
3387 if (!force_p)
3388 detect_input_pending_ignore_squeezables ();
3390 /* If forced to complete the update, or if no input is pending, do
3391 the update. */
3392 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3394 struct glyph_row *row, *end;
3395 struct glyph_row *mode_line_row;
3396 struct glyph_row *header_line_row;
3397 int yb;
3398 bool changed_p = 0, mouse_face_overwritten_p = 0;
3399 int n_updated = 0;
3401 rif->update_window_begin_hook (w);
3402 yb = window_text_bottom_y (w);
3403 row = MATRIX_ROW (desired_matrix, 0);
3404 end = MATRIX_MODE_LINE_ROW (desired_matrix);
3406 /* Take note of the header line, if there is one. We will
3407 update it below, after updating all of the window's lines. */
3408 if (row->mode_line_p)
3410 header_line_row = row;
3411 ++row;
3413 else
3414 header_line_row = NULL;
3416 /* Update the mode line, if necessary. */
3417 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3418 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3420 mode_line_row->y = yb;
3421 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3422 desired_matrix),
3423 &mouse_face_overwritten_p);
3426 /* Find first enabled row. Optimizations in redisplay_internal
3427 may lead to an update with only one row enabled. There may
3428 be also completely empty matrices. */
3429 while (row < end && !row->enabled_p)
3430 ++row;
3432 /* Try reusing part of the display by copying. */
3433 if (row < end && !desired_matrix->no_scrolling_p)
3435 int rc = scrolling_window (w, header_line_row != NULL);
3436 if (rc < 0)
3438 /* All rows were found to be equal. */
3439 paused_p = 0;
3440 goto set_cursor;
3442 else if (rc > 0)
3444 /* We've scrolled the display. */
3445 force_p = 1;
3446 changed_p = 1;
3450 /* Update the rest of the lines. */
3451 for (; row < end && (force_p || !input_pending); ++row)
3452 /* scrolling_window resets the enabled_p flag of the rows it
3453 reuses from current_matrix. */
3454 if (row->enabled_p)
3456 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3457 int i;
3459 /* We'll have to play a little bit with when to
3460 detect_input_pending. If it's done too often,
3461 scrolling large windows with repeated scroll-up
3462 commands will too quickly pause redisplay. */
3463 if (!force_p && ++n_updated % preempt_count == 0)
3464 detect_input_pending_ignore_squeezables ();
3465 changed_p |= update_window_line (w, vpos,
3466 &mouse_face_overwritten_p);
3468 /* Mark all rows below the last visible one in the current
3469 matrix as invalid. This is necessary because of
3470 variable line heights. Consider the case of three
3471 successive redisplays, where the first displays 5
3472 lines, the second 3 lines, and the third 5 lines again.
3473 If the second redisplay wouldn't mark rows in the
3474 current matrix invalid, the third redisplay might be
3475 tempted to optimize redisplay based on lines displayed
3476 in the first redisplay. */
3477 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3478 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3479 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3482 /* Was display preempted? */
3483 paused_p = row < end;
3485 set_cursor:
3487 /* Update the header line after scrolling because a new header
3488 line would otherwise overwrite lines at the top of the window
3489 that can be scrolled. */
3490 if (header_line_row && header_line_row->enabled_p)
3492 header_line_row->y = 0;
3493 update_window_line (w, 0, &mouse_face_overwritten_p);
3496 /* Fix the appearance of overlapping/overlapped rows. */
3497 if (!paused_p && !w->pseudo_window_p)
3499 #ifdef HAVE_WINDOW_SYSTEM
3500 if (changed_p && rif->fix_overlapping_area)
3502 redraw_overlapped_rows (w, yb);
3503 redraw_overlapping_rows (w, yb);
3505 #endif
3507 /* Make cursor visible at cursor position of W. */
3508 set_window_cursor_after_update (w);
3510 #if 0 /* Check that current matrix invariants are satisfied. This is
3511 for debugging only. See the comment of check_matrix_invariants. */
3512 IF_DEBUG (check_matrix_invariants (w));
3513 #endif
3516 #ifdef GLYPH_DEBUG
3517 /* Remember the redisplay method used to display the matrix. */
3518 strcpy (w->current_matrix->method, w->desired_matrix->method);
3519 #endif
3521 #ifdef HAVE_WINDOW_SYSTEM
3522 update_window_fringes (w, 0);
3523 #endif
3525 /* End the update of window W. Don't set the cursor if we
3526 paused updating the display because in this case,
3527 set_window_cursor_after_update hasn't been called, and
3528 output_cursor doesn't contain the cursor location. */
3529 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3531 else
3532 paused_p = 1;
3534 #ifdef GLYPH_DEBUG
3535 /* check_current_matrix_flags (w); */
3536 add_window_display_history (w, w->current_matrix->method, paused_p);
3537 #endif
3539 clear_glyph_matrix (desired_matrix);
3541 return paused_p;
3545 /* Update the display of area AREA in window W, row number VPOS.
3546 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3548 static void
3549 update_marginal_area (struct window *w, int area, int vpos)
3551 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3552 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3554 /* Let functions in xterm.c know what area subsequent X positions
3555 will be relative to. */
3556 updated_area = area;
3558 /* Set cursor to start of glyphs, write them, and clear to the end
3559 of the area. I don't think that something more sophisticated is
3560 necessary here, since marginal areas will not be the default. */
3561 rif->cursor_to (vpos, 0, desired_row->y, 0);
3562 if (desired_row->used[area])
3563 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3564 rif->clear_end_of_line (-1);
3568 /* Update the display of the text area of row VPOS in window W.
3569 Value is true if display has changed. */
3571 static bool
3572 update_text_area (struct window *w, int vpos)
3574 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3575 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3576 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3577 bool changed_p = 0;
3579 /* Let functions in xterm.c know what area subsequent X positions
3580 will be relative to. */
3581 updated_area = TEXT_AREA;
3583 /* If rows are at different X or Y, or rows have different height,
3584 or the current row is marked invalid, write the entire line. */
3585 if (!current_row->enabled_p
3586 || desired_row->y != current_row->y
3587 || desired_row->ascent != current_row->ascent
3588 || desired_row->phys_ascent != current_row->phys_ascent
3589 || desired_row->phys_height != current_row->phys_height
3590 || desired_row->visible_height != current_row->visible_height
3591 || current_row->overlapped_p
3592 /* This next line is necessary for correctly redrawing
3593 mouse-face areas after scrolling and other operations.
3594 However, it causes excessive flickering when mouse is moved
3595 across the mode line. Luckily, turning it off for the mode
3596 line doesn't seem to hurt anything. -- cyd.
3597 But it is still needed for the header line. -- kfs. */
3598 || (current_row->mouse_face_p
3599 && !(current_row->mode_line_p && vpos > 0))
3600 || current_row->x != desired_row->x)
3602 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
3604 if (desired_row->used[TEXT_AREA])
3605 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3606 desired_row->used[TEXT_AREA]);
3608 /* Clear to end of window. */
3609 rif->clear_end_of_line (-1);
3610 changed_p = 1;
3612 /* This erases the cursor. We do this here because
3613 notice_overwritten_cursor cannot easily check this, which
3614 might indicate that the whole functionality of
3615 notice_overwritten_cursor would better be implemented here.
3616 On the other hand, we need notice_overwritten_cursor as long
3617 as mouse highlighting is done asynchronously outside of
3618 redisplay. */
3619 if (vpos == w->phys_cursor.vpos)
3620 w->phys_cursor_on_p = 0;
3622 else
3624 int stop, i, x;
3625 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3626 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3627 bool overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3628 int desired_stop_pos = desired_row->used[TEXT_AREA];
3629 bool abort_skipping = 0;
3631 /* If the desired row extends its face to the text area end, and
3632 unless the current row also does so at the same position,
3633 make sure we write at least one glyph, so that the face
3634 extension actually takes place. */
3635 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3636 && (desired_stop_pos < current_row->used[TEXT_AREA]
3637 || (desired_stop_pos == current_row->used[TEXT_AREA]
3638 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3639 --desired_stop_pos;
3641 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3642 i = 0;
3643 x = desired_row->x;
3645 /* Loop over glyphs that current and desired row may have
3646 in common. */
3647 while (i < stop)
3649 bool can_skip_p = !abort_skipping;
3651 /* Skip over glyphs that both rows have in common. These
3652 don't have to be written. We can't skip if the last
3653 current glyph overlaps the glyph to its right. For
3654 example, consider a current row of `if ' with the `f' in
3655 Courier bold so that it overlaps the ` ' to its right.
3656 If the desired row is ` ', we would skip over the space
3657 after the `if' and there would remain a pixel from the
3658 `f' on the screen. */
3659 if (overlapping_glyphs_p && i > 0)
3661 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3662 int left, right;
3664 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3665 &left, &right);
3666 can_skip_p = (right == 0 && !abort_skipping);
3669 if (can_skip_p)
3671 int start_hpos = i;
3673 while (i < stop
3674 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3676 x += desired_glyph->pixel_width;
3677 ++desired_glyph, ++current_glyph, ++i;
3680 /* Consider the case that the current row contains "xxx
3681 ppp ggg" in italic Courier font, and the desired row
3682 is "xxx ggg". The character `p' has lbearing, `g'
3683 has not. The loop above will stop in front of the
3684 first `p' in the current row. If we would start
3685 writing glyphs there, we wouldn't erase the lbearing
3686 of the `p'. The rest of the lbearing problem is then
3687 taken care of by draw_glyphs. */
3688 if (overlapping_glyphs_p
3689 && i > 0
3690 && i < current_row->used[TEXT_AREA]
3691 && (current_row->used[TEXT_AREA]
3692 != desired_row->used[TEXT_AREA]))
3694 int left, right;
3696 rif->get_glyph_overhangs (current_glyph,
3697 XFRAME (w->frame),
3698 &left, &right);
3699 while (left > 0 && i > 0)
3701 --i, --desired_glyph, --current_glyph;
3702 x -= desired_glyph->pixel_width;
3703 left -= desired_glyph->pixel_width;
3706 /* Abort the skipping algorithm if we end up before
3707 our starting point, to avoid looping (bug#1070).
3708 This can happen when the lbearing is larger than
3709 the pixel width. */
3710 abort_skipping = (i < start_hpos);
3714 /* Try to avoid writing the entire rest of the desired row
3715 by looking for a resync point. This mainly prevents
3716 mode line flickering in the case the mode line is in
3717 fixed-pitch font, which it usually will be. */
3718 if (i < desired_row->used[TEXT_AREA])
3720 int start_x = x, start_hpos = i;
3721 struct glyph *start = desired_glyph;
3722 int current_x = x;
3723 bool skip_first_p = !can_skip_p;
3725 /* Find the next glyph that's equal again. */
3726 while (i < stop
3727 && (skip_first_p
3728 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3729 && x == current_x)
3731 x += desired_glyph->pixel_width;
3732 current_x += current_glyph->pixel_width;
3733 ++desired_glyph, ++current_glyph, ++i;
3734 skip_first_p = 0;
3737 if (i == start_hpos || x != current_x)
3739 i = start_hpos;
3740 x = start_x;
3741 desired_glyph = start;
3742 break;
3745 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
3746 rif->write_glyphs (start, i - start_hpos);
3747 changed_p = 1;
3751 /* Write the rest. */
3752 if (i < desired_row->used[TEXT_AREA])
3754 rif->cursor_to (vpos, i, desired_row->y, x);
3755 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
3756 changed_p = 1;
3759 /* Maybe clear to end of line. */
3760 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3762 /* If new row extends to the end of the text area, nothing
3763 has to be cleared, if and only if we did a write_glyphs
3764 above. This is made sure by setting desired_stop_pos
3765 appropriately above. */
3766 eassert (i < desired_row->used[TEXT_AREA]
3767 || ((desired_row->used[TEXT_AREA]
3768 == current_row->used[TEXT_AREA])
3769 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3771 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3773 /* If old row extends to the end of the text area, clear. */
3774 if (i >= desired_row->used[TEXT_AREA])
3775 rif->cursor_to (vpos, i, desired_row->y,
3776 desired_row->pixel_width);
3777 rif->clear_end_of_line (-1);
3778 changed_p = 1;
3780 else if (desired_row->pixel_width < current_row->pixel_width)
3782 /* Otherwise clear to the end of the old row. Everything
3783 after that position should be clear already. */
3784 int xlim;
3786 if (i >= desired_row->used[TEXT_AREA])
3787 rif->cursor_to (vpos, i, desired_row->y,
3788 desired_row->pixel_width);
3790 /* If cursor is displayed at the end of the line, make sure
3791 it's cleared. Nowadays we don't have a phys_cursor_glyph
3792 with which to erase the cursor (because this method
3793 doesn't work with lbearing/rbearing), so we must do it
3794 this way. */
3795 if (vpos == w->phys_cursor.vpos
3796 && (desired_row->reversed_p
3797 ? (w->phys_cursor.hpos < 0)
3798 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3800 w->phys_cursor_on_p = 0;
3801 xlim = -1;
3803 else
3804 xlim = current_row->pixel_width;
3805 rif->clear_end_of_line (xlim);
3806 changed_p = 1;
3810 return changed_p;
3814 /* Update row VPOS in window W. Value is true if display has been changed. */
3816 static bool
3817 update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
3819 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3820 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3821 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3822 bool changed_p = 0;
3824 /* Set the row being updated. This is important to let xterm.c
3825 know what line height values are in effect. */
3826 updated_row = desired_row;
3828 /* A row can be completely invisible in case a desired matrix was
3829 built with a vscroll and then make_cursor_line_fully_visible shifts
3830 the matrix. Make sure to make such rows current anyway, since
3831 we need the correct y-position, for example, in the current matrix. */
3832 if (desired_row->mode_line_p
3833 || desired_row->visible_height > 0)
3835 eassert (desired_row->enabled_p);
3837 /* Update display of the left margin area, if there is one. */
3838 if (!desired_row->full_width_p
3839 && !NILP (w->left_margin_cols))
3841 changed_p = 1;
3842 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
3843 /* Setting this flag will ensure the vertical border, if
3844 any, between this window and the one on its left will be
3845 redrawn. This is necessary because updating the left
3846 margin area can potentially draw over the border. */
3847 current_row->redraw_fringe_bitmaps_p = 1;
3850 /* Update the display of the text area. */
3851 if (update_text_area (w, vpos))
3853 changed_p = 1;
3854 if (current_row->mouse_face_p)
3855 *mouse_face_overwritten_p = 1;
3858 /* Update display of the right margin area, if there is one. */
3859 if (!desired_row->full_width_p
3860 && !NILP (w->right_margin_cols))
3862 changed_p = 1;
3863 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
3866 /* Draw truncation marks etc. */
3867 if (!current_row->enabled_p
3868 || desired_row->y != current_row->y
3869 || desired_row->visible_height != current_row->visible_height
3870 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
3871 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
3872 || current_row->redraw_fringe_bitmaps_p
3873 || desired_row->mode_line_p != current_row->mode_line_p
3874 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
3875 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
3876 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
3877 rif->after_update_window_line_hook (desired_row);
3880 /* Update current_row from desired_row. */
3881 make_current (w->desired_matrix, w->current_matrix, vpos);
3882 updated_row = NULL;
3883 return changed_p;
3887 /* Set the cursor after an update of window W. This function may only
3888 be called from update_window. */
3890 static void
3891 set_window_cursor_after_update (struct window *w)
3893 struct frame *f = XFRAME (w->frame);
3894 struct redisplay_interface *rif = FRAME_RIF (f);
3895 int cx, cy, vpos, hpos;
3897 /* Not intended for frame matrix updates. */
3898 eassert (FRAME_WINDOW_P (f));
3900 if (cursor_in_echo_area
3901 && !NILP (echo_area_buffer[0])
3902 /* If we are showing a message instead of the mini-buffer,
3903 show the cursor for the message instead. */
3904 && XWINDOW (minibuf_window) == w
3905 && EQ (minibuf_window, echo_area_window)
3906 /* These cases apply only to the frame that contains
3907 the active mini-buffer window. */
3908 && FRAME_HAS_MINIBUF_P (f)
3909 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
3911 cx = cy = vpos = hpos = 0;
3913 if (cursor_in_echo_area >= 0)
3915 /* If the mini-buffer is several lines high, find the last
3916 line that has any text on it. Note: either all lines
3917 are enabled or none. Otherwise we wouldn't be able to
3918 determine Y. */
3919 struct glyph_row *row, *last_row;
3920 struct glyph *glyph;
3921 int yb = window_text_bottom_y (w);
3923 last_row = NULL;
3924 row = w->current_matrix->rows;
3925 while (row->enabled_p
3926 && (last_row == NULL
3927 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
3929 if (row->used[TEXT_AREA]
3930 && row->glyphs[TEXT_AREA][0].charpos >= 0)
3931 last_row = row;
3932 ++row;
3935 if (last_row)
3937 struct glyph *start = last_row->glyphs[TEXT_AREA];
3938 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
3940 while (last > start && last->charpos < 0)
3941 --last;
3943 for (glyph = start; glyph < last; ++glyph)
3945 cx += glyph->pixel_width;
3946 ++hpos;
3949 cy = last_row->y;
3950 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
3954 else
3956 cx = w->cursor.x;
3957 cy = w->cursor.y;
3958 hpos = w->cursor.hpos;
3959 vpos = w->cursor.vpos;
3962 /* Window cursor can be out of sync for horizontally split windows.
3963 Horizontal position is -1 when cursor is on the left fringe. */
3964 hpos = clip_to_bounds (-1, hpos, w->current_matrix->matrix_w - 1);
3965 vpos = clip_to_bounds (0, vpos, w->current_matrix->nrows - 1);
3966 rif->cursor_to (vpos, hpos, cy, cx);
3970 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
3971 tree rooted at W. */
3973 void
3974 set_window_update_flags (struct window *w, bool on_p)
3976 while (w)
3978 if (!NILP (w->hchild))
3979 set_window_update_flags (XWINDOW (w->hchild), on_p);
3980 else if (!NILP (w->vchild))
3981 set_window_update_flags (XWINDOW (w->vchild), on_p);
3982 else
3983 w->must_be_updated_p = on_p;
3985 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3991 /***********************************************************************
3992 Window-Based Scrolling
3993 ***********************************************************************/
3995 /* Structure describing rows in scrolling_window. */
3997 struct row_entry
3999 /* Number of occurrences of this row in desired and current matrix. */
4000 int old_uses, new_uses;
4002 /* Vpos of row in new matrix. */
4003 int new_line_number;
4005 /* Bucket index of this row_entry in the hash table row_table. */
4006 ptrdiff_t bucket;
4008 /* The row described by this entry. */
4009 struct glyph_row *row;
4011 /* Hash collision chain. */
4012 struct row_entry *next;
4015 /* A pool to allocate row_entry structures from, and the size of the
4016 pool. The pool is reallocated in scrolling_window when we find
4017 that we need a larger one. */
4019 static struct row_entry *row_entry_pool;
4020 static ptrdiff_t row_entry_pool_size;
4022 /* Index of next free entry in row_entry_pool. */
4024 static ptrdiff_t row_entry_idx;
4026 /* The hash table used during scrolling, and the table's size. This
4027 table is used to quickly identify equal rows in the desired and
4028 current matrix. */
4030 static struct row_entry **row_table;
4031 static ptrdiff_t row_table_size;
4033 /* Vectors of pointers to row_entry structures belonging to the
4034 current and desired matrix, and the size of the vectors. */
4036 static struct row_entry **old_lines, **new_lines;
4037 static ptrdiff_t old_lines_size, new_lines_size;
4039 /* A pool to allocate run structures from, and its size. */
4041 static struct run *run_pool;
4042 static ptrdiff_t runs_size;
4044 /* A vector of runs of lines found during scrolling. */
4046 static struct run **runs;
4048 /* Add glyph row ROW to the scrolling hash table. */
4050 static struct row_entry *
4051 add_row_entry (struct glyph_row *row)
4053 struct row_entry *entry;
4054 ptrdiff_t i = row->hash % row_table_size;
4056 entry = row_table[i];
4057 eassert (entry || verify_row_hash (row));
4058 while (entry && !row_equal_p (entry->row, row, 1))
4059 entry = entry->next;
4061 if (entry == NULL)
4063 entry = row_entry_pool + row_entry_idx++;
4064 entry->row = row;
4065 entry->old_uses = entry->new_uses = 0;
4066 entry->new_line_number = 0;
4067 entry->bucket = i;
4068 entry->next = row_table[i];
4069 row_table[i] = entry;
4072 return entry;
4076 /* Try to reuse part of the current display of W by scrolling lines.
4077 HEADER_LINE_P means W has a header line.
4079 The algorithm is taken from Communications of the ACM, Apr78 "A
4080 Technique for Isolating Differences Between Files." It should take
4081 O(N) time.
4083 A short outline of the steps of the algorithm
4085 1. Skip lines equal at the start and end of both matrices.
4087 2. Enter rows in the current and desired matrix into a symbol
4088 table, counting how often they appear in both matrices.
4090 3. Rows that appear exactly once in both matrices serve as anchors,
4091 i.e. we assume that such lines are likely to have been moved.
4093 4. Starting from anchor lines, extend regions to be scrolled both
4094 forward and backward.
4096 Value is
4098 -1 if all rows were found to be equal.
4099 0 to indicate that we did not scroll the display, or
4100 1 if we did scroll. */
4102 static int
4103 scrolling_window (struct window *w, bool header_line_p)
4105 struct glyph_matrix *desired_matrix = w->desired_matrix;
4106 struct glyph_matrix *current_matrix = w->current_matrix;
4107 int yb = window_text_bottom_y (w);
4108 ptrdiff_t i;
4109 int j, first_old, first_new, last_old, last_new;
4110 int nruns, run_idx;
4111 ptrdiff_t n;
4112 struct row_entry *entry;
4113 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4115 /* Skip over rows equal at the start. */
4116 for (i = header_line_p; i < current_matrix->nrows - 1; ++i)
4118 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4119 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4121 if (c->enabled_p
4122 && d->enabled_p
4123 && !d->redraw_fringe_bitmaps_p
4124 && c->y == d->y
4125 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4126 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4127 && row_equal_p (c, d, 1))
4129 assign_row (c, d);
4130 d->enabled_p = 0;
4132 else
4133 break;
4136 /* Give up if some rows in the desired matrix are not enabled. */
4137 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4138 return -1;
4140 first_old = first_new = i;
4142 /* Set last_new to the index + 1 of the row that reaches the
4143 bottom boundary in the desired matrix. Give up if we find a
4144 disabled row before we reach the bottom boundary. */
4145 i = first_new + 1;
4146 while (i < desired_matrix->nrows - 1)
4148 int bottom;
4150 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4151 return 0;
4152 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4153 if (bottom <= yb)
4154 ++i;
4155 if (bottom >= yb)
4156 break;
4159 last_new = i;
4161 /* Set last_old to the index + 1 of the row that reaches the bottom
4162 boundary in the current matrix. We don't look at the enabled
4163 flag here because we plan to reuse part of the display even if
4164 other parts are disabled. */
4165 i = first_old + 1;
4166 while (i < current_matrix->nrows - 1)
4168 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4169 if (bottom <= yb)
4170 ++i;
4171 if (bottom >= yb)
4172 break;
4175 last_old = i;
4177 /* Skip over rows equal at the bottom. */
4178 i = last_new;
4179 j = last_old;
4180 while (i - 1 > first_new
4181 && j - 1 > first_old
4182 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4183 && (MATRIX_ROW (current_matrix, j - 1)->y
4184 == MATRIX_ROW (desired_matrix, i - 1)->y)
4185 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4186 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4187 MATRIX_ROW (current_matrix, j - 1), 1))
4188 --i, --j;
4189 last_new = i;
4190 last_old = j;
4192 /* Nothing to do if all rows are equal. */
4193 if (last_new == first_new)
4194 return 0;
4196 /* Check for integer overflow in size calculation.
4198 If next_almost_prime checks (N) for divisibility by 2..10, then
4199 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4200 So, set next_almost_prime_increment_max to 10.
4202 It's just a coincidence that next_almost_prime_increment_max ==
4203 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4204 13, then next_almost_prime_increment_max would be 14, e.g.,
4205 because next_almost_prime (113) would be 127. */
4207 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4208 enum { next_almost_prime_increment_max = 10 };
4209 ptrdiff_t row_table_max =
4210 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4211 - next_almost_prime_increment_max);
4212 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4213 if (current_nrows_max < current_matrix->nrows)
4214 memory_full (SIZE_MAX);
4217 /* Reallocate vectors, tables etc. if necessary. */
4219 if (current_matrix->nrows > old_lines_size)
4220 old_lines = xpalloc (old_lines, &old_lines_size,
4221 current_matrix->nrows - old_lines_size,
4222 INT_MAX, sizeof *old_lines);
4224 if (desired_matrix->nrows > new_lines_size)
4225 new_lines = xpalloc (new_lines, &new_lines_size,
4226 desired_matrix->nrows - new_lines_size,
4227 INT_MAX, sizeof *new_lines);
4229 n = desired_matrix->nrows;
4230 n += current_matrix->nrows;
4231 if (row_table_size < 3 * n)
4233 ptrdiff_t size = next_almost_prime (3 * n);
4234 row_table = xnrealloc (row_table, size, sizeof *row_table);
4235 row_table_size = size;
4236 memset (row_table, 0, size * sizeof *row_table);
4239 if (n > row_entry_pool_size)
4240 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4241 n - row_entry_pool_size,
4242 -1, sizeof *row_entry_pool);
4244 if (desired_matrix->nrows > runs_size)
4246 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4247 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4248 runs_size = desired_matrix->nrows;
4251 nruns = run_idx = 0;
4252 row_entry_idx = 0;
4254 /* Add rows from the current and desired matrix to the hash table
4255 row_hash_table to be able to find equal ones quickly. */
4257 for (i = first_old; i < last_old; ++i)
4259 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4261 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4262 old_lines[i] = entry;
4263 ++entry->old_uses;
4265 else
4266 old_lines[i] = NULL;
4269 for (i = first_new; i < last_new; ++i)
4271 eassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4272 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4273 ++entry->new_uses;
4274 entry->new_line_number = i;
4275 new_lines[i] = entry;
4278 /* Identify moves based on lines that are unique and equal
4279 in both matrices. */
4280 for (i = first_old; i < last_old;)
4281 if (old_lines[i]
4282 && old_lines[i]->old_uses == 1
4283 && old_lines[i]->new_uses == 1)
4285 int p, q;
4286 int new_line = old_lines[i]->new_line_number;
4287 struct run *run = run_pool + run_idx++;
4289 /* Record move. */
4290 run->current_vpos = i;
4291 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4292 run->desired_vpos = new_line;
4293 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4294 run->nrows = 1;
4295 run->height = MATRIX_ROW (current_matrix, i)->height;
4297 /* Extend backward. */
4298 p = i - 1;
4299 q = new_line - 1;
4300 while (p > first_old
4301 && q > first_new
4302 && old_lines[p] == new_lines[q])
4304 int h = MATRIX_ROW (current_matrix, p)->height;
4305 --run->current_vpos;
4306 --run->desired_vpos;
4307 ++run->nrows;
4308 run->height += h;
4309 run->desired_y -= h;
4310 run->current_y -= h;
4311 --p, --q;
4314 /* Extend forward. */
4315 p = i + 1;
4316 q = new_line + 1;
4317 while (p < last_old
4318 && q < last_new
4319 && old_lines[p] == new_lines[q])
4321 int h = MATRIX_ROW (current_matrix, p)->height;
4322 ++run->nrows;
4323 run->height += h;
4324 ++p, ++q;
4327 /* Insert run into list of all runs. Order runs by copied
4328 pixel lines. Note that we record runs that don't have to
4329 be copied because they are already in place. This is done
4330 because we can avoid calling update_window_line in this
4331 case. */
4332 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4334 for (q = nruns; q > p; --q)
4335 runs[q] = runs[q - 1];
4336 runs[p] = run;
4337 ++nruns;
4339 i += run->nrows;
4341 else
4342 ++i;
4344 /* Do the moves. Do it in a way that we don't overwrite something
4345 we want to copy later on. This is not solvable in general
4346 because there is only one display and we don't have a way to
4347 exchange areas on this display. Example:
4349 +-----------+ +-----------+
4350 | A | | B |
4351 +-----------+ --> +-----------+
4352 | B | | A |
4353 +-----------+ +-----------+
4355 Instead, prefer bigger moves, and invalidate moves that would
4356 copy from where we copied to. */
4358 for (i = 0; i < nruns; ++i)
4359 if (runs[i]->nrows > 0)
4361 struct run *r = runs[i];
4363 /* Copy on the display. */
4364 if (r->current_y != r->desired_y)
4366 rif->clear_window_mouse_face (w);
4367 rif->scroll_run_hook (w, r);
4370 /* Truncate runs that copy to where we copied to, and
4371 invalidate runs that copy from where we copied to. */
4372 for (j = nruns - 1; j > i; --j)
4374 struct run *p = runs[j];
4375 bool truncated_p = 0;
4377 if (p->nrows > 0
4378 && p->desired_y < r->desired_y + r->height
4379 && p->desired_y + p->height > r->desired_y)
4381 if (p->desired_y < r->desired_y)
4383 p->nrows = r->desired_vpos - p->desired_vpos;
4384 p->height = r->desired_y - p->desired_y;
4385 truncated_p = 1;
4387 else
4389 int nrows_copied = (r->desired_vpos + r->nrows
4390 - p->desired_vpos);
4392 if (p->nrows <= nrows_copied)
4393 p->nrows = 0;
4394 else
4396 int height_copied = (r->desired_y + r->height
4397 - p->desired_y);
4399 p->current_vpos += nrows_copied;
4400 p->desired_vpos += nrows_copied;
4401 p->nrows -= nrows_copied;
4402 p->current_y += height_copied;
4403 p->desired_y += height_copied;
4404 p->height -= height_copied;
4405 truncated_p = 1;
4410 if (r->current_y != r->desired_y
4411 /* The condition below is equivalent to
4412 ((p->current_y >= r->desired_y
4413 && p->current_y < r->desired_y + r->height)
4414 || (p->current_y + p->height > r->desired_y
4415 && (p->current_y + p->height
4416 <= r->desired_y + r->height)))
4417 because we have 0 < p->height <= r->height. */
4418 && p->current_y < r->desired_y + r->height
4419 && p->current_y + p->height > r->desired_y)
4420 p->nrows = 0;
4422 /* Reorder runs by copied pixel lines if truncated. */
4423 if (truncated_p && p->nrows > 0)
4425 int k = nruns - 1;
4427 while (runs[k]->nrows == 0 || runs[k]->height < p->height)
4428 k--;
4429 memmove (runs + j, runs + j + 1, (k - j) * sizeof (*runs));
4430 runs[k] = p;
4434 /* Assign matrix rows. */
4435 for (j = 0; j < r->nrows; ++j)
4437 struct glyph_row *from, *to;
4438 bool to_overlapped_p;
4440 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4441 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4442 to_overlapped_p = to->overlapped_p;
4443 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4444 assign_row (to, from);
4445 /* The above `assign_row' actually does swap, so if we had
4446 an overlap in the copy destination of two runs, then
4447 the second run would assign a previously disabled bogus
4448 row. But thanks to the truncation code in the
4449 preceding for-loop, we no longer have such an overlap,
4450 and thus the assigned row should always be enabled. */
4451 eassert (to->enabled_p);
4452 from->enabled_p = 0;
4453 to->overlapped_p = to_overlapped_p;
4457 /* Clear the hash table, for the next time. */
4458 for (i = 0; i < row_entry_idx; ++i)
4459 row_table[row_entry_pool[i].bucket] = NULL;
4461 /* Value is 1 to indicate that we scrolled the display. */
4462 return 0 < nruns;
4467 /************************************************************************
4468 Frame-Based Updates
4469 ************************************************************************/
4471 /* Update the desired frame matrix of frame F.
4473 FORCE_P means that the update should not be stopped by pending input.
4474 INHIBIT_HAIRY_ID_P means that scrolling should not be tried.
4476 Value is true if update was stopped due to pending input. */
4478 static bool
4479 update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p)
4481 /* Frame matrices to work on. */
4482 struct glyph_matrix *current_matrix = f->current_matrix;
4483 struct glyph_matrix *desired_matrix = f->desired_matrix;
4484 int i;
4485 bool pause_p;
4486 int preempt_count = baud_rate / 2400 + 1;
4488 eassert (current_matrix && desired_matrix);
4490 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4491 calculate_costs (f);
4493 if (preempt_count <= 0)
4494 preempt_count = 1;
4496 if (!force_p && detect_input_pending_ignore_squeezables ())
4498 pause_p = 1;
4499 goto do_pause;
4502 /* If we cannot insert/delete lines, it's no use trying it. */
4503 if (!FRAME_LINE_INS_DEL_OK (f))
4504 inhibit_id_p = 1;
4506 /* See if any of the desired lines are enabled; don't compute for
4507 i/d line if just want cursor motion. */
4508 for (i = 0; i < desired_matrix->nrows; i++)
4509 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4510 break;
4512 /* Try doing i/d line, if not yet inhibited. */
4513 if (!inhibit_id_p && i < desired_matrix->nrows)
4514 force_p |= scrolling (f);
4516 /* Update the individual lines as needed. Do bottom line first. */
4517 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4518 update_frame_line (f, desired_matrix->nrows - 1);
4520 /* Now update the rest of the lines. */
4521 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4523 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4525 if (FRAME_TERMCAP_P (f))
4527 /* Flush out every so many lines.
4528 Also flush out if likely to have more than 1k buffered
4529 otherwise. I'm told that some telnet connections get
4530 really screwed by more than 1k output at once. */
4531 FILE *display_output = FRAME_TTY (f)->output;
4532 if (display_output)
4534 ptrdiff_t outq = __fpending (display_output);
4535 if (outq > 900
4536 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4537 fflush (display_output);
4541 if (!force_p && (i - 1) % preempt_count == 0)
4542 detect_input_pending_ignore_squeezables ();
4544 update_frame_line (f, i);
4548 lint_assume (0 <= FRAME_LINES (f));
4549 pause_p = 0 < i && i < FRAME_LINES (f) - 1;
4551 /* Now just clean up termcap drivers and set cursor, etc. */
4552 if (!pause_p)
4554 if ((cursor_in_echo_area
4555 /* If we are showing a message instead of the mini-buffer,
4556 show the cursor for the message instead of for the
4557 (now hidden) mini-buffer contents. */
4558 || (EQ (minibuf_window, selected_window)
4559 && EQ (minibuf_window, echo_area_window)
4560 && !NILP (echo_area_buffer[0])))
4561 /* These cases apply only to the frame that contains
4562 the active mini-buffer window. */
4563 && FRAME_HAS_MINIBUF_P (f)
4564 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4566 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4567 int row, col;
4569 if (cursor_in_echo_area < 0)
4571 /* Negative value of cursor_in_echo_area means put
4572 cursor at beginning of line. */
4573 row = top;
4574 col = 0;
4576 else
4578 /* Positive value of cursor_in_echo_area means put
4579 cursor at the end of the prompt. If the mini-buffer
4580 is several lines high, find the last line that has
4581 any text on it. */
4582 row = FRAME_LINES (f);
4585 --row;
4586 col = 0;
4588 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4590 /* Frame rows are filled up with spaces that
4591 must be ignored here. */
4592 struct glyph_row *r = MATRIX_ROW (current_matrix,
4593 row);
4594 struct glyph *start = r->glyphs[TEXT_AREA];
4595 struct glyph *last = start + r->used[TEXT_AREA];
4597 while (last > start
4598 && (last - 1)->charpos < 0)
4599 --last;
4601 col = last - start;
4604 while (row > top && col == 0);
4606 /* Make sure COL is not out of range. */
4607 if (col >= FRAME_CURSOR_X_LIMIT (f))
4609 /* If we have another row, advance cursor into it. */
4610 if (row < FRAME_LINES (f) - 1)
4612 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4613 row++;
4615 /* Otherwise move it back in range. */
4616 else
4617 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4621 cursor_to (f, row, col);
4623 else
4625 /* We have only one cursor on terminal frames. Use it to
4626 display the cursor of the selected window. */
4627 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4628 if (w->cursor.vpos >= 0
4629 /* The cursor vpos may be temporarily out of bounds
4630 in the following situation: There is one window,
4631 with the cursor in the lower half of it. The window
4632 is split, and a message causes a redisplay before
4633 a new cursor position has been computed. */
4634 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4636 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4637 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4639 if (INTEGERP (w->left_margin_cols))
4640 x += XFASTINT (w->left_margin_cols);
4642 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4643 cursor_to (f, y, x);
4648 do_pause:
4650 clear_desired_matrices (f);
4651 return pause_p;
4655 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4657 static bool
4658 scrolling (struct frame *frame)
4660 int unchanged_at_top, unchanged_at_bottom;
4661 int window_size;
4662 int changed_lines;
4663 int *old_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4664 int *new_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4665 int *draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4666 int *old_draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4667 register int i;
4668 int free_at_end_vpos = FRAME_LINES (frame);
4669 struct glyph_matrix *current_matrix = frame->current_matrix;
4670 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4672 if (!current_matrix)
4673 emacs_abort ();
4675 /* Compute hash codes of all the lines. Also calculate number of
4676 changed lines, number of unchanged lines at the beginning, and
4677 number of unchanged lines at the end. */
4678 changed_lines = 0;
4679 unchanged_at_top = 0;
4680 unchanged_at_bottom = FRAME_LINES (frame);
4681 for (i = 0; i < FRAME_LINES (frame); i++)
4683 /* Give up on this scrolling if some old lines are not enabled. */
4684 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4685 return 0;
4686 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4687 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4689 /* This line cannot be redrawn, so don't let scrolling mess it. */
4690 new_hash[i] = old_hash[i];
4691 #define INFINITY 1000000 /* Taken from scroll.c */
4692 draw_cost[i] = INFINITY;
4694 else
4696 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4697 draw_cost[i] = line_draw_cost (desired_matrix, i);
4700 if (old_hash[i] != new_hash[i])
4702 changed_lines++;
4703 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4705 else if (i == unchanged_at_top)
4706 unchanged_at_top++;
4707 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4710 /* If changed lines are few, don't allow preemption, don't scroll. */
4711 if ((!FRAME_SCROLL_REGION_OK (frame)
4712 && changed_lines < baud_rate / 2400)
4713 || unchanged_at_bottom == FRAME_LINES (frame))
4714 return 1;
4716 window_size = (FRAME_LINES (frame) - unchanged_at_top
4717 - unchanged_at_bottom);
4719 if (FRAME_SCROLL_REGION_OK (frame))
4720 free_at_end_vpos -= unchanged_at_bottom;
4721 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4722 free_at_end_vpos = -1;
4724 /* If large window, fast terminal and few lines in common between
4725 current frame and desired frame, don't bother with i/d calc. */
4726 if (!FRAME_SCROLL_REGION_OK (frame)
4727 && window_size >= 18 && baud_rate > 2400
4728 && (window_size >=
4729 10 * scrolling_max_lines_saved (unchanged_at_top,
4730 FRAME_LINES (frame) - unchanged_at_bottom,
4731 old_hash, new_hash, draw_cost)))
4732 return 0;
4734 if (window_size < 2)
4735 return 0;
4737 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4738 draw_cost + unchanged_at_top - 1,
4739 old_draw_cost + unchanged_at_top - 1,
4740 old_hash + unchanged_at_top - 1,
4741 new_hash + unchanged_at_top - 1,
4742 free_at_end_vpos - unchanged_at_top);
4744 return 0;
4748 /* Count the number of blanks at the start of the vector of glyphs R
4749 which is LEN glyphs long. */
4751 static int
4752 count_blanks (struct glyph *r, int len)
4754 int i;
4756 for (i = 0; i < len; ++i)
4757 if (!CHAR_GLYPH_SPACE_P (r[i]))
4758 break;
4760 return i;
4764 /* Count the number of glyphs in common at the start of the glyph
4765 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4766 of STR2. Value is the number of equal glyphs equal at the start. */
4768 static int
4769 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4771 struct glyph *p1 = str1;
4772 struct glyph *p2 = str2;
4774 while (p1 < end1
4775 && p2 < end2
4776 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4777 ++p1, ++p2;
4779 return p1 - str1;
4783 /* Char insertion/deletion cost vector, from term.c */
4785 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4788 /* Perform a frame-based update on line VPOS in frame FRAME. */
4790 static void
4791 update_frame_line (struct frame *f, int vpos)
4793 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4794 int tem;
4795 int osp, nsp, begmatch, endmatch, olen, nlen;
4796 struct glyph_matrix *current_matrix = f->current_matrix;
4797 struct glyph_matrix *desired_matrix = f->desired_matrix;
4798 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4799 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4800 bool must_write_whole_line_p;
4801 bool write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4802 bool colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4803 != FACE_TTY_DEFAULT_BG_COLOR);
4805 if (colored_spaces_p)
4806 write_spaces_p = 1;
4808 /* Current row not enabled means it has unknown contents. We must
4809 write the whole desired line in that case. */
4810 must_write_whole_line_p = !current_row->enabled_p;
4811 if (must_write_whole_line_p)
4813 obody = 0;
4814 olen = 0;
4816 else
4818 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4819 olen = current_row->used[TEXT_AREA];
4821 /* Ignore trailing spaces, if we can. */
4822 if (!write_spaces_p)
4823 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4824 olen--;
4827 current_row->enabled_p = 1;
4828 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4830 /* If desired line is empty, just clear the line. */
4831 if (!desired_row->enabled_p)
4833 nlen = 0;
4834 goto just_erase;
4837 nbody = desired_row->glyphs[TEXT_AREA];
4838 nlen = desired_row->used[TEXT_AREA];
4839 nend = nbody + nlen;
4841 /* If display line has unknown contents, write the whole line. */
4842 if (must_write_whole_line_p)
4844 /* Ignore spaces at the end, if we can. */
4845 if (!write_spaces_p)
4846 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4847 --nlen;
4849 /* Write the contents of the desired line. */
4850 if (nlen)
4852 cursor_to (f, vpos, 0);
4853 write_glyphs (f, nbody, nlen);
4856 /* Don't call clear_end_of_line if we already wrote the whole
4857 line. The cursor will not be at the right margin in that
4858 case but in the line below. */
4859 if (nlen < FRAME_TOTAL_COLS (f))
4861 cursor_to (f, vpos, nlen);
4862 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
4864 else
4865 /* Make sure we are in the right row, otherwise cursor movement
4866 with cmgoto might use `ch' in the wrong row. */
4867 cursor_to (f, vpos, 0);
4869 make_current (desired_matrix, current_matrix, vpos);
4870 return;
4873 /* Pretend trailing spaces are not there at all,
4874 unless for one reason or another we must write all spaces. */
4875 if (!write_spaces_p)
4876 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4877 nlen--;
4879 /* If there's no i/d char, quickly do the best we can without it. */
4880 if (!FRAME_CHAR_INS_DEL_OK (f))
4882 int i, j;
4884 /* Find the first glyph in desired row that doesn't agree with
4885 a glyph in the current row, and write the rest from there on. */
4886 for (i = 0; i < nlen; i++)
4888 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
4890 /* Find the end of the run of different glyphs. */
4891 j = i + 1;
4892 while (j < nlen
4893 && (j >= olen
4894 || !GLYPH_EQUAL_P (nbody + j, obody + j)
4895 || CHAR_GLYPH_PADDING_P (nbody[j])))
4896 ++j;
4898 /* Output this run of non-matching chars. */
4899 cursor_to (f, vpos, i);
4900 write_glyphs (f, nbody + i, j - i);
4901 i = j - 1;
4903 /* Now find the next non-match. */
4907 /* Clear the rest of the line, or the non-clear part of it. */
4908 if (olen > nlen)
4910 cursor_to (f, vpos, nlen);
4911 clear_end_of_line (f, olen);
4914 /* Make current row = desired row. */
4915 make_current (desired_matrix, current_matrix, vpos);
4916 return;
4919 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
4920 characters in a row. */
4922 if (!olen)
4924 /* If current line is blank, skip over initial spaces, if
4925 possible, and write the rest. */
4926 if (write_spaces_p)
4927 nsp = 0;
4928 else
4929 nsp = count_blanks (nbody, nlen);
4931 if (nlen > nsp)
4933 cursor_to (f, vpos, nsp);
4934 write_glyphs (f, nbody + nsp, nlen - nsp);
4937 /* Exchange contents between current_frame and new_frame. */
4938 make_current (desired_matrix, current_matrix, vpos);
4939 return;
4942 /* Compute number of leading blanks in old and new contents. */
4943 osp = count_blanks (obody, olen);
4944 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
4946 /* Compute number of matching chars starting with first non-blank. */
4947 begmatch = count_match (obody + osp, obody + olen,
4948 nbody + nsp, nbody + nlen);
4950 /* Spaces in new match implicit space past the end of old. */
4951 /* A bug causing this to be a no-op was fixed in 18.29. */
4952 if (!write_spaces_p && osp + begmatch == olen)
4954 np1 = nbody + nsp;
4955 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
4956 ++begmatch;
4959 /* Avoid doing insert/delete char
4960 just cause number of leading spaces differs
4961 when the following text does not match. */
4962 if (begmatch == 0 && osp != nsp)
4963 osp = nsp = min (osp, nsp);
4965 /* Find matching characters at end of line */
4966 op1 = obody + olen;
4967 np1 = nbody + nlen;
4968 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
4969 while (op1 > op2
4970 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
4972 op1--;
4973 np1--;
4975 endmatch = obody + olen - op1;
4977 /* tem gets the distance to insert or delete.
4978 endmatch is how many characters we save by doing so.
4979 Is it worth it? */
4981 tem = (nlen - nsp) - (olen - osp);
4982 if (endmatch && tem
4983 && (!FRAME_CHAR_INS_DEL_OK (f)
4984 || endmatch <= char_ins_del_cost (f)[tem]))
4985 endmatch = 0;
4987 /* nsp - osp is the distance to insert or delete.
4988 If that is nonzero, begmatch is known to be nonzero also.
4989 begmatch + endmatch is how much we save by doing the ins/del.
4990 Is it worth it? */
4992 if (nsp != osp
4993 && (!FRAME_CHAR_INS_DEL_OK (f)
4994 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
4996 begmatch = 0;
4997 endmatch = 0;
4998 osp = nsp = min (osp, nsp);
5001 /* Now go through the line, inserting, writing and
5002 deleting as appropriate. */
5004 if (osp > nsp)
5006 cursor_to (f, vpos, nsp);
5007 delete_glyphs (f, osp - nsp);
5009 else if (nsp > osp)
5011 /* If going to delete chars later in line
5012 and insert earlier in the line,
5013 must delete first to avoid losing data in the insert */
5014 if (endmatch && nlen < olen + nsp - osp)
5016 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5017 delete_glyphs (f, olen + nsp - osp - nlen);
5018 olen = nlen - (nsp - osp);
5020 cursor_to (f, vpos, osp);
5021 insert_glyphs (f, 0, nsp - osp);
5023 olen += nsp - osp;
5025 tem = nsp + begmatch + endmatch;
5026 if (nlen != tem || olen != tem)
5028 if (!endmatch || nlen == olen)
5030 /* If new text being written reaches right margin, there is
5031 no need to do clear-to-eol at the end of this function
5032 (and it would not be safe, since cursor is not going to
5033 be "at the margin" after the text is done). */
5034 if (nlen == FRAME_TOTAL_COLS (f))
5035 olen = 0;
5037 /* Function write_glyphs is prepared to do nothing
5038 if passed a length <= 0. Check it here to avoid
5039 unnecessary cursor movement. */
5040 if (nlen - tem > 0)
5042 cursor_to (f, vpos, nsp + begmatch);
5043 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5046 else if (nlen > olen)
5048 /* Here, we used to have the following simple code:
5049 ----------------------------------------
5050 write_glyphs (nbody + nsp + begmatch, olen - tem);
5051 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5052 ----------------------------------------
5053 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5054 is a padding glyph. */
5055 int out = olen - tem; /* Columns to be overwritten originally. */
5056 int del;
5058 cursor_to (f, vpos, nsp + begmatch);
5060 /* Calculate columns we can actually overwrite. */
5061 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5062 out--;
5063 write_glyphs (f, nbody + nsp + begmatch, out);
5065 /* If we left columns to be overwritten, we must delete them. */
5066 del = olen - tem - out;
5067 if (del > 0)
5068 delete_glyphs (f, del);
5070 /* At last, we insert columns not yet written out. */
5071 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5072 olen = nlen;
5074 else if (olen > nlen)
5076 cursor_to (f, vpos, nsp + begmatch);
5077 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5078 delete_glyphs (f, olen - nlen);
5079 olen = nlen;
5083 just_erase:
5084 /* If any unerased characters remain after the new line, erase them. */
5085 if (olen > nlen)
5087 cursor_to (f, vpos, nlen);
5088 clear_end_of_line (f, olen);
5091 /* Exchange contents between current_frame and new_frame. */
5092 make_current (desired_matrix, current_matrix, vpos);
5097 /***********************************************************************
5098 X/Y Position -> Buffer Position
5099 ***********************************************************************/
5101 /* Determine what's under window-relative pixel position (*X, *Y).
5102 Return the OBJECT (string or buffer) that's there.
5103 Return in *POS the position in that object.
5104 Adjust *X and *Y to character positions.
5105 Return in *DX and *DY the pixel coordinates of the click,
5106 relative to the top left corner of OBJECT, or relative to
5107 the top left corner of the character glyph at (*X, *Y)
5108 if OBJECT is nil.
5109 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5110 if the coordinates point to an empty area of the display. */
5112 Lisp_Object
5113 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)
5115 struct it it;
5116 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5117 struct text_pos startp;
5118 Lisp_Object string;
5119 struct glyph_row *row;
5120 #ifdef HAVE_WINDOW_SYSTEM
5121 struct image *img = 0;
5122 #endif
5123 int x0, x1, to_x;
5124 void *itdata = NULL;
5126 /* We used to set current_buffer directly here, but that does the
5127 wrong thing with `face-remapping-alist' (bug#2044). */
5128 Fset_buffer (w->buffer);
5129 itdata = bidi_shelve_cache ();
5130 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5131 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5132 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5133 start_display (&it, w, startp);
5134 /* start_display takes into account the header-line row, but IT's
5135 vpos still counts from the glyph row that includes the window's
5136 start position. Adjust for a possible header-line row. */
5137 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w);
5139 x0 = *x;
5141 /* First, move to the beginning of the row corresponding to *Y. We
5142 need to be in that row to get the correct value of base paragraph
5143 direction for the text at (*X, *Y). */
5144 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5146 /* TO_X is the pixel position that the iterator will compute for the
5147 glyph at *X. We add it.first_visible_x because iterator
5148 positions include the hscroll. */
5149 to_x = x0 + it.first_visible_x;
5150 if (it.bidi_it.paragraph_dir == R2L)
5151 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5152 text area. This is because the iterator, even in R2L
5153 paragraphs, delivers glyphs as if they started at the left
5154 margin of the window. (When we actually produce glyphs for
5155 display, we reverse their order in PRODUCE_GLYPHS, but the
5156 iterator doesn't know about that.) The following line adjusts
5157 the pixel position to the iterator geometry, which is what
5158 move_it_* routines use. (The -1 is because in a window whose
5159 text-area width is W, the rightmost pixel position is W-1, and
5160 it should be mirrored into zero pixel position.) */
5161 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5163 /* Now move horizontally in the row to the glyph under *X. Second
5164 argument is ZV to prevent move_it_in_display_line from matching
5165 based on buffer positions. */
5166 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5167 bidi_unshelve_cache (itdata, 0);
5169 Fset_buffer (old_current_buffer);
5171 *dx = x0 + it.first_visible_x - it.current_x;
5172 *dy = *y - it.current_y;
5174 string = w->buffer;
5175 if (STRINGP (it.string))
5176 string = it.string;
5177 *pos = it.current;
5178 if (it.what == IT_COMPOSITION
5179 && it.cmp_it.nchars > 1
5180 && it.cmp_it.reversed_p)
5182 /* The current display element is a grapheme cluster in a
5183 composition. In that case, we need the position of the first
5184 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5185 it.current points to the last character of the cluster, thus
5186 we must move back to the first character of the same
5187 cluster. */
5188 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5189 if (STRINGP (it.string))
5190 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5191 else
5192 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->buffer),
5193 CHARPOS (pos->pos));
5196 #ifdef HAVE_WINDOW_SYSTEM
5197 if (it.what == IT_IMAGE)
5199 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5200 && !NILP (img->spec))
5201 *object = img->spec;
5203 #endif
5205 if (it.vpos < w->current_matrix->nrows
5206 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5207 row->enabled_p))
5209 if (it.hpos < row->used[TEXT_AREA])
5211 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5212 #ifdef HAVE_WINDOW_SYSTEM
5213 if (img)
5215 *dy -= row->ascent - glyph->ascent;
5216 *dx += glyph->slice.img.x;
5217 *dy += glyph->slice.img.y;
5218 /* Image slices positions are still relative to the entire image */
5219 *width = img->width;
5220 *height = img->height;
5222 else
5223 #endif
5225 *width = glyph->pixel_width;
5226 *height = glyph->ascent + glyph->descent;
5229 else
5231 *width = 0;
5232 *height = row->height;
5235 else
5237 *width = *height = 0;
5240 /* Add extra (default width) columns if clicked after EOL. */
5241 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5242 if (x0 > x1)
5243 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5245 *x = it.hpos;
5246 *y = it.vpos;
5248 return string;
5252 /* Value is the string under window-relative coordinates X/Y in the
5253 mode line or header line (PART says which) of window W, or nil if none.
5254 *CHARPOS is set to the position in the string returned. */
5256 Lisp_Object
5257 mode_line_string (struct window *w, enum window_part part,
5258 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5259 int *dx, int *dy, int *width, int *height)
5261 struct glyph_row *row;
5262 struct glyph *glyph, *end;
5263 int x0, y0;
5264 Lisp_Object string = Qnil;
5266 if (part == ON_MODE_LINE)
5267 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5268 else
5269 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5270 y0 = *y - row->y;
5271 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5273 if (row->mode_line_p && row->enabled_p)
5275 /* Find the glyph under X. If we find one with a string object,
5276 it's the one we were looking for. */
5277 glyph = row->glyphs[TEXT_AREA];
5278 end = glyph + row->used[TEXT_AREA];
5279 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5280 x0 -= glyph->pixel_width;
5281 *x = glyph - row->glyphs[TEXT_AREA];
5282 if (glyph < end)
5284 string = glyph->object;
5285 *charpos = glyph->charpos;
5286 *width = glyph->pixel_width;
5287 *height = glyph->ascent + glyph->descent;
5288 #ifdef HAVE_WINDOW_SYSTEM
5289 if (glyph->type == IMAGE_GLYPH)
5291 struct image *img;
5292 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5293 if (img != NULL)
5294 *object = img->spec;
5295 y0 -= row->ascent - glyph->ascent;
5297 #endif
5299 else
5301 /* Add extra (default width) columns if clicked after EOL. */
5302 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5303 *width = 0;
5304 *height = row->height;
5307 else
5309 *x = 0;
5310 x0 = 0;
5311 *width = *height = 0;
5314 *dx = x0;
5315 *dy = y0;
5317 return string;
5321 /* Value is the string under window-relative coordinates X/Y in either
5322 marginal area, or nil if none. *CHARPOS is set to the position in
5323 the string returned. */
5325 Lisp_Object
5326 marginal_area_string (struct window *w, enum window_part part,
5327 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5328 int *dx, int *dy, int *width, int *height)
5330 struct glyph_row *row = w->current_matrix->rows;
5331 struct glyph *glyph, *end;
5332 int x0, y0, i, wy = *y;
5333 int area;
5334 Lisp_Object string = Qnil;
5336 if (part == ON_LEFT_MARGIN)
5337 area = LEFT_MARGIN_AREA;
5338 else if (part == ON_RIGHT_MARGIN)
5339 area = RIGHT_MARGIN_AREA;
5340 else
5341 emacs_abort ();
5343 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5344 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5345 break;
5346 y0 = *y - row->y;
5347 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5349 if (row->enabled_p)
5351 /* Find the glyph under X. If we find one with a string object,
5352 it's the one we were looking for. */
5353 if (area == RIGHT_MARGIN_AREA)
5354 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5355 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5356 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5357 + window_box_width (w, LEFT_MARGIN_AREA)
5358 + window_box_width (w, TEXT_AREA));
5359 else
5360 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5361 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5362 : 0);
5364 glyph = row->glyphs[area];
5365 end = glyph + row->used[area];
5366 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5367 x0 -= glyph->pixel_width;
5368 *x = glyph - row->glyphs[area];
5369 if (glyph < end)
5371 string = glyph->object;
5372 *charpos = glyph->charpos;
5373 *width = glyph->pixel_width;
5374 *height = glyph->ascent + glyph->descent;
5375 #ifdef HAVE_WINDOW_SYSTEM
5376 if (glyph->type == IMAGE_GLYPH)
5378 struct image *img;
5379 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5380 if (img != NULL)
5381 *object = img->spec;
5382 y0 -= row->ascent - glyph->ascent;
5383 x0 += glyph->slice.img.x;
5384 y0 += glyph->slice.img.y;
5386 #endif
5388 else
5390 /* Add extra (default width) columns if clicked after EOL. */
5391 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5392 *width = 0;
5393 *height = row->height;
5396 else
5398 x0 = 0;
5399 *x = 0;
5400 *width = *height = 0;
5403 *dx = x0;
5404 *dy = y0;
5406 return string;
5410 /***********************************************************************
5411 Changing Frame Sizes
5412 ***********************************************************************/
5414 #ifdef SIGWINCH
5416 static void deliver_window_change_signal (int);
5418 static void
5419 handle_window_change_signal (int sig)
5421 int width, height;
5422 struct tty_display_info *tty;
5424 /* The frame size change obviously applies to a single
5425 termcap-controlled terminal, but we can't decide which.
5426 Therefore, we resize the frames corresponding to each tty.
5428 for (tty = tty_list; tty; tty = tty->next) {
5430 if (! tty->term_initted)
5431 continue;
5433 /* Suspended tty frames have tty->input == NULL avoid trying to
5434 use it. */
5435 if (!tty->input)
5436 continue;
5438 get_tty_size (fileno (tty->input), &width, &height);
5440 if (width > 5 && height > 2) {
5441 Lisp_Object tail, frame;
5443 FOR_EACH_FRAME (tail, frame)
5444 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5445 /* Record the new sizes, but don't reallocate the data
5446 structures now. Let that be done later outside of the
5447 signal handler. */
5448 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5453 static void
5454 deliver_window_change_signal (int sig)
5456 deliver_process_signal (sig, handle_window_change_signal);
5458 #endif /* SIGWINCH */
5461 /* Do any change in frame size that was requested by a signal.
5462 SAFE means this function is called from a place where it is
5463 safe to change frame sizes while a redisplay is in progress. */
5465 void
5466 do_pending_window_change (bool safe)
5468 /* If window change signal handler should have run before, run it now. */
5469 if (redisplaying_p && !safe)
5470 return;
5472 while (delayed_size_change)
5474 Lisp_Object tail, frame;
5476 delayed_size_change = 0;
5478 FOR_EACH_FRAME (tail, frame)
5480 struct frame *f = XFRAME (frame);
5482 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5483 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5484 0, 0, safe);
5490 /* Change the frame height and/or width. Values may be given as zero to
5491 indicate no change is to take place.
5493 If DELAY, assume we're being called from a signal handler, and
5494 queue the change for later - perhaps the next redisplay.
5495 Since this tries to resize windows, we can't call it
5496 from a signal handler.
5498 SAFE means this function is called from a place where it's
5499 safe to change frame sizes while a redisplay is in progress. */
5501 void
5502 change_frame_size (struct frame *f, int newheight, int newwidth,
5503 bool pretend, bool delay, bool safe)
5505 Lisp_Object tail, frame;
5507 if (FRAME_MSDOS_P (f))
5509 /* On MS-DOS, all frames use the same screen, so a change in
5510 size affects all frames. Termcap now supports multiple
5511 ttys. */
5512 FOR_EACH_FRAME (tail, frame)
5513 if (! FRAME_WINDOW_P (XFRAME (frame)))
5514 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5515 pretend, delay, safe);
5517 else
5518 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5521 static void
5522 change_frame_size_1 (struct frame *f, int newheight, int newwidth,
5523 bool pretend, bool delay, bool safe)
5525 int new_frame_total_cols;
5526 ptrdiff_t count = SPECPDL_INDEX ();
5528 /* If we can't deal with the change now, queue it for later. */
5529 if (delay || (redisplaying_p && !safe))
5531 f->new_text_lines = newheight;
5532 f->new_text_cols = newwidth;
5533 delayed_size_change = 1;
5534 return;
5537 /* This size-change overrides any pending one for this frame. */
5538 f->new_text_lines = 0;
5539 f->new_text_cols = 0;
5541 /* If an argument is zero, set it to the current value. */
5542 if (newheight == 0)
5543 newheight = FRAME_LINES (f);
5544 if (newwidth == 0)
5545 newwidth = FRAME_COLS (f);
5547 /* Compute width of windows in F. */
5548 /* Round up to the smallest acceptable size. */
5549 check_frame_size (f, &newheight, &newwidth);
5551 /* This is the width of the frame with vertical scroll bars and fringe
5552 columns. Do this after rounding - see discussion of bug#9723. */
5553 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5555 /* If we're not changing the frame size, quit now. */
5556 /* Frame width may be unchanged but the text portion may change, for
5557 example, fullscreen and remove/add scroll bar. */
5558 if (newheight == FRAME_LINES (f)
5559 /* Text portion unchanged? */
5560 && newwidth == FRAME_COLS (f)
5561 /* Frame width unchanged? */
5562 && new_frame_total_cols == FRAME_TOTAL_COLS (f))
5563 return;
5565 block_input ();
5567 #ifdef MSDOS
5568 /* We only can set screen dimensions to certain values supported
5569 by our video hardware. Try to find the smallest size greater
5570 or equal to the requested dimensions. */
5571 dos_set_window_size (&newheight, &newwidth);
5572 #endif
5574 if (newheight != FRAME_LINES (f))
5576 resize_frame_windows (f, newheight, 0);
5578 /* MSDOS frames cannot PRETEND, as they change frame size by
5579 manipulating video hardware. */
5580 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5581 FrameRows (FRAME_TTY (f)) = newheight;
5584 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5586 resize_frame_windows (f, new_frame_total_cols, 1);
5588 /* MSDOS frames cannot PRETEND, as they change frame size by
5589 manipulating video hardware. */
5590 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5591 FrameCols (FRAME_TTY (f)) = newwidth;
5593 if (WINDOWP (f->tool_bar_window))
5594 XWINDOW (f->tool_bar_window)->total_cols = newwidth;
5597 FRAME_LINES (f) = newheight;
5598 SET_FRAME_COLS (f, newwidth);
5601 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5602 int text_area_x, text_area_y, text_area_width, text_area_height;
5604 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5605 &text_area_height);
5606 if (w->cursor.x >= text_area_x + text_area_width)
5607 w->cursor.hpos = w->cursor.x = 0;
5608 if (w->cursor.y >= text_area_y + text_area_height)
5609 w->cursor.vpos = w->cursor.y = 0;
5612 adjust_glyphs (f);
5613 calculate_costs (f);
5614 SET_FRAME_GARBAGED (f);
5615 f->resized_p = 1;
5617 unblock_input ();
5619 record_unwind_current_buffer ();
5621 run_window_configuration_change_hook (f);
5623 unbind_to (count, Qnil);
5628 /***********************************************************************
5629 Terminal Related Lisp Functions
5630 ***********************************************************************/
5632 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5633 1, 1, "FOpen termscript file: ",
5634 doc: /* Start writing all terminal output to FILE as well as the terminal.
5635 FILE = nil means just close any termscript file currently open. */)
5636 (Lisp_Object file)
5638 struct tty_display_info *tty;
5640 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5641 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5642 error ("Current frame is not on a tty device");
5644 tty = CURTTY ();
5646 if (tty->termscript != 0)
5648 block_input ();
5649 fclose (tty->termscript);
5650 unblock_input ();
5652 tty->termscript = 0;
5654 if (! NILP (file))
5656 file = Fexpand_file_name (file, Qnil);
5657 tty->termscript = fopen (SSDATA (file), "w");
5658 if (tty->termscript == 0)
5659 report_file_error ("Opening termscript", Fcons (file, Qnil));
5661 return Qnil;
5665 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5666 Ssend_string_to_terminal, 1, 2, 0,
5667 doc: /* Send STRING to the terminal without alteration.
5668 Control characters in STRING will have terminal-dependent effects.
5670 Optional parameter TERMINAL specifies the tty terminal device to use.
5671 It may be a terminal object, a frame, or nil for the terminal used by
5672 the currently selected frame. In batch mode, STRING is sent to stdout
5673 when TERMINAL is nil. */)
5674 (Lisp_Object string, Lisp_Object terminal)
5676 struct terminal *t = get_terminal (terminal, 1);
5677 FILE *out;
5679 /* ??? Perhaps we should do something special for multibyte strings here. */
5680 CHECK_STRING (string);
5681 block_input ();
5683 if (!t)
5684 error ("Unknown terminal device");
5686 if (t->type == output_initial)
5687 out = stdout;
5688 else if (t->type != output_termcap && t->type != output_msdos_raw)
5689 error ("Device %d is not a termcap terminal device", t->id);
5690 else
5692 struct tty_display_info *tty = t->display_info.tty;
5694 if (! tty->output)
5695 error ("Terminal is currently suspended");
5697 if (tty->termscript)
5699 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5700 fflush (tty->termscript);
5702 out = tty->output;
5704 fwrite (SDATA (string), 1, SBYTES (string), out);
5705 fflush (out);
5706 unblock_input ();
5707 return Qnil;
5711 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5712 doc: /* Beep, or flash the screen.
5713 Also, unless an argument is given,
5714 terminate any keyboard macro currently executing. */)
5715 (Lisp_Object arg)
5717 if (!NILP (arg))
5719 if (noninteractive)
5720 putchar (07);
5721 else
5722 ring_bell (XFRAME (selected_frame));
5724 else
5725 bitch_at_user ();
5727 return Qnil;
5730 void
5731 bitch_at_user (void)
5733 if (noninteractive)
5734 putchar (07);
5735 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5737 const char *msg
5738 = "Keyboard macro terminated by a command ringing the bell";
5739 Fsignal (Quser_error, Fcons (build_string (msg), Qnil));
5741 else
5742 ring_bell (XFRAME (selected_frame));
5747 /***********************************************************************
5748 Sleeping, Waiting
5749 ***********************************************************************/
5751 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5752 doc: /* Pause, without updating display, for SECONDS seconds.
5753 SECONDS may be a floating-point value, meaning that you can wait for a
5754 fraction of a second. Optional second arg MILLISECONDS specifies an
5755 additional wait period, in milliseconds; this is for backwards compatibility.
5756 \(Not all operating systems support waiting for a fraction of a second.) */)
5757 (Lisp_Object seconds, Lisp_Object milliseconds)
5759 double duration = extract_float (seconds);
5761 if (!NILP (milliseconds))
5763 CHECK_NUMBER (milliseconds);
5764 duration += XINT (milliseconds) / 1000.0;
5767 if (0 < duration)
5769 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (duration);
5770 wait_reading_process_output (min (EMACS_SECS (t), WAIT_READING_MAX),
5771 EMACS_NSECS (t), 0, 0, Qnil, NULL, 0);
5774 return Qnil;
5778 /* This is just like wait_reading_process_output, except that
5779 it does redisplay.
5781 TIMEOUT is number of seconds to wait (float or integer),
5782 or t to wait forever.
5783 READING is true if reading input.
5784 If DISPLAY_OPTION is >0 display process output while waiting.
5785 If DISPLAY_OPTION is >1 perform an initial redisplay before waiting.
5788 Lisp_Object
5789 sit_for (Lisp_Object timeout, bool reading, int display_option)
5791 intmax_t sec;
5792 int nsec;
5793 bool do_display = display_option > 0;
5795 swallow_events (do_display);
5797 if ((detect_input_pending_run_timers (do_display))
5798 || !NILP (Vexecuting_kbd_macro))
5799 return Qnil;
5801 if (display_option > 1)
5802 redisplay_preserve_echo_area (2);
5804 if (INTEGERP (timeout))
5806 sec = XINT (timeout);
5807 if (! (0 < sec))
5808 return Qt;
5809 nsec = 0;
5811 else if (FLOATP (timeout))
5813 double seconds = XFLOAT_DATA (timeout);
5814 if (! (0 < seconds))
5815 return Qt;
5816 else
5818 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (seconds);
5819 sec = min (EMACS_SECS (t), WAIT_READING_MAX);
5820 nsec = EMACS_NSECS (t);
5823 else if (EQ (timeout, Qt))
5825 sec = 0;
5826 nsec = 0;
5828 else
5829 wrong_type_argument (Qnumberp, timeout);
5832 #ifdef USABLE_SIGIO
5833 gobble_input ();
5834 #endif
5836 wait_reading_process_output (sec, nsec, reading ? -1 : 1, do_display,
5837 Qnil, NULL, 0);
5839 return detect_input_pending () ? Qnil : Qt;
5843 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
5844 doc: /* Perform redisplay.
5845 Optional arg FORCE, if non-nil, prevents redisplay from being
5846 preempted by arriving input, even if `redisplay-dont-pause' is nil.
5847 If `redisplay-dont-pause' is non-nil (the default), redisplay is never
5848 preempted by arriving input, so FORCE does nothing.
5850 Return t if redisplay was performed, nil if redisplay was preempted
5851 immediately by pending input. */)
5852 (Lisp_Object force)
5854 ptrdiff_t count;
5856 swallow_events (1);
5857 if ((detect_input_pending_run_timers (1)
5858 && NILP (force) && !redisplay_dont_pause)
5859 || !NILP (Vexecuting_kbd_macro))
5860 return Qnil;
5862 count = SPECPDL_INDEX ();
5863 if (!NILP (force) && !redisplay_dont_pause)
5864 specbind (Qredisplay_dont_pause, Qt);
5865 redisplay_preserve_echo_area (2);
5866 unbind_to (count, Qnil);
5867 return Qt;
5872 /***********************************************************************
5873 Other Lisp Functions
5874 ***********************************************************************/
5876 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
5877 session's frames, frame names, buffers, buffer-read-only flags, and
5878 buffer-modified-flags. */
5880 static Lisp_Object frame_and_buffer_state;
5883 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
5884 Sframe_or_buffer_changed_p, 0, 1, 0,
5885 doc: /* Return non-nil if the frame and buffer state appears to have changed.
5886 VARIABLE is a variable name whose value is either nil or a state vector
5887 that will be updated to contain all frames and buffers,
5888 aside from buffers whose names start with space,
5889 along with the buffers' read-only and modified flags. This allows a fast
5890 check to see whether buffer menus might need to be recomputed.
5891 If this function returns non-nil, it updates the internal vector to reflect
5892 the current state.
5894 If VARIABLE is nil, an internal variable is used. Users should not
5895 pass nil for VARIABLE. */)
5896 (Lisp_Object variable)
5898 Lisp_Object state, tail, frame, buf;
5899 ptrdiff_t n, idx;
5901 if (! NILP (variable))
5903 CHECK_SYMBOL (variable);
5904 state = Fsymbol_value (variable);
5905 if (! VECTORP (state))
5906 goto changed;
5908 else
5909 state = frame_and_buffer_state;
5911 idx = 0;
5912 FOR_EACH_FRAME (tail, frame)
5914 if (idx == ASIZE (state))
5915 goto changed;
5916 if (!EQ (AREF (state, idx++), frame))
5917 goto changed;
5918 if (idx == ASIZE (state))
5919 goto changed;
5920 if (!EQ (AREF (state, idx++), XFRAME (frame)->name))
5921 goto changed;
5923 /* Check that the buffer info matches. */
5924 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
5926 buf = XCDR (XCAR (tail));
5927 /* Ignore buffers that aren't included in buffer lists. */
5928 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5929 continue;
5930 if (idx == ASIZE (state))
5931 goto changed;
5932 if (!EQ (AREF (state, idx++), buf))
5933 goto changed;
5934 if (idx == ASIZE (state))
5935 goto changed;
5936 if (!EQ (AREF (state, idx++), BVAR (XBUFFER (buf), read_only)))
5937 goto changed;
5938 if (idx == ASIZE (state))
5939 goto changed;
5940 if (!EQ (AREF (state, idx++), Fbuffer_modified_p (buf)))
5941 goto changed;
5943 if (idx == ASIZE (state))
5944 goto changed;
5945 /* Detect deletion of a buffer at the end of the list. */
5946 if (EQ (AREF (state, idx), Qlambda))
5947 return Qnil;
5949 /* Come here if we decide the data has changed. */
5950 changed:
5951 /* Count the size we will need.
5952 Start with 1 so there is room for at least one lambda at the end. */
5953 n = 1;
5954 FOR_EACH_FRAME (tail, frame)
5955 n += 2;
5956 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
5957 n += 3;
5958 /* Reallocate the vector if data has grown to need it,
5959 or if it has shrunk a lot. */
5960 if (! VECTORP (state)
5961 || n > ASIZE (state)
5962 || n + 20 < ASIZE (state) / 2)
5963 /* Add 20 extra so we grow it less often. */
5965 state = Fmake_vector (make_number (n + 20), Qlambda);
5966 if (! NILP (variable))
5967 Fset (variable, state);
5968 else
5969 frame_and_buffer_state = state;
5972 /* Record the new data in the (possibly reallocated) vector. */
5973 idx = 0;
5974 FOR_EACH_FRAME (tail, frame)
5976 ASET (state, idx, frame);
5977 idx++;
5978 ASET (state, idx, XFRAME (frame)->name);
5979 idx++;
5981 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
5983 buf = XCDR (XCAR (tail));
5984 /* Ignore buffers that aren't included in buffer lists. */
5985 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5986 continue;
5987 ASET (state, idx, buf);
5988 idx++;
5989 ASET (state, idx, BVAR (XBUFFER (buf), read_only));
5990 idx++;
5991 ASET (state, idx, Fbuffer_modified_p (buf));
5992 idx++;
5994 /* Fill up the vector with lambdas (always at least one). */
5995 ASET (state, idx, Qlambda);
5996 idx++;
5997 while (idx < ASIZE (state))
5999 ASET (state, idx, Qlambda);
6000 idx++;
6002 /* Make sure we didn't overflow the vector. */
6003 eassert (idx <= ASIZE (state));
6004 return Qt;
6009 /***********************************************************************
6010 Initialization
6011 ***********************************************************************/
6013 /* Initialization done when Emacs fork is started, before doing stty.
6014 Determine terminal type and set terminal_driver. Then invoke its
6015 decoding routine to set up variables in the terminal package. */
6017 void
6018 init_display (void)
6020 char *terminal_type;
6022 /* Construct the space glyph. */
6023 space_glyph.type = CHAR_GLYPH;
6024 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
6025 space_glyph.charpos = -1;
6027 inverse_video = 0;
6028 cursor_in_echo_area = 0;
6030 /* Now is the time to initialize this; it's used by init_sys_modes
6031 during startup. */
6032 Vinitial_window_system = Qnil;
6034 /* SIGWINCH needs to be handled no matter what display we start
6035 with. Otherwise newly opened tty frames will not resize
6036 automatically. */
6037 #ifdef SIGWINCH
6038 #ifndef CANNOT_DUMP
6039 if (initialized)
6040 #endif /* CANNOT_DUMP */
6042 struct sigaction action;
6043 emacs_sigaction_init (&action, deliver_window_change_signal);
6044 sigaction (SIGWINCH, &action, 0);
6046 #endif /* SIGWINCH */
6048 /* If running as a daemon, no need to initialize any frames/terminal. */
6049 if (IS_DAEMON)
6050 return;
6052 /* If the user wants to use a window system, we shouldn't bother
6053 initializing the terminal. This is especially important when the
6054 terminal is so dumb that emacs gives up before and doesn't bother
6055 using the window system.
6057 If the DISPLAY environment variable is set and nonempty,
6058 try to use X, and die with an error message if that doesn't work. */
6060 #ifdef HAVE_X_WINDOWS
6061 if (! inhibit_window_system && ! display_arg)
6063 char *display;
6064 display = getenv ("DISPLAY");
6065 display_arg = (display != 0 && *display != 0);
6067 if (display_arg && !x_display_ok (display))
6069 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6070 display);
6071 inhibit_window_system = 1;
6075 if (!inhibit_window_system && display_arg)
6077 Vinitial_window_system = Qx;
6078 #ifdef HAVE_X11
6079 Vwindow_system_version = make_number (11);
6080 #endif
6081 #ifdef GNU_LINUX
6082 /* In some versions of ncurses,
6083 tputs crashes if we have not called tgetent.
6084 So call tgetent. */
6085 { char b[2044]; tgetent (b, "xterm");}
6086 #endif
6087 return;
6089 #endif /* HAVE_X_WINDOWS */
6091 #ifdef HAVE_NTGUI
6092 if (!inhibit_window_system)
6094 Vinitial_window_system = Qw32;
6095 Vwindow_system_version = make_number (1);
6096 return;
6098 #endif /* HAVE_NTGUI */
6100 #ifdef HAVE_NS
6101 if (!inhibit_window_system
6102 #ifndef CANNOT_DUMP
6103 && initialized
6104 #endif
6107 Vinitial_window_system = Qns;
6108 Vwindow_system_version = make_number (10);
6109 return;
6111 #endif
6113 /* If no window system has been specified, try to use the terminal. */
6114 if (! isatty (0))
6115 fatal ("standard input is not a tty");
6117 #ifdef WINDOWSNT
6118 terminal_type = "w32console";
6119 #else
6120 terminal_type = getenv ("TERM");
6121 #endif
6122 if (!terminal_type)
6124 #ifdef HAVE_WINDOW_SYSTEM
6125 if (! inhibit_window_system)
6126 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6127 else
6128 #endif /* HAVE_WINDOW_SYSTEM */
6129 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6130 exit (1);
6134 struct terminal *t;
6135 struct frame *f = XFRAME (selected_frame);
6137 init_foreground_group ();
6139 /* Open a display on the controlling tty. */
6140 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6142 /* Convert the initial frame to use the new display. */
6143 if (f->output_method != output_initial)
6144 emacs_abort ();
6145 f->output_method = t->type;
6146 f->terminal = t;
6148 t->reference_count++;
6149 #ifdef MSDOS
6150 f->output_data.tty->display_info = &the_only_display_info;
6151 #else
6152 if (f->output_method == output_termcap)
6153 create_tty_output (f);
6154 #endif
6155 t->display_info.tty->top_frame = selected_frame;
6156 change_frame_size (XFRAME (selected_frame),
6157 FrameRows (t->display_info.tty),
6158 FrameCols (t->display_info.tty), 0, 0, 1);
6160 /* Delete the initial terminal. */
6161 if (--initial_terminal->reference_count == 0
6162 && initial_terminal->delete_terminal_hook)
6163 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6165 /* Update frame parameters to reflect the new type. */
6166 Fmodify_frame_parameters
6167 (selected_frame, Fcons (Fcons (Qtty_type,
6168 Ftty_type (selected_frame)), Qnil));
6169 if (t->display_info.tty->name)
6170 Fmodify_frame_parameters (selected_frame,
6171 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6172 Qnil));
6173 else
6174 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6175 Qnil));
6179 struct frame *sf = SELECTED_FRAME ();
6180 int width = FRAME_TOTAL_COLS (sf);
6181 int height = FRAME_LINES (sf);
6183 /* If these sizes are so big they cause overflow, just ignore the
6184 change. It's not clear what better we could do. The rest of
6185 the code assumes that (width + 2) * height * sizeof (struct glyph)
6186 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6187 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6188 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6189 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6190 < (width + 2) * height))
6191 fatal ("screen size %dx%d too big", width, height);
6194 calculate_costs (XFRAME (selected_frame));
6196 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6197 if (initialized
6198 && !noninteractive
6199 && NILP (Vinitial_window_system))
6201 /* For the initial frame, we don't have any way of knowing what
6202 are the foreground and background colors of the terminal. */
6203 struct frame *sf = SELECTED_FRAME ();
6205 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6206 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6207 call0 (intern ("tty-set-up-initial-frame-faces"));
6213 /***********************************************************************
6214 Blinking cursor
6215 ***********************************************************************/
6217 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6218 Sinternal_show_cursor, 2, 2, 0,
6219 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6220 WINDOW nil means use the selected window. SHOW non-nil means
6221 show a cursor in WINDOW in the next redisplay. SHOW nil means
6222 don't show a cursor. */)
6223 (Lisp_Object window, Lisp_Object show)
6225 /* Don't change cursor state while redisplaying. This could confuse
6226 output routines. */
6227 if (!redisplaying_p)
6228 decode_any_window (window)->cursor_off_p = NILP (show);
6229 return Qnil;
6233 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6234 Sinternal_show_cursor_p, 0, 1, 0,
6235 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6236 WINDOW nil or omitted means report on the selected window. */)
6237 (Lisp_Object window)
6239 return decode_any_window (window)->cursor_off_p ? Qnil : Qt;
6242 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6243 Slast_nonminibuf_frame, 0, 0, 0,
6244 doc: /* Value is last nonminibuffer frame. */)
6245 (void)
6247 Lisp_Object frame = Qnil;
6249 if (last_nonminibuf_frame)
6250 XSETFRAME (frame, last_nonminibuf_frame);
6252 return frame;
6255 /***********************************************************************
6256 Initialization
6257 ***********************************************************************/
6259 void
6260 syms_of_display (void)
6262 defsubr (&Sredraw_frame);
6263 defsubr (&Sredraw_display);
6264 defsubr (&Sframe_or_buffer_changed_p);
6265 defsubr (&Sopen_termscript);
6266 defsubr (&Sding);
6267 defsubr (&Sredisplay);
6268 defsubr (&Ssleep_for);
6269 defsubr (&Ssend_string_to_terminal);
6270 defsubr (&Sinternal_show_cursor);
6271 defsubr (&Sinternal_show_cursor_p);
6272 defsubr (&Slast_nonminibuf_frame);
6274 #ifdef GLYPH_DEBUG
6275 defsubr (&Sdump_redisplay_history);
6276 #endif
6278 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6279 staticpro (&frame_and_buffer_state);
6281 DEFSYM (Qdisplay_table, "display-table");
6282 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6284 DEFVAR_INT ("baud-rate", baud_rate,
6285 doc: /* The output baud rate of the terminal.
6286 On most systems, changing this value will affect the amount of padding
6287 and the other strategic decisions made during redisplay. */);
6289 DEFVAR_BOOL ("inverse-video", inverse_video,
6290 doc: /* Non-nil means invert the entire frame display.
6291 This means everything is in inverse video which otherwise would not be. */);
6293 DEFVAR_BOOL ("visible-bell", visible_bell,
6294 doc: /* Non-nil means try to flash the frame to represent a bell.
6296 See also `ring-bell-function'. */);
6298 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6299 doc: /* Non-nil means no need to redraw entire frame after suspending.
6300 A non-nil value is useful if the terminal can automatically preserve
6301 Emacs's frame display when you reenter Emacs.
6302 It is up to you to set this variable if your terminal can do that. */);
6304 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6305 doc: /* Name of the window system that Emacs uses for the first frame.
6306 The value is a symbol:
6307 nil for a termcap frame (a character-only terminal),
6308 'x' for an Emacs frame that is really an X window,
6309 'w32' for an Emacs frame that is a window on MS-Windows display,
6310 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6311 'pc' for a direct-write MS-DOS frame.
6313 Use of this variable as a boolean is deprecated. Instead,
6314 use `display-graphic-p' or any of the other `display-*-p'
6315 predicates which report frame's specific UI-related capabilities. */);
6317 DEFVAR_KBOARD ("window-system", Vwindow_system,
6318 doc: /* Name of window system through which the selected frame is displayed.
6319 The value is a symbol:
6320 nil for a termcap frame (a character-only terminal),
6321 'x' for an Emacs frame that is really an X window,
6322 'w32' for an Emacs frame that is a window on MS-Windows display,
6323 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6324 'pc' for a direct-write MS-DOS frame.
6326 Use of this variable as a boolean is deprecated. Instead,
6327 use `display-graphic-p' or any of the other `display-*-p'
6328 predicates which report frame's specific UI-related capabilities. */);
6330 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6331 doc: /* The version number of the window system in use.
6332 For X windows, this is 11. */);
6334 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6335 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6337 DEFVAR_LISP ("glyph-table", Vglyph_table,
6338 doc: /* Table defining how to output a glyph code to the frame.
6339 If not nil, this is a vector indexed by glyph code to define the glyph.
6340 Each element can be:
6341 integer: a glyph code which this glyph is an alias for.
6342 string: output this glyph using that string (not impl. in X windows).
6343 nil: this glyph mod 524288 is the code of a character to output,
6344 and this glyph / 524288 is the face number (see `face-id') to use
6345 while outputting it. */);
6346 Vglyph_table = Qnil;
6348 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6349 doc: /* Display table to use for buffers that specify none.
6350 See `buffer-display-table' for more information. */);
6351 Vstandard_display_table = Qnil;
6353 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6354 doc: /* Non-nil means display update isn't paused when input is detected. */);
6355 redisplay_dont_pause = 1;
6357 #ifdef CANNOT_DUMP
6358 if (noninteractive)
6359 #endif
6361 Vinitial_window_system = Qnil;
6362 Vwindow_system_version = Qnil;