merge from trunk
[emacs.git] / src / dispnew.c
blobcd7be59c122095fa360711ed918a1200b3f7d85b
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/>. */
20 #include <config.h>
22 #define DISPEXTERN_INLINE EXTERN_INLINE
24 #include "sysstdio.h"
25 #include <unistd.h>
27 #include "lisp.h"
28 #include "termchar.h"
29 /* cm.h must come after dispextern.h on Windows. */
30 #include "dispextern.h"
31 #include "cm.h"
32 #include "character.h"
33 #include "buffer.h"
34 #include "keyboard.h"
35 #include "frame.h"
36 #include "termhooks.h"
37 #include "window.h"
38 #include "commands.h"
39 #include "disptab.h"
40 #include "indent.h"
41 #include "intervals.h"
42 #include "blockinput.h"
43 #include "process.h"
45 #include "syssignal.h"
47 #ifdef HAVE_WINDOW_SYSTEM
48 #include TERM_HEADER
49 #endif /* HAVE_WINDOW_SYSTEM */
51 #ifdef HAVE_XWIDGETS
52 #include "xwidget.h"
53 #endif
55 #include <errno.h>
57 #include <fpending.h>
58 #include <timespec.h>
60 #if defined (HAVE_TERM_H) && defined (GNU_LINUX)
61 #include <term.h> /* for tgetent */
62 #endif
64 #ifdef WINDOWSNT
65 #include "w32.h"
66 #endif
68 /* Structure to pass dimensions around. Used for character bounding
69 boxes, glyph matrix dimensions and alike. */
71 struct dim
73 int width;
74 int height;
78 /* Function prototypes. */
80 static void update_frame_line (struct frame *, int);
81 static int required_matrix_height (struct window *);
82 static int required_matrix_width (struct window *);
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 /* A glyph for a space. */
140 struct glyph space_glyph;
142 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
144 /* Counts of allocated structures. These counts serve to diagnose
145 memory leaks and double frees. */
147 static int glyph_matrix_count;
148 static int glyph_pool_count;
150 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
152 /* If non-null, the frame whose frame matrices are manipulated. If
153 null, window matrices are worked on. */
155 static struct frame *frame_matrix_frame;
157 /* Convert vpos and hpos from frame to window and vice versa.
158 This may only be used for terminal frames. */
160 #ifdef GLYPH_DEBUG
162 static int window_to_frame_vpos (struct window *, int);
163 static int window_to_frame_hpos (struct window *, int);
164 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
165 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
167 /* One element of the ring buffer containing redisplay history
168 information. */
170 struct redisplay_history
172 char trace[512 + 100];
175 /* The size of the history buffer. */
177 #define REDISPLAY_HISTORY_SIZE 30
179 /* The redisplay history buffer. */
181 static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
183 /* Next free entry in redisplay_history. */
185 static int history_idx;
187 /* A tick that's incremented each time something is added to the
188 history. */
190 static uprintmax_t history_tick;
192 /* Add to the redisplay history how window W has been displayed.
193 MSG is a trace containing the information how W's glyph matrix
194 has been constructed. PAUSED_P means that the update
195 has been interrupted for pending input. */
197 static void
198 add_window_display_history (struct window *w, const char *msg, bool paused_p)
200 char *buf;
201 void *ptr = w;
203 if (history_idx >= REDISPLAY_HISTORY_SIZE)
204 history_idx = 0;
205 buf = redisplay_history[history_idx].trace;
206 ++history_idx;
208 snprintf (buf, sizeof redisplay_history[0].trace,
209 "%"pMu": window %p (`%s')%s\n%s",
210 history_tick++,
211 ptr,
212 ((BUFFERP (w->contents)
213 && STRINGP (BVAR (XBUFFER (w->contents), name)))
214 ? SSDATA (BVAR (XBUFFER (w->contents), name))
215 : "???"),
216 paused_p ? " ***paused***" : "",
217 msg);
221 /* Add to the redisplay history that frame F has been displayed.
222 PAUSED_P means that the update has been interrupted for
223 pending input. */
225 static void add_frame_display_history (struct frame *f, int paused_p)
227 char *buf;
228 void *ptr = f;
230 if (history_idx >= REDISPLAY_HISTORY_SIZE)
231 history_idx = 0;
232 buf = redisplay_history[history_idx].trace;
233 ++history_idx;
235 sprintf (buf, "%"pMu": update frame %p%s",
236 history_tick++,
237 ptr, paused_p ? " ***paused***" : "");
241 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
242 Sdump_redisplay_history, 0, 0, "",
243 doc: /* Dump redisplay history to stderr. */)
244 (void)
246 int i;
248 for (i = history_idx - 1; i != history_idx; --i)
250 if (i < 0)
251 i = REDISPLAY_HISTORY_SIZE - 1;
252 fprintf (stderr, "%s\n", redisplay_history[i].trace);
255 return Qnil;
259 #else /* not GLYPH_DEBUG */
261 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
262 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
264 #endif /* GLYPH_DEBUG */
267 #if (defined PROFILING \
268 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
269 && !HAVE___EXECUTABLE_START)
270 /* This function comes first in the Emacs executable and is used only
271 to estimate the text start for profiling. */
272 void
273 __executable_start (void)
275 emacs_abort ();
277 #endif
279 /***********************************************************************
280 Glyph Matrices
281 ***********************************************************************/
283 /* Allocate and return a glyph_matrix structure. POOL is the glyph
284 pool from which memory for the matrix should be allocated, or null
285 for window-based redisplay where no glyph pools are used. The
286 member `pool' of the glyph matrix structure returned is set to
287 POOL, the structure is otherwise zeroed. */
289 static struct glyph_matrix *
290 new_glyph_matrix (struct glyph_pool *pool)
292 struct glyph_matrix *result = xzalloc (sizeof *result);
294 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
295 /* Increment number of allocated matrices. This count is used
296 to detect memory leaks. */
297 ++glyph_matrix_count;
298 #endif
300 /* Set pool and return. */
301 result->pool = pool;
302 return result;
306 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
308 If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
309 glyph_matrix_count is decremented when a matrix is freed. If the count
310 gets negative, more structures were freed than allocated, i.e. one matrix
311 was freed more than once or a bogus pointer was passed to this function.
313 If MATRIX->pool is null, this means that the matrix manages its own
314 glyph memory---this is done for matrices on X frames. Freeing the
315 matrix also frees the glyph memory in this case. */
317 static void
318 free_glyph_matrix (struct glyph_matrix *matrix)
320 if (matrix)
322 int i;
324 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
325 /* Detect the case that more matrices are freed than were
326 allocated. */
327 --glyph_matrix_count;
328 eassert (glyph_matrix_count >= 0);
329 #endif
331 /* Free glyph memory if MATRIX owns it. */
332 if (matrix->pool == NULL)
333 for (i = 0; i < matrix->rows_allocated; ++i)
334 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
336 /* Free row structures and the matrix itself. */
337 xfree (matrix->rows);
338 xfree (matrix);
343 /* Return the number of glyphs to reserve for a marginal area of
344 window W. TOTAL_GLYPHS is the number of glyphs in a complete
345 display line of window W. MARGIN gives the width of the marginal
346 area in canonical character units. */
348 static int
349 margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
351 if (margin > 0)
353 int width = w->total_cols;
354 double d = max (0, margin);
355 d = min (width / 2 - 1, d);
356 return (int) ((double) total_glyphs / width * d);
358 return 0;
361 /* Return true if ROW's hash value is correct.
362 Optimized away if ENABLE_CHECKING is not defined. */
364 static bool
365 verify_row_hash (struct glyph_row *row)
367 return row->hash == row_hash (row);
370 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
371 window sizes.
373 W is null if the function is called for a frame glyph matrix.
374 Otherwise it is the window MATRIX is a member of. X and Y are the
375 indices of the first column and row of MATRIX within the frame
376 matrix, if such a matrix exists. They are zero for purely
377 window-based redisplay. DIM is the needed size of the matrix.
379 In window-based redisplay, where no frame matrices exist, glyph
380 matrices manage their own glyph storage. Otherwise, they allocate
381 storage from a common frame glyph pool which can be found in
382 MATRIX->pool.
384 The reason for this memory management strategy is to avoid complete
385 frame redraws if possible. When we allocate from a common pool, a
386 change of the location or size of a sub-matrix within the pool
387 requires a complete redisplay of the frame because we cannot easily
388 make sure that the current matrices of all windows still agree with
389 what is displayed on the screen. While this is usually fast, it
390 leads to screen flickering. */
392 static void
393 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
395 int i;
396 int new_rows;
397 bool marginal_areas_changed_p = 0;
398 bool header_line_changed_p = 0;
399 bool header_line_p = 0;
400 int left = -1, right = -1;
401 int window_width = -1, window_height = -1;
403 /* See if W had a header line that has disappeared now, or vice versa.
404 Get W's size. */
405 if (w)
407 window_box (w, ANY_AREA, 0, 0, &window_width, &window_height);
409 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
410 header_line_changed_p = header_line_p != matrix->header_line_p;
412 matrix->header_line_p = header_line_p;
414 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
415 Do nothing if MATRIX' size, position, vscroll, and marginal areas
416 haven't changed. This optimization is important because preserving
417 the matrix means preventing redisplay. */
418 if (matrix->pool == NULL)
420 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
421 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
422 eassert (left >= 0 && right >= 0);
423 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
424 || right != matrix->right_margin_glyphs);
426 if (!marginal_areas_changed_p
427 && !XFRAME (w->frame)->fonts_changed
428 && !header_line_changed_p
429 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
430 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
431 && matrix->window_height == window_height
432 && matrix->window_vscroll == w->vscroll
433 && matrix->window_width == window_width)
434 return;
437 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
438 if (matrix->rows_allocated < dim.height)
440 int old_alloc = matrix->rows_allocated;
441 new_rows = dim.height - matrix->rows_allocated;
442 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
443 new_rows, INT_MAX, sizeof *matrix->rows);
444 memset (matrix->rows + old_alloc, 0,
445 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
447 else
448 new_rows = 0;
450 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
451 on a frame not using window-based redisplay. Set up pointers for
452 each row into the glyph pool. */
453 if (matrix->pool)
455 eassert (matrix->pool->glyphs);
457 if (w)
459 left = margin_glyphs_to_reserve (w, dim.width,
460 w->left_margin_cols);
461 right = margin_glyphs_to_reserve (w, dim.width,
462 w->right_margin_cols);
464 else
465 left = right = 0;
467 for (i = 0; i < dim.height; ++i)
469 struct glyph_row *row = &matrix->rows[i];
471 row->glyphs[LEFT_MARGIN_AREA]
472 = (matrix->pool->glyphs
473 + (y + i) * matrix->pool->ncolumns
474 + x);
476 if (w == NULL
477 || row == matrix->rows + dim.height - 1
478 || (row == matrix->rows && matrix->header_line_p))
480 row->glyphs[TEXT_AREA]
481 = row->glyphs[LEFT_MARGIN_AREA];
482 row->glyphs[RIGHT_MARGIN_AREA]
483 = row->glyphs[TEXT_AREA] + dim.width;
484 row->glyphs[LAST_AREA]
485 = row->glyphs[RIGHT_MARGIN_AREA];
487 else
489 row->glyphs[TEXT_AREA]
490 = row->glyphs[LEFT_MARGIN_AREA] + left;
491 row->glyphs[RIGHT_MARGIN_AREA]
492 = row->glyphs[TEXT_AREA] + dim.width - left - right;
493 row->glyphs[LAST_AREA]
494 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
498 matrix->left_margin_glyphs = left;
499 matrix->right_margin_glyphs = right;
501 else
503 /* If MATRIX->pool is null, MATRIX is responsible for managing
504 its own memory. It is a window matrix for window-based redisplay.
505 Allocate glyph memory from the heap. */
506 if (dim.width > matrix->matrix_w
507 || new_rows
508 || header_line_changed_p
509 || marginal_areas_changed_p)
511 struct glyph_row *row = matrix->rows;
512 struct glyph_row *end = row + matrix->rows_allocated;
514 while (row < end)
516 row->glyphs[LEFT_MARGIN_AREA]
517 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
518 dim.width, sizeof (struct glyph));
520 /* The mode line never has marginal areas. */
521 if (row == matrix->rows + dim.height - 1
522 || (row == matrix->rows && matrix->header_line_p))
524 row->glyphs[TEXT_AREA]
525 = row->glyphs[LEFT_MARGIN_AREA];
526 row->glyphs[RIGHT_MARGIN_AREA]
527 = row->glyphs[TEXT_AREA] + dim.width;
528 row->glyphs[LAST_AREA]
529 = row->glyphs[RIGHT_MARGIN_AREA];
531 else
533 row->glyphs[TEXT_AREA]
534 = row->glyphs[LEFT_MARGIN_AREA] + left;
535 row->glyphs[RIGHT_MARGIN_AREA]
536 = row->glyphs[TEXT_AREA] + dim.width - left - right;
537 row->glyphs[LAST_AREA]
538 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
540 ++row;
544 eassert (left >= 0 && right >= 0);
545 matrix->left_margin_glyphs = left;
546 matrix->right_margin_glyphs = right;
549 /* Number of rows to be used by MATRIX. */
550 matrix->nrows = dim.height;
551 eassert (matrix->nrows >= 0);
553 if (w)
555 if (matrix == w->current_matrix)
557 /* Mark rows in a current matrix of a window as not having
558 valid contents. It's important to not do this for
559 desired matrices. When Emacs starts, it may already be
560 building desired matrices when this function runs. */
561 if (window_width < 0)
562 window_width = window_box_width (w, -1);
564 /* Optimize the case that only the height has changed (C-x 2,
565 upper window). Invalidate all rows that are no longer part
566 of the window. */
567 if (!marginal_areas_changed_p
568 && !header_line_changed_p
569 && new_rows == 0
570 && dim.width == matrix->matrix_w
571 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
572 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
573 && matrix->window_width == window_width)
575 /* Find the last row in the window. */
576 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
577 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
579 ++i;
580 break;
583 /* Window end is invalid, if inside of the rows that
584 are invalidated below. */
585 if (w->window_end_vpos >= i)
586 w->window_end_valid = 0;
588 while (i < matrix->nrows)
589 matrix->rows[i++].enabled_p = 0;
591 else
593 for (i = 0; i < matrix->nrows; ++i)
594 matrix->rows[i].enabled_p = 0;
597 else if (matrix == w->desired_matrix)
599 /* Rows in desired matrices always have to be cleared;
600 redisplay expects this is the case when it runs, so it
601 had better be the case when we adjust matrices between
602 redisplays. */
603 for (i = 0; i < matrix->nrows; ++i)
604 matrix->rows[i].enabled_p = 0;
609 /* Remember last values to be able to optimize frame redraws. */
610 matrix->matrix_x = x;
611 matrix->matrix_y = y;
612 matrix->matrix_w = dim.width;
613 matrix->matrix_h = dim.height;
615 /* Record the top y location and height of W at the time the matrix
616 was last adjusted. This is used to optimize redisplay above. */
617 if (w)
619 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
620 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
621 matrix->window_height = window_height;
622 matrix->window_width = window_width;
623 matrix->window_vscroll = w->vscroll;
628 /* Reverse the contents of rows in MATRIX between START and END. The
629 contents of the row at END - 1 end up at START, END - 2 at START +
630 1 etc. This is part of the implementation of rotate_matrix (see
631 below). */
633 static void
634 reverse_rows (struct glyph_matrix *matrix, int start, int end)
636 int i, j;
638 for (i = start, j = end - 1; i < j; ++i, --j)
640 /* Non-ISO HP/UX compiler doesn't like auto struct
641 initialization. */
642 struct glyph_row temp;
643 temp = matrix->rows[i];
644 matrix->rows[i] = matrix->rows[j];
645 matrix->rows[j] = temp;
650 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
651 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
652 indices. (Note: this does not copy glyphs, only glyph pointers in
653 row structures are moved around).
655 The algorithm used for rotating the vector was, I believe, first
656 described by Kernighan. See the vector R as consisting of two
657 sub-vectors AB, where A has length BY for BY >= 0. The result
658 after rotating is then BA. Reverse both sub-vectors to get ArBr
659 and reverse the result to get (ArBr)r which is BA. Similar for
660 rotating right. */
662 void
663 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
665 if (by < 0)
667 /* Up (rotate left, i.e. towards lower indices). */
668 by = -by;
669 reverse_rows (matrix, first, first + by);
670 reverse_rows (matrix, first + by, last);
671 reverse_rows (matrix, first, last);
673 else if (by > 0)
675 /* Down (rotate right, i.e. towards higher indices). */
676 reverse_rows (matrix, last - by, last);
677 reverse_rows (matrix, first, last - by);
678 reverse_rows (matrix, first, last);
683 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
684 with indices START <= index < END. Increment positions by DELTA/
685 DELTA_BYTES. */
687 void
688 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
689 ptrdiff_t delta, ptrdiff_t delta_bytes)
691 /* Check that START and END are reasonable values. */
692 eassert (start >= 0 && start <= matrix->nrows);
693 eassert (end >= 0 && end <= matrix->nrows);
694 eassert (start <= end);
696 for (; start < end; ++start)
697 increment_row_positions (matrix->rows + start, delta, delta_bytes);
701 /* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
702 START and END are the row indices of the first and last + 1 row to clear. */
704 void
705 clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
707 eassert (start <= end);
708 eassert (start >= 0 && start < matrix->nrows);
709 eassert (end >= 0 && end <= matrix->nrows);
711 for (; start < end; ++start)
712 matrix->rows[start].enabled_p = 0;
716 /* Clear MATRIX.
718 Empty all rows in MATRIX by clearing their enabled_p flags.
719 The function prepare_desired_row will eventually really clear a row
720 when it sees one with a false enabled_p flag.
722 Reset update hints to default values. The only update hint
723 currently present is the flag MATRIX->no_scrolling_p. */
725 void
726 clear_glyph_matrix (struct glyph_matrix *matrix)
728 if (matrix)
730 clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
731 matrix->no_scrolling_p = 0;
736 /* Shift part of the glyph matrix MATRIX of window W up or down.
737 Increment y-positions in glyph rows between START and END by DY,
738 and recompute their visible height. */
740 void
741 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
743 int min_y, max_y;
745 eassert (start <= end);
746 eassert (start >= 0 && start < matrix->nrows);
747 eassert (end >= 0 && end <= matrix->nrows);
749 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
750 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
752 for (; start < end; ++start)
754 struct glyph_row *row = &matrix->rows[start];
756 row->y += dy;
757 row->visible_height = row->height;
759 if (row->y < min_y)
760 row->visible_height -= min_y - row->y;
761 if (row->y + row->height > max_y)
762 row->visible_height -= row->y + row->height - max_y;
763 if (row->fringe_bitmap_periodic_p)
764 row->redraw_fringe_bitmaps_p = 1;
769 /* Mark all rows in current matrices of frame F as invalid. Marking
770 invalid is done by setting enabled_p to zero for all rows in a
771 current matrix. */
773 void
774 clear_current_matrices (register struct frame *f)
776 /* Clear frame current matrix, if we have one. */
777 if (f->current_matrix)
778 clear_glyph_matrix (f->current_matrix);
780 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
781 /* Clear the matrix of the menu bar window, if such a window exists.
782 The menu bar window is currently used to display menus on X when
783 no toolkit support is compiled in. */
784 if (WINDOWP (f->menu_bar_window))
785 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
786 #endif
788 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
789 /* Clear the matrix of the tool-bar window, if any. */
790 if (WINDOWP (f->tool_bar_window))
791 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
792 #endif
794 /* Clear current window matrices. */
795 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
796 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
800 /* Clear out all display lines of F for a coming redisplay. */
802 void
803 clear_desired_matrices (register struct frame *f)
805 if (f->desired_matrix)
806 clear_glyph_matrix (f->desired_matrix);
808 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
809 if (WINDOWP (f->menu_bar_window))
810 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
811 #endif
813 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
814 if (WINDOWP (f->tool_bar_window))
815 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
816 #endif
818 /* Do it for window matrices. */
819 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
820 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
824 /* Clear matrices in window tree rooted in W. If DESIRED_P,
825 clear desired matrices, otherwise clear current matrices. */
827 static void
828 clear_window_matrices (struct window *w, bool desired_p)
830 while (w)
832 if (WINDOWP (w->contents))
833 clear_window_matrices (XWINDOW (w->contents), desired_p);
834 else
836 if (desired_p)
837 clear_glyph_matrix (w->desired_matrix);
838 else
840 clear_glyph_matrix (w->current_matrix);
841 w->window_end_valid = 0;
845 w = NILP (w->next) ? 0 : XWINDOW (w->next);
851 /***********************************************************************
852 Glyph Rows
854 See dispextern.h for an overall explanation of glyph rows.
855 ***********************************************************************/
857 /* Clear glyph row ROW. Do it in a way that makes it robust against
858 changes in the glyph_row structure, i.e. addition or removal of
859 structure members. */
861 static struct glyph_row null_row;
863 void
864 clear_glyph_row (struct glyph_row *row)
866 struct glyph *p[1 + LAST_AREA];
868 /* Save pointers. */
869 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
870 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
871 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
872 p[LAST_AREA] = row->glyphs[LAST_AREA];
874 /* Clear. */
875 *row = null_row;
877 /* Restore pointers. */
878 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
879 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
880 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
881 row->glyphs[LAST_AREA] = p[LAST_AREA];
883 #if 0 /* At some point, some bit-fields of struct glyph were not set,
884 which made glyphs unequal when compared with GLYPH_EQUAL_P.
885 Redisplay outputs such glyphs, and flickering effects were
886 the result. This also depended on the contents of memory
887 returned by xmalloc. If flickering happens again, activate
888 the code below. If the flickering is gone with that, chances
889 are that the flickering has the same reason as here. */
890 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
891 #endif
895 /* Make ROW an empty, enabled row of canonical character height,
896 in window W starting at y-position Y. */
898 void
899 blank_row (struct window *w, struct glyph_row *row, int y)
901 int min_y, max_y;
903 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
904 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
906 clear_glyph_row (row);
907 row->y = y;
908 row->ascent = row->phys_ascent = 0;
909 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
910 row->visible_height = row->height;
912 if (row->y < min_y)
913 row->visible_height -= min_y - row->y;
914 if (row->y + row->height > max_y)
915 row->visible_height -= row->y + row->height - max_y;
917 row->enabled_p = 1;
921 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
922 are the amounts by which to change positions. Note that the first
923 glyph of the text area of a row can have a buffer position even if
924 the used count of the text area is zero. Such rows display line
925 ends. */
927 static void
928 increment_row_positions (struct glyph_row *row,
929 ptrdiff_t delta, ptrdiff_t delta_bytes)
931 int area, i;
933 /* Increment start and end positions. */
934 MATRIX_ROW_START_CHARPOS (row) += delta;
935 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
936 MATRIX_ROW_END_CHARPOS (row) += delta;
937 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
938 CHARPOS (row->start.pos) += delta;
939 BYTEPOS (row->start.pos) += delta_bytes;
940 CHARPOS (row->end.pos) += delta;
941 BYTEPOS (row->end.pos) += delta_bytes;
943 if (!row->enabled_p)
944 return;
946 /* Increment positions in glyphs. */
947 for (area = 0; area < LAST_AREA; ++area)
948 for (i = 0; i < row->used[area]; ++i)
949 if (BUFFERP (row->glyphs[area][i].object)
950 && row->glyphs[area][i].charpos > 0)
951 row->glyphs[area][i].charpos += delta;
953 /* Capture the case of rows displaying a line end. */
954 if (row->used[TEXT_AREA] == 0
955 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
956 row->glyphs[TEXT_AREA]->charpos += delta;
960 #if 0
961 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
962 contents, i.e. glyph structure contents are exchanged between A and
963 B without changing glyph pointers in A and B. */
965 static void
966 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
968 int area;
970 for (area = 0; area < LAST_AREA; ++area)
972 /* Number of glyphs to swap. */
973 int max_used = max (a->used[area], b->used[area]);
975 /* Start of glyphs in area of row A. */
976 struct glyph *glyph_a = a->glyphs[area];
978 /* End + 1 of glyphs in area of row A. */
979 struct glyph *glyph_a_end = a->glyphs[max_used];
981 /* Start of glyphs in area of row B. */
982 struct glyph *glyph_b = b->glyphs[area];
984 while (glyph_a < glyph_a_end)
986 /* Non-ISO HP/UX compiler doesn't like auto struct
987 initialization. */
988 struct glyph temp;
989 temp = *glyph_a;
990 *glyph_a = *glyph_b;
991 *glyph_b = temp;
992 ++glyph_a;
993 ++glyph_b;
998 #endif /* 0 */
1000 /* Exchange pointers to glyph memory between glyph rows A and B. Also
1001 exchange the used[] array and the hash values of the rows, because
1002 these should all go together for the row's hash value to be
1003 correct. */
1005 static void
1006 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1008 int i;
1009 unsigned hash_tem = a->hash;
1011 for (i = 0; i < LAST_AREA + 1; ++i)
1013 struct glyph *temp = a->glyphs[i];
1015 a->glyphs[i] = b->glyphs[i];
1016 b->glyphs[i] = temp;
1017 if (i < LAST_AREA)
1019 short used_tem = a->used[i];
1021 a->used[i] = b->used[i];
1022 b->used[i] = used_tem;
1025 a->hash = b->hash;
1026 b->hash = hash_tem;
1030 /* Copy glyph row structure FROM to glyph row structure TO, except
1031 that glyph pointers, the `used' counts, and the hash values in the
1032 structures are left unchanged. */
1034 static void
1035 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1037 struct glyph *pointers[1 + LAST_AREA];
1038 short used[LAST_AREA];
1039 unsigned hashval;
1041 /* Save glyph pointers of TO. */
1042 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1043 memcpy (used, to->used, sizeof to->used);
1044 hashval = to->hash;
1046 /* Do a structure assignment. */
1047 *to = *from;
1049 /* Restore original pointers of TO. */
1050 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1051 memcpy (to->used, used, sizeof to->used);
1052 to->hash = hashval;
1056 /* Assign glyph row FROM to glyph row TO. This works like a structure
1057 assignment TO = FROM, except that glyph pointers are not copied but
1058 exchanged between TO and FROM. Pointers must be exchanged to avoid
1059 a memory leak. */
1061 static void
1062 assign_row (struct glyph_row *to, struct glyph_row *from)
1064 swap_glyph_pointers (to, from);
1065 copy_row_except_pointers (to, from);
1069 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1070 a row in a window matrix, is a slice of the glyph memory of the
1071 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1072 is true if the glyph memory of WINDOW_ROW is part of the glyph
1073 memory of FRAME_ROW. */
1075 #ifdef GLYPH_DEBUG
1077 static bool
1078 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1080 struct glyph *window_glyph_start = window_row->glyphs[0];
1081 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1082 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1084 return (frame_glyph_start <= window_glyph_start
1085 && window_glyph_start < frame_glyph_end);
1088 #endif /* GLYPH_DEBUG */
1090 #if 0
1092 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1093 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1094 in WINDOW_MATRIX is found satisfying the condition. */
1096 static struct glyph_row *
1097 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1098 struct glyph_matrix *frame_matrix, int row)
1100 int i;
1102 eassert (row >= 0 && row < frame_matrix->nrows);
1104 for (i = 0; i < window_matrix->nrows; ++i)
1105 if (glyph_row_slice_p (window_matrix->rows + i,
1106 frame_matrix->rows + row))
1107 break;
1109 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1112 #endif /* 0 */
1114 /* Prepare ROW for display. Desired rows are cleared lazily,
1115 i.e. they are only marked as to be cleared by setting their
1116 enabled_p flag to zero. When a row is to be displayed, a prior
1117 call to this function really clears it. */
1119 void
1120 prepare_desired_row (struct glyph_row *row)
1122 if (!row->enabled_p)
1124 bool rp = row->reversed_p;
1126 clear_glyph_row (row);
1127 row->enabled_p = 1;
1128 row->reversed_p = rp;
1133 /* Return a hash code for glyph row ROW. */
1135 static int
1136 line_hash_code (struct glyph_row *row)
1138 int hash = 0;
1140 if (row->enabled_p)
1142 struct glyph *glyph = row->glyphs[TEXT_AREA];
1143 struct glyph *end = glyph + row->used[TEXT_AREA];
1145 while (glyph < end)
1147 int c = glyph->u.ch;
1148 int face_id = glyph->face_id;
1149 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1150 c -= SPACEGLYPH;
1151 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1152 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1153 ++glyph;
1156 if (hash == 0)
1157 hash = 1;
1160 return hash;
1164 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1165 the number of characters in the line. If must_write_spaces is
1166 zero, leading and trailing spaces are ignored. */
1168 static int
1169 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1171 struct glyph_row *row = matrix->rows + vpos;
1172 struct glyph *beg = row->glyphs[TEXT_AREA];
1173 struct glyph *end = beg + row->used[TEXT_AREA];
1174 int len;
1175 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1176 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1178 /* Ignore trailing and leading spaces if we can. */
1179 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1181 /* Skip from the end over trailing spaces. */
1182 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1183 --end;
1185 /* All blank line. */
1186 if (end == beg)
1187 return 0;
1189 /* Skip over leading spaces. */
1190 while (CHAR_GLYPH_SPACE_P (*beg))
1191 ++beg;
1194 /* If we don't have a glyph-table, each glyph is one character,
1195 so return the number of glyphs. */
1196 if (glyph_table_base == 0)
1197 len = end - beg;
1198 else
1200 /* Otherwise, scan the glyphs and accumulate their total length
1201 in LEN. */
1202 len = 0;
1203 while (beg < end)
1205 GLYPH g;
1207 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1209 if (GLYPH_INVALID_P (g)
1210 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1211 len += 1;
1212 else
1213 len += GLYPH_LENGTH (glyph_table_base, g);
1215 ++beg;
1219 return len;
1223 /* Return true if the glyph rows A and B have equal contents.
1224 MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too. */
1226 static bool
1227 row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
1229 eassert (verify_row_hash (a));
1230 eassert (verify_row_hash (b));
1232 if (a == b)
1233 return 1;
1234 else if (a->hash != b->hash)
1235 return 0;
1236 else
1238 struct glyph *a_glyph, *b_glyph, *a_end;
1239 int area;
1241 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1242 return 0;
1244 /* Compare glyphs. */
1245 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1247 if (a->used[area] != b->used[area])
1248 return 0;
1250 a_glyph = a->glyphs[area];
1251 a_end = a_glyph + a->used[area];
1252 b_glyph = b->glyphs[area];
1254 while (a_glyph < a_end
1255 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1256 ++a_glyph, ++b_glyph;
1258 if (a_glyph != a_end)
1259 return 0;
1262 if (a->fill_line_p != b->fill_line_p
1263 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1264 || a->left_fringe_bitmap != b->left_fringe_bitmap
1265 || a->left_fringe_face_id != b->left_fringe_face_id
1266 || a->left_fringe_offset != b->left_fringe_offset
1267 || a->right_fringe_bitmap != b->right_fringe_bitmap
1268 || a->right_fringe_face_id != b->right_fringe_face_id
1269 || a->right_fringe_offset != b->right_fringe_offset
1270 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1271 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1272 || a->exact_window_width_line_p != b->exact_window_width_line_p
1273 || a->overlapped_p != b->overlapped_p
1274 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1275 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1276 || a->reversed_p != b->reversed_p
1277 /* Different partially visible characters on left margin. */
1278 || a->x != b->x
1279 /* Different height. */
1280 || a->ascent != b->ascent
1281 || a->phys_ascent != b->phys_ascent
1282 || a->phys_height != b->phys_height
1283 || a->visible_height != b->visible_height)
1284 return 0;
1287 return 1;
1292 /***********************************************************************
1293 Glyph Pool
1295 See dispextern.h for an overall explanation of glyph pools.
1296 ***********************************************************************/
1298 /* Allocate a glyph_pool structure. The structure returned is initialized
1299 with zeros. If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global
1300 variable glyph_pool_count is incremented for each pool allocated. */
1302 static struct glyph_pool *
1303 new_glyph_pool (void)
1305 struct glyph_pool *result = xzalloc (sizeof *result);
1307 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1308 /* For memory leak and double deletion checking. */
1309 ++glyph_pool_count;
1310 #endif
1312 return result;
1316 /* Free a glyph_pool structure POOL. The function may be called with
1317 a null POOL pointer. If GLYPH_DEBUG and ENABLE_CHECKING are in effect,
1318 global variable glyph_pool_count is decremented with every pool structure
1319 freed. If this count gets negative, more structures were freed than
1320 allocated, i.e. one structure must have been freed more than once or
1321 a bogus pointer was passed to free_glyph_pool. */
1323 static void
1324 free_glyph_pool (struct glyph_pool *pool)
1326 if (pool)
1328 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1329 /* More freed than allocated? */
1330 --glyph_pool_count;
1331 eassert (glyph_pool_count >= 0);
1332 #endif
1333 xfree (pool->glyphs);
1334 xfree (pool);
1339 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1340 columns we need. This function never shrinks a pool. The only
1341 case in which this would make sense, would be when a frame's size
1342 is changed from a large value to a smaller one. But, if someone
1343 does it once, we can expect that he will do it again.
1345 Return true if the pool changed in a way which makes
1346 re-adjusting window glyph matrices necessary. */
1348 static bool
1349 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1351 ptrdiff_t needed;
1352 bool changed_p;
1354 changed_p = (pool->glyphs == 0
1355 || matrix_dim.height != pool->nrows
1356 || matrix_dim.width != pool->ncolumns);
1358 /* Enlarge the glyph pool. */
1359 needed = matrix_dim.width;
1360 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1361 memory_full (SIZE_MAX);
1362 needed *= matrix_dim.height;
1363 if (needed > pool->nglyphs)
1365 ptrdiff_t old_nglyphs = pool->nglyphs;
1366 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1367 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1368 memset (pool->glyphs + old_nglyphs, 0,
1369 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1372 /* Remember the number of rows and columns because (a) we use them
1373 to do sanity checks, and (b) the number of columns determines
1374 where rows in the frame matrix start---this must be available to
1375 determine pointers to rows of window sub-matrices. */
1376 pool->nrows = matrix_dim.height;
1377 pool->ncolumns = matrix_dim.width;
1379 return changed_p;
1384 /***********************************************************************
1385 Debug Code
1386 ***********************************************************************/
1388 #ifdef GLYPH_DEBUG
1391 /* Flush standard output. This is sometimes useful to call from the debugger.
1392 XXX Maybe this should be changed to flush the current terminal instead of
1393 stdout.
1396 void flush_stdout (void) EXTERNALLY_VISIBLE;
1398 void
1399 flush_stdout (void)
1401 fflush (stdout);
1405 /* Check that no glyph pointers have been lost in MATRIX. If a
1406 pointer has been lost, e.g. by using a structure assignment between
1407 rows, at least one pointer must occur more than once in the rows of
1408 MATRIX. */
1410 void
1411 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1413 int i, j;
1415 for (i = 0; i < matrix->nrows; ++i)
1416 for (j = 0; j < matrix->nrows; ++j)
1417 eassert (i == j
1418 || (matrix->rows[i].glyphs[TEXT_AREA]
1419 != matrix->rows[j].glyphs[TEXT_AREA]));
1423 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1425 struct glyph_row *
1426 matrix_row (struct glyph_matrix *matrix, int row)
1428 eassert (matrix && matrix->rows);
1429 eassert (row >= 0 && row < matrix->nrows);
1431 /* That's really too slow for normal testing because this function
1432 is called almost everywhere. Although---it's still astonishingly
1433 fast, so it is valuable to have for debugging purposes. */
1434 #if 0
1435 check_matrix_pointer_lossage (matrix);
1436 #endif
1438 return matrix->rows + row;
1442 #if 0 /* This function makes invalid assumptions when text is
1443 partially invisible. But it might come handy for debugging
1444 nevertheless. */
1446 /* Check invariants that must hold for an up to date current matrix of
1447 window W. */
1449 static void
1450 check_matrix_invariants (struct window *w)
1452 struct glyph_matrix *matrix = w->current_matrix;
1453 int yb = window_text_bottom_y (w);
1454 struct glyph_row *row = matrix->rows;
1455 struct glyph_row *last_text_row = NULL;
1456 struct buffer *saved = current_buffer;
1457 struct buffer *buffer = XBUFFER (w->contents);
1458 int c;
1460 /* This can sometimes happen for a fresh window. */
1461 if (matrix->nrows < 2)
1462 return;
1464 set_buffer_temp (buffer);
1466 /* Note: last row is always reserved for the mode line. */
1467 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1468 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1470 struct glyph_row *next = row + 1;
1472 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1473 last_text_row = row;
1475 /* Check that character and byte positions are in sync. */
1476 eassert (MATRIX_ROW_START_BYTEPOS (row)
1477 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1478 eassert (BYTEPOS (row->start.pos)
1479 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1481 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1482 have such a position temporarily in case of a minibuffer
1483 displaying something like `[Sole completion]' at its end. */
1484 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1486 eassert (MATRIX_ROW_END_BYTEPOS (row)
1487 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1488 eassert (BYTEPOS (row->end.pos)
1489 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1492 /* Check that end position of `row' is equal to start position
1493 of next row. */
1494 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1496 eassert (MATRIX_ROW_END_CHARPOS (row)
1497 == MATRIX_ROW_START_CHARPOS (next));
1498 eassert (MATRIX_ROW_END_BYTEPOS (row)
1499 == MATRIX_ROW_START_BYTEPOS (next));
1500 eassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1501 eassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1503 row = next;
1506 eassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1507 eassert (w->desired_matrix->rows != NULL);
1508 set_buffer_temp (saved);
1511 #endif /* 0 */
1513 #endif /* GLYPH_DEBUG */
1517 /**********************************************************************
1518 Allocating/ Adjusting Glyph Matrices
1519 **********************************************************************/
1521 /* Allocate glyph matrices over a window tree for a frame-based
1522 redisplay
1524 X and Y are column/row within the frame glyph matrix where
1525 sub-matrices for the window tree rooted at WINDOW must be
1526 allocated. DIM_ONLY_P means that the caller of this
1527 function is only interested in the result matrix dimension, and
1528 matrix adjustments should not be performed.
1530 The function returns the total width/height of the sub-matrices of
1531 the window tree. If called on a frame root window, the computation
1532 will take the mini-buffer window into account.
1534 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1536 NEW_LEAF_MATRIX set if any window in the tree did not have a
1537 glyph matrices yet, and
1539 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1540 any window in the tree will be changed or have been changed (see
1541 DIM_ONLY_P)
1543 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1544 function.
1546 Windows are arranged into chains of windows on the same level
1547 through the next fields of window structures. Such a level can be
1548 either a sequence of horizontally adjacent windows from left to
1549 right, or a sequence of vertically adjacent windows from top to
1550 bottom. Each window in a horizontal sequence can be either a leaf
1551 window or a vertical sequence; a window in a vertical sequence can
1552 be either a leaf or a horizontal sequence. All windows in a
1553 horizontal sequence have the same height, and all windows in a
1554 vertical sequence have the same width.
1556 This function uses, for historical reasons, a more general
1557 algorithm to determine glyph matrix dimensions that would be
1558 necessary.
1560 The matrix height of a horizontal sequence is determined by the
1561 maximum height of any matrix in the sequence. The matrix width of
1562 a horizontal sequence is computed by adding up matrix widths of
1563 windows in the sequence.
1565 |<------- result width ------->|
1566 +---------+----------+---------+ ---
1567 | | | | |
1568 | | | |
1569 +---------+ | | result height
1570 | +---------+
1571 | | |
1572 +----------+ ---
1574 The matrix width of a vertical sequence is the maximum matrix width
1575 of any window in the sequence. Its height is computed by adding up
1576 matrix heights of windows in the sequence.
1578 |<---- result width -->|
1579 +---------+ ---
1580 | | |
1581 | | |
1582 +---------+--+ |
1583 | | |
1584 | | result height
1586 +------------+---------+ |
1587 | | |
1588 | | |
1589 +------------+---------+ --- */
1591 /* Bit indicating that a new matrix will be allocated or has been
1592 allocated. */
1594 #define NEW_LEAF_MATRIX (1 << 0)
1596 /* Bit indicating that a matrix will or has changed its location or
1597 size. */
1599 #define CHANGED_LEAF_MATRIX (1 << 1)
1601 static struct dim
1602 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1603 bool dim_only_p, int *window_change_flags)
1605 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1606 int x0 = x, y0 = y;
1607 int wmax = 0, hmax = 0;
1608 struct dim total;
1609 struct dim dim;
1610 struct window *w;
1611 bool in_horz_combination_p;
1613 /* What combination is WINDOW part of? Compute this once since the
1614 result is the same for all windows in the `next' chain. The
1615 special case of a root window (parent equal to nil) is treated
1616 like a vertical combination because a root window's `next'
1617 points to the mini-buffer window, if any, which is arranged
1618 vertically below other windows. */
1619 in_horz_combination_p
1620 = (!NILP (XWINDOW (window)->parent)
1621 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (XWINDOW (window)->parent)));
1623 /* For WINDOW and all windows on the same level. */
1626 w = XWINDOW (window);
1628 /* Get the dimension of the window sub-matrix for W, depending
1629 on whether this is a combination or a leaf window. */
1630 if (WINDOWP (w->contents))
1631 dim = allocate_matrices_for_frame_redisplay (w->contents, x, y,
1632 dim_only_p,
1633 window_change_flags);
1634 else
1636 /* If not already done, allocate sub-matrix structures. */
1637 if (w->desired_matrix == NULL)
1639 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1640 w->current_matrix = new_glyph_matrix (f->current_pool);
1641 *window_change_flags |= NEW_LEAF_MATRIX;
1644 /* Width and height MUST be chosen so that there are no
1645 holes in the frame matrix. */
1646 dim.width = required_matrix_width (w);
1647 dim.height = required_matrix_height (w);
1649 /* Will matrix be re-allocated? */
1650 if (x != w->desired_matrix->matrix_x
1651 || y != w->desired_matrix->matrix_y
1652 || dim.width != w->desired_matrix->matrix_w
1653 || dim.height != w->desired_matrix->matrix_h
1654 || (margin_glyphs_to_reserve (w, dim.width,
1655 w->left_margin_cols)
1656 != w->desired_matrix->left_margin_glyphs)
1657 || (margin_glyphs_to_reserve (w, dim.width,
1658 w->right_margin_cols)
1659 != w->desired_matrix->right_margin_glyphs))
1660 *window_change_flags |= CHANGED_LEAF_MATRIX;
1662 /* Actually change matrices, if allowed. Do not consider
1663 CHANGED_LEAF_MATRIX computed above here because the pool
1664 may have been changed which we don't now here. We trust
1665 that we only will be called with DIM_ONLY_P when
1666 necessary. */
1667 if (!dim_only_p)
1669 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1670 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1674 /* If we are part of a horizontal combination, advance x for
1675 windows to the right of W; otherwise advance y for windows
1676 below W. */
1677 if (in_horz_combination_p)
1678 x += dim.width;
1679 else
1680 y += dim.height;
1682 /* Remember maximum glyph matrix dimensions. */
1683 wmax = max (wmax, dim.width);
1684 hmax = max (hmax, dim.height);
1686 /* Next window on same level. */
1687 window = w->next;
1689 while (!NILP (window));
1691 /* Set `total' to the total glyph matrix dimension of this window
1692 level. In a vertical combination, the width is the width of the
1693 widest window; the height is the y we finally reached, corrected
1694 by the y we started with. In a horizontal combination, the total
1695 height is the height of the tallest window, and the width is the
1696 x we finally reached, corrected by the x we started with. */
1697 if (in_horz_combination_p)
1699 total.width = x - x0;
1700 total.height = hmax;
1702 else
1704 total.width = wmax;
1705 total.height = y - y0;
1708 return total;
1712 /* Return the required height of glyph matrices for window W. */
1714 static int
1715 required_matrix_height (struct window *w)
1717 #ifdef HAVE_WINDOW_SYSTEM
1718 struct frame *f = XFRAME (w->frame);
1720 if (FRAME_WINDOW_P (f))
1722 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1723 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1724 return (((window_pixel_height + ch_height - 1)
1725 / ch_height) * w->nrows_scale_factor
1726 /* One partially visible line at the top and
1727 bottom of the window. */
1729 /* 2 for header and mode line. */
1730 + 2);
1732 #endif /* HAVE_WINDOW_SYSTEM */
1734 return WINDOW_TOTAL_LINES (w);
1738 /* Return the required width of glyph matrices for window W. */
1740 static int
1741 required_matrix_width (struct window *w)
1743 #ifdef HAVE_WINDOW_SYSTEM
1744 struct frame *f = XFRAME (w->frame);
1745 if (FRAME_WINDOW_P (f))
1747 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1748 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1750 /* Compute number of glyphs needed in a glyph row. */
1751 return (((window_pixel_width + ch_width - 1)
1752 / ch_width) * w->ncols_scale_factor
1753 /* 2 partially visible columns in the text area. */
1755 /* One partially visible column at the right
1756 edge of each marginal area. */
1757 + 1 + 1);
1759 #endif /* HAVE_WINDOW_SYSTEM */
1761 return w->total_cols;
1765 /* Allocate window matrices for window-based redisplay. W is the
1766 window whose matrices must be allocated/reallocated. */
1768 static void
1769 allocate_matrices_for_window_redisplay (struct window *w)
1771 while (w)
1773 if (WINDOWP (w->contents))
1774 allocate_matrices_for_window_redisplay (XWINDOW (w->contents));
1775 else
1777 /* W is a leaf window. */
1778 struct dim dim;
1780 /* If matrices are not yet allocated, allocate them now. */
1781 if (w->desired_matrix == NULL)
1783 w->desired_matrix = new_glyph_matrix (NULL);
1784 w->current_matrix = new_glyph_matrix (NULL);
1787 dim.width = required_matrix_width (w);
1788 dim.height = required_matrix_height (w);
1789 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1790 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1793 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1797 /* Allocate/reallocate glyph matrices of a single frame F.
1798 This function must be called when a new frame is created,
1799 its size changes, or its window configuration changes. */
1801 void
1802 adjust_frame_glyphs (struct frame *f)
1804 /* Block input so that expose events and other events that access
1805 glyph matrices are not processed while we are changing them. */
1806 block_input ();
1808 if (FRAME_WINDOW_P (f))
1809 adjust_frame_glyphs_for_window_redisplay (f);
1810 else
1811 adjust_frame_glyphs_for_frame_redisplay (f);
1813 /* Don't forget the buffer for decode_mode_spec. */
1814 adjust_decode_mode_spec_buffer (f);
1816 f->glyphs_initialized_p = 1;
1818 unblock_input ();
1821 /* Return true if any window in the tree has nonzero window margins. See
1822 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1823 static bool
1824 showing_window_margins_p (struct window *w)
1826 while (w)
1828 if (WINDOWP (w->contents))
1830 if (showing_window_margins_p (XWINDOW (w->contents)))
1831 return 1;
1833 else if (w->left_margin_cols > 0 || w->right_margin_cols > 0)
1834 return 1;
1836 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1838 return 0;
1842 /* In the window tree with root W, build current matrices of leaf
1843 windows from the frame's current matrix. */
1845 static void
1846 fake_current_matrices (Lisp_Object window)
1848 struct window *w;
1850 for (; !NILP (window); window = w->next)
1852 w = XWINDOW (window);
1854 if (WINDOWP (w->contents))
1855 fake_current_matrices (w->contents);
1856 else
1858 int i;
1859 struct frame *f = XFRAME (w->frame);
1860 struct glyph_matrix *m = w->current_matrix;
1861 struct glyph_matrix *fm = f->current_matrix;
1863 eassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
1864 eassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
1866 for (i = 0; i < m->matrix_h; ++i)
1868 struct glyph_row *r = m->rows + i;
1869 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
1871 eassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
1872 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
1874 r->enabled_p = fr->enabled_p;
1875 if (r->enabled_p)
1877 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
1878 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
1879 r->used[TEXT_AREA] = (m->matrix_w
1880 - r->used[LEFT_MARGIN_AREA]
1881 - r->used[RIGHT_MARGIN_AREA]);
1882 r->mode_line_p = 0;
1890 /* Save away the contents of frame F's current frame matrix. Value is
1891 a glyph matrix holding the contents of F's current frame matrix. */
1893 static struct glyph_matrix *
1894 save_current_matrix (struct frame *f)
1896 int i;
1897 struct glyph_matrix *saved = xzalloc (sizeof *saved);
1898 saved->nrows = f->current_matrix->nrows;
1899 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
1901 for (i = 0; i < saved->nrows; ++i)
1903 struct glyph_row *from = f->current_matrix->rows + i;
1904 struct glyph_row *to = saved->rows + i;
1905 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1906 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
1907 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1908 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1911 return saved;
1915 /* Restore the contents of frame F's current frame matrix from SAVED,
1916 and free memory associated with SAVED. */
1918 static void
1919 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
1921 int i;
1923 for (i = 0; i < saved->nrows; ++i)
1925 struct glyph_row *from = saved->rows + i;
1926 struct glyph_row *to = f->current_matrix->rows + i;
1927 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1928 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1929 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1930 xfree (from->glyphs[TEXT_AREA]);
1933 xfree (saved->rows);
1934 xfree (saved);
1939 /* Allocate/reallocate glyph matrices of a single frame F for
1940 frame-based redisplay. */
1942 static void
1943 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
1945 struct dim matrix_dim;
1946 bool pool_changed_p;
1947 int window_change_flags;
1948 int top_window_y;
1950 if (!FRAME_LIVE_P (f))
1951 return;
1953 top_window_y = FRAME_TOP_MARGIN (f);
1955 /* Allocate glyph pool structures if not already done. */
1956 if (f->desired_pool == NULL)
1958 f->desired_pool = new_glyph_pool ();
1959 f->current_pool = new_glyph_pool ();
1962 /* Allocate frames matrix structures if needed. */
1963 if (f->desired_matrix == NULL)
1965 f->desired_matrix = new_glyph_matrix (f->desired_pool);
1966 f->current_matrix = new_glyph_matrix (f->current_pool);
1969 /* Compute window glyph matrices. (This takes the mini-buffer
1970 window into account). The result is the size of the frame glyph
1971 matrix needed. The variable window_change_flags is set to a bit
1972 mask indicating whether new matrices will be allocated or
1973 existing matrices change their size or location within the frame
1974 matrix. */
1975 window_change_flags = 0;
1976 matrix_dim
1977 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
1978 0, top_window_y,
1980 &window_change_flags);
1982 /* Add in menu bar lines, if any. */
1983 matrix_dim.height += top_window_y;
1985 /* Enlarge pools as necessary. */
1986 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
1987 realloc_glyph_pool (f->current_pool, matrix_dim);
1989 /* Set up glyph pointers within window matrices. Do this only if
1990 absolutely necessary since it requires a frame redraw. */
1991 if (pool_changed_p || window_change_flags)
1993 /* Do it for window matrices. */
1994 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
1995 0, top_window_y, 0,
1996 &window_change_flags);
1998 /* Size of frame matrices must equal size of frame. Note
1999 that we are called for X frames with window widths NOT equal
2000 to the frame width (from CHANGE_FRAME_SIZE_1). */
2001 eassert (matrix_dim.width == FRAME_COLS (f)
2002 && matrix_dim.height == FRAME_LINES (f));
2004 /* Pointers to glyph memory in glyph rows are exchanged during
2005 the update phase of redisplay, which means in general that a
2006 frame's current matrix consists of pointers into both the
2007 desired and current glyph pool of the frame. Adjusting a
2008 matrix sets the frame matrix up so that pointers are all into
2009 the same pool. If we want to preserve glyph contents of the
2010 current matrix over a call to adjust_glyph_matrix, we must
2011 make a copy of the current glyphs, and restore the current
2012 matrix' contents from that copy. */
2013 if (display_completed
2014 && !FRAME_GARBAGED_P (f)
2015 && matrix_dim.width == f->current_matrix->matrix_w
2016 && matrix_dim.height == f->current_matrix->matrix_h
2017 /* For some reason, the frame glyph matrix gets corrupted if
2018 any of the windows contain margins. I haven't been able
2019 to hunt down the reason, but for the moment this prevents
2020 the problem from manifesting. -- cyd */
2021 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2023 struct glyph_matrix *copy = save_current_matrix (f);
2024 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2025 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2026 restore_current_matrix (f, copy);
2027 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2029 else
2031 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2032 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2033 SET_FRAME_GARBAGED (f);
2039 /* Allocate/reallocate glyph matrices of a single frame F for
2040 window-based redisplay. */
2042 static void
2043 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2045 eassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2047 /* Allocate/reallocate window matrices. */
2048 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2050 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2051 /* Allocate/ reallocate matrices of the dummy window used to display
2052 the menu bar under X when no X toolkit support is available. */
2054 /* Allocate a dummy window if not already done. */
2055 struct window *w;
2056 if (NILP (f->menu_bar_window))
2058 Lisp_Object frame;
2059 fset_menu_bar_window (f, make_window ());
2060 w = XWINDOW (f->menu_bar_window);
2061 XSETFRAME (frame, f);
2062 wset_frame (w, frame);
2063 w->pseudo_window_p = 1;
2065 else
2066 w = XWINDOW (f->menu_bar_window);
2068 /* Set window dimensions to frame dimensions and allocate or
2069 adjust glyph matrices of W. */
2070 w->top_line = 0;
2071 w->left_col = 0;
2072 w->total_lines = FRAME_MENU_BAR_LINES (f);
2073 w->total_cols = FRAME_TOTAL_COLS (f);
2074 allocate_matrices_for_window_redisplay (w);
2076 #endif
2078 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
2080 /* Allocate/ reallocate matrices of the tool bar window. If we
2081 don't have a tool bar window yet, make one. */
2082 struct window *w;
2083 if (NILP (f->tool_bar_window))
2085 Lisp_Object frame;
2086 fset_tool_bar_window (f, make_window ());
2087 w = XWINDOW (f->tool_bar_window);
2088 XSETFRAME (frame, f);
2089 wset_frame (w, frame);
2090 w->pseudo_window_p = 1;
2092 else
2093 w = XWINDOW (f->tool_bar_window);
2095 w->top_line = FRAME_MENU_BAR_LINES (f);
2096 w->left_col = 0;
2097 w->total_lines = FRAME_TOOL_BAR_LINES (f);
2098 w->total_cols = FRAME_TOTAL_COLS (f);
2099 allocate_matrices_for_window_redisplay (w);
2101 #endif
2105 /* Re-allocate buffer for decode_mode_spec on frame F. */
2107 static void
2108 adjust_decode_mode_spec_buffer (struct frame *f)
2110 f->decode_mode_spec_buffer = xrealloc (f->decode_mode_spec_buffer,
2111 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2116 /**********************************************************************
2117 Freeing Glyph Matrices
2118 **********************************************************************/
2120 /* Free glyph memory for a frame F. F may be null. This function can
2121 be called for the same frame more than once. The root window of
2122 F may be nil when this function is called. This is the case when
2123 the function is called when F is destroyed. */
2125 void
2126 free_glyphs (struct frame *f)
2128 if (f && f->glyphs_initialized_p)
2130 /* Block interrupt input so that we don't get surprised by an X
2131 event while we're in an inconsistent state. */
2132 block_input ();
2133 f->glyphs_initialized_p = 0;
2135 /* Release window sub-matrices. */
2136 if (!NILP (f->root_window))
2137 free_window_matrices (XWINDOW (f->root_window));
2139 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2140 /* Free the dummy window for menu bars without X toolkit and its
2141 glyph matrices. */
2142 if (!NILP (f->menu_bar_window))
2144 struct window *w = XWINDOW (f->menu_bar_window);
2145 free_glyph_matrix (w->desired_matrix);
2146 free_glyph_matrix (w->current_matrix);
2147 w->desired_matrix = w->current_matrix = NULL;
2148 fset_menu_bar_window (f, Qnil);
2150 #endif
2152 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
2153 /* Free the tool bar window and its glyph matrices. */
2154 if (!NILP (f->tool_bar_window))
2156 struct window *w = XWINDOW (f->tool_bar_window);
2157 free_glyph_matrix (w->desired_matrix);
2158 free_glyph_matrix (w->current_matrix);
2159 w->desired_matrix = w->current_matrix = NULL;
2160 fset_tool_bar_window (f, Qnil);
2162 #endif
2164 /* Release frame glyph matrices. Reset fields to zero in
2165 case we are called a second time. */
2166 if (f->desired_matrix)
2168 free_glyph_matrix (f->desired_matrix);
2169 free_glyph_matrix (f->current_matrix);
2170 f->desired_matrix = f->current_matrix = NULL;
2173 /* Release glyph pools. */
2174 if (f->desired_pool)
2176 free_glyph_pool (f->desired_pool);
2177 free_glyph_pool (f->current_pool);
2178 f->desired_pool = f->current_pool = NULL;
2181 unblock_input ();
2186 /* Free glyph sub-matrices in the window tree rooted at W. This
2187 function may be called with a null pointer, and it may be called on
2188 the same tree more than once. */
2190 void
2191 free_window_matrices (struct window *w)
2193 while (w)
2195 if (WINDOWP (w->contents))
2196 free_window_matrices (XWINDOW (w->contents));
2197 else
2199 /* This is a leaf window. Free its memory and reset fields
2200 to zero in case this function is called a second time for
2201 W. */
2202 free_glyph_matrix (w->current_matrix);
2203 free_glyph_matrix (w->desired_matrix);
2204 w->current_matrix = w->desired_matrix = NULL;
2207 /* Next window on same level. */
2208 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2213 /* Check glyph memory leaks. This function is called from
2214 shut_down_emacs. Note that frames are not destroyed when Emacs
2215 exits. We therefore free all glyph memory for all active frames
2216 explicitly and check that nothing is left allocated. */
2218 void
2219 check_glyph_memory (void)
2221 Lisp_Object tail, frame;
2223 /* Free glyph memory for all frames. */
2224 FOR_EACH_FRAME (tail, frame)
2225 free_glyphs (XFRAME (frame));
2227 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2228 /* Check that nothing is left allocated. */
2229 eassert (glyph_matrix_count == 0);
2230 eassert (glyph_pool_count == 0);
2231 #endif
2236 /**********************************************************************
2237 Building a Frame Matrix
2238 **********************************************************************/
2240 /* Most of the redisplay code works on glyph matrices attached to
2241 windows. This is a good solution most of the time, but it is not
2242 suitable for terminal code. Terminal output functions cannot rely
2243 on being able to set an arbitrary terminal window. Instead they
2244 must be provided with a view of the whole frame, i.e. the whole
2245 screen. We build such a view by constructing a frame matrix from
2246 window matrices in this section.
2248 Windows that must be updated have their must_be_updated_p flag set.
2249 For all such windows, their desired matrix is made part of the
2250 desired frame matrix. For other windows, their current matrix is
2251 made part of the desired frame matrix.
2253 +-----------------+----------------+
2254 | desired | desired |
2255 | | |
2256 +-----------------+----------------+
2257 | current |
2259 +----------------------------------+
2261 Desired window matrices can be made part of the frame matrix in a
2262 cheap way: We exploit the fact that the desired frame matrix and
2263 desired window matrices share their glyph memory. This is not
2264 possible for current window matrices. Their glyphs are copied to
2265 the desired frame matrix. The latter is equivalent to
2266 preserve_other_columns in the old redisplay.
2268 Used glyphs counters for frame matrix rows are the result of adding
2269 up glyph lengths of the window matrices. A line in the frame
2270 matrix is enabled, if a corresponding line in a window matrix is
2271 enabled.
2273 After building the desired frame matrix, it will be passed to
2274 terminal code, which will manipulate both the desired and current
2275 frame matrix. Changes applied to the frame's current matrix have
2276 to be visible in current window matrices afterwards, of course.
2278 This problem is solved like this:
2280 1. Window and frame matrices share glyphs. Window matrices are
2281 constructed in a way that their glyph contents ARE the glyph
2282 contents needed in a frame matrix. Thus, any modification of
2283 glyphs done in terminal code will be reflected in window matrices
2284 automatically.
2286 2. Exchanges of rows in a frame matrix done by terminal code are
2287 intercepted by hook functions so that corresponding row operations
2288 on window matrices can be performed. This is necessary because we
2289 use pointers to glyphs in glyph row structures. To satisfy the
2290 assumption of point 1 above that glyphs are updated implicitly in
2291 window matrices when they are manipulated via the frame matrix,
2292 window and frame matrix must of course agree where to find the
2293 glyphs for their rows. Possible manipulations that must be
2294 mirrored are assignments of rows of the desired frame matrix to the
2295 current frame matrix and scrolling the current frame matrix. */
2297 /* Build frame F's desired matrix from window matrices. Only windows
2298 which have the flag must_be_updated_p set have to be updated. Menu
2299 bar lines of a frame are not covered by window matrices, so make
2300 sure not to touch them in this function. */
2302 static void
2303 build_frame_matrix (struct frame *f)
2305 int i;
2307 /* F must have a frame matrix when this function is called. */
2308 eassert (!FRAME_WINDOW_P (f));
2310 /* Clear all rows in the frame matrix covered by window matrices.
2311 Menu bar lines are not covered by windows. */
2312 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2313 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2315 /* Build the matrix by walking the window tree. */
2316 build_frame_matrix_from_window_tree (f->desired_matrix,
2317 XWINDOW (FRAME_ROOT_WINDOW (f)));
2321 /* Walk a window tree, building a frame matrix MATRIX from window
2322 matrices. W is the root of a window tree. */
2324 static void
2325 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2327 while (w)
2329 if (WINDOWP (w->contents))
2330 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->contents));
2331 else
2332 build_frame_matrix_from_leaf_window (matrix, w);
2334 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2339 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2340 desired frame matrix built. W is a leaf window whose desired or
2341 current matrix is to be added to FRAME_MATRIX. W's flag
2342 must_be_updated_p determines which matrix it contributes to
2343 FRAME_MATRIX. If W->must_be_updated_p, W's desired matrix
2344 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2345 Adding a desired matrix means setting up used counters and such in
2346 frame rows, while adding a current window matrix to FRAME_MATRIX
2347 means copying glyphs. The latter case corresponds to
2348 preserve_other_columns in the old redisplay. */
2350 static void
2351 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2353 struct glyph_matrix *window_matrix;
2354 int window_y, frame_y;
2355 /* If non-zero, a glyph to insert at the right border of W. */
2356 GLYPH right_border_glyph;
2358 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2360 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2361 if (w->must_be_updated_p)
2363 window_matrix = w->desired_matrix;
2365 /* Decide whether we want to add a vertical border glyph. */
2366 if (!WINDOW_RIGHTMOST_P (w))
2368 struct Lisp_Char_Table *dp = window_display_table (w);
2369 Lisp_Object gc;
2371 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2372 if (dp
2373 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc)))
2375 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2376 spec_glyph_lookup_face (w, &right_border_glyph);
2379 if (GLYPH_FACE (right_border_glyph) <= 0)
2380 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2383 else
2384 window_matrix = w->current_matrix;
2386 /* For all rows in the window matrix and corresponding rows in the
2387 frame matrix. */
2388 window_y = 0;
2389 frame_y = window_matrix->matrix_y;
2390 while (window_y < window_matrix->nrows)
2392 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2393 struct glyph_row *window_row = window_matrix->rows + window_y;
2394 bool current_row_p = window_matrix == w->current_matrix;
2396 /* Fill up the frame row with spaces up to the left margin of the
2397 window row. */
2398 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2400 /* Fill up areas in the window matrix row with spaces. */
2401 fill_up_glyph_row_with_spaces (window_row);
2403 /* If only part of W's desired matrix has been built, and
2404 window_row wasn't displayed, use the corresponding current
2405 row instead. */
2406 if (window_matrix == w->desired_matrix
2407 && !window_row->enabled_p)
2409 window_row = w->current_matrix->rows + window_y;
2410 current_row_p = 1;
2413 if (current_row_p)
2415 /* Copy window row to frame row. */
2416 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2417 window_row->glyphs[0],
2418 window_matrix->matrix_w * sizeof (struct glyph));
2420 else
2422 eassert (window_row->enabled_p);
2424 /* Only when a desired row has been displayed, we want
2425 the corresponding frame row to be updated. */
2426 frame_row->enabled_p = 1;
2428 /* Maybe insert a vertical border between horizontally adjacent
2429 windows. */
2430 if (GLYPH_CHAR (right_border_glyph) != 0)
2432 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2433 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2436 #ifdef GLYPH_DEBUG
2437 /* Window row window_y must be a slice of frame row
2438 frame_y. */
2439 eassert (glyph_row_slice_p (window_row, frame_row));
2441 /* If rows are in sync, we don't have to copy glyphs because
2442 frame and window share glyphs. */
2444 strcpy (w->current_matrix->method, w->desired_matrix->method);
2445 add_window_display_history (w, w->current_matrix->method, 0);
2446 #endif
2449 /* Set number of used glyphs in the frame matrix. Since we fill
2450 up with spaces, and visit leaf windows from left to right it
2451 can be done simply. */
2452 frame_row->used[TEXT_AREA]
2453 = window_matrix->matrix_x + window_matrix->matrix_w;
2455 /* Next row. */
2456 ++window_y;
2457 ++frame_y;
2461 /* Given a user-specified glyph, possibly including a Lisp-level face
2462 ID, return a glyph that has a realized face ID.
2463 This is used for glyphs displayed specially and not part of the text;
2464 for instance, vertical separators, truncation markers, etc. */
2466 void
2467 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2469 int lface_id = GLYPH_FACE (*glyph);
2470 /* Convert the glyph's specified face to a realized (cache) face. */
2471 if (lface_id > 0)
2473 int face_id = merge_faces (XFRAME (w->frame),
2474 Qt, lface_id, DEFAULT_FACE_ID);
2475 SET_GLYPH_FACE (*glyph, face_id);
2479 /* Add spaces to a glyph row ROW in a window matrix.
2481 Each row has the form:
2483 +---------+-----------------------------+------------+
2484 | left | text | right |
2485 +---------+-----------------------------+------------+
2487 Left and right marginal areas are optional. This function adds
2488 spaces to areas so that there are no empty holes between areas.
2489 In other words: If the right area is not empty, the text area
2490 is filled up with spaces up to the right area. If the text area
2491 is not empty, the left area is filled up.
2493 To be called for frame-based redisplay, only. */
2495 static void
2496 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2498 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2499 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2500 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2504 /* Fill area AREA of glyph row ROW with spaces. To be called for
2505 frame-based redisplay only. */
2507 static void
2508 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2510 if (row->glyphs[area] < row->glyphs[area + 1])
2512 struct glyph *end = row->glyphs[area + 1];
2513 struct glyph *text = row->glyphs[area] + row->used[area];
2515 while (text < end)
2516 *text++ = space_glyph;
2517 row->used[area] = text - row->glyphs[area];
2522 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2523 reached. In frame matrices only one area, TEXT_AREA, is used. */
2525 static void
2526 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2528 int i = row->used[TEXT_AREA];
2529 struct glyph *glyph = row->glyphs[TEXT_AREA];
2531 while (i < upto)
2532 glyph[i++] = space_glyph;
2534 row->used[TEXT_AREA] = i;
2539 /**********************************************************************
2540 Mirroring operations on frame matrices in window matrices
2541 **********************************************************************/
2543 /* Set frame being updated via frame-based redisplay to F. This
2544 function must be called before updates to make explicit that we are
2545 working on frame matrices or not. */
2547 static void
2548 set_frame_matrix_frame (struct frame *f)
2550 frame_matrix_frame = f;
2554 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2555 DESIRED_MATRIX is the desired matrix corresponding to
2556 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2557 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2558 frame_matrix_frame is non-null, this indicates that the exchange is
2559 done in frame matrices, and that we have to perform analogous
2560 operations in window matrices of frame_matrix_frame. */
2562 static void
2563 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2565 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2566 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2567 bool mouse_face_p = current_row->mouse_face_p;
2569 /* Do current_row = desired_row. This exchanges glyph pointers
2570 between both rows, and does a structure assignment otherwise. */
2571 assign_row (current_row, desired_row);
2573 /* Enable current_row to mark it as valid. */
2574 current_row->enabled_p = 1;
2575 current_row->mouse_face_p = mouse_face_p;
2577 /* If we are called on frame matrices, perform analogous operations
2578 for window matrices. */
2579 if (frame_matrix_frame)
2580 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2584 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2585 W's frame which has been made current (by swapping pointers between
2586 current and desired matrix). Perform analogous operations in the
2587 matrices of leaf windows in the window tree rooted at W. */
2589 static void
2590 mirror_make_current (struct window *w, int frame_row)
2592 while (w)
2594 if (WINDOWP (w->contents))
2595 mirror_make_current (XWINDOW (w->contents), frame_row);
2596 else
2598 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2599 here because the checks performed in debug mode there
2600 will not allow the conversion. */
2601 int row = frame_row - w->desired_matrix->matrix_y;
2603 /* If FRAME_ROW is within W, assign the desired row to the
2604 current row (exchanging glyph pointers). */
2605 if (row >= 0 && row < w->desired_matrix->matrix_h)
2607 struct glyph_row *current_row
2608 = MATRIX_ROW (w->current_matrix, row);
2609 struct glyph_row *desired_row
2610 = MATRIX_ROW (w->desired_matrix, row);
2612 if (desired_row->enabled_p)
2613 assign_row (current_row, desired_row);
2614 else
2615 swap_glyph_pointers (desired_row, current_row);
2616 current_row->enabled_p = 1;
2618 /* Set the Y coordinate of the mode/header line's row.
2619 It is needed in draw_row_with_mouse_face to find the
2620 screen coordinates. (Window-based redisplay sets
2621 this in update_window, but no one seems to do that
2622 for frame-based redisplay.) */
2623 if (current_row->mode_line_p)
2624 current_row->y = row;
2628 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2633 /* Perform row dance after scrolling. We are working on the range of
2634 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2635 including) in MATRIX. COPY_FROM is a vector containing, for each
2636 row I in the range 0 <= I < NLINES, the index of the original line
2637 to move to I. This index is relative to the row range, i.e. 0 <=
2638 index < NLINES. RETAINED_P is a vector containing zero for each
2639 row 0 <= I < NLINES which is empty.
2641 This function is called from do_scrolling and do_direct_scrolling. */
2643 void
2644 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2645 int *copy_from, char *retained_p)
2647 /* A copy of original rows. */
2648 struct glyph_row *old_rows;
2650 /* Rows to assign to. */
2651 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2653 int i;
2655 /* Make a copy of the original rows. */
2656 old_rows = alloca (nlines * sizeof *old_rows);
2657 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2659 /* Assign new rows, maybe clear lines. */
2660 for (i = 0; i < nlines; ++i)
2662 bool enabled_before_p = new_rows[i].enabled_p;
2664 eassert (i + unchanged_at_top < matrix->nrows);
2665 eassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2666 new_rows[i] = old_rows[copy_from[i]];
2667 new_rows[i].enabled_p = enabled_before_p;
2669 /* RETAINED_P is zero for empty lines. */
2670 if (!retained_p[copy_from[i]])
2671 new_rows[i].enabled_p = 0;
2674 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2675 if (frame_matrix_frame)
2676 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2677 unchanged_at_top, nlines, copy_from, retained_p);
2681 /* Synchronize glyph pointers in the current matrix of window W with
2682 the current frame matrix. */
2684 static void
2685 sync_window_with_frame_matrix_rows (struct window *w)
2687 struct frame *f = XFRAME (w->frame);
2688 struct glyph_row *window_row, *window_row_end, *frame_row;
2689 int left, right, x, width;
2691 /* Preconditions: W must be a live window on a tty frame. */
2692 eassert (BUFFERP (w->contents));
2693 eassert (!FRAME_WINDOW_P (f));
2695 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2696 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2697 x = w->current_matrix->matrix_x;
2698 width = w->current_matrix->matrix_w;
2700 window_row = w->current_matrix->rows;
2701 window_row_end = window_row + w->current_matrix->nrows;
2702 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2704 for (; window_row < window_row_end; ++window_row, ++frame_row)
2706 window_row->glyphs[LEFT_MARGIN_AREA]
2707 = frame_row->glyphs[0] + x;
2708 window_row->glyphs[TEXT_AREA]
2709 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2710 window_row->glyphs[LAST_AREA]
2711 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2712 window_row->glyphs[RIGHT_MARGIN_AREA]
2713 = window_row->glyphs[LAST_AREA] - right;
2718 /* Return the window in the window tree rooted in W containing frame
2719 row ROW. Value is null if none is found. */
2721 static struct window *
2722 frame_row_to_window (struct window *w, int row)
2724 struct window *found = NULL;
2726 while (w && !found)
2728 if (WINDOWP (w->contents))
2729 found = frame_row_to_window (XWINDOW (w->contents), row);
2730 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2731 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2732 found = w;
2734 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2737 return found;
2741 /* Perform a line dance in the window tree rooted at W, after
2742 scrolling a frame matrix in mirrored_line_dance.
2744 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2745 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2746 COPY_FROM is a vector containing, for each row I in the range 0 <=
2747 I < NLINES, the index of the original line to move to I. This
2748 index is relative to the row range, i.e. 0 <= index < NLINES.
2749 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2750 which is empty. */
2752 static void
2753 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2755 while (w)
2757 if (WINDOWP (w->contents))
2758 mirror_line_dance (XWINDOW (w->contents), unchanged_at_top,
2759 nlines, copy_from, retained_p);
2760 else
2762 /* W is a leaf window, and we are working on its current
2763 matrix m. */
2764 struct glyph_matrix *m = w->current_matrix;
2765 int i;
2766 bool sync_p = 0;
2767 struct glyph_row *old_rows;
2769 /* Make a copy of the original rows of matrix m. */
2770 old_rows = alloca (m->nrows * sizeof *old_rows);
2771 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2773 for (i = 0; i < nlines; ++i)
2775 /* Frame relative line assigned to. */
2776 int frame_to = i + unchanged_at_top;
2778 /* Frame relative line assigned. */
2779 int frame_from = copy_from[i] + unchanged_at_top;
2781 /* Window relative line assigned to. */
2782 int window_to = frame_to - m->matrix_y;
2784 /* Window relative line assigned. */
2785 int window_from = frame_from - m->matrix_y;
2787 /* Is assigned line inside window? */
2788 bool from_inside_window_p
2789 = window_from >= 0 && window_from < m->matrix_h;
2791 /* Is assigned to line inside window? */
2792 bool to_inside_window_p
2793 = window_to >= 0 && window_to < m->matrix_h;
2795 if (from_inside_window_p && to_inside_window_p)
2797 /* Do the assignment. The enabled_p flag is saved
2798 over the assignment because the old redisplay did
2799 that. */
2800 bool enabled_before_p = m->rows[window_to].enabled_p;
2801 m->rows[window_to] = old_rows[window_from];
2802 m->rows[window_to].enabled_p = enabled_before_p;
2804 /* If frame line is empty, window line is empty, too. */
2805 if (!retained_p[copy_from[i]])
2806 m->rows[window_to].enabled_p = 0;
2808 else if (to_inside_window_p)
2810 /* A copy between windows. This is an infrequent
2811 case not worth optimizing. */
2812 struct frame *f = XFRAME (w->frame);
2813 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2814 struct window *w2;
2815 struct glyph_matrix *m2;
2816 int m2_from;
2818 w2 = frame_row_to_window (root, frame_from);
2819 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2820 -nw', FROM_FRAME sometimes has no associated window.
2821 This check avoids a segfault if W2 is null. */
2822 if (w2)
2824 m2 = w2->current_matrix;
2825 m2_from = frame_from - m2->matrix_y;
2826 copy_row_except_pointers (m->rows + window_to,
2827 m2->rows + m2_from);
2829 /* If frame line is empty, window line is empty, too. */
2830 if (!retained_p[copy_from[i]])
2831 m->rows[window_to].enabled_p = 0;
2833 sync_p = 1;
2835 else if (from_inside_window_p)
2836 sync_p = 1;
2839 /* If there was a copy between windows, make sure glyph
2840 pointers are in sync with the frame matrix. */
2841 if (sync_p)
2842 sync_window_with_frame_matrix_rows (w);
2844 /* Check that no pointers are lost. */
2845 CHECK_MATRIX (m);
2848 /* Next window on same level. */
2849 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2854 #ifdef GLYPH_DEBUG
2856 /* Check that window and frame matrices agree about their
2857 understanding where glyphs of the rows are to find. For each
2858 window in the window tree rooted at W, check that rows in the
2859 matrices of leaf window agree with their frame matrices about
2860 glyph pointers. */
2862 static void
2863 check_window_matrix_pointers (struct window *w)
2865 while (w)
2867 if (WINDOWP (w->contents))
2868 check_window_matrix_pointers (XWINDOW (w->contents));
2869 else
2871 struct frame *f = XFRAME (w->frame);
2872 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
2873 check_matrix_pointers (w->current_matrix, f->current_matrix);
2876 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2881 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
2882 a window and FRAME_MATRIX is the corresponding frame matrix. For
2883 each row in WINDOW_MATRIX check that it's a slice of the
2884 corresponding frame row. If it isn't, abort. */
2886 static void
2887 check_matrix_pointers (struct glyph_matrix *window_matrix,
2888 struct glyph_matrix *frame_matrix)
2890 /* Row number in WINDOW_MATRIX. */
2891 int i = 0;
2893 /* Row number corresponding to I in FRAME_MATRIX. */
2894 int j = window_matrix->matrix_y;
2896 /* For all rows check that the row in the window matrix is a
2897 slice of the row in the frame matrix. If it isn't we didn't
2898 mirror an operation on the frame matrix correctly. */
2899 while (i < window_matrix->nrows)
2901 if (!glyph_row_slice_p (window_matrix->rows + i,
2902 frame_matrix->rows + j))
2903 emacs_abort ();
2904 ++i, ++j;
2908 #endif /* GLYPH_DEBUG */
2912 /**********************************************************************
2913 VPOS and HPOS translations
2914 **********************************************************************/
2916 #ifdef GLYPH_DEBUG
2918 /* Translate vertical position VPOS which is relative to window W to a
2919 vertical position relative to W's frame. */
2921 static int
2922 window_to_frame_vpos (struct window *w, int vpos)
2924 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
2925 eassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
2926 vpos += WINDOW_TOP_EDGE_LINE (w);
2927 eassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
2928 return vpos;
2932 /* Translate horizontal position HPOS which is relative to window W to
2933 a horizontal position relative to W's frame. */
2935 static int
2936 window_to_frame_hpos (struct window *w, int hpos)
2938 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
2939 hpos += WINDOW_LEFT_EDGE_COL (w);
2940 return hpos;
2943 #endif /* GLYPH_DEBUG */
2947 /**********************************************************************
2948 Redrawing Frames
2949 **********************************************************************/
2951 /* Redraw frame F. */
2953 void
2954 redraw_frame (struct frame *f)
2956 /* Error if F has no glyphs. */
2957 eassert (f->glyphs_initialized_p);
2958 update_begin (f);
2959 #ifdef MSDOS
2960 if (FRAME_MSDOS_P (f))
2961 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
2962 #endif
2963 clear_frame (f);
2964 clear_current_matrices (f);
2965 update_end (f);
2966 if (FRAME_TERMCAP_P (f))
2967 fflush (FRAME_TTY (f)->output);
2968 windows_or_buffers_changed++;
2969 /* Mark all windows as inaccurate, so that every window will have
2970 its redisplay done. */
2971 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
2972 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
2973 f->garbaged = 0;
2976 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 0, 1, 0,
2977 doc: /* Clear frame FRAME and output again what is supposed to appear on it.
2978 If FRAME is omitted or nil, the selected frame is used. */)
2979 (Lisp_Object frame)
2981 redraw_frame (decode_live_frame (frame));
2982 return Qnil;
2985 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
2986 doc: /* Clear and redisplay all visible frames. */)
2987 (void)
2989 Lisp_Object tail, frame;
2991 FOR_EACH_FRAME (tail, frame)
2992 if (FRAME_VISIBLE_P (XFRAME (frame)))
2993 redraw_frame (XFRAME (frame));
2995 return Qnil;
3000 /***********************************************************************
3001 Frame Update
3002 ***********************************************************************/
3004 /* Update frame F based on the data in desired matrices.
3006 If FORCE_P, don't let redisplay be stopped by detecting pending input.
3007 If INHIBIT_HAIRY_ID_P, don't try scrolling.
3009 Value is true if redisplay was stopped due to pending input. */
3011 bool
3012 update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
3014 /* True means display has been paused because of pending input. */
3015 bool paused_p;
3016 struct window *root_window = XWINDOW (f->root_window);
3018 if (redisplay_dont_pause)
3019 force_p = 1;
3020 else if (!force_p && detect_input_pending_ignore_squeezables ())
3022 paused_p = 1;
3023 goto do_pause;
3026 if (FRAME_WINDOW_P (f))
3028 /* We are working on window matrix basis. All windows whose
3029 flag must_be_updated_p is set have to be updated. */
3031 /* Record that we are not working on frame matrices. */
3032 set_frame_matrix_frame (NULL);
3034 /* Update all windows in the window tree of F, maybe stopping
3035 when pending input is detected. */
3036 update_begin (f);
3038 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
3039 /* Update the menu bar on X frames that don't have toolkit
3040 support. */
3041 if (WINDOWP (f->menu_bar_window))
3042 update_window (XWINDOW (f->menu_bar_window), 1);
3043 #endif
3045 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
3046 /* Update the tool-bar window, if present. */
3047 if (WINDOWP (f->tool_bar_window))
3049 struct window *w = XWINDOW (f->tool_bar_window);
3051 /* Update tool-bar window. */
3052 if (w->must_be_updated_p)
3054 Lisp_Object tem;
3056 update_window (w, 1);
3057 w->must_be_updated_p = 0;
3059 /* Swap tool-bar strings. We swap because we want to
3060 reuse strings. */
3061 tem = f->current_tool_bar_string;
3062 fset_current_tool_bar_string (f, f->desired_tool_bar_string);
3063 fset_desired_tool_bar_string (f, tem);
3066 #endif
3068 /* Update windows. */
3069 paused_p = update_window_tree (root_window, force_p);
3070 update_end (f);
3072 else
3074 /* We are working on frame matrix basis. Set the frame on whose
3075 frame matrix we operate. */
3076 set_frame_matrix_frame (f);
3078 /* Build F's desired matrix from window matrices. */
3079 build_frame_matrix (f);
3081 /* Update the display */
3082 update_begin (f);
3083 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3084 update_end (f);
3086 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3088 if (FRAME_TTY (f)->termscript)
3089 fflush (FRAME_TTY (f)->termscript);
3090 if (FRAME_TERMCAP_P (f))
3091 fflush (FRAME_TTY (f)->output);
3094 /* Check window matrices for lost pointers. */
3095 #ifdef GLYPH_DEBUG
3096 check_window_matrix_pointers (root_window);
3097 add_frame_display_history (f, paused_p);
3098 #endif
3101 do_pause:
3102 /* Reset flags indicating that a window should be updated. */
3103 set_window_update_flags (root_window, 0);
3105 display_completed = !paused_p;
3106 return paused_p;
3111 /************************************************************************
3112 Window-based updates
3113 ************************************************************************/
3115 /* Perform updates in window tree rooted at W.
3116 If FORCE_P, don't stop updating if input is pending. */
3118 static bool
3119 update_window_tree (struct window *w, bool force_p)
3121 bool paused_p = 0;
3123 while (w && !paused_p)
3125 if (WINDOWP (w->contents))
3126 paused_p |= update_window_tree (XWINDOW (w->contents), force_p);
3127 else if (w->must_be_updated_p)
3128 paused_p |= update_window (w, force_p);
3130 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3133 return paused_p;
3137 /* Update window W if its flag must_be_updated_p is set.
3138 If FORCE_P, don't stop updating if input is pending. */
3140 void
3141 update_single_window (struct window *w, bool force_p)
3143 if (w->must_be_updated_p)
3145 struct frame *f = XFRAME (WINDOW_FRAME (w));
3147 /* Record that this is not a frame-based redisplay. */
3148 set_frame_matrix_frame (NULL);
3150 if (redisplay_dont_pause)
3151 force_p = 1;
3153 /* Update W. */
3154 update_begin (f);
3155 update_window (w, force_p);
3156 update_end (f);
3158 /* Reset flag in W. */
3159 w->must_be_updated_p = 0;
3163 #ifdef HAVE_WINDOW_SYSTEM
3165 /* Redraw lines from the current matrix of window W that are
3166 overlapped by other rows. YB is bottom-most y-position in W. */
3168 static void
3169 redraw_overlapped_rows (struct window *w, int yb)
3171 int i;
3172 struct frame *f = XFRAME (WINDOW_FRAME (w));
3174 /* If rows overlapping others have been changed, the rows being
3175 overlapped have to be redrawn. This won't draw lines that have
3176 already been drawn in update_window_line because overlapped_p in
3177 desired rows is 0, so after row assignment overlapped_p in
3178 current rows is 0. */
3179 for (i = 0; i < w->current_matrix->nrows; ++i)
3181 struct glyph_row *row = w->current_matrix->rows + i;
3183 if (!row->enabled_p)
3184 break;
3185 else if (row->mode_line_p)
3186 continue;
3188 if (row->overlapped_p)
3190 enum glyph_row_area area;
3192 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3194 output_cursor_to (w, i, 0, row->y,
3195 area == TEXT_AREA ? row->x : 0);
3196 if (row->used[area])
3197 FRAME_RIF (f)->write_glyphs (w, row, row->glyphs[area],
3198 area, row->used[area]);
3199 FRAME_RIF (f)->clear_end_of_line (w, row, area, -1);
3202 row->overlapped_p = 0;
3205 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3206 break;
3211 /* Redraw lines from the current matrix of window W that overlap
3212 others. YB is bottom-most y-position in W. */
3214 static void
3215 redraw_overlapping_rows (struct window *w, int yb)
3217 int i, bottom_y;
3218 struct glyph_row *row;
3219 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3221 for (i = 0; i < w->current_matrix->nrows; ++i)
3223 row = w->current_matrix->rows + i;
3225 if (!row->enabled_p)
3226 break;
3227 else if (row->mode_line_p)
3228 continue;
3230 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3232 if (row->overlapping_p)
3234 int overlaps = 0;
3236 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3237 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3238 overlaps |= OVERLAPS_PRED;
3239 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3240 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3241 overlaps |= OVERLAPS_SUCC;
3243 if (overlaps)
3245 if (row->used[LEFT_MARGIN_AREA])
3246 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3248 if (row->used[TEXT_AREA])
3249 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3251 if (row->used[RIGHT_MARGIN_AREA])
3252 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3254 /* Record in neighbor rows that ROW overwrites part of
3255 their display. */
3256 if (overlaps & OVERLAPS_PRED)
3257 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3258 if (overlaps & OVERLAPS_SUCC)
3259 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3263 if (bottom_y >= yb)
3264 break;
3268 #endif /* HAVE_WINDOW_SYSTEM */
3271 #if defined GLYPH_DEBUG && 0
3273 /* Check that no row in the current matrix of window W is enabled
3274 which is below what's displayed in the window. */
3276 static void
3277 check_current_matrix_flags (struct window *w)
3279 bool last_seen_p = 0;
3280 int i, yb = window_text_bottom_y (w);
3282 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3284 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3285 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3286 last_seen_p = 1;
3287 else if (last_seen_p && row->enabled_p)
3288 emacs_abort ();
3292 #endif /* GLYPH_DEBUG */
3295 /* Update display of window W.
3296 If FORCE_P, don't stop updating when input is pending. */
3298 static bool
3299 update_window (struct window *w, bool force_p)
3301 struct glyph_matrix *desired_matrix = w->desired_matrix;
3302 bool paused_p;
3303 int preempt_count = baud_rate / 2400 + 1;
3304 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3305 #ifdef GLYPH_DEBUG
3306 /* Check that W's frame doesn't have glyph matrices. */
3307 eassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3308 #endif
3310 /* Check pending input the first time so that we can quickly return. */
3311 if (!force_p)
3312 detect_input_pending_ignore_squeezables ();
3314 /* If forced to complete the update, or if no input is pending, do
3315 the update. */
3316 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3318 struct glyph_row *row, *end;
3319 struct glyph_row *mode_line_row;
3320 struct glyph_row *header_line_row;
3321 int yb;
3322 bool changed_p = 0, mouse_face_overwritten_p = 0;
3323 int n_updated = 0;
3325 rif->update_window_begin_hook (w);
3326 yb = window_text_bottom_y (w);
3327 row = MATRIX_ROW (desired_matrix, 0);
3328 end = MATRIX_MODE_LINE_ROW (desired_matrix);
3330 /* Take note of the header line, if there is one. We will
3331 update it below, after updating all of the window's lines. */
3332 if (row->mode_line_p)
3334 header_line_row = row;
3335 ++row;
3337 else
3338 header_line_row = NULL;
3340 /* Update the mode line, if necessary. */
3341 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3342 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3344 mode_line_row->y = yb;
3345 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3346 desired_matrix),
3347 &mouse_face_overwritten_p);
3350 /* Find first enabled row. Optimizations in redisplay_internal
3351 may lead to an update with only one row enabled. There may
3352 be also completely empty matrices. */
3353 while (row < end && !row->enabled_p)
3354 ++row;
3356 /* Try reusing part of the display by copying. */
3357 if (row < end && !desired_matrix->no_scrolling_p)
3359 int rc = scrolling_window (w, header_line_row != NULL);
3360 if (rc < 0)
3362 /* All rows were found to be equal. */
3363 paused_p = 0;
3364 goto set_cursor;
3366 else if (rc > 0)
3368 /* We've scrolled the display. */
3369 force_p = 1;
3370 changed_p = 1;
3374 /* Update the rest of the lines. */
3375 for (; row < end && (force_p || !input_pending); ++row)
3376 /* scrolling_window resets the enabled_p flag of the rows it
3377 reuses from current_matrix. */
3378 if (row->enabled_p)
3380 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3381 int i;
3383 /* We'll have to play a little bit with when to
3384 detect_input_pending. If it's done too often,
3385 scrolling large windows with repeated scroll-up
3386 commands will too quickly pause redisplay. */
3387 if (!force_p && ++n_updated % preempt_count == 0)
3388 detect_input_pending_ignore_squeezables ();
3389 changed_p |= update_window_line (w, vpos,
3390 &mouse_face_overwritten_p);
3392 /* Mark all rows below the last visible one in the current
3393 matrix as invalid. This is necessary because of
3394 variable line heights. Consider the case of three
3395 successive redisplays, where the first displays 5
3396 lines, the second 3 lines, and the third 5 lines again.
3397 If the second redisplay wouldn't mark rows in the
3398 current matrix invalid, the third redisplay might be
3399 tempted to optimize redisplay based on lines displayed
3400 in the first redisplay. */
3401 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3402 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3403 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3406 /* Was display preempted? */
3407 paused_p = row < end;
3409 set_cursor:
3411 /* Update the header line after scrolling because a new header
3412 line would otherwise overwrite lines at the top of the window
3413 that can be scrolled. */
3414 if (header_line_row && header_line_row->enabled_p)
3416 header_line_row->y = 0;
3417 update_window_line (w, 0, &mouse_face_overwritten_p);
3420 /* Fix the appearance of overlapping/overlapped rows. */
3421 if (!paused_p && !w->pseudo_window_p)
3423 #ifdef HAVE_WINDOW_SYSTEM
3424 if (changed_p && rif->fix_overlapping_area)
3426 redraw_overlapped_rows (w, yb);
3427 redraw_overlapping_rows (w, yb);
3429 #endif
3431 /* Make cursor visible at cursor position of W. */
3432 set_window_cursor_after_update (w);
3434 #if 0 /* Check that current matrix invariants are satisfied. This is
3435 for debugging only. See the comment of check_matrix_invariants. */
3436 IF_DEBUG (check_matrix_invariants (w));
3437 #endif
3440 #ifdef GLYPH_DEBUG
3441 /* Remember the redisplay method used to display the matrix. */
3442 strcpy (w->current_matrix->method, w->desired_matrix->method);
3443 #endif
3445 #ifdef HAVE_WINDOW_SYSTEM
3446 update_window_fringes (w, 0);
3447 #endif
3449 /* End the update of window W. Don't set the cursor if we
3450 paused updating the display because in this case,
3451 set_window_cursor_after_update hasn't been called, and
3452 W->output_cursor doesn't contain the cursor location. */
3453 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3455 else
3456 paused_p = 1;
3458 #ifdef GLYPH_DEBUG
3459 /* check_current_matrix_flags (w); */
3460 add_window_display_history (w, w->current_matrix->method, paused_p);
3461 #endif
3463 #ifdef HAVE_XWIDGETS
3464 xwidget_end_redisplay(w, w->current_matrix);
3465 #endif
3466 clear_glyph_matrix (desired_matrix);
3468 return paused_p;
3472 /* Update the display of area AREA in window W, row number VPOS.
3473 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3475 static void
3476 update_marginal_area (struct window *w, struct glyph_row *updated_row,
3477 enum glyph_row_area area, int vpos)
3479 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3480 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3482 /* Set cursor to start of glyphs, write them, and clear to the end
3483 of the area. I don't think that something more sophisticated is
3484 necessary here, since marginal areas will not be the default. */
3485 output_cursor_to (w, vpos, 0, desired_row->y, 0);
3486 if (desired_row->used[area])
3487 rif->write_glyphs (w, updated_row, desired_row->glyphs[area],
3488 area, desired_row->used[area]);
3489 rif->clear_end_of_line (w, updated_row, area, -1);
3493 /* Update the display of the text area of row VPOS in window W.
3494 Value is true if display has changed. */
3496 static bool
3497 update_text_area (struct window *w, struct glyph_row *updated_row, int vpos)
3499 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3500 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3501 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3502 bool changed_p = 0;
3504 /* If rows are at different X or Y, or rows have different height,
3505 or the current row is marked invalid, write the entire line. */
3506 if (!current_row->enabled_p
3507 || desired_row->y != current_row->y
3508 || desired_row->ascent != current_row->ascent
3509 || desired_row->phys_ascent != current_row->phys_ascent
3510 || desired_row->phys_height != current_row->phys_height
3511 || desired_row->visible_height != current_row->visible_height
3512 || current_row->overlapped_p
3513 /* This next line is necessary for correctly redrawing
3514 mouse-face areas after scrolling and other operations.
3515 However, it causes excessive flickering when mouse is moved
3516 across the mode line. Luckily, turning it off for the mode
3517 line doesn't seem to hurt anything. -- cyd.
3518 But it is still needed for the header line. -- kfs. */
3519 || (current_row->mouse_face_p
3520 && !(current_row->mode_line_p && vpos > 0))
3521 || current_row->x != desired_row->x)
3523 output_cursor_to (w, vpos, 0, desired_row->y, desired_row->x);
3525 if (desired_row->used[TEXT_AREA])
3526 rif->write_glyphs (w, updated_row, desired_row->glyphs[TEXT_AREA],
3527 TEXT_AREA, desired_row->used[TEXT_AREA]);
3529 /* Clear to end of window. */
3530 rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
3531 changed_p = 1;
3533 /* This erases the cursor. We do this here because
3534 notice_overwritten_cursor cannot easily check this, which
3535 might indicate that the whole functionality of
3536 notice_overwritten_cursor would better be implemented here.
3537 On the other hand, we need notice_overwritten_cursor as long
3538 as mouse highlighting is done asynchronously outside of
3539 redisplay. */
3540 if (vpos == w->phys_cursor.vpos)
3541 w->phys_cursor_on_p = 0;
3543 else
3545 int stop, i, x;
3546 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3547 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3548 bool overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3549 int desired_stop_pos = desired_row->used[TEXT_AREA];
3550 bool abort_skipping = 0;
3552 /* If the desired row extends its face to the text area end, and
3553 unless the current row also does so at the same position,
3554 make sure we write at least one glyph, so that the face
3555 extension actually takes place. */
3556 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3557 && (desired_stop_pos < current_row->used[TEXT_AREA]
3558 || (desired_stop_pos == current_row->used[TEXT_AREA]
3559 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3560 --desired_stop_pos;
3562 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3563 i = 0;
3564 x = desired_row->x;
3566 /* Loop over glyphs that current and desired row may have
3567 in common. */
3568 while (i < stop)
3570 bool can_skip_p = !abort_skipping;
3572 /* Skip over glyphs that both rows have in common. These
3573 don't have to be written. We can't skip if the last
3574 current glyph overlaps the glyph to its right. For
3575 example, consider a current row of `if ' with the `f' in
3576 Courier bold so that it overlaps the ` ' to its right.
3577 If the desired row is ` ', we would skip over the space
3578 after the `if' and there would remain a pixel from the
3579 `f' on the screen. */
3580 if (overlapping_glyphs_p && i > 0)
3582 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3583 int left, right;
3585 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3586 &left, &right);
3587 can_skip_p = (right == 0 && !abort_skipping);
3590 if (can_skip_p)
3592 int start_hpos = i;
3594 while (i < stop
3595 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3597 x += desired_glyph->pixel_width;
3598 ++desired_glyph, ++current_glyph, ++i;
3601 /* Consider the case that the current row contains "xxx
3602 ppp ggg" in italic Courier font, and the desired row
3603 is "xxx ggg". The character `p' has lbearing, `g'
3604 has not. The loop above will stop in front of the
3605 first `p' in the current row. If we would start
3606 writing glyphs there, we wouldn't erase the lbearing
3607 of the `p'. The rest of the lbearing problem is then
3608 taken care of by draw_glyphs. */
3609 if (overlapping_glyphs_p
3610 && i > 0
3611 && i < current_row->used[TEXT_AREA]
3612 && (current_row->used[TEXT_AREA]
3613 != desired_row->used[TEXT_AREA]))
3615 int left, right;
3617 rif->get_glyph_overhangs (current_glyph,
3618 XFRAME (w->frame),
3619 &left, &right);
3620 while (left > 0 && i > 0)
3622 --i, --desired_glyph, --current_glyph;
3623 x -= desired_glyph->pixel_width;
3624 left -= desired_glyph->pixel_width;
3627 /* Abort the skipping algorithm if we end up before
3628 our starting point, to avoid looping (bug#1070).
3629 This can happen when the lbearing is larger than
3630 the pixel width. */
3631 abort_skipping = (i < start_hpos);
3635 /* Try to avoid writing the entire rest of the desired row
3636 by looking for a resync point. This mainly prevents
3637 mode line flickering in the case the mode line is in
3638 fixed-pitch font, which it usually will be. */
3639 if (i < desired_row->used[TEXT_AREA])
3641 int start_x = x, start_hpos = i;
3642 struct glyph *start = desired_glyph;
3643 int current_x = x;
3644 bool skip_first_p = !can_skip_p;
3646 /* Find the next glyph that's equal again. */
3647 while (i < stop
3648 && (skip_first_p
3649 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3650 && x == current_x)
3652 x += desired_glyph->pixel_width;
3653 current_x += current_glyph->pixel_width;
3654 ++desired_glyph, ++current_glyph, ++i;
3655 skip_first_p = 0;
3658 if (i == start_hpos || x != current_x)
3660 i = start_hpos;
3661 x = start_x;
3662 desired_glyph = start;
3663 break;
3666 output_cursor_to (w, vpos, start_hpos, desired_row->y, start_x);
3667 rif->write_glyphs (w, updated_row, start,
3668 TEXT_AREA, i - start_hpos);
3669 changed_p = 1;
3673 /* Write the rest. */
3674 if (i < desired_row->used[TEXT_AREA])
3676 output_cursor_to (w, vpos, i, desired_row->y, x);
3677 rif->write_glyphs (w, updated_row, desired_glyph,
3678 TEXT_AREA, desired_row->used[TEXT_AREA] - i);
3679 changed_p = 1;
3682 /* Maybe clear to end of line. */
3683 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3685 /* If new row extends to the end of the text area, nothing
3686 has to be cleared, if and only if we did a write_glyphs
3687 above. This is made sure by setting desired_stop_pos
3688 appropriately above. */
3689 eassert (i < desired_row->used[TEXT_AREA]
3690 || ((desired_row->used[TEXT_AREA]
3691 == current_row->used[TEXT_AREA])
3692 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3694 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3696 /* If old row extends to the end of the text area, clear. */
3697 if (i >= desired_row->used[TEXT_AREA])
3698 output_cursor_to (w, vpos, i, desired_row->y,
3699 desired_row->pixel_width);
3700 rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
3701 changed_p = 1;
3703 else if (desired_row->pixel_width < current_row->pixel_width)
3705 /* Otherwise clear to the end of the old row. Everything
3706 after that position should be clear already. */
3707 int xlim;
3709 if (i >= desired_row->used[TEXT_AREA])
3710 output_cursor_to (w, vpos, i, desired_row->y,
3711 desired_row->pixel_width);
3713 /* If cursor is displayed at the end of the line, make sure
3714 it's cleared. Nowadays we don't have a phys_cursor_glyph
3715 with which to erase the cursor (because this method
3716 doesn't work with lbearing/rbearing), so we must do it
3717 this way. */
3718 if (vpos == w->phys_cursor.vpos
3719 && (desired_row->reversed_p
3720 ? (w->phys_cursor.hpos < 0)
3721 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3723 w->phys_cursor_on_p = 0;
3724 xlim = -1;
3726 else
3727 xlim = current_row->pixel_width;
3728 rif->clear_end_of_line (w, updated_row, TEXT_AREA, xlim);
3729 changed_p = 1;
3733 return changed_p;
3737 /* Update row VPOS in window W. Value is true if display has been changed. */
3739 static bool
3740 update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
3742 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3743 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3744 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3745 bool changed_p = 0;
3747 /* A row can be completely invisible in case a desired matrix was
3748 built with a vscroll and then make_cursor_line_fully_visible shifts
3749 the matrix. Make sure to make such rows current anyway, since
3750 we need the correct y-position, for example, in the current matrix. */
3751 if (desired_row->mode_line_p
3752 || desired_row->visible_height > 0)
3754 eassert (desired_row->enabled_p);
3756 /* Update display of the left margin area, if there is one. */
3757 if (!desired_row->full_width_p && w->left_margin_cols > 0)
3759 changed_p = 1;
3760 update_marginal_area (w, desired_row, LEFT_MARGIN_AREA, vpos);
3761 /* Setting this flag will ensure the vertical border, if
3762 any, between this window and the one on its left will be
3763 redrawn. This is necessary because updating the left
3764 margin area can potentially draw over the border. */
3765 current_row->redraw_fringe_bitmaps_p = 1;
3768 /* Update the display of the text area. */
3769 if (update_text_area (w, desired_row, vpos))
3771 changed_p = 1;
3772 if (current_row->mouse_face_p)
3773 *mouse_face_overwritten_p = 1;
3776 /* Update display of the right margin area, if there is one. */
3777 if (!desired_row->full_width_p && w->right_margin_cols > 0)
3779 changed_p = 1;
3780 update_marginal_area (w, desired_row, RIGHT_MARGIN_AREA, vpos);
3783 /* Draw truncation marks etc. */
3784 if (!current_row->enabled_p
3785 || desired_row->y != current_row->y
3786 || desired_row->visible_height != current_row->visible_height
3787 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
3788 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
3789 || current_row->redraw_fringe_bitmaps_p
3790 || desired_row->mode_line_p != current_row->mode_line_p
3791 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
3792 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
3793 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
3794 rif->after_update_window_line_hook (w, desired_row);
3797 /* Update current_row from desired_row. */
3798 make_current (w->desired_matrix, w->current_matrix, vpos);
3799 return changed_p;
3803 /* Set the cursor after an update of window W. This function may only
3804 be called from update_window. */
3806 static void
3807 set_window_cursor_after_update (struct window *w)
3809 struct frame *f = XFRAME (w->frame);
3810 int cx, cy, vpos, hpos;
3812 /* Not intended for frame matrix updates. */
3813 eassert (FRAME_WINDOW_P (f));
3815 if (cursor_in_echo_area
3816 && !NILP (echo_area_buffer[0])
3817 /* If we are showing a message instead of the mini-buffer,
3818 show the cursor for the message instead. */
3819 && XWINDOW (minibuf_window) == w
3820 && EQ (minibuf_window, echo_area_window)
3821 /* These cases apply only to the frame that contains
3822 the active mini-buffer window. */
3823 && FRAME_HAS_MINIBUF_P (f)
3824 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
3826 cx = cy = vpos = hpos = 0;
3828 if (cursor_in_echo_area >= 0)
3830 /* If the mini-buffer is several lines high, find the last
3831 line that has any text on it. Note: either all lines
3832 are enabled or none. Otherwise we wouldn't be able to
3833 determine Y. */
3834 struct glyph_row *row, *last_row;
3835 struct glyph *glyph;
3836 int yb = window_text_bottom_y (w);
3838 last_row = NULL;
3839 row = w->current_matrix->rows;
3840 while (row->enabled_p
3841 && (last_row == NULL
3842 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
3844 if (row->used[TEXT_AREA]
3845 && row->glyphs[TEXT_AREA][0].charpos >= 0)
3846 last_row = row;
3847 ++row;
3850 if (last_row)
3852 struct glyph *start = last_row->glyphs[TEXT_AREA];
3853 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
3855 while (last > start && last->charpos < 0)
3856 --last;
3858 for (glyph = start; glyph < last; ++glyph)
3860 cx += glyph->pixel_width;
3861 ++hpos;
3864 cy = last_row->y;
3865 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
3869 else
3871 cx = w->cursor.x;
3872 cy = w->cursor.y;
3873 hpos = w->cursor.hpos;
3874 vpos = w->cursor.vpos;
3877 /* Window cursor can be out of sync for horizontally split windows.
3878 Horizontal position is -1 when cursor is on the left fringe. */
3879 hpos = clip_to_bounds (-1, hpos, w->current_matrix->matrix_w - 1);
3880 vpos = clip_to_bounds (0, vpos, w->current_matrix->nrows - 1);
3881 output_cursor_to (w, vpos, hpos, cy, cx);
3885 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
3886 tree rooted at W. */
3888 void
3889 set_window_update_flags (struct window *w, bool on_p)
3891 while (w)
3893 if (WINDOWP (w->contents))
3894 set_window_update_flags (XWINDOW (w->contents), on_p);
3895 else
3896 w->must_be_updated_p = on_p;
3898 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3904 /***********************************************************************
3905 Window-Based Scrolling
3906 ***********************************************************************/
3908 /* Structure describing rows in scrolling_window. */
3910 struct row_entry
3912 /* Number of occurrences of this row in desired and current matrix. */
3913 int old_uses, new_uses;
3915 /* Vpos of row in new matrix. */
3916 int new_line_number;
3918 /* Bucket index of this row_entry in the hash table row_table. */
3919 ptrdiff_t bucket;
3921 /* The row described by this entry. */
3922 struct glyph_row *row;
3924 /* Hash collision chain. */
3925 struct row_entry *next;
3928 /* A pool to allocate row_entry structures from, and the size of the
3929 pool. The pool is reallocated in scrolling_window when we find
3930 that we need a larger one. */
3932 static struct row_entry *row_entry_pool;
3933 static ptrdiff_t row_entry_pool_size;
3935 /* Index of next free entry in row_entry_pool. */
3937 static ptrdiff_t row_entry_idx;
3939 /* The hash table used during scrolling, and the table's size. This
3940 table is used to quickly identify equal rows in the desired and
3941 current matrix. */
3943 static struct row_entry **row_table;
3944 static ptrdiff_t row_table_size;
3946 /* Vectors of pointers to row_entry structures belonging to the
3947 current and desired matrix, and the size of the vectors. */
3949 static struct row_entry **old_lines, **new_lines;
3950 static ptrdiff_t old_lines_size, new_lines_size;
3952 /* A pool to allocate run structures from, and its size. */
3954 static struct run *run_pool;
3955 static ptrdiff_t runs_size;
3957 /* A vector of runs of lines found during scrolling. */
3959 static struct run **runs;
3961 /* Add glyph row ROW to the scrolling hash table. */
3963 static struct row_entry *
3964 add_row_entry (struct glyph_row *row)
3966 struct row_entry *entry;
3967 ptrdiff_t i = row->hash % row_table_size;
3969 entry = row_table[i];
3970 eassert (entry || verify_row_hash (row));
3971 while (entry && !row_equal_p (entry->row, row, 1))
3972 entry = entry->next;
3974 if (entry == NULL)
3976 entry = row_entry_pool + row_entry_idx++;
3977 entry->row = row;
3978 entry->old_uses = entry->new_uses = 0;
3979 entry->new_line_number = 0;
3980 entry->bucket = i;
3981 entry->next = row_table[i];
3982 row_table[i] = entry;
3985 return entry;
3989 /* Try to reuse part of the current display of W by scrolling lines.
3990 HEADER_LINE_P means W has a header line.
3992 The algorithm is taken from Communications of the ACM, Apr78 "A
3993 Technique for Isolating Differences Between Files." It should take
3994 O(N) time.
3996 A short outline of the steps of the algorithm
3998 1. Skip lines equal at the start and end of both matrices.
4000 2. Enter rows in the current and desired matrix into a symbol
4001 table, counting how often they appear in both matrices.
4003 3. Rows that appear exactly once in both matrices serve as anchors,
4004 i.e. we assume that such lines are likely to have been moved.
4006 4. Starting from anchor lines, extend regions to be scrolled both
4007 forward and backward.
4009 Value is
4011 -1 if all rows were found to be equal.
4012 0 to indicate that we did not scroll the display, or
4013 1 if we did scroll. */
4015 static int
4016 scrolling_window (struct window *w, bool header_line_p)
4018 struct glyph_matrix *desired_matrix = w->desired_matrix;
4019 struct glyph_matrix *current_matrix = w->current_matrix;
4020 int yb = window_text_bottom_y (w);
4021 ptrdiff_t i;
4022 int j, first_old, first_new, last_old, last_new;
4023 int nruns, run_idx;
4024 ptrdiff_t n;
4025 struct row_entry *entry;
4026 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4028 /* Skip over rows equal at the start. */
4029 for (i = header_line_p; i < current_matrix->nrows - 1; ++i)
4031 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4032 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4034 if (c->enabled_p
4035 && d->enabled_p
4036 && !d->redraw_fringe_bitmaps_p
4037 && c->y == d->y
4038 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4039 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4040 && row_equal_p (c, d, 1))
4042 assign_row (c, d);
4043 d->enabled_p = 0;
4045 else
4046 break;
4049 #ifdef HAVE_XWIDGETS
4050 //currently this is needed to detect xwidget movement reliably. or probably not.
4051 //printf("scrolling_window\n");
4052 return 0;
4053 #endif
4055 /* Give up if some rows in the desired matrix are not enabled. */
4056 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4057 return -1;
4059 first_old = first_new = i;
4061 /* Set last_new to the index + 1 of the row that reaches the
4062 bottom boundary in the desired matrix. Give up if we find a
4063 disabled row before we reach the bottom boundary. */
4064 i = first_new + 1;
4065 while (i < desired_matrix->nrows - 1)
4067 int bottom;
4069 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4070 return 0;
4071 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4072 if (bottom <= yb)
4073 ++i;
4074 if (bottom >= yb)
4075 break;
4078 last_new = i;
4080 /* Set last_old to the index + 1 of the row that reaches the bottom
4081 boundary in the current matrix. We don't look at the enabled
4082 flag here because we plan to reuse part of the display even if
4083 other parts are disabled. */
4084 i = first_old + 1;
4085 while (i < current_matrix->nrows - 1)
4087 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4088 if (bottom <= yb)
4089 ++i;
4090 if (bottom >= yb)
4091 break;
4094 last_old = i;
4096 /* Skip over rows equal at the bottom. */
4097 i = last_new;
4098 j = last_old;
4099 while (i - 1 > first_new
4100 && j - 1 > first_old
4101 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4102 && (MATRIX_ROW (current_matrix, j - 1)->y
4103 == MATRIX_ROW (desired_matrix, i - 1)->y)
4104 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4105 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4106 MATRIX_ROW (current_matrix, j - 1), 1))
4107 --i, --j;
4108 last_new = i;
4109 last_old = j;
4111 /* Nothing to do if all rows are equal. */
4112 if (last_new == first_new)
4113 return 0;
4115 /* Check for integer overflow in size calculation.
4117 If next_almost_prime checks (N) for divisibility by 2..10, then
4118 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4119 So, set next_almost_prime_increment_max to 10.
4121 It's just a coincidence that next_almost_prime_increment_max ==
4122 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4123 13, then next_almost_prime_increment_max would be 14, e.g.,
4124 because next_almost_prime (113) would be 127. */
4126 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4127 enum { next_almost_prime_increment_max = 10 };
4128 ptrdiff_t row_table_max =
4129 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4130 - next_almost_prime_increment_max);
4131 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4132 if (current_nrows_max < current_matrix->nrows)
4133 memory_full (SIZE_MAX);
4136 /* Reallocate vectors, tables etc. if necessary. */
4138 if (current_matrix->nrows > old_lines_size)
4139 old_lines = xpalloc (old_lines, &old_lines_size,
4140 current_matrix->nrows - old_lines_size,
4141 INT_MAX, sizeof *old_lines);
4143 if (desired_matrix->nrows > new_lines_size)
4144 new_lines = xpalloc (new_lines, &new_lines_size,
4145 desired_matrix->nrows - new_lines_size,
4146 INT_MAX, sizeof *new_lines);
4148 n = desired_matrix->nrows;
4149 n += current_matrix->nrows;
4150 if (row_table_size < 3 * n)
4152 ptrdiff_t size = next_almost_prime (3 * n);
4153 row_table = xnrealloc (row_table, size, sizeof *row_table);
4154 row_table_size = size;
4155 memset (row_table, 0, size * sizeof *row_table);
4158 if (n > row_entry_pool_size)
4159 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4160 n - row_entry_pool_size,
4161 -1, sizeof *row_entry_pool);
4163 if (desired_matrix->nrows > runs_size)
4165 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4166 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4167 runs_size = desired_matrix->nrows;
4170 nruns = run_idx = 0;
4171 row_entry_idx = 0;
4173 /* Add rows from the current and desired matrix to the hash table
4174 row_hash_table to be able to find equal ones quickly. */
4176 for (i = first_old; i < last_old; ++i)
4178 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4180 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4181 old_lines[i] = entry;
4182 ++entry->old_uses;
4184 else
4185 old_lines[i] = NULL;
4188 for (i = first_new; i < last_new; ++i)
4190 eassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4191 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4192 ++entry->new_uses;
4193 entry->new_line_number = i;
4194 new_lines[i] = entry;
4197 /* Identify moves based on lines that are unique and equal
4198 in both matrices. */
4199 for (i = first_old; i < last_old;)
4200 if (old_lines[i]
4201 && old_lines[i]->old_uses == 1
4202 && old_lines[i]->new_uses == 1)
4204 int p, q;
4205 int new_line = old_lines[i]->new_line_number;
4206 struct run *run = run_pool + run_idx++;
4208 /* Record move. */
4209 run->current_vpos = i;
4210 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4211 run->desired_vpos = new_line;
4212 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4213 run->nrows = 1;
4214 run->height = MATRIX_ROW (current_matrix, i)->height;
4216 /* Extend backward. */
4217 p = i - 1;
4218 q = new_line - 1;
4219 while (p > first_old
4220 && q > first_new
4221 && old_lines[p] == new_lines[q])
4223 int h = MATRIX_ROW (current_matrix, p)->height;
4224 --run->current_vpos;
4225 --run->desired_vpos;
4226 ++run->nrows;
4227 run->height += h;
4228 run->desired_y -= h;
4229 run->current_y -= h;
4230 --p, --q;
4233 /* Extend forward. */
4234 p = i + 1;
4235 q = new_line + 1;
4236 while (p < last_old
4237 && q < last_new
4238 && old_lines[p] == new_lines[q])
4240 int h = MATRIX_ROW (current_matrix, p)->height;
4241 ++run->nrows;
4242 run->height += h;
4243 ++p, ++q;
4246 /* Insert run into list of all runs. Order runs by copied
4247 pixel lines. Note that we record runs that don't have to
4248 be copied because they are already in place. This is done
4249 because we can avoid calling update_window_line in this
4250 case. */
4251 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4253 for (q = nruns; q > p; --q)
4254 runs[q] = runs[q - 1];
4255 runs[p] = run;
4256 ++nruns;
4258 i += run->nrows;
4260 else
4261 ++i;
4263 /* Do the moves. Do it in a way that we don't overwrite something
4264 we want to copy later on. This is not solvable in general
4265 because there is only one display and we don't have a way to
4266 exchange areas on this display. Example:
4268 +-----------+ +-----------+
4269 | A | | B |
4270 +-----------+ --> +-----------+
4271 | B | | A |
4272 +-----------+ +-----------+
4274 Instead, prefer bigger moves, and invalidate moves that would
4275 copy from where we copied to. */
4277 for (i = 0; i < nruns; ++i)
4278 if (runs[i]->nrows > 0)
4280 struct run *r = runs[i];
4282 /* Copy on the display. */
4283 if (r->current_y != r->desired_y)
4285 rif->clear_window_mouse_face (w);
4286 rif->scroll_run_hook (w, r);
4289 /* Truncate runs that copy to where we copied to, and
4290 invalidate runs that copy from where we copied to. */
4291 for (j = nruns - 1; j > i; --j)
4293 struct run *p = runs[j];
4294 bool truncated_p = 0;
4296 if (p->nrows > 0
4297 && p->desired_y < r->desired_y + r->height
4298 && p->desired_y + p->height > r->desired_y)
4300 if (p->desired_y < r->desired_y)
4302 p->nrows = r->desired_vpos - p->desired_vpos;
4303 p->height = r->desired_y - p->desired_y;
4304 truncated_p = 1;
4306 else
4308 int nrows_copied = (r->desired_vpos + r->nrows
4309 - p->desired_vpos);
4311 if (p->nrows <= nrows_copied)
4312 p->nrows = 0;
4313 else
4315 int height_copied = (r->desired_y + r->height
4316 - p->desired_y);
4318 p->current_vpos += nrows_copied;
4319 p->desired_vpos += nrows_copied;
4320 p->nrows -= nrows_copied;
4321 p->current_y += height_copied;
4322 p->desired_y += height_copied;
4323 p->height -= height_copied;
4324 truncated_p = 1;
4329 if (r->current_y != r->desired_y
4330 /* The condition below is equivalent to
4331 ((p->current_y >= r->desired_y
4332 && p->current_y < r->desired_y + r->height)
4333 || (p->current_y + p->height > r->desired_y
4334 && (p->current_y + p->height
4335 <= r->desired_y + r->height)))
4336 because we have 0 < p->height <= r->height. */
4337 && p->current_y < r->desired_y + r->height
4338 && p->current_y + p->height > r->desired_y)
4339 p->nrows = 0;
4341 /* Reorder runs by copied pixel lines if truncated. */
4342 if (truncated_p && p->nrows > 0)
4344 int k = nruns - 1;
4346 while (runs[k]->nrows == 0 || runs[k]->height < p->height)
4347 k--;
4348 memmove (runs + j, runs + j + 1, (k - j) * sizeof (*runs));
4349 runs[k] = p;
4353 /* Assign matrix rows. */
4354 for (j = 0; j < r->nrows; ++j)
4356 struct glyph_row *from, *to;
4357 bool to_overlapped_p;
4359 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4360 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4361 to_overlapped_p = to->overlapped_p;
4362 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4363 assign_row (to, from);
4364 /* The above `assign_row' actually does swap, so if we had
4365 an overlap in the copy destination of two runs, then
4366 the second run would assign a previously disabled bogus
4367 row. But thanks to the truncation code in the
4368 preceding for-loop, we no longer have such an overlap,
4369 and thus the assigned row should always be enabled. */
4370 eassert (to->enabled_p);
4371 from->enabled_p = 0;
4372 to->overlapped_p = to_overlapped_p;
4376 /* Clear the hash table, for the next time. */
4377 for (i = 0; i < row_entry_idx; ++i)
4378 row_table[row_entry_pool[i].bucket] = NULL;
4380 /* Value is 1 to indicate that we scrolled the display. */
4381 return nruns > 0;
4386 /************************************************************************
4387 Frame-Based Updates
4388 ************************************************************************/
4390 /* Update the desired frame matrix of frame F.
4392 FORCE_P means that the update should not be stopped by pending input.
4393 INHIBIT_HAIRY_ID_P means that scrolling should not be tried.
4395 Value is true if update was stopped due to pending input. */
4397 static bool
4398 update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p)
4400 /* Frame matrices to work on. */
4401 struct glyph_matrix *current_matrix = f->current_matrix;
4402 struct glyph_matrix *desired_matrix = f->desired_matrix;
4403 int i;
4404 bool pause_p;
4405 int preempt_count = baud_rate / 2400 + 1;
4407 eassert (current_matrix && desired_matrix);
4409 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4410 calculate_costs (f);
4412 if (preempt_count <= 0)
4413 preempt_count = 1;
4415 if (!force_p && detect_input_pending_ignore_squeezables ())
4417 pause_p = 1;
4418 goto do_pause;
4421 /* If we cannot insert/delete lines, it's no use trying it. */
4422 if (!FRAME_LINE_INS_DEL_OK (f))
4423 inhibit_id_p = 1;
4425 /* See if any of the desired lines are enabled; don't compute for
4426 i/d line if just want cursor motion. */
4427 for (i = 0; i < desired_matrix->nrows; i++)
4428 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4429 break;
4431 /* Try doing i/d line, if not yet inhibited. */
4432 if (!inhibit_id_p && i < desired_matrix->nrows)
4433 force_p |= scrolling (f);
4435 /* Update the individual lines as needed. Do bottom line first. */
4436 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4437 update_frame_line (f, desired_matrix->nrows - 1);
4439 /* Now update the rest of the lines. */
4440 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4442 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4444 if (FRAME_TERMCAP_P (f))
4446 /* Flush out every so many lines.
4447 Also flush out if likely to have more than 1k buffered
4448 otherwise. I'm told that some telnet connections get
4449 really screwed by more than 1k output at once. */
4450 FILE *display_output = FRAME_TTY (f)->output;
4451 if (display_output)
4453 ptrdiff_t outq = __fpending (display_output);
4454 if (outq > 900
4455 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4456 fflush (display_output);
4460 if (!force_p && (i - 1) % preempt_count == 0)
4461 detect_input_pending_ignore_squeezables ();
4463 update_frame_line (f, i);
4467 lint_assume (0 <= FRAME_LINES (f));
4468 pause_p = 0 < i && i < FRAME_LINES (f) - 1;
4470 /* Now just clean up termcap drivers and set cursor, etc. */
4471 if (!pause_p)
4473 if ((cursor_in_echo_area
4474 /* If we are showing a message instead of the mini-buffer,
4475 show the cursor for the message instead of for the
4476 (now hidden) mini-buffer contents. */
4477 || (EQ (minibuf_window, selected_window)
4478 && EQ (minibuf_window, echo_area_window)
4479 && !NILP (echo_area_buffer[0])))
4480 /* These cases apply only to the frame that contains
4481 the active mini-buffer window. */
4482 && FRAME_HAS_MINIBUF_P (f)
4483 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4485 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4486 int row, col;
4488 if (cursor_in_echo_area < 0)
4490 /* Negative value of cursor_in_echo_area means put
4491 cursor at beginning of line. */
4492 row = top;
4493 col = 0;
4495 else
4497 /* Positive value of cursor_in_echo_area means put
4498 cursor at the end of the prompt. If the mini-buffer
4499 is several lines high, find the last line that has
4500 any text on it. */
4501 row = FRAME_LINES (f);
4504 --row;
4505 col = 0;
4507 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4509 /* Frame rows are filled up with spaces that
4510 must be ignored here. */
4511 struct glyph_row *r = MATRIX_ROW (current_matrix,
4512 row);
4513 struct glyph *start = r->glyphs[TEXT_AREA];
4514 struct glyph *last = start + r->used[TEXT_AREA];
4516 while (last > start
4517 && (last - 1)->charpos < 0)
4518 --last;
4520 col = last - start;
4523 while (row > top && col == 0);
4525 /* Make sure COL is not out of range. */
4526 if (col >= FRAME_CURSOR_X_LIMIT (f))
4528 /* If we have another row, advance cursor into it. */
4529 if (row < FRAME_LINES (f) - 1)
4531 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4532 row++;
4534 /* Otherwise move it back in range. */
4535 else
4536 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4540 cursor_to (f, row, col);
4542 else
4544 /* We have only one cursor on terminal frames. Use it to
4545 display the cursor of the selected window. */
4546 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4547 if (w->cursor.vpos >= 0
4548 /* The cursor vpos may be temporarily out of bounds
4549 in the following situation: There is one window,
4550 with the cursor in the lower half of it. The window
4551 is split, and a message causes a redisplay before
4552 a new cursor position has been computed. */
4553 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4555 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4556 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4558 x += max (0, w->left_margin_cols);
4559 cursor_to (f, y, x);
4564 do_pause:
4566 clear_desired_matrices (f);
4567 return pause_p;
4571 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4573 static bool
4574 scrolling (struct frame *frame)
4576 int unchanged_at_top, unchanged_at_bottom;
4577 int window_size;
4578 int changed_lines;
4579 int *old_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4580 int *new_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4581 int *draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4582 int *old_draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4583 register int i;
4584 int free_at_end_vpos = FRAME_LINES (frame);
4585 struct glyph_matrix *current_matrix = frame->current_matrix;
4586 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4588 if (!current_matrix)
4589 emacs_abort ();
4591 /* Compute hash codes of all the lines. Also calculate number of
4592 changed lines, number of unchanged lines at the beginning, and
4593 number of unchanged lines at the end. */
4594 changed_lines = 0;
4595 unchanged_at_top = 0;
4596 unchanged_at_bottom = FRAME_LINES (frame);
4597 for (i = 0; i < FRAME_LINES (frame); i++)
4599 /* Give up on this scrolling if some old lines are not enabled. */
4600 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4601 return 0;
4602 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4603 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4605 /* This line cannot be redrawn, so don't let scrolling mess it. */
4606 new_hash[i] = old_hash[i];
4607 #define INFINITY 1000000 /* Taken from scroll.c */
4608 draw_cost[i] = INFINITY;
4610 else
4612 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4613 draw_cost[i] = line_draw_cost (desired_matrix, i);
4616 if (old_hash[i] != new_hash[i])
4618 changed_lines++;
4619 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4621 else if (i == unchanged_at_top)
4622 unchanged_at_top++;
4623 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4626 /* If changed lines are few, don't allow preemption, don't scroll. */
4627 if ((!FRAME_SCROLL_REGION_OK (frame)
4628 && changed_lines < baud_rate / 2400)
4629 || unchanged_at_bottom == FRAME_LINES (frame))
4630 return 1;
4632 window_size = (FRAME_LINES (frame) - unchanged_at_top
4633 - unchanged_at_bottom);
4635 if (FRAME_SCROLL_REGION_OK (frame))
4636 free_at_end_vpos -= unchanged_at_bottom;
4637 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4638 free_at_end_vpos = -1;
4640 /* If large window, fast terminal and few lines in common between
4641 current frame and desired frame, don't bother with i/d calc. */
4642 if (!FRAME_SCROLL_REGION_OK (frame)
4643 && window_size >= 18 && baud_rate > 2400
4644 && (window_size >=
4645 10 * scrolling_max_lines_saved (unchanged_at_top,
4646 FRAME_LINES (frame) - unchanged_at_bottom,
4647 old_hash, new_hash, draw_cost)))
4648 return 0;
4650 if (window_size < 2)
4651 return 0;
4653 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4654 draw_cost + unchanged_at_top - 1,
4655 old_draw_cost + unchanged_at_top - 1,
4656 old_hash + unchanged_at_top - 1,
4657 new_hash + unchanged_at_top - 1,
4658 free_at_end_vpos - unchanged_at_top);
4660 return 0;
4664 /* Count the number of blanks at the start of the vector of glyphs R
4665 which is LEN glyphs long. */
4667 static int
4668 count_blanks (struct glyph *r, int len)
4670 int i;
4672 for (i = 0; i < len; ++i)
4673 if (!CHAR_GLYPH_SPACE_P (r[i]))
4674 break;
4676 return i;
4680 /* Count the number of glyphs in common at the start of the glyph
4681 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4682 of STR2. Value is the number of equal glyphs equal at the start. */
4684 static int
4685 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4687 struct glyph *p1 = str1;
4688 struct glyph *p2 = str2;
4690 while (p1 < end1
4691 && p2 < end2
4692 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4693 ++p1, ++p2;
4695 return p1 - str1;
4699 /* Char insertion/deletion cost vector, from term.c */
4701 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4704 /* Perform a frame-based update on line VPOS in frame FRAME. */
4706 static void
4707 update_frame_line (struct frame *f, int vpos)
4709 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4710 int tem;
4711 int osp, nsp, begmatch, endmatch, olen, nlen;
4712 struct glyph_matrix *current_matrix = f->current_matrix;
4713 struct glyph_matrix *desired_matrix = f->desired_matrix;
4714 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4715 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4716 bool must_write_whole_line_p;
4717 bool write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4718 bool colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4719 != FACE_TTY_DEFAULT_BG_COLOR);
4721 if (colored_spaces_p)
4722 write_spaces_p = 1;
4724 /* Current row not enabled means it has unknown contents. We must
4725 write the whole desired line in that case. */
4726 must_write_whole_line_p = !current_row->enabled_p;
4727 if (must_write_whole_line_p)
4729 obody = 0;
4730 olen = 0;
4732 else
4734 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4735 olen = current_row->used[TEXT_AREA];
4737 /* Ignore trailing spaces, if we can. */
4738 if (!write_spaces_p)
4739 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4740 olen--;
4743 current_row->enabled_p = 1;
4744 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4746 /* If desired line is empty, just clear the line. */
4747 if (!desired_row->enabled_p)
4749 nlen = 0;
4750 goto just_erase;
4753 nbody = desired_row->glyphs[TEXT_AREA];
4754 nlen = desired_row->used[TEXT_AREA];
4755 nend = nbody + nlen;
4757 /* If display line has unknown contents, write the whole line. */
4758 if (must_write_whole_line_p)
4760 /* Ignore spaces at the end, if we can. */
4761 if (!write_spaces_p)
4762 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4763 --nlen;
4765 /* Write the contents of the desired line. */
4766 if (nlen)
4768 cursor_to (f, vpos, 0);
4769 write_glyphs (f, nbody, nlen);
4772 /* Don't call clear_end_of_line if we already wrote the whole
4773 line. The cursor will not be at the right margin in that
4774 case but in the line below. */
4775 if (nlen < FRAME_TOTAL_COLS (f))
4777 cursor_to (f, vpos, nlen);
4778 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
4780 else
4781 /* Make sure we are in the right row, otherwise cursor movement
4782 with cmgoto might use `ch' in the wrong row. */
4783 cursor_to (f, vpos, 0);
4785 make_current (desired_matrix, current_matrix, vpos);
4786 return;
4789 /* Pretend trailing spaces are not there at all,
4790 unless for one reason or another we must write all spaces. */
4791 if (!write_spaces_p)
4792 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4793 nlen--;
4795 /* If there's no i/d char, quickly do the best we can without it. */
4796 if (!FRAME_CHAR_INS_DEL_OK (f))
4798 int i, j;
4800 /* Find the first glyph in desired row that doesn't agree with
4801 a glyph in the current row, and write the rest from there on. */
4802 for (i = 0; i < nlen; i++)
4804 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
4806 /* Find the end of the run of different glyphs. */
4807 j = i + 1;
4808 while (j < nlen
4809 && (j >= olen
4810 || !GLYPH_EQUAL_P (nbody + j, obody + j)
4811 || CHAR_GLYPH_PADDING_P (nbody[j])))
4812 ++j;
4814 /* Output this run of non-matching chars. */
4815 cursor_to (f, vpos, i);
4816 write_glyphs (f, nbody + i, j - i);
4817 i = j - 1;
4819 /* Now find the next non-match. */
4823 /* Clear the rest of the line, or the non-clear part of it. */
4824 if (olen > nlen)
4826 cursor_to (f, vpos, nlen);
4827 clear_end_of_line (f, olen);
4830 /* Make current row = desired row. */
4831 make_current (desired_matrix, current_matrix, vpos);
4832 return;
4835 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
4836 characters in a row. */
4838 if (!olen)
4840 /* If current line is blank, skip over initial spaces, if
4841 possible, and write the rest. */
4842 if (write_spaces_p)
4843 nsp = 0;
4844 else
4845 nsp = count_blanks (nbody, nlen);
4847 if (nlen > nsp)
4849 cursor_to (f, vpos, nsp);
4850 write_glyphs (f, nbody + nsp, nlen - nsp);
4853 /* Exchange contents between current_frame and new_frame. */
4854 make_current (desired_matrix, current_matrix, vpos);
4855 return;
4858 /* Compute number of leading blanks in old and new contents. */
4859 osp = count_blanks (obody, olen);
4860 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
4862 /* Compute number of matching chars starting with first non-blank. */
4863 begmatch = count_match (obody + osp, obody + olen,
4864 nbody + nsp, nbody + nlen);
4866 /* Spaces in new match implicit space past the end of old. */
4867 /* A bug causing this to be a no-op was fixed in 18.29. */
4868 if (!write_spaces_p && osp + begmatch == olen)
4870 np1 = nbody + nsp;
4871 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
4872 ++begmatch;
4875 /* Avoid doing insert/delete char
4876 just cause number of leading spaces differs
4877 when the following text does not match. */
4878 if (begmatch == 0 && osp != nsp)
4879 osp = nsp = min (osp, nsp);
4881 /* Find matching characters at end of line */
4882 op1 = obody + olen;
4883 np1 = nbody + nlen;
4884 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
4885 while (op1 > op2
4886 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
4888 op1--;
4889 np1--;
4891 endmatch = obody + olen - op1;
4893 /* tem gets the distance to insert or delete.
4894 endmatch is how many characters we save by doing so.
4895 Is it worth it? */
4897 tem = (nlen - nsp) - (olen - osp);
4898 if (endmatch && tem
4899 && (!FRAME_CHAR_INS_DEL_OK (f)
4900 || endmatch <= char_ins_del_cost (f)[tem]))
4901 endmatch = 0;
4903 /* nsp - osp is the distance to insert or delete.
4904 If that is nonzero, begmatch is known to be nonzero also.
4905 begmatch + endmatch is how much we save by doing the ins/del.
4906 Is it worth it? */
4908 if (nsp != osp
4909 && (!FRAME_CHAR_INS_DEL_OK (f)
4910 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
4912 begmatch = 0;
4913 endmatch = 0;
4914 osp = nsp = min (osp, nsp);
4917 /* Now go through the line, inserting, writing and
4918 deleting as appropriate. */
4920 if (osp > nsp)
4922 cursor_to (f, vpos, nsp);
4923 delete_glyphs (f, osp - nsp);
4925 else if (nsp > osp)
4927 /* If going to delete chars later in line
4928 and insert earlier in the line,
4929 must delete first to avoid losing data in the insert */
4930 if (endmatch && nlen < olen + nsp - osp)
4932 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
4933 delete_glyphs (f, olen + nsp - osp - nlen);
4934 olen = nlen - (nsp - osp);
4936 cursor_to (f, vpos, osp);
4937 insert_glyphs (f, 0, nsp - osp);
4939 olen += nsp - osp;
4941 tem = nsp + begmatch + endmatch;
4942 if (nlen != tem || olen != tem)
4944 if (!endmatch || nlen == olen)
4946 /* If new text being written reaches right margin, there is
4947 no need to do clear-to-eol at the end of this function
4948 (and it would not be safe, since cursor is not going to
4949 be "at the margin" after the text is done). */
4950 if (nlen == FRAME_TOTAL_COLS (f))
4951 olen = 0;
4953 /* Function write_glyphs is prepared to do nothing
4954 if passed a length <= 0. Check it here to avoid
4955 unnecessary cursor movement. */
4956 if (nlen - tem > 0)
4958 cursor_to (f, vpos, nsp + begmatch);
4959 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
4962 else if (nlen > olen)
4964 /* Here, we used to have the following simple code:
4965 ----------------------------------------
4966 write_glyphs (nbody + nsp + begmatch, olen - tem);
4967 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
4968 ----------------------------------------
4969 but it doesn't work if nbody[nsp + begmatch + olen - tem]
4970 is a padding glyph. */
4971 int out = olen - tem; /* Columns to be overwritten originally. */
4972 int del;
4974 cursor_to (f, vpos, nsp + begmatch);
4976 /* Calculate columns we can actually overwrite. */
4977 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
4978 out--;
4979 write_glyphs (f, nbody + nsp + begmatch, out);
4981 /* If we left columns to be overwritten, we must delete them. */
4982 del = olen - tem - out;
4983 if (del > 0)
4984 delete_glyphs (f, del);
4986 /* At last, we insert columns not yet written out. */
4987 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
4988 olen = nlen;
4990 else if (olen > nlen)
4992 cursor_to (f, vpos, nsp + begmatch);
4993 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
4994 delete_glyphs (f, olen - nlen);
4995 olen = nlen;
4999 just_erase:
5000 /* If any unerased characters remain after the new line, erase them. */
5001 if (olen > nlen)
5003 cursor_to (f, vpos, nlen);
5004 clear_end_of_line (f, olen);
5007 /* Exchange contents between current_frame and new_frame. */
5008 make_current (desired_matrix, current_matrix, vpos);
5013 /***********************************************************************
5014 X/Y Position -> Buffer Position
5015 ***********************************************************************/
5017 /* Determine what's under window-relative pixel position (*X, *Y).
5018 Return the OBJECT (string or buffer) that's there.
5019 Return in *POS the position in that object.
5020 Adjust *X and *Y to character positions.
5021 Return in *DX and *DY the pixel coordinates of the click,
5022 relative to the top left corner of OBJECT, or relative to
5023 the top left corner of the character glyph at (*X, *Y)
5024 if OBJECT is nil.
5025 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5026 if the coordinates point to an empty area of the display. */
5028 Lisp_Object
5029 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)
5031 struct it it;
5032 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5033 struct text_pos startp;
5034 Lisp_Object string;
5035 struct glyph_row *row;
5036 #ifdef HAVE_WINDOW_SYSTEM
5037 struct image *img = 0;
5038 #endif
5039 int x0, x1, to_x;
5040 void *itdata = NULL;
5042 /* We used to set current_buffer directly here, but that does the
5043 wrong thing with `face-remapping-alist' (bug#2044). */
5044 Fset_buffer (w->contents);
5045 itdata = bidi_shelve_cache ();
5046 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
5047 start_display (&it, w, startp);
5048 /* start_display takes into account the header-line row, but IT's
5049 vpos still counts from the glyph row that includes the window's
5050 start position. Adjust for a possible header-line row. */
5051 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w);
5053 x0 = *x;
5055 /* First, move to the beginning of the row corresponding to *Y. We
5056 need to be in that row to get the correct value of base paragraph
5057 direction for the text at (*X, *Y). */
5058 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5060 /* TO_X is the pixel position that the iterator will compute for the
5061 glyph at *X. We add it.first_visible_x because iterator
5062 positions include the hscroll. */
5063 to_x = x0 + it.first_visible_x;
5064 if (it.bidi_it.paragraph_dir == R2L)
5065 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5066 text area. This is because the iterator, even in R2L
5067 paragraphs, delivers glyphs as if they started at the left
5068 margin of the window. (When we actually produce glyphs for
5069 display, we reverse their order in PRODUCE_GLYPHS, but the
5070 iterator doesn't know about that.) The following line adjusts
5071 the pixel position to the iterator geometry, which is what
5072 move_it_* routines use. (The -1 is because in a window whose
5073 text-area width is W, the rightmost pixel position is W-1, and
5074 it should be mirrored into zero pixel position.) */
5075 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5077 /* Now move horizontally in the row to the glyph under *X. Second
5078 argument is ZV to prevent move_it_in_display_line from matching
5079 based on buffer positions. */
5080 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5081 bidi_unshelve_cache (itdata, 0);
5083 Fset_buffer (old_current_buffer);
5085 *dx = x0 + it.first_visible_x - it.current_x;
5086 *dy = *y - it.current_y;
5088 string = w->contents;
5089 if (STRINGP (it.string))
5090 string = it.string;
5091 *pos = it.current;
5092 if (it.what == IT_COMPOSITION
5093 && it.cmp_it.nchars > 1
5094 && it.cmp_it.reversed_p)
5096 /* The current display element is a grapheme cluster in a
5097 composition. In that case, we need the position of the first
5098 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5099 it.current points to the last character of the cluster, thus
5100 we must move back to the first character of the same
5101 cluster. */
5102 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5103 if (STRINGP (it.string))
5104 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5105 else
5106 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->contents),
5107 CHARPOS (pos->pos));
5110 #ifdef HAVE_WINDOW_SYSTEM
5111 if (it.what == IT_IMAGE)
5113 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5114 && !NILP (img->spec))
5115 *object = img->spec;
5117 #endif
5119 if (it.vpos < w->current_matrix->nrows
5120 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5121 row->enabled_p))
5123 if (it.hpos < row->used[TEXT_AREA])
5125 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5126 #ifdef HAVE_WINDOW_SYSTEM
5127 if (img)
5129 *dy -= row->ascent - glyph->ascent;
5130 *dx += glyph->slice.img.x;
5131 *dy += glyph->slice.img.y;
5132 /* Image slices positions are still relative to the entire image */
5133 *width = img->width;
5134 *height = img->height;
5136 else
5137 #endif
5139 *width = glyph->pixel_width;
5140 *height = glyph->ascent + glyph->descent;
5143 else
5145 *width = 0;
5146 *height = row->height;
5149 else
5151 *width = *height = 0;
5154 /* Add extra (default width) columns if clicked after EOL. */
5155 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5156 if (x0 > x1)
5157 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5159 *x = it.hpos;
5160 *y = it.vpos;
5162 return string;
5166 /* Value is the string under window-relative coordinates X/Y in the
5167 mode line or header line (PART says which) of window W, or nil if none.
5168 *CHARPOS is set to the position in the string returned. */
5170 Lisp_Object
5171 mode_line_string (struct window *w, enum window_part part,
5172 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5173 int *dx, int *dy, int *width, int *height)
5175 struct glyph_row *row;
5176 struct glyph *glyph, *end;
5177 int x0, y0;
5178 Lisp_Object string = Qnil;
5180 if (part == ON_MODE_LINE)
5181 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5182 else
5183 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5184 y0 = *y - row->y;
5185 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5187 if (row->mode_line_p && row->enabled_p)
5189 /* Find the glyph under X. If we find one with a string object,
5190 it's the one we were looking for. */
5191 glyph = row->glyphs[TEXT_AREA];
5192 end = glyph + row->used[TEXT_AREA];
5193 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5194 x0 -= glyph->pixel_width;
5195 *x = glyph - row->glyphs[TEXT_AREA];
5196 if (glyph < end)
5198 string = glyph->object;
5199 *charpos = glyph->charpos;
5200 *width = glyph->pixel_width;
5201 *height = glyph->ascent + glyph->descent;
5202 #ifdef HAVE_WINDOW_SYSTEM
5203 if (glyph->type == IMAGE_GLYPH)
5205 struct image *img;
5206 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5207 if (img != NULL)
5208 *object = img->spec;
5209 y0 -= row->ascent - glyph->ascent;
5211 #endif
5213 else
5215 /* Add extra (default width) columns if clicked after EOL. */
5216 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5217 *width = 0;
5218 *height = row->height;
5221 else
5223 *x = 0;
5224 x0 = 0;
5225 *width = *height = 0;
5228 *dx = x0;
5229 *dy = y0;
5231 return string;
5235 /* Value is the string under window-relative coordinates X/Y in either
5236 marginal area, or nil if none. *CHARPOS is set to the position in
5237 the string returned. */
5239 Lisp_Object
5240 marginal_area_string (struct window *w, enum window_part part,
5241 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5242 int *dx, int *dy, int *width, int *height)
5244 struct glyph_row *row = w->current_matrix->rows;
5245 struct glyph *glyph, *end;
5246 int x0, y0, i, wy = *y;
5247 int area;
5248 Lisp_Object string = Qnil;
5250 if (part == ON_LEFT_MARGIN)
5251 area = LEFT_MARGIN_AREA;
5252 else if (part == ON_RIGHT_MARGIN)
5253 area = RIGHT_MARGIN_AREA;
5254 else
5255 emacs_abort ();
5257 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5258 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5259 break;
5260 y0 = *y - row->y;
5261 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5263 if (row->enabled_p)
5265 /* Find the glyph under X. If we find one with a string object,
5266 it's the one we were looking for. */
5267 if (area == RIGHT_MARGIN_AREA)
5268 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5269 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5270 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5271 + window_box_width (w, LEFT_MARGIN_AREA)
5272 + window_box_width (w, TEXT_AREA));
5273 else
5274 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5275 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5276 : 0);
5278 glyph = row->glyphs[area];
5279 end = glyph + row->used[area];
5280 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5281 x0 -= glyph->pixel_width;
5282 *x = glyph - row->glyphs[area];
5283 if (glyph < end)
5285 string = glyph->object;
5286 *charpos = glyph->charpos;
5287 *width = glyph->pixel_width;
5288 *height = glyph->ascent + glyph->descent;
5289 #ifdef HAVE_WINDOW_SYSTEM
5290 if (glyph->type == IMAGE_GLYPH)
5292 struct image *img;
5293 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5294 if (img != NULL)
5295 *object = img->spec;
5296 y0 -= row->ascent - glyph->ascent;
5297 x0 += glyph->slice.img.x;
5298 y0 += glyph->slice.img.y;
5300 #endif
5302 else
5304 /* Add extra (default width) columns if clicked after EOL. */
5305 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5306 *width = 0;
5307 *height = row->height;
5310 else
5312 x0 = 0;
5313 *x = 0;
5314 *width = *height = 0;
5317 *dx = x0;
5318 *dy = y0;
5320 return string;
5324 /***********************************************************************
5325 Changing Frame Sizes
5326 ***********************************************************************/
5328 #ifdef SIGWINCH
5330 static void deliver_window_change_signal (int);
5332 static void
5333 handle_window_change_signal (int sig)
5335 int width, height;
5336 struct tty_display_info *tty;
5338 /* The frame size change obviously applies to a single
5339 termcap-controlled terminal, but we can't decide which.
5340 Therefore, we resize the frames corresponding to each tty.
5342 for (tty = tty_list; tty; tty = tty->next) {
5344 if (! tty->term_initted)
5345 continue;
5347 /* Suspended tty frames have tty->input == NULL avoid trying to
5348 use it. */
5349 if (!tty->input)
5350 continue;
5352 get_tty_size (fileno (tty->input), &width, &height);
5354 if (width > 5 && height > 2) {
5355 Lisp_Object tail, frame;
5357 FOR_EACH_FRAME (tail, frame)
5358 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5359 /* Record the new sizes, but don't reallocate the data
5360 structures now. Let that be done later outside of the
5361 signal handler. */
5362 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5367 static void
5368 deliver_window_change_signal (int sig)
5370 deliver_process_signal (sig, handle_window_change_signal);
5372 #endif /* SIGWINCH */
5375 /* Do any change in frame size that was requested by a signal.
5376 SAFE means this function is called from a place where it is
5377 safe to change frame sizes while a redisplay is in progress. */
5379 void
5380 do_pending_window_change (bool safe)
5382 /* If window change signal handler should have run before, run it now. */
5383 if (redisplaying_p && !safe)
5384 return;
5386 while (delayed_size_change)
5388 Lisp_Object tail, frame;
5390 delayed_size_change = 0;
5392 FOR_EACH_FRAME (tail, frame)
5394 struct frame *f = XFRAME (frame);
5396 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5397 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5398 0, 0, safe);
5404 /* Change the frame height and/or width. Values may be given as zero to
5405 indicate no change is to take place.
5407 If DELAY, assume we're being called from a signal handler, and
5408 queue the change for later - perhaps the next redisplay.
5409 Since this tries to resize windows, we can't call it
5410 from a signal handler.
5412 SAFE means this function is called from a place where it's
5413 safe to change frame sizes while a redisplay is in progress. */
5415 void
5416 change_frame_size (struct frame *f, int newheight, int newwidth,
5417 bool pretend, bool delay, bool safe)
5419 Lisp_Object tail, frame;
5421 if (FRAME_MSDOS_P (f))
5423 /* On MS-DOS, all frames use the same screen, so a change in
5424 size affects all frames. Termcap now supports multiple
5425 ttys. */
5426 FOR_EACH_FRAME (tail, frame)
5427 if (! FRAME_WINDOW_P (XFRAME (frame)))
5428 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5429 pretend, delay, safe);
5431 else
5432 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5435 static void
5436 change_frame_size_1 (struct frame *f, int newheight, int newwidth,
5437 bool pretend, bool delay, bool safe)
5439 int new_frame_total_cols;
5440 ptrdiff_t count = SPECPDL_INDEX ();
5442 /* If we can't deal with the change now, queue it for later. */
5443 if (delay || (redisplaying_p && !safe))
5445 f->new_text_lines = newheight;
5446 f->new_text_cols = newwidth;
5447 delayed_size_change = 1;
5448 return;
5451 /* This size-change overrides any pending one for this frame. */
5452 f->new_text_lines = 0;
5453 f->new_text_cols = 0;
5455 /* If an argument is zero, set it to the current value. */
5456 if (newheight == 0)
5457 newheight = FRAME_LINES (f);
5458 if (newwidth == 0)
5459 newwidth = FRAME_COLS (f);
5461 /* Compute width of windows in F. */
5462 /* Round up to the smallest acceptable size. */
5463 check_frame_size (f, &newheight, &newwidth);
5465 /* This is the width of the frame with vertical scroll bars and fringe
5466 columns. Do this after rounding - see discussion of bug#9723. */
5467 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5469 /* If we're not changing the frame size, quit now. */
5470 /* Frame width may be unchanged but the text portion may change, for
5471 example, fullscreen and remove/add scroll bar. */
5472 if (newheight == FRAME_LINES (f)
5473 /* Text portion unchanged? */
5474 && newwidth == FRAME_COLS (f)
5475 /* Frame width unchanged? */
5476 && new_frame_total_cols == FRAME_TOTAL_COLS (f))
5477 return;
5479 block_input ();
5481 #ifdef MSDOS
5482 /* We only can set screen dimensions to certain values supported
5483 by our video hardware. Try to find the smallest size greater
5484 or equal to the requested dimensions. */
5485 dos_set_window_size (&newheight, &newwidth);
5486 #endif
5488 if (newheight != FRAME_LINES (f))
5490 resize_frame_windows (f, newheight, 0);
5492 /* MSDOS frames cannot PRETEND, as they change frame size by
5493 manipulating video hardware. */
5494 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5495 FrameRows (FRAME_TTY (f)) = newheight;
5498 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5500 resize_frame_windows (f, new_frame_total_cols, 1);
5502 /* MSDOS frames cannot PRETEND, as they change frame size by
5503 manipulating video hardware. */
5504 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5505 FrameCols (FRAME_TTY (f)) = newwidth;
5507 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5508 if (WINDOWP (f->tool_bar_window))
5509 XWINDOW (f->tool_bar_window)->total_cols = newwidth;
5510 #endif
5513 FRAME_LINES (f) = newheight;
5514 SET_FRAME_COLS (f, newwidth);
5517 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5518 int text_area_x, text_area_y, text_area_width, text_area_height;
5520 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5521 &text_area_height);
5522 if (w->cursor.x >= text_area_x + text_area_width)
5523 w->cursor.hpos = w->cursor.x = 0;
5524 if (w->cursor.y >= text_area_y + text_area_height)
5525 w->cursor.vpos = w->cursor.y = 0;
5528 adjust_frame_glyphs (f);
5529 calculate_costs (f);
5530 SET_FRAME_GARBAGED (f);
5531 f->resized_p = 1;
5533 unblock_input ();
5535 record_unwind_current_buffer ();
5537 run_window_configuration_change_hook (f);
5539 unbind_to (count, Qnil);
5544 /***********************************************************************
5545 Terminal Related Lisp Functions
5546 ***********************************************************************/
5548 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5549 1, 1, "FOpen termscript file: ",
5550 doc: /* Start writing all terminal output to FILE as well as the terminal.
5551 FILE = nil means just close any termscript file currently open. */)
5552 (Lisp_Object file)
5554 struct tty_display_info *tty;
5556 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5557 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5558 error ("Current frame is not on a tty device");
5560 tty = CURTTY ();
5562 if (tty->termscript != 0)
5564 block_input ();
5565 fclose (tty->termscript);
5566 tty->termscript = 0;
5567 unblock_input ();
5570 if (! NILP (file))
5572 file = Fexpand_file_name (file, Qnil);
5573 tty->termscript = emacs_fopen (SSDATA (file), "w");
5574 if (tty->termscript == 0)
5575 report_file_error ("Opening termscript", file);
5577 return Qnil;
5581 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5582 Ssend_string_to_terminal, 1, 2, 0,
5583 doc: /* Send STRING to the terminal without alteration.
5584 Control characters in STRING will have terminal-dependent effects.
5586 Optional parameter TERMINAL specifies the tty terminal device to use.
5587 It may be a terminal object, a frame, or nil for the terminal used by
5588 the currently selected frame. In batch mode, STRING is sent to stdout
5589 when TERMINAL is nil. */)
5590 (Lisp_Object string, Lisp_Object terminal)
5592 struct terminal *t = get_terminal (terminal, 1);
5593 FILE *out;
5595 /* ??? Perhaps we should do something special for multibyte strings here. */
5596 CHECK_STRING (string);
5597 block_input ();
5599 if (!t)
5600 error ("Unknown terminal device");
5602 if (t->type == output_initial)
5603 out = stdout;
5604 else if (t->type != output_termcap && t->type != output_msdos_raw)
5605 error ("Device %d is not a termcap terminal device", t->id);
5606 else
5608 struct tty_display_info *tty = t->display_info.tty;
5610 if (! tty->output)
5611 error ("Terminal is currently suspended");
5613 if (tty->termscript)
5615 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5616 fflush (tty->termscript);
5618 out = tty->output;
5620 fwrite (SDATA (string), 1, SBYTES (string), out);
5621 fflush (out);
5622 unblock_input ();
5623 return Qnil;
5627 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5628 doc: /* Beep, or flash the screen.
5629 Also, unless an argument is given,
5630 terminate any keyboard macro currently executing. */)
5631 (Lisp_Object arg)
5633 if (!NILP (arg))
5635 if (noninteractive)
5636 putchar (07);
5637 else
5638 ring_bell (XFRAME (selected_frame));
5640 else
5641 bitch_at_user ();
5643 return Qnil;
5646 void
5647 bitch_at_user (void)
5649 if (noninteractive)
5650 putchar (07);
5651 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5653 const char *msg
5654 = "Keyboard macro terminated by a command ringing the bell";
5655 Fsignal (Quser_error, list1 (build_string (msg)));
5657 else
5658 ring_bell (XFRAME (selected_frame));
5663 /***********************************************************************
5664 Sleeping, Waiting
5665 ***********************************************************************/
5667 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5668 doc: /* Pause, without updating display, for SECONDS seconds.
5669 SECONDS may be a floating-point value, meaning that you can wait for a
5670 fraction of a second. Optional second arg MILLISECONDS specifies an
5671 additional wait period, in milliseconds; this is for backwards compatibility.
5672 \(Not all operating systems support waiting for a fraction of a second.) */)
5673 (Lisp_Object seconds, Lisp_Object milliseconds)
5675 double duration = extract_float (seconds);
5677 if (!NILP (milliseconds))
5679 CHECK_NUMBER (milliseconds);
5680 duration += XINT (milliseconds) / 1000.0;
5683 if (duration > 0)
5685 struct timespec t = dtotimespec (duration);
5686 wait_reading_process_output (min (t.tv_sec, WAIT_READING_MAX),
5687 t.tv_nsec, 0, 0, Qnil, NULL, 0);
5690 return Qnil;
5694 /* This is just like wait_reading_process_output, except that
5695 it does redisplay.
5697 TIMEOUT is number of seconds to wait (float or integer),
5698 or t to wait forever.
5699 READING is true if reading input.
5700 If DISPLAY_OPTION is >0 display process output while waiting.
5701 If DISPLAY_OPTION is >1 perform an initial redisplay before waiting.
5704 Lisp_Object
5705 sit_for (Lisp_Object timeout, bool reading, int display_option)
5707 intmax_t sec;
5708 int nsec;
5709 bool do_display = display_option > 0;
5711 swallow_events (do_display);
5713 if ((detect_input_pending_run_timers (do_display))
5714 || !NILP (Vexecuting_kbd_macro))
5715 return Qnil;
5717 if (display_option > 1)
5718 redisplay_preserve_echo_area (2);
5720 if (INTEGERP (timeout))
5722 sec = XINT (timeout);
5723 if (sec <= 0)
5724 return Qt;
5725 nsec = 0;
5727 else if (FLOATP (timeout))
5729 double seconds = XFLOAT_DATA (timeout);
5730 if (! (0 < seconds))
5731 return Qt;
5732 else
5734 struct timespec t = dtotimespec (seconds);
5735 sec = min (t.tv_sec, WAIT_READING_MAX);
5736 nsec = t.tv_nsec;
5739 else if (EQ (timeout, Qt))
5741 sec = 0;
5742 nsec = 0;
5744 else
5745 wrong_type_argument (Qnumberp, timeout);
5748 #ifdef USABLE_SIGIO
5749 gobble_input ();
5750 #endif
5752 wait_reading_process_output (sec, nsec, reading ? -1 : 1, do_display,
5753 Qnil, NULL, 0);
5755 return detect_input_pending () ? Qnil : Qt;
5759 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
5760 doc: /* Perform redisplay.
5761 Optional arg FORCE, if non-nil, prevents redisplay from being
5762 preempted by arriving input, even if `redisplay-dont-pause' is nil.
5763 If `redisplay-dont-pause' is non-nil (the default), redisplay is never
5764 preempted by arriving input, so FORCE does nothing.
5766 Return t if redisplay was performed, nil if redisplay was preempted
5767 immediately by pending input. */)
5768 (Lisp_Object force)
5770 ptrdiff_t count;
5772 swallow_events (1);
5773 if ((detect_input_pending_run_timers (1)
5774 && NILP (force) && !redisplay_dont_pause)
5775 || !NILP (Vexecuting_kbd_macro))
5776 return Qnil;
5778 count = SPECPDL_INDEX ();
5779 if (!NILP (force) && !redisplay_dont_pause)
5780 specbind (Qredisplay_dont_pause, Qt);
5781 redisplay_preserve_echo_area (2);
5782 unbind_to (count, Qnil);
5783 return Qt;
5788 /***********************************************************************
5789 Other Lisp Functions
5790 ***********************************************************************/
5792 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
5793 session's frames, frame names, buffers, buffer-read-only flags, and
5794 buffer-modified-flags. */
5796 static Lisp_Object frame_and_buffer_state;
5799 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
5800 Sframe_or_buffer_changed_p, 0, 1, 0,
5801 doc: /* Return non-nil if the frame and buffer state appears to have changed.
5802 VARIABLE is a variable name whose value is either nil or a state vector
5803 that will be updated to contain all frames and buffers,
5804 aside from buffers whose names start with space,
5805 along with the buffers' read-only and modified flags. This allows a fast
5806 check to see whether buffer menus might need to be recomputed.
5807 If this function returns non-nil, it updates the internal vector to reflect
5808 the current state.
5810 If VARIABLE is nil, an internal variable is used. Users should not
5811 pass nil for VARIABLE. */)
5812 (Lisp_Object variable)
5814 Lisp_Object state, tail, frame, buf;
5815 ptrdiff_t n, idx;
5817 if (! NILP (variable))
5819 CHECK_SYMBOL (variable);
5820 state = Fsymbol_value (variable);
5821 if (! VECTORP (state))
5822 goto changed;
5824 else
5825 state = frame_and_buffer_state;
5827 idx = 0;
5828 FOR_EACH_FRAME (tail, frame)
5830 if (idx == ASIZE (state))
5831 goto changed;
5832 if (!EQ (AREF (state, idx++), frame))
5833 goto changed;
5834 if (idx == ASIZE (state))
5835 goto changed;
5836 if (!EQ (AREF (state, idx++), XFRAME (frame)->name))
5837 goto changed;
5839 /* Check that the buffer info matches. */
5840 FOR_EACH_LIVE_BUFFER (tail, buf)
5842 /* Ignore buffers that aren't included in buffer lists. */
5843 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5844 continue;
5845 if (idx == ASIZE (state))
5846 goto changed;
5847 if (!EQ (AREF (state, idx++), buf))
5848 goto changed;
5849 if (idx == ASIZE (state))
5850 goto changed;
5851 if (!EQ (AREF (state, idx++), BVAR (XBUFFER (buf), read_only)))
5852 goto changed;
5853 if (idx == ASIZE (state))
5854 goto changed;
5855 if (!EQ (AREF (state, idx++), Fbuffer_modified_p (buf)))
5856 goto changed;
5858 if (idx == ASIZE (state))
5859 goto changed;
5860 /* Detect deletion of a buffer at the end of the list. */
5861 if (EQ (AREF (state, idx), Qlambda))
5862 return Qnil;
5864 /* Come here if we decide the data has changed. */
5865 changed:
5866 /* Count the size we will need.
5867 Start with 1 so there is room for at least one lambda at the end. */
5868 n = 1;
5869 FOR_EACH_FRAME (tail, frame)
5870 n += 2;
5871 FOR_EACH_LIVE_BUFFER (tail, buf)
5872 n += 3;
5873 /* Reallocate the vector if data has grown to need it,
5874 or if it has shrunk a lot. */
5875 if (! VECTORP (state)
5876 || n > ASIZE (state)
5877 || n + 20 < ASIZE (state) / 2)
5878 /* Add 20 extra so we grow it less often. */
5880 state = Fmake_vector (make_number (n + 20), Qlambda);
5881 if (! NILP (variable))
5882 Fset (variable, state);
5883 else
5884 frame_and_buffer_state = state;
5887 /* Record the new data in the (possibly reallocated) vector. */
5888 idx = 0;
5889 FOR_EACH_FRAME (tail, frame)
5891 ASET (state, idx, frame);
5892 idx++;
5893 ASET (state, idx, XFRAME (frame)->name);
5894 idx++;
5896 FOR_EACH_LIVE_BUFFER (tail, buf)
5898 /* Ignore buffers that aren't included in buffer lists. */
5899 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5900 continue;
5901 ASET (state, idx, buf);
5902 idx++;
5903 ASET (state, idx, BVAR (XBUFFER (buf), read_only));
5904 idx++;
5905 ASET (state, idx, Fbuffer_modified_p (buf));
5906 idx++;
5908 /* Fill up the vector with lambdas (always at least one). */
5909 ASET (state, idx, Qlambda);
5910 idx++;
5911 while (idx < ASIZE (state))
5913 ASET (state, idx, Qlambda);
5914 idx++;
5916 /* Make sure we didn't overflow the vector. */
5917 eassert (idx <= ASIZE (state));
5918 return Qt;
5923 /***********************************************************************
5924 Initialization
5925 ***********************************************************************/
5927 /* Initialization done when Emacs fork is started, before doing stty.
5928 Determine terminal type and set terminal_driver. Then invoke its
5929 decoding routine to set up variables in the terminal package. */
5931 void
5932 init_display (void)
5934 char *terminal_type;
5936 /* Construct the space glyph. */
5937 space_glyph.type = CHAR_GLYPH;
5938 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
5939 space_glyph.charpos = -1;
5941 inverse_video = 0;
5942 cursor_in_echo_area = 0;
5944 /* Now is the time to initialize this; it's used by init_sys_modes
5945 during startup. */
5946 Vinitial_window_system = Qnil;
5948 /* SIGWINCH needs to be handled no matter what display we start
5949 with. Otherwise newly opened tty frames will not resize
5950 automatically. */
5951 #ifdef SIGWINCH
5952 #ifndef CANNOT_DUMP
5953 if (initialized)
5954 #endif /* CANNOT_DUMP */
5956 struct sigaction action;
5957 emacs_sigaction_init (&action, deliver_window_change_signal);
5958 sigaction (SIGWINCH, &action, 0);
5960 #endif /* SIGWINCH */
5962 /* If running as a daemon, no need to initialize any frames/terminal. */
5963 if (IS_DAEMON)
5964 return;
5966 /* If the user wants to use a window system, we shouldn't bother
5967 initializing the terminal. This is especially important when the
5968 terminal is so dumb that emacs gives up before and doesn't bother
5969 using the window system.
5971 If the DISPLAY environment variable is set and nonempty,
5972 try to use X, and die with an error message if that doesn't work. */
5974 #ifdef HAVE_X_WINDOWS
5975 if (! inhibit_window_system && ! display_arg)
5977 char *display;
5978 display = getenv ("DISPLAY");
5979 display_arg = (display != 0 && *display != 0);
5981 if (display_arg && !x_display_ok (display))
5983 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
5984 display);
5985 inhibit_window_system = 1;
5989 if (!inhibit_window_system && display_arg)
5991 Vinitial_window_system = Qx;
5992 #ifdef HAVE_X11
5993 Vwindow_system_version = make_number (11);
5994 #endif
5995 #ifdef USE_NCURSES
5996 /* In some versions of ncurses,
5997 tputs crashes if we have not called tgetent.
5998 So call tgetent. */
5999 { char b[2044]; tgetent (b, "xterm");}
6000 #endif
6001 return;
6003 #endif /* HAVE_X_WINDOWS */
6005 #ifdef HAVE_NTGUI
6006 if (!inhibit_window_system)
6008 Vinitial_window_system = Qw32;
6009 Vwindow_system_version = make_number (1);
6010 return;
6012 #endif /* HAVE_NTGUI */
6014 #ifdef HAVE_NS
6015 if (!inhibit_window_system
6016 #ifndef CANNOT_DUMP
6017 && initialized
6018 #endif
6021 Vinitial_window_system = Qns;
6022 Vwindow_system_version = make_number (10);
6023 return;
6025 #endif
6027 /* If no window system has been specified, try to use the terminal. */
6028 if (! isatty (0))
6029 fatal ("standard input is not a tty");
6031 #ifdef WINDOWSNT
6032 terminal_type = "w32console";
6033 #else
6034 terminal_type = getenv ("TERM");
6035 #endif
6036 if (!terminal_type)
6038 #ifdef HAVE_WINDOW_SYSTEM
6039 if (! inhibit_window_system)
6040 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6041 else
6042 #endif /* HAVE_WINDOW_SYSTEM */
6043 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6044 exit (1);
6048 struct terminal *t;
6049 struct frame *f = XFRAME (selected_frame);
6051 init_foreground_group ();
6053 /* Open a display on the controlling tty. */
6054 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6056 /* Convert the initial frame to use the new display. */
6057 if (f->output_method != output_initial)
6058 emacs_abort ();
6059 f->output_method = t->type;
6060 f->terminal = t;
6062 t->reference_count++;
6063 #ifdef MSDOS
6064 f->output_data.tty->display_info = &the_only_display_info;
6065 #else
6066 if (f->output_method == output_termcap)
6067 create_tty_output (f);
6068 #endif
6069 t->display_info.tty->top_frame = selected_frame;
6070 change_frame_size (XFRAME (selected_frame),
6071 FrameRows (t->display_info.tty),
6072 FrameCols (t->display_info.tty), 0, 0, 1);
6074 /* Delete the initial terminal. */
6075 if (--initial_terminal->reference_count == 0
6076 && initial_terminal->delete_terminal_hook)
6077 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6079 /* Update frame parameters to reflect the new type. */
6080 Fmodify_frame_parameters
6081 (selected_frame, list1 (Fcons (Qtty_type,
6082 Ftty_type (selected_frame))));
6083 if (t->display_info.tty->name)
6084 Fmodify_frame_parameters
6085 (selected_frame,
6086 list1 (Fcons (Qtty, build_string (t->display_info.tty->name))));
6087 else
6088 Fmodify_frame_parameters (selected_frame, list1 (Fcons (Qtty, Qnil)));
6092 struct frame *sf = SELECTED_FRAME ();
6093 int width = FRAME_TOTAL_COLS (sf);
6094 int height = FRAME_LINES (sf);
6096 /* If these sizes are so big they cause overflow, just ignore the
6097 change. It's not clear what better we could do. The rest of
6098 the code assumes that (width + 2) * height * sizeof (struct glyph)
6099 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6100 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6101 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6102 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6103 < (width + 2) * height))
6104 fatal ("screen size %dx%d too big", width, height);
6107 calculate_costs (XFRAME (selected_frame));
6109 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6110 if (initialized
6111 && !noninteractive
6112 && NILP (Vinitial_window_system))
6114 /* For the initial frame, we don't have any way of knowing what
6115 are the foreground and background colors of the terminal. */
6116 struct frame *sf = SELECTED_FRAME ();
6118 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6119 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6120 call0 (intern ("tty-set-up-initial-frame-faces"));
6126 /***********************************************************************
6127 Blinking cursor
6128 ***********************************************************************/
6130 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6131 Sinternal_show_cursor, 2, 2, 0,
6132 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6133 WINDOW nil means use the selected window. SHOW non-nil means
6134 show a cursor in WINDOW in the next redisplay. SHOW nil means
6135 don't show a cursor. */)
6136 (Lisp_Object window, Lisp_Object show)
6138 /* Don't change cursor state while redisplaying. This could confuse
6139 output routines. */
6140 if (!redisplaying_p)
6141 decode_any_window (window)->cursor_off_p = NILP (show);
6142 return Qnil;
6146 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6147 Sinternal_show_cursor_p, 0, 1, 0,
6148 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6149 WINDOW nil or omitted means report on the selected window. */)
6150 (Lisp_Object window)
6152 return decode_any_window (window)->cursor_off_p ? Qnil : Qt;
6155 /***********************************************************************
6156 Initialization
6157 ***********************************************************************/
6159 void
6160 syms_of_display (void)
6162 defsubr (&Sredraw_frame);
6163 defsubr (&Sredraw_display);
6164 defsubr (&Sframe_or_buffer_changed_p);
6165 defsubr (&Sopen_termscript);
6166 defsubr (&Sding);
6167 defsubr (&Sredisplay);
6168 defsubr (&Ssleep_for);
6169 defsubr (&Ssend_string_to_terminal);
6170 defsubr (&Sinternal_show_cursor);
6171 defsubr (&Sinternal_show_cursor_p);
6173 #ifdef GLYPH_DEBUG
6174 defsubr (&Sdump_redisplay_history);
6175 #endif
6177 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6178 staticpro (&frame_and_buffer_state);
6180 DEFSYM (Qdisplay_table, "display-table");
6181 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6183 DEFVAR_INT ("baud-rate", baud_rate,
6184 doc: /* The output baud rate of the terminal.
6185 On most systems, changing this value will affect the amount of padding
6186 and the other strategic decisions made during redisplay. */);
6188 DEFVAR_BOOL ("inverse-video", inverse_video,
6189 doc: /* Non-nil means invert the entire frame display.
6190 This means everything is in inverse video which otherwise would not be. */);
6192 DEFVAR_BOOL ("visible-bell", visible_bell,
6193 doc: /* Non-nil means try to flash the frame to represent a bell.
6195 See also `ring-bell-function'. */);
6197 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6198 doc: /* Non-nil means no need to redraw entire frame after suspending.
6199 A non-nil value is useful if the terminal can automatically preserve
6200 Emacs's frame display when you reenter Emacs.
6201 It is up to you to set this variable if your terminal can do that. */);
6203 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6204 doc: /* Name of the window system that Emacs uses for the first frame.
6205 The value is a symbol:
6206 nil for a termcap frame (a character-only terminal),
6207 'x' for an Emacs frame that is really an X window,
6208 'w32' for an Emacs frame that is a window on MS-Windows display,
6209 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6210 'pc' for a direct-write MS-DOS frame.
6212 Use of this variable as a boolean is deprecated. Instead,
6213 use `display-graphic-p' or any of the other `display-*-p'
6214 predicates which report frame's specific UI-related capabilities. */);
6216 DEFVAR_KBOARD ("window-system", Vwindow_system,
6217 doc: /* Name of window system through which the selected frame is displayed.
6218 The value is a symbol:
6219 nil for a termcap frame (a character-only terminal),
6220 'x' for an Emacs frame that is really an X window,
6221 'w32' for an Emacs frame that is a window on MS-Windows display,
6222 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6223 'pc' for a direct-write MS-DOS frame.
6225 Use of this variable as a boolean is deprecated. Instead,
6226 use `display-graphic-p' or any of the other `display-*-p'
6227 predicates which report frame's specific UI-related capabilities. */);
6229 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6230 doc: /* The version number of the window system in use.
6231 For X windows, this is 11. */);
6233 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6234 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6236 DEFVAR_LISP ("glyph-table", Vglyph_table,
6237 doc: /* Table defining how to output a glyph code to the frame.
6238 If not nil, this is a vector indexed by glyph code to define the glyph.
6239 Each element can be:
6240 integer: a glyph code which this glyph is an alias for.
6241 string: output this glyph using that string (not impl. in X windows).
6242 nil: this glyph mod 524288 is the code of a character to output,
6243 and this glyph / 524288 is the face number (see `face-id') to use
6244 while outputting it. */);
6245 Vglyph_table = Qnil;
6247 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6248 doc: /* Display table to use for buffers that specify none.
6249 See `buffer-display-table' for more information. */);
6250 Vstandard_display_table = Qnil;
6252 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6253 doc: /* Non-nil means display update isn't paused when input is detected. */);
6254 redisplay_dont_pause = 1;
6256 #ifdef CANNOT_DUMP
6257 if (noninteractive)
6258 #endif
6260 Vinitial_window_system = Qnil;
6261 Vwindow_system_version = Qnil;