Skip to content
Permalink
emacs-26.3
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
6328 lines (5257 sloc) 190 KB
/* Updating of data structures for redisplay.
Copyright (C) 1985-1988, 1993-1995, 1997-2019 Free Software Foundation,
Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "sysstdio.h"
#include <stdlib.h>
#include <unistd.h>
#include "lisp.h"
#include "termchar.h"
/* cm.h must come after dispextern.h on Windows. */
#include "dispextern.h"
#include "cm.h"
#include "buffer.h"
#include "keyboard.h"
#include "frame.h"
#include "termhooks.h"
#include "window.h"
#include "commands.h"
#include "disptab.h"
#include "blockinput.h"
#include "syssignal.h"
#include "systime.h"
#include "tparam.h"
#include "xwidget.h"
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
#include <errno.h>
#include <fpending.h>
#ifdef WINDOWSNT
#include "w32.h"
#endif
/* Structure to pass dimensions around. Used for character bounding
boxes, glyph matrix dimensions and alike. */
struct dim
{
int width;
int height;
};
/* Function prototypes. */
static void update_frame_line (struct frame *, int, bool);
static int required_matrix_height (struct window *);
static int required_matrix_width (struct window *);
static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
struct window *);
static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
struct window *);
static void adjust_decode_mode_spec_buffer (struct frame *);
static void fill_up_glyph_row_with_spaces (struct glyph_row *);
static void clear_window_matrices (struct window *, bool);
static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
static int scrolling_window (struct window *, bool);
static bool update_window_line (struct window *, int, bool *);
static void mirror_make_current (struct window *, int);
#ifdef GLYPH_DEBUG
static void check_matrix_pointers (struct glyph_matrix *,
struct glyph_matrix *);
#endif
static void mirror_line_dance (struct window *, int, int, int *, char *);
static bool update_window_tree (struct window *, bool);
static bool update_window (struct window *, bool);
static bool update_frame_1 (struct frame *, bool, bool, bool, bool);
static bool scrolling (struct frame *);
static void set_window_cursor_after_update (struct window *);
static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
static void set_window_update_flags (struct window *w, bool on_p);
/* True means last display completed. False means it was preempted. */
bool display_completed;
/* True means SIGWINCH happened when not safe. */
static bool delayed_size_change;
/* A glyph for a space. */
struct glyph space_glyph;
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* Counts of allocated structures. These counts serve to diagnose
memory leaks and double frees. */
static int glyph_matrix_count;
static int glyph_pool_count;
#endif /* GLYPH_DEBUG and ENABLE_CHECKING */
/* If non-null, the frame whose frame matrices are manipulated. If
null, window matrices are worked on. */
static struct frame *frame_matrix_frame;
/* Convert vpos and hpos from frame to window and vice versa.
This may only be used for terminal frames. */
#ifdef GLYPH_DEBUG
static int window_to_frame_vpos (struct window *, int);
static int window_to_frame_hpos (struct window *, int);
#define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
#define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
/* One element of the ring buffer containing redisplay history
information. */
struct redisplay_history
{
char trace[512 + 100];
};
/* The size of the history buffer. */
#define REDISPLAY_HISTORY_SIZE 30
/* The redisplay history buffer. */
static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
/* Next free entry in redisplay_history. */
static int history_idx;
/* A tick that's incremented each time something is added to the
history. */
static uprintmax_t history_tick;
/* Add to the redisplay history how window W has been displayed.
MSG is a trace containing the information how W's glyph matrix
has been constructed. PAUSED_P means that the update
has been interrupted for pending input. */
static void
add_window_display_history (struct window *w, const char *msg, bool paused_p)
{
char *buf;
void *ptr = w;
if (history_idx >= REDISPLAY_HISTORY_SIZE)
history_idx = 0;
buf = redisplay_history[history_idx].trace;
++history_idx;
snprintf (buf, sizeof redisplay_history[0].trace,
"%"pMu": window %p (%s)%s\n%s",
history_tick++,
ptr,
((BUFFERP (w->contents)
&& STRINGP (BVAR (XBUFFER (w->contents), name)))
? SSDATA (BVAR (XBUFFER (w->contents), name))
: "???"),
paused_p ? " ***paused***" : "",
msg);
}
/* Add to the redisplay history that frame F has been displayed.
PAUSED_P means that the update has been interrupted for
pending input. */
static void
add_frame_display_history (struct frame *f, bool paused_p)
{
char *buf;
void *ptr = f;
if (history_idx >= REDISPLAY_HISTORY_SIZE)
history_idx = 0;
buf = redisplay_history[history_idx].trace;
++history_idx;
sprintf (buf, "%"pMu": update frame %p%s",
history_tick++,
ptr, paused_p ? " ***paused***" : "");
}
DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
Sdump_redisplay_history, 0, 0, "",
doc: /* Dump redisplay history to stderr. */)
(void)
{
int i;
for (i = history_idx - 1; i != history_idx; --i)
{
if (i < 0)
i = REDISPLAY_HISTORY_SIZE - 1;
fprintf (stderr, "%s\n", redisplay_history[i].trace);
}
return Qnil;
}
#else /* not GLYPH_DEBUG */
#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
#endif /* GLYPH_DEBUG */
#if (defined PROFILING \
&& (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
&& !HAVE___EXECUTABLE_START)
/* This function comes first in the Emacs executable and is used only
to estimate the text start for profiling. */
void
__executable_start (void)
{
emacs_abort ();
}
#endif
/***********************************************************************
Glyph Matrices
***********************************************************************/
/* Allocate and return a glyph_matrix structure. POOL is the glyph
pool from which memory for the matrix should be allocated, or null
for window-based redisplay where no glyph pools are used. The
member `pool' of the glyph matrix structure returned is set to
POOL, the structure is otherwise zeroed. */
static struct glyph_matrix *
new_glyph_matrix (struct glyph_pool *pool)
{
struct glyph_matrix *result = xzalloc (sizeof *result);
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* Increment number of allocated matrices. This count is used
to detect memory leaks. */
++glyph_matrix_count;
#endif
/* Set pool and return. */
result->pool = pool;
return result;
}
/* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
glyph_matrix_count is decremented when a matrix is freed. If the count
gets negative, more structures were freed than allocated, i.e. one matrix
was freed more than once or a bogus pointer was passed to this function.
If MATRIX->pool is null, this means that the matrix manages its own
glyph memory---this is done for matrices on X frames. Freeing the
matrix also frees the glyph memory in this case. */
static void
free_glyph_matrix (struct glyph_matrix *matrix)
{
if (matrix)
{
int i;
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* Detect the case that more matrices are freed than were
allocated. */
--glyph_matrix_count;
eassert (glyph_matrix_count >= 0);
#endif
/* Free glyph memory if MATRIX owns it. */
if (matrix->pool == NULL)
for (i = 0; i < matrix->rows_allocated; ++i)
xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
/* Free row structures and the matrix itself. */
xfree (matrix->rows);
xfree (matrix);
}
}
/* Return the number of glyphs to reserve for a marginal area of
window W. TOTAL_GLYPHS is the number of glyphs in a complete
display line of window W. MARGIN gives the width of the marginal
area in canonical character units. */
static int
margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
{
if (margin > 0)
{
int width = w->total_cols;
double d = max (0, margin);
d = min (width / 2 - 1, d);
/* Since MARGIN is positive, we cannot possibly have less than
one glyph for the marginal area. */
return max (1, (int) ((double) total_glyphs / width * d));
}
return 0;
}
/* Return true if ROW's hash value is correct.
Optimized away if ENABLE_CHECKING is not defined. */
static bool
verify_row_hash (struct glyph_row *row)
{
return row->hash == row_hash (row);
}
/* Adjust glyph matrix MATRIX on window W or on a frame to changed
window sizes.
W is null if the function is called for a frame glyph matrix.
Otherwise it is the window MATRIX is a member of. X and Y are the
indices of the first column and row of MATRIX within the frame
matrix, if such a matrix exists. They are zero for purely
window-based redisplay. DIM is the needed size of the matrix.
In window-based redisplay, where no frame matrices exist, glyph
matrices manage their own glyph storage. Otherwise, they allocate
storage from a common frame glyph pool which can be found in
MATRIX->pool.
The reason for this memory management strategy is to avoid complete
frame redraws if possible. When we allocate from a common pool, a
change of the location or size of a sub-matrix within the pool
requires a complete redisplay of the frame because we cannot easily
make sure that the current matrices of all windows still agree with
what is displayed on the screen. While this is usually fast, it
leads to screen flickering. */
static void
adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
{
int i;
int new_rows;
bool marginal_areas_changed_p = 0;
bool header_line_changed_p = 0;
bool header_line_p = 0;
int left = -1, right = -1;
int window_width = -1, window_height = -1;
/* See if W had a header line that has disappeared now, or vice versa.
Get W's size. */
if (w)
{
window_box (w, ANY_AREA, 0, 0, &window_width, &window_height);
header_line_p = window_wants_header_line (w);
header_line_changed_p = header_line_p != matrix->header_line_p;
}
matrix->header_line_p = header_line_p;
/* If POOL is null, MATRIX is a window matrix for window-based redisplay.
Do nothing if MATRIX' size, position, vscroll, and marginal areas
haven't changed. This optimization is important because preserving
the matrix means preventing redisplay. */
eassume (w != NULL || matrix->pool != NULL);
if (matrix->pool == NULL)
{
left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
eassert (left >= 0 && right >= 0);
marginal_areas_changed_p = (left != matrix->left_margin_glyphs
|| right != matrix->right_margin_glyphs);
if (!marginal_areas_changed_p
&& !XFRAME (w->frame)->fonts_changed
&& !header_line_changed_p
&& matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
&& matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
&& matrix->window_height == window_height
&& matrix->window_vscroll == w->vscroll
&& matrix->window_width == window_width)
return;
}
/* Enlarge MATRIX->rows if necessary. New rows are cleared. */
if (matrix->rows_allocated < dim.height)
{
int old_alloc = matrix->rows_allocated;
new_rows = dim.height - matrix->rows_allocated;
matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
new_rows, INT_MAX, sizeof *matrix->rows);
memset (matrix->rows + old_alloc, 0,
(matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
}
else
new_rows = 0;
/* If POOL is not null, MATRIX is a frame matrix or a window matrix
on a frame not using window-based redisplay. Set up pointers for
each row into the glyph pool. */
if (matrix->pool)
{
eassert (matrix->pool->glyphs);
if (w)
{
left = margin_glyphs_to_reserve (w, dim.width,
w->left_margin_cols);
right = margin_glyphs_to_reserve (w, dim.width,
w->right_margin_cols);
}
else
left = right = 0;
for (i = 0; i < dim.height; ++i)
{
struct glyph_row *row = &matrix->rows[i];
row->glyphs[LEFT_MARGIN_AREA]
= (matrix->pool->glyphs
+ (y + i) * matrix->pool->ncolumns
+ x);
if (w == NULL
|| (row == matrix->rows + dim.height - 1
&& window_wants_mode_line (w))
|| (row == matrix->rows && matrix->header_line_p))
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA];
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width;
row->glyphs[LAST_AREA]
= row->glyphs[RIGHT_MARGIN_AREA];
}
else
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + left;
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width - left - right;
row->glyphs[LAST_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + dim.width;
}
}
matrix->left_margin_glyphs = left;
matrix->right_margin_glyphs = right;
}
else
{
/* If MATRIX->pool is null, MATRIX is responsible for managing
its own memory. It is a window matrix for window-based redisplay.
Allocate glyph memory from the heap. */
if (dim.width > matrix->matrix_w
|| new_rows
|| header_line_changed_p
|| marginal_areas_changed_p)
{
struct glyph_row *row = matrix->rows;
struct glyph_row *end = row + matrix->rows_allocated;
while (row < end)
{
row->glyphs[LEFT_MARGIN_AREA]
= xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
dim.width, sizeof (struct glyph));
/* The mode line, if displayed, never has marginal areas. */
if ((row == matrix->rows + dim.height - 1
&& !(w && window_wants_mode_line (w)))
|| (row == matrix->rows && matrix->header_line_p))
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA];
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width;
row->glyphs[LAST_AREA]
= row->glyphs[RIGHT_MARGIN_AREA];
}
else
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + left;
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width - left - right;
row->glyphs[LAST_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + dim.width;
}
++row;
}
}
eassert (left >= 0 && right >= 0);
matrix->left_margin_glyphs = left;
matrix->right_margin_glyphs = right;
}
/* Number of rows to be used by MATRIX. */
matrix->nrows = dim.height;
eassert (matrix->nrows >= 0);
if (w)
{
if (matrix == w->current_matrix)
{
/* Mark rows in a current matrix of a window as not having
valid contents. It's important to not do this for
desired matrices. When Emacs starts, it may already be
building desired matrices when this function runs. */
if (window_width < 0)
window_width = window_box_width (w, -1);
/* Optimize the case that only the height has changed (C-x 2,
upper window). Invalidate all rows that are no longer part
of the window. */
if (!marginal_areas_changed_p
&& !header_line_changed_p
&& new_rows == 0
&& dim.width == matrix->matrix_w
&& matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
&& matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
&& matrix->window_width == window_width)
{
/* Find the last row in the window. */
for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
{
++i;
break;
}
/* Window end is invalid, if inside of the rows that
are invalidated below. */
if (w->window_end_vpos >= i)
w->window_end_valid = 0;
while (i < matrix->nrows)
matrix->rows[i++].enabled_p = false;
}
else
{
for (i = 0; i < matrix->nrows; ++i)
matrix->rows[i].enabled_p = false;
}
/* We've disabled the mode-line row, so force redrawing of
the mode line, if any, since otherwise it will remain
disabled in the current matrix, and expose events won't
redraw it. */
if (window_wants_mode_line (w))
w->update_mode_line = 1;
}
else if (matrix == w->desired_matrix)
{
/* Rows in desired matrices always have to be cleared;
redisplay expects this is the case when it runs, so it
had better be the case when we adjust matrices between
redisplays. */
for (i = 0; i < matrix->nrows; ++i)
matrix->rows[i].enabled_p = false;
}
}
/* Remember last values to be able to optimize frame redraws. */
matrix->matrix_x = x;
matrix->matrix_y = y;
matrix->matrix_w = dim.width;
matrix->matrix_h = dim.height;
/* Record the top y location and height of W at the time the matrix
was last adjusted. This is used to optimize redisplay above. */
if (w)
{
matrix->window_pixel_left = WINDOW_LEFT_PIXEL_EDGE (w);
matrix->window_pixel_top = WINDOW_TOP_PIXEL_EDGE (w);
matrix->window_height = window_height;
matrix->window_width = window_width;
matrix->window_vscroll = w->vscroll;
}
}
/* Reverse the contents of rows in MATRIX between START and END. The
contents of the row at END - 1 end up at START, END - 2 at START +
1 etc. This is part of the implementation of rotate_matrix (see
below). */
static void
reverse_rows (struct glyph_matrix *matrix, int start, int end)
{
int i, j;
for (i = start, j = end - 1; i < j; ++i, --j)
{
/* Non-ISO HP/UX compiler doesn't like auto struct
initialization. */
struct glyph_row temp;
temp = matrix->rows[i];
matrix->rows[i] = matrix->rows[j];
matrix->rows[j] = temp;
}
}
/* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
1 by BY positions. BY < 0 means rotate left, i.e. towards lower
indices. (Note: this does not copy glyphs, only glyph pointers in
row structures are moved around).
The algorithm used for rotating the vector was, I believe, first
described by Kernighan. See the vector R as consisting of two
sub-vectors AB, where A has length BY for BY >= 0. The result
after rotating is then BA. Reverse both sub-vectors to get ArBr
and reverse the result to get (ArBr)r which is BA. Similar for
rotating right. */
void
rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
{
if (by < 0)
{
/* Up (rotate left, i.e. towards lower indices). */
by = -by;
reverse_rows (matrix, first, first + by);
reverse_rows (matrix, first + by, last);
reverse_rows (matrix, first, last);
}
else if (by > 0)
{
/* Down (rotate right, i.e. towards higher indices). */
reverse_rows (matrix, last - by, last);
reverse_rows (matrix, first, last - by);
reverse_rows (matrix, first, last);
}
}
/* Increment buffer positions in glyph rows of MATRIX. Do it for rows
with indices START <= index < END. Increment positions by DELTA/
DELTA_BYTES. */
void
increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
ptrdiff_t delta, ptrdiff_t delta_bytes)
{
/* Check that START and END are reasonable values. */
eassert (start >= 0 && start <= matrix->nrows);
eassert (end >= 0 && end <= matrix->nrows);
eassert (start <= end);
for (; start < end; ++start)
increment_row_positions (matrix->rows + start, delta, delta_bytes);
}
/* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
START and END are the row indices of the first and last + 1 row to clear. */
void
clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
{
eassert (start <= end);
eassert (start >= 0 && (start < matrix->nrows
/* matrix->nrows can be 0 for the initial frame. */
|| (matrix->nrows == 0)));
eassert (end >= 0 && end <= matrix->nrows);
for (; start < end; ++start)
matrix->rows[start].enabled_p = false;
}
/* Clear MATRIX.
Empty all rows in MATRIX by clearing their enabled_p flags.
The function prepare_desired_row will eventually really clear a row
when it sees one with a false enabled_p flag.
Reset update hints to default values. The only update hint
currently present is the flag MATRIX->no_scrolling_p. */
void
clear_glyph_matrix (struct glyph_matrix *matrix)
{
if (matrix)
{
clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
matrix->no_scrolling_p = 0;
}
}
/* Shift part of the glyph matrix MATRIX of window W up or down.
Increment y-positions in glyph rows between START and END by DY,
and recompute their visible height. */
void
shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
{
int min_y, max_y;
eassert (start <= end);
eassert (start >= 0 && start < matrix->nrows);
eassert (end >= 0 && end <= matrix->nrows);
min_y = WINDOW_HEADER_LINE_HEIGHT (w);
max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
for (; start < end; ++start)
{
struct glyph_row *row = &matrix->rows[start];
row->y += dy;
row->visible_height = row->height;
if (row->y < min_y)
row->visible_height -= min_y - row->y;
if (row->y + row->height > max_y)
row->visible_height -= row->y + row->height - max_y;
if (row->fringe_bitmap_periodic_p)
row->redraw_fringe_bitmaps_p = 1;
}
}
/* Mark all rows in current matrices of frame F as invalid. Marking
invalid is done by setting enabled_p to zero for all rows in a
current matrix. */
void
clear_current_matrices (register struct frame *f)
{
/* Clear frame current matrix, if we have one. */
if (f->current_matrix)
clear_glyph_matrix (f->current_matrix);
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
/* Clear the matrix of the menu bar window, if such a window exists.
The menu bar window is currently used to display menus on X when
no toolkit support is compiled in. */
if (WINDOWP (f->menu_bar_window))
clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
#endif
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
/* Clear the matrix of the tool-bar window, if any. */
if (WINDOWP (f->tool_bar_window))
clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
#endif
/* Clear current window matrices. */
eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
}
/* Clear out all display lines of F for a coming redisplay. */
void
clear_desired_matrices (register struct frame *f)
{
if (f->desired_matrix)
clear_glyph_matrix (f->desired_matrix);
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
if (WINDOWP (f->menu_bar_window))
clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
#endif
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
if (WINDOWP (f->tool_bar_window))
clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
#endif
/* Do it for window matrices. */
eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
}
/* Clear matrices in window tree rooted in W. If DESIRED_P,
clear desired matrices, otherwise clear current matrices. */
static void
clear_window_matrices (struct window *w, bool desired_p)
{
while (w)
{
if (WINDOWP (w->contents))
clear_window_matrices (XWINDOW (w->contents), desired_p);
else
{
if (desired_p)
clear_glyph_matrix (w->desired_matrix);
else
{
clear_glyph_matrix (w->current_matrix);
w->window_end_valid = 0;
}
}
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
}
/***********************************************************************
Glyph Rows
See dispextern.h for an overall explanation of glyph rows.
***********************************************************************/
/* Clear glyph row ROW. NOTE: this code relies on the current
layout of `glyphs' and `used' fields of `struct glyph_row'. */
void
clear_glyph_row (struct glyph_row *row)
{
enum { off = offsetof (struct glyph_row, used) };
/* Zero everything except pointers in `glyphs'. */
memset (row->used, 0, sizeof *row - off);
}
/* Make ROW an empty, enabled row of canonical character height,
in window W starting at y-position Y. */
void
blank_row (struct window *w, struct glyph_row *row, int y)
{
int min_y, max_y;
min_y = WINDOW_HEADER_LINE_HEIGHT (w);
max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
clear_glyph_row (row);
row->y = y;
row->ascent = row->phys_ascent = 0;
row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
row->visible_height = row->height;
if (row->y < min_y)
row->visible_height -= min_y - row->y;
if (row->y + row->height > max_y)
row->visible_height -= row->y + row->height - max_y;
row->enabled_p = true;
}
/* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
are the amounts by which to change positions. Note that the first
glyph of the text area of a row can have a buffer position even if
the used count of the text area is zero. Such rows display line
ends. */
static void
increment_row_positions (struct glyph_row *row,
ptrdiff_t delta, ptrdiff_t delta_bytes)
{
int area, i;
/* Increment start and end positions. */
MATRIX_ROW_START_CHARPOS (row) += delta;
MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
MATRIX_ROW_END_CHARPOS (row) += delta;
MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
CHARPOS (row->start.pos) += delta;
BYTEPOS (row->start.pos) += delta_bytes;
CHARPOS (row->end.pos) += delta;
BYTEPOS (row->end.pos) += delta_bytes;
if (!row->enabled_p)
return;
/* Increment positions in glyphs. */
for (area = 0; area < LAST_AREA; ++area)
for (i = 0; i < row->used[area]; ++i)
if (BUFFERP (row->glyphs[area][i].object)
&& row->glyphs[area][i].charpos > 0)
row->glyphs[area][i].charpos += delta;
/* Capture the case of rows displaying a line end. */
if (row->used[TEXT_AREA] == 0
&& MATRIX_ROW_DISPLAYS_TEXT_P (row))
row->glyphs[TEXT_AREA]->charpos += delta;
}
#if 0
/* Swap glyphs between two glyph rows A and B. This exchanges glyph
contents, i.e. glyph structure contents are exchanged between A and
B without changing glyph pointers in A and B. */
static void
swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
{
int area;
for (area = 0; area < LAST_AREA; ++area)
{
/* Number of glyphs to swap. */
int max_used = max (a->used[area], b->used[area]);
/* Start of glyphs in area of row A. */
struct glyph *glyph_a = a->glyphs[area];
/* End + 1 of glyphs in area of row A. */
struct glyph *glyph_a_end = a->glyphs[max_used];
/* Start of glyphs in area of row B. */
struct glyph *glyph_b = b->glyphs[area];
while (glyph_a < glyph_a_end)
{
/* Non-ISO HP/UX compiler doesn't like auto struct
initialization. */
struct glyph temp;
temp = *glyph_a;
*glyph_a = *glyph_b;
*glyph_b = temp;
++glyph_a;
++glyph_b;
}
}
}
#endif /* 0 */
/* Exchange pointers to glyph memory between glyph rows A and B. Also
exchange the used[] array and the hash values of the rows, because
these should all go together for the row's hash value to be
correct. */
static void
swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
{
int i;
unsigned hash_tem = a->hash;
for (i = 0; i < LAST_AREA + 1; ++i)
{
struct glyph *temp = a->glyphs[i];
a->glyphs[i] = b->glyphs[i];
b->glyphs[i] = temp;
if (i < LAST_AREA)
{
short used_tem = a->used[i];
a->used[i] = b->used[i];
b->used[i] = used_tem;
}
}
a->hash = b->hash;
b->hash = hash_tem;
}
/* Copy glyph row structure FROM to glyph row structure TO, except that
glyph pointers, the `used' counts, and the hash values in the structures
are left unchanged. NOTE: this code relies on the current layout of
`glyphs', `used', `hash' and `x' fields of `struct glyph_row'. */
static void
copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
{
enum { off = offsetof (struct glyph_row, x) };
memcpy (&to->x, &from->x, sizeof *to - off);
}
/* Assign glyph row FROM to glyph row TO. This works like a structure
assignment TO = FROM, except that glyph pointers are not copied but
exchanged between TO and FROM. Pointers must be exchanged to avoid
a memory leak. */
static void
assign_row (struct glyph_row *to, struct glyph_row *from)
{
swap_glyph_pointers (to, from);
copy_row_except_pointers (to, from);
}
/* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
a row in a window matrix, is a slice of the glyph memory of the
glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
is true if the glyph memory of WINDOW_ROW is part of the glyph
memory of FRAME_ROW. */
#ifdef GLYPH_DEBUG
static bool
glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
{
struct glyph *window_glyph_start = window_row->glyphs[0];
struct glyph *frame_glyph_start = frame_row->glyphs[0];
struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
return (frame_glyph_start <= window_glyph_start
&& window_glyph_start < frame_glyph_end);
}
#endif /* GLYPH_DEBUG */
#if 0
/* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
in WINDOW_MATRIX is found satisfying the condition. */
static struct glyph_row *
find_glyph_row_slice (struct glyph_matrix *window_matrix,
struct glyph_matrix *frame_matrix, int row)
{
int i;
eassert (row >= 0 && row < frame_matrix->nrows);
for (i = 0; i < window_matrix->nrows; ++i)
if (glyph_row_slice_p (window_matrix->rows + i,
frame_matrix->rows + row))
break;
return i < window_matrix->nrows ? window_matrix->rows + i : 0;
}
#endif /* 0 */
/* Prepare ROW for display in windows W. Desired rows are cleared
lazily, i.e. they are only marked as to be cleared by setting their
enabled_p flag to zero. When a row is to be displayed, a prior
call to this function really clears it. In addition, this function
makes sure the marginal areas of ROW are in sync with the window's
display margins. MODE_LINE_P non-zero means we are preparing a
glyph row for header line or mode line. */
void
prepare_desired_row (struct window *w, struct glyph_row *row, bool mode_line_p)
{
if (!row->enabled_p)
{
bool rp = row->reversed_p;
clear_glyph_row (row);
row->enabled_p = true;
row->reversed_p = rp;
}
if (mode_line_p)
{
/* Mode and header lines, if displayed, never have marginal
areas. If we are called with MODE_LINE_P non-zero, we are
displaying the mode/header line in this window, and so the
marginal areas of this glyph row should be eliminated. This
is needed when the mode/header line is switched on in a
window that has display margins. */
if (w->left_margin_cols > 0)
row->glyphs[TEXT_AREA] = row->glyphs[LEFT_MARGIN_AREA];
if (w->right_margin_cols > 0)
row->glyphs[RIGHT_MARGIN_AREA] = row->glyphs[LAST_AREA];
}
else
{
/* The real number of glyphs reserved for the margins is
recorded in the glyph matrix, and can be different from
window's left_margin_cols and right_margin_cols; see
margin_glyphs_to_reserve for when that happens. */
int left = w->desired_matrix->left_margin_glyphs;
int right = w->desired_matrix->right_margin_glyphs;
/* Make sure the marginal areas of this row are in sync with
what the window wants, when the row actually displays text
and not header/mode line. */
if (w->left_margin_cols > 0
&& (left != row->glyphs[TEXT_AREA] - row->glyphs[LEFT_MARGIN_AREA]))
row->glyphs[TEXT_AREA] = row->glyphs[LEFT_MARGIN_AREA] + left;
if (w->right_margin_cols > 0
&& (right != row->glyphs[LAST_AREA] - row->glyphs[RIGHT_MARGIN_AREA]))
row->glyphs[RIGHT_MARGIN_AREA] = row->glyphs[LAST_AREA] - right;
}
}
/* Return a hash code for glyph row ROW, which may
be from current or desired matrix of frame F. */
static unsigned
line_hash_code (struct frame *f, struct glyph_row *row)
{
unsigned hash = 0;
if (row->enabled_p)
{
struct glyph *glyph = row->glyphs[TEXT_AREA];
struct glyph *end = glyph + row->used[TEXT_AREA];
while (glyph < end)
{
int c = glyph->u.ch;
int face_id = glyph->face_id;
if (FRAME_MUST_WRITE_SPACES (f))
c -= SPACEGLYPH;
hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
++glyph;
}
if (hash == 0)
hash = 1;
}
return hash;
}
/* Return the cost of drawing line VPOS in MATRIX, which may
be current or desired matrix of frame F. The cost equals
the number of characters in the line. If must_write_spaces
is zero, leading and trailing spaces are ignored. */
static int
line_draw_cost (struct frame *f, struct glyph_matrix *matrix, int vpos)
{
struct glyph_row *row = matrix->rows + vpos;
struct glyph *beg = row->glyphs[TEXT_AREA];
struct glyph *end = beg + row->used[TEXT_AREA];
int len;
Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
/* Ignore trailing and leading spaces if we can. */
if (!FRAME_MUST_WRITE_SPACES (f))
{
/* Skip from the end over trailing spaces. */
while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
--end;
/* All blank line. */
if (end == beg)
return 0;
/* Skip over leading spaces. */
while (CHAR_GLYPH_SPACE_P (*beg))
++beg;
}
/* If we don't have a glyph-table, each glyph is one character,
so return the number of glyphs. */
if (glyph_table_base == 0)
len = end - beg;
else
{
/* Otherwise, scan the glyphs and accumulate their total length
in LEN. */
len = 0;
while (beg < end)
{
GLYPH g;
SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
if (GLYPH_INVALID_P (g)
|| GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
len += 1;
else
len += GLYPH_LENGTH (glyph_table_base, g);
++beg;
}
}
return len;
}
/* Return true if the glyph rows A and B have equal contents.
MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too. */
static bool
row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
{
eassert (verify_row_hash (a));
eassert (verify_row_hash (b));
if (a == b)
return 1;
else if (a->hash != b->hash)
return 0;
else
{
struct glyph *a_glyph, *b_glyph, *a_end;
int area;
if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
return 0;
/* Compare glyphs. */
for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
{
if (a->used[area] != b->used[area])
return 0;
a_glyph = a->glyphs[area];
a_end = a_glyph + a->used[area];
b_glyph = b->glyphs[area];
while (a_glyph < a_end
&& GLYPH_EQUAL_P (a_glyph, b_glyph))
++a_glyph, ++b_glyph;
if (a_glyph != a_end)
return 0;
}
if (a->fill_line_p != b->fill_line_p
|| a->cursor_in_fringe_p != b->cursor_in_fringe_p
|| a->left_fringe_bitmap != b->left_fringe_bitmap
|| a->left_fringe_face_id != b->left_fringe_face_id
|| a->left_fringe_offset != b->left_fringe_offset
|| a->right_fringe_bitmap != b->right_fringe_bitmap
|| a->right_fringe_face_id != b->right_fringe_face_id
|| a->right_fringe_offset != b->right_fringe_offset
|| a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
|| a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
|| a->exact_window_width_line_p != b->exact_window_width_line_p
|| a->overlapped_p != b->overlapped_p
|| (MATRIX_ROW_CONTINUATION_LINE_P (a)
!= MATRIX_ROW_CONTINUATION_LINE_P (b))
|| a->reversed_p != b->reversed_p
/* Different partially visible characters on left margin. */
|| a->x != b->x
/* Different height. */
|| a->ascent != b->ascent
|| a->phys_ascent != b->phys_ascent
|| a->phys_height != b->phys_height
|| a->visible_height != b->visible_height)
return 0;
}
return 1;
}
/***********************************************************************
Glyph Pool
See dispextern.h for an overall explanation of glyph pools.
***********************************************************************/
/* Allocate a glyph_pool structure. The structure returned is initialized
with zeros. If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global
variable glyph_pool_count is incremented for each pool allocated. */
static struct glyph_pool *
new_glyph_pool (void)
{
struct glyph_pool *result = xzalloc (sizeof *result);
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* For memory leak and double deletion checking. */
++glyph_pool_count;
#endif
return result;
}
/* Free a glyph_pool structure POOL. The function may be called with
a null POOL pointer. If GLYPH_DEBUG and ENABLE_CHECKING are in effect,
global variable glyph_pool_count is decremented with every pool structure
freed. If this count gets negative, more structures were freed than
allocated, i.e. one structure must have been freed more than once or
a bogus pointer was passed to free_glyph_pool. */
static void
free_glyph_pool (struct glyph_pool *pool)
{
if (pool)
{
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* More freed than allocated? */
--glyph_pool_count;
eassert (glyph_pool_count >= 0);
#endif
xfree (pool->glyphs);
xfree (pool);
}
}
/* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
columns we need. This function never shrinks a pool. The only
case in which this would make sense, would be when a frame's size
is changed from a large value to a smaller one. But, if someone
does it once, we can expect that he will do it again.
Return true if the pool changed in a way which makes
re-adjusting window glyph matrices necessary. */
static bool
realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
{
ptrdiff_t needed;
bool changed_p;
changed_p = (pool->glyphs == 0
|| matrix_dim.height != pool->nrows
|| matrix_dim.width != pool->ncolumns);
/* Enlarge the glyph pool. */
if (INT_MULTIPLY_WRAPV (matrix_dim.height, matrix_dim.width, &needed))
memory_full (SIZE_MAX);
if (needed > pool->nglyphs)
{
ptrdiff_t old_nglyphs = pool->nglyphs;
pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
needed - old_nglyphs, -1, sizeof *pool->glyphs);
memclear (pool->glyphs + old_nglyphs,
(pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
}
/* Remember the number of rows and columns because (a) we use them
to do sanity checks, and (b) the number of columns determines
where rows in the frame matrix start---this must be available to
determine pointers to rows of window sub-matrices. */
pool->nrows = matrix_dim.height;
pool->ncolumns = matrix_dim.width;
return changed_p;
}
/***********************************************************************
Debug Code
***********************************************************************/
#ifdef GLYPH_DEBUG
/* Flush standard output. This is sometimes useful to call from the debugger.
XXX Maybe this should be changed to flush the current terminal instead of
stdout.
*/
void flush_stdout (void) EXTERNALLY_VISIBLE;
void
flush_stdout (void)
{
fflush (stdout);
}
/* Check that no glyph pointers have been lost in MATRIX. If a
pointer has been lost, e.g. by using a structure assignment between
rows, at least one pointer must occur more than once in the rows of
MATRIX. */
void
check_matrix_pointer_lossage (struct glyph_matrix *matrix)
{
int i, j;
for (i = 0; i < matrix->nrows; ++i)
for (j = 0; j < matrix->nrows; ++j)
eassert (i == j
|| (matrix->rows[i].glyphs[TEXT_AREA]
!= matrix->rows[j].glyphs[TEXT_AREA]));
}
/* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
struct glyph_row *
matrix_row (struct glyph_matrix *matrix, int row)
{
eassert (matrix && matrix->rows);
eassert (row >= 0 && row < matrix->nrows);
/* That's really too slow for normal testing because this function
is called almost everywhere. Although---it's still astonishingly
fast, so it is valuable to have for debugging purposes. */
#if 0
check_matrix_pointer_lossage (matrix);
#endif
return matrix->rows + row;
}
#if 0 /* This function makes invalid assumptions when text is
partially invisible. But it might come handy for debugging
nevertheless. */
/* Check invariants that must hold for an up to date current matrix of
window W. */
static void
check_matrix_invariants (struct window *w)
{
struct glyph_matrix *matrix = w->current_matrix;
int yb = window_text_bottom_y (w);
struct glyph_row *row = matrix->rows;
struct glyph_row *last_text_row = NULL;
struct buffer *saved = current_buffer;
struct buffer *buffer = XBUFFER (w->contents);
int c;
/* This can sometimes happen for a fresh window. */
if (matrix->nrows < 2)
return;
set_buffer_temp (buffer);
/* Note: last row is always reserved for the mode line. */
while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
&& MATRIX_ROW_BOTTOM_Y (row) < yb)
{
struct glyph_row *next = row + 1;
if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
last_text_row = row;
/* Check that character and byte positions are in sync. */
eassert (MATRIX_ROW_START_BYTEPOS (row)
== CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
eassert (BYTEPOS (row->start.pos)
== CHAR_TO_BYTE (CHARPOS (row->start.pos)));
/* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
have such a position temporarily in case of a minibuffer
displaying something like `[Sole completion]' at its end. */
if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
{
eassert (MATRIX_ROW_END_BYTEPOS (row)
== CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
eassert (BYTEPOS (row->end.pos)
== CHAR_TO_BYTE (CHARPOS (row->end.pos)));
}
/* Check that end position of `row' is equal to start position
of next row. */
if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
{
eassert (MATRIX_ROW_END_CHARPOS (row)
== MATRIX_ROW_START_CHARPOS (next));
eassert (MATRIX_ROW_END_BYTEPOS (row)
== MATRIX_ROW_START_BYTEPOS (next));
eassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
eassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
}
row = next;
}
eassert (w->current_matrix->nrows == w->desired_matrix->nrows);
eassert (w->desired_matrix->rows != NULL);
set_buffer_temp (saved);
}
#endif /* 0 */
#endif /* GLYPH_DEBUG */
/**********************************************************************
Allocating/ Adjusting Glyph Matrices
**********************************************************************/
/* Allocate glyph matrices over a window tree for a frame-based
redisplay
X and Y are column/row within the frame glyph matrix where
sub-matrices for the window tree rooted at WINDOW must be
allocated. DIM_ONLY_P means that the caller of this
function is only interested in the result matrix dimension, and
matrix adjustments should not be performed.
The function returns the total width/height of the sub-matrices of
the window tree. If called on a frame root window, the computation
will take the mini-buffer window into account.
*WINDOW_CHANGE_FLAGS is set to a bit mask with bits
NEW_LEAF_MATRIX set if any window in the tree did not have a
glyph matrices yet, and
CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
any window in the tree will be changed or have been changed (see
DIM_ONLY_P)
*WINDOW_CHANGE_FLAGS must be initialized by the caller of this
function.
Windows are arranged into chains of windows on the same level
through the next fields of window structures. Such a level can be
either a sequence of horizontally adjacent windows from left to
right, or a sequence of vertically adjacent windows from top to
bottom. Each window in a horizontal sequence can be either a leaf
window or a vertical sequence; a window in a vertical sequence can
be either a leaf or a horizontal sequence. All windows in a
horizontal sequence have the same height, and all windows in a
vertical sequence have the same width.
This function uses, for historical reasons, a more general
algorithm to determine glyph matrix dimensions that would be
necessary.
The matrix height of a horizontal sequence is determined by the
maximum height of any matrix in the sequence. The matrix width of
a horizontal sequence is computed by adding up matrix widths of
windows in the sequence.
|<------- result width ------->|
+---------+----------+---------+ ---
| | | | |
| | | |
+---------+ | | result height
| +---------+
| | |
+----------+ ---
The matrix width of a vertical sequence is the maximum matrix width
of any window in the sequence. Its height is computed by adding up
matrix heights of windows in the sequence.
|<---- result width -->|
+---------+ ---
| | |
| | |
+---------+--+ |
| | |
| | result height
| |
+------------+---------+ |
| | |
| | |
+------------+---------+ --- */
/* Bit indicating that a new matrix will be allocated or has been
allocated. */
#define NEW_LEAF_MATRIX (1 << 0)
/* Bit indicating that a matrix will or has changed its location or
size. */
#define CHANGED_LEAF_MATRIX (1 << 1)
static struct dim
allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
bool dim_only_p, int *window_change_flags)
{
struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
int x0 = x, y0 = y;
int wmax = 0, hmax = 0;
struct dim total;
struct dim dim;
struct window *w;
bool in_horz_combination_p;
/* What combination is WINDOW part of? Compute this once since the
result is the same for all windows in the `next' chain. The
special case of a root window (parent equal to nil) is treated
like a vertical combination because a root window's `next'
points to the mini-buffer window, if any, which is arranged
vertically below other windows. */
in_horz_combination_p
= (!NILP (XWINDOW (window)->parent)
&& WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (XWINDOW (window)->parent)));
/* For WINDOW and all windows on the same level. */
do
{
w = XWINDOW (window);
/* Get the dimension of the window sub-matrix for W, depending
on whether this is a combination or a leaf window. */
if (WINDOWP (w->contents))
dim = allocate_matrices_for_frame_redisplay (w->contents, x, y,
dim_only_p,
window_change_flags);
else
{
/* If not already done, allocate sub-matrix structures. */
if (w->desired_matrix == NULL)
{
w->desired_matrix = new_glyph_matrix (f->desired_pool);
w->current_matrix = new_glyph_matrix (f->current_pool);
*window_change_flags |= NEW_LEAF_MATRIX;
}
/* Width and height MUST be chosen so that there are no
holes in the frame matrix. */
dim.width = required_matrix_width (w);
dim.height = required_matrix_height (w);
/* Will matrix be re-allocated? */
if (x != w->desired_matrix->matrix_x
|| y != w->desired_matrix->matrix_y
|| dim.width != w->desired_matrix->matrix_w
|| dim.height != w->desired_matrix->matrix_h
|| (margin_glyphs_to_reserve (w, dim.width,
w->left_margin_cols)
!= w->desired_matrix->left_margin_glyphs)
|| (margin_glyphs_to_reserve (w, dim.width,
w->right_margin_cols)
!= w->desired_matrix->right_margin_glyphs))
*window_change_flags |= CHANGED_LEAF_MATRIX;
/* Actually change matrices, if allowed. Do not consider
CHANGED_LEAF_MATRIX computed above here because the pool
may have been changed which we don't know here. We trust
that we only will be called with DIM_ONLY_P when
necessary. */
if (!dim_only_p)
{
adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
}
}
/* If we are part of a horizontal combination, advance x for
windows to the right of W; otherwise advance y for windows
below W. */
if (in_horz_combination_p)
x += dim.width;
else
y += dim.height;
/* Remember maximum glyph matrix dimensions. */
wmax = max (wmax, dim.width);
hmax = max (hmax, dim.height);
/* Next window on same level. */
window = w->next;
}
while (!NILP (window));
/* Set `total' to the total glyph matrix dimension of this window
level. In a vertical combination, the width is the width of the
widest window; the height is the y we finally reached, corrected
by the y we started with. In a horizontal combination, the total
height is the height of the tallest window, and the width is the
x we finally reached, corrected by the x we started with. */
if (in_horz_combination_p)
{
total.width = x - x0;
total.height = hmax;
}
else
{
total.width = wmax;
total.height = y - y0;
}
return total;
}
/* Return the required height of glyph matrices for window W. */
static int
required_matrix_height (struct window *w)
{
#ifdef HAVE_WINDOW_SYSTEM
struct frame *f = XFRAME (w->frame);
if (FRAME_WINDOW_P (f))
{
/* https://lists.gnu.org/r/emacs-devel/2015-11/msg00194.html */
int ch_height = max (FRAME_SMALLEST_FONT_HEIGHT (f), 1);
int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
return (((window_pixel_height + ch_height - 1)
/ ch_height) * w->nrows_scale_factor
/* One partially visible line at the top and
bottom of the window. */
+ 2
/* 2 for header and mode line. */
+ 2);
}
#endif /* HAVE_WINDOW_SYSTEM */
return WINDOW_TOTAL_LINES (w);
}
/* Return the required width of glyph matrices for window W. */
static int
required_matrix_width (struct window *w)
{
#ifdef HAVE_WINDOW_SYSTEM
struct frame *f = XFRAME (w->frame);
if (FRAME_WINDOW_P (f))
{
/* https://lists.gnu.org/r/emacs-devel/2015-11/msg00194.html */
int ch_width = max (FRAME_SMALLEST_CHAR_WIDTH (f), 1);
/* Compute number of glyphs needed in a glyph row. */
return (((WINDOW_PIXEL_WIDTH (w) + ch_width - 1)
/ ch_width) * w->ncols_scale_factor
/* 2 partially visible columns in the text area. */
+ 2
/* One partially visible column at the right
edge of each marginal area. */
+ 1 + 1);
}
#endif /* HAVE_WINDOW_SYSTEM */
return w->total_cols;
}
/* Allocate window matrices for window-based redisplay. W is the
window whose matrices must be allocated/reallocated. */
static void
allocate_matrices_for_window_redisplay (struct window *w)
{
while (w)
{
if (WINDOWP (w->contents))
allocate_matrices_for_window_redisplay (XWINDOW (w->contents));
else
{
/* W is a leaf window. */
struct dim dim;
/* If matrices are not yet allocated, allocate them now. */
if (w->desired_matrix == NULL)
{
w->desired_matrix = new_glyph_matrix (NULL);
w->current_matrix = new_glyph_matrix (NULL);
}
dim.width = required_matrix_width (w);
dim.height = required_matrix_height (w);
adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
}
w = NILP (w->next) ? NULL : XWINDOW (w->next);
}
}
/* Allocate/reallocate glyph matrices of a single frame F.
This function must be called when a new frame is created,
its size changes, or its window configuration changes. */
void
adjust_frame_glyphs (struct frame *f)
{
/* Block input so that expose events and other events that access
glyph matrices are not processed while we are changing them. */
block_input ();
if (FRAME_WINDOW_P (f))
adjust_frame_glyphs_for_window_redisplay (f);
else
adjust_frame_glyphs_for_frame_redisplay (f);
/* Don't forget the buffer for decode_mode_spec. */
adjust_decode_mode_spec_buffer (f);
f->glyphs_initialized_p = 1;
unblock_input ();
}
/* Return true if any window in the tree has nonzero window margins. See
the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
static bool
showing_window_margins_p (struct window *w)
{
while (w)
{
if (WINDOWP (w->contents))
{
if (showing_window_margins_p (XWINDOW (w->contents)))
return 1;
}
else if (w->left_margin_cols > 0 || w->right_margin_cols > 0)
return 1;
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
return 0;
}
/* In the window tree with root W, build current matrices of leaf
windows from the frame's current matrix. */
static void
fake_current_matrices (Lisp_Object window)
{
struct window *w;
for (; !NILP (window); window = w->next)
{
w = XWINDOW (window);
if (WINDOWP (w->contents))
fake_current_matrices (w->contents);
else
{
int i;
struct frame *f = XFRAME (w->frame);
struct glyph_matrix *m = w->current_matrix;
struct glyph_matrix *fm = f->current_matrix;
eassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
eassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
for (i = 0; i < m->matrix_h; ++i)
{
struct glyph_row *r = m->rows + i;
struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
eassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
&& r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
r->enabled_p = fr->enabled_p;
if (r->enabled_p)
{
r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
r->used[TEXT_AREA] = (m->matrix_w
- r->used[LEFT_MARGIN_AREA]
- r->used[RIGHT_MARGIN_AREA]);
r->mode_line_p = 0;
}
}
}
}
}
/* Save away the contents of frame F's current frame matrix. Value is
a glyph matrix holding the contents of F's current frame matrix. */
static struct glyph_matrix *
save_current_matrix (struct frame *f)
{
int i;
struct glyph_matrix *saved = xzalloc (sizeof *saved);
saved->nrows = f->current_matrix->nrows;
saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
for (i = 0; i < saved->nrows; ++i)
{
struct glyph_row *from = f->current_matrix->rows + i;
struct glyph_row *to = saved->rows + i;
ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
to->glyphs[TEXT_AREA] = xmalloc (nbytes);
memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
to->used[TEXT_AREA] = from->used[TEXT_AREA];
to->enabled_p = from->enabled_p;
to->hash = from->hash;
if (from->used[LEFT_MARGIN_AREA])
{
nbytes = from->used[LEFT_MARGIN_AREA] * sizeof (struct glyph);
to->glyphs[LEFT_MARGIN_AREA] = xmalloc (nbytes);
memcpy (to->glyphs[LEFT_MARGIN_AREA],
from->glyphs[LEFT_MARGIN_AREA], nbytes);
to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
}
if (from->used[RIGHT_MARGIN_AREA])
{
nbytes = from->used[RIGHT_MARGIN_AREA] * sizeof (struct glyph);
to->glyphs[RIGHT_MARGIN_AREA] = xmalloc (nbytes);
memcpy (to->glyphs[RIGHT_MARGIN_AREA],
from->glyphs[RIGHT_MARGIN_AREA], nbytes);
to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
}
}
return saved;
}
/* Restore the contents of frame F's current frame matrix from SAVED,
and free memory associated with SAVED. */
static void
restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
{
int i;
for (i = 0; i < saved->nrows; ++i)
{
struct glyph_row *from = saved->rows + i;
struct glyph_row *to = f->current_matrix->rows + i;
ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
to->used[TEXT_AREA] = from->used[TEXT_AREA];
xfree (from->glyphs[TEXT_AREA]);
nbytes = from->used[LEFT_MARGIN_AREA] * sizeof (struct glyph);
if (nbytes)
{
memcpy (to->glyphs[LEFT_MARGIN_AREA],
from->glyphs[LEFT_MARGIN_AREA], nbytes);
to->used[LEFT_MARGIN_AREA] = from->used[LEFT_MARGIN_AREA];
xfree (from->glyphs[LEFT_MARGIN_AREA]);
}
else
to->used[LEFT_MARGIN_AREA] = 0;
nbytes = from->used[RIGHT_MARGIN_AREA] * sizeof (struct glyph);
if (nbytes)
{
memcpy (to->glyphs[RIGHT_MARGIN_AREA],
from->glyphs[RIGHT_MARGIN_AREA], nbytes);
to->used[RIGHT_MARGIN_AREA] = from->used[RIGHT_MARGIN_AREA];
xfree (from->glyphs[RIGHT_MARGIN_AREA]);
}
else
to->used[RIGHT_MARGIN_AREA] = 0;
}
xfree (saved->rows);
xfree (saved);
}
/* Allocate/reallocate glyph matrices of a single frame F for
frame-based redisplay. */
static void
adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
{
struct dim matrix_dim;
bool pool_changed_p;
int window_change_flags;
int top_window_y;
if (!FRAME_LIVE_P (f))
return;
top_window_y = FRAME_TOP_MARGIN (f);
/* Allocate glyph pool structures if not already done. */
if (f->desired_pool == NULL)
{
f->desired_pool = new_glyph_pool ();
f->current_pool = new_glyph_pool ();
}
/* Allocate frames matrix structures if needed. */
if (f->desired_matrix == NULL)
{
f->desired_matrix = new_glyph_matrix (f->desired_pool);
f->current_matrix = new_glyph_matrix (f->current_pool);
}
/* Compute window glyph matrices. (This takes the mini-buffer
window into account). The result is the size of the frame glyph
matrix needed. The variable window_change_flags is set to a bit
mask indicating whether new matrices will be allocated or
existing matrices change their size or location within the frame
matrix. */
window_change_flags = 0;
matrix_dim
= allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
0, top_window_y,
1,
&window_change_flags);
/* Add in menu bar lines, if any. */
matrix_dim.height += top_window_y;
/* Enlarge pools as necessary. */
pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
realloc_glyph_pool (f->current_pool, matrix_dim);
/* Set up glyph pointers within window matrices. Do this only if
absolutely necessary since it requires a frame redraw. */
if (pool_changed_p || window_change_flags)
{
/* Do it for window matrices. */
allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
0, top_window_y, 0,
&window_change_flags);
/* Size of frame matrices must equal size of frame. Note
that we are called for X frames with window widths NOT equal
to the frame width (from CHANGE_FRAME_SIZE_1). */
if (matrix_dim.width != FRAME_TOTAL_COLS (f)
|| matrix_dim.height != FRAME_TOTAL_LINES (f))
return;
eassert (matrix_dim.width == FRAME_TOTAL_COLS (f)
&& matrix_dim.height == FRAME_TOTAL_LINES (f));
/* Pointers to glyph memory in glyph rows are exchanged during
the update phase of redisplay, which means in general that a
frame's current matrix consists of pointers into both the
desired and current glyph pool of the frame. Adjusting a
matrix sets the frame matrix up so that pointers are all into
the same pool. If we want to preserve glyph contents of the
current matrix over a call to adjust_glyph_matrix, we must
make a copy of the current glyphs, and restore the current
matrix' contents from that copy. */
if (display_completed
&& !FRAME_GARBAGED_P (f)
&& matrix_dim.width == f->current_matrix->matrix_w
&& matrix_dim.height == f->current_matrix->matrix_h
/* For some reason, the frame glyph matrix gets corrupted if
any of the windows contain margins. I haven't been able
to hunt down the reason, but for the moment this prevents
the problem from manifesting. -- cyd */
&& !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
{
struct glyph_matrix *copy = save_current_matrix (f);
adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
restore_current_matrix (f, copy);
fake_current_matrices (FRAME_ROOT_WINDOW (f));
}
else
{
adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
SET_FRAME_GARBAGED (f);
}
}
}
/* Allocate/reallocate glyph matrices of a single frame F for
window-based redisplay. */
static void
adjust_frame_glyphs_for_window_redisplay (struct frame *f)
{
eassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
/* Allocate/reallocate window matrices. */
allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
/* Allocate/ reallocate matrices of the dummy window used to display
the menu bar under X when no X toolkit support is available. */
{
/* Allocate a dummy window if not already done. */
struct window *w;
if (NILP (f->menu_bar_window))
{
Lisp_Object frame;
fset_menu_bar_window (f, make_window ());
w = XWINDOW (f->menu_bar_window);
XSETFRAME (frame, f);
wset_frame (w, frame);
w->pseudo_window_p = 1;
}
else
w = XWINDOW (f->menu_bar_window);
/* Set window dimensions to frame dimensions and allocate or
adjust glyph matrices of W. */
w->pixel_left = 0;
w->left_col = 0;
w->pixel_top = 0;
w->top_line = 0;
w->pixel_width = (FRAME_PIXEL_WIDTH (f)
- 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
w->total_cols = FRAME_TOTAL_COLS (f);
w->pixel_height = FRAME_MENU_BAR_HEIGHT (f);
w->total_lines = FRAME_MENU_BAR_LINES (f);
allocate_matrices_for_window_redisplay (w);
}
#endif
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
{
/* Allocate/ reallocate matrices of the tool bar window. If we
don't have a tool bar window yet, make one. */
struct window *w;
if (NILP (f->tool_bar_window))
{
Lisp_Object frame;
fset_tool_bar_window (f, make_window ());
w = XWINDOW (f->tool_bar_window);
XSETFRAME (frame, f);
wset_frame (w, frame);
w->pseudo_window_p = 1;
}
else
w = XWINDOW (f->tool_bar_window);
w->pixel_left = 0;
w->left_col = 0;
w->pixel_top = FRAME_MENU_BAR_HEIGHT (f);
w->top_line = FRAME_MENU_BAR_LINES (f);
w->total_cols = FRAME_TOTAL_COLS (f);
w->pixel_width = (FRAME_PIXEL_WIDTH (f)
- 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
w->total_lines = FRAME_TOOL_BAR_LINES (f);
w->pixel_height = FRAME_TOOL_BAR_HEIGHT (f);
allocate_matrices_for_window_redisplay (w);
}
#endif
}
/* Re-allocate buffer for decode_mode_spec on frame F. */
static void
adjust_decode_mode_spec_buffer (struct frame *f)
{
int frame_message_buf_size = FRAME_MESSAGE_BUF_SIZE (f);
eassert (frame_message_buf_size >= 0);
f->decode_mode_spec_buffer = xrealloc (f->decode_mode_spec_buffer,
frame_message_buf_size + 1);
}
/**********************************************************************
Freeing Glyph Matrices
**********************************************************************/
/* Free glyph memory for a frame F. F may be null. This function can
be called for the same frame more than once. The root window of
F may be nil when this function is called. This is the case when
the function is called when F is destroyed. */
void
free_glyphs (struct frame *f)
{
if (f && f->glyphs_initialized_p)
{
/* Block interrupt input so that we don't get surprised by an X
event while we're in an inconsistent state. */
block_input ();
f->glyphs_initialized_p = 0;
/* Release window sub-matrices. */
if (!NILP (f->root_window))
free_window_matrices (XWINDOW (f->root_window));
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
/* Free the dummy window for menu bars without X toolkit and its
glyph matrices. */
if (!NILP (f->menu_bar_window))
{
struct window *w = XWINDOW (f->menu_bar_window);
free_glyph_matrix (w->desired_matrix);
free_glyph_matrix (w->current_matrix);
w->desired_matrix = w->current_matrix = NULL;
fset_menu_bar_window (f, Qnil);
}
#endif
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
/* Free the tool bar window and its glyph matrices. */
if (!NILP (f->tool_bar_window))
{
struct window *w = XWINDOW (f->tool_bar_window);
free_glyph_matrix (w->desired_matrix);
free_glyph_matrix (w->current_matrix);
w->desired_matrix = w->current_matrix = NULL;
fset_tool_bar_window (f, Qnil);
}
#endif
/* Release frame glyph matrices. Reset fields to zero in
case we are called a second time. */
if (f->desired_matrix)
{
free_glyph_matrix (f->desired_matrix);
free_glyph_matrix (f->current_matrix);
f->desired_matrix = f->current_matrix = NULL;
}
/* Release glyph pools. */
if (f->desired_pool)
{
free_glyph_pool (f->desired_pool);
free_glyph_pool (f->current_pool);
f->desired_pool = f->current_pool = NULL;
}
unblock_input ();
}
}
/* Free glyph sub-matrices in the window tree rooted at W. This
function may be called with a null pointer, and it may be called on
the same tree more than once. */
void
free_window_matrices (struct window *w)
{
while (w)
{
if (WINDOWP (w->contents))
free_window_matrices (XWINDOW (w->contents));
else
{
/* This is a leaf window. Free its memory and reset fields
to zero in case this function is called a second time for
W. */
free_glyph_matrix (w->current_matrix);
free_glyph_matrix (w->desired_matrix);
w->current_matrix = w->desired_matrix = NULL;
}
/* Next window on same level. */
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
}
/* Check glyph memory leaks. This function is called from
shut_down_emacs. Note that frames are not destroyed when Emacs
exits. We therefore free all glyph memory for all active frames
explicitly and check that nothing is left allocated. */
void
check_glyph_memory (void)
{
Lisp_Object tail, frame;
/* Free glyph memory for all frames. */
FOR_EACH_FRAME (tail, frame)
free_glyphs (XFRAME (frame));
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* Check that nothing is left allocated. */
eassert (glyph_matrix_count == 0);
eassert (glyph_pool_count == 0);
#endif
}
/**********************************************************************
Building a Frame Matrix
**********************************************************************/
/* Most of the redisplay code works on glyph matrices attached to
windows. This is a good solution most of the time, but it is not
suitable for terminal code. Terminal output functions cannot rely
on being able to set an arbitrary terminal window. Instead they
must be provided with a view of the whole frame, i.e. the whole
screen. We build such a view by constructing a frame matrix from
window matrices in this section.
Windows that must be updated have their must_be_updated_p flag set.
For all such windows, their desired matrix is made part of the
desired frame matrix. For other windows, their current matrix is
made part of the desired frame matrix.
+-----------------+----------------+
| desired | desired |
| | |
+-----------------+----------------+
| current |
| |
+----------------------------------+
Desired window matrices can be made part of the frame matrix in a
cheap way: We exploit the fact that the desired frame matrix and
desired window matrices share their glyph memory. This is not
possible for current window matrices. Their glyphs are copied to
the desired frame matrix. The latter is equivalent to
preserve_other_columns in the old redisplay.
Used glyphs counters for frame matrix rows are the result of adding
up glyph lengths of the window matrices. A line in the frame
matrix is enabled, if a corresponding line in a window matrix is
enabled.
After building the desired frame matrix, it will be passed to
terminal code, which will manipulate both the desired and current
frame matrix. Changes applied to the frame's current matrix have
to be visible in current window matrices afterwards, of course.
This problem is solved like this:
1. Window and frame matrices share glyphs. Window matrices are
constructed in a way that their glyph contents ARE the glyph
contents needed in a frame matrix. Thus, any modification of
glyphs done in terminal code will be reflected in window matrices
automatically.
2. Exchanges of rows in a frame matrix done by terminal code are
intercepted by hook functions so that corresponding row operations
on window matrices can be performed. This is necessary because we
use pointers to glyphs in glyph row structures. To satisfy the
assumption of point 1 above that glyphs are updated implicitly in
window matrices when they are manipulated via the frame matrix,
window and frame matrix must of course agree where to find the
glyphs for their rows. Possible manipulations that must be
mirrored are assignments of rows of the desired frame matrix to the
current frame matrix and scrolling the current frame matrix. */
/* Build frame F's desired matrix from window matrices. Only windows
which have the flag must_be_updated_p set have to be updated. Menu
bar lines of a frame are not covered by window matrices, so make
sure not to touch them in this function. */
static void
build_frame_matrix (struct frame *f)
{
int i;
/* F must have a frame matrix when this function is called. */
eassert (!FRAME_WINDOW_P (f));
/* Clear all rows in the frame matrix covered by window matrices.
Menu bar lines are not covered by windows. */
for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
/* Build the matrix by walking the window tree. */
build_frame_matrix_from_window_tree (f->desired_matrix,
XWINDOW (FRAME_ROOT_WINDOW (f)));
}
/* Walk a window tree, building a frame matrix MATRIX from window
matrices. W is the root of a window tree. */
static void
build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
{
while (w)
{
if (WINDOWP (w->contents))
build_frame_matrix_from_window_tree (matrix, XWINDOW (w->contents));
else
build_frame_matrix_from_leaf_window (matrix, w);
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
}
/* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
desired frame matrix built. W is a leaf window whose desired or
current matrix is to be added to FRAME_MATRIX. W's flag
must_be_updated_p determines which matrix it contributes to
FRAME_MATRIX. If W->must_be_updated_p, W's desired matrix
is added to FRAME_MATRIX, otherwise W's current matrix is added.
Adding a desired matrix means setting up used counters and such in
frame rows, while adding a current window matrix to FRAME_MATRIX
means copying glyphs. The latter case corresponds to
preserve_other_columns in the old redisplay. */
static void
build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
{
struct glyph_matrix *window_matrix;
int window_y, frame_y;
/* If non-zero, a glyph to insert at the right border of W. */
GLYPH right_border_glyph;
SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
/* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
if (w->must_be_updated_p)
{
window_matrix = w->desired_matrix;
/* Decide whether we want to add a vertical border glyph. */
if (!WINDOW_RIGHTMOST_P (w))
{
struct Lisp_Char_Table *dp = window_display_table (w);
Lisp_Object gc;
SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
if (dp
&& (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc)))
{
SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
spec_glyph_lookup_face (w, &right_border_glyph);
}
if (GLYPH_FACE (right_border_glyph) <= 0)
SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
}
}
else
window_matrix = w->current_matrix;
/* For all rows in the window matrix and corresponding rows in the
frame matrix. */
window_y = 0;
frame_y = window_matrix->matrix_y;
while (window_y < window_matrix->nrows)
{
struct glyph_row *frame_row = frame_matrix->rows + frame_y;
struct glyph_row *window_row = window_matrix->rows + window_y;
bool current_row_p = window_matrix == w->current_matrix;
/* Fill up the frame row with spaces up to the left margin of the
window row. */
fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
/* Fill up areas in the window matrix row with spaces. */
fill_up_glyph_row_with_spaces (window_row);
/* If only part of W's desired matrix has been built, and
window_row wasn't displayed, use the corresponding current
row instead. */
if (window_matrix == w->desired_matrix
&& !window_row->enabled_p)
{
window_row = w->current_matrix->rows + window_y;
current_row_p = 1;
}
if (current_row_p)
{
/* Copy window row to frame row. */
memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
window_row->glyphs[0],
window_matrix->matrix_w * sizeof (struct glyph));
}
else
{
eassert (window_row->enabled_p);
/* Only when a desired row has been displayed, we want
the corresponding frame row to be updated. */
frame_row->enabled_p = true;
/* Maybe insert a vertical border between horizontally adjacent
windows. */
if (GLYPH_CHAR (right_border_glyph) != 0)
{
struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
}
#ifdef GLYPH_DEBUG
/* Window row window_y must be a slice of frame row
frame_y. */
eassert (glyph_row_slice_p (window_row, frame_row));
/* If rows are in sync, we don't have to copy glyphs because
frame and window share glyphs. */
strcpy (w->current_matrix->method, w->desired_matrix->method);
add_window_display_history (w, w->current_matrix->method, 0);
#endif
}
/* Set number of used glyphs in the frame matrix. Since we fill
up with spaces, and visit leaf windows from left to right it
can be done simply. */
frame_row->used[TEXT_AREA]
= window_matrix->matrix_x + window_matrix->matrix_w;
/* Next row. */
++window_y;
++frame_y;
}
}
/* Given a user-specified glyph, possibly including a Lisp-level face
ID, return a glyph that has a realized face ID.
This is used for glyphs displayed specially and not part of the text;
for instance, vertical separators, truncation markers, etc. */
void
spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
{
int lface_id = GLYPH_FACE (*glyph);
/* Convert the glyph's specified face to a realized (cache) face. */
if (lface_id > 0)
{
int face_id = merge_faces (XFRAME (w->frame),
Qt, lface_id, DEFAULT_FACE_ID);
SET_GLYPH_FACE (*glyph, face_id);
}
}
/* Add spaces to a glyph row ROW in a window matrix.
Each row has the form:
+---------+-----------------------------+------------+
| left | text | right |
+---------+-----------------------------+------------+
Left and right marginal areas are optional. This function adds
spaces to areas so that there are no empty holes between areas.
In other words: If the right area is not empty, the text area
is filled up with spaces up to the right area. If the text area
is not empty, the left area is filled up.
To be called for frame-based redisplay, only. */
static void
fill_up_glyph_row_with_spaces (struct glyph_row *row)
{
fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
}
/* Fill area AREA of glyph row ROW with spaces. To be called for
frame-based redisplay only. */
static void
fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
{
if (row->glyphs[area] < row->glyphs[area + 1])
{
struct glyph *end = row->glyphs[area + 1];
struct glyph *text = row->glyphs[area] + row->used[area];
while (text < end)
*text++ = space_glyph;
row->used[area] = text - row->glyphs[area];
}
}
/* Add spaces to the end of ROW in a frame matrix until index UPTO is
reached. In frame matrices only one area, TEXT_AREA, is used. */
void
fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
{
int i = row->used[TEXT_AREA];
struct glyph *glyph = row->glyphs[TEXT_AREA];
while (i < upto)
glyph[i++] = space_glyph;
row->used[TEXT_AREA] = i;
}
/**********************************************************************
Mirroring operations on frame matrices in window matrices
**********************************************************************/
/* Set frame being updated via frame-based redisplay to F. This
function must be called before updates to make explicit that we are
working on frame matrices or not. */
static void
set_frame_matrix_frame (struct frame *f)
{
frame_matrix_frame = f;
}
/* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
DESIRED_MATRIX is the desired matrix corresponding to
CURRENT_MATRIX. The update is done by exchanging glyph pointers
between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
frame_matrix_frame is non-null, this indicates that the exchange is
done in frame matrices, and that we have to perform analogous
operations in window matrices of frame_matrix_frame. */
static void
make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
{
struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
bool mouse_face_p = current_row->mouse_face_p;
/* Do current_row = desired_row. This exchanges glyph pointers
between both rows, and does a structure assignment otherwise. */
assign_row (current_row, desired_row);
/* Enable current_row to mark it as valid. */
current_row->enabled_p = true;
current_row->mouse_face_p = mouse_face_p;
/* If we are called on frame matrices, perform analogous operations
for window matrices. */
if (frame_matrix_frame)
mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
}
/* W is the root of a window tree. FRAME_ROW is the index of a row in
W's frame which has been made current (by swapping pointers between
current and desired matrix). Perform analogous operations in the
matrices of leaf windows in the window tree rooted at W. */
static void
mirror_make_current (struct window *w, int frame_row)
{
while (w)
{
if (WINDOWP (w->contents))
mirror_make_current (XWINDOW (w->contents), frame_row);
else
{
/* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
here because the checks performed in debug mode there
will not allow the conversion. */
int row = frame_row - w->desired_matrix->matrix_y;
/* If FRAME_ROW is within W, assign the desired row to the
current row (exchanging glyph pointers). */
if (row >= 0 && row < w->desired_matrix->matrix_h)
{
struct glyph_row *current_row
= MATRIX_ROW (w->current_matrix, row);
struct glyph_row *desired_row
= MATRIX_ROW (w->desired_matrix, row);
if (desired_row->enabled_p)
assign_row (current_row, desired_row);
else
swap_glyph_pointers (desired_row, current_row);
current_row->enabled_p = true;
/* Set the Y coordinate of the mode/header line's row.
It is needed in draw_row_with_mouse_face to find the
screen coordinates. (Window-based redisplay sets
this in update_window, but no one seems to do that
for frame-based redisplay.) */
if (current_row->mode_line_p)
current_row->y = row;
}
}
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
}
/* Perform row dance after scrolling. We are working on the range of
lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
including) in MATRIX. COPY_FROM is a vector containing, for each
row I in the range 0 <= I < NLINES, the index of the original line
to move to I. This index is relative to the row range, i.e. 0 <=
index < NLINES. RETAINED_P is a vector containing zero for each
row 0 <= I < NLINES which is empty.
This function is called from do_scrolling and do_direct_scrolling. */
void
mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
int *copy_from, char *retained_p)
{
/* A copy of original rows. */
struct glyph_row *old_rows;
/* Rows to assign to. */
struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
int i;
/* Make a copy of the original rows. */
USE_SAFE_ALLOCA;
SAFE_NALLOCA (old_rows, 1, nlines);
memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
/* Assign new rows, maybe clear lines. */
for (i = 0; i < nlines; ++i)
{
bool enabled_before_p = new_rows[i].enabled_p;
eassert (i + unchanged_at_top < matrix->nrows);
eassert (unchanged_at_top + copy_from[i] < matrix->nrows);
new_rows[i] = old_rows[copy_from[i]];
new_rows[i].enabled_p = enabled_before_p;
/* RETAINED_P is zero for empty lines. */
if (!retained_p[copy_from[i]])
new_rows[i].enabled_p = false;
}
/* Do the same for window matrices, if MATRIX is a frame matrix. */
if (frame_matrix_frame)
mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
unchanged_at_top, nlines, copy_from, retained_p);
SAFE_FREE ();
}
/* Synchronize glyph pointers in the current matrix of window W with
the current frame matrix. */
static void
sync_window_with_frame_matrix_rows (struct window *w)
{
struct frame *f = XFRAME (w->frame);
struct glyph_row *window_row, *window_row_end, *frame_row;
int left, right, x, width;
/* Preconditions: W must be a live window on a tty frame. */
eassert (BUFFERP (w->contents));
eassert (!FRAME_WINDOW_P (f));
left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
x = w->current_matrix->matrix_x;
width = w->current_matrix->matrix_w;
window_row = w->current_matrix->rows;
window_row_end = window_row + w->current_matrix->nrows;
frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
for (; window_row < window_row_end; ++window_row, ++frame_row)
{
window_row->glyphs[LEFT_MARGIN_AREA]
= frame_row->glyphs[0] + x;
window_row->glyphs[TEXT_AREA]
= window_row->glyphs[LEFT_MARGIN_AREA] + left;
window_row->glyphs[LAST_AREA]
= window_row->glyphs[LEFT_MARGIN_AREA] + width;
window_row->glyphs[RIGHT_MARGIN_AREA]
= window_row->glyphs[LAST_AREA] - right;
}
}
/* Return the window in the window tree rooted in W containing frame
row ROW. Value is null if none is found. */
static struct window *
frame_row_to_window (struct window *w, int row)
{
struct window *found = NULL;
while (w && !found)
{
if (WINDOWP (w->contents))
found = frame_row_to_window (XWINDOW (w->contents), row);
else if (row >= WINDOW_TOP_EDGE_LINE (w)
&& row < WINDOW_BOTTOM_EDGE_LINE (w))
found = w;
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
return found;
}
/* Perform a line dance in the window tree rooted at W, after
scrolling a frame matrix in mirrored_line_dance.
We are working on the range of lines UNCHANGED_AT_TOP + 1 to
UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
COPY_FROM is a vector containing, for each row I in the range 0 <=
I < NLINES, the index of the original line to move to I. This
index is relative to the row range, i.e. 0 <= index < NLINES.
RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
which is empty. */
static void
mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
{
while (w)
{
if (WINDOWP (w->contents))
mirror_line_dance (XWINDOW (w->contents), unchanged_at_top,
nlines, copy_from, retained_p);
else
{
/* W is a leaf window, and we are working on its current
matrix m. */
struct glyph_matrix *m = w->current_matrix;
int i;
bool sync_p = 0;
struct glyph_row *old_rows;
/* Make a copy of the original rows of matrix m. */
USE_SAFE_ALLOCA;
SAFE_NALLOCA (old_rows, 1, m->nrows);
memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
for (i = 0; i < nlines; ++i)
{
/* Frame relative line assigned to. */
int frame_to = i + unchanged_at_top;
/* Frame relative line assigned. */
int frame_from = copy_from[i] + unchanged_at_top;
/* Window relative line assigned to. */
int window_to = frame_to - m->matrix_y;
/* Window relative line assigned. */
int window_from = frame_from - m->matrix_y;
/* Is assigned line inside window? */
bool from_inside_window_p
= window_from >= 0 && window_from < m->matrix_h;
/* Is assigned to line inside window? */
bool to_inside_window_p
= window_to >= 0 && window_to < m->matrix_h;
if (from_inside_window_p && to_inside_window_p)
{
/* Do the assignment. The enabled_p flag is saved
over the assignment because the old redisplay did
that. */
bool enabled_before_p = m->rows[window_to].enabled_p;
m->rows[window_to] = old_rows[window_from];
m->rows[window_to].enabled_p = enabled_before_p;
/* If frame line is empty, window line is empty, too. */
if (!retained_p[copy_from[i]])
m->rows[window_to].enabled_p = false;
}
else if (to_inside_window_p)
{
/* A copy between windows. This is an infrequent
case not worth optimizing. */
struct frame *f = XFRAME (w->frame);
struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
struct window *w2;
struct glyph_matrix *m2;
int m2_from;
w2 = frame_row_to_window (root, frame_from);
/* ttn@surf.glug.org: when enabling menu bar using `emacs
-nw', FROM_FRAME sometimes has no associated window.
This check avoids a segfault if W2 is null. */
if (w2)
{
m2 = w2->current_matrix;
m2_from = frame_from - m2->matrix_y;
copy_row_except_pointers (m->rows + window_to,
m2->rows + m2_from);
/* If frame line is empty, window line is empty, too. */
if (!retained_p[copy_from[i]])
m->rows[window_to].enabled_p = false;
}
sync_p = 1;
}
else if (from_inside_window_p)
sync_p = 1;
}
/* If there was a copy between windows, make sure glyph
pointers are in sync with the frame matrix. */
if (sync_p)
sync_window_with_frame_matrix_rows (w);
/* Check that no pointers are lost. */
CHECK_MATRIX (m);
SAFE_FREE ();
}
/* Next window on same level. */
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
}
#ifdef GLYPH_DEBUG
/* Check that window and frame matrices agree about their
understanding where glyphs of the rows are to find. For each
window in the window tree rooted at W, check that rows in the
matrices of leaf window agree with their frame matrices about
glyph pointers. */
static void
check_window_matrix_pointers (struct window *w)
{
while (w)
{
if (WINDOWP (w->contents))
check_window_matrix_pointers (XWINDOW (w->contents));
else
{
struct frame *f = XFRAME (w->frame);
check_matrix_pointers (w->desired_matrix, f->desired_matrix);
check_matrix_pointers (w->current_matrix, f->current_matrix);
}
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
}
/* Check that window rows are slices of frame rows. WINDOW_MATRIX is
a window and FRAME_MATRIX is the corresponding frame matrix. For
each row in WINDOW_MATRIX check that it's a slice of the
corresponding frame row. If it isn't, abort. */
static void
check_matrix_pointers (struct glyph_matrix *window_matrix,
struct glyph_matrix *frame_matrix)
{
/* Row number in WINDOW_MATRIX. */
int i = 0;
/* Row number corresponding to I in FRAME_MATRIX. */
int j = window_matrix->matrix_y;
/* For all rows check that the row in the window matrix is a
slice of the row in the frame matrix. If it isn't we didn't
mirror an operation on the frame matrix correctly. */
while (i < window_matrix->nrows)
{
if (!glyph_row_slice_p (window_matrix->rows + i,
frame_matrix->rows + j))
emacs_abort ();
++i, ++j;
}
}
#endif /* GLYPH_DEBUG */
/**********************************************************************
VPOS and HPOS translations
**********************************************************************/
#ifdef GLYPH_DEBUG
/* Translate vertical position VPOS which is relative to window W to a
vertical position relative to W's frame. */
static int
window_to_frame_vpos (struct window *w, int vpos)
{
eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
eassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
vpos += WINDOW_TOP_EDGE_LINE (w);
eassert (vpos >= 0 && vpos <= FRAME_TOTAL_LINES (XFRAME (w->frame)));
return vpos;
}
/* Translate horizontal position HPOS which is relative to window W to
a horizontal position relative to W's frame. */
static int
window_to_frame_hpos (struct window *w, int hpos)
{
eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
hpos += WINDOW_LEFT_EDGE_COL (w);
return hpos;
}
#endif /* GLYPH_DEBUG */
/**********************************************************************
Redrawing Frames
**********************************************************************/
/* Redraw frame F. */
void
redraw_frame (struct frame *f)
{
/* Error if F has no glyphs. */
eassert (f->glyphs_initialized_p);
update_begin (f);
if (FRAME_MSDOS_P (f))
FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
clear_frame (f);
clear_current_matrices (f);
update_end (f);
fset_redisplay (f);
/* Mark all windows as inaccurate, so that every window will have
its redisplay done. */
mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), true);
f->garbaged = false;
}
DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 0, 1, 0,
doc: /* Clear frame FRAME and output again what is supposed to appear on it.
If FRAME is omitted or nil, the selected frame is used. */)
(Lisp_Object frame)
{
redraw_frame (decode_live_frame (frame));
return Qnil;
}
DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
doc: /* Clear and redisplay all visible frames. */)
(void)
{
Lisp_Object tail, frame;
FOR_EACH_FRAME (tail, frame)
if (FRAME_VISIBLE_P (XFRAME (frame)))
redraw_frame (XFRAME (frame));
return Qnil;
}
/***********************************************************************
Frame Update
***********************************************************************/
/* Update frame F based on the data in desired matrices.
If FORCE_P, don't let redisplay be stopped by detecting pending input.
If INHIBIT_HAIRY_ID_P, don't try scrolling.
Value is true if redisplay was stopped due to pending input. */
bool
update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
{
/* True means display has been paused because of pending input. */
bool paused_p;
struct window *root_window = XWINDOW (f->root_window);
if (redisplay_dont_pause)
force_p = true;
else if (!force_p && detect_input_pending_ignore_squeezables ())
{
paused_p = true;
goto do_pause;
}
if (FRAME_WINDOW_P (f))
{
/* We are working on window matrix basis. All windows whose
flag must_be_updated_p is set have to be updated. */
/* Record that we are not working on frame matrices. */
set_frame_matrix_frame (NULL);
/* Update all windows in the window tree of F, maybe stopping
when pending input is detected. */
update_begin (f);
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
/* Update the menu bar on X frames that don't have toolkit
support. */
if (WINDOWP (f->menu_bar_window))
update_window (XWINDOW (f->menu_bar_window), true);
#endif
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
/* Update the tool-bar window, if present. */
if (WINDOWP (f->tool_bar_window))
{
struct window *w = XWINDOW (f->tool_bar_window);
/* Update tool-bar window. */
if (w->must_be_updated_p)
{
Lisp_Object tem;
update_window (w, true);
w->must_be_updated_p = false;
/* Swap tool-bar strings. We swap because we want to
reuse strings. */
tem = f->current_tool_bar_string;
fset_current_tool_bar_string (f, f->desired_tool_bar_string);
fset_desired_tool_bar_string (f, tem);
}
}
#endif
/* Update windows. */
paused_p = update_window_tree (root_window, force_p);
update_end (f);
}
else
{
/* We are working on frame matrix basis. Set the frame on whose
frame matrix we operate. */
set_frame_matrix_frame (f);
/* Build F's desired matrix from window matrices. */
build_frame_matrix (f);
/* Update the display. */
update_begin (f);
paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p, 1, false);
update_end (f);
if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
{
if (FRAME_TTY (f)->termscript)
fflush_unlocked (FRAME_TTY (f)->termscript);
if (FRAME_TERMCAP_P (f))
fflush_unlocked (FRAME_TTY (f)->output);
}
/* Check window matrices for lost pointers. */
#ifdef GLYPH_DEBUG
check_window_matrix_pointers (root_window);
add_frame_display_history (f, paused_p);
#endif
}
do_pause:
/* Reset flags indicating that a window should be updated. */
set_window_update_flags (root_window, false);
display_completed = !paused_p;
return paused_p;
}
/* Update a TTY frame F that has a menu dropped down over some of its
glyphs. This is like the second part of update_frame, but it
doesn't call build_frame_matrix, because we already have the
desired matrix prepared, and don't want it to be overwritten by the
text of the normal display.
ROW and COL, if non-negative, are the row and column of the TTY
frame where to position the cursor after the frame update is
complete. Negative values mean ask update_frame_1 to position the
cursor "normally", i.e. at point in the selected window. */
void
update_frame_with_menu (struct frame *f, int row, int col)
{
struct window *root_window = XWINDOW (f->root_window);
bool paused_p, cursor_at_point_p;
eassert (FRAME_TERMCAP_P (f));
/* We are working on frame matrix basis. Set the frame on whose
frame matrix we operate. */
set_frame_matrix_frame (f);