auto upstream
[emacs.git] / src / dispnew.c
blob144fa659c47f1e86e1f134aa64d7df5f15436cd9
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 <stdio.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 systime.h after xterm.h to avoid double inclusion of time.h. */
57 #include "systime.h"
58 #include <errno.h>
60 #include <fpending.h>
62 #if defined (HAVE_TERM_H) && defined (GNU_LINUX)
63 #include <term.h> /* for tgetent */
64 #endif
66 #ifdef WINDOWSNT
67 #include "w32.h"
68 #endif
70 /* Structure to pass dimensions around. Used for character bounding
71 boxes, glyph matrix dimensions and alike. */
73 struct dim
75 int width;
76 int height;
80 /* Function prototypes. */
82 static void update_frame_line (struct frame *, int);
83 static int required_matrix_height (struct window *);
84 static int required_matrix_width (struct window *);
85 static void adjust_frame_glyphs (struct frame *);
86 static void change_frame_size_1 (struct frame *, int, int, bool, bool, bool);
87 static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
88 static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
89 static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
90 struct window *);
91 static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
92 struct window *);
93 static void adjust_frame_message_buffer (struct frame *);
94 static void adjust_decode_mode_spec_buffer (struct frame *);
95 static void fill_up_glyph_row_with_spaces (struct glyph_row *);
96 static void clear_window_matrices (struct window *, bool);
97 static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
98 static int scrolling_window (struct window *, bool);
99 static bool update_window_line (struct window *, int, bool *);
100 static void mirror_make_current (struct window *, int);
101 #ifdef GLYPH_DEBUG
102 static void check_matrix_pointers (struct glyph_matrix *,
103 struct glyph_matrix *);
104 #endif
105 static void mirror_line_dance (struct window *, int, int, int *, char *);
106 static bool update_window_tree (struct window *, bool);
107 static bool update_window (struct window *, bool);
108 static bool update_frame_1 (struct frame *, bool, bool);
109 static bool scrolling (struct frame *);
110 static void set_window_cursor_after_update (struct window *);
111 static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
112 static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
115 /* Redisplay preemption timers. */
117 static EMACS_TIME preemption_period;
118 static EMACS_TIME preemption_next_check;
120 /* True upon entry to redisplay means do not assume anything about
121 current contents of actual terminal frame; clear and redraw it. */
123 bool frame_garbaged;
125 /* True means last display completed. False means it was preempted. */
127 bool display_completed;
129 Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
132 /* The currently selected frame. In a single-frame version, this
133 variable always equals the_only_frame. */
135 Lisp_Object selected_frame;
137 /* A frame which is not just a mini-buffer, or 0 if there are no such
138 frames. This is usually the most recent such frame that was
139 selected. In a single-frame version, this variable always holds
140 the address of the_only_frame. */
142 struct frame *last_nonminibuf_frame;
144 /* True means SIGWINCH happened when not safe. */
146 static bool delayed_size_change;
148 /* Updated window if != 0. Set by update_window. */
150 struct window *updated_window;
152 /* Glyph row updated in update_window_line, and area that is updated. */
154 struct glyph_row *updated_row;
155 int updated_area;
157 /* A glyph for a space. */
159 struct glyph space_glyph;
161 /* Counts of allocated structures. These counts serve to diagnose
162 memory leaks and double frees. */
164 static int glyph_matrix_count;
165 static int glyph_pool_count;
167 /* If non-null, the frame whose frame matrices are manipulated. If
168 null, window matrices are worked on. */
170 static struct frame *frame_matrix_frame;
172 /* True means that fonts have been loaded since the last glyph
173 matrix adjustments. Redisplay must stop, and glyph matrices must
174 be adjusted when this flag becomes true during display. The
175 reason fonts can be loaded so late is that fonts of fontsets are
176 loaded on demand. Another reason is that a line contains many
177 characters displayed by zero width or very narrow glyphs of
178 variable-width fonts. */
180 bool fonts_changed_p;
182 /* Convert vpos and hpos from frame to window and vice versa.
183 This may only be used for terminal frames. */
185 #ifdef GLYPH_DEBUG
187 static int window_to_frame_vpos (struct window *, int);
188 static int window_to_frame_hpos (struct window *, int);
189 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
190 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
192 /* One element of the ring buffer containing redisplay history
193 information. */
195 struct redisplay_history
197 char trace[512 + 100];
200 /* The size of the history buffer. */
202 #define REDISPLAY_HISTORY_SIZE 30
204 /* The redisplay history buffer. */
206 static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
208 /* Next free entry in redisplay_history. */
210 static int history_idx;
212 /* A tick that's incremented each time something is added to the
213 history. */
215 static uprintmax_t history_tick;
217 /* Add to the redisplay history how window W has been displayed.
218 MSG is a trace containing the information how W's glyph matrix
219 has been constructed. PAUSED_P means that the update
220 has been interrupted for pending input. */
222 static void
223 add_window_display_history (struct window *w, const char *msg, bool paused_p)
225 char *buf;
227 if (history_idx >= REDISPLAY_HISTORY_SIZE)
228 history_idx = 0;
229 buf = redisplay_history[history_idx].trace;
230 ++history_idx;
232 snprintf (buf, sizeof redisplay_history[0].trace,
233 "%"pMu": window %p (`%s')%s\n%s",
234 history_tick++,
236 ((BUFFERP (w->buffer)
237 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
238 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
239 : "???"),
240 paused_p ? " ***paused***" : "",
241 msg);
245 /* Add to the redisplay history that frame F has been displayed.
246 PAUSED_P means that the update has been interrupted for
247 pending input. */
249 static void add_frame_display_history (struct frame *f, int paused_p)
251 char *buf;
253 if (history_idx >= REDISPLAY_HISTORY_SIZE)
254 history_idx = 0;
255 buf = redisplay_history[history_idx].trace;
256 ++history_idx;
258 sprintf (buf, "%"pMu": update frame %p%s",
259 history_tick++,
260 f, paused_p ? " ***paused***" : "");
264 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
265 Sdump_redisplay_history, 0, 0, "",
266 doc: /* Dump redisplay history to stderr. */)
267 (void)
269 int i;
271 for (i = history_idx - 1; i != history_idx; --i)
273 if (i < 0)
274 i = REDISPLAY_HISTORY_SIZE - 1;
275 fprintf (stderr, "%s\n", redisplay_history[i].trace);
278 return Qnil;
282 #else /* not GLYPH_DEBUG */
284 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
285 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
287 #endif /* GLYPH_DEBUG */
290 #if (defined PROFILING \
291 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
292 && !HAVE___EXECUTABLE_START)
293 /* This function comes first in the Emacs executable and is used only
294 to estimate the text start for profiling. */
295 void
296 __executable_start (void)
298 emacs_abort ();
300 #endif
302 /***********************************************************************
303 Glyph Matrices
304 ***********************************************************************/
306 /* Allocate and return a glyph_matrix structure. POOL is the glyph
307 pool from which memory for the matrix should be allocated, or null
308 for window-based redisplay where no glyph pools are used. The
309 member `pool' of the glyph matrix structure returned is set to
310 POOL, the structure is otherwise zeroed. */
312 static struct glyph_matrix *
313 new_glyph_matrix (struct glyph_pool *pool)
315 struct glyph_matrix *result = xzalloc (sizeof *result);
317 /* Increment number of allocated matrices. This count is used
318 to detect memory leaks. */
319 ++glyph_matrix_count;
321 /* Set pool and return. */
322 result->pool = pool;
323 return result;
327 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
329 The global counter glyph_matrix_count is decremented when a matrix
330 is freed. If the count gets negative, more structures were freed
331 than allocated, i.e. one matrix was freed more than once or a bogus
332 pointer was passed to this function.
334 If MATRIX->pool is null, this means that the matrix manages its own
335 glyph memory---this is done for matrices on X frames. Freeing the
336 matrix also frees the glyph memory in this case. */
338 static void
339 free_glyph_matrix (struct glyph_matrix *matrix)
341 if (matrix)
343 int i;
345 /* Detect the case that more matrices are freed than were
346 allocated. */
347 if (--glyph_matrix_count < 0)
348 emacs_abort ();
350 /* Free glyph memory if MATRIX owns it. */
351 if (matrix->pool == NULL)
352 for (i = 0; i < matrix->rows_allocated; ++i)
353 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
355 /* Free row structures and the matrix itself. */
356 xfree (matrix->rows);
357 xfree (matrix);
362 /* Return the number of glyphs to reserve for a marginal area of
363 window W. TOTAL_GLYPHS is the number of glyphs in a complete
364 display line of window W. MARGIN gives the width of the marginal
365 area in canonical character units. MARGIN should be an integer
366 or a float. */
368 static int
369 margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
371 int n;
373 if (NUMBERP (margin))
375 int width = XFASTINT (w->total_cols);
376 double d = max (0, XFLOATINT (margin));
377 d = min (width / 2 - 1, d);
378 n = (int) ((double) total_glyphs / width * d);
380 else
381 n = 0;
383 return n;
386 /* Return true if ROW's hash value is correct.
387 Optimized away if ENABLE_CHECKING is not defined. */
389 static bool
390 verify_row_hash (struct glyph_row *row)
392 return row->hash == row_hash (row);
395 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
396 window sizes.
398 W is null if the function is called for a frame glyph matrix.
399 Otherwise it is the window MATRIX is a member of. X and Y are the
400 indices of the first column and row of MATRIX within the frame
401 matrix, if such a matrix exists. They are zero for purely
402 window-based redisplay. DIM is the needed size of the matrix.
404 In window-based redisplay, where no frame matrices exist, glyph
405 matrices manage their own glyph storage. Otherwise, they allocate
406 storage from a common frame glyph pool which can be found in
407 MATRIX->pool.
409 The reason for this memory management strategy is to avoid complete
410 frame redraws if possible. When we allocate from a common pool, a
411 change of the location or size of a sub-matrix within the pool
412 requires a complete redisplay of the frame because we cannot easily
413 make sure that the current matrices of all windows still agree with
414 what is displayed on the screen. While this is usually fast, it
415 leads to screen flickering. */
417 static void
418 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
420 int i;
421 int new_rows;
422 bool marginal_areas_changed_p = 0;
423 bool header_line_changed_p = 0;
424 bool header_line_p = 0;
425 int left = -1, right = -1;
426 int window_width = -1, window_height = -1;
428 /* See if W had a header line that has disappeared now, or vice versa.
429 Get W's size. */
430 if (w)
432 window_box (w, -1, 0, 0, &window_width, &window_height);
434 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
435 header_line_changed_p = header_line_p != matrix->header_line_p;
437 matrix->header_line_p = header_line_p;
439 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
440 Do nothing if MATRIX' size, position, vscroll, and marginal areas
441 haven't changed. This optimization is important because preserving
442 the matrix means preventing redisplay. */
443 if (matrix->pool == NULL)
445 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
446 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
447 eassert (left >= 0 && right >= 0);
448 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
449 || right != matrix->right_margin_glyphs);
451 if (!marginal_areas_changed_p
452 && !fonts_changed_p
453 && !header_line_changed_p
454 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
455 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
456 && matrix->window_height == window_height
457 && matrix->window_vscroll == w->vscroll
458 && matrix->window_width == window_width)
459 return;
462 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
463 if (matrix->rows_allocated < dim.height)
465 int old_alloc = matrix->rows_allocated;
466 new_rows = dim.height - matrix->rows_allocated;
467 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
468 new_rows, INT_MAX, sizeof *matrix->rows);
469 memset (matrix->rows + old_alloc, 0,
470 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
472 else
473 new_rows = 0;
475 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
476 on a frame not using window-based redisplay. Set up pointers for
477 each row into the glyph pool. */
478 if (matrix->pool)
480 eassert (matrix->pool->glyphs);
482 if (w)
484 left = margin_glyphs_to_reserve (w, dim.width,
485 w->left_margin_cols);
486 right = margin_glyphs_to_reserve (w, dim.width,
487 w->right_margin_cols);
489 else
490 left = right = 0;
492 for (i = 0; i < dim.height; ++i)
494 struct glyph_row *row = &matrix->rows[i];
496 row->glyphs[LEFT_MARGIN_AREA]
497 = (matrix->pool->glyphs
498 + (y + i) * matrix->pool->ncolumns
499 + x);
501 if (w == NULL
502 || row == matrix->rows + dim.height - 1
503 || (row == matrix->rows && matrix->header_line_p))
505 row->glyphs[TEXT_AREA]
506 = row->glyphs[LEFT_MARGIN_AREA];
507 row->glyphs[RIGHT_MARGIN_AREA]
508 = row->glyphs[TEXT_AREA] + dim.width;
509 row->glyphs[LAST_AREA]
510 = row->glyphs[RIGHT_MARGIN_AREA];
512 else
514 row->glyphs[TEXT_AREA]
515 = row->glyphs[LEFT_MARGIN_AREA] + left;
516 row->glyphs[RIGHT_MARGIN_AREA]
517 = row->glyphs[TEXT_AREA] + dim.width - left - right;
518 row->glyphs[LAST_AREA]
519 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
523 matrix->left_margin_glyphs = left;
524 matrix->right_margin_glyphs = right;
526 else
528 /* If MATRIX->pool is null, MATRIX is responsible for managing
529 its own memory. It is a window matrix for window-based redisplay.
530 Allocate glyph memory from the heap. */
531 if (dim.width > matrix->matrix_w
532 || new_rows
533 || header_line_changed_p
534 || marginal_areas_changed_p)
536 struct glyph_row *row = matrix->rows;
537 struct glyph_row *end = row + matrix->rows_allocated;
539 while (row < end)
541 row->glyphs[LEFT_MARGIN_AREA]
542 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
543 dim.width, sizeof (struct glyph));
545 /* The mode line never has marginal areas. */
546 if (row == matrix->rows + dim.height - 1
547 || (row == matrix->rows && matrix->header_line_p))
549 row->glyphs[TEXT_AREA]
550 = row->glyphs[LEFT_MARGIN_AREA];
551 row->glyphs[RIGHT_MARGIN_AREA]
552 = row->glyphs[TEXT_AREA] + dim.width;
553 row->glyphs[LAST_AREA]
554 = row->glyphs[RIGHT_MARGIN_AREA];
556 else
558 row->glyphs[TEXT_AREA]
559 = row->glyphs[LEFT_MARGIN_AREA] + left;
560 row->glyphs[RIGHT_MARGIN_AREA]
561 = row->glyphs[TEXT_AREA] + dim.width - left - right;
562 row->glyphs[LAST_AREA]
563 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
565 ++row;
569 eassert (left >= 0 && right >= 0);
570 matrix->left_margin_glyphs = left;
571 matrix->right_margin_glyphs = right;
574 /* Number of rows to be used by MATRIX. */
575 matrix->nrows = dim.height;
576 eassert (matrix->nrows >= 0);
578 if (w)
580 if (matrix == w->current_matrix)
582 /* Mark rows in a current matrix of a window as not having
583 valid contents. It's important to not do this for
584 desired matrices. When Emacs starts, it may already be
585 building desired matrices when this function runs. */
586 if (window_width < 0)
587 window_width = window_box_width (w, -1);
589 /* Optimize the case that only the height has changed (C-x 2,
590 upper window). Invalidate all rows that are no longer part
591 of the window. */
592 if (!marginal_areas_changed_p
593 && !header_line_changed_p
594 && new_rows == 0
595 && dim.width == matrix->matrix_w
596 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
597 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
598 && matrix->window_width == window_width)
600 /* Find the last row in the window. */
601 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
602 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
604 ++i;
605 break;
608 /* Window end is invalid, if inside of the rows that
609 are invalidated below. */
610 if (INTEGERP (w->window_end_vpos)
611 && XFASTINT (w->window_end_vpos) >= i)
612 wset_window_end_valid (w, Qnil);
614 while (i < matrix->nrows)
615 matrix->rows[i++].enabled_p = 0;
617 else
619 for (i = 0; i < matrix->nrows; ++i)
620 matrix->rows[i].enabled_p = 0;
623 else if (matrix == w->desired_matrix)
625 /* Rows in desired matrices always have to be cleared;
626 redisplay expects this is the case when it runs, so it
627 had better be the case when we adjust matrices between
628 redisplays. */
629 for (i = 0; i < matrix->nrows; ++i)
630 matrix->rows[i].enabled_p = 0;
635 /* Remember last values to be able to optimize frame redraws. */
636 matrix->matrix_x = x;
637 matrix->matrix_y = y;
638 matrix->matrix_w = dim.width;
639 matrix->matrix_h = dim.height;
641 /* Record the top y location and height of W at the time the matrix
642 was last adjusted. This is used to optimize redisplay above. */
643 if (w)
645 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
646 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
647 matrix->window_height = window_height;
648 matrix->window_width = window_width;
649 matrix->window_vscroll = w->vscroll;
654 /* Reverse the contents of rows in MATRIX between START and END. The
655 contents of the row at END - 1 end up at START, END - 2 at START +
656 1 etc. This is part of the implementation of rotate_matrix (see
657 below). */
659 static void
660 reverse_rows (struct glyph_matrix *matrix, int start, int end)
662 int i, j;
664 for (i = start, j = end - 1; i < j; ++i, --j)
666 /* Non-ISO HP/UX compiler doesn't like auto struct
667 initialization. */
668 struct glyph_row temp;
669 temp = matrix->rows[i];
670 matrix->rows[i] = matrix->rows[j];
671 matrix->rows[j] = temp;
676 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
677 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
678 indices. (Note: this does not copy glyphs, only glyph pointers in
679 row structures are moved around).
681 The algorithm used for rotating the vector was, I believe, first
682 described by Kernighan. See the vector R as consisting of two
683 sub-vectors AB, where A has length BY for BY >= 0. The result
684 after rotating is then BA. Reverse both sub-vectors to get ArBr
685 and reverse the result to get (ArBr)r which is BA. Similar for
686 rotating right. */
688 void
689 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
691 if (by < 0)
693 /* Up (rotate left, i.e. towards lower indices). */
694 by = -by;
695 reverse_rows (matrix, first, first + by);
696 reverse_rows (matrix, first + by, last);
697 reverse_rows (matrix, first, last);
699 else if (by > 0)
701 /* Down (rotate right, i.e. towards higher indices). */
702 reverse_rows (matrix, last - by, last);
703 reverse_rows (matrix, first, last - by);
704 reverse_rows (matrix, first, last);
709 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
710 with indices START <= index < END. Increment positions by DELTA/
711 DELTA_BYTES. */
713 void
714 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
715 ptrdiff_t delta, ptrdiff_t delta_bytes)
717 /* Check that START and END are reasonable values. */
718 eassert (start >= 0 && start <= matrix->nrows);
719 eassert (end >= 0 && end <= matrix->nrows);
720 eassert (start <= end);
722 for (; start < end; ++start)
723 increment_row_positions (matrix->rows + start, delta, delta_bytes);
727 /* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
728 START and END are the row indices of the first and last + 1 row to clear. */
730 void
731 clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
733 eassert (start <= end);
734 eassert (start >= 0 && start < matrix->nrows);
735 eassert (end >= 0 && end <= matrix->nrows);
737 for (; start < end; ++start)
738 matrix->rows[start].enabled_p = 0;
742 /* Clear MATRIX.
744 Empty all rows in MATRIX by clearing their enabled_p flags.
745 The function prepare_desired_row will eventually really clear a row
746 when it sees one with a false enabled_p flag.
748 Reset update hints to default values. The only update hint
749 currently present is the flag MATRIX->no_scrolling_p. */
751 void
752 clear_glyph_matrix (struct glyph_matrix *matrix)
754 if (matrix)
756 clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
757 matrix->no_scrolling_p = 0;
762 /* Shift part of the glyph matrix MATRIX of window W up or down.
763 Increment y-positions in glyph rows between START and END by DY,
764 and recompute their visible height. */
766 void
767 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
769 int min_y, max_y;
771 eassert (start <= end);
772 eassert (start >= 0 && start < matrix->nrows);
773 eassert (end >= 0 && end <= matrix->nrows);
775 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
776 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
778 for (; start < end; ++start)
780 struct glyph_row *row = &matrix->rows[start];
782 row->y += dy;
783 row->visible_height = row->height;
785 if (row->y < min_y)
786 row->visible_height -= min_y - row->y;
787 if (row->y + row->height > max_y)
788 row->visible_height -= row->y + row->height - max_y;
789 if (row->fringe_bitmap_periodic_p)
790 row->redraw_fringe_bitmaps_p = 1;
795 /* Mark all rows in current matrices of frame F as invalid. Marking
796 invalid is done by setting enabled_p to zero for all rows in a
797 current matrix. */
799 void
800 clear_current_matrices (register struct frame *f)
802 /* Clear frame current matrix, if we have one. */
803 if (f->current_matrix)
804 clear_glyph_matrix (f->current_matrix);
806 /* Clear the matrix of the menu bar window, if such a window exists.
807 The menu bar window is currently used to display menus on X when
808 no toolkit support is compiled in. */
809 if (WINDOWP (f->menu_bar_window))
810 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
812 /* Clear the matrix of the tool-bar window, if any. */
813 if (WINDOWP (f->tool_bar_window))
814 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
816 /* Clear current window matrices. */
817 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
818 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
822 /* Clear out all display lines of F for a coming redisplay. */
824 void
825 clear_desired_matrices (register struct frame *f)
827 if (f->desired_matrix)
828 clear_glyph_matrix (f->desired_matrix);
830 if (WINDOWP (f->menu_bar_window))
831 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
833 if (WINDOWP (f->tool_bar_window))
834 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
836 /* Do it for window matrices. */
837 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
838 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
842 /* Clear matrices in window tree rooted in W. If DESIRED_P,
843 clear desired matrices, otherwise clear current matrices. */
845 static void
846 clear_window_matrices (struct window *w, bool desired_p)
848 while (w)
850 if (!NILP (w->hchild))
852 eassert (WINDOWP (w->hchild));
853 clear_window_matrices (XWINDOW (w->hchild), desired_p);
855 else if (!NILP (w->vchild))
857 eassert (WINDOWP (w->vchild));
858 clear_window_matrices (XWINDOW (w->vchild), desired_p);
860 else
862 if (desired_p)
863 clear_glyph_matrix (w->desired_matrix);
864 else
866 clear_glyph_matrix (w->current_matrix);
867 wset_window_end_valid (w, Qnil);
871 w = NILP (w->next) ? 0 : XWINDOW (w->next);
877 /***********************************************************************
878 Glyph Rows
880 See dispextern.h for an overall explanation of glyph rows.
881 ***********************************************************************/
883 /* Clear glyph row ROW. Do it in a way that makes it robust against
884 changes in the glyph_row structure, i.e. addition or removal of
885 structure members. */
887 static struct glyph_row null_row;
889 void
890 clear_glyph_row (struct glyph_row *row)
892 struct glyph *p[1 + LAST_AREA];
894 /* Save pointers. */
895 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
896 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
897 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
898 p[LAST_AREA] = row->glyphs[LAST_AREA];
900 /* Clear. */
901 *row = null_row;
903 /* Restore pointers. */
904 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
905 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
906 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
907 row->glyphs[LAST_AREA] = p[LAST_AREA];
909 #if 0 /* At some point, some bit-fields of struct glyph were not set,
910 which made glyphs unequal when compared with GLYPH_EQUAL_P.
911 Redisplay outputs such glyphs, and flickering effects were
912 the result. This also depended on the contents of memory
913 returned by xmalloc. If flickering happens again, activate
914 the code below. If the flickering is gone with that, chances
915 are that the flickering has the same reason as here. */
916 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
917 #endif
921 /* Make ROW an empty, enabled row of canonical character height,
922 in window W starting at y-position Y. */
924 void
925 blank_row (struct window *w, struct glyph_row *row, int y)
927 int min_y, max_y;
929 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
930 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
932 clear_glyph_row (row);
933 row->y = y;
934 row->ascent = row->phys_ascent = 0;
935 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
936 row->visible_height = row->height;
938 if (row->y < min_y)
939 row->visible_height -= min_y - row->y;
940 if (row->y + row->height > max_y)
941 row->visible_height -= row->y + row->height - max_y;
943 row->enabled_p = 1;
947 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
948 are the amounts by which to change positions. Note that the first
949 glyph of the text area of a row can have a buffer position even if
950 the used count of the text area is zero. Such rows display line
951 ends. */
953 static void
954 increment_row_positions (struct glyph_row *row,
955 ptrdiff_t delta, ptrdiff_t delta_bytes)
957 int area, i;
959 /* Increment start and end positions. */
960 MATRIX_ROW_START_CHARPOS (row) += delta;
961 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
962 MATRIX_ROW_END_CHARPOS (row) += delta;
963 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
964 CHARPOS (row->start.pos) += delta;
965 BYTEPOS (row->start.pos) += delta_bytes;
966 CHARPOS (row->end.pos) += delta;
967 BYTEPOS (row->end.pos) += delta_bytes;
969 if (!row->enabled_p)
970 return;
972 /* Increment positions in glyphs. */
973 for (area = 0; area < LAST_AREA; ++area)
974 for (i = 0; i < row->used[area]; ++i)
975 if (BUFFERP (row->glyphs[area][i].object)
976 && row->glyphs[area][i].charpos > 0)
977 row->glyphs[area][i].charpos += delta;
979 /* Capture the case of rows displaying a line end. */
980 if (row->used[TEXT_AREA] == 0
981 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
982 row->glyphs[TEXT_AREA]->charpos += delta;
986 #if 0
987 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
988 contents, i.e. glyph structure contents are exchanged between A and
989 B without changing glyph pointers in A and B. */
991 static void
992 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
994 int area;
996 for (area = 0; area < LAST_AREA; ++area)
998 /* Number of glyphs to swap. */
999 int max_used = max (a->used[area], b->used[area]);
1001 /* Start of glyphs in area of row A. */
1002 struct glyph *glyph_a = a->glyphs[area];
1004 /* End + 1 of glyphs in area of row A. */
1005 struct glyph *glyph_a_end = a->glyphs[max_used];
1007 /* Start of glyphs in area of row B. */
1008 struct glyph *glyph_b = b->glyphs[area];
1010 while (glyph_a < glyph_a_end)
1012 /* Non-ISO HP/UX compiler doesn't like auto struct
1013 initialization. */
1014 struct glyph temp;
1015 temp = *glyph_a;
1016 *glyph_a = *glyph_b;
1017 *glyph_b = temp;
1018 ++glyph_a;
1019 ++glyph_b;
1024 #endif /* 0 */
1026 /* Exchange pointers to glyph memory between glyph rows A and B. Also
1027 exchange the used[] array and the hash values of the rows, because
1028 these should all go together for the row's hash value to be
1029 correct. */
1031 static void
1032 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1034 int i;
1035 unsigned hash_tem = a->hash;
1037 for (i = 0; i < LAST_AREA + 1; ++i)
1039 struct glyph *temp = a->glyphs[i];
1041 a->glyphs[i] = b->glyphs[i];
1042 b->glyphs[i] = temp;
1043 if (i < LAST_AREA)
1045 short used_tem = a->used[i];
1047 a->used[i] = b->used[i];
1048 b->used[i] = used_tem;
1051 a->hash = b->hash;
1052 b->hash = hash_tem;
1056 /* Copy glyph row structure FROM to glyph row structure TO, except
1057 that glyph pointers, the `used' counts, and the hash values in the
1058 structures are left unchanged. */
1060 static void
1061 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1063 struct glyph *pointers[1 + LAST_AREA];
1064 short used[LAST_AREA];
1065 unsigned hashval;
1067 /* Save glyph pointers of TO. */
1068 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1069 memcpy (used, to->used, sizeof to->used);
1070 hashval = to->hash;
1072 /* Do a structure assignment. */
1073 *to = *from;
1075 /* Restore original pointers of TO. */
1076 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1077 memcpy (to->used, used, sizeof to->used);
1078 to->hash = hashval;
1082 /* Assign glyph row FROM to glyph row TO. This works like a structure
1083 assignment TO = FROM, except that glyph pointers are not copied but
1084 exchanged between TO and FROM. Pointers must be exchanged to avoid
1085 a memory leak. */
1087 static void
1088 assign_row (struct glyph_row *to, struct glyph_row *from)
1090 swap_glyph_pointers (to, from);
1091 copy_row_except_pointers (to, from);
1095 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1096 a row in a window matrix, is a slice of the glyph memory of the
1097 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1098 is true if the glyph memory of WINDOW_ROW is part of the glyph
1099 memory of FRAME_ROW. */
1101 #ifdef GLYPH_DEBUG
1103 static bool
1104 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1106 struct glyph *window_glyph_start = window_row->glyphs[0];
1107 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1108 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1110 return (frame_glyph_start <= window_glyph_start
1111 && window_glyph_start < frame_glyph_end);
1114 #endif /* GLYPH_DEBUG */
1116 #if 0
1118 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1119 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1120 in WINDOW_MATRIX is found satisfying the condition. */
1122 static struct glyph_row *
1123 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1124 struct glyph_matrix *frame_matrix, int row)
1126 int i;
1128 eassert (row >= 0 && row < frame_matrix->nrows);
1130 for (i = 0; i < window_matrix->nrows; ++i)
1131 if (glyph_row_slice_p (window_matrix->rows + i,
1132 frame_matrix->rows + row))
1133 break;
1135 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1138 #endif /* 0 */
1140 /* Prepare ROW for display. Desired rows are cleared lazily,
1141 i.e. they are only marked as to be cleared by setting their
1142 enabled_p flag to zero. When a row is to be displayed, a prior
1143 call to this function really clears it. */
1145 void
1146 prepare_desired_row (struct glyph_row *row)
1148 if (!row->enabled_p)
1150 bool rp = row->reversed_p;
1152 clear_glyph_row (row);
1153 row->enabled_p = 1;
1154 row->reversed_p = rp;
1159 /* Return a hash code for glyph row ROW. */
1161 static int
1162 line_hash_code (struct glyph_row *row)
1164 int hash = 0;
1166 if (row->enabled_p)
1168 struct glyph *glyph = row->glyphs[TEXT_AREA];
1169 struct glyph *end = glyph + row->used[TEXT_AREA];
1171 while (glyph < end)
1173 int c = glyph->u.ch;
1174 int face_id = glyph->face_id;
1175 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1176 c -= SPACEGLYPH;
1177 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1178 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1179 ++glyph;
1182 if (hash == 0)
1183 hash = 1;
1186 return hash;
1190 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1191 the number of characters in the line. If must_write_spaces is
1192 zero, leading and trailing spaces are ignored. */
1194 static int
1195 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1197 struct glyph_row *row = matrix->rows + vpos;
1198 struct glyph *beg = row->glyphs[TEXT_AREA];
1199 struct glyph *end = beg + row->used[TEXT_AREA];
1200 int len;
1201 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1202 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1204 /* Ignore trailing and leading spaces if we can. */
1205 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1207 /* Skip from the end over trailing spaces. */
1208 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1209 --end;
1211 /* All blank line. */
1212 if (end == beg)
1213 return 0;
1215 /* Skip over leading spaces. */
1216 while (CHAR_GLYPH_SPACE_P (*beg))
1217 ++beg;
1220 /* If we don't have a glyph-table, each glyph is one character,
1221 so return the number of glyphs. */
1222 if (glyph_table_base == 0)
1223 len = end - beg;
1224 else
1226 /* Otherwise, scan the glyphs and accumulate their total length
1227 in LEN. */
1228 len = 0;
1229 while (beg < end)
1231 GLYPH g;
1233 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1235 if (GLYPH_INVALID_P (g)
1236 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1237 len += 1;
1238 else
1239 len += GLYPH_LENGTH (glyph_table_base, g);
1241 ++beg;
1245 return len;
1249 /* Return true if the glyph rows A and B have equal contents.
1250 MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too. */
1252 static bool
1253 row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
1255 eassert (verify_row_hash (a));
1256 eassert (verify_row_hash (b));
1258 if (a == b)
1259 return 1;
1260 else if (a->hash != b->hash)
1261 return 0;
1262 else
1264 struct glyph *a_glyph, *b_glyph, *a_end;
1265 int area;
1267 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1268 return 0;
1270 /* Compare glyphs. */
1271 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1273 if (a->used[area] != b->used[area])
1274 return 0;
1276 a_glyph = a->glyphs[area];
1277 a_end = a_glyph + a->used[area];
1278 b_glyph = b->glyphs[area];
1280 while (a_glyph < a_end
1281 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1282 ++a_glyph, ++b_glyph;
1284 if (a_glyph != a_end)
1285 return 0;
1288 if (a->fill_line_p != b->fill_line_p
1289 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1290 || a->left_fringe_bitmap != b->left_fringe_bitmap
1291 || a->left_fringe_face_id != b->left_fringe_face_id
1292 || a->left_fringe_offset != b->left_fringe_offset
1293 || a->right_fringe_bitmap != b->right_fringe_bitmap
1294 || a->right_fringe_face_id != b->right_fringe_face_id
1295 || a->right_fringe_offset != b->right_fringe_offset
1296 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1297 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1298 || a->exact_window_width_line_p != b->exact_window_width_line_p
1299 || a->overlapped_p != b->overlapped_p
1300 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1301 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1302 || a->reversed_p != b->reversed_p
1303 /* Different partially visible characters on left margin. */
1304 || a->x != b->x
1305 /* Different height. */
1306 || a->ascent != b->ascent
1307 || a->phys_ascent != b->phys_ascent
1308 || a->phys_height != b->phys_height
1309 || a->visible_height != b->visible_height)
1310 return 0;
1313 return 1;
1318 /***********************************************************************
1319 Glyph Pool
1321 See dispextern.h for an overall explanation of glyph pools.
1322 ***********************************************************************/
1324 /* Allocate a glyph_pool structure. The structure returned is
1325 initialized with zeros. The global variable glyph_pool_count is
1326 incremented for each pool allocated. */
1328 static struct glyph_pool *
1329 new_glyph_pool (void)
1331 struct glyph_pool *result = xzalloc (sizeof *result);
1333 /* For memory leak and double deletion checking. */
1334 ++glyph_pool_count;
1336 return result;
1340 /* Free a glyph_pool structure POOL. The function may be called with
1341 a null POOL pointer. The global variable glyph_pool_count is
1342 decremented with every pool structure freed. If this count gets
1343 negative, more structures were freed than allocated, i.e. one
1344 structure must have been freed more than once or a bogus pointer
1345 was passed to free_glyph_pool. */
1347 static void
1348 free_glyph_pool (struct glyph_pool *pool)
1350 if (pool)
1352 /* More freed than allocated? */
1353 --glyph_pool_count;
1354 eassert (glyph_pool_count >= 0);
1356 xfree (pool->glyphs);
1357 xfree (pool);
1362 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1363 columns we need. This function never shrinks a pool. The only
1364 case in which this would make sense, would be when a frame's size
1365 is changed from a large value to a smaller one. But, if someone
1366 does it once, we can expect that he will do it again.
1368 Return true if the pool changed in a way which makes
1369 re-adjusting window glyph matrices necessary. */
1371 static bool
1372 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1374 ptrdiff_t needed;
1375 bool changed_p;
1377 changed_p = (pool->glyphs == 0
1378 || matrix_dim.height != pool->nrows
1379 || matrix_dim.width != pool->ncolumns);
1381 /* Enlarge the glyph pool. */
1382 needed = matrix_dim.width;
1383 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1384 memory_full (SIZE_MAX);
1385 needed *= matrix_dim.height;
1386 if (needed > pool->nglyphs)
1388 ptrdiff_t old_nglyphs = pool->nglyphs;
1389 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1390 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1391 memset (pool->glyphs + old_nglyphs, 0,
1392 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1395 /* Remember the number of rows and columns because (a) we use them
1396 to do sanity checks, and (b) the number of columns determines
1397 where rows in the frame matrix start---this must be available to
1398 determine pointers to rows of window sub-matrices. */
1399 pool->nrows = matrix_dim.height;
1400 pool->ncolumns = matrix_dim.width;
1402 return changed_p;
1407 /***********************************************************************
1408 Debug Code
1409 ***********************************************************************/
1411 #ifdef GLYPH_DEBUG
1414 /* Flush standard output. This is sometimes useful to call from the debugger.
1415 XXX Maybe this should be changed to flush the current terminal instead of
1416 stdout.
1419 void flush_stdout (void) EXTERNALLY_VISIBLE;
1421 void
1422 flush_stdout (void)
1424 fflush (stdout);
1428 /* Check that no glyph pointers have been lost in MATRIX. If a
1429 pointer has been lost, e.g. by using a structure assignment between
1430 rows, at least one pointer must occur more than once in the rows of
1431 MATRIX. */
1433 void
1434 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1436 int i, j;
1438 for (i = 0; i < matrix->nrows; ++i)
1439 for (j = 0; j < matrix->nrows; ++j)
1440 eassert (i == j
1441 || (matrix->rows[i].glyphs[TEXT_AREA]
1442 != matrix->rows[j].glyphs[TEXT_AREA]));
1446 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1448 struct glyph_row *
1449 matrix_row (struct glyph_matrix *matrix, int row)
1451 eassert (matrix && matrix->rows);
1452 eassert (row >= 0 && row < matrix->nrows);
1454 /* That's really too slow for normal testing because this function
1455 is called almost everywhere. Although---it's still astonishingly
1456 fast, so it is valuable to have for debugging purposes. */
1457 #if 0
1458 check_matrix_pointer_lossage (matrix);
1459 #endif
1461 return matrix->rows + row;
1465 #if 0 /* This function makes invalid assumptions when text is
1466 partially invisible. But it might come handy for debugging
1467 nevertheless. */
1469 /* Check invariants that must hold for an up to date current matrix of
1470 window W. */
1472 static void
1473 check_matrix_invariants (struct window *w)
1475 struct glyph_matrix *matrix = w->current_matrix;
1476 int yb = window_text_bottom_y (w);
1477 struct glyph_row *row = matrix->rows;
1478 struct glyph_row *last_text_row = NULL;
1479 struct buffer *saved = current_buffer;
1480 struct buffer *buffer = XBUFFER (w->buffer);
1481 int c;
1483 /* This can sometimes happen for a fresh window. */
1484 if (matrix->nrows < 2)
1485 return;
1487 set_buffer_temp (buffer);
1489 /* Note: last row is always reserved for the mode line. */
1490 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1491 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1493 struct glyph_row *next = row + 1;
1495 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1496 last_text_row = row;
1498 /* Check that character and byte positions are in sync. */
1499 eassert (MATRIX_ROW_START_BYTEPOS (row)
1500 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1501 eassert (BYTEPOS (row->start.pos)
1502 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1504 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1505 have such a position temporarily in case of a minibuffer
1506 displaying something like `[Sole completion]' at its end. */
1507 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1509 eassert (MATRIX_ROW_END_BYTEPOS (row)
1510 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1511 eassert (BYTEPOS (row->end.pos)
1512 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1515 /* Check that end position of `row' is equal to start position
1516 of next row. */
1517 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1519 eassert (MATRIX_ROW_END_CHARPOS (row)
1520 == MATRIX_ROW_START_CHARPOS (next));
1521 eassert (MATRIX_ROW_END_BYTEPOS (row)
1522 == MATRIX_ROW_START_BYTEPOS (next));
1523 eassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1524 eassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1526 row = next;
1529 eassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1530 eassert (w->desired_matrix->rows != NULL);
1531 set_buffer_temp (saved);
1534 #endif /* 0 */
1536 #endif /* GLYPH_DEBUG */
1540 /**********************************************************************
1541 Allocating/ Adjusting Glyph Matrices
1542 **********************************************************************/
1544 /* Allocate glyph matrices over a window tree for a frame-based
1545 redisplay
1547 X and Y are column/row within the frame glyph matrix where
1548 sub-matrices for the window tree rooted at WINDOW must be
1549 allocated. DIM_ONLY_P means that the caller of this
1550 function is only interested in the result matrix dimension, and
1551 matrix adjustments should not be performed.
1553 The function returns the total width/height of the sub-matrices of
1554 the window tree. If called on a frame root window, the computation
1555 will take the mini-buffer window into account.
1557 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1559 NEW_LEAF_MATRIX set if any window in the tree did not have a
1560 glyph matrices yet, and
1562 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1563 any window in the tree will be changed or have been changed (see
1564 DIM_ONLY_P)
1566 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1567 function.
1569 Windows are arranged into chains of windows on the same level
1570 through the next fields of window structures. Such a level can be
1571 either a sequence of horizontally adjacent windows from left to
1572 right, or a sequence of vertically adjacent windows from top to
1573 bottom. Each window in a horizontal sequence can be either a leaf
1574 window or a vertical sequence; a window in a vertical sequence can
1575 be either a leaf or a horizontal sequence. All windows in a
1576 horizontal sequence have the same height, and all windows in a
1577 vertical sequence have the same width.
1579 This function uses, for historical reasons, a more general
1580 algorithm to determine glyph matrix dimensions that would be
1581 necessary.
1583 The matrix height of a horizontal sequence is determined by the
1584 maximum height of any matrix in the sequence. The matrix width of
1585 a horizontal sequence is computed by adding up matrix widths of
1586 windows in the sequence.
1588 |<------- result width ------->|
1589 +---------+----------+---------+ ---
1590 | | | | |
1591 | | | |
1592 +---------+ | | result height
1593 | +---------+
1594 | | |
1595 +----------+ ---
1597 The matrix width of a vertical sequence is the maximum matrix width
1598 of any window in the sequence. Its height is computed by adding up
1599 matrix heights of windows in the sequence.
1601 |<---- result width -->|
1602 +---------+ ---
1603 | | |
1604 | | |
1605 +---------+--+ |
1606 | | |
1607 | | result height
1609 +------------+---------+ |
1610 | | |
1611 | | |
1612 +------------+---------+ --- */
1614 /* Bit indicating that a new matrix will be allocated or has been
1615 allocated. */
1617 #define NEW_LEAF_MATRIX (1 << 0)
1619 /* Bit indicating that a matrix will or has changed its location or
1620 size. */
1622 #define CHANGED_LEAF_MATRIX (1 << 1)
1624 static struct dim
1625 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1626 bool dim_only_p, int *window_change_flags)
1628 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1629 int x0 = x, y0 = y;
1630 int wmax = 0, hmax = 0;
1631 struct dim total;
1632 struct dim dim;
1633 struct window *w;
1634 bool in_horz_combination_p;
1636 /* What combination is WINDOW part of? Compute this once since the
1637 result is the same for all windows in the `next' chain. The
1638 special case of a root window (parent equal to nil) is treated
1639 like a vertical combination because a root window's `next'
1640 points to the mini-buffer window, if any, which is arranged
1641 vertically below other windows. */
1642 in_horz_combination_p
1643 = (!NILP (XWINDOW (window)->parent)
1644 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1646 /* For WINDOW and all windows on the same level. */
1649 w = XWINDOW (window);
1651 /* Get the dimension of the window sub-matrix for W, depending
1652 on whether this is a combination or a leaf window. */
1653 if (!NILP (w->hchild))
1654 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
1655 dim_only_p,
1656 window_change_flags);
1657 else if (!NILP (w->vchild))
1658 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
1659 dim_only_p,
1660 window_change_flags);
1661 else
1663 /* If not already done, allocate sub-matrix structures. */
1664 if (w->desired_matrix == NULL)
1666 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1667 w->current_matrix = new_glyph_matrix (f->current_pool);
1668 *window_change_flags |= NEW_LEAF_MATRIX;
1671 /* Width and height MUST be chosen so that there are no
1672 holes in the frame matrix. */
1673 dim.width = required_matrix_width (w);
1674 dim.height = required_matrix_height (w);
1676 /* Will matrix be re-allocated? */
1677 if (x != w->desired_matrix->matrix_x
1678 || y != w->desired_matrix->matrix_y
1679 || dim.width != w->desired_matrix->matrix_w
1680 || dim.height != w->desired_matrix->matrix_h
1681 || (margin_glyphs_to_reserve (w, dim.width,
1682 w->left_margin_cols)
1683 != w->desired_matrix->left_margin_glyphs)
1684 || (margin_glyphs_to_reserve (w, dim.width,
1685 w->right_margin_cols)
1686 != w->desired_matrix->right_margin_glyphs))
1687 *window_change_flags |= CHANGED_LEAF_MATRIX;
1689 /* Actually change matrices, if allowed. Do not consider
1690 CHANGED_LEAF_MATRIX computed above here because the pool
1691 may have been changed which we don't now here. We trust
1692 that we only will be called with DIM_ONLY_P when
1693 necessary. */
1694 if (!dim_only_p)
1696 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1697 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1701 /* If we are part of a horizontal combination, advance x for
1702 windows to the right of W; otherwise advance y for windows
1703 below W. */
1704 if (in_horz_combination_p)
1705 x += dim.width;
1706 else
1707 y += dim.height;
1709 /* Remember maximum glyph matrix dimensions. */
1710 wmax = max (wmax, dim.width);
1711 hmax = max (hmax, dim.height);
1713 /* Next window on same level. */
1714 window = w->next;
1716 while (!NILP (window));
1718 /* Set `total' to the total glyph matrix dimension of this window
1719 level. In a vertical combination, the width is the width of the
1720 widest window; the height is the y we finally reached, corrected
1721 by the y we started with. In a horizontal combination, the total
1722 height is the height of the tallest window, and the width is the
1723 x we finally reached, corrected by the x we started with. */
1724 if (in_horz_combination_p)
1726 total.width = x - x0;
1727 total.height = hmax;
1729 else
1731 total.width = wmax;
1732 total.height = y - y0;
1735 return total;
1739 /* Return the required height of glyph matrices for window W. */
1741 static int
1742 required_matrix_height (struct window *w)
1744 #ifdef HAVE_WINDOW_SYSTEM
1745 struct frame *f = XFRAME (w->frame);
1747 if (FRAME_WINDOW_P (f))
1749 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1750 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1751 return (((window_pixel_height + ch_height - 1)
1752 / ch_height) * w->nrows_scale_factor
1753 /* One partially visible line at the top and
1754 bottom of the window. */
1756 /* 2 for header and mode line. */
1757 + 2);
1759 #endif /* HAVE_WINDOW_SYSTEM */
1761 return WINDOW_TOTAL_LINES (w);
1765 /* Return the required width of glyph matrices for window W. */
1767 static int
1768 required_matrix_width (struct window *w)
1770 #ifdef HAVE_WINDOW_SYSTEM
1771 struct frame *f = XFRAME (w->frame);
1772 if (FRAME_WINDOW_P (f))
1774 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1775 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1777 /* Compute number of glyphs needed in a glyph row. */
1778 return (((window_pixel_width + ch_width - 1)
1779 / ch_width) * w->ncols_scale_factor
1780 /* 2 partially visible columns in the text area. */
1782 /* One partially visible column at the right
1783 edge of each marginal area. */
1784 + 1 + 1);
1786 #endif /* HAVE_WINDOW_SYSTEM */
1788 return XINT (w->total_cols);
1792 /* Allocate window matrices for window-based redisplay. W is the
1793 window whose matrices must be allocated/reallocated. */
1795 static void
1796 allocate_matrices_for_window_redisplay (struct window *w)
1798 while (w)
1800 if (!NILP (w->vchild))
1801 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
1802 else if (!NILP (w->hchild))
1803 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
1804 else
1806 /* W is a leaf window. */
1807 struct dim dim;
1809 /* If matrices are not yet allocated, allocate them now. */
1810 if (w->desired_matrix == NULL)
1812 w->desired_matrix = new_glyph_matrix (NULL);
1813 w->current_matrix = new_glyph_matrix (NULL);
1816 dim.width = required_matrix_width (w);
1817 dim.height = required_matrix_height (w);
1818 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1819 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1822 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1827 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1828 do it for all frames; otherwise do it just for the given frame.
1829 This function must be called when a new frame is created, its size
1830 changes, or its window configuration changes. */
1832 void
1833 adjust_glyphs (struct frame *f)
1835 /* Block input so that expose events and other events that access
1836 glyph matrices are not processed while we are changing them. */
1837 block_input ();
1839 if (f)
1840 adjust_frame_glyphs (f);
1841 else
1843 Lisp_Object tail, lisp_frame;
1845 FOR_EACH_FRAME (tail, lisp_frame)
1846 adjust_frame_glyphs (XFRAME (lisp_frame));
1849 unblock_input ();
1852 /* Allocate/reallocate glyph matrices of a single frame F. */
1854 static void
1855 adjust_frame_glyphs (struct frame *f)
1857 if (FRAME_WINDOW_P (f))
1858 adjust_frame_glyphs_for_window_redisplay (f);
1859 else
1860 adjust_frame_glyphs_for_frame_redisplay (f);
1862 /* Don't forget the message buffer and the buffer for
1863 decode_mode_spec. */
1864 adjust_frame_message_buffer (f);
1865 adjust_decode_mode_spec_buffer (f);
1867 f->glyphs_initialized_p = 1;
1870 /* Return true if any window in the tree has nonzero window margins. See
1871 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1872 static bool
1873 showing_window_margins_p (struct window *w)
1875 while (w)
1877 if (!NILP (w->hchild))
1879 if (showing_window_margins_p (XWINDOW (w->hchild)))
1880 return 1;
1882 else if (!NILP (w->vchild))
1884 if (showing_window_margins_p (XWINDOW (w->vchild)))
1885 return 1;
1887 else if (!NILP (w->left_margin_cols)
1888 || !NILP (w->right_margin_cols))
1889 return 1;
1891 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1893 return 0;
1897 /* In the window tree with root W, build current matrices of leaf
1898 windows from the frame's current matrix. */
1900 static void
1901 fake_current_matrices (Lisp_Object window)
1903 struct window *w;
1905 for (; !NILP (window); window = w->next)
1907 w = XWINDOW (window);
1909 if (!NILP (w->hchild))
1910 fake_current_matrices (w->hchild);
1911 else if (!NILP (w->vchild))
1912 fake_current_matrices (w->vchild);
1913 else
1915 int i;
1916 struct frame *f = XFRAME (w->frame);
1917 struct glyph_matrix *m = w->current_matrix;
1918 struct glyph_matrix *fm = f->current_matrix;
1920 eassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
1921 eassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
1923 for (i = 0; i < m->matrix_h; ++i)
1925 struct glyph_row *r = m->rows + i;
1926 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
1928 eassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
1929 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
1931 r->enabled_p = fr->enabled_p;
1932 if (r->enabled_p)
1934 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
1935 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
1936 r->used[TEXT_AREA] = (m->matrix_w
1937 - r->used[LEFT_MARGIN_AREA]
1938 - r->used[RIGHT_MARGIN_AREA]);
1939 r->mode_line_p = 0;
1947 /* Save away the contents of frame F's current frame matrix. Value is
1948 a glyph matrix holding the contents of F's current frame matrix. */
1950 static struct glyph_matrix *
1951 save_current_matrix (struct frame *f)
1953 int i;
1954 struct glyph_matrix *saved = xzalloc (sizeof *saved);
1955 saved->nrows = f->current_matrix->nrows;
1956 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
1958 for (i = 0; i < saved->nrows; ++i)
1960 struct glyph_row *from = f->current_matrix->rows + i;
1961 struct glyph_row *to = saved->rows + i;
1962 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1963 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
1964 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1965 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1968 return saved;
1972 /* Restore the contents of frame F's current frame matrix from SAVED,
1973 and free memory associated with SAVED. */
1975 static void
1976 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
1978 int i;
1980 for (i = 0; i < saved->nrows; ++i)
1982 struct glyph_row *from = saved->rows + i;
1983 struct glyph_row *to = f->current_matrix->rows + i;
1984 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1985 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1986 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1987 xfree (from->glyphs[TEXT_AREA]);
1990 xfree (saved->rows);
1991 xfree (saved);
1996 /* Allocate/reallocate glyph matrices of a single frame F for
1997 frame-based redisplay. */
1999 static void
2000 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
2002 struct dim matrix_dim;
2003 bool pool_changed_p;
2004 int window_change_flags;
2005 int top_window_y;
2007 if (!FRAME_LIVE_P (f))
2008 return;
2010 top_window_y = FRAME_TOP_MARGIN (f);
2012 /* Allocate glyph pool structures if not already done. */
2013 if (f->desired_pool == NULL)
2015 f->desired_pool = new_glyph_pool ();
2016 f->current_pool = new_glyph_pool ();
2019 /* Allocate frames matrix structures if needed. */
2020 if (f->desired_matrix == NULL)
2022 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2023 f->current_matrix = new_glyph_matrix (f->current_pool);
2026 /* Compute window glyph matrices. (This takes the mini-buffer
2027 window into account). The result is the size of the frame glyph
2028 matrix needed. The variable window_change_flags is set to a bit
2029 mask indicating whether new matrices will be allocated or
2030 existing matrices change their size or location within the frame
2031 matrix. */
2032 window_change_flags = 0;
2033 matrix_dim
2034 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2035 0, top_window_y,
2037 &window_change_flags);
2039 /* Add in menu bar lines, if any. */
2040 matrix_dim.height += top_window_y;
2042 /* Enlarge pools as necessary. */
2043 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2044 realloc_glyph_pool (f->current_pool, matrix_dim);
2046 /* Set up glyph pointers within window matrices. Do this only if
2047 absolutely necessary since it requires a frame redraw. */
2048 if (pool_changed_p || window_change_flags)
2050 /* Do it for window matrices. */
2051 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2052 0, top_window_y, 0,
2053 &window_change_flags);
2055 /* Size of frame matrices must equal size of frame. Note
2056 that we are called for X frames with window widths NOT equal
2057 to the frame width (from CHANGE_FRAME_SIZE_1). */
2058 eassert (matrix_dim.width == FRAME_COLS (f)
2059 && matrix_dim.height == FRAME_LINES (f));
2061 /* Pointers to glyph memory in glyph rows are exchanged during
2062 the update phase of redisplay, which means in general that a
2063 frame's current matrix consists of pointers into both the
2064 desired and current glyph pool of the frame. Adjusting a
2065 matrix sets the frame matrix up so that pointers are all into
2066 the same pool. If we want to preserve glyph contents of the
2067 current matrix over a call to adjust_glyph_matrix, we must
2068 make a copy of the current glyphs, and restore the current
2069 matrix' contents from that copy. */
2070 if (display_completed
2071 && !FRAME_GARBAGED_P (f)
2072 && matrix_dim.width == f->current_matrix->matrix_w
2073 && matrix_dim.height == f->current_matrix->matrix_h
2074 /* For some reason, the frame glyph matrix gets corrupted if
2075 any of the windows contain margins. I haven't been able
2076 to hunt down the reason, but for the moment this prevents
2077 the problem from manifesting. -- cyd */
2078 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2080 struct glyph_matrix *copy = save_current_matrix (f);
2081 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2082 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2083 restore_current_matrix (f, copy);
2084 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2086 else
2088 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2089 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2090 SET_FRAME_GARBAGED (f);
2096 /* Allocate/reallocate glyph matrices of a single frame F for
2097 window-based redisplay. */
2099 static void
2100 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2102 eassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2104 /* Allocate/reallocate window matrices. */
2105 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2107 #ifdef HAVE_X_WINDOWS
2108 /* Allocate/ reallocate matrices of the dummy window used to display
2109 the menu bar under X when no X toolkit support is available. */
2110 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2112 /* Allocate a dummy window if not already done. */
2113 struct window *w;
2114 if (NILP (f->menu_bar_window))
2116 Lisp_Object frame;
2117 fset_menu_bar_window (f, make_window ());
2118 w = XWINDOW (f->menu_bar_window);
2119 XSETFRAME (frame, f);
2120 wset_frame (w, frame);
2121 w->pseudo_window_p = 1;
2123 else
2124 w = XWINDOW (f->menu_bar_window);
2126 /* Set window dimensions to frame dimensions and allocate or
2127 adjust glyph matrices of W. */
2128 wset_top_line (w, make_number (0));
2129 wset_left_col (w, make_number (0));
2130 wset_total_lines (w, make_number (FRAME_MENU_BAR_LINES (f)));
2131 wset_total_cols (w, make_number (FRAME_TOTAL_COLS (f)));
2132 allocate_matrices_for_window_redisplay (w);
2134 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2135 #endif /* HAVE_X_WINDOWS */
2137 #ifndef USE_GTK
2139 /* Allocate/ reallocate matrices of the tool bar window. If we
2140 don't have a tool bar window yet, make one. */
2141 struct window *w;
2142 if (NILP (f->tool_bar_window))
2144 Lisp_Object frame;
2145 fset_tool_bar_window (f, make_window ());
2146 w = XWINDOW (f->tool_bar_window);
2147 XSETFRAME (frame, f);
2148 wset_frame (w, frame);
2149 w->pseudo_window_p = 1;
2151 else
2152 w = XWINDOW (f->tool_bar_window);
2154 wset_top_line (w, make_number (FRAME_MENU_BAR_LINES (f)));
2155 wset_left_col (w, make_number (0));
2156 wset_total_lines (w, make_number (FRAME_TOOL_BAR_LINES (f)));
2157 wset_total_cols (w, make_number (FRAME_TOTAL_COLS (f)));
2158 allocate_matrices_for_window_redisplay (w);
2160 #endif
2164 /* Adjust/ allocate message buffer of frame F.
2166 Note that the message buffer is never freed. Since I could not
2167 find a free in 19.34, I assume that freeing it would be
2168 problematic in some way and don't do it either.
2170 (Implementation note: It should be checked if we can free it
2171 eventually without causing trouble). */
2173 static void
2174 adjust_frame_message_buffer (struct frame *f)
2176 FRAME_MESSAGE_BUF (f) = xrealloc (FRAME_MESSAGE_BUF (f),
2177 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2181 /* Re-allocate buffer for decode_mode_spec on frame F. */
2183 static void
2184 adjust_decode_mode_spec_buffer (struct frame *f)
2186 f->decode_mode_spec_buffer = xrealloc (f->decode_mode_spec_buffer,
2187 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2192 /**********************************************************************
2193 Freeing Glyph Matrices
2194 **********************************************************************/
2196 /* Free glyph memory for a frame F. F may be null. This function can
2197 be called for the same frame more than once. The root window of
2198 F may be nil when this function is called. This is the case when
2199 the function is called when F is destroyed. */
2201 void
2202 free_glyphs (struct frame *f)
2204 if (f && f->glyphs_initialized_p)
2206 /* Block interrupt input so that we don't get surprised by an X
2207 event while we're in an inconsistent state. */
2208 block_input ();
2209 f->glyphs_initialized_p = 0;
2211 /* Release window sub-matrices. */
2212 if (!NILP (f->root_window))
2213 free_window_matrices (XWINDOW (f->root_window));
2215 /* Free the dummy window for menu bars without X toolkit and its
2216 glyph matrices. */
2217 if (!NILP (f->menu_bar_window))
2219 struct window *w = XWINDOW (f->menu_bar_window);
2220 free_glyph_matrix (w->desired_matrix);
2221 free_glyph_matrix (w->current_matrix);
2222 w->desired_matrix = w->current_matrix = NULL;
2223 fset_menu_bar_window (f, Qnil);
2226 /* Free the tool bar window and its glyph matrices. */
2227 if (!NILP (f->tool_bar_window))
2229 struct window *w = XWINDOW (f->tool_bar_window);
2230 free_glyph_matrix (w->desired_matrix);
2231 free_glyph_matrix (w->current_matrix);
2232 w->desired_matrix = w->current_matrix = NULL;
2233 fset_tool_bar_window (f, Qnil);
2236 /* Release frame glyph matrices. Reset fields to zero in
2237 case we are called a second time. */
2238 if (f->desired_matrix)
2240 free_glyph_matrix (f->desired_matrix);
2241 free_glyph_matrix (f->current_matrix);
2242 f->desired_matrix = f->current_matrix = NULL;
2245 /* Release glyph pools. */
2246 if (f->desired_pool)
2248 free_glyph_pool (f->desired_pool);
2249 free_glyph_pool (f->current_pool);
2250 f->desired_pool = f->current_pool = NULL;
2253 unblock_input ();
2258 /* Free glyph sub-matrices in the window tree rooted at W. This
2259 function may be called with a null pointer, and it may be called on
2260 the same tree more than once. */
2262 void
2263 free_window_matrices (struct window *w)
2265 while (w)
2267 if (!NILP (w->hchild))
2268 free_window_matrices (XWINDOW (w->hchild));
2269 else if (!NILP (w->vchild))
2270 free_window_matrices (XWINDOW (w->vchild));
2271 else
2273 /* This is a leaf window. Free its memory and reset fields
2274 to zero in case this function is called a second time for
2275 W. */
2276 free_glyph_matrix (w->current_matrix);
2277 free_glyph_matrix (w->desired_matrix);
2278 w->current_matrix = w->desired_matrix = NULL;
2281 /* Next window on same level. */
2282 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2287 /* Check glyph memory leaks. This function is called from
2288 shut_down_emacs. Note that frames are not destroyed when Emacs
2289 exits. We therefore free all glyph memory for all active frames
2290 explicitly and check that nothing is left allocated. */
2292 void
2293 check_glyph_memory (void)
2295 Lisp_Object tail, frame;
2297 /* Free glyph memory for all frames. */
2298 FOR_EACH_FRAME (tail, frame)
2299 free_glyphs (XFRAME (frame));
2301 /* Check that nothing is left allocated. */
2302 if (glyph_matrix_count)
2303 emacs_abort ();
2304 if (glyph_pool_count)
2305 emacs_abort ();
2310 /**********************************************************************
2311 Building a Frame Matrix
2312 **********************************************************************/
2314 /* Most of the redisplay code works on glyph matrices attached to
2315 windows. This is a good solution most of the time, but it is not
2316 suitable for terminal code. Terminal output functions cannot rely
2317 on being able to set an arbitrary terminal window. Instead they
2318 must be provided with a view of the whole frame, i.e. the whole
2319 screen. We build such a view by constructing a frame matrix from
2320 window matrices in this section.
2322 Windows that must be updated have their must_be_update_p flag set.
2323 For all such windows, their desired matrix is made part of the
2324 desired frame matrix. For other windows, their current matrix is
2325 made part of the desired frame matrix.
2327 +-----------------+----------------+
2328 | desired | desired |
2329 | | |
2330 +-----------------+----------------+
2331 | current |
2333 +----------------------------------+
2335 Desired window matrices can be made part of the frame matrix in a
2336 cheap way: We exploit the fact that the desired frame matrix and
2337 desired window matrices share their glyph memory. This is not
2338 possible for current window matrices. Their glyphs are copied to
2339 the desired frame matrix. The latter is equivalent to
2340 preserve_other_columns in the old redisplay.
2342 Used glyphs counters for frame matrix rows are the result of adding
2343 up glyph lengths of the window matrices. A line in the frame
2344 matrix is enabled, if a corresponding line in a window matrix is
2345 enabled.
2347 After building the desired frame matrix, it will be passed to
2348 terminal code, which will manipulate both the desired and current
2349 frame matrix. Changes applied to the frame's current matrix have
2350 to be visible in current window matrices afterwards, of course.
2352 This problem is solved like this:
2354 1. Window and frame matrices share glyphs. Window matrices are
2355 constructed in a way that their glyph contents ARE the glyph
2356 contents needed in a frame matrix. Thus, any modification of
2357 glyphs done in terminal code will be reflected in window matrices
2358 automatically.
2360 2. Exchanges of rows in a frame matrix done by terminal code are
2361 intercepted by hook functions so that corresponding row operations
2362 on window matrices can be performed. This is necessary because we
2363 use pointers to glyphs in glyph row structures. To satisfy the
2364 assumption of point 1 above that glyphs are updated implicitly in
2365 window matrices when they are manipulated via the frame matrix,
2366 window and frame matrix must of course agree where to find the
2367 glyphs for their rows. Possible manipulations that must be
2368 mirrored are assignments of rows of the desired frame matrix to the
2369 current frame matrix and scrolling the current frame matrix. */
2371 /* Build frame F's desired matrix from window matrices. Only windows
2372 which have the flag must_be_updated_p set have to be updated. Menu
2373 bar lines of a frame are not covered by window matrices, so make
2374 sure not to touch them in this function. */
2376 static void
2377 build_frame_matrix (struct frame *f)
2379 int i;
2381 /* F must have a frame matrix when this function is called. */
2382 eassert (!FRAME_WINDOW_P (f));
2384 /* Clear all rows in the frame matrix covered by window matrices.
2385 Menu bar lines are not covered by windows. */
2386 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2387 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2389 /* Build the matrix by walking the window tree. */
2390 build_frame_matrix_from_window_tree (f->desired_matrix,
2391 XWINDOW (FRAME_ROOT_WINDOW (f)));
2395 /* Walk a window tree, building a frame matrix MATRIX from window
2396 matrices. W is the root of a window tree. */
2398 static void
2399 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2401 while (w)
2403 if (!NILP (w->hchild))
2404 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2405 else if (!NILP (w->vchild))
2406 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2407 else
2408 build_frame_matrix_from_leaf_window (matrix, w);
2410 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2415 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2416 desired frame matrix built. W is a leaf window whose desired or
2417 current matrix is to be added to FRAME_MATRIX. W's flag
2418 must_be_updated_p determines which matrix it contributes to
2419 FRAME_MATRIX. If W->must_be_updated_p, W's desired matrix
2420 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2421 Adding a desired matrix means setting up used counters and such in
2422 frame rows, while adding a current window matrix to FRAME_MATRIX
2423 means copying glyphs. The latter case corresponds to
2424 preserve_other_columns in the old redisplay. */
2426 static void
2427 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2429 struct glyph_matrix *window_matrix;
2430 int window_y, frame_y;
2431 /* If non-zero, a glyph to insert at the right border of W. */
2432 GLYPH right_border_glyph;
2434 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2436 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2437 if (w->must_be_updated_p)
2439 window_matrix = w->desired_matrix;
2441 /* Decide whether we want to add a vertical border glyph. */
2442 if (!WINDOW_RIGHTMOST_P (w))
2444 struct Lisp_Char_Table *dp = window_display_table (w);
2445 Lisp_Object gc;
2447 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2448 if (dp
2449 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc)))
2451 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2452 spec_glyph_lookup_face (w, &right_border_glyph);
2455 if (GLYPH_FACE (right_border_glyph) <= 0)
2456 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2459 else
2460 window_matrix = w->current_matrix;
2462 /* For all rows in the window matrix and corresponding rows in the
2463 frame matrix. */
2464 window_y = 0;
2465 frame_y = window_matrix->matrix_y;
2466 while (window_y < window_matrix->nrows)
2468 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2469 struct glyph_row *window_row = window_matrix->rows + window_y;
2470 bool current_row_p = window_matrix == w->current_matrix;
2472 /* Fill up the frame row with spaces up to the left margin of the
2473 window row. */
2474 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2476 /* Fill up areas in the window matrix row with spaces. */
2477 fill_up_glyph_row_with_spaces (window_row);
2479 /* If only part of W's desired matrix has been built, and
2480 window_row wasn't displayed, use the corresponding current
2481 row instead. */
2482 if (window_matrix == w->desired_matrix
2483 && !window_row->enabled_p)
2485 window_row = w->current_matrix->rows + window_y;
2486 current_row_p = 1;
2489 if (current_row_p)
2491 /* Copy window row to frame row. */
2492 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2493 window_row->glyphs[0],
2494 window_matrix->matrix_w * sizeof (struct glyph));
2496 else
2498 eassert (window_row->enabled_p);
2500 /* Only when a desired row has been displayed, we want
2501 the corresponding frame row to be updated. */
2502 frame_row->enabled_p = 1;
2504 /* Maybe insert a vertical border between horizontally adjacent
2505 windows. */
2506 if (GLYPH_CHAR (right_border_glyph) != 0)
2508 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2509 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2512 #ifdef GLYPH_DEBUG
2513 /* Window row window_y must be a slice of frame row
2514 frame_y. */
2515 eassert (glyph_row_slice_p (window_row, frame_row));
2517 /* If rows are in sync, we don't have to copy glyphs because
2518 frame and window share glyphs. */
2520 strcpy (w->current_matrix->method, w->desired_matrix->method);
2521 add_window_display_history (w, w->current_matrix->method, 0);
2522 #endif
2525 /* Set number of used glyphs in the frame matrix. Since we fill
2526 up with spaces, and visit leaf windows from left to right it
2527 can be done simply. */
2528 frame_row->used[TEXT_AREA]
2529 = window_matrix->matrix_x + window_matrix->matrix_w;
2531 /* Next row. */
2532 ++window_y;
2533 ++frame_y;
2537 /* Given a user-specified glyph, possibly including a Lisp-level face
2538 ID, return a glyph that has a realized face ID.
2539 This is used for glyphs displayed specially and not part of the text;
2540 for instance, vertical separators, truncation markers, etc. */
2542 void
2543 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2545 int lface_id = GLYPH_FACE (*glyph);
2546 /* Convert the glyph's specified face to a realized (cache) face. */
2547 if (lface_id > 0)
2549 int face_id = merge_faces (XFRAME (w->frame),
2550 Qt, lface_id, DEFAULT_FACE_ID);
2551 SET_GLYPH_FACE (*glyph, face_id);
2555 /* Add spaces to a glyph row ROW in a window matrix.
2557 Each row has the form:
2559 +---------+-----------------------------+------------+
2560 | left | text | right |
2561 +---------+-----------------------------+------------+
2563 Left and right marginal areas are optional. This function adds
2564 spaces to areas so that there are no empty holes between areas.
2565 In other words: If the right area is not empty, the text area
2566 is filled up with spaces up to the right area. If the text area
2567 is not empty, the left area is filled up.
2569 To be called for frame-based redisplay, only. */
2571 static void
2572 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2574 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2575 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2576 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2580 /* Fill area AREA of glyph row ROW with spaces. To be called for
2581 frame-based redisplay only. */
2583 static void
2584 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2586 if (row->glyphs[area] < row->glyphs[area + 1])
2588 struct glyph *end = row->glyphs[area + 1];
2589 struct glyph *text = row->glyphs[area] + row->used[area];
2591 while (text < end)
2592 *text++ = space_glyph;
2593 row->used[area] = text - row->glyphs[area];
2598 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2599 reached. In frame matrices only one area, TEXT_AREA, is used. */
2601 static void
2602 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2604 int i = row->used[TEXT_AREA];
2605 struct glyph *glyph = row->glyphs[TEXT_AREA];
2607 while (i < upto)
2608 glyph[i++] = space_glyph;
2610 row->used[TEXT_AREA] = i;
2615 /**********************************************************************
2616 Mirroring operations on frame matrices in window matrices
2617 **********************************************************************/
2619 /* Set frame being updated via frame-based redisplay to F. This
2620 function must be called before updates to make explicit that we are
2621 working on frame matrices or not. */
2623 static void
2624 set_frame_matrix_frame (struct frame *f)
2626 frame_matrix_frame = f;
2630 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2631 DESIRED_MATRIX is the desired matrix corresponding to
2632 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2633 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2634 frame_matrix_frame is non-null, this indicates that the exchange is
2635 done in frame matrices, and that we have to perform analogous
2636 operations in window matrices of frame_matrix_frame. */
2638 static void
2639 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2641 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2642 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2643 bool mouse_face_p = current_row->mouse_face_p;
2645 /* Do current_row = desired_row. This exchanges glyph pointers
2646 between both rows, and does a structure assignment otherwise. */
2647 assign_row (current_row, desired_row);
2649 /* Enable current_row to mark it as valid. */
2650 current_row->enabled_p = 1;
2651 current_row->mouse_face_p = mouse_face_p;
2653 /* If we are called on frame matrices, perform analogous operations
2654 for window matrices. */
2655 if (frame_matrix_frame)
2656 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2660 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2661 W's frame which has been made current (by swapping pointers between
2662 current and desired matrix). Perform analogous operations in the
2663 matrices of leaf windows in the window tree rooted at W. */
2665 static void
2666 mirror_make_current (struct window *w, int frame_row)
2668 while (w)
2670 if (!NILP (w->hchild))
2671 mirror_make_current (XWINDOW (w->hchild), frame_row);
2672 else if (!NILP (w->vchild))
2673 mirror_make_current (XWINDOW (w->vchild), frame_row);
2674 else
2676 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2677 here because the checks performed in debug mode there
2678 will not allow the conversion. */
2679 int row = frame_row - w->desired_matrix->matrix_y;
2681 /* If FRAME_ROW is within W, assign the desired row to the
2682 current row (exchanging glyph pointers). */
2683 if (row >= 0 && row < w->desired_matrix->matrix_h)
2685 struct glyph_row *current_row
2686 = MATRIX_ROW (w->current_matrix, row);
2687 struct glyph_row *desired_row
2688 = MATRIX_ROW (w->desired_matrix, row);
2690 if (desired_row->enabled_p)
2691 assign_row (current_row, desired_row);
2692 else
2693 swap_glyph_pointers (desired_row, current_row);
2694 current_row->enabled_p = 1;
2696 /* Set the Y coordinate of the mode/header line's row.
2697 It is needed in draw_row_with_mouse_face to find the
2698 screen coordinates. (Window-based redisplay sets
2699 this in update_window, but no one seems to do that
2700 for frame-based redisplay.) */
2701 if (current_row->mode_line_p)
2702 current_row->y = row;
2706 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2711 /* Perform row dance after scrolling. We are working on the range of
2712 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2713 including) in MATRIX. COPY_FROM is a vector containing, for each
2714 row I in the range 0 <= I < NLINES, the index of the original line
2715 to move to I. This index is relative to the row range, i.e. 0 <=
2716 index < NLINES. RETAINED_P is a vector containing zero for each
2717 row 0 <= I < NLINES which is empty.
2719 This function is called from do_scrolling and do_direct_scrolling. */
2721 void
2722 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2723 int *copy_from, char *retained_p)
2725 /* A copy of original rows. */
2726 struct glyph_row *old_rows;
2728 /* Rows to assign to. */
2729 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2731 int i;
2733 /* Make a copy of the original rows. */
2734 old_rows = alloca (nlines * sizeof *old_rows);
2735 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2737 /* Assign new rows, maybe clear lines. */
2738 for (i = 0; i < nlines; ++i)
2740 bool enabled_before_p = new_rows[i].enabled_p;
2742 eassert (i + unchanged_at_top < matrix->nrows);
2743 eassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2744 new_rows[i] = old_rows[copy_from[i]];
2745 new_rows[i].enabled_p = enabled_before_p;
2747 /* RETAINED_P is zero for empty lines. */
2748 if (!retained_p[copy_from[i]])
2749 new_rows[i].enabled_p = 0;
2752 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2753 if (frame_matrix_frame)
2754 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2755 unchanged_at_top, nlines, copy_from, retained_p);
2759 /* Synchronize glyph pointers in the current matrix of window W with
2760 the current frame matrix. */
2762 static void
2763 sync_window_with_frame_matrix_rows (struct window *w)
2765 struct frame *f = XFRAME (w->frame);
2766 struct glyph_row *window_row, *window_row_end, *frame_row;
2767 int left, right, x, width;
2769 /* Preconditions: W must be a leaf window on a tty frame. */
2770 eassert (NILP (w->hchild) && NILP (w->vchild));
2771 eassert (!FRAME_WINDOW_P (f));
2773 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2774 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2775 x = w->current_matrix->matrix_x;
2776 width = w->current_matrix->matrix_w;
2778 window_row = w->current_matrix->rows;
2779 window_row_end = window_row + w->current_matrix->nrows;
2780 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2782 for (; window_row < window_row_end; ++window_row, ++frame_row)
2784 window_row->glyphs[LEFT_MARGIN_AREA]
2785 = frame_row->glyphs[0] + x;
2786 window_row->glyphs[TEXT_AREA]
2787 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2788 window_row->glyphs[LAST_AREA]
2789 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2790 window_row->glyphs[RIGHT_MARGIN_AREA]
2791 = window_row->glyphs[LAST_AREA] - right;
2796 /* Return the window in the window tree rooted in W containing frame
2797 row ROW. Value is null if none is found. */
2799 static struct window *
2800 frame_row_to_window (struct window *w, int row)
2802 struct window *found = NULL;
2804 while (w && !found)
2806 if (!NILP (w->hchild))
2807 found = frame_row_to_window (XWINDOW (w->hchild), row);
2808 else if (!NILP (w->vchild))
2809 found = frame_row_to_window (XWINDOW (w->vchild), row);
2810 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2811 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2812 found = w;
2814 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2817 return found;
2821 /* Perform a line dance in the window tree rooted at W, after
2822 scrolling a frame matrix in mirrored_line_dance.
2824 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2825 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2826 COPY_FROM is a vector containing, for each row I in the range 0 <=
2827 I < NLINES, the index of the original line to move to I. This
2828 index is relative to the row range, i.e. 0 <= index < NLINES.
2829 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2830 which is empty. */
2832 static void
2833 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2835 while (w)
2837 if (!NILP (w->hchild))
2838 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
2839 nlines, copy_from, retained_p);
2840 else if (!NILP (w->vchild))
2841 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
2842 nlines, copy_from, retained_p);
2843 else
2845 /* W is a leaf window, and we are working on its current
2846 matrix m. */
2847 struct glyph_matrix *m = w->current_matrix;
2848 int i;
2849 bool sync_p = 0;
2850 struct glyph_row *old_rows;
2852 /* Make a copy of the original rows of matrix m. */
2853 old_rows = alloca (m->nrows * sizeof *old_rows);
2854 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2856 for (i = 0; i < nlines; ++i)
2858 /* Frame relative line assigned to. */
2859 int frame_to = i + unchanged_at_top;
2861 /* Frame relative line assigned. */
2862 int frame_from = copy_from[i] + unchanged_at_top;
2864 /* Window relative line assigned to. */
2865 int window_to = frame_to - m->matrix_y;
2867 /* Window relative line assigned. */
2868 int window_from = frame_from - m->matrix_y;
2870 /* Is assigned line inside window? */
2871 bool from_inside_window_p
2872 = window_from >= 0 && window_from < m->matrix_h;
2874 /* Is assigned to line inside window? */
2875 bool to_inside_window_p
2876 = window_to >= 0 && window_to < m->matrix_h;
2878 if (from_inside_window_p && to_inside_window_p)
2880 /* Do the assignment. The enabled_p flag is saved
2881 over the assignment because the old redisplay did
2882 that. */
2883 bool enabled_before_p = m->rows[window_to].enabled_p;
2884 m->rows[window_to] = old_rows[window_from];
2885 m->rows[window_to].enabled_p = enabled_before_p;
2887 /* If frame line is empty, window line is empty, too. */
2888 if (!retained_p[copy_from[i]])
2889 m->rows[window_to].enabled_p = 0;
2891 else if (to_inside_window_p)
2893 /* A copy between windows. This is an infrequent
2894 case not worth optimizing. */
2895 struct frame *f = XFRAME (w->frame);
2896 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2897 struct window *w2;
2898 struct glyph_matrix *m2;
2899 int m2_from;
2901 w2 = frame_row_to_window (root, frame_from);
2902 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2903 -nw', FROM_FRAME sometimes has no associated window.
2904 This check avoids a segfault if W2 is null. */
2905 if (w2)
2907 m2 = w2->current_matrix;
2908 m2_from = frame_from - m2->matrix_y;
2909 copy_row_except_pointers (m->rows + window_to,
2910 m2->rows + m2_from);
2912 /* If frame line is empty, window line is empty, too. */
2913 if (!retained_p[copy_from[i]])
2914 m->rows[window_to].enabled_p = 0;
2916 sync_p = 1;
2918 else if (from_inside_window_p)
2919 sync_p = 1;
2922 /* If there was a copy between windows, make sure glyph
2923 pointers are in sync with the frame matrix. */
2924 if (sync_p)
2925 sync_window_with_frame_matrix_rows (w);
2927 /* Check that no pointers are lost. */
2928 CHECK_MATRIX (m);
2931 /* Next window on same level. */
2932 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2937 #ifdef GLYPH_DEBUG
2939 /* Check that window and frame matrices agree about their
2940 understanding where glyphs of the rows are to find. For each
2941 window in the window tree rooted at W, check that rows in the
2942 matrices of leaf window agree with their frame matrices about
2943 glyph pointers. */
2945 static void
2946 check_window_matrix_pointers (struct window *w)
2948 while (w)
2950 if (!NILP (w->hchild))
2951 check_window_matrix_pointers (XWINDOW (w->hchild));
2952 else if (!NILP (w->vchild))
2953 check_window_matrix_pointers (XWINDOW (w->vchild));
2954 else
2956 struct frame *f = XFRAME (w->frame);
2957 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
2958 check_matrix_pointers (w->current_matrix, f->current_matrix);
2961 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2966 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
2967 a window and FRAME_MATRIX is the corresponding frame matrix. For
2968 each row in WINDOW_MATRIX check that it's a slice of the
2969 corresponding frame row. If it isn't, abort. */
2971 static void
2972 check_matrix_pointers (struct glyph_matrix *window_matrix,
2973 struct glyph_matrix *frame_matrix)
2975 /* Row number in WINDOW_MATRIX. */
2976 int i = 0;
2978 /* Row number corresponding to I in FRAME_MATRIX. */
2979 int j = window_matrix->matrix_y;
2981 /* For all rows check that the row in the window matrix is a
2982 slice of the row in the frame matrix. If it isn't we didn't
2983 mirror an operation on the frame matrix correctly. */
2984 while (i < window_matrix->nrows)
2986 if (!glyph_row_slice_p (window_matrix->rows + i,
2987 frame_matrix->rows + j))
2988 emacs_abort ();
2989 ++i, ++j;
2993 #endif /* GLYPH_DEBUG */
2997 /**********************************************************************
2998 VPOS and HPOS translations
2999 **********************************************************************/
3001 #ifdef GLYPH_DEBUG
3003 /* Translate vertical position VPOS which is relative to window W to a
3004 vertical position relative to W's frame. */
3006 static int
3007 window_to_frame_vpos (struct window *w, int vpos)
3009 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3010 eassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
3011 vpos += WINDOW_TOP_EDGE_LINE (w);
3012 eassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
3013 return vpos;
3017 /* Translate horizontal position HPOS which is relative to window W to
3018 a horizontal position relative to W's frame. */
3020 static int
3021 window_to_frame_hpos (struct window *w, int hpos)
3023 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3024 hpos += WINDOW_LEFT_EDGE_COL (w);
3025 return hpos;
3028 #endif /* GLYPH_DEBUG */
3032 /**********************************************************************
3033 Redrawing Frames
3034 **********************************************************************/
3036 /* Redraw frame F. */
3038 void
3039 redraw_frame (struct frame *f)
3041 /* Error if F has no glyphs. */
3042 eassert (f->glyphs_initialized_p);
3043 update_begin (f);
3044 #ifdef MSDOS
3045 if (FRAME_MSDOS_P (f))
3046 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3047 #endif
3048 clear_frame (f);
3049 clear_current_matrices (f);
3050 update_end (f);
3051 if (FRAME_TERMCAP_P (f))
3052 fflush (FRAME_TTY (f)->output);
3053 windows_or_buffers_changed++;
3054 /* Mark all windows as inaccurate, so that every window will have
3055 its redisplay done. */
3056 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3057 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3058 f->garbaged = 0;
3061 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 0, 1, 0,
3062 doc: /* Clear frame FRAME and output again what is supposed to appear on it.
3063 If FRAME is omitted or nil, the selected frame is used. */)
3064 (Lisp_Object frame)
3066 redraw_frame (decode_live_frame (frame));
3067 return Qnil;
3070 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3071 doc: /* Clear and redisplay all visible frames. */)
3072 (void)
3074 Lisp_Object tail, frame;
3076 FOR_EACH_FRAME (tail, frame)
3077 if (FRAME_VISIBLE_P (XFRAME (frame)))
3078 redraw_frame (XFRAME (frame));
3080 return Qnil;
3085 /***********************************************************************
3086 Frame Update
3087 ***********************************************************************/
3089 /* Update frame F based on the data in desired matrices.
3091 If FORCE_P, don't let redisplay be stopped by detecting pending input.
3092 If INHIBIT_HAIRY_ID_P, don't try scrolling.
3094 Value is true if redisplay was stopped due to pending input. */
3096 bool
3097 update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
3099 /* True means display has been paused because of pending input. */
3100 bool paused_p;
3101 struct window *root_window = XWINDOW (f->root_window);
3103 if (redisplay_dont_pause)
3104 force_p = 1;
3105 else if (NILP (Vredisplay_preemption_period))
3106 force_p = 1;
3107 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3109 double p = XFLOATINT (Vredisplay_preemption_period);
3111 if (detect_input_pending_ignore_squeezables ())
3113 paused_p = 1;
3114 goto do_pause;
3117 preemption_period = EMACS_TIME_FROM_DOUBLE (p);
3118 preemption_next_check = add_emacs_time (current_emacs_time (),
3119 preemption_period);
3122 if (FRAME_WINDOW_P (f))
3124 /* We are working on window matrix basis. All windows whose
3125 flag must_be_updated_p is set have to be updated. */
3127 /* Record that we are not working on frame matrices. */
3128 set_frame_matrix_frame (NULL);
3130 /* Update all windows in the window tree of F, maybe stopping
3131 when pending input is detected. */
3132 update_begin (f);
3134 /* Update the menu bar on X frames that don't have toolkit
3135 support. */
3136 if (WINDOWP (f->menu_bar_window))
3137 update_window (XWINDOW (f->menu_bar_window), 1);
3139 /* Update the tool-bar window, if present. */
3140 if (WINDOWP (f->tool_bar_window))
3142 struct window *w = XWINDOW (f->tool_bar_window);
3144 /* Update tool-bar window. */
3145 if (w->must_be_updated_p)
3147 Lisp_Object tem;
3149 update_window (w, 1);
3150 w->must_be_updated_p = 0;
3152 /* Swap tool-bar strings. We swap because we want to
3153 reuse strings. */
3154 tem = f->current_tool_bar_string;
3155 fset_current_tool_bar_string (f, f->desired_tool_bar_string);
3156 fset_desired_tool_bar_string (f, tem);
3161 /* Update windows. */
3162 paused_p = update_window_tree (root_window, force_p);
3163 update_end (f);
3165 /* This flush is a performance bottleneck under X,
3166 and it doesn't seem to be necessary anyway (in general).
3167 It is necessary when resizing the window with the mouse, or
3168 at least the fringes are not redrawn in a timely manner. ++kfs */
3169 if (f->force_flush_display_p)
3171 FRAME_RIF (f)->flush_display (f);
3172 f->force_flush_display_p = 0;
3175 else
3177 /* We are working on frame matrix basis. Set the frame on whose
3178 frame matrix we operate. */
3179 set_frame_matrix_frame (f);
3181 /* Build F's desired matrix from window matrices. */
3182 build_frame_matrix (f);
3184 /* Update the display */
3185 update_begin (f);
3186 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3187 update_end (f);
3189 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3191 if (FRAME_TTY (f)->termscript)
3192 fflush (FRAME_TTY (f)->termscript);
3193 if (FRAME_TERMCAP_P (f))
3194 fflush (FRAME_TTY (f)->output);
3197 /* Check window matrices for lost pointers. */
3198 #ifdef GLYPH_DEBUG
3199 check_window_matrix_pointers (root_window);
3200 add_frame_display_history (f, paused_p);
3201 #endif
3204 do_pause:
3205 /* Reset flags indicating that a window should be updated. */
3206 set_window_update_flags (root_window, 0);
3208 display_completed = !paused_p;
3209 return paused_p;
3214 /************************************************************************
3215 Window-based updates
3216 ************************************************************************/
3218 /* Perform updates in window tree rooted at W.
3219 If FORCE_P, don't stop updating if input is pending. */
3221 static bool
3222 update_window_tree (struct window *w, bool force_p)
3224 bool paused_p = 0;
3226 while (w && !paused_p)
3228 if (!NILP (w->hchild))
3229 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3230 else if (!NILP (w->vchild))
3231 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3232 else if (w->must_be_updated_p)
3233 paused_p |= update_window (w, force_p);
3235 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3238 return paused_p;
3242 /* Update window W if its flag must_be_updated_p is set.
3243 If FORCE_P, don't stop updating if input is pending. */
3245 void
3246 update_single_window (struct window *w, bool force_p)
3248 if (w->must_be_updated_p)
3250 struct frame *f = XFRAME (WINDOW_FRAME (w));
3252 /* Record that this is not a frame-based redisplay. */
3253 set_frame_matrix_frame (NULL);
3255 if (redisplay_dont_pause)
3256 force_p = 1;
3257 else if (NILP (Vredisplay_preemption_period))
3258 force_p = 1;
3259 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3261 double p = XFLOATINT (Vredisplay_preemption_period);
3262 preemption_period = EMACS_TIME_FROM_DOUBLE (p);
3263 preemption_next_check = add_emacs_time (current_emacs_time (),
3264 preemption_period);
3267 /* Update W. */
3268 update_begin (f);
3269 update_window (w, force_p);
3270 update_end (f);
3272 /* Reset flag in W. */
3273 w->must_be_updated_p = 0;
3277 #ifdef HAVE_WINDOW_SYSTEM
3279 /* Redraw lines from the current matrix of window W that are
3280 overlapped by other rows. YB is bottom-most y-position in W. */
3282 static void
3283 redraw_overlapped_rows (struct window *w, int yb)
3285 int i;
3286 struct frame *f = XFRAME (WINDOW_FRAME (w));
3288 /* If rows overlapping others have been changed, the rows being
3289 overlapped have to be redrawn. This won't draw lines that have
3290 already been drawn in update_window_line because overlapped_p in
3291 desired rows is 0, so after row assignment overlapped_p in
3292 current rows is 0. */
3293 for (i = 0; i < w->current_matrix->nrows; ++i)
3295 struct glyph_row *row = w->current_matrix->rows + i;
3297 if (!row->enabled_p)
3298 break;
3299 else if (row->mode_line_p)
3300 continue;
3302 if (row->overlapped_p)
3304 enum glyph_row_area area;
3306 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3308 updated_row = row;
3309 updated_area = area;
3310 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3311 area == TEXT_AREA ? row->x : 0);
3312 if (row->used[area])
3313 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3314 row->used[area]);
3315 FRAME_RIF (f)->clear_end_of_line (-1);
3318 row->overlapped_p = 0;
3321 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3322 break;
3327 /* Redraw lines from the current matrix of window W that overlap
3328 others. YB is bottom-most y-position in W. */
3330 static void
3331 redraw_overlapping_rows (struct window *w, int yb)
3333 int i, bottom_y;
3334 struct glyph_row *row;
3335 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3337 for (i = 0; i < w->current_matrix->nrows; ++i)
3339 row = w->current_matrix->rows + i;
3341 if (!row->enabled_p)
3342 break;
3343 else if (row->mode_line_p)
3344 continue;
3346 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3348 if (row->overlapping_p)
3350 int overlaps = 0;
3352 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3353 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3354 overlaps |= OVERLAPS_PRED;
3355 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3356 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3357 overlaps |= OVERLAPS_SUCC;
3359 if (overlaps)
3361 if (row->used[LEFT_MARGIN_AREA])
3362 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3364 if (row->used[TEXT_AREA])
3365 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3367 if (row->used[RIGHT_MARGIN_AREA])
3368 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3370 /* Record in neighbor rows that ROW overwrites part of
3371 their display. */
3372 if (overlaps & OVERLAPS_PRED)
3373 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3374 if (overlaps & OVERLAPS_SUCC)
3375 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3379 if (bottom_y >= yb)
3380 break;
3384 #endif /* HAVE_WINDOW_SYSTEM */
3387 #if defined GLYPH_DEBUG && 0
3389 /* Check that no row in the current matrix of window W is enabled
3390 which is below what's displayed in the window. */
3392 static void
3393 check_current_matrix_flags (struct window *w)
3395 bool last_seen_p = 0;
3396 int i, yb = window_text_bottom_y (w);
3398 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3400 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3401 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3402 last_seen_p = 1;
3403 else if (last_seen_p && row->enabled_p)
3404 emacs_abort ();
3408 #endif /* GLYPH_DEBUG */
3411 /* Update display of window W.
3412 If FORCE_P, don't stop updating when input is pending. */
3414 static bool
3415 update_window (struct window *w, bool force_p)
3417 struct glyph_matrix *desired_matrix = w->desired_matrix;
3418 bool paused_p;
3419 #if !PERIODIC_PREEMPTION_CHECKING
3420 int preempt_count = baud_rate / 2400 + 1;
3421 #endif
3422 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3423 #ifdef GLYPH_DEBUG
3424 /* Check that W's frame doesn't have glyph matrices. */
3425 eassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3426 #endif
3428 /* Check pending input the first time so that we can quickly return. */
3429 #if !PERIODIC_PREEMPTION_CHECKING
3430 if (!force_p)
3431 detect_input_pending_ignore_squeezables ();
3432 #endif
3434 /* If forced to complete the update, or if no input is pending, do
3435 the update. */
3436 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3438 struct glyph_row *row, *end;
3439 struct glyph_row *mode_line_row;
3440 struct glyph_row *header_line_row;
3441 int yb;
3442 bool changed_p = 0, mouse_face_overwritten_p = 0;
3443 #if ! PERIODIC_PREEMPTION_CHECKING
3444 int n_updated = 0;
3445 #endif
3447 rif->update_window_begin_hook (w);
3448 yb = window_text_bottom_y (w);
3449 row = desired_matrix->rows;
3450 end = row + desired_matrix->nrows - 1;
3452 /* Take note of the header line, if there is one. We will
3453 update it below, after updating all of the window's lines. */
3454 if (row->mode_line_p)
3456 header_line_row = row;
3457 ++row;
3459 else
3460 header_line_row = NULL;
3462 /* Update the mode line, if necessary. */
3463 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3464 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3466 mode_line_row->y = yb;
3467 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3468 desired_matrix),
3469 &mouse_face_overwritten_p);
3472 /* Find first enabled row. Optimizations in redisplay_internal
3473 may lead to an update with only one row enabled. There may
3474 be also completely empty matrices. */
3475 while (row < end && !row->enabled_p)
3476 ++row;
3478 /* Try reusing part of the display by copying. */
3479 if (row < end && !desired_matrix->no_scrolling_p)
3481 int rc = scrolling_window (w, header_line_row != NULL);
3482 if (rc < 0)
3484 /* All rows were found to be equal. */
3485 paused_p = 0;
3486 goto set_cursor;
3488 else if (rc > 0)
3490 /* We've scrolled the display. */
3491 force_p = 1;
3492 changed_p = 1;
3496 /* Update the rest of the lines. */
3497 for (; row < end && (force_p || !input_pending); ++row)
3498 /* scrolling_window resets the enabled_p flag of the rows it
3499 reuses from current_matrix. */
3500 if (row->enabled_p)
3502 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3503 int i;
3505 /* We'll have to play a little bit with when to
3506 detect_input_pending. If it's done too often,
3507 scrolling large windows with repeated scroll-up
3508 commands will too quickly pause redisplay. */
3509 #if PERIODIC_PREEMPTION_CHECKING
3510 if (!force_p)
3512 EMACS_TIME tm = current_emacs_time ();
3513 if (EMACS_TIME_LT (preemption_next_check, tm))
3515 preemption_next_check = add_emacs_time (tm,
3516 preemption_period);
3517 if (detect_input_pending_ignore_squeezables ())
3518 break;
3521 #else
3522 if (!force_p && ++n_updated % preempt_count == 0)
3523 detect_input_pending_ignore_squeezables ();
3524 #endif
3525 changed_p |= update_window_line (w, vpos,
3526 &mouse_face_overwritten_p);
3528 /* Mark all rows below the last visible one in the current
3529 matrix as invalid. This is necessary because of
3530 variable line heights. Consider the case of three
3531 successive redisplays, where the first displays 5
3532 lines, the second 3 lines, and the third 5 lines again.
3533 If the second redisplay wouldn't mark rows in the
3534 current matrix invalid, the third redisplay might be
3535 tempted to optimize redisplay based on lines displayed
3536 in the first redisplay. */
3537 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3538 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3539 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3542 /* Was display preempted? */
3543 paused_p = row < end;
3545 set_cursor:
3547 /* Update the header line after scrolling because a new header
3548 line would otherwise overwrite lines at the top of the window
3549 that can be scrolled. */
3550 if (header_line_row && header_line_row->enabled_p)
3552 header_line_row->y = 0;
3553 update_window_line (w, 0, &mouse_face_overwritten_p);
3556 /* Fix the appearance of overlapping/overlapped rows. */
3557 if (!paused_p && !w->pseudo_window_p)
3559 #ifdef HAVE_WINDOW_SYSTEM
3560 if (changed_p && rif->fix_overlapping_area)
3562 redraw_overlapped_rows (w, yb);
3563 redraw_overlapping_rows (w, yb);
3565 #endif
3567 /* Make cursor visible at cursor position of W. */
3568 set_window_cursor_after_update (w);
3570 #if 0 /* Check that current matrix invariants are satisfied. This is
3571 for debugging only. See the comment of check_matrix_invariants. */
3572 IF_DEBUG (check_matrix_invariants (w));
3573 #endif
3576 #ifdef GLYPH_DEBUG
3577 /* Remember the redisplay method used to display the matrix. */
3578 strcpy (w->current_matrix->method, w->desired_matrix->method);
3579 #endif
3581 #ifdef HAVE_WINDOW_SYSTEM
3582 update_window_fringes (w, 0);
3583 #endif
3585 /* End the update of window W. Don't set the cursor if we
3586 paused updating the display because in this case,
3587 set_window_cursor_after_update hasn't been called, and
3588 output_cursor doesn't contain the cursor location. */
3589 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3591 else
3592 paused_p = 1;
3594 #ifdef GLYPH_DEBUG
3595 /* check_current_matrix_flags (w); */
3596 add_window_display_history (w, w->current_matrix->method, paused_p);
3597 #endif
3599 #ifdef HAVE_XWIDGETS
3600 xwidget_end_redisplay(w, w->current_matrix);
3601 #endif
3602 clear_glyph_matrix (desired_matrix);
3604 return paused_p;
3608 /* Update the display of area AREA in window W, row number VPOS.
3609 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3611 static void
3612 update_marginal_area (struct window *w, int area, int vpos)
3614 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3615 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3617 /* Let functions in xterm.c know what area subsequent X positions
3618 will be relative to. */
3619 updated_area = area;
3621 /* Set cursor to start of glyphs, write them, and clear to the end
3622 of the area. I don't think that something more sophisticated is
3623 necessary here, since marginal areas will not be the default. */
3624 rif->cursor_to (vpos, 0, desired_row->y, 0);
3625 if (desired_row->used[area])
3626 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3627 rif->clear_end_of_line (-1);
3631 /* Update the display of the text area of row VPOS in window W.
3632 Value is true if display has changed. */
3634 static bool
3635 update_text_area (struct window *w, int vpos)
3637 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3638 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3639 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3640 bool changed_p = 0;
3642 /* Let functions in xterm.c know what area subsequent X positions
3643 will be relative to. */
3644 updated_area = TEXT_AREA;
3646 /* If rows are at different X or Y, or rows have different height,
3647 or the current row is marked invalid, write the entire line. */
3648 if (!current_row->enabled_p
3649 || desired_row->y != current_row->y
3650 || desired_row->ascent != current_row->ascent
3651 || desired_row->phys_ascent != current_row->phys_ascent
3652 || desired_row->phys_height != current_row->phys_height
3653 || desired_row->visible_height != current_row->visible_height
3654 || current_row->overlapped_p
3655 /* This next line is necessary for correctly redrawing
3656 mouse-face areas after scrolling and other operations.
3657 However, it causes excessive flickering when mouse is moved
3658 across the mode line. Luckily, turning it off for the mode
3659 line doesn't seem to hurt anything. -- cyd.
3660 But it is still needed for the header line. -- kfs. */
3661 || (current_row->mouse_face_p
3662 && !(current_row->mode_line_p && vpos > 0))
3663 || current_row->x != desired_row->x)
3665 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
3667 if (desired_row->used[TEXT_AREA])
3668 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3669 desired_row->used[TEXT_AREA]);
3671 /* Clear to end of window. */
3672 rif->clear_end_of_line (-1);
3673 changed_p = 1;
3675 /* This erases the cursor. We do this here because
3676 notice_overwritten_cursor cannot easily check this, which
3677 might indicate that the whole functionality of
3678 notice_overwritten_cursor would better be implemented here.
3679 On the other hand, we need notice_overwritten_cursor as long
3680 as mouse highlighting is done asynchronously outside of
3681 redisplay. */
3682 if (vpos == w->phys_cursor.vpos)
3683 w->phys_cursor_on_p = 0;
3685 else
3687 int stop, i, x;
3688 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3689 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3690 bool overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3691 int desired_stop_pos = desired_row->used[TEXT_AREA];
3692 bool abort_skipping = 0;
3694 /* If the desired row extends its face to the text area end, and
3695 unless the current row also does so at the same position,
3696 make sure we write at least one glyph, so that the face
3697 extension actually takes place. */
3698 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3699 && (desired_stop_pos < current_row->used[TEXT_AREA]
3700 || (desired_stop_pos == current_row->used[TEXT_AREA]
3701 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3702 --desired_stop_pos;
3704 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3705 i = 0;
3706 x = desired_row->x;
3708 /* Loop over glyphs that current and desired row may have
3709 in common. */
3710 while (i < stop)
3712 bool can_skip_p = !abort_skipping;
3714 /* Skip over glyphs that both rows have in common. These
3715 don't have to be written. We can't skip if the last
3716 current glyph overlaps the glyph to its right. For
3717 example, consider a current row of `if ' with the `f' in
3718 Courier bold so that it overlaps the ` ' to its right.
3719 If the desired row is ` ', we would skip over the space
3720 after the `if' and there would remain a pixel from the
3721 `f' on the screen. */
3722 if (overlapping_glyphs_p && i > 0)
3724 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3725 int left, right;
3727 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3728 &left, &right);
3729 can_skip_p = (right == 0 && !abort_skipping);
3732 if (can_skip_p)
3734 int start_hpos = i;
3736 while (i < stop
3737 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3739 x += desired_glyph->pixel_width;
3740 ++desired_glyph, ++current_glyph, ++i;
3743 /* Consider the case that the current row contains "xxx
3744 ppp ggg" in italic Courier font, and the desired row
3745 is "xxx ggg". The character `p' has lbearing, `g'
3746 has not. The loop above will stop in front of the
3747 first `p' in the current row. If we would start
3748 writing glyphs there, we wouldn't erase the lbearing
3749 of the `p'. The rest of the lbearing problem is then
3750 taken care of by draw_glyphs. */
3751 if (overlapping_glyphs_p
3752 && i > 0
3753 && i < current_row->used[TEXT_AREA]
3754 && (current_row->used[TEXT_AREA]
3755 != desired_row->used[TEXT_AREA]))
3757 int left, right;
3759 rif->get_glyph_overhangs (current_glyph,
3760 XFRAME (w->frame),
3761 &left, &right);
3762 while (left > 0 && i > 0)
3764 --i, --desired_glyph, --current_glyph;
3765 x -= desired_glyph->pixel_width;
3766 left -= desired_glyph->pixel_width;
3769 /* Abort the skipping algorithm if we end up before
3770 our starting point, to avoid looping (bug#1070).
3771 This can happen when the lbearing is larger than
3772 the pixel width. */
3773 abort_skipping = (i < start_hpos);
3777 /* Try to avoid writing the entire rest of the desired row
3778 by looking for a resync point. This mainly prevents
3779 mode line flickering in the case the mode line is in
3780 fixed-pitch font, which it usually will be. */
3781 if (i < desired_row->used[TEXT_AREA])
3783 int start_x = x, start_hpos = i;
3784 struct glyph *start = desired_glyph;
3785 int current_x = x;
3786 bool skip_first_p = !can_skip_p;
3788 /* Find the next glyph that's equal again. */
3789 while (i < stop
3790 && (skip_first_p
3791 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3792 && x == current_x)
3794 x += desired_glyph->pixel_width;
3795 current_x += current_glyph->pixel_width;
3796 ++desired_glyph, ++current_glyph, ++i;
3797 skip_first_p = 0;
3800 if (i == start_hpos || x != current_x)
3802 i = start_hpos;
3803 x = start_x;
3804 desired_glyph = start;
3805 break;
3808 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
3809 rif->write_glyphs (start, i - start_hpos);
3810 changed_p = 1;
3814 /* Write the rest. */
3815 if (i < desired_row->used[TEXT_AREA])
3817 rif->cursor_to (vpos, i, desired_row->y, x);
3818 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
3819 changed_p = 1;
3822 /* Maybe clear to end of line. */
3823 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3825 /* If new row extends to the end of the text area, nothing
3826 has to be cleared, if and only if we did a write_glyphs
3827 above. This is made sure by setting desired_stop_pos
3828 appropriately above. */
3829 eassert (i < desired_row->used[TEXT_AREA]
3830 || ((desired_row->used[TEXT_AREA]
3831 == current_row->used[TEXT_AREA])
3832 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3834 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3836 /* If old row extends to the end of the text area, clear. */
3837 if (i >= desired_row->used[TEXT_AREA])
3838 rif->cursor_to (vpos, i, desired_row->y,
3839 desired_row->pixel_width);
3840 rif->clear_end_of_line (-1);
3841 changed_p = 1;
3843 else if (desired_row->pixel_width < current_row->pixel_width)
3845 /* Otherwise clear to the end of the old row. Everything
3846 after that position should be clear already. */
3847 int xlim;
3849 if (i >= desired_row->used[TEXT_AREA])
3850 rif->cursor_to (vpos, i, desired_row->y,
3851 desired_row->pixel_width);
3853 /* If cursor is displayed at the end of the line, make sure
3854 it's cleared. Nowadays we don't have a phys_cursor_glyph
3855 with which to erase the cursor (because this method
3856 doesn't work with lbearing/rbearing), so we must do it
3857 this way. */
3858 if (vpos == w->phys_cursor.vpos
3859 && (desired_row->reversed_p
3860 ? (w->phys_cursor.hpos < 0)
3861 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3863 w->phys_cursor_on_p = 0;
3864 xlim = -1;
3866 else
3867 xlim = current_row->pixel_width;
3868 rif->clear_end_of_line (xlim);
3869 changed_p = 1;
3873 return changed_p;
3877 /* Update row VPOS in window W. Value is true if display has been changed. */
3879 static bool
3880 update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
3882 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3883 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3884 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3885 bool changed_p = 0;
3887 /* Set the row being updated. This is important to let xterm.c
3888 know what line height values are in effect. */
3889 updated_row = desired_row;
3891 /* A row can be completely invisible in case a desired matrix was
3892 built with a vscroll and then make_cursor_line_fully_visible shifts
3893 the matrix. Make sure to make such rows current anyway, since
3894 we need the correct y-position, for example, in the current matrix. */
3895 if (desired_row->mode_line_p
3896 || desired_row->visible_height > 0)
3898 eassert (desired_row->enabled_p);
3900 /* Update display of the left margin area, if there is one. */
3901 if (!desired_row->full_width_p
3902 && !NILP (w->left_margin_cols))
3904 changed_p = 1;
3905 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
3906 /* Setting this flag will ensure the vertical border, if
3907 any, between this window and the one on its left will be
3908 redrawn. This is necessary because updating the left
3909 margin area can potentially draw over the border. */
3910 current_row->redraw_fringe_bitmaps_p = 1;
3913 /* Update the display of the text area. */
3914 if (update_text_area (w, vpos))
3916 changed_p = 1;
3917 if (current_row->mouse_face_p)
3918 *mouse_face_overwritten_p = 1;
3921 /* Update display of the right margin area, if there is one. */
3922 if (!desired_row->full_width_p
3923 && !NILP (w->right_margin_cols))
3925 changed_p = 1;
3926 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
3929 /* Draw truncation marks etc. */
3930 if (!current_row->enabled_p
3931 || desired_row->y != current_row->y
3932 || desired_row->visible_height != current_row->visible_height
3933 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
3934 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
3935 || current_row->redraw_fringe_bitmaps_p
3936 || desired_row->mode_line_p != current_row->mode_line_p
3937 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
3938 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
3939 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
3940 rif->after_update_window_line_hook (desired_row);
3943 /* Update current_row from desired_row. */
3944 make_current (w->desired_matrix, w->current_matrix, vpos);
3945 updated_row = NULL;
3946 return changed_p;
3950 /* Set the cursor after an update of window W. This function may only
3951 be called from update_window. */
3953 static void
3954 set_window_cursor_after_update (struct window *w)
3956 struct frame *f = XFRAME (w->frame);
3957 struct redisplay_interface *rif = FRAME_RIF (f);
3958 int cx, cy, vpos, hpos;
3960 /* Not intended for frame matrix updates. */
3961 eassert (FRAME_WINDOW_P (f));
3963 if (cursor_in_echo_area
3964 && !NILP (echo_area_buffer[0])
3965 /* If we are showing a message instead of the mini-buffer,
3966 show the cursor for the message instead. */
3967 && XWINDOW (minibuf_window) == w
3968 && EQ (minibuf_window, echo_area_window)
3969 /* These cases apply only to the frame that contains
3970 the active mini-buffer window. */
3971 && FRAME_HAS_MINIBUF_P (f)
3972 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
3974 cx = cy = vpos = hpos = 0;
3976 if (cursor_in_echo_area >= 0)
3978 /* If the mini-buffer is several lines high, find the last
3979 line that has any text on it. Note: either all lines
3980 are enabled or none. Otherwise we wouldn't be able to
3981 determine Y. */
3982 struct glyph_row *row, *last_row;
3983 struct glyph *glyph;
3984 int yb = window_text_bottom_y (w);
3986 last_row = NULL;
3987 row = w->current_matrix->rows;
3988 while (row->enabled_p
3989 && (last_row == NULL
3990 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
3992 if (row->used[TEXT_AREA]
3993 && row->glyphs[TEXT_AREA][0].charpos >= 0)
3994 last_row = row;
3995 ++row;
3998 if (last_row)
4000 struct glyph *start = last_row->glyphs[TEXT_AREA];
4001 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
4003 while (last > start && last->charpos < 0)
4004 --last;
4006 for (glyph = start; glyph < last; ++glyph)
4008 cx += glyph->pixel_width;
4009 ++hpos;
4012 cy = last_row->y;
4013 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
4017 else
4019 cx = w->cursor.x;
4020 cy = w->cursor.y;
4021 hpos = w->cursor.hpos;
4022 vpos = w->cursor.vpos;
4025 /* Window cursor can be out of sync for horizontally split windows.
4026 Horizontal position is -1 when cursor is on the left fringe. */
4027 hpos = clip_to_bounds (-1, hpos, w->current_matrix->matrix_w - 1);
4028 vpos = clip_to_bounds (0, vpos, w->current_matrix->nrows - 1);
4029 rif->cursor_to (vpos, hpos, cy, cx);
4033 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4034 tree rooted at W. */
4036 void
4037 set_window_update_flags (struct window *w, bool on_p)
4039 while (w)
4041 if (!NILP (w->hchild))
4042 set_window_update_flags (XWINDOW (w->hchild), on_p);
4043 else if (!NILP (w->vchild))
4044 set_window_update_flags (XWINDOW (w->vchild), on_p);
4045 else
4046 w->must_be_updated_p = on_p;
4048 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4054 /***********************************************************************
4055 Window-Based Scrolling
4056 ***********************************************************************/
4058 /* Structure describing rows in scrolling_window. */
4060 struct row_entry
4062 /* Number of occurrences of this row in desired and current matrix. */
4063 int old_uses, new_uses;
4065 /* Vpos of row in new matrix. */
4066 int new_line_number;
4068 /* Bucket index of this row_entry in the hash table row_table. */
4069 ptrdiff_t bucket;
4071 /* The row described by this entry. */
4072 struct glyph_row *row;
4074 /* Hash collision chain. */
4075 struct row_entry *next;
4078 /* A pool to allocate row_entry structures from, and the size of the
4079 pool. The pool is reallocated in scrolling_window when we find
4080 that we need a larger one. */
4082 static struct row_entry *row_entry_pool;
4083 static ptrdiff_t row_entry_pool_size;
4085 /* Index of next free entry in row_entry_pool. */
4087 static ptrdiff_t row_entry_idx;
4089 /* The hash table used during scrolling, and the table's size. This
4090 table is used to quickly identify equal rows in the desired and
4091 current matrix. */
4093 static struct row_entry **row_table;
4094 static ptrdiff_t row_table_size;
4096 /* Vectors of pointers to row_entry structures belonging to the
4097 current and desired matrix, and the size of the vectors. */
4099 static struct row_entry **old_lines, **new_lines;
4100 static ptrdiff_t old_lines_size, new_lines_size;
4102 /* A pool to allocate run structures from, and its size. */
4104 static struct run *run_pool;
4105 static ptrdiff_t runs_size;
4107 /* A vector of runs of lines found during scrolling. */
4109 static struct run **runs;
4111 /* Add glyph row ROW to the scrolling hash table. */
4113 static struct row_entry *
4114 add_row_entry (struct glyph_row *row)
4116 struct row_entry *entry;
4117 ptrdiff_t i = row->hash % row_table_size;
4119 entry = row_table[i];
4120 eassert (entry || verify_row_hash (row));
4121 while (entry && !row_equal_p (entry->row, row, 1))
4122 entry = entry->next;
4124 if (entry == NULL)
4126 entry = row_entry_pool + row_entry_idx++;
4127 entry->row = row;
4128 entry->old_uses = entry->new_uses = 0;
4129 entry->new_line_number = 0;
4130 entry->bucket = i;
4131 entry->next = row_table[i];
4132 row_table[i] = entry;
4135 return entry;
4139 /* Try to reuse part of the current display of W by scrolling lines.
4140 HEADER_LINE_P means W has a header line.
4142 The algorithm is taken from Communications of the ACM, Apr78 "A
4143 Technique for Isolating Differences Between Files." It should take
4144 O(N) time.
4146 A short outline of the steps of the algorithm
4148 1. Skip lines equal at the start and end of both matrices.
4150 2. Enter rows in the current and desired matrix into a symbol
4151 table, counting how often they appear in both matrices.
4153 3. Rows that appear exactly once in both matrices serve as anchors,
4154 i.e. we assume that such lines are likely to have been moved.
4156 4. Starting from anchor lines, extend regions to be scrolled both
4157 forward and backward.
4159 Value is
4161 -1 if all rows were found to be equal.
4162 0 to indicate that we did not scroll the display, or
4163 1 if we did scroll. */
4165 static int
4166 scrolling_window (struct window *w, bool header_line_p)
4168 struct glyph_matrix *desired_matrix = w->desired_matrix;
4169 struct glyph_matrix *current_matrix = w->current_matrix;
4170 int yb = window_text_bottom_y (w);
4171 ptrdiff_t i;
4172 int j, first_old, first_new, last_old, last_new;
4173 int nruns, run_idx;
4174 ptrdiff_t n;
4175 struct row_entry *entry;
4176 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4178 /* Skip over rows equal at the start. */
4179 for (i = header_line_p; i < current_matrix->nrows - 1; ++i)
4181 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4182 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4184 if (c->enabled_p
4185 && d->enabled_p
4186 && !d->redraw_fringe_bitmaps_p
4187 && c->y == d->y
4188 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4189 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4190 && row_equal_p (c, d, 1))
4192 assign_row (c, d);
4193 d->enabled_p = 0;
4195 else
4196 break;
4199 #ifdef HAVE_XWIDGETS
4200 //currently this is needed to detect xwidget movement reliably. or probably not.
4201 //printf("scrolling_window\n");
4202 return 0;
4203 #endif
4205 /* Give up if some rows in the desired matrix are not enabled. */
4206 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4207 return -1;
4209 first_old = first_new = i;
4211 /* Set last_new to the index + 1 of the row that reaches the
4212 bottom boundary in the desired matrix. Give up if we find a
4213 disabled row before we reach the bottom boundary. */
4214 i = first_new + 1;
4215 while (i < desired_matrix->nrows - 1)
4217 int bottom;
4219 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4220 return 0;
4221 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4222 if (bottom <= yb)
4223 ++i;
4224 if (bottom >= yb)
4225 break;
4228 last_new = i;
4230 /* Set last_old to the index + 1 of the row that reaches the bottom
4231 boundary in the current matrix. We don't look at the enabled
4232 flag here because we plan to reuse part of the display even if
4233 other parts are disabled. */
4234 i = first_old + 1;
4235 while (i < current_matrix->nrows - 1)
4237 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4238 if (bottom <= yb)
4239 ++i;
4240 if (bottom >= yb)
4241 break;
4244 last_old = i;
4246 /* Skip over rows equal at the bottom. */
4247 i = last_new;
4248 j = last_old;
4249 while (i - 1 > first_new
4250 && j - 1 > first_old
4251 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4252 && (MATRIX_ROW (current_matrix, j - 1)->y
4253 == MATRIX_ROW (desired_matrix, i - 1)->y)
4254 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4255 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4256 MATRIX_ROW (current_matrix, j - 1), 1))
4257 --i, --j;
4258 last_new = i;
4259 last_old = j;
4261 /* Nothing to do if all rows are equal. */
4262 if (last_new == first_new)
4263 return 0;
4265 /* Check for integer overflow in size calculation.
4267 If next_almost_prime checks (N) for divisibility by 2..10, then
4268 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4269 So, set next_almost_prime_increment_max to 10.
4271 It's just a coincidence that next_almost_prime_increment_max ==
4272 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4273 13, then next_almost_prime_increment_max would be 14, e.g.,
4274 because next_almost_prime (113) would be 127. */
4276 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4277 enum { next_almost_prime_increment_max = 10 };
4278 ptrdiff_t row_table_max =
4279 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4280 - next_almost_prime_increment_max);
4281 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4282 if (current_nrows_max < current_matrix->nrows)
4283 memory_full (SIZE_MAX);
4286 /* Reallocate vectors, tables etc. if necessary. */
4288 if (current_matrix->nrows > old_lines_size)
4289 old_lines = xpalloc (old_lines, &old_lines_size,
4290 current_matrix->nrows - old_lines_size,
4291 INT_MAX, sizeof *old_lines);
4293 if (desired_matrix->nrows > new_lines_size)
4294 new_lines = xpalloc (new_lines, &new_lines_size,
4295 desired_matrix->nrows - new_lines_size,
4296 INT_MAX, sizeof *new_lines);
4298 n = desired_matrix->nrows;
4299 n += current_matrix->nrows;
4300 if (row_table_size < 3 * n)
4302 ptrdiff_t size = next_almost_prime (3 * n);
4303 row_table = xnrealloc (row_table, size, sizeof *row_table);
4304 row_table_size = size;
4305 memset (row_table, 0, size * sizeof *row_table);
4308 if (n > row_entry_pool_size)
4309 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4310 n - row_entry_pool_size,
4311 -1, sizeof *row_entry_pool);
4313 if (desired_matrix->nrows > runs_size)
4315 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4316 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4317 runs_size = desired_matrix->nrows;
4320 nruns = run_idx = 0;
4321 row_entry_idx = 0;
4323 /* Add rows from the current and desired matrix to the hash table
4324 row_hash_table to be able to find equal ones quickly. */
4326 for (i = first_old; i < last_old; ++i)
4328 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4330 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4331 old_lines[i] = entry;
4332 ++entry->old_uses;
4334 else
4335 old_lines[i] = NULL;
4338 for (i = first_new; i < last_new; ++i)
4340 eassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4341 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4342 ++entry->new_uses;
4343 entry->new_line_number = i;
4344 new_lines[i] = entry;
4347 /* Identify moves based on lines that are unique and equal
4348 in both matrices. */
4349 for (i = first_old; i < last_old;)
4350 if (old_lines[i]
4351 && old_lines[i]->old_uses == 1
4352 && old_lines[i]->new_uses == 1)
4354 int p, q;
4355 int new_line = old_lines[i]->new_line_number;
4356 struct run *run = run_pool + run_idx++;
4358 /* Record move. */
4359 run->current_vpos = i;
4360 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4361 run->desired_vpos = new_line;
4362 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4363 run->nrows = 1;
4364 run->height = MATRIX_ROW (current_matrix, i)->height;
4366 /* Extend backward. */
4367 p = i - 1;
4368 q = new_line - 1;
4369 while (p > first_old
4370 && q > first_new
4371 && old_lines[p] == new_lines[q])
4373 int h = MATRIX_ROW (current_matrix, p)->height;
4374 --run->current_vpos;
4375 --run->desired_vpos;
4376 ++run->nrows;
4377 run->height += h;
4378 run->desired_y -= h;
4379 run->current_y -= h;
4380 --p, --q;
4383 /* Extend forward. */
4384 p = i + 1;
4385 q = new_line + 1;
4386 while (p < last_old
4387 && q < last_new
4388 && old_lines[p] == new_lines[q])
4390 int h = MATRIX_ROW (current_matrix, p)->height;
4391 ++run->nrows;
4392 run->height += h;
4393 ++p, ++q;
4396 /* Insert run into list of all runs. Order runs by copied
4397 pixel lines. Note that we record runs that don't have to
4398 be copied because they are already in place. This is done
4399 because we can avoid calling update_window_line in this
4400 case. */
4401 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4403 for (q = nruns; q > p; --q)
4404 runs[q] = runs[q - 1];
4405 runs[p] = run;
4406 ++nruns;
4408 i += run->nrows;
4410 else
4411 ++i;
4413 /* Do the moves. Do it in a way that we don't overwrite something
4414 we want to copy later on. This is not solvable in general
4415 because there is only one display and we don't have a way to
4416 exchange areas on this display. Example:
4418 +-----------+ +-----------+
4419 | A | | B |
4420 +-----------+ --> +-----------+
4421 | B | | A |
4422 +-----------+ +-----------+
4424 Instead, prefer bigger moves, and invalidate moves that would
4425 copy from where we copied to. */
4427 for (i = 0; i < nruns; ++i)
4428 if (runs[i]->nrows > 0)
4430 struct run *r = runs[i];
4432 /* Copy on the display. */
4433 if (r->current_y != r->desired_y)
4435 rif->clear_window_mouse_face (w);
4436 rif->scroll_run_hook (w, r);
4439 /* Truncate runs that copy to where we copied to, and
4440 invalidate runs that copy from where we copied to. */
4441 for (j = nruns - 1; j > i; --j)
4443 struct run *p = runs[j];
4444 bool truncated_p = 0;
4446 if (p->nrows > 0
4447 && p->desired_y < r->desired_y + r->height
4448 && p->desired_y + p->height > r->desired_y)
4450 if (p->desired_y < r->desired_y)
4452 p->nrows = r->desired_vpos - p->desired_vpos;
4453 p->height = r->desired_y - p->desired_y;
4454 truncated_p = 1;
4456 else
4458 int nrows_copied = (r->desired_vpos + r->nrows
4459 - p->desired_vpos);
4461 if (p->nrows <= nrows_copied)
4462 p->nrows = 0;
4463 else
4465 int height_copied = (r->desired_y + r->height
4466 - p->desired_y);
4468 p->current_vpos += nrows_copied;
4469 p->desired_vpos += nrows_copied;
4470 p->nrows -= nrows_copied;
4471 p->current_y += height_copied;
4472 p->desired_y += height_copied;
4473 p->height -= height_copied;
4474 truncated_p = 1;
4479 if (r->current_y != r->desired_y
4480 /* The condition below is equivalent to
4481 ((p->current_y >= r->desired_y
4482 && p->current_y < r->desired_y + r->height)
4483 || (p->current_y + p->height > r->desired_y
4484 && (p->current_y + p->height
4485 <= r->desired_y + r->height)))
4486 because we have 0 < p->height <= r->height. */
4487 && p->current_y < r->desired_y + r->height
4488 && p->current_y + p->height > r->desired_y)
4489 p->nrows = 0;
4491 /* Reorder runs by copied pixel lines if truncated. */
4492 if (truncated_p && p->nrows > 0)
4494 int k = nruns - 1;
4496 while (runs[k]->nrows == 0 || runs[k]->height < p->height)
4497 k--;
4498 memmove (runs + j, runs + j + 1, (k - j) * sizeof (*runs));
4499 runs[k] = p;
4503 /* Assign matrix rows. */
4504 for (j = 0; j < r->nrows; ++j)
4506 struct glyph_row *from, *to;
4507 bool to_overlapped_p;
4509 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4510 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4511 to_overlapped_p = to->overlapped_p;
4512 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4513 assign_row (to, from);
4514 /* The above `assign_row' actually does swap, so if we had
4515 an overlap in the copy destination of two runs, then
4516 the second run would assign a previously disabled bogus
4517 row. But thanks to the truncation code in the
4518 preceding for-loop, we no longer have such an overlap,
4519 and thus the assigned row should always be enabled. */
4520 eassert (to->enabled_p);
4521 from->enabled_p = 0;
4522 to->overlapped_p = to_overlapped_p;
4526 /* Clear the hash table, for the next time. */
4527 for (i = 0; i < row_entry_idx; ++i)
4528 row_table[row_entry_pool[i].bucket] = NULL;
4530 /* Value is 1 to indicate that we scrolled the display. */
4531 return 0 < nruns;
4536 /************************************************************************
4537 Frame-Based Updates
4538 ************************************************************************/
4540 /* Update the desired frame matrix of frame F.
4542 FORCE_P means that the update should not be stopped by pending input.
4543 INHIBIT_HAIRY_ID_P means that scrolling should not be tried.
4545 Value is true if update was stopped due to pending input. */
4547 static bool
4548 update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p)
4550 /* Frame matrices to work on. */
4551 struct glyph_matrix *current_matrix = f->current_matrix;
4552 struct glyph_matrix *desired_matrix = f->desired_matrix;
4553 int i;
4554 bool pause_p;
4555 int preempt_count = baud_rate / 2400 + 1;
4557 eassert (current_matrix && desired_matrix);
4559 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4560 calculate_costs (f);
4562 if (preempt_count <= 0)
4563 preempt_count = 1;
4565 #if !PERIODIC_PREEMPTION_CHECKING
4566 if (!force_p && detect_input_pending_ignore_squeezables ())
4568 pause_p = 1;
4569 goto do_pause;
4571 #endif
4573 /* If we cannot insert/delete lines, it's no use trying it. */
4574 if (!FRAME_LINE_INS_DEL_OK (f))
4575 inhibit_id_p = 1;
4577 /* See if any of the desired lines are enabled; don't compute for
4578 i/d line if just want cursor motion. */
4579 for (i = 0; i < desired_matrix->nrows; i++)
4580 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4581 break;
4583 /* Try doing i/d line, if not yet inhibited. */
4584 if (!inhibit_id_p && i < desired_matrix->nrows)
4585 force_p |= scrolling (f);
4587 /* Update the individual lines as needed. Do bottom line first. */
4588 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4589 update_frame_line (f, desired_matrix->nrows - 1);
4591 /* Now update the rest of the lines. */
4592 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4594 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4596 if (FRAME_TERMCAP_P (f))
4598 /* Flush out every so many lines.
4599 Also flush out if likely to have more than 1k buffered
4600 otherwise. I'm told that some telnet connections get
4601 really screwed by more than 1k output at once. */
4602 FILE *display_output = FRAME_TTY (f)->output;
4603 if (display_output)
4605 ptrdiff_t outq = __fpending (display_output);
4606 if (outq > 900
4607 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4608 fflush (display_output);
4612 #if PERIODIC_PREEMPTION_CHECKING
4613 if (!force_p)
4615 EMACS_TIME tm = current_emacs_time ();
4616 if (EMACS_TIME_LT (preemption_next_check, tm))
4618 preemption_next_check = add_emacs_time (tm, preemption_period);
4619 if (detect_input_pending_ignore_squeezables ())
4620 break;
4623 #else
4624 if (!force_p && (i - 1) % preempt_count == 0)
4625 detect_input_pending_ignore_squeezables ();
4626 #endif
4628 update_frame_line (f, i);
4632 lint_assume (0 <= FRAME_LINES (f));
4633 pause_p = 0 < i && i < FRAME_LINES (f) - 1;
4635 /* Now just clean up termcap drivers and set cursor, etc. */
4636 if (!pause_p)
4638 if ((cursor_in_echo_area
4639 /* If we are showing a message instead of the mini-buffer,
4640 show the cursor for the message instead of for the
4641 (now hidden) mini-buffer contents. */
4642 || (EQ (minibuf_window, selected_window)
4643 && EQ (minibuf_window, echo_area_window)
4644 && !NILP (echo_area_buffer[0])))
4645 /* These cases apply only to the frame that contains
4646 the active mini-buffer window. */
4647 && FRAME_HAS_MINIBUF_P (f)
4648 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4650 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4651 int row, col;
4653 if (cursor_in_echo_area < 0)
4655 /* Negative value of cursor_in_echo_area means put
4656 cursor at beginning of line. */
4657 row = top;
4658 col = 0;
4660 else
4662 /* Positive value of cursor_in_echo_area means put
4663 cursor at the end of the prompt. If the mini-buffer
4664 is several lines high, find the last line that has
4665 any text on it. */
4666 row = FRAME_LINES (f);
4669 --row;
4670 col = 0;
4672 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4674 /* Frame rows are filled up with spaces that
4675 must be ignored here. */
4676 struct glyph_row *r = MATRIX_ROW (current_matrix,
4677 row);
4678 struct glyph *start = r->glyphs[TEXT_AREA];
4679 struct glyph *last = start + r->used[TEXT_AREA];
4681 while (last > start
4682 && (last - 1)->charpos < 0)
4683 --last;
4685 col = last - start;
4688 while (row > top && col == 0);
4690 /* Make sure COL is not out of range. */
4691 if (col >= FRAME_CURSOR_X_LIMIT (f))
4693 /* If we have another row, advance cursor into it. */
4694 if (row < FRAME_LINES (f) - 1)
4696 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4697 row++;
4699 /* Otherwise move it back in range. */
4700 else
4701 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4705 cursor_to (f, row, col);
4707 else
4709 /* We have only one cursor on terminal frames. Use it to
4710 display the cursor of the selected window. */
4711 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4712 if (w->cursor.vpos >= 0
4713 /* The cursor vpos may be temporarily out of bounds
4714 in the following situation: There is one window,
4715 with the cursor in the lower half of it. The window
4716 is split, and a message causes a redisplay before
4717 a new cursor position has been computed. */
4718 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4720 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4721 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4723 if (INTEGERP (w->left_margin_cols))
4724 x += XFASTINT (w->left_margin_cols);
4726 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4727 cursor_to (f, y, x);
4732 #if !PERIODIC_PREEMPTION_CHECKING
4733 do_pause:
4734 #endif
4736 clear_desired_matrices (f);
4737 return pause_p;
4741 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4743 static bool
4744 scrolling (struct frame *frame)
4746 int unchanged_at_top, unchanged_at_bottom;
4747 int window_size;
4748 int changed_lines;
4749 int *old_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4750 int *new_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4751 int *draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4752 int *old_draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4753 register int i;
4754 int free_at_end_vpos = FRAME_LINES (frame);
4755 struct glyph_matrix *current_matrix = frame->current_matrix;
4756 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4758 if (!current_matrix)
4759 emacs_abort ();
4761 /* Compute hash codes of all the lines. Also calculate number of
4762 changed lines, number of unchanged lines at the beginning, and
4763 number of unchanged lines at the end. */
4764 changed_lines = 0;
4765 unchanged_at_top = 0;
4766 unchanged_at_bottom = FRAME_LINES (frame);
4767 for (i = 0; i < FRAME_LINES (frame); i++)
4769 /* Give up on this scrolling if some old lines are not enabled. */
4770 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4771 return 0;
4772 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4773 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4775 /* This line cannot be redrawn, so don't let scrolling mess it. */
4776 new_hash[i] = old_hash[i];
4777 #define INFINITY 1000000 /* Taken from scroll.c */
4778 draw_cost[i] = INFINITY;
4780 else
4782 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4783 draw_cost[i] = line_draw_cost (desired_matrix, i);
4786 if (old_hash[i] != new_hash[i])
4788 changed_lines++;
4789 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4791 else if (i == unchanged_at_top)
4792 unchanged_at_top++;
4793 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4796 /* If changed lines are few, don't allow preemption, don't scroll. */
4797 if ((!FRAME_SCROLL_REGION_OK (frame)
4798 && changed_lines < baud_rate / 2400)
4799 || unchanged_at_bottom == FRAME_LINES (frame))
4800 return 1;
4802 window_size = (FRAME_LINES (frame) - unchanged_at_top
4803 - unchanged_at_bottom);
4805 if (FRAME_SCROLL_REGION_OK (frame))
4806 free_at_end_vpos -= unchanged_at_bottom;
4807 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4808 free_at_end_vpos = -1;
4810 /* If large window, fast terminal and few lines in common between
4811 current frame and desired frame, don't bother with i/d calc. */
4812 if (!FRAME_SCROLL_REGION_OK (frame)
4813 && window_size >= 18 && baud_rate > 2400
4814 && (window_size >=
4815 10 * scrolling_max_lines_saved (unchanged_at_top,
4816 FRAME_LINES (frame) - unchanged_at_bottom,
4817 old_hash, new_hash, draw_cost)))
4818 return 0;
4820 if (window_size < 2)
4821 return 0;
4823 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4824 draw_cost + unchanged_at_top - 1,
4825 old_draw_cost + unchanged_at_top - 1,
4826 old_hash + unchanged_at_top - 1,
4827 new_hash + unchanged_at_top - 1,
4828 free_at_end_vpos - unchanged_at_top);
4830 return 0;
4834 /* Count the number of blanks at the start of the vector of glyphs R
4835 which is LEN glyphs long. */
4837 static int
4838 count_blanks (struct glyph *r, int len)
4840 int i;
4842 for (i = 0; i < len; ++i)
4843 if (!CHAR_GLYPH_SPACE_P (r[i]))
4844 break;
4846 return i;
4850 /* Count the number of glyphs in common at the start of the glyph
4851 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4852 of STR2. Value is the number of equal glyphs equal at the start. */
4854 static int
4855 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4857 struct glyph *p1 = str1;
4858 struct glyph *p2 = str2;
4860 while (p1 < end1
4861 && p2 < end2
4862 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4863 ++p1, ++p2;
4865 return p1 - str1;
4869 /* Char insertion/deletion cost vector, from term.c */
4871 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4874 /* Perform a frame-based update on line VPOS in frame FRAME. */
4876 static void
4877 update_frame_line (struct frame *f, int vpos)
4879 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4880 int tem;
4881 int osp, nsp, begmatch, endmatch, olen, nlen;
4882 struct glyph_matrix *current_matrix = f->current_matrix;
4883 struct glyph_matrix *desired_matrix = f->desired_matrix;
4884 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4885 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4886 bool must_write_whole_line_p;
4887 bool write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4888 bool colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4889 != FACE_TTY_DEFAULT_BG_COLOR);
4891 if (colored_spaces_p)
4892 write_spaces_p = 1;
4894 /* Current row not enabled means it has unknown contents. We must
4895 write the whole desired line in that case. */
4896 must_write_whole_line_p = !current_row->enabled_p;
4897 if (must_write_whole_line_p)
4899 obody = 0;
4900 olen = 0;
4902 else
4904 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4905 olen = current_row->used[TEXT_AREA];
4907 /* Ignore trailing spaces, if we can. */
4908 if (!write_spaces_p)
4909 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4910 olen--;
4913 current_row->enabled_p = 1;
4914 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4916 /* If desired line is empty, just clear the line. */
4917 if (!desired_row->enabled_p)
4919 nlen = 0;
4920 goto just_erase;
4923 nbody = desired_row->glyphs[TEXT_AREA];
4924 nlen = desired_row->used[TEXT_AREA];
4925 nend = nbody + nlen;
4927 /* If display line has unknown contents, write the whole line. */
4928 if (must_write_whole_line_p)
4930 /* Ignore spaces at the end, if we can. */
4931 if (!write_spaces_p)
4932 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4933 --nlen;
4935 /* Write the contents of the desired line. */
4936 if (nlen)
4938 cursor_to (f, vpos, 0);
4939 write_glyphs (f, nbody, nlen);
4942 /* Don't call clear_end_of_line if we already wrote the whole
4943 line. The cursor will not be at the right margin in that
4944 case but in the line below. */
4945 if (nlen < FRAME_TOTAL_COLS (f))
4947 cursor_to (f, vpos, nlen);
4948 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
4950 else
4951 /* Make sure we are in the right row, otherwise cursor movement
4952 with cmgoto might use `ch' in the wrong row. */
4953 cursor_to (f, vpos, 0);
4955 make_current (desired_matrix, current_matrix, vpos);
4956 return;
4959 /* Pretend trailing spaces are not there at all,
4960 unless for one reason or another we must write all spaces. */
4961 if (!write_spaces_p)
4962 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4963 nlen--;
4965 /* If there's no i/d char, quickly do the best we can without it. */
4966 if (!FRAME_CHAR_INS_DEL_OK (f))
4968 int i, j;
4970 /* Find the first glyph in desired row that doesn't agree with
4971 a glyph in the current row, and write the rest from there on. */
4972 for (i = 0; i < nlen; i++)
4974 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
4976 /* Find the end of the run of different glyphs. */
4977 j = i + 1;
4978 while (j < nlen
4979 && (j >= olen
4980 || !GLYPH_EQUAL_P (nbody + j, obody + j)
4981 || CHAR_GLYPH_PADDING_P (nbody[j])))
4982 ++j;
4984 /* Output this run of non-matching chars. */
4985 cursor_to (f, vpos, i);
4986 write_glyphs (f, nbody + i, j - i);
4987 i = j - 1;
4989 /* Now find the next non-match. */
4993 /* Clear the rest of the line, or the non-clear part of it. */
4994 if (olen > nlen)
4996 cursor_to (f, vpos, nlen);
4997 clear_end_of_line (f, olen);
5000 /* Make current row = desired row. */
5001 make_current (desired_matrix, current_matrix, vpos);
5002 return;
5005 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5006 characters in a row. */
5008 if (!olen)
5010 /* If current line is blank, skip over initial spaces, if
5011 possible, and write the rest. */
5012 if (write_spaces_p)
5013 nsp = 0;
5014 else
5015 nsp = count_blanks (nbody, nlen);
5017 if (nlen > nsp)
5019 cursor_to (f, vpos, nsp);
5020 write_glyphs (f, nbody + nsp, nlen - nsp);
5023 /* Exchange contents between current_frame and new_frame. */
5024 make_current (desired_matrix, current_matrix, vpos);
5025 return;
5028 /* Compute number of leading blanks in old and new contents. */
5029 osp = count_blanks (obody, olen);
5030 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
5032 /* Compute number of matching chars starting with first non-blank. */
5033 begmatch = count_match (obody + osp, obody + olen,
5034 nbody + nsp, nbody + nlen);
5036 /* Spaces in new match implicit space past the end of old. */
5037 /* A bug causing this to be a no-op was fixed in 18.29. */
5038 if (!write_spaces_p && osp + begmatch == olen)
5040 np1 = nbody + nsp;
5041 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
5042 ++begmatch;
5045 /* Avoid doing insert/delete char
5046 just cause number of leading spaces differs
5047 when the following text does not match. */
5048 if (begmatch == 0 && osp != nsp)
5049 osp = nsp = min (osp, nsp);
5051 /* Find matching characters at end of line */
5052 op1 = obody + olen;
5053 np1 = nbody + nlen;
5054 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5055 while (op1 > op2
5056 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
5058 op1--;
5059 np1--;
5061 endmatch = obody + olen - op1;
5063 /* tem gets the distance to insert or delete.
5064 endmatch is how many characters we save by doing so.
5065 Is it worth it? */
5067 tem = (nlen - nsp) - (olen - osp);
5068 if (endmatch && tem
5069 && (!FRAME_CHAR_INS_DEL_OK (f)
5070 || endmatch <= char_ins_del_cost (f)[tem]))
5071 endmatch = 0;
5073 /* nsp - osp is the distance to insert or delete.
5074 If that is nonzero, begmatch is known to be nonzero also.
5075 begmatch + endmatch is how much we save by doing the ins/del.
5076 Is it worth it? */
5078 if (nsp != osp
5079 && (!FRAME_CHAR_INS_DEL_OK (f)
5080 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
5082 begmatch = 0;
5083 endmatch = 0;
5084 osp = nsp = min (osp, nsp);
5087 /* Now go through the line, inserting, writing and
5088 deleting as appropriate. */
5090 if (osp > nsp)
5092 cursor_to (f, vpos, nsp);
5093 delete_glyphs (f, osp - nsp);
5095 else if (nsp > osp)
5097 /* If going to delete chars later in line
5098 and insert earlier in the line,
5099 must delete first to avoid losing data in the insert */
5100 if (endmatch && nlen < olen + nsp - osp)
5102 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5103 delete_glyphs (f, olen + nsp - osp - nlen);
5104 olen = nlen - (nsp - osp);
5106 cursor_to (f, vpos, osp);
5107 insert_glyphs (f, 0, nsp - osp);
5109 olen += nsp - osp;
5111 tem = nsp + begmatch + endmatch;
5112 if (nlen != tem || olen != tem)
5114 if (!endmatch || nlen == olen)
5116 /* If new text being written reaches right margin, there is
5117 no need to do clear-to-eol at the end of this function
5118 (and it would not be safe, since cursor is not going to
5119 be "at the margin" after the text is done). */
5120 if (nlen == FRAME_TOTAL_COLS (f))
5121 olen = 0;
5123 /* Function write_glyphs is prepared to do nothing
5124 if passed a length <= 0. Check it here to avoid
5125 unnecessary cursor movement. */
5126 if (nlen - tem > 0)
5128 cursor_to (f, vpos, nsp + begmatch);
5129 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5132 else if (nlen > olen)
5134 /* Here, we used to have the following simple code:
5135 ----------------------------------------
5136 write_glyphs (nbody + nsp + begmatch, olen - tem);
5137 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5138 ----------------------------------------
5139 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5140 is a padding glyph. */
5141 int out = olen - tem; /* Columns to be overwritten originally. */
5142 int del;
5144 cursor_to (f, vpos, nsp + begmatch);
5146 /* Calculate columns we can actually overwrite. */
5147 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5148 out--;
5149 write_glyphs (f, nbody + nsp + begmatch, out);
5151 /* If we left columns to be overwritten, we must delete them. */
5152 del = olen - tem - out;
5153 if (del > 0)
5154 delete_glyphs (f, del);
5156 /* At last, we insert columns not yet written out. */
5157 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5158 olen = nlen;
5160 else if (olen > nlen)
5162 cursor_to (f, vpos, nsp + begmatch);
5163 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5164 delete_glyphs (f, olen - nlen);
5165 olen = nlen;
5169 just_erase:
5170 /* If any unerased characters remain after the new line, erase them. */
5171 if (olen > nlen)
5173 cursor_to (f, vpos, nlen);
5174 clear_end_of_line (f, olen);
5177 /* Exchange contents between current_frame and new_frame. */
5178 make_current (desired_matrix, current_matrix, vpos);
5183 /***********************************************************************
5184 X/Y Position -> Buffer Position
5185 ***********************************************************************/
5187 /* Determine what's under window-relative pixel position (*X, *Y).
5188 Return the OBJECT (string or buffer) that's there.
5189 Return in *POS the position in that object.
5190 Adjust *X and *Y to character positions.
5191 Return in *DX and *DY the pixel coordinates of the click,
5192 relative to the top left corner of OBJECT, or relative to
5193 the top left corner of the character glyph at (*X, *Y)
5194 if OBJECT is nil.
5195 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5196 if the coordinates point to an empty area of the display. */
5198 Lisp_Object
5199 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)
5201 struct it it;
5202 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5203 struct text_pos startp;
5204 Lisp_Object string;
5205 struct glyph_row *row;
5206 #ifdef HAVE_WINDOW_SYSTEM
5207 struct image *img = 0;
5208 #endif
5209 int x0, x1, to_x;
5210 void *itdata = NULL;
5212 /* We used to set current_buffer directly here, but that does the
5213 wrong thing with `face-remapping-alist' (bug#2044). */
5214 Fset_buffer (w->buffer);
5215 itdata = bidi_shelve_cache ();
5216 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5217 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5218 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5219 start_display (&it, w, startp);
5220 /* start_display takes into account the header-line row, but IT's
5221 vpos still counts from the glyph row that includes the window's
5222 start position. Adjust for a possible header-line row. */
5223 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w);
5225 x0 = *x;
5227 /* First, move to the beginning of the row corresponding to *Y. We
5228 need to be in that row to get the correct value of base paragraph
5229 direction for the text at (*X, *Y). */
5230 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5232 /* TO_X is the pixel position that the iterator will compute for the
5233 glyph at *X. We add it.first_visible_x because iterator
5234 positions include the hscroll. */
5235 to_x = x0 + it.first_visible_x;
5236 if (it.bidi_it.paragraph_dir == R2L)
5237 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5238 text area. This is because the iterator, even in R2L
5239 paragraphs, delivers glyphs as if they started at the left
5240 margin of the window. (When we actually produce glyphs for
5241 display, we reverse their order in PRODUCE_GLYPHS, but the
5242 iterator doesn't know about that.) The following line adjusts
5243 the pixel position to the iterator geometry, which is what
5244 move_it_* routines use. (The -1 is because in a window whose
5245 text-area width is W, the rightmost pixel position is W-1, and
5246 it should be mirrored into zero pixel position.) */
5247 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5249 /* Now move horizontally in the row to the glyph under *X. Second
5250 argument is ZV to prevent move_it_in_display_line from matching
5251 based on buffer positions. */
5252 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5253 bidi_unshelve_cache (itdata, 0);
5255 Fset_buffer (old_current_buffer);
5257 *dx = x0 + it.first_visible_x - it.current_x;
5258 *dy = *y - it.current_y;
5260 string = w->buffer;
5261 if (STRINGP (it.string))
5262 string = it.string;
5263 *pos = it.current;
5264 if (it.what == IT_COMPOSITION
5265 && it.cmp_it.nchars > 1
5266 && it.cmp_it.reversed_p)
5268 /* The current display element is a grapheme cluster in a
5269 composition. In that case, we need the position of the first
5270 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5271 it.current points to the last character of the cluster, thus
5272 we must move back to the first character of the same
5273 cluster. */
5274 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5275 if (STRINGP (it.string))
5276 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5277 else
5278 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->buffer),
5279 CHARPOS (pos->pos));
5282 #ifdef HAVE_WINDOW_SYSTEM
5283 if (it.what == IT_IMAGE)
5285 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5286 && !NILP (img->spec))
5287 *object = img->spec;
5289 #endif
5291 if (it.vpos < w->current_matrix->nrows
5292 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5293 row->enabled_p))
5295 if (it.hpos < row->used[TEXT_AREA])
5297 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5298 #ifdef HAVE_WINDOW_SYSTEM
5299 if (img)
5301 *dy -= row->ascent - glyph->ascent;
5302 *dx += glyph->slice.img.x;
5303 *dy += glyph->slice.img.y;
5304 /* Image slices positions are still relative to the entire image */
5305 *width = img->width;
5306 *height = img->height;
5308 else
5309 #endif
5311 *width = glyph->pixel_width;
5312 *height = glyph->ascent + glyph->descent;
5315 else
5317 *width = 0;
5318 *height = row->height;
5321 else
5323 *width = *height = 0;
5326 /* Add extra (default width) columns if clicked after EOL. */
5327 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5328 if (x0 > x1)
5329 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5331 *x = it.hpos;
5332 *y = it.vpos;
5334 return string;
5338 /* Value is the string under window-relative coordinates X/Y in the
5339 mode line or header line (PART says which) of window W, or nil if none.
5340 *CHARPOS is set to the position in the string returned. */
5342 Lisp_Object
5343 mode_line_string (struct window *w, enum window_part part,
5344 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5345 int *dx, int *dy, int *width, int *height)
5347 struct glyph_row *row;
5348 struct glyph *glyph, *end;
5349 int x0, y0;
5350 Lisp_Object string = Qnil;
5352 if (part == ON_MODE_LINE)
5353 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5354 else
5355 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5356 y0 = *y - row->y;
5357 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5359 if (row->mode_line_p && row->enabled_p)
5361 /* Find the glyph under X. If we find one with a string object,
5362 it's the one we were looking for. */
5363 glyph = row->glyphs[TEXT_AREA];
5364 end = glyph + row->used[TEXT_AREA];
5365 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5366 x0 -= glyph->pixel_width;
5367 *x = glyph - row->glyphs[TEXT_AREA];
5368 if (glyph < end)
5370 string = glyph->object;
5371 *charpos = glyph->charpos;
5372 *width = glyph->pixel_width;
5373 *height = glyph->ascent + glyph->descent;
5374 #ifdef HAVE_WINDOW_SYSTEM
5375 if (glyph->type == IMAGE_GLYPH)
5377 struct image *img;
5378 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5379 if (img != NULL)
5380 *object = img->spec;
5381 y0 -= row->ascent - glyph->ascent;
5383 #endif
5385 else
5387 /* Add extra (default width) columns if clicked after EOL. */
5388 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5389 *width = 0;
5390 *height = row->height;
5393 else
5395 *x = 0;
5396 x0 = 0;
5397 *width = *height = 0;
5400 *dx = x0;
5401 *dy = y0;
5403 return string;
5407 /* Value is the string under window-relative coordinates X/Y in either
5408 marginal area, or nil if none. *CHARPOS is set to the position in
5409 the string returned. */
5411 Lisp_Object
5412 marginal_area_string (struct window *w, enum window_part part,
5413 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5414 int *dx, int *dy, int *width, int *height)
5416 struct glyph_row *row = w->current_matrix->rows;
5417 struct glyph *glyph, *end;
5418 int x0, y0, i, wy = *y;
5419 int area;
5420 Lisp_Object string = Qnil;
5422 if (part == ON_LEFT_MARGIN)
5423 area = LEFT_MARGIN_AREA;
5424 else if (part == ON_RIGHT_MARGIN)
5425 area = RIGHT_MARGIN_AREA;
5426 else
5427 emacs_abort ();
5429 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5430 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5431 break;
5432 y0 = *y - row->y;
5433 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5435 if (row->enabled_p)
5437 /* Find the glyph under X. If we find one with a string object,
5438 it's the one we were looking for. */
5439 if (area == RIGHT_MARGIN_AREA)
5440 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5441 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5442 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5443 + window_box_width (w, LEFT_MARGIN_AREA)
5444 + window_box_width (w, TEXT_AREA));
5445 else
5446 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5447 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5448 : 0);
5450 glyph = row->glyphs[area];
5451 end = glyph + row->used[area];
5452 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5453 x0 -= glyph->pixel_width;
5454 *x = glyph - row->glyphs[area];
5455 if (glyph < end)
5457 string = glyph->object;
5458 *charpos = glyph->charpos;
5459 *width = glyph->pixel_width;
5460 *height = glyph->ascent + glyph->descent;
5461 #ifdef HAVE_WINDOW_SYSTEM
5462 if (glyph->type == IMAGE_GLYPH)
5464 struct image *img;
5465 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5466 if (img != NULL)
5467 *object = img->spec;
5468 y0 -= row->ascent - glyph->ascent;
5469 x0 += glyph->slice.img.x;
5470 y0 += glyph->slice.img.y;
5472 #endif
5474 else
5476 /* Add extra (default width) columns if clicked after EOL. */
5477 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5478 *width = 0;
5479 *height = row->height;
5482 else
5484 x0 = 0;
5485 *x = 0;
5486 *width = *height = 0;
5489 *dx = x0;
5490 *dy = y0;
5492 return string;
5496 /***********************************************************************
5497 Changing Frame Sizes
5498 ***********************************************************************/
5500 #ifdef SIGWINCH
5502 static void deliver_window_change_signal (int);
5504 static void
5505 handle_window_change_signal (int sig)
5507 int width, height;
5508 struct tty_display_info *tty;
5510 /* The frame size change obviously applies to a single
5511 termcap-controlled terminal, but we can't decide which.
5512 Therefore, we resize the frames corresponding to each tty.
5514 for (tty = tty_list; tty; tty = tty->next) {
5516 if (! tty->term_initted)
5517 continue;
5519 /* Suspended tty frames have tty->input == NULL avoid trying to
5520 use it. */
5521 if (!tty->input)
5522 continue;
5524 get_tty_size (fileno (tty->input), &width, &height);
5526 if (width > 5 && height > 2) {
5527 Lisp_Object tail, frame;
5529 FOR_EACH_FRAME (tail, frame)
5530 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5531 /* Record the new sizes, but don't reallocate the data
5532 structures now. Let that be done later outside of the
5533 signal handler. */
5534 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5539 static void
5540 deliver_window_change_signal (int sig)
5542 deliver_process_signal (sig, handle_window_change_signal);
5544 #endif /* SIGWINCH */
5547 /* Do any change in frame size that was requested by a signal.
5548 SAFE means this function is called from a place where it is
5549 safe to change frame sizes while a redisplay is in progress. */
5551 void
5552 do_pending_window_change (bool safe)
5554 /* If window change signal handler should have run before, run it now. */
5555 if (redisplaying_p && !safe)
5556 return;
5558 while (delayed_size_change)
5560 Lisp_Object tail, frame;
5562 delayed_size_change = 0;
5564 FOR_EACH_FRAME (tail, frame)
5566 struct frame *f = XFRAME (frame);
5568 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5569 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5570 0, 0, safe);
5576 /* Change the frame height and/or width. Values may be given as zero to
5577 indicate no change is to take place.
5579 If DELAY, assume we're being called from a signal handler, and
5580 queue the change for later - perhaps the next redisplay.
5581 Since this tries to resize windows, we can't call it
5582 from a signal handler.
5584 SAFE means this function is called from a place where it's
5585 safe to change frame sizes while a redisplay is in progress. */
5587 void
5588 change_frame_size (struct frame *f, int newheight, int newwidth,
5589 bool pretend, bool delay, bool safe)
5591 Lisp_Object tail, frame;
5593 if (FRAME_MSDOS_P (f))
5595 /* On MS-DOS, all frames use the same screen, so a change in
5596 size affects all frames. Termcap now supports multiple
5597 ttys. */
5598 FOR_EACH_FRAME (tail, frame)
5599 if (! FRAME_WINDOW_P (XFRAME (frame)))
5600 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5601 pretend, delay, safe);
5603 else
5604 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5607 static void
5608 change_frame_size_1 (struct frame *f, int newheight, int newwidth,
5609 bool pretend, bool delay, bool safe)
5611 int new_frame_total_cols;
5612 ptrdiff_t count = SPECPDL_INDEX ();
5614 /* If we can't deal with the change now, queue it for later. */
5615 if (delay || (redisplaying_p && !safe))
5617 f->new_text_lines = newheight;
5618 f->new_text_cols = newwidth;
5619 delayed_size_change = 1;
5620 return;
5623 /* This size-change overrides any pending one for this frame. */
5624 f->new_text_lines = 0;
5625 f->new_text_cols = 0;
5627 /* If an argument is zero, set it to the current value. */
5628 if (newheight == 0)
5629 newheight = FRAME_LINES (f);
5630 if (newwidth == 0)
5631 newwidth = FRAME_COLS (f);
5633 /* Compute width of windows in F. */
5634 /* Round up to the smallest acceptable size. */
5635 check_frame_size (f, &newheight, &newwidth);
5637 /* This is the width of the frame with vertical scroll bars and fringe
5638 columns. Do this after rounding - see discussion of bug#9723. */
5639 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5641 /* If we're not changing the frame size, quit now. */
5642 /* Frame width may be unchanged but the text portion may change, for
5643 example, fullscreen and remove/add scroll bar. */
5644 if (newheight == FRAME_LINES (f)
5645 /* Text portion unchanged? */
5646 && newwidth == FRAME_COLS (f)
5647 /* Frame width unchanged? */
5648 && new_frame_total_cols == FRAME_TOTAL_COLS (f))
5649 return;
5651 block_input ();
5653 #ifdef MSDOS
5654 /* We only can set screen dimensions to certain values supported
5655 by our video hardware. Try to find the smallest size greater
5656 or equal to the requested dimensions. */
5657 dos_set_window_size (&newheight, &newwidth);
5658 #endif
5660 if (newheight != FRAME_LINES (f))
5662 resize_frame_windows (f, newheight, 0);
5664 /* MSDOS frames cannot PRETEND, as they change frame size by
5665 manipulating video hardware. */
5666 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5667 FrameRows (FRAME_TTY (f)) = newheight;
5670 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5672 resize_frame_windows (f, new_frame_total_cols, 1);
5674 /* MSDOS frames cannot PRETEND, as they change frame size by
5675 manipulating video hardware. */
5676 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5677 FrameCols (FRAME_TTY (f)) = newwidth;
5679 if (WINDOWP (f->tool_bar_window))
5680 wset_total_cols (XWINDOW (f->tool_bar_window), make_number (newwidth));
5683 FRAME_LINES (f) = newheight;
5684 SET_FRAME_COLS (f, newwidth);
5687 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5688 int text_area_x, text_area_y, text_area_width, text_area_height;
5690 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5691 &text_area_height);
5692 if (w->cursor.x >= text_area_x + text_area_width)
5693 w->cursor.hpos = w->cursor.x = 0;
5694 if (w->cursor.y >= text_area_y + text_area_height)
5695 w->cursor.vpos = w->cursor.y = 0;
5698 adjust_glyphs (f);
5699 calculate_costs (f);
5700 SET_FRAME_GARBAGED (f);
5701 f->resized_p = 1;
5703 unblock_input ();
5705 record_unwind_current_buffer ();
5707 run_window_configuration_change_hook (f);
5709 unbind_to (count, Qnil);
5714 /***********************************************************************
5715 Terminal Related Lisp Functions
5716 ***********************************************************************/
5718 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5719 1, 1, "FOpen termscript file: ",
5720 doc: /* Start writing all terminal output to FILE as well as the terminal.
5721 FILE = nil means just close any termscript file currently open. */)
5722 (Lisp_Object file)
5724 struct tty_display_info *tty;
5726 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5727 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5728 error ("Current frame is not on a tty device");
5730 tty = CURTTY ();
5732 if (tty->termscript != 0)
5734 block_input ();
5735 fclose (tty->termscript);
5736 unblock_input ();
5738 tty->termscript = 0;
5740 if (! NILP (file))
5742 file = Fexpand_file_name (file, Qnil);
5743 tty->termscript = fopen (SSDATA (file), "w");
5744 if (tty->termscript == 0)
5745 report_file_error ("Opening termscript", Fcons (file, Qnil));
5747 return Qnil;
5751 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5752 Ssend_string_to_terminal, 1, 2, 0,
5753 doc: /* Send STRING to the terminal without alteration.
5754 Control characters in STRING will have terminal-dependent effects.
5756 Optional parameter TERMINAL specifies the tty terminal device to use.
5757 It may be a terminal object, a frame, or nil for the terminal used by
5758 the currently selected frame. In batch mode, STRING is sent to stdout
5759 when TERMINAL is nil. */)
5760 (Lisp_Object string, Lisp_Object terminal)
5762 struct terminal *t = get_terminal (terminal, 1);
5763 FILE *out;
5765 /* ??? Perhaps we should do something special for multibyte strings here. */
5766 CHECK_STRING (string);
5767 block_input ();
5769 if (!t)
5770 error ("Unknown terminal device");
5772 if (t->type == output_initial)
5773 out = stdout;
5774 else if (t->type != output_termcap && t->type != output_msdos_raw)
5775 error ("Device %d is not a termcap terminal device", t->id);
5776 else
5778 struct tty_display_info *tty = t->display_info.tty;
5780 if (! tty->output)
5781 error ("Terminal is currently suspended");
5783 if (tty->termscript)
5785 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5786 fflush (tty->termscript);
5788 out = tty->output;
5790 fwrite (SDATA (string), 1, SBYTES (string), out);
5791 fflush (out);
5792 unblock_input ();
5793 return Qnil;
5797 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5798 doc: /* Beep, or flash the screen.
5799 Also, unless an argument is given,
5800 terminate any keyboard macro currently executing. */)
5801 (Lisp_Object arg)
5803 if (!NILP (arg))
5805 if (noninteractive)
5806 putchar (07);
5807 else
5808 ring_bell (XFRAME (selected_frame));
5810 else
5811 bitch_at_user ();
5813 return Qnil;
5816 void
5817 bitch_at_user (void)
5819 if (noninteractive)
5820 putchar (07);
5821 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5822 error ("Keyboard macro terminated by a command ringing the bell");
5823 else
5824 ring_bell (XFRAME (selected_frame));
5829 /***********************************************************************
5830 Sleeping, Waiting
5831 ***********************************************************************/
5833 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5834 doc: /* Pause, without updating display, for SECONDS seconds.
5835 SECONDS may be a floating-point value, meaning that you can wait for a
5836 fraction of a second. Optional second arg MILLISECONDS specifies an
5837 additional wait period, in milliseconds; this is for backwards compatibility.
5838 \(Not all operating systems support waiting for a fraction of a second.) */)
5839 (Lisp_Object seconds, Lisp_Object milliseconds)
5841 double duration = extract_float (seconds);
5843 if (!NILP (milliseconds))
5845 CHECK_NUMBER (milliseconds);
5846 duration += XINT (milliseconds) / 1000.0;
5849 if (0 < duration)
5851 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (duration);
5852 wait_reading_process_output (min (EMACS_SECS (t), WAIT_READING_MAX),
5853 EMACS_NSECS (t), 0, 0, Qnil, NULL, 0);
5856 return Qnil;
5860 /* This is just like wait_reading_process_output, except that
5861 it does redisplay.
5863 TIMEOUT is number of seconds to wait (float or integer),
5864 or t to wait forever.
5865 READING is true if reading input.
5866 If DISPLAY_OPTION is >0 display process output while waiting.
5867 If DISPLAY_OPTION is >1 perform an initial redisplay before waiting.
5870 Lisp_Object
5871 sit_for (Lisp_Object timeout, bool reading, int display_option)
5873 intmax_t sec;
5874 int nsec;
5875 bool do_display = display_option > 0;
5877 swallow_events (do_display);
5879 if ((detect_input_pending_run_timers (do_display))
5880 || !NILP (Vexecuting_kbd_macro))
5881 return Qnil;
5883 if (display_option > 1)
5884 redisplay_preserve_echo_area (2);
5886 if (INTEGERP (timeout))
5888 sec = XINT (timeout);
5889 if (! (0 < sec))
5890 return Qt;
5891 nsec = 0;
5893 else if (FLOATP (timeout))
5895 double seconds = XFLOAT_DATA (timeout);
5896 if (! (0 < seconds))
5897 return Qt;
5898 else
5900 EMACS_TIME t = EMACS_TIME_FROM_DOUBLE (seconds);
5901 sec = min (EMACS_SECS (t), WAIT_READING_MAX);
5902 nsec = EMACS_NSECS (t);
5905 else if (EQ (timeout, Qt))
5907 sec = 0;
5908 nsec = 0;
5910 else
5911 wrong_type_argument (Qnumberp, timeout);
5914 #ifdef USABLE_SIGIO
5915 gobble_input ();
5916 #endif
5918 wait_reading_process_output (sec, nsec, reading ? -1 : 1, do_display,
5919 Qnil, NULL, 0);
5921 return detect_input_pending () ? Qnil : Qt;
5925 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
5926 doc: /* Perform redisplay.
5927 Optional arg FORCE, if non-nil, prevents redisplay from being
5928 preempted by arriving input, even if `redisplay-dont-pause' is nil.
5929 If `redisplay-dont-pause' is non-nil (the default), redisplay is never
5930 preempted by arriving input, so FORCE does nothing.
5932 Return t if redisplay was performed, nil if redisplay was preempted
5933 immediately by pending input. */)
5934 (Lisp_Object force)
5936 ptrdiff_t count;
5938 swallow_events (1);
5939 if ((detect_input_pending_run_timers (1)
5940 && NILP (force) && !redisplay_dont_pause)
5941 || !NILP (Vexecuting_kbd_macro))
5942 return Qnil;
5944 count = SPECPDL_INDEX ();
5945 if (!NILP (force) && !redisplay_dont_pause)
5946 specbind (Qredisplay_dont_pause, Qt);
5947 redisplay_preserve_echo_area (2);
5948 unbind_to (count, Qnil);
5949 return Qt;
5954 /***********************************************************************
5955 Other Lisp Functions
5956 ***********************************************************************/
5958 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
5959 session's frames, frame names, buffers, buffer-read-only flags, and
5960 buffer-modified-flags. */
5962 static Lisp_Object frame_and_buffer_state;
5965 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
5966 Sframe_or_buffer_changed_p, 0, 1, 0,
5967 doc: /* Return non-nil if the frame and buffer state appears to have changed.
5968 VARIABLE is a variable name whose value is either nil or a state vector
5969 that will be updated to contain all frames and buffers,
5970 aside from buffers whose names start with space,
5971 along with the buffers' read-only and modified flags. This allows a fast
5972 check to see whether buffer menus might need to be recomputed.
5973 If this function returns non-nil, it updates the internal vector to reflect
5974 the current state.
5976 If VARIABLE is nil, an internal variable is used. Users should not
5977 pass nil for VARIABLE. */)
5978 (Lisp_Object variable)
5980 Lisp_Object state, tail, frame, buf;
5981 ptrdiff_t n, idx;
5983 if (! NILP (variable))
5985 CHECK_SYMBOL (variable);
5986 state = Fsymbol_value (variable);
5987 if (! VECTORP (state))
5988 goto changed;
5990 else
5991 state = frame_and_buffer_state;
5993 idx = 0;
5994 FOR_EACH_FRAME (tail, frame)
5996 if (idx == ASIZE (state))
5997 goto changed;
5998 if (!EQ (AREF (state, idx++), frame))
5999 goto changed;
6000 if (idx == ASIZE (state))
6001 goto changed;
6002 if (!EQ (AREF (state, idx++), XFRAME (frame)->name))
6003 goto changed;
6005 /* Check that the buffer info matches. */
6006 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6008 buf = XCDR (XCAR (tail));
6009 /* Ignore buffers that aren't included in buffer lists. */
6010 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6011 continue;
6012 if (idx == ASIZE (state))
6013 goto changed;
6014 if (!EQ (AREF (state, idx++), buf))
6015 goto changed;
6016 if (idx == ASIZE (state))
6017 goto changed;
6018 if (!EQ (AREF (state, idx++), BVAR (XBUFFER (buf), read_only)))
6019 goto changed;
6020 if (idx == ASIZE (state))
6021 goto changed;
6022 if (!EQ (AREF (state, idx++), Fbuffer_modified_p (buf)))
6023 goto changed;
6025 if (idx == ASIZE (state))
6026 goto changed;
6027 /* Detect deletion of a buffer at the end of the list. */
6028 if (EQ (AREF (state, idx), Qlambda))
6029 return Qnil;
6031 /* Come here if we decide the data has changed. */
6032 changed:
6033 /* Count the size we will need.
6034 Start with 1 so there is room for at least one lambda at the end. */
6035 n = 1;
6036 FOR_EACH_FRAME (tail, frame)
6037 n += 2;
6038 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6039 n += 3;
6040 /* Reallocate the vector if data has grown to need it,
6041 or if it has shrunk a lot. */
6042 if (! VECTORP (state)
6043 || n > ASIZE (state)
6044 || n + 20 < ASIZE (state) / 2)
6045 /* Add 20 extra so we grow it less often. */
6047 state = Fmake_vector (make_number (n + 20), Qlambda);
6048 if (! NILP (variable))
6049 Fset (variable, state);
6050 else
6051 frame_and_buffer_state = state;
6054 /* Record the new data in the (possibly reallocated) vector. */
6055 idx = 0;
6056 FOR_EACH_FRAME (tail, frame)
6058 ASET (state, idx, frame);
6059 idx++;
6060 ASET (state, idx, XFRAME (frame)->name);
6061 idx++;
6063 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6065 buf = XCDR (XCAR (tail));
6066 /* Ignore buffers that aren't included in buffer lists. */
6067 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
6068 continue;
6069 ASET (state, idx, buf);
6070 idx++;
6071 ASET (state, idx, BVAR (XBUFFER (buf), read_only));
6072 idx++;
6073 ASET (state, idx, Fbuffer_modified_p (buf));
6074 idx++;
6076 /* Fill up the vector with lambdas (always at least one). */
6077 ASET (state, idx, Qlambda);
6078 idx++;
6079 while (idx < ASIZE (state))
6081 ASET (state, idx, Qlambda);
6082 idx++;
6084 /* Make sure we didn't overflow the vector. */
6085 eassert (idx <= ASIZE (state));
6086 return Qt;
6091 /***********************************************************************
6092 Initialization
6093 ***********************************************************************/
6095 /* Initialization done when Emacs fork is started, before doing stty.
6096 Determine terminal type and set terminal_driver. Then invoke its
6097 decoding routine to set up variables in the terminal package. */
6099 void
6100 init_display (void)
6102 char *terminal_type;
6104 /* Construct the space glyph. */
6105 space_glyph.type = CHAR_GLYPH;
6106 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
6107 space_glyph.charpos = -1;
6109 inverse_video = 0;
6110 cursor_in_echo_area = 0;
6111 terminal_type = (char *) 0;
6113 /* Now is the time to initialize this; it's used by init_sys_modes
6114 during startup. */
6115 Vinitial_window_system = Qnil;
6117 /* SIGWINCH needs to be handled no matter what display we start
6118 with. Otherwise newly opened tty frames will not resize
6119 automatically. */
6120 #ifdef SIGWINCH
6121 #ifndef CANNOT_DUMP
6122 if (initialized)
6123 #endif /* CANNOT_DUMP */
6125 struct sigaction action;
6126 emacs_sigaction_init (&action, deliver_window_change_signal);
6127 sigaction (SIGWINCH, &action, 0);
6129 #endif /* SIGWINCH */
6131 /* If running as a daemon, no need to initialize any frames/terminal. */
6132 if (IS_DAEMON)
6133 return;
6135 /* If the user wants to use a window system, we shouldn't bother
6136 initializing the terminal. This is especially important when the
6137 terminal is so dumb that emacs gives up before and doesn't bother
6138 using the window system.
6140 If the DISPLAY environment variable is set and nonempty,
6141 try to use X, and die with an error message if that doesn't work. */
6143 #ifdef HAVE_X_WINDOWS
6144 if (! inhibit_window_system && ! display_arg)
6146 char *display;
6147 display = getenv ("DISPLAY");
6148 display_arg = (display != 0 && *display != 0);
6150 if (display_arg && !x_display_ok (display))
6152 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6153 display);
6154 inhibit_window_system = 1;
6158 if (!inhibit_window_system && display_arg)
6160 Vinitial_window_system = Qx;
6161 #ifdef HAVE_X11
6162 Vwindow_system_version = make_number (11);
6163 #endif
6164 #ifdef GNU_LINUX
6165 /* In some versions of ncurses,
6166 tputs crashes if we have not called tgetent.
6167 So call tgetent. */
6168 { char b[2044]; tgetent (b, "xterm");}
6169 #endif
6170 return;
6172 #endif /* HAVE_X_WINDOWS */
6174 #ifdef HAVE_NTGUI
6175 if (!inhibit_window_system)
6177 Vinitial_window_system = Qw32;
6178 Vwindow_system_version = make_number (1);
6179 return;
6181 #endif /* HAVE_NTGUI */
6183 #ifdef HAVE_NS
6184 if (!inhibit_window_system
6185 #ifndef CANNOT_DUMP
6186 && initialized
6187 #endif
6190 Vinitial_window_system = Qns;
6191 Vwindow_system_version = make_number (10);
6192 return;
6194 #endif
6196 /* If no window system has been specified, try to use the terminal. */
6197 if (! isatty (0))
6199 fatal ("standard input is not a tty");
6200 exit (1);
6203 #ifdef WINDOWSNT
6204 terminal_type = "w32console";
6205 #else
6206 /* Look at the TERM variable. */
6207 terminal_type = (char *) getenv ("TERM");
6208 #endif
6209 if (!terminal_type)
6211 #ifdef HAVE_WINDOW_SYSTEM
6212 if (! inhibit_window_system)
6213 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6214 else
6215 #endif /* HAVE_WINDOW_SYSTEM */
6216 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6217 exit (1);
6221 struct terminal *t;
6222 struct frame *f = XFRAME (selected_frame);
6224 init_foreground_group ();
6226 /* Open a display on the controlling tty. */
6227 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6229 /* Convert the initial frame to use the new display. */
6230 if (f->output_method != output_initial)
6231 emacs_abort ();
6232 f->output_method = t->type;
6233 f->terminal = t;
6235 t->reference_count++;
6236 #ifdef MSDOS
6237 f->output_data.tty->display_info = &the_only_display_info;
6238 #else
6239 if (f->output_method == output_termcap)
6240 create_tty_output (f);
6241 #endif
6242 t->display_info.tty->top_frame = selected_frame;
6243 change_frame_size (XFRAME (selected_frame),
6244 FrameRows (t->display_info.tty),
6245 FrameCols (t->display_info.tty), 0, 0, 1);
6247 /* Delete the initial terminal. */
6248 if (--initial_terminal->reference_count == 0
6249 && initial_terminal->delete_terminal_hook)
6250 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6252 /* Update frame parameters to reflect the new type. */
6253 Fmodify_frame_parameters
6254 (selected_frame, Fcons (Fcons (Qtty_type,
6255 Ftty_type (selected_frame)), Qnil));
6256 if (t->display_info.tty->name)
6257 Fmodify_frame_parameters (selected_frame,
6258 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6259 Qnil));
6260 else
6261 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6262 Qnil));
6266 struct frame *sf = SELECTED_FRAME ();
6267 int width = FRAME_TOTAL_COLS (sf);
6268 int height = FRAME_LINES (sf);
6270 /* If these sizes are so big they cause overflow, just ignore the
6271 change. It's not clear what better we could do. The rest of
6272 the code assumes that (width + 2) * height * sizeof (struct glyph)
6273 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6274 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6275 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6276 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6277 < (width + 2) * height))
6278 fatal ("screen size %dx%d too big", width, height);
6281 calculate_costs (XFRAME (selected_frame));
6283 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6284 if (initialized
6285 && !noninteractive
6286 && NILP (Vinitial_window_system))
6288 /* For the initial frame, we don't have any way of knowing what
6289 are the foreground and background colors of the terminal. */
6290 struct frame *sf = SELECTED_FRAME ();
6292 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6293 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6294 call0 (intern ("tty-set-up-initial-frame-faces"));
6300 /***********************************************************************
6301 Blinking cursor
6302 ***********************************************************************/
6304 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6305 Sinternal_show_cursor, 2, 2, 0,
6306 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6307 WINDOW nil means use the selected window. SHOW non-nil means
6308 show a cursor in WINDOW in the next redisplay. SHOW nil means
6309 don't show a cursor. */)
6310 (Lisp_Object window, Lisp_Object show)
6312 /* Don't change cursor state while redisplaying. This could confuse
6313 output routines. */
6314 if (!redisplaying_p)
6315 decode_any_window (window)->cursor_off_p = NILP (show);
6316 return Qnil;
6320 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6321 Sinternal_show_cursor_p, 0, 1, 0,
6322 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6323 WINDOW nil or omitted means report on the selected window. */)
6324 (Lisp_Object window)
6326 return decode_any_window (window)->cursor_off_p ? Qnil : Qt;
6329 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6330 Slast_nonminibuf_frame, 0, 0, 0,
6331 doc: /* Value is last nonminibuffer frame. */)
6332 (void)
6334 Lisp_Object frame = Qnil;
6336 if (last_nonminibuf_frame)
6337 XSETFRAME (frame, last_nonminibuf_frame);
6339 return frame;
6342 /***********************************************************************
6343 Initialization
6344 ***********************************************************************/
6346 void
6347 syms_of_display (void)
6349 defsubr (&Sredraw_frame);
6350 defsubr (&Sredraw_display);
6351 defsubr (&Sframe_or_buffer_changed_p);
6352 defsubr (&Sopen_termscript);
6353 defsubr (&Sding);
6354 defsubr (&Sredisplay);
6355 defsubr (&Ssleep_for);
6356 defsubr (&Ssend_string_to_terminal);
6357 defsubr (&Sinternal_show_cursor);
6358 defsubr (&Sinternal_show_cursor_p);
6359 defsubr (&Slast_nonminibuf_frame);
6361 #ifdef GLYPH_DEBUG
6362 defsubr (&Sdump_redisplay_history);
6363 #endif
6365 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6366 staticpro (&frame_and_buffer_state);
6368 DEFSYM (Qdisplay_table, "display-table");
6369 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6371 DEFVAR_INT ("baud-rate", baud_rate,
6372 doc: /* The output baud rate of the terminal.
6373 On most systems, changing this value will affect the amount of padding
6374 and the other strategic decisions made during redisplay. */);
6376 DEFVAR_BOOL ("inverse-video", inverse_video,
6377 doc: /* Non-nil means invert the entire frame display.
6378 This means everything is in inverse video which otherwise would not be. */);
6380 DEFVAR_BOOL ("visible-bell", visible_bell,
6381 doc: /* Non-nil means try to flash the frame to represent a bell.
6383 See also `ring-bell-function'. */);
6385 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6386 doc: /* Non-nil means no need to redraw entire frame after suspending.
6387 A non-nil value is useful if the terminal can automatically preserve
6388 Emacs's frame display when you reenter Emacs.
6389 It is up to you to set this variable if your terminal can do that. */);
6391 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6392 doc: /* Name of the window system that Emacs uses for the first frame.
6393 The value is a symbol:
6394 nil for a termcap frame (a character-only terminal),
6395 'x' for an Emacs frame that is really an X window,
6396 'w32' for an Emacs frame that is a window on MS-Windows display,
6397 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6398 'pc' for a direct-write MS-DOS frame.
6400 Use of this variable as a boolean is deprecated. Instead,
6401 use `display-graphic-p' or any of the other `display-*-p'
6402 predicates which report frame's specific UI-related capabilities. */);
6404 DEFVAR_KBOARD ("window-system", Vwindow_system,
6405 doc: /* Name of window system through which the selected frame is displayed.
6406 The value is a symbol:
6407 nil for a termcap frame (a character-only terminal),
6408 'x' for an Emacs frame that is really an X window,
6409 'w32' for an Emacs frame that is a window on MS-Windows display,
6410 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6411 'pc' for a direct-write MS-DOS frame.
6413 Use of this variable as a boolean is deprecated. Instead,
6414 use `display-graphic-p' or any of the other `display-*-p'
6415 predicates which report frame's specific UI-related capabilities. */);
6417 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6418 doc: /* The version number of the window system in use.
6419 For X windows, this is 11. */);
6421 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6422 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6424 DEFVAR_LISP ("glyph-table", Vglyph_table,
6425 doc: /* Table defining how to output a glyph code to the frame.
6426 If not nil, this is a vector indexed by glyph code to define the glyph.
6427 Each element can be:
6428 integer: a glyph code which this glyph is an alias for.
6429 string: output this glyph using that string (not impl. in X windows).
6430 nil: this glyph mod 524288 is the code of a character to output,
6431 and this glyph / 524288 is the face number (see `face-id') to use
6432 while outputting it. */);
6433 Vglyph_table = Qnil;
6435 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6436 doc: /* Display table to use for buffers that specify none.
6437 See `buffer-display-table' for more information. */);
6438 Vstandard_display_table = Qnil;
6440 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6441 doc: /* Non-nil means display update isn't paused when input is detected. */);
6442 redisplay_dont_pause = 1;
6444 #if PERIODIC_PREEMPTION_CHECKING
6445 DEFVAR_LISP ("redisplay-preemption-period", Vredisplay_preemption_period,
6446 doc: /* Period in seconds between checking for input during redisplay.
6447 This has an effect only if `redisplay-dont-pause' is nil; in that
6448 case, arriving input preempts redisplay until the input is processed.
6449 If the value is nil, redisplay is never preempted. */);
6450 Vredisplay_preemption_period = make_float (0.10);
6451 #endif
6453 #ifdef CANNOT_DUMP
6454 if (noninteractive)
6455 #endif
6457 Vinitial_window_system = Qnil;
6458 Vwindow_system_version = Qnil;