Merge from mainline.
[emacs.git] / src / dispnew.c
blobeb632aeb2647bef5973ba4ec819dce00e8f138de
1 /* Updating of data structures for redisplay.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <setjmp.h>
26 #include <unistd.h>
28 #include "lisp.h"
29 #include "termchar.h"
30 #include "termopts.h"
31 /* cm.h must come after dispextern.h on Windows. */
32 #include "dispextern.h"
33 #include "cm.h"
34 #include "buffer.h"
35 #include "character.h"
36 #include "keyboard.h"
37 #include "frame.h"
38 #include "termhooks.h"
39 #include "window.h"
40 #include "commands.h"
41 #include "disptab.h"
42 #include "indent.h"
43 #include "intervals.h"
44 #include "blockinput.h"
45 #include "process.h"
47 #include "syssignal.h"
49 #ifdef HAVE_X_WINDOWS
50 #include "xterm.h"
51 #endif /* HAVE_X_WINDOWS */
53 #ifdef HAVE_NTGUI
54 #include "w32term.h"
55 #endif /* HAVE_NTGUI */
57 #ifdef HAVE_NS
58 #include "nsterm.h"
59 #endif
61 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
63 #include "systime.h"
64 #include <errno.h>
66 /* Get number of chars of output now in the buffer of a stdio stream.
67 This ought to be built in in stdio, but it isn't. Some s- files
68 override this because their stdio internals differ. */
70 #ifdef __GNU_LIBRARY__
72 /* The s- file might have overridden the definition with one that
73 works for the system's C library. But we are using the GNU C
74 library, so this is the right definition for every system. */
76 #ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
77 #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
78 #else
79 #undef PENDING_OUTPUT_COUNT
80 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
81 #endif
82 #else /* not __GNU_LIBRARY__ */
83 #if !defined (PENDING_OUTPUT_COUNT) && HAVE_STDIO_EXT_H && HAVE___FPENDING
84 #include <stdio_ext.h>
85 #define PENDING_OUTPUT_COUNT(FILE) __fpending (FILE)
86 #endif
87 #ifndef PENDING_OUTPUT_COUNT
88 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
89 #endif
90 #endif /* not __GNU_LIBRARY__ */
92 #if defined(HAVE_TERM_H) && defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
93 #include <term.h> /* for tgetent */
94 #endif
96 /* Structure to pass dimensions around. Used for character bounding
97 boxes, glyph matrix dimensions and alike. */
99 struct dim
101 int width;
102 int height;
106 /* Function prototypes. */
108 static struct glyph_matrix *save_current_matrix (struct frame *);
109 static void restore_current_matrix (struct frame *, struct glyph_matrix *);
110 static int showing_window_margins_p (struct window *);
111 static void fake_current_matrices (Lisp_Object);
112 static void redraw_overlapping_rows (struct window *, int);
113 static void redraw_overlapped_rows (struct window *, int);
114 static int count_blanks (struct glyph *, int);
115 static int count_match (struct glyph *, struct glyph *,
116 struct glyph *, struct glyph *);
117 static unsigned line_draw_cost (struct glyph_matrix *, int);
118 static void update_frame_line (struct frame *, int);
119 static struct dim allocate_matrices_for_frame_redisplay
120 (Lisp_Object, int, int, int, int *);
121 static int required_matrix_height (struct window *);
122 static int required_matrix_width (struct window *);
123 static void allocate_matrices_for_window_redisplay (struct window *);
124 static int realloc_glyph_pool (struct glyph_pool *, struct dim);
125 static void adjust_frame_glyphs (struct frame *);
126 struct glyph_matrix *new_glyph_matrix (struct glyph_pool *);
127 static void free_glyph_matrix (struct glyph_matrix *);
128 static void adjust_glyph_matrix (struct window *, struct glyph_matrix *,
129 int, int, struct dim);
130 static void change_frame_size_1 (struct frame *, int, int, int, int, int);
131 static void swap_glyph_pointers (struct glyph_row *, struct glyph_row *);
132 #if GLYPH_DEBUG
133 static int glyph_row_slice_p (struct glyph_row *, struct glyph_row *);
134 #endif
135 static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
136 static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
137 struct window *);
138 static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
139 struct window *);
140 static struct glyph_pool *new_glyph_pool (void);
141 static void free_glyph_pool (struct glyph_pool *);
142 static void adjust_frame_glyphs_initially (void);
143 static void adjust_frame_message_buffer (struct frame *);
144 static void adjust_decode_mode_spec_buffer (struct frame *);
145 static void fill_up_glyph_row_with_spaces (struct glyph_row *);
146 static void build_frame_matrix (struct frame *);
147 void clear_current_matrices (struct frame *);
148 void scroll_glyph_matrix_range (struct glyph_matrix *, int, int,
149 int, int);
150 static void clear_window_matrices (struct window *, int);
151 static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
152 static int scrolling_window (struct window *, int);
153 static int update_window_line (struct window *, int, int *);
154 static void update_marginal_area (struct window *, int, int);
155 static int update_text_area (struct window *, int);
156 static void make_current (struct glyph_matrix *, struct glyph_matrix *,
157 int);
158 static void mirror_make_current (struct window *, int);
159 void check_window_matrix_pointers (struct window *);
160 #if GLYPH_DEBUG
161 static void check_matrix_pointers (struct glyph_matrix *,
162 struct glyph_matrix *);
163 #endif
164 static void mirror_line_dance (struct window *, int, int, int *, char *);
165 static int update_window_tree (struct window *, int);
166 static int update_window (struct window *, int);
167 static int update_frame_1 (struct frame *, int, int);
168 static void set_window_cursor_after_update (struct window *);
169 static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
170 static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
173 /* Non-zero means don't pause redisplay for pending input. (This is
174 for debugging and for a future implementation of EDT-like
175 scrolling. */
177 int redisplay_dont_pause;
179 /* Define PERIODIC_PREEMPTION_CHECKING to 1, if micro-second timers
180 are supported, so we can check for input during redisplay at
181 regular intervals. */
182 #ifdef EMACS_HAS_USECS
183 #define PERIODIC_PREEMPTION_CHECKING 1
184 #else
185 #define PERIODIC_PREEMPTION_CHECKING 0
186 #endif
188 #if PERIODIC_PREEMPTION_CHECKING
190 /* If a number (float), check for user input every N seconds. */
192 Lisp_Object Vredisplay_preemption_period;
194 /* Redisplay preemption timers. */
196 static EMACS_TIME preemption_period;
197 static EMACS_TIME preemption_next_check;
199 #endif
201 /* Nonzero upon entry to redisplay means do not assume anything about
202 current contents of actual terminal frame; clear and redraw it. */
204 int frame_garbaged;
206 /* Nonzero means last display completed. Zero means it was preempted. */
208 int display_completed;
210 /* Lisp variable visible-bell; enables use of screen-flash instead of
211 audible bell. */
213 int visible_bell;
215 /* Invert the color of the whole frame, at a low level. */
217 int inverse_video;
219 /* Line speed of the terminal. */
221 EMACS_INT baud_rate;
223 /* Either nil or a symbol naming the window system under which Emacs
224 creates the first frame. */
226 Lisp_Object Vinitial_window_system;
228 /* Version number of X windows: 10, 11 or nil. */
230 Lisp_Object Vwindow_system_version;
232 /* Vector of glyph definitions. Indexed by glyph number, the contents
233 are a string which is how to output the glyph.
235 If Vglyph_table is nil, a glyph is output by using its low 8 bits
236 as a character code.
238 This is an obsolete feature that is no longer used. The variable
239 is retained for compatibility. */
241 Lisp_Object Vglyph_table;
243 /* Display table to use for vectors that don't specify their own. */
245 Lisp_Object Vstandard_display_table;
247 /* Nonzero means reading single-character input with prompt so put
248 cursor on mini-buffer after the prompt. Positive means at end of
249 text in echo area; negative means at beginning of line. */
251 int cursor_in_echo_area;
253 Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
256 /* The currently selected frame. In a single-frame version, this
257 variable always equals the_only_frame. */
259 Lisp_Object selected_frame;
261 /* A frame which is not just a mini-buffer, or 0 if there are no such
262 frames. This is usually the most recent such frame that was
263 selected. In a single-frame version, this variable always holds
264 the address of the_only_frame. */
266 struct frame *last_nonminibuf_frame;
268 /* 1 means SIGWINCH happened when not safe. */
270 int delayed_size_change;
272 /* 1 means glyph initialization has been completed at startup. */
274 static int glyphs_initialized_initially_p;
276 /* Updated window if != 0. Set by update_window. */
278 struct window *updated_window;
280 /* Glyph row updated in update_window_line, and area that is updated. */
282 struct glyph_row *updated_row;
283 int updated_area;
285 /* A glyph for a space. */
287 struct glyph space_glyph;
289 /* Counts of allocated structures. These counts serve to diagnose
290 memory leaks and double frees. */
292 int glyph_matrix_count;
293 int glyph_pool_count;
295 /* If non-null, the frame whose frame matrices are manipulated. If
296 null, window matrices are worked on. */
298 static struct frame *frame_matrix_frame;
300 /* Non-zero means that fonts have been loaded since the last glyph
301 matrix adjustments. Redisplay must stop, and glyph matrices must
302 be adjusted when this flag becomes non-zero during display. The
303 reason fonts can be loaded so late is that fonts of fontsets are
304 loaded on demand. Another reason is that a line contains many
305 characters displayed by zero width or very narrow glyphs of
306 variable-width fonts. */
308 int fonts_changed_p;
310 /* Convert vpos and hpos from frame to window and vice versa.
311 This may only be used for terminal frames. */
313 #if GLYPH_DEBUG
315 static int window_to_frame_vpos (struct window *, int);
316 static int window_to_frame_hpos (struct window *, int);
317 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
318 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
320 /* One element of the ring buffer containing redisplay history
321 information. */
323 struct redisplay_history
325 char trace[512 + 100];
328 /* The size of the history buffer. */
330 #define REDISPLAY_HISTORY_SIZE 30
332 /* The redisplay history buffer. */
334 static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
336 /* Next free entry in redisplay_history. */
338 static int history_idx;
340 /* A tick that's incremented each time something is added to the
341 history. */
343 static unsigned history_tick;
345 static void add_frame_display_history (struct frame *, int);
346 static void add_window_display_history (struct window *, char *, int);
348 /* Add to the redisplay history how window W has been displayed.
349 MSG is a trace containing the information how W's glyph matrix
350 has been constructed. PAUSED_P non-zero means that the update
351 has been interrupted for pending input. */
353 static void
354 add_window_display_history (struct window *w, char *msg, int paused_p)
356 char *buf;
358 if (history_idx >= REDISPLAY_HISTORY_SIZE)
359 history_idx = 0;
360 buf = redisplay_history[history_idx].trace;
361 ++history_idx;
363 sprintf (buf, "%d: window %p (`%s')%s\n",
364 history_tick++,
366 ((BUFFERP (w->buffer)
367 && STRINGP (XBUFFER (w->buffer)->name))
368 ? (char *) SDATA (XBUFFER (w->buffer)->name)
369 : "???"),
370 paused_p ? " ***paused***" : "");
371 strcat (buf, msg);
375 /* Add to the redisplay history that frame F has been displayed.
376 PAUSED_P non-zero means that the update has been interrupted for
377 pending input. */
379 static void
380 add_frame_display_history (struct frame *f, int paused_p)
382 char *buf;
384 if (history_idx >= REDISPLAY_HISTORY_SIZE)
385 history_idx = 0;
386 buf = redisplay_history[history_idx].trace;
387 ++history_idx;
389 sprintf (buf, "%d: update frame %p%s",
390 history_tick++,
391 f, paused_p ? " ***paused***" : "");
395 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
396 Sdump_redisplay_history, 0, 0, "",
397 doc: /* Dump redisplay history to stderr. */)
398 (void)
400 int i;
402 for (i = history_idx - 1; i != history_idx; --i)
404 if (i < 0)
405 i = REDISPLAY_HISTORY_SIZE - 1;
406 fprintf (stderr, "%s\n", redisplay_history[i].trace);
409 return Qnil;
413 #else /* GLYPH_DEBUG == 0 */
415 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
416 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
418 #endif /* GLYPH_DEBUG == 0 */
421 #if defined PROFILING && !HAVE___EXECUTABLE_START
422 /* FIXME: only used to find text start for profiling. */
424 void
425 safe_bcopy (const char *from, char *to, int size)
427 abort ();
429 #endif
431 /***********************************************************************
432 Glyph Matrices
433 ***********************************************************************/
435 /* Allocate and return a glyph_matrix structure. POOL is the glyph
436 pool from which memory for the matrix should be allocated, or null
437 for window-based redisplay where no glyph pools are used. The
438 member `pool' of the glyph matrix structure returned is set to
439 POOL, the structure is otherwise zeroed. */
441 struct glyph_matrix *
442 new_glyph_matrix (struct glyph_pool *pool)
444 struct glyph_matrix *result;
446 /* Allocate and clear. */
447 result = (struct glyph_matrix *) xmalloc (sizeof *result);
448 memset (result, 0, sizeof *result);
450 /* Increment number of allocated matrices. This count is used
451 to detect memory leaks. */
452 ++glyph_matrix_count;
454 /* Set pool and return. */
455 result->pool = pool;
456 return result;
460 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
462 The global counter glyph_matrix_count is decremented when a matrix
463 is freed. If the count gets negative, more structures were freed
464 than allocated, i.e. one matrix was freed more than once or a bogus
465 pointer was passed to this function.
467 If MATRIX->pool is null, this means that the matrix manages its own
468 glyph memory---this is done for matrices on X frames. Freeing the
469 matrix also frees the glyph memory in this case. */
471 static void
472 free_glyph_matrix (struct glyph_matrix *matrix)
474 if (matrix)
476 int i;
478 /* Detect the case that more matrices are freed than were
479 allocated. */
480 if (--glyph_matrix_count < 0)
481 abort ();
483 /* Free glyph memory if MATRIX owns it. */
484 if (matrix->pool == NULL)
485 for (i = 0; i < matrix->rows_allocated; ++i)
486 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
488 /* Free row structures and the matrix itself. */
489 xfree (matrix->rows);
490 xfree (matrix);
495 /* Return the number of glyphs to reserve for a marginal area of
496 window W. TOTAL_GLYPHS is the number of glyphs in a complete
497 display line of window W. MARGIN gives the width of the marginal
498 area in canonical character units. MARGIN should be an integer
499 or a float. */
501 static int
502 margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
504 int n;
506 if (NUMBERP (margin))
508 int width = XFASTINT (w->total_cols);
509 double d = max (0, XFLOATINT (margin));
510 d = min (width / 2 - 1, d);
511 n = (int) ((double) total_glyphs / width * d);
513 else
514 n = 0;
516 return n;
520 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
521 window sizes.
523 W is null if the function is called for a frame glyph matrix.
524 Otherwise it is the window MATRIX is a member of. X and Y are the
525 indices of the first column and row of MATRIX within the frame
526 matrix, if such a matrix exists. They are zero for purely
527 window-based redisplay. DIM is the needed size of the matrix.
529 In window-based redisplay, where no frame matrices exist, glyph
530 matrices manage their own glyph storage. Otherwise, they allocate
531 storage from a common frame glyph pool which can be found in
532 MATRIX->pool.
534 The reason for this memory management strategy is to avoid complete
535 frame redraws if possible. When we allocate from a common pool, a
536 change of the location or size of a sub-matrix within the pool
537 requires a complete redisplay of the frame because we cannot easily
538 make sure that the current matrices of all windows still agree with
539 what is displayed on the screen. While this is usually fast, it
540 leads to screen flickering. */
542 static void
543 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
545 int i;
546 int new_rows;
547 int marginal_areas_changed_p = 0;
548 int header_line_changed_p = 0;
549 int header_line_p = 0;
550 int left = -1, right = -1;
551 int window_width = -1, window_height = -1;
553 /* See if W had a header line that has disappeared now, or vice versa.
554 Get W's size. */
555 if (w)
557 window_box (w, -1, 0, 0, &window_width, &window_height);
559 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
560 header_line_changed_p = header_line_p != matrix->header_line_p;
562 matrix->header_line_p = header_line_p;
564 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
565 Do nothing if MATRIX' size, position, vscroll, and marginal areas
566 haven't changed. This optimization is important because preserving
567 the matrix means preventing redisplay. */
568 if (matrix->pool == NULL)
570 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
571 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
572 xassert (left >= 0 && right >= 0);
573 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
574 || right != matrix->right_margin_glyphs);
576 if (!marginal_areas_changed_p
577 && !fonts_changed_p
578 && !header_line_changed_p
579 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
580 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
581 && matrix->window_height == window_height
582 && matrix->window_vscroll == w->vscroll
583 && matrix->window_width == window_width)
584 return;
587 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
588 if (matrix->rows_allocated < dim.height)
590 int size = dim.height * sizeof (struct glyph_row);
591 new_rows = dim.height - matrix->rows_allocated;
592 matrix->rows = (struct glyph_row *) xrealloc (matrix->rows, size);
593 memset (matrix->rows + matrix->rows_allocated, 0,
594 new_rows * sizeof *matrix->rows);
595 matrix->rows_allocated = dim.height;
597 else
598 new_rows = 0;
600 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
601 on a frame not using window-based redisplay. Set up pointers for
602 each row into the glyph pool. */
603 if (matrix->pool)
605 xassert (matrix->pool->glyphs);
607 if (w)
609 left = margin_glyphs_to_reserve (w, dim.width,
610 w->left_margin_cols);
611 right = margin_glyphs_to_reserve (w, dim.width,
612 w->right_margin_cols);
614 else
615 left = right = 0;
617 for (i = 0; i < dim.height; ++i)
619 struct glyph_row *row = &matrix->rows[i];
621 row->glyphs[LEFT_MARGIN_AREA]
622 = (matrix->pool->glyphs
623 + (y + i) * matrix->pool->ncolumns
624 + x);
626 if (w == NULL
627 || row == matrix->rows + dim.height - 1
628 || (row == matrix->rows && matrix->header_line_p))
630 row->glyphs[TEXT_AREA]
631 = row->glyphs[LEFT_MARGIN_AREA];
632 row->glyphs[RIGHT_MARGIN_AREA]
633 = row->glyphs[TEXT_AREA] + dim.width;
634 row->glyphs[LAST_AREA]
635 = row->glyphs[RIGHT_MARGIN_AREA];
637 else
639 row->glyphs[TEXT_AREA]
640 = row->glyphs[LEFT_MARGIN_AREA] + left;
641 row->glyphs[RIGHT_MARGIN_AREA]
642 = row->glyphs[TEXT_AREA] + dim.width - left - right;
643 row->glyphs[LAST_AREA]
644 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
648 matrix->left_margin_glyphs = left;
649 matrix->right_margin_glyphs = right;
651 else
653 /* If MATRIX->pool is null, MATRIX is responsible for managing
654 its own memory. It is a window matrix for window-based redisplay.
655 Allocate glyph memory from the heap. */
656 if (dim.width > matrix->matrix_w
657 || new_rows
658 || header_line_changed_p
659 || marginal_areas_changed_p)
661 struct glyph_row *row = matrix->rows;
662 struct glyph_row *end = row + matrix->rows_allocated;
664 while (row < end)
666 row->glyphs[LEFT_MARGIN_AREA]
667 = (struct glyph *) xrealloc (row->glyphs[LEFT_MARGIN_AREA],
668 (dim.width
669 * sizeof (struct glyph)));
671 /* The mode line never has marginal areas. */
672 if (row == matrix->rows + dim.height - 1
673 || (row == matrix->rows && matrix->header_line_p))
675 row->glyphs[TEXT_AREA]
676 = row->glyphs[LEFT_MARGIN_AREA];
677 row->glyphs[RIGHT_MARGIN_AREA]
678 = row->glyphs[TEXT_AREA] + dim.width;
679 row->glyphs[LAST_AREA]
680 = row->glyphs[RIGHT_MARGIN_AREA];
682 else
684 row->glyphs[TEXT_AREA]
685 = row->glyphs[LEFT_MARGIN_AREA] + left;
686 row->glyphs[RIGHT_MARGIN_AREA]
687 = row->glyphs[TEXT_AREA] + dim.width - left - right;
688 row->glyphs[LAST_AREA]
689 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
691 ++row;
695 xassert (left >= 0 && right >= 0);
696 matrix->left_margin_glyphs = left;
697 matrix->right_margin_glyphs = right;
700 /* Number of rows to be used by MATRIX. */
701 matrix->nrows = dim.height;
702 xassert (matrix->nrows >= 0);
704 if (w)
706 if (matrix == w->current_matrix)
708 /* Mark rows in a current matrix of a window as not having
709 valid contents. It's important to not do this for
710 desired matrices. When Emacs starts, it may already be
711 building desired matrices when this function runs. */
712 if (window_width < 0)
713 window_width = window_box_width (w, -1);
715 /* Optimize the case that only the height has changed (C-x 2,
716 upper window). Invalidate all rows that are no longer part
717 of the window. */
718 if (!marginal_areas_changed_p
719 && !header_line_changed_p
720 && new_rows == 0
721 && dim.width == matrix->matrix_w
722 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
723 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
724 && matrix->window_width == window_width)
726 /* Find the last row in the window. */
727 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
728 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
730 ++i;
731 break;
734 /* Window end is invalid, if inside of the rows that
735 are invalidated below. */
736 if (INTEGERP (w->window_end_vpos)
737 && XFASTINT (w->window_end_vpos) >= i)
738 w->window_end_valid = Qnil;
740 while (i < matrix->nrows)
741 matrix->rows[i++].enabled_p = 0;
743 else
745 for (i = 0; i < matrix->nrows; ++i)
746 matrix->rows[i].enabled_p = 0;
749 else if (matrix == w->desired_matrix)
751 /* Rows in desired matrices always have to be cleared;
752 redisplay expects this is the case when it runs, so it
753 had better be the case when we adjust matrices between
754 redisplays. */
755 for (i = 0; i < matrix->nrows; ++i)
756 matrix->rows[i].enabled_p = 0;
761 /* Remember last values to be able to optimize frame redraws. */
762 matrix->matrix_x = x;
763 matrix->matrix_y = y;
764 matrix->matrix_w = dim.width;
765 matrix->matrix_h = dim.height;
767 /* Record the top y location and height of W at the time the matrix
768 was last adjusted. This is used to optimize redisplay above. */
769 if (w)
771 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
772 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
773 matrix->window_height = window_height;
774 matrix->window_width = window_width;
775 matrix->window_vscroll = w->vscroll;
780 /* Reverse the contents of rows in MATRIX between START and END. The
781 contents of the row at END - 1 end up at START, END - 2 at START +
782 1 etc. This is part of the implementation of rotate_matrix (see
783 below). */
785 static void
786 reverse_rows (struct glyph_matrix *matrix, int start, int end)
788 int i, j;
790 for (i = start, j = end - 1; i < j; ++i, --j)
792 /* Non-ISO HP/UX compiler doesn't like auto struct
793 initialization. */
794 struct glyph_row temp;
795 temp = matrix->rows[i];
796 matrix->rows[i] = matrix->rows[j];
797 matrix->rows[j] = temp;
802 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
803 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
804 indices. (Note: this does not copy glyphs, only glyph pointers in
805 row structures are moved around).
807 The algorithm used for rotating the vector was, I believe, first
808 described by Kernighan. See the vector R as consisting of two
809 sub-vectors AB, where A has length BY for BY >= 0. The result
810 after rotating is then BA. Reverse both sub-vectors to get ArBr
811 and reverse the result to get (ArBr)r which is BA. Similar for
812 rotating right. */
814 void
815 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
817 if (by < 0)
819 /* Up (rotate left, i.e. towards lower indices). */
820 by = -by;
821 reverse_rows (matrix, first, first + by);
822 reverse_rows (matrix, first + by, last);
823 reverse_rows (matrix, first, last);
825 else if (by > 0)
827 /* Down (rotate right, i.e. towards higher indices). */
828 reverse_rows (matrix, last - by, last);
829 reverse_rows (matrix, first, last - by);
830 reverse_rows (matrix, first, last);
835 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
836 with indices START <= index < END. Increment positions by DELTA/
837 DELTA_BYTES. */
839 void
840 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
841 EMACS_INT delta, EMACS_INT delta_bytes)
843 /* Check that START and END are reasonable values. */
844 xassert (start >= 0 && start <= matrix->nrows);
845 xassert (end >= 0 && end <= matrix->nrows);
846 xassert (start <= end);
848 for (; start < end; ++start)
849 increment_row_positions (matrix->rows + start, delta, delta_bytes);
853 /* Enable a range of rows in glyph matrix MATRIX. START and END are
854 the row indices of the first and last + 1 row to enable. If
855 ENABLED_P is non-zero, enabled_p flags in rows will be set to 1. */
857 void
858 enable_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end, int enabled_p)
860 xassert (start <= end);
861 xassert (start >= 0 && start < matrix->nrows);
862 xassert (end >= 0 && end <= matrix->nrows);
864 for (; start < end; ++start)
865 matrix->rows[start].enabled_p = enabled_p != 0;
869 /* Clear MATRIX.
871 This empties all rows in MATRIX by setting the enabled_p flag for
872 all rows of the matrix to zero. The function prepare_desired_row
873 will eventually really clear a row when it sees one with a zero
874 enabled_p flag.
876 Resets update hints to defaults value. The only update hint
877 currently present is the flag MATRIX->no_scrolling_p. */
879 void
880 clear_glyph_matrix (struct glyph_matrix *matrix)
882 if (matrix)
884 enable_glyph_matrix_rows (matrix, 0, matrix->nrows, 0);
885 matrix->no_scrolling_p = 0;
890 /* Shift part of the glyph matrix MATRIX of window W up or down.
891 Increment y-positions in glyph rows between START and END by DY,
892 and recompute their visible height. */
894 void
895 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
897 int min_y, max_y;
899 xassert (start <= end);
900 xassert (start >= 0 && start < matrix->nrows);
901 xassert (end >= 0 && end <= matrix->nrows);
903 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
904 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
906 for (; start < end; ++start)
908 struct glyph_row *row = &matrix->rows[start];
910 row->y += dy;
911 row->visible_height = row->height;
913 if (row->y < min_y)
914 row->visible_height -= min_y - row->y;
915 if (row->y + row->height > max_y)
916 row->visible_height -= row->y + row->height - max_y;
921 /* Mark all rows in current matrices of frame F as invalid. Marking
922 invalid is done by setting enabled_p to zero for all rows in a
923 current matrix. */
925 void
926 clear_current_matrices (register struct frame *f)
928 /* Clear frame current matrix, if we have one. */
929 if (f->current_matrix)
930 clear_glyph_matrix (f->current_matrix);
932 /* Clear the matrix of the menu bar window, if such a window exists.
933 The menu bar window is currently used to display menus on X when
934 no toolkit support is compiled in. */
935 if (WINDOWP (f->menu_bar_window))
936 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
938 /* Clear the matrix of the tool-bar window, if any. */
939 if (WINDOWP (f->tool_bar_window))
940 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
942 /* Clear current window matrices. */
943 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
944 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
948 /* Clear out all display lines of F for a coming redisplay. */
950 void
951 clear_desired_matrices (register struct frame *f)
953 if (f->desired_matrix)
954 clear_glyph_matrix (f->desired_matrix);
956 if (WINDOWP (f->menu_bar_window))
957 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
959 if (WINDOWP (f->tool_bar_window))
960 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
962 /* Do it for window matrices. */
963 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
964 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
968 /* Clear matrices in window tree rooted in W. If DESIRED_P is
969 non-zero clear desired matrices, otherwise clear current matrices. */
971 static void
972 clear_window_matrices (struct window *w, int desired_p)
974 while (w)
976 if (!NILP (w->hchild))
978 xassert (WINDOWP (w->hchild));
979 clear_window_matrices (XWINDOW (w->hchild), desired_p);
981 else if (!NILP (w->vchild))
983 xassert (WINDOWP (w->vchild));
984 clear_window_matrices (XWINDOW (w->vchild), desired_p);
986 else
988 if (desired_p)
989 clear_glyph_matrix (w->desired_matrix);
990 else
992 clear_glyph_matrix (w->current_matrix);
993 w->window_end_valid = Qnil;
997 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1003 /***********************************************************************
1004 Glyph Rows
1006 See dispextern.h for an overall explanation of glyph rows.
1007 ***********************************************************************/
1009 /* Clear glyph row ROW. Do it in a way that makes it robust against
1010 changes in the glyph_row structure, i.e. addition or removal of
1011 structure members. */
1013 static struct glyph_row null_row;
1015 void
1016 clear_glyph_row (struct glyph_row *row)
1018 struct glyph *p[1 + LAST_AREA];
1020 /* Save pointers. */
1021 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
1022 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
1023 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
1024 p[LAST_AREA] = row->glyphs[LAST_AREA];
1026 /* Clear. */
1027 *row = null_row;
1029 /* Restore pointers. */
1030 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
1031 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
1032 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
1033 row->glyphs[LAST_AREA] = p[LAST_AREA];
1035 #if 0 /* At some point, some bit-fields of struct glyph were not set,
1036 which made glyphs unequal when compared with GLYPH_EQUAL_P.
1037 Redisplay outputs such glyphs, and flickering effects were
1038 the result. This also depended on the contents of memory
1039 returned by xmalloc. If flickering happens again, activate
1040 the code below. If the flickering is gone with that, chances
1041 are that the flickering has the same reason as here. */
1042 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
1043 #endif
1047 /* Make ROW an empty, enabled row of canonical character height,
1048 in window W starting at y-position Y. */
1050 void
1051 blank_row (struct window *w, struct glyph_row *row, int y)
1053 int min_y, max_y;
1055 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
1056 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
1058 clear_glyph_row (row);
1059 row->y = y;
1060 row->ascent = row->phys_ascent = 0;
1061 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
1062 row->visible_height = row->height;
1064 if (row->y < min_y)
1065 row->visible_height -= min_y - row->y;
1066 if (row->y + row->height > max_y)
1067 row->visible_height -= row->y + row->height - max_y;
1069 row->enabled_p = 1;
1073 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
1074 are the amounts by which to change positions. Note that the first
1075 glyph of the text area of a row can have a buffer position even if
1076 the used count of the text area is zero. Such rows display line
1077 ends. */
1079 void
1080 increment_row_positions (struct glyph_row *row,
1081 EMACS_INT delta, EMACS_INT delta_bytes)
1083 int area, i;
1085 /* Increment start and end positions. */
1086 MATRIX_ROW_START_CHARPOS (row) += delta;
1087 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
1088 MATRIX_ROW_END_CHARPOS (row) += delta;
1089 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
1090 CHARPOS (row->start.pos) += delta;
1091 BYTEPOS (row->start.pos) += delta_bytes;
1092 CHARPOS (row->end.pos) += delta;
1093 BYTEPOS (row->end.pos) += delta_bytes;
1095 if (!row->enabled_p)
1096 return;
1098 /* Increment positions in glyphs. */
1099 for (area = 0; area < LAST_AREA; ++area)
1100 for (i = 0; i < row->used[area]; ++i)
1101 if (BUFFERP (row->glyphs[area][i].object)
1102 && row->glyphs[area][i].charpos > 0)
1103 row->glyphs[area][i].charpos += delta;
1105 /* Capture the case of rows displaying a line end. */
1106 if (row->used[TEXT_AREA] == 0
1107 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
1108 row->glyphs[TEXT_AREA]->charpos += delta;
1112 #if 0
1113 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
1114 contents, i.e. glyph structure contents are exchanged between A and
1115 B without changing glyph pointers in A and B. */
1117 static void
1118 swap_glyphs_in_rows (a, b)
1119 struct glyph_row *a, *b;
1121 int area;
1123 for (area = 0; area < LAST_AREA; ++area)
1125 /* Number of glyphs to swap. */
1126 int max_used = max (a->used[area], b->used[area]);
1128 /* Start of glyphs in area of row A. */
1129 struct glyph *glyph_a = a->glyphs[area];
1131 /* End + 1 of glyphs in area of row A. */
1132 struct glyph *glyph_a_end = a->glyphs[max_used];
1134 /* Start of glyphs in area of row B. */
1135 struct glyph *glyph_b = b->glyphs[area];
1137 while (glyph_a < glyph_a_end)
1139 /* Non-ISO HP/UX compiler doesn't like auto struct
1140 initialization. */
1141 struct glyph temp;
1142 temp = *glyph_a;
1143 *glyph_a = *glyph_b;
1144 *glyph_b = temp;
1145 ++glyph_a;
1146 ++glyph_b;
1151 #endif /* 0 */
1153 /* Exchange pointers to glyph memory between glyph rows A and B. */
1155 static INLINE void
1156 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1158 int i;
1159 for (i = 0; i < LAST_AREA + 1; ++i)
1161 struct glyph *temp = a->glyphs[i];
1162 a->glyphs[i] = b->glyphs[i];
1163 b->glyphs[i] = temp;
1168 /* Copy glyph row structure FROM to glyph row structure TO, except
1169 that glyph pointers in the structures are left unchanged. */
1171 static INLINE void
1172 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1174 struct glyph *pointers[1 + LAST_AREA];
1176 /* Save glyph pointers of TO. */
1177 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1179 /* Do a structure assignment. */
1180 *to = *from;
1182 /* Restore original pointers of TO. */
1183 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1187 /* Copy contents of glyph row FROM to glyph row TO. Glyph pointers in
1188 TO and FROM are left unchanged. Glyph contents are copied from the
1189 glyph memory of FROM to the glyph memory of TO. Increment buffer
1190 positions in row TO by DELTA/ DELTA_BYTES. */
1192 void
1193 copy_glyph_row_contents (struct glyph_row *to, struct glyph_row *from,
1194 EMACS_INT delta, EMACS_INT delta_bytes)
1196 int area;
1198 /* This is like a structure assignment TO = FROM, except that
1199 glyph pointers in the rows are left unchanged. */
1200 copy_row_except_pointers (to, from);
1202 /* Copy glyphs from FROM to TO. */
1203 for (area = 0; area < LAST_AREA; ++area)
1204 if (from->used[area])
1205 memcpy (to->glyphs[area], from->glyphs[area],
1206 from->used[area] * sizeof (struct glyph));
1208 /* Increment buffer positions in TO by DELTA. */
1209 increment_row_positions (to, delta, delta_bytes);
1213 /* Assign glyph row FROM to glyph row TO. This works like a structure
1214 assignment TO = FROM, except that glyph pointers are not copied but
1215 exchanged between TO and FROM. Pointers must be exchanged to avoid
1216 a memory leak. */
1218 static INLINE void
1219 assign_row (struct glyph_row *to, struct glyph_row *from)
1221 swap_glyph_pointers (to, from);
1222 copy_row_except_pointers (to, from);
1226 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1227 a row in a window matrix, is a slice of the glyph memory of the
1228 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1229 is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
1230 memory of FRAME_ROW. */
1232 #if GLYPH_DEBUG
1234 static int
1235 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1237 struct glyph *window_glyph_start = window_row->glyphs[0];
1238 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1239 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1241 return (frame_glyph_start <= window_glyph_start
1242 && window_glyph_start < frame_glyph_end);
1245 #endif /* GLYPH_DEBUG */
1247 #if 0
1249 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1250 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1251 in WINDOW_MATRIX is found satisfying the condition. */
1253 static struct glyph_row *
1254 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1255 struct glyph_matrix *frame_matrix, int row)
1257 int i;
1259 xassert (row >= 0 && row < frame_matrix->nrows);
1261 for (i = 0; i < window_matrix->nrows; ++i)
1262 if (glyph_row_slice_p (window_matrix->rows + i,
1263 frame_matrix->rows + row))
1264 break;
1266 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1269 #endif /* 0 */
1271 /* Prepare ROW for display. Desired rows are cleared lazily,
1272 i.e. they are only marked as to be cleared by setting their
1273 enabled_p flag to zero. When a row is to be displayed, a prior
1274 call to this function really clears it. */
1276 void
1277 prepare_desired_row (struct glyph_row *row)
1279 if (!row->enabled_p)
1281 unsigned rp = row->reversed_p;
1283 clear_glyph_row (row);
1284 row->enabled_p = 1;
1285 row->reversed_p = rp;
1290 /* Return a hash code for glyph row ROW. */
1292 static int
1293 line_hash_code (struct glyph_row *row)
1295 int hash = 0;
1297 if (row->enabled_p)
1299 struct glyph *glyph = row->glyphs[TEXT_AREA];
1300 struct glyph *end = glyph + row->used[TEXT_AREA];
1302 while (glyph < end)
1304 int c = glyph->u.ch;
1305 int face_id = glyph->face_id;
1306 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1307 c -= SPACEGLYPH;
1308 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1309 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1310 ++glyph;
1313 if (hash == 0)
1314 hash = 1;
1317 return hash;
1321 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1322 the number of characters in the line. If must_write_spaces is
1323 zero, leading and trailing spaces are ignored. */
1325 static unsigned int
1326 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1328 struct glyph_row *row = matrix->rows + vpos;
1329 struct glyph *beg = row->glyphs[TEXT_AREA];
1330 struct glyph *end = beg + row->used[TEXT_AREA];
1331 int len;
1332 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1333 int glyph_table_len = GLYPH_TABLE_LENGTH;
1335 /* Ignore trailing and leading spaces if we can. */
1336 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1338 /* Skip from the end over trailing spaces. */
1339 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1340 --end;
1342 /* All blank line. */
1343 if (end == beg)
1344 return 0;
1346 /* Skip over leading spaces. */
1347 while (CHAR_GLYPH_SPACE_P (*beg))
1348 ++beg;
1351 /* If we don't have a glyph-table, each glyph is one character,
1352 so return the number of glyphs. */
1353 if (glyph_table_base == 0)
1354 len = end - beg;
1355 else
1357 /* Otherwise, scan the glyphs and accumulate their total length
1358 in LEN. */
1359 len = 0;
1360 while (beg < end)
1362 GLYPH g;
1364 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1366 if (GLYPH_INVALID_P (g)
1367 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1368 len += 1;
1369 else
1370 len += GLYPH_LENGTH (glyph_table_base, g);
1372 ++beg;
1376 return len;
1380 /* Test two glyph rows A and B for equality. Value is non-zero if A
1381 and B have equal contents. W is the window to which the glyphs
1382 rows A and B belong. It is needed here to test for partial row
1383 visibility. MOUSE_FACE_P non-zero means compare the mouse_face_p
1384 flags of A and B, too. */
1386 static INLINE int
1387 row_equal_p (struct window *w, struct glyph_row *a, struct glyph_row *b, int mouse_face_p)
1389 if (a == b)
1390 return 1;
1391 else if (a->hash != b->hash)
1392 return 0;
1393 else
1395 struct glyph *a_glyph, *b_glyph, *a_end;
1396 int area;
1398 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1399 return 0;
1401 /* Compare glyphs. */
1402 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1404 if (a->used[area] != b->used[area])
1405 return 0;
1407 a_glyph = a->glyphs[area];
1408 a_end = a_glyph + a->used[area];
1409 b_glyph = b->glyphs[area];
1411 while (a_glyph < a_end
1412 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1413 ++a_glyph, ++b_glyph;
1415 if (a_glyph != a_end)
1416 return 0;
1419 if (a->fill_line_p != b->fill_line_p
1420 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1421 || a->left_fringe_bitmap != b->left_fringe_bitmap
1422 || a->left_fringe_face_id != b->left_fringe_face_id
1423 || a->right_fringe_bitmap != b->right_fringe_bitmap
1424 || a->right_fringe_face_id != b->right_fringe_face_id
1425 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1426 || a->exact_window_width_line_p != b->exact_window_width_line_p
1427 || a->overlapped_p != b->overlapped_p
1428 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1429 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1430 || a->reversed_p != b->reversed_p
1431 /* Different partially visible characters on left margin. */
1432 || a->x != b->x
1433 /* Different height. */
1434 || a->ascent != b->ascent
1435 || a->phys_ascent != b->phys_ascent
1436 || a->phys_height != b->phys_height
1437 || a->visible_height != b->visible_height)
1438 return 0;
1441 return 1;
1446 /***********************************************************************
1447 Glyph Pool
1449 See dispextern.h for an overall explanation of glyph pools.
1450 ***********************************************************************/
1452 /* Allocate a glyph_pool structure. The structure returned is
1453 initialized with zeros. The global variable glyph_pool_count is
1454 incremented for each pool allocated. */
1456 static struct glyph_pool *
1457 new_glyph_pool (void)
1459 struct glyph_pool *result;
1461 /* Allocate a new glyph_pool and clear it. */
1462 result = (struct glyph_pool *) xmalloc (sizeof *result);
1463 memset (result, 0, sizeof *result);
1465 /* For memory leak and double deletion checking. */
1466 ++glyph_pool_count;
1468 return result;
1472 /* Free a glyph_pool structure POOL. The function may be called with
1473 a null POOL pointer. The global variable glyph_pool_count is
1474 decremented with every pool structure freed. If this count gets
1475 negative, more structures were freed than allocated, i.e. one
1476 structure must have been freed more than once or a bogus pointer
1477 was passed to free_glyph_pool. */
1479 static void
1480 free_glyph_pool (struct glyph_pool *pool)
1482 if (pool)
1484 /* More freed than allocated? */
1485 --glyph_pool_count;
1486 xassert (glyph_pool_count >= 0);
1488 xfree (pool->glyphs);
1489 xfree (pool);
1494 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1495 columns we need. This function never shrinks a pool. The only
1496 case in which this would make sense, would be when a frame's size
1497 is changed from a large value to a smaller one. But, if someone
1498 does it once, we can expect that he will do it again.
1500 Value is non-zero if the pool changed in a way which makes
1501 re-adjusting window glyph matrices necessary. */
1503 static int
1504 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1506 int needed;
1507 int changed_p;
1509 changed_p = (pool->glyphs == 0
1510 || matrix_dim.height != pool->nrows
1511 || matrix_dim.width != pool->ncolumns);
1513 /* Enlarge the glyph pool. */
1514 needed = matrix_dim.width * matrix_dim.height;
1515 if (needed > pool->nglyphs)
1517 int size = needed * sizeof (struct glyph);
1519 if (pool->glyphs)
1521 pool->glyphs = (struct glyph *) xrealloc (pool->glyphs, size);
1522 memset (pool->glyphs + pool->nglyphs, 0,
1523 size - pool->nglyphs * sizeof (struct glyph));
1525 else
1527 pool->glyphs = (struct glyph *) xmalloc (size);
1528 memset (pool->glyphs, 0, size);
1531 pool->nglyphs = needed;
1534 /* Remember the number of rows and columns because (a) we use them
1535 to do sanity checks, and (b) the number of columns determines
1536 where rows in the frame matrix start---this must be available to
1537 determine pointers to rows of window sub-matrices. */
1538 pool->nrows = matrix_dim.height;
1539 pool->ncolumns = matrix_dim.width;
1541 return changed_p;
1546 /***********************************************************************
1547 Debug Code
1548 ***********************************************************************/
1550 #if GLYPH_DEBUG
1553 /* Flush standard output. This is sometimes useful to call from the debugger.
1554 XXX Maybe this should be changed to flush the current terminal instead of
1555 stdout.
1558 void
1559 flush_stdout (void)
1561 fflush (stdout);
1565 /* Check that no glyph pointers have been lost in MATRIX. If a
1566 pointer has been lost, e.g. by using a structure assignment between
1567 rows, at least one pointer must occur more than once in the rows of
1568 MATRIX. */
1570 void
1571 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1573 int i, j;
1575 for (i = 0; i < matrix->nrows; ++i)
1576 for (j = 0; j < matrix->nrows; ++j)
1577 xassert (i == j
1578 || (matrix->rows[i].glyphs[TEXT_AREA]
1579 != matrix->rows[j].glyphs[TEXT_AREA]));
1583 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1585 struct glyph_row *
1586 matrix_row (struct glyph_matrix *matrix, int row)
1588 xassert (matrix && matrix->rows);
1589 xassert (row >= 0 && row < matrix->nrows);
1591 /* That's really too slow for normal testing because this function
1592 is called almost everywhere. Although---it's still astonishingly
1593 fast, so it is valuable to have for debugging purposes. */
1594 #if 0
1595 check_matrix_pointer_lossage (matrix);
1596 #endif
1598 return matrix->rows + row;
1602 #if 0 /* This function makes invalid assumptions when text is
1603 partially invisible. But it might come handy for debugging
1604 nevertheless. */
1606 /* Check invariants that must hold for an up to date current matrix of
1607 window W. */
1609 static void
1610 check_matrix_invariants (struct window *w)
1612 struct glyph_matrix *matrix = w->current_matrix;
1613 int yb = window_text_bottom_y (w);
1614 struct glyph_row *row = matrix->rows;
1615 struct glyph_row *last_text_row = NULL;
1616 struct buffer *saved = current_buffer;
1617 struct buffer *buffer = XBUFFER (w->buffer);
1618 int c;
1620 /* This can sometimes happen for a fresh window. */
1621 if (matrix->nrows < 2)
1622 return;
1624 set_buffer_temp (buffer);
1626 /* Note: last row is always reserved for the mode line. */
1627 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1628 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1630 struct glyph_row *next = row + 1;
1632 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1633 last_text_row = row;
1635 /* Check that character and byte positions are in sync. */
1636 xassert (MATRIX_ROW_START_BYTEPOS (row)
1637 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1638 xassert (BYTEPOS (row->start.pos)
1639 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1641 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1642 have such a position temporarily in case of a minibuffer
1643 displaying something like `[Sole completion]' at its end. */
1644 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1646 xassert (MATRIX_ROW_END_BYTEPOS (row)
1647 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1648 xassert (BYTEPOS (row->end.pos)
1649 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1652 /* Check that end position of `row' is equal to start position
1653 of next row. */
1654 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1656 xassert (MATRIX_ROW_END_CHARPOS (row)
1657 == MATRIX_ROW_START_CHARPOS (next));
1658 xassert (MATRIX_ROW_END_BYTEPOS (row)
1659 == MATRIX_ROW_START_BYTEPOS (next));
1660 xassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1661 xassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1663 row = next;
1666 xassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1667 xassert (w->desired_matrix->rows != NULL);
1668 set_buffer_temp (saved);
1671 #endif /* 0 */
1673 #endif /* GLYPH_DEBUG != 0 */
1677 /**********************************************************************
1678 Allocating/ Adjusting Glyph Matrices
1679 **********************************************************************/
1681 /* Allocate glyph matrices over a window tree for a frame-based
1682 redisplay
1684 X and Y are column/row within the frame glyph matrix where
1685 sub-matrices for the window tree rooted at WINDOW must be
1686 allocated. DIM_ONLY_P non-zero means that the caller of this
1687 function is only interested in the result matrix dimension, and
1688 matrix adjustments should not be performed.
1690 The function returns the total width/height of the sub-matrices of
1691 the window tree. If called on a frame root window, the computation
1692 will take the mini-buffer window into account.
1694 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1696 NEW_LEAF_MATRIX set if any window in the tree did not have a
1697 glyph matrices yet, and
1699 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1700 any window in the tree will be changed or have been changed (see
1701 DIM_ONLY_P)
1703 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1704 function.
1706 Windows are arranged into chains of windows on the same level
1707 through the next fields of window structures. Such a level can be
1708 either a sequence of horizontally adjacent windows from left to
1709 right, or a sequence of vertically adjacent windows from top to
1710 bottom. Each window in a horizontal sequence can be either a leaf
1711 window or a vertical sequence; a window in a vertical sequence can
1712 be either a leaf or a horizontal sequence. All windows in a
1713 horizontal sequence have the same height, and all windows in a
1714 vertical sequence have the same width.
1716 This function uses, for historical reasons, a more general
1717 algorithm to determine glyph matrix dimensions that would be
1718 necessary.
1720 The matrix height of a horizontal sequence is determined by the
1721 maximum height of any matrix in the sequence. The matrix width of
1722 a horizontal sequence is computed by adding up matrix widths of
1723 windows in the sequence.
1725 |<------- result width ------->|
1726 +---------+----------+---------+ ---
1727 | | | | |
1728 | | | |
1729 +---------+ | | result height
1730 | +---------+
1731 | | |
1732 +----------+ ---
1734 The matrix width of a vertical sequence is the maximum matrix width
1735 of any window in the sequence. Its height is computed by adding up
1736 matrix heights of windows in the sequence.
1738 |<---- result width -->|
1739 +---------+ ---
1740 | | |
1741 | | |
1742 +---------+--+ |
1743 | | |
1744 | | result height
1746 +------------+---------+ |
1747 | | |
1748 | | |
1749 +------------+---------+ --- */
1751 /* Bit indicating that a new matrix will be allocated or has been
1752 allocated. */
1754 #define NEW_LEAF_MATRIX (1 << 0)
1756 /* Bit indicating that a matrix will or has changed its location or
1757 size. */
1759 #define CHANGED_LEAF_MATRIX (1 << 1)
1761 static struct dim
1762 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1763 int dim_only_p, int *window_change_flags)
1765 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1766 int x0 = x, y0 = y;
1767 int wmax = 0, hmax = 0;
1768 struct dim total;
1769 struct dim dim;
1770 struct window *w;
1771 int in_horz_combination_p;
1773 /* What combination is WINDOW part of? Compute this once since the
1774 result is the same for all windows in the `next' chain. The
1775 special case of a root window (parent equal to nil) is treated
1776 like a vertical combination because a root window's `next'
1777 points to the mini-buffer window, if any, which is arranged
1778 vertically below other windows. */
1779 in_horz_combination_p
1780 = (!NILP (XWINDOW (window)->parent)
1781 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1783 /* For WINDOW and all windows on the same level. */
1786 w = XWINDOW (window);
1788 /* Get the dimension of the window sub-matrix for W, depending
1789 on whether this is a combination or a leaf window. */
1790 if (!NILP (w->hchild))
1791 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
1792 dim_only_p,
1793 window_change_flags);
1794 else if (!NILP (w->vchild))
1795 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
1796 dim_only_p,
1797 window_change_flags);
1798 else
1800 /* If not already done, allocate sub-matrix structures. */
1801 if (w->desired_matrix == NULL)
1803 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1804 w->current_matrix = new_glyph_matrix (f->current_pool);
1805 *window_change_flags |= NEW_LEAF_MATRIX;
1808 /* Width and height MUST be chosen so that there are no
1809 holes in the frame matrix. */
1810 dim.width = required_matrix_width (w);
1811 dim.height = required_matrix_height (w);
1813 /* Will matrix be re-allocated? */
1814 if (x != w->desired_matrix->matrix_x
1815 || y != w->desired_matrix->matrix_y
1816 || dim.width != w->desired_matrix->matrix_w
1817 || dim.height != w->desired_matrix->matrix_h
1818 || (margin_glyphs_to_reserve (w, dim.width,
1819 w->left_margin_cols)
1820 != w->desired_matrix->left_margin_glyphs)
1821 || (margin_glyphs_to_reserve (w, dim.width,
1822 w->right_margin_cols)
1823 != w->desired_matrix->right_margin_glyphs))
1824 *window_change_flags |= CHANGED_LEAF_MATRIX;
1826 /* Actually change matrices, if allowed. Do not consider
1827 CHANGED_LEAF_MATRIX computed above here because the pool
1828 may have been changed which we don't now here. We trust
1829 that we only will be called with DIM_ONLY_P != 0 when
1830 necessary. */
1831 if (!dim_only_p)
1833 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1834 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1838 /* If we are part of a horizontal combination, advance x for
1839 windows to the right of W; otherwise advance y for windows
1840 below W. */
1841 if (in_horz_combination_p)
1842 x += dim.width;
1843 else
1844 y += dim.height;
1846 /* Remember maximum glyph matrix dimensions. */
1847 wmax = max (wmax, dim.width);
1848 hmax = max (hmax, dim.height);
1850 /* Next window on same level. */
1851 window = w->next;
1853 while (!NILP (window));
1855 /* Set `total' to the total glyph matrix dimension of this window
1856 level. In a vertical combination, the width is the width of the
1857 widest window; the height is the y we finally reached, corrected
1858 by the y we started with. In a horizontal combination, the total
1859 height is the height of the tallest window, and the width is the
1860 x we finally reached, corrected by the x we started with. */
1861 if (in_horz_combination_p)
1863 total.width = x - x0;
1864 total.height = hmax;
1866 else
1868 total.width = wmax;
1869 total.height = y - y0;
1872 return total;
1876 /* Return the required height of glyph matrices for window W. */
1878 static int
1879 required_matrix_height (struct window *w)
1881 #ifdef HAVE_WINDOW_SYSTEM
1882 struct frame *f = XFRAME (w->frame);
1884 if (FRAME_WINDOW_P (f))
1886 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1887 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1888 return (((window_pixel_height + ch_height - 1)
1889 / ch_height) * w->nrows_scale_factor
1890 /* One partially visible line at the top and
1891 bottom of the window. */
1893 /* 2 for header and mode line. */
1894 + 2);
1896 #endif /* HAVE_WINDOW_SYSTEM */
1898 return WINDOW_TOTAL_LINES (w);
1902 /* Return the required width of glyph matrices for window W. */
1904 static int
1905 required_matrix_width (struct window *w)
1907 #ifdef HAVE_WINDOW_SYSTEM
1908 struct frame *f = XFRAME (w->frame);
1909 if (FRAME_WINDOW_P (f))
1911 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1912 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1914 /* Compute number of glyphs needed in a glyph row. */
1915 return (((window_pixel_width + ch_width - 1)
1916 / ch_width) * w->ncols_scale_factor
1917 /* 2 partially visible columns in the text area. */
1919 /* One partially visible column at the right
1920 edge of each marginal area. */
1921 + 1 + 1);
1923 #endif /* HAVE_WINDOW_SYSTEM */
1925 return XINT (w->total_cols);
1929 /* Allocate window matrices for window-based redisplay. W is the
1930 window whose matrices must be allocated/reallocated. */
1932 static void
1933 allocate_matrices_for_window_redisplay (struct window *w)
1935 while (w)
1937 if (!NILP (w->vchild))
1938 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
1939 else if (!NILP (w->hchild))
1940 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
1941 else
1943 /* W is a leaf window. */
1944 struct dim dim;
1946 /* If matrices are not yet allocated, allocate them now. */
1947 if (w->desired_matrix == NULL)
1949 w->desired_matrix = new_glyph_matrix (NULL);
1950 w->current_matrix = new_glyph_matrix (NULL);
1953 dim.width = required_matrix_width (w);
1954 dim.height = required_matrix_height (w);
1955 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1956 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1959 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1964 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1965 do it for all frames; otherwise do it just for the given frame.
1966 This function must be called when a new frame is created, its size
1967 changes, or its window configuration changes. */
1969 void
1970 adjust_glyphs (struct frame *f)
1972 /* Block input so that expose events and other events that access
1973 glyph matrices are not processed while we are changing them. */
1974 BLOCK_INPUT;
1976 if (f)
1977 adjust_frame_glyphs (f);
1978 else
1980 Lisp_Object tail, lisp_frame;
1982 FOR_EACH_FRAME (tail, lisp_frame)
1983 adjust_frame_glyphs (XFRAME (lisp_frame));
1986 UNBLOCK_INPUT;
1990 /* Adjust frame glyphs when Emacs is initialized.
1992 To be called from init_display.
1994 We need a glyph matrix because redraw will happen soon.
1995 Unfortunately, window sizes on selected_frame are not yet set to
1996 meaningful values. I believe we can assume that there are only two
1997 windows on the frame---the mini-buffer and the root window. Frame
1998 height and width seem to be correct so far. So, set the sizes of
1999 windows to estimated values. */
2001 static void
2002 adjust_frame_glyphs_initially (void)
2004 struct frame *sf = SELECTED_FRAME ();
2005 struct window *root = XWINDOW (sf->root_window);
2006 struct window *mini = XWINDOW (root->next);
2007 int frame_lines = FRAME_LINES (sf);
2008 int frame_cols = FRAME_COLS (sf);
2009 int top_margin = FRAME_TOP_MARGIN (sf);
2011 /* Do it for the root window. */
2012 XSETFASTINT (root->top_line, top_margin);
2013 XSETFASTINT (root->total_cols, frame_cols);
2014 set_window_height (sf->root_window, frame_lines - 1 - top_margin, 0);
2016 /* Do it for the mini-buffer window. */
2017 XSETFASTINT (mini->top_line, frame_lines - 1);
2018 XSETFASTINT (mini->total_cols, frame_cols);
2019 set_window_height (root->next, 1, 0);
2021 adjust_frame_glyphs (sf);
2022 glyphs_initialized_initially_p = 1;
2026 /* Allocate/reallocate glyph matrices of a single frame F. */
2028 static void
2029 adjust_frame_glyphs (struct frame *f)
2031 if (FRAME_WINDOW_P (f))
2032 adjust_frame_glyphs_for_window_redisplay (f);
2033 else
2034 adjust_frame_glyphs_for_frame_redisplay (f);
2036 /* Don't forget the message buffer and the buffer for
2037 decode_mode_spec. */
2038 adjust_frame_message_buffer (f);
2039 adjust_decode_mode_spec_buffer (f);
2041 f->glyphs_initialized_p = 1;
2044 /* Return 1 if any window in the tree has nonzero window margins. See
2045 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
2046 static int
2047 showing_window_margins_p (struct window *w)
2049 while (w)
2051 if (!NILP (w->hchild))
2053 if (showing_window_margins_p (XWINDOW (w->hchild)))
2054 return 1;
2056 else if (!NILP (w->vchild))
2058 if (showing_window_margins_p (XWINDOW (w->vchild)))
2059 return 1;
2061 else if (!NILP (w->left_margin_cols)
2062 || !NILP (w->right_margin_cols))
2063 return 1;
2065 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2067 return 0;
2071 /* In the window tree with root W, build current matrices of leaf
2072 windows from the frame's current matrix. */
2074 static void
2075 fake_current_matrices (Lisp_Object window)
2077 struct window *w;
2079 for (; !NILP (window); window = w->next)
2081 w = XWINDOW (window);
2083 if (!NILP (w->hchild))
2084 fake_current_matrices (w->hchild);
2085 else if (!NILP (w->vchild))
2086 fake_current_matrices (w->vchild);
2087 else
2089 int i;
2090 struct frame *f = XFRAME (w->frame);
2091 struct glyph_matrix *m = w->current_matrix;
2092 struct glyph_matrix *fm = f->current_matrix;
2094 xassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
2095 xassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
2097 for (i = 0; i < m->matrix_h; ++i)
2099 struct glyph_row *r = m->rows + i;
2100 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
2102 xassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
2103 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
2105 r->enabled_p = fr->enabled_p;
2106 if (r->enabled_p)
2108 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
2109 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
2110 r->used[TEXT_AREA] = (m->matrix_w
2111 - r->used[LEFT_MARGIN_AREA]
2112 - r->used[RIGHT_MARGIN_AREA]);
2113 r->mode_line_p = 0;
2121 /* Save away the contents of frame F's current frame matrix. Value is
2122 a glyph matrix holding the contents of F's current frame matrix. */
2124 static struct glyph_matrix *
2125 save_current_matrix (struct frame *f)
2127 int i;
2128 struct glyph_matrix *saved;
2130 saved = (struct glyph_matrix *) xmalloc (sizeof *saved);
2131 memset (saved, 0, sizeof *saved);
2132 saved->nrows = f->current_matrix->nrows;
2133 saved->rows = (struct glyph_row *) xmalloc (saved->nrows
2134 * sizeof *saved->rows);
2135 memset (saved->rows, 0, saved->nrows * sizeof *saved->rows);
2137 for (i = 0; i < saved->nrows; ++i)
2139 struct glyph_row *from = f->current_matrix->rows + i;
2140 struct glyph_row *to = saved->rows + i;
2141 size_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2142 to->glyphs[TEXT_AREA] = (struct glyph *) xmalloc (nbytes);
2143 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2144 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2147 return saved;
2151 /* Restore the contents of frame F's current frame matrix from SAVED,
2152 and free memory associated with SAVED. */
2154 static void
2155 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
2157 int i;
2159 for (i = 0; i < saved->nrows; ++i)
2161 struct glyph_row *from = saved->rows + i;
2162 struct glyph_row *to = f->current_matrix->rows + i;
2163 size_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2164 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2165 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2166 xfree (from->glyphs[TEXT_AREA]);
2169 xfree (saved->rows);
2170 xfree (saved);
2175 /* Allocate/reallocate glyph matrices of a single frame F for
2176 frame-based redisplay. */
2178 static void
2179 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
2181 struct dim matrix_dim;
2182 int pool_changed_p;
2183 int window_change_flags;
2184 int top_window_y;
2186 if (!FRAME_LIVE_P (f))
2187 return;
2189 top_window_y = FRAME_TOP_MARGIN (f);
2191 /* Allocate glyph pool structures if not already done. */
2192 if (f->desired_pool == NULL)
2194 f->desired_pool = new_glyph_pool ();
2195 f->current_pool = new_glyph_pool ();
2198 /* Allocate frames matrix structures if needed. */
2199 if (f->desired_matrix == NULL)
2201 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2202 f->current_matrix = new_glyph_matrix (f->current_pool);
2205 /* Compute window glyph matrices. (This takes the mini-buffer
2206 window into account). The result is the size of the frame glyph
2207 matrix needed. The variable window_change_flags is set to a bit
2208 mask indicating whether new matrices will be allocated or
2209 existing matrices change their size or location within the frame
2210 matrix. */
2211 window_change_flags = 0;
2212 matrix_dim
2213 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2214 0, top_window_y,
2216 &window_change_flags);
2218 /* Add in menu bar lines, if any. */
2219 matrix_dim.height += top_window_y;
2221 /* Enlarge pools as necessary. */
2222 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2223 realloc_glyph_pool (f->current_pool, matrix_dim);
2225 /* Set up glyph pointers within window matrices. Do this only if
2226 absolutely necessary since it requires a frame redraw. */
2227 if (pool_changed_p || window_change_flags)
2229 /* Do it for window matrices. */
2230 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2231 0, top_window_y, 0,
2232 &window_change_flags);
2234 /* Size of frame matrices must equal size of frame. Note
2235 that we are called for X frames with window widths NOT equal
2236 to the frame width (from CHANGE_FRAME_SIZE_1). */
2237 xassert (matrix_dim.width == FRAME_COLS (f)
2238 && matrix_dim.height == FRAME_LINES (f));
2240 /* Pointers to glyph memory in glyph rows are exchanged during
2241 the update phase of redisplay, which means in general that a
2242 frame's current matrix consists of pointers into both the
2243 desired and current glyph pool of the frame. Adjusting a
2244 matrix sets the frame matrix up so that pointers are all into
2245 the same pool. If we want to preserve glyph contents of the
2246 current matrix over a call to adjust_glyph_matrix, we must
2247 make a copy of the current glyphs, and restore the current
2248 matrix' contents from that copy. */
2249 if (display_completed
2250 && !FRAME_GARBAGED_P (f)
2251 && matrix_dim.width == f->current_matrix->matrix_w
2252 && matrix_dim.height == f->current_matrix->matrix_h
2253 /* For some reason, the frame glyph matrix gets corrupted if
2254 any of the windows contain margins. I haven't been able
2255 to hunt down the reason, but for the moment this prevents
2256 the problem from manifesting. -- cyd */
2257 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2259 struct glyph_matrix *copy = save_current_matrix (f);
2260 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2261 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2262 restore_current_matrix (f, copy);
2263 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2265 else
2267 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2268 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2269 SET_FRAME_GARBAGED (f);
2275 /* Allocate/reallocate glyph matrices of a single frame F for
2276 window-based redisplay. */
2278 static void
2279 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2281 struct window *w;
2283 xassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2285 /* Allocate/reallocate window matrices. */
2286 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2288 #ifdef HAVE_X_WINDOWS
2289 /* Allocate/ reallocate matrices of the dummy window used to display
2290 the menu bar under X when no X toolkit support is available. */
2291 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2293 /* Allocate a dummy window if not already done. */
2294 if (NILP (f->menu_bar_window))
2296 f->menu_bar_window = make_window ();
2297 w = XWINDOW (f->menu_bar_window);
2298 XSETFRAME (w->frame, f);
2299 w->pseudo_window_p = 1;
2301 else
2302 w = XWINDOW (f->menu_bar_window);
2304 /* Set window dimensions to frame dimensions and allocate or
2305 adjust glyph matrices of W. */
2306 XSETFASTINT (w->top_line, 0);
2307 XSETFASTINT (w->left_col, 0);
2308 XSETFASTINT (w->total_lines, FRAME_MENU_BAR_LINES (f));
2309 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2310 allocate_matrices_for_window_redisplay (w);
2312 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2313 #endif /* HAVE_X_WINDOWS */
2315 #ifndef USE_GTK
2316 /* Allocate/ reallocate matrices of the tool bar window. If we
2317 don't have a tool bar window yet, make one. */
2318 if (NILP (f->tool_bar_window))
2320 f->tool_bar_window = make_window ();
2321 w = XWINDOW (f->tool_bar_window);
2322 XSETFRAME (w->frame, f);
2323 w->pseudo_window_p = 1;
2325 else
2326 w = XWINDOW (f->tool_bar_window);
2328 XSETFASTINT (w->top_line, FRAME_MENU_BAR_LINES (f));
2329 XSETFASTINT (w->left_col, 0);
2330 XSETFASTINT (w->total_lines, FRAME_TOOL_BAR_LINES (f));
2331 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2332 allocate_matrices_for_window_redisplay (w);
2333 #endif
2337 /* Adjust/ allocate message buffer of frame F.
2339 Note that the message buffer is never freed. Since I could not
2340 find a free in 19.34, I assume that freeing it would be
2341 problematic in some way and don't do it either.
2343 (Implementation note: It should be checked if we can free it
2344 eventually without causing trouble). */
2346 static void
2347 adjust_frame_message_buffer (struct frame *f)
2349 int size = FRAME_MESSAGE_BUF_SIZE (f) + 1;
2351 if (FRAME_MESSAGE_BUF (f))
2353 char *buffer = FRAME_MESSAGE_BUF (f);
2354 char *new_buffer = (char *) xrealloc (buffer, size);
2355 FRAME_MESSAGE_BUF (f) = new_buffer;
2357 else
2358 FRAME_MESSAGE_BUF (f) = (char *) xmalloc (size);
2362 /* Re-allocate buffer for decode_mode_spec on frame F. */
2364 static void
2365 adjust_decode_mode_spec_buffer (struct frame *f)
2367 f->decode_mode_spec_buffer
2368 = (char *) xrealloc (f->decode_mode_spec_buffer,
2369 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2374 /**********************************************************************
2375 Freeing Glyph Matrices
2376 **********************************************************************/
2378 /* Free glyph memory for a frame F. F may be null. This function can
2379 be called for the same frame more than once. The root window of
2380 F may be nil when this function is called. This is the case when
2381 the function is called when F is destroyed. */
2383 void
2384 free_glyphs (struct frame *f)
2386 if (f && f->glyphs_initialized_p)
2388 /* Block interrupt input so that we don't get surprised by an X
2389 event while we're in an inconsistent state. */
2390 BLOCK_INPUT;
2391 f->glyphs_initialized_p = 0;
2393 /* Release window sub-matrices. */
2394 if (!NILP (f->root_window))
2395 free_window_matrices (XWINDOW (f->root_window));
2397 /* Free the dummy window for menu bars without X toolkit and its
2398 glyph matrices. */
2399 if (!NILP (f->menu_bar_window))
2401 struct window *w = XWINDOW (f->menu_bar_window);
2402 free_glyph_matrix (w->desired_matrix);
2403 free_glyph_matrix (w->current_matrix);
2404 w->desired_matrix = w->current_matrix = NULL;
2405 f->menu_bar_window = Qnil;
2408 /* Free the tool bar window and its glyph matrices. */
2409 if (!NILP (f->tool_bar_window))
2411 struct window *w = XWINDOW (f->tool_bar_window);
2412 free_glyph_matrix (w->desired_matrix);
2413 free_glyph_matrix (w->current_matrix);
2414 w->desired_matrix = w->current_matrix = NULL;
2415 f->tool_bar_window = Qnil;
2418 /* Release frame glyph matrices. Reset fields to zero in
2419 case we are called a second time. */
2420 if (f->desired_matrix)
2422 free_glyph_matrix (f->desired_matrix);
2423 free_glyph_matrix (f->current_matrix);
2424 f->desired_matrix = f->current_matrix = NULL;
2427 /* Release glyph pools. */
2428 if (f->desired_pool)
2430 free_glyph_pool (f->desired_pool);
2431 free_glyph_pool (f->current_pool);
2432 f->desired_pool = f->current_pool = NULL;
2435 UNBLOCK_INPUT;
2440 /* Free glyph sub-matrices in the window tree rooted at W. This
2441 function may be called with a null pointer, and it may be called on
2442 the same tree more than once. */
2444 void
2445 free_window_matrices (struct window *w)
2447 while (w)
2449 if (!NILP (w->hchild))
2450 free_window_matrices (XWINDOW (w->hchild));
2451 else if (!NILP (w->vchild))
2452 free_window_matrices (XWINDOW (w->vchild));
2453 else
2455 /* This is a leaf window. Free its memory and reset fields
2456 to zero in case this function is called a second time for
2457 W. */
2458 free_glyph_matrix (w->current_matrix);
2459 free_glyph_matrix (w->desired_matrix);
2460 w->current_matrix = w->desired_matrix = NULL;
2463 /* Next window on same level. */
2464 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2469 /* Check glyph memory leaks. This function is called from
2470 shut_down_emacs. Note that frames are not destroyed when Emacs
2471 exits. We therefore free all glyph memory for all active frames
2472 explicitly and check that nothing is left allocated. */
2474 void
2475 check_glyph_memory (void)
2477 Lisp_Object tail, frame;
2479 /* Free glyph memory for all frames. */
2480 FOR_EACH_FRAME (tail, frame)
2481 free_glyphs (XFRAME (frame));
2483 /* Check that nothing is left allocated. */
2484 if (glyph_matrix_count)
2485 abort ();
2486 if (glyph_pool_count)
2487 abort ();
2492 /**********************************************************************
2493 Building a Frame Matrix
2494 **********************************************************************/
2496 /* Most of the redisplay code works on glyph matrices attached to
2497 windows. This is a good solution most of the time, but it is not
2498 suitable for terminal code. Terminal output functions cannot rely
2499 on being able to set an arbitrary terminal window. Instead they
2500 must be provided with a view of the whole frame, i.e. the whole
2501 screen. We build such a view by constructing a frame matrix from
2502 window matrices in this section.
2504 Windows that must be updated have their must_be_update_p flag set.
2505 For all such windows, their desired matrix is made part of the
2506 desired frame matrix. For other windows, their current matrix is
2507 made part of the desired frame matrix.
2509 +-----------------+----------------+
2510 | desired | desired |
2511 | | |
2512 +-----------------+----------------+
2513 | current |
2515 +----------------------------------+
2517 Desired window matrices can be made part of the frame matrix in a
2518 cheap way: We exploit the fact that the desired frame matrix and
2519 desired window matrices share their glyph memory. This is not
2520 possible for current window matrices. Their glyphs are copied to
2521 the desired frame matrix. The latter is equivalent to
2522 preserve_other_columns in the old redisplay.
2524 Used glyphs counters for frame matrix rows are the result of adding
2525 up glyph lengths of the window matrices. A line in the frame
2526 matrix is enabled, if a corresponding line in a window matrix is
2527 enabled.
2529 After building the desired frame matrix, it will be passed to
2530 terminal code, which will manipulate both the desired and current
2531 frame matrix. Changes applied to the frame's current matrix have
2532 to be visible in current window matrices afterwards, of course.
2534 This problem is solved like this:
2536 1. Window and frame matrices share glyphs. Window matrices are
2537 constructed in a way that their glyph contents ARE the glyph
2538 contents needed in a frame matrix. Thus, any modification of
2539 glyphs done in terminal code will be reflected in window matrices
2540 automatically.
2542 2. Exchanges of rows in a frame matrix done by terminal code are
2543 intercepted by hook functions so that corresponding row operations
2544 on window matrices can be performed. This is necessary because we
2545 use pointers to glyphs in glyph row structures. To satisfy the
2546 assumption of point 1 above that glyphs are updated implicitly in
2547 window matrices when they are manipulated via the frame matrix,
2548 window and frame matrix must of course agree where to find the
2549 glyphs for their rows. Possible manipulations that must be
2550 mirrored are assignments of rows of the desired frame matrix to the
2551 current frame matrix and scrolling the current frame matrix. */
2553 /* Build frame F's desired matrix from window matrices. Only windows
2554 which have the flag must_be_updated_p set have to be updated. Menu
2555 bar lines of a frame are not covered by window matrices, so make
2556 sure not to touch them in this function. */
2558 static void
2559 build_frame_matrix (struct frame *f)
2561 int i;
2563 /* F must have a frame matrix when this function is called. */
2564 xassert (!FRAME_WINDOW_P (f));
2566 /* Clear all rows in the frame matrix covered by window matrices.
2567 Menu bar lines are not covered by windows. */
2568 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2569 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2571 /* Build the matrix by walking the window tree. */
2572 build_frame_matrix_from_window_tree (f->desired_matrix,
2573 XWINDOW (FRAME_ROOT_WINDOW (f)));
2577 /* Walk a window tree, building a frame matrix MATRIX from window
2578 matrices. W is the root of a window tree. */
2580 static void
2581 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2583 while (w)
2585 if (!NILP (w->hchild))
2586 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2587 else if (!NILP (w->vchild))
2588 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2589 else
2590 build_frame_matrix_from_leaf_window (matrix, w);
2592 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2597 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2598 desired frame matrix built. W is a leaf window whose desired or
2599 current matrix is to be added to FRAME_MATRIX. W's flag
2600 must_be_updated_p determines which matrix it contributes to
2601 FRAME_MATRIX. If must_be_updated_p is non-zero, W's desired matrix
2602 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2603 Adding a desired matrix means setting up used counters and such in
2604 frame rows, while adding a current window matrix to FRAME_MATRIX
2605 means copying glyphs. The latter case corresponds to
2606 preserve_other_columns in the old redisplay. */
2608 static void
2609 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2611 struct glyph_matrix *window_matrix;
2612 int window_y, frame_y;
2613 /* If non-zero, a glyph to insert at the right border of W. */
2614 GLYPH right_border_glyph;
2616 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2618 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2619 if (w->must_be_updated_p)
2621 window_matrix = w->desired_matrix;
2623 /* Decide whether we want to add a vertical border glyph. */
2624 if (!WINDOW_RIGHTMOST_P (w))
2626 struct Lisp_Char_Table *dp = window_display_table (w);
2627 Lisp_Object gc;
2629 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2630 if (dp
2631 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc))
2632 && GLYPH_CODE_CHAR_VALID_P (gc))
2634 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2635 spec_glyph_lookup_face (w, &right_border_glyph);
2638 if (GLYPH_FACE (right_border_glyph) <= 0)
2639 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2642 else
2643 window_matrix = w->current_matrix;
2645 /* For all rows in the window matrix and corresponding rows in the
2646 frame matrix. */
2647 window_y = 0;
2648 frame_y = window_matrix->matrix_y;
2649 while (window_y < window_matrix->nrows)
2651 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2652 struct glyph_row *window_row = window_matrix->rows + window_y;
2653 int current_row_p = window_matrix == w->current_matrix;
2655 /* Fill up the frame row with spaces up to the left margin of the
2656 window row. */
2657 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2659 /* Fill up areas in the window matrix row with spaces. */
2660 fill_up_glyph_row_with_spaces (window_row);
2662 /* If only part of W's desired matrix has been built, and
2663 window_row wasn't displayed, use the corresponding current
2664 row instead. */
2665 if (window_matrix == w->desired_matrix
2666 && !window_row->enabled_p)
2668 window_row = w->current_matrix->rows + window_y;
2669 current_row_p = 1;
2672 if (current_row_p)
2674 /* Copy window row to frame row. */
2675 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2676 window_row->glyphs[0],
2677 window_matrix->matrix_w * sizeof (struct glyph));
2679 else
2681 xassert (window_row->enabled_p);
2683 /* Only when a desired row has been displayed, we want
2684 the corresponding frame row to be updated. */
2685 frame_row->enabled_p = 1;
2687 /* Maybe insert a vertical border between horizontally adjacent
2688 windows. */
2689 if (GLYPH_CHAR (right_border_glyph) != 0)
2691 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2692 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2695 #if GLYPH_DEBUG
2696 /* Window row window_y must be a slice of frame row
2697 frame_y. */
2698 xassert (glyph_row_slice_p (window_row, frame_row));
2700 /* If rows are in sync, we don't have to copy glyphs because
2701 frame and window share glyphs. */
2703 strcpy (w->current_matrix->method, w->desired_matrix->method);
2704 add_window_display_history (w, w->current_matrix->method, 0);
2705 #endif
2708 /* Set number of used glyphs in the frame matrix. Since we fill
2709 up with spaces, and visit leaf windows from left to right it
2710 can be done simply. */
2711 frame_row->used[TEXT_AREA]
2712 = window_matrix->matrix_x + window_matrix->matrix_w;
2714 /* Next row. */
2715 ++window_y;
2716 ++frame_y;
2720 /* Given a user-specified glyph, possibly including a Lisp-level face
2721 ID, return a glyph that has a realized face ID.
2722 This is used for glyphs displayed specially and not part of the text;
2723 for instance, vertical separators, truncation markers, etc. */
2725 void
2726 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2728 int lface_id = GLYPH_FACE (*glyph);
2729 /* Convert the glyph's specified face to a realized (cache) face. */
2730 if (lface_id > 0)
2732 int face_id = merge_faces (XFRAME (w->frame),
2733 Qt, lface_id, DEFAULT_FACE_ID);
2734 SET_GLYPH_FACE (*glyph, face_id);
2738 /* Add spaces to a glyph row ROW in a window matrix.
2740 Each row has the form:
2742 +---------+-----------------------------+------------+
2743 | left | text | right |
2744 +---------+-----------------------------+------------+
2746 Left and right marginal areas are optional. This function adds
2747 spaces to areas so that there are no empty holes between areas.
2748 In other words: If the right area is not empty, the text area
2749 is filled up with spaces up to the right area. If the text area
2750 is not empty, the left area is filled up.
2752 To be called for frame-based redisplay, only. */
2754 static void
2755 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2757 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2758 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2759 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2763 /* Fill area AREA of glyph row ROW with spaces. To be called for
2764 frame-based redisplay only. */
2766 static void
2767 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2769 if (row->glyphs[area] < row->glyphs[area + 1])
2771 struct glyph *end = row->glyphs[area + 1];
2772 struct glyph *text = row->glyphs[area] + row->used[area];
2774 while (text < end)
2775 *text++ = space_glyph;
2776 row->used[area] = text - row->glyphs[area];
2781 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2782 reached. In frame matrices only one area, TEXT_AREA, is used. */
2784 static void
2785 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2787 int i = row->used[TEXT_AREA];
2788 struct glyph *glyph = row->glyphs[TEXT_AREA];
2790 while (i < upto)
2791 glyph[i++] = space_glyph;
2793 row->used[TEXT_AREA] = i;
2798 /**********************************************************************
2799 Mirroring operations on frame matrices in window matrices
2800 **********************************************************************/
2802 /* Set frame being updated via frame-based redisplay to F. This
2803 function must be called before updates to make explicit that we are
2804 working on frame matrices or not. */
2806 static INLINE void
2807 set_frame_matrix_frame (struct frame *f)
2809 frame_matrix_frame = f;
2813 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2814 DESIRED_MATRIX is the desired matrix corresponding to
2815 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2816 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2817 frame_matrix_frame is non-null, this indicates that the exchange is
2818 done in frame matrices, and that we have to perform analogous
2819 operations in window matrices of frame_matrix_frame. */
2821 static INLINE void
2822 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2824 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2825 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2826 int mouse_face_p = current_row->mouse_face_p;
2828 /* Do current_row = desired_row. This exchanges glyph pointers
2829 between both rows, and does a structure assignment otherwise. */
2830 assign_row (current_row, desired_row);
2832 /* Enable current_row to mark it as valid. */
2833 current_row->enabled_p = 1;
2834 current_row->mouse_face_p = mouse_face_p;
2836 /* If we are called on frame matrices, perform analogous operations
2837 for window matrices. */
2838 if (frame_matrix_frame)
2839 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2843 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2844 W's frame which has been made current (by swapping pointers between
2845 current and desired matrix). Perform analogous operations in the
2846 matrices of leaf windows in the window tree rooted at W. */
2848 static void
2849 mirror_make_current (struct window *w, int frame_row)
2851 while (w)
2853 if (!NILP (w->hchild))
2854 mirror_make_current (XWINDOW (w->hchild), frame_row);
2855 else if (!NILP (w->vchild))
2856 mirror_make_current (XWINDOW (w->vchild), frame_row);
2857 else
2859 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2860 here because the checks performed in debug mode there
2861 will not allow the conversion. */
2862 int row = frame_row - w->desired_matrix->matrix_y;
2864 /* If FRAME_ROW is within W, assign the desired row to the
2865 current row (exchanging glyph pointers). */
2866 if (row >= 0 && row < w->desired_matrix->matrix_h)
2868 struct glyph_row *current_row
2869 = MATRIX_ROW (w->current_matrix, row);
2870 struct glyph_row *desired_row
2871 = MATRIX_ROW (w->desired_matrix, row);
2873 if (desired_row->enabled_p)
2874 assign_row (current_row, desired_row);
2875 else
2876 swap_glyph_pointers (desired_row, current_row);
2877 current_row->enabled_p = 1;
2879 /* Set the Y coordinate of the mode/header line's row.
2880 It is needed in draw_row_with_mouse_face to find the
2881 screen coordinates. (Window-based redisplay sets
2882 this in update_window, but no one seems to do that
2883 for frame-based redisplay.) */
2884 if (current_row->mode_line_p)
2885 current_row->y = row;
2889 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2894 /* Perform row dance after scrolling. We are working on the range of
2895 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2896 including) in MATRIX. COPY_FROM is a vector containing, for each
2897 row I in the range 0 <= I < NLINES, the index of the original line
2898 to move to I. This index is relative to the row range, i.e. 0 <=
2899 index < NLINES. RETAINED_P is a vector containing zero for each
2900 row 0 <= I < NLINES which is empty.
2902 This function is called from do_scrolling and do_direct_scrolling. */
2904 void
2905 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2906 int *copy_from, char *retained_p)
2908 /* A copy of original rows. */
2909 struct glyph_row *old_rows;
2911 /* Rows to assign to. */
2912 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2914 int i;
2916 /* Make a copy of the original rows. */
2917 old_rows = (struct glyph_row *) alloca (nlines * sizeof *old_rows);
2918 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2920 /* Assign new rows, maybe clear lines. */
2921 for (i = 0; i < nlines; ++i)
2923 int enabled_before_p = new_rows[i].enabled_p;
2925 xassert (i + unchanged_at_top < matrix->nrows);
2926 xassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2927 new_rows[i] = old_rows[copy_from[i]];
2928 new_rows[i].enabled_p = enabled_before_p;
2930 /* RETAINED_P is zero for empty lines. */
2931 if (!retained_p[copy_from[i]])
2932 new_rows[i].enabled_p = 0;
2935 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2936 if (frame_matrix_frame)
2937 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2938 unchanged_at_top, nlines, copy_from, retained_p);
2942 /* Synchronize glyph pointers in the current matrix of window W with
2943 the current frame matrix. */
2945 static void
2946 sync_window_with_frame_matrix_rows (struct window *w)
2948 struct frame *f = XFRAME (w->frame);
2949 struct glyph_row *window_row, *window_row_end, *frame_row;
2950 int left, right, x, width;
2952 /* Preconditions: W must be a leaf window on a tty frame. */
2953 xassert (NILP (w->hchild) && NILP (w->vchild));
2954 xassert (!FRAME_WINDOW_P (f));
2956 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2957 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2958 x = w->current_matrix->matrix_x;
2959 width = w->current_matrix->matrix_w;
2961 window_row = w->current_matrix->rows;
2962 window_row_end = window_row + w->current_matrix->nrows;
2963 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2965 for (; window_row < window_row_end; ++window_row, ++frame_row)
2967 window_row->glyphs[LEFT_MARGIN_AREA]
2968 = frame_row->glyphs[0] + x;
2969 window_row->glyphs[TEXT_AREA]
2970 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2971 window_row->glyphs[LAST_AREA]
2972 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2973 window_row->glyphs[RIGHT_MARGIN_AREA]
2974 = window_row->glyphs[LAST_AREA] - right;
2979 /* Return the window in the window tree rooted in W containing frame
2980 row ROW. Value is null if none is found. */
2982 struct window *
2983 frame_row_to_window (struct window *w, int row)
2985 struct window *found = NULL;
2987 while (w && !found)
2989 if (!NILP (w->hchild))
2990 found = frame_row_to_window (XWINDOW (w->hchild), row);
2991 else if (!NILP (w->vchild))
2992 found = frame_row_to_window (XWINDOW (w->vchild), row);
2993 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2994 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2995 found = w;
2997 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3000 return found;
3004 /* Perform a line dance in the window tree rooted at W, after
3005 scrolling a frame matrix in mirrored_line_dance.
3007 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
3008 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
3009 COPY_FROM is a vector containing, for each row I in the range 0 <=
3010 I < NLINES, the index of the original line to move to I. This
3011 index is relative to the row range, i.e. 0 <= index < NLINES.
3012 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
3013 which is empty. */
3015 static void
3016 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
3018 while (w)
3020 if (!NILP (w->hchild))
3021 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
3022 nlines, copy_from, retained_p);
3023 else if (!NILP (w->vchild))
3024 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
3025 nlines, copy_from, retained_p);
3026 else
3028 /* W is a leaf window, and we are working on its current
3029 matrix m. */
3030 struct glyph_matrix *m = w->current_matrix;
3031 int i, sync_p = 0;
3032 struct glyph_row *old_rows;
3034 /* Make a copy of the original rows of matrix m. */
3035 old_rows = (struct glyph_row *) alloca (m->nrows * sizeof *old_rows);
3036 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
3038 for (i = 0; i < nlines; ++i)
3040 /* Frame relative line assigned to. */
3041 int frame_to = i + unchanged_at_top;
3043 /* Frame relative line assigned. */
3044 int frame_from = copy_from[i] + unchanged_at_top;
3046 /* Window relative line assigned to. */
3047 int window_to = frame_to - m->matrix_y;
3049 /* Window relative line assigned. */
3050 int window_from = frame_from - m->matrix_y;
3052 /* Is assigned line inside window? */
3053 int from_inside_window_p
3054 = window_from >= 0 && window_from < m->matrix_h;
3056 /* Is assigned to line inside window? */
3057 int to_inside_window_p
3058 = window_to >= 0 && window_to < m->matrix_h;
3060 if (from_inside_window_p && to_inside_window_p)
3062 /* Enabled setting before assignment. */
3063 int enabled_before_p;
3065 /* Do the assignment. The enabled_p flag is saved
3066 over the assignment because the old redisplay did
3067 that. */
3068 enabled_before_p = m->rows[window_to].enabled_p;
3069 m->rows[window_to] = old_rows[window_from];
3070 m->rows[window_to].enabled_p = enabled_before_p;
3072 /* If frame line is empty, window line is empty, too. */
3073 if (!retained_p[copy_from[i]])
3074 m->rows[window_to].enabled_p = 0;
3076 else if (to_inside_window_p)
3078 /* A copy between windows. This is an infrequent
3079 case not worth optimizing. */
3080 struct frame *f = XFRAME (w->frame);
3081 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
3082 struct window *w2;
3083 struct glyph_matrix *m2;
3084 int m2_from;
3086 w2 = frame_row_to_window (root, frame_from);
3087 /* ttn@surf.glug.org: when enabling menu bar using `emacs
3088 -nw', FROM_FRAME sometimes has no associated window.
3089 This check avoids a segfault if W2 is null. */
3090 if (w2)
3092 m2 = w2->current_matrix;
3093 m2_from = frame_from - m2->matrix_y;
3094 copy_row_except_pointers (m->rows + window_to,
3095 m2->rows + m2_from);
3097 /* If frame line is empty, window line is empty, too. */
3098 if (!retained_p[copy_from[i]])
3099 m->rows[window_to].enabled_p = 0;
3101 sync_p = 1;
3103 else if (from_inside_window_p)
3104 sync_p = 1;
3107 /* If there was a copy between windows, make sure glyph
3108 pointers are in sync with the frame matrix. */
3109 if (sync_p)
3110 sync_window_with_frame_matrix_rows (w);
3112 /* Check that no pointers are lost. */
3113 CHECK_MATRIX (m);
3116 /* Next window on same level. */
3117 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3122 #if GLYPH_DEBUG
3124 /* Check that window and frame matrices agree about their
3125 understanding where glyphs of the rows are to find. For each
3126 window in the window tree rooted at W, check that rows in the
3127 matrices of leaf window agree with their frame matrices about
3128 glyph pointers. */
3130 void
3131 check_window_matrix_pointers (struct window *w)
3133 while (w)
3135 if (!NILP (w->hchild))
3136 check_window_matrix_pointers (XWINDOW (w->hchild));
3137 else if (!NILP (w->vchild))
3138 check_window_matrix_pointers (XWINDOW (w->vchild));
3139 else
3141 struct frame *f = XFRAME (w->frame);
3142 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
3143 check_matrix_pointers (w->current_matrix, f->current_matrix);
3146 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3151 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
3152 a window and FRAME_MATRIX is the corresponding frame matrix. For
3153 each row in WINDOW_MATRIX check that it's a slice of the
3154 corresponding frame row. If it isn't, abort. */
3156 static void
3157 check_matrix_pointers (struct glyph_matrix *window_matrix,
3158 struct glyph_matrix *frame_matrix)
3160 /* Row number in WINDOW_MATRIX. */
3161 int i = 0;
3163 /* Row number corresponding to I in FRAME_MATRIX. */
3164 int j = window_matrix->matrix_y;
3166 /* For all rows check that the row in the window matrix is a
3167 slice of the row in the frame matrix. If it isn't we didn't
3168 mirror an operation on the frame matrix correctly. */
3169 while (i < window_matrix->nrows)
3171 if (!glyph_row_slice_p (window_matrix->rows + i,
3172 frame_matrix->rows + j))
3173 abort ();
3174 ++i, ++j;
3178 #endif /* GLYPH_DEBUG != 0 */
3182 /**********************************************************************
3183 VPOS and HPOS translations
3184 **********************************************************************/
3186 #if GLYPH_DEBUG
3188 /* Translate vertical position VPOS which is relative to window W to a
3189 vertical position relative to W's frame. */
3191 static int
3192 window_to_frame_vpos (struct window *w, int vpos)
3194 struct frame *f = XFRAME (w->frame);
3196 xassert (!FRAME_WINDOW_P (f));
3197 xassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
3198 vpos += WINDOW_TOP_EDGE_LINE (w);
3199 xassert (vpos >= 0 && vpos <= FRAME_LINES (f));
3200 return vpos;
3204 /* Translate horizontal position HPOS which is relative to window W to
3205 a horizontal position relative to W's frame. */
3207 static int
3208 window_to_frame_hpos (struct window *w, int hpos)
3210 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3211 hpos += WINDOW_LEFT_EDGE_COL (w);
3212 return hpos;
3215 #endif /* GLYPH_DEBUG */
3219 /**********************************************************************
3220 Redrawing Frames
3221 **********************************************************************/
3223 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
3224 doc: /* Clear frame FRAME and output again what is supposed to appear on it. */)
3225 (Lisp_Object frame)
3227 struct frame *f;
3229 CHECK_LIVE_FRAME (frame);
3230 f = XFRAME (frame);
3232 /* Ignore redraw requests, if frame has no glyphs yet.
3233 (Implementation note: It still has to be checked why we are
3234 called so early here). */
3235 if (!glyphs_initialized_initially_p)
3236 return Qnil;
3238 update_begin (f);
3239 #ifdef MSDOS
3240 if (FRAME_MSDOS_P (f))
3241 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3242 #endif
3243 clear_frame (f);
3244 clear_current_matrices (f);
3245 update_end (f);
3246 if (FRAME_TERMCAP_P (f))
3247 fflush (FRAME_TTY (f)->output);
3248 windows_or_buffers_changed++;
3249 /* Mark all windows as inaccurate, so that every window will have
3250 its redisplay done. */
3251 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3252 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3253 f->garbaged = 0;
3254 return Qnil;
3258 /* Redraw frame F. This is nothing more than a call to the Lisp
3259 function redraw-frame. */
3261 void
3262 redraw_frame (struct frame *f)
3264 Lisp_Object frame;
3265 XSETFRAME (frame, f);
3266 Fredraw_frame (frame);
3270 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3271 doc: /* Clear and redisplay all visible frames. */)
3272 (void)
3274 Lisp_Object tail, frame;
3276 FOR_EACH_FRAME (tail, frame)
3277 if (FRAME_VISIBLE_P (XFRAME (frame)))
3278 Fredraw_frame (frame);
3280 return Qnil;
3284 /* This is used when frame_garbaged is set. Call Fredraw_frame on all
3285 visible frames marked as garbaged. */
3287 void
3288 redraw_garbaged_frames (void)
3290 Lisp_Object tail, frame;
3292 FOR_EACH_FRAME (tail, frame)
3293 if (FRAME_VISIBLE_P (XFRAME (frame))
3294 && FRAME_GARBAGED_P (XFRAME (frame)))
3295 Fredraw_frame (frame);
3300 /***********************************************************************
3301 Frame Update
3302 ***********************************************************************/
3304 /* Update frame F based on the data in desired matrices.
3306 If FORCE_P is non-zero, don't let redisplay be stopped by detecting
3307 pending input. If INHIBIT_HAIRY_ID_P is non-zero, don't try
3308 scrolling.
3310 Value is non-zero if redisplay was stopped due to pending input. */
3313 update_frame (struct frame *f, int force_p, int inhibit_hairy_id_p)
3315 /* 1 means display has been paused because of pending input. */
3316 int paused_p;
3317 struct window *root_window = XWINDOW (f->root_window);
3319 if (redisplay_dont_pause)
3320 force_p = 1;
3321 #if PERIODIC_PREEMPTION_CHECKING
3322 else if (NILP (Vredisplay_preemption_period))
3323 force_p = 1;
3324 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3326 EMACS_TIME tm;
3327 double p = XFLOATINT (Vredisplay_preemption_period);
3328 int sec, usec;
3330 if (detect_input_pending_ignore_squeezables ())
3332 paused_p = 1;
3333 goto do_pause;
3336 sec = (int) p;
3337 usec = (p - sec) * 1000000;
3339 EMACS_GET_TIME (tm);
3340 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3341 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3343 #endif
3345 if (FRAME_WINDOW_P (f))
3347 /* We are working on window matrix basis. All windows whose
3348 flag must_be_updated_p is set have to be updated. */
3350 /* Record that we are not working on frame matrices. */
3351 set_frame_matrix_frame (NULL);
3353 /* Update all windows in the window tree of F, maybe stopping
3354 when pending input is detected. */
3355 update_begin (f);
3357 /* Update the menu bar on X frames that don't have toolkit
3358 support. */
3359 if (WINDOWP (f->menu_bar_window))
3360 update_window (XWINDOW (f->menu_bar_window), 1);
3362 /* Update the tool-bar window, if present. */
3363 if (WINDOWP (f->tool_bar_window))
3365 struct window *w = XWINDOW (f->tool_bar_window);
3367 /* Update tool-bar window. */
3368 if (w->must_be_updated_p)
3370 Lisp_Object tem;
3372 update_window (w, 1);
3373 w->must_be_updated_p = 0;
3375 /* Swap tool-bar strings. We swap because we want to
3376 reuse strings. */
3377 tem = f->current_tool_bar_string;
3378 f->current_tool_bar_string = f->desired_tool_bar_string;
3379 f->desired_tool_bar_string = tem;
3384 /* Update windows. */
3385 paused_p = update_window_tree (root_window, force_p);
3386 update_end (f);
3388 /* This flush is a performance bottleneck under X,
3389 and it doesn't seem to be necessary anyway (in general).
3390 It is necessary when resizing the window with the mouse, or
3391 at least the fringes are not redrawn in a timely manner. ++kfs */
3392 if (f->force_flush_display_p)
3394 FRAME_RIF (f)->flush_display (f);
3395 f->force_flush_display_p = 0;
3398 else
3400 /* We are working on frame matrix basis. Set the frame on whose
3401 frame matrix we operate. */
3402 set_frame_matrix_frame (f);
3404 /* Build F's desired matrix from window matrices. */
3405 build_frame_matrix (f);
3407 /* Update the display */
3408 update_begin (f);
3409 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3410 update_end (f);
3412 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3414 if (FRAME_TTY (f)->termscript)
3415 fflush (FRAME_TTY (f)->termscript);
3416 if (FRAME_TERMCAP_P (f))
3417 fflush (FRAME_TTY (f)->output);
3420 /* Check window matrices for lost pointers. */
3421 #if GLYPH_DEBUG
3422 check_window_matrix_pointers (root_window);
3423 add_frame_display_history (f, paused_p);
3424 #endif
3427 #if PERIODIC_PREEMPTION_CHECKING
3428 do_pause:
3429 #endif
3430 /* Reset flags indicating that a window should be updated. */
3431 set_window_update_flags (root_window, 0);
3433 display_completed = !paused_p;
3434 return paused_p;
3439 /************************************************************************
3440 Window-based updates
3441 ************************************************************************/
3443 /* Perform updates in window tree rooted at W. FORCE_P non-zero means
3444 don't stop updating when input is pending. */
3446 static int
3447 update_window_tree (struct window *w, int force_p)
3449 int paused_p = 0;
3451 while (w && !paused_p)
3453 if (!NILP (w->hchild))
3454 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3455 else if (!NILP (w->vchild))
3456 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3457 else if (w->must_be_updated_p)
3458 paused_p |= update_window (w, force_p);
3460 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3463 return paused_p;
3467 /* Update window W if its flag must_be_updated_p is non-zero. If
3468 FORCE_P is non-zero, don't stop updating if input is pending. */
3470 void
3471 update_single_window (struct window *w, int force_p)
3473 if (w->must_be_updated_p)
3475 struct frame *f = XFRAME (WINDOW_FRAME (w));
3477 /* Record that this is not a frame-based redisplay. */
3478 set_frame_matrix_frame (NULL);
3480 if (redisplay_dont_pause)
3481 force_p = 1;
3482 #if PERIODIC_PREEMPTION_CHECKING
3483 else if (NILP (Vredisplay_preemption_period))
3484 force_p = 1;
3485 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3487 EMACS_TIME tm;
3488 double p = XFLOATINT (Vredisplay_preemption_period);
3489 int sec, usec;
3491 sec = (int) p;
3492 usec = (p - sec) * 1000000;
3494 EMACS_GET_TIME (tm);
3495 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3496 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3498 #endif
3500 /* Update W. */
3501 update_begin (f);
3502 update_window (w, force_p);
3503 update_end (f);
3505 /* Reset flag in W. */
3506 w->must_be_updated_p = 0;
3510 #ifdef HAVE_WINDOW_SYSTEM
3512 /* Redraw lines from the current matrix of window W that are
3513 overlapped by other rows. YB is bottom-most y-position in W. */
3515 static void
3516 redraw_overlapped_rows (struct window *w, int yb)
3518 int i;
3519 struct frame *f = XFRAME (WINDOW_FRAME (w));
3521 /* If rows overlapping others have been changed, the rows being
3522 overlapped have to be redrawn. This won't draw lines that have
3523 already been drawn in update_window_line because overlapped_p in
3524 desired rows is 0, so after row assignment overlapped_p in
3525 current rows is 0. */
3526 for (i = 0; i < w->current_matrix->nrows; ++i)
3528 struct glyph_row *row = w->current_matrix->rows + i;
3530 if (!row->enabled_p)
3531 break;
3532 else if (row->mode_line_p)
3533 continue;
3535 if (row->overlapped_p)
3537 enum glyph_row_area area;
3539 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3541 updated_row = row;
3542 updated_area = area;
3543 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3544 area == TEXT_AREA ? row->x : 0);
3545 if (row->used[area])
3546 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3547 row->used[area]);
3548 FRAME_RIF (f)->clear_end_of_line (-1);
3551 row->overlapped_p = 0;
3554 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3555 break;
3560 /* Redraw lines from the current matrix of window W that overlap
3561 others. YB is bottom-most y-position in W. */
3563 static void
3564 redraw_overlapping_rows (struct window *w, int yb)
3566 int i, bottom_y;
3567 struct glyph_row *row;
3568 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3570 for (i = 0; i < w->current_matrix->nrows; ++i)
3572 row = w->current_matrix->rows + i;
3574 if (!row->enabled_p)
3575 break;
3576 else if (row->mode_line_p)
3577 continue;
3579 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3581 if (row->overlapping_p)
3583 int overlaps = 0;
3585 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3586 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3587 overlaps |= OVERLAPS_PRED;
3588 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3589 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3590 overlaps |= OVERLAPS_SUCC;
3592 if (overlaps)
3594 if (row->used[LEFT_MARGIN_AREA])
3595 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3597 if (row->used[TEXT_AREA])
3598 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3600 if (row->used[RIGHT_MARGIN_AREA])
3601 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3603 /* Record in neighbour rows that ROW overwrites part of
3604 their display. */
3605 if (overlaps & OVERLAPS_PRED)
3606 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3607 if (overlaps & OVERLAPS_SUCC)
3608 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3612 if (bottom_y >= yb)
3613 break;
3617 #endif /* HAVE_WINDOW_SYSTEM */
3620 #ifdef GLYPH_DEBUG
3622 /* Check that no row in the current matrix of window W is enabled
3623 which is below what's displayed in the window. */
3625 void
3626 check_current_matrix_flags (struct window *w)
3628 int last_seen_p = 0;
3629 int i, yb = window_text_bottom_y (w);
3631 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3633 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3634 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3635 last_seen_p = 1;
3636 else if (last_seen_p && row->enabled_p)
3637 abort ();
3641 #endif /* GLYPH_DEBUG */
3644 /* Update display of window W. FORCE_P non-zero means that we should
3645 not stop when detecting pending input. */
3647 static int
3648 update_window (struct window *w, int force_p)
3650 struct glyph_matrix *desired_matrix = w->desired_matrix;
3651 int paused_p;
3652 #if !PERIODIC_PREEMPTION_CHECKING
3653 int preempt_count = baud_rate / 2400 + 1;
3654 #endif
3655 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3656 #if GLYPH_DEBUG
3657 /* Check that W's frame doesn't have glyph matrices. */
3658 xassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3659 #endif
3661 /* Check pending input the first time so that we can quickly return. */
3662 #if !PERIODIC_PREEMPTION_CHECKING
3663 if (!force_p)
3664 detect_input_pending_ignore_squeezables ();
3665 #endif
3667 /* If forced to complete the update, or if no input is pending, do
3668 the update. */
3669 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3671 struct glyph_row *row, *end;
3672 struct glyph_row *mode_line_row;
3673 struct glyph_row *header_line_row;
3674 int yb, changed_p = 0, mouse_face_overwritten_p = 0, n_updated;
3676 rif->update_window_begin_hook (w);
3677 yb = window_text_bottom_y (w);
3679 /* If window has a header line, update it before everything else.
3680 Adjust y-positions of other rows by the header line height. */
3681 row = desired_matrix->rows;
3682 end = row + desired_matrix->nrows - 1;
3684 if (row->mode_line_p)
3686 header_line_row = row;
3687 ++row;
3689 else
3690 header_line_row = NULL;
3692 /* Update the mode line, if necessary. */
3693 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3694 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3696 mode_line_row->y = yb;
3697 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3698 desired_matrix),
3699 &mouse_face_overwritten_p);
3702 /* Find first enabled row. Optimizations in redisplay_internal
3703 may lead to an update with only one row enabled. There may
3704 be also completely empty matrices. */
3705 while (row < end && !row->enabled_p)
3706 ++row;
3708 /* Try reusing part of the display by copying. */
3709 if (row < end && !desired_matrix->no_scrolling_p)
3711 int rc = scrolling_window (w, header_line_row != NULL);
3712 if (rc < 0)
3714 /* All rows were found to be equal. */
3715 paused_p = 0;
3716 goto set_cursor;
3718 else if (rc > 0)
3720 /* We've scrolled the display. */
3721 force_p = 1;
3722 changed_p = 1;
3726 /* Update the rest of the lines. */
3727 for (n_updated = 0; row < end && (force_p || !input_pending); ++row)
3728 if (row->enabled_p)
3730 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3731 int i;
3733 /* We'll have to play a little bit with when to
3734 detect_input_pending. If it's done too often,
3735 scrolling large windows with repeated scroll-up
3736 commands will too quickly pause redisplay. */
3737 #if PERIODIC_PREEMPTION_CHECKING
3738 if (!force_p)
3740 EMACS_TIME tm, dif;
3741 EMACS_GET_TIME (tm);
3742 EMACS_SUB_TIME (dif, preemption_next_check, tm);
3743 if (EMACS_TIME_NEG_P (dif))
3745 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3746 if (detect_input_pending_ignore_squeezables ())
3747 break;
3750 #else
3751 if (!force_p && ++n_updated % preempt_count == 0)
3752 detect_input_pending_ignore_squeezables ();
3753 #endif
3754 changed_p |= update_window_line (w, vpos,
3755 &mouse_face_overwritten_p);
3757 /* Mark all rows below the last visible one in the current
3758 matrix as invalid. This is necessary because of
3759 variable line heights. Consider the case of three
3760 successive redisplays, where the first displays 5
3761 lines, the second 3 lines, and the third 5 lines again.
3762 If the second redisplay wouldn't mark rows in the
3763 current matrix invalid, the third redisplay might be
3764 tempted to optimize redisplay based on lines displayed
3765 in the first redisplay. */
3766 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3767 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3768 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3771 /* Was display preempted? */
3772 paused_p = row < end;
3774 set_cursor:
3776 /* Update the header line after scrolling because a new header
3777 line would otherwise overwrite lines at the top of the window
3778 that can be scrolled. */
3779 if (header_line_row && header_line_row->enabled_p)
3781 header_line_row->y = 0;
3782 update_window_line (w, 0, &mouse_face_overwritten_p);
3785 /* Fix the appearance of overlapping/overlapped rows. */
3786 if (!paused_p && !w->pseudo_window_p)
3788 #ifdef HAVE_WINDOW_SYSTEM
3789 if (changed_p && rif->fix_overlapping_area)
3791 redraw_overlapped_rows (w, yb);
3792 redraw_overlapping_rows (w, yb);
3794 #endif
3796 /* Make cursor visible at cursor position of W. */
3797 set_window_cursor_after_update (w);
3799 #if 0 /* Check that current matrix invariants are satisfied. This is
3800 for debugging only. See the comment of check_matrix_invariants. */
3801 IF_DEBUG (check_matrix_invariants (w));
3802 #endif
3805 #if GLYPH_DEBUG
3806 /* Remember the redisplay method used to display the matrix. */
3807 strcpy (w->current_matrix->method, w->desired_matrix->method);
3808 #endif
3810 #ifdef HAVE_WINDOW_SYSTEM
3811 update_window_fringes (w, 0);
3812 #endif
3814 /* End the update of window W. Don't set the cursor if we
3815 paused updating the display because in this case,
3816 set_window_cursor_after_update hasn't been called, and
3817 output_cursor doesn't contain the cursor location. */
3818 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3820 else
3821 paused_p = 1;
3823 #if GLYPH_DEBUG
3824 /* check_current_matrix_flags (w); */
3825 add_window_display_history (w, w->current_matrix->method, paused_p);
3826 #endif
3828 clear_glyph_matrix (desired_matrix);
3830 return paused_p;
3834 /* Update the display of area AREA in window W, row number VPOS.
3835 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3837 static void
3838 update_marginal_area (struct window *w, int area, int vpos)
3840 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3841 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3843 /* Let functions in xterm.c know what area subsequent X positions
3844 will be relative to. */
3845 updated_area = area;
3847 /* Set cursor to start of glyphs, write them, and clear to the end
3848 of the area. I don't think that something more sophisticated is
3849 necessary here, since marginal areas will not be the default. */
3850 rif->cursor_to (vpos, 0, desired_row->y, 0);
3851 if (desired_row->used[area])
3852 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3853 rif->clear_end_of_line (-1);
3857 /* Update the display of the text area of row VPOS in window W.
3858 Value is non-zero if display has changed. */
3860 static int
3861 update_text_area (struct window *w, int vpos)
3863 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3864 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3865 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3866 int changed_p = 0;
3868 /* Let functions in xterm.c know what area subsequent X positions
3869 will be relative to. */
3870 updated_area = TEXT_AREA;
3872 /* If rows are at different X or Y, or rows have different height,
3873 or the current row is marked invalid, write the entire line. */
3874 if (!current_row->enabled_p
3875 || desired_row->y != current_row->y
3876 || desired_row->ascent != current_row->ascent
3877 || desired_row->phys_ascent != current_row->phys_ascent
3878 || desired_row->phys_height != current_row->phys_height
3879 || desired_row->visible_height != current_row->visible_height
3880 || current_row->overlapped_p
3881 /* This next line is necessary for correctly redrawing
3882 mouse-face areas after scrolling and other operations.
3883 However, it causes excessive flickering when mouse is moved
3884 across the mode line. Luckily, turning it off for the mode
3885 line doesn't seem to hurt anything. -- cyd.
3886 But it is still needed for the header line. -- kfs. */
3887 || (current_row->mouse_face_p
3888 && !(current_row->mode_line_p && vpos > 0))
3889 || current_row->x != desired_row->x)
3891 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
3893 if (desired_row->used[TEXT_AREA])
3894 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3895 desired_row->used[TEXT_AREA]);
3897 /* Clear to end of window. */
3898 rif->clear_end_of_line (-1);
3899 changed_p = 1;
3901 /* This erases the cursor. We do this here because
3902 notice_overwritten_cursor cannot easily check this, which
3903 might indicate that the whole functionality of
3904 notice_overwritten_cursor would better be implemented here.
3905 On the other hand, we need notice_overwritten_cursor as long
3906 as mouse highlighting is done asynchronously outside of
3907 redisplay. */
3908 if (vpos == w->phys_cursor.vpos)
3909 w->phys_cursor_on_p = 0;
3911 else
3913 int stop, i, x;
3914 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3915 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3916 int overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3917 int desired_stop_pos = desired_row->used[TEXT_AREA];
3918 int abort_skipping = 0;
3920 /* If the desired row extends its face to the text area end, and
3921 unless the current row also does so at the same position,
3922 make sure we write at least one glyph, so that the face
3923 extension actually takes place. */
3924 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3925 && (desired_stop_pos < current_row->used[TEXT_AREA]
3926 || (desired_stop_pos == current_row->used[TEXT_AREA]
3927 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3928 --desired_stop_pos;
3930 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3931 i = 0;
3932 x = desired_row->x;
3934 /* Loop over glyphs that current and desired row may have
3935 in common. */
3936 while (i < stop)
3938 int can_skip_p = !abort_skipping;
3940 /* Skip over glyphs that both rows have in common. These
3941 don't have to be written. We can't skip if the last
3942 current glyph overlaps the glyph to its right. For
3943 example, consider a current row of `if ' with the `f' in
3944 Courier bold so that it overlaps the ` ' to its right.
3945 If the desired row is ` ', we would skip over the space
3946 after the `if' and there would remain a pixel from the
3947 `f' on the screen. */
3948 if (overlapping_glyphs_p && i > 0)
3950 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3951 int left, right;
3953 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3954 &left, &right);
3955 can_skip_p = (right == 0 && !abort_skipping);
3958 if (can_skip_p)
3960 int start_hpos = i;
3962 while (i < stop
3963 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3965 x += desired_glyph->pixel_width;
3966 ++desired_glyph, ++current_glyph, ++i;
3969 /* Consider the case that the current row contains "xxx
3970 ppp ggg" in italic Courier font, and the desired row
3971 is "xxx ggg". The character `p' has lbearing, `g'
3972 has not. The loop above will stop in front of the
3973 first `p' in the current row. If we would start
3974 writing glyphs there, we wouldn't erase the lbearing
3975 of the `p'. The rest of the lbearing problem is then
3976 taken care of by draw_glyphs. */
3977 if (overlapping_glyphs_p
3978 && i > 0
3979 && i < current_row->used[TEXT_AREA]
3980 && (current_row->used[TEXT_AREA]
3981 != desired_row->used[TEXT_AREA]))
3983 int left, right;
3985 rif->get_glyph_overhangs (current_glyph, XFRAME (w->frame),
3986 &left, &right);
3987 while (left > 0 && i > 0)
3989 --i, --desired_glyph, --current_glyph;
3990 x -= desired_glyph->pixel_width;
3991 left -= desired_glyph->pixel_width;
3994 /* Abort the skipping algorithm if we end up before
3995 our starting point, to avoid looping (bug#1070).
3996 This can happen when the lbearing is larger than
3997 the pixel width. */
3998 abort_skipping = (i < start_hpos);
4002 /* Try to avoid writing the entire rest of the desired row
4003 by looking for a resync point. This mainly prevents
4004 mode line flickering in the case the mode line is in
4005 fixed-pitch font, which it usually will be. */
4006 if (i < desired_row->used[TEXT_AREA])
4008 int start_x = x, start_hpos = i;
4009 struct glyph *start = desired_glyph;
4010 int current_x = x;
4011 int skip_first_p = !can_skip_p;
4013 /* Find the next glyph that's equal again. */
4014 while (i < stop
4015 && (skip_first_p
4016 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
4017 && x == current_x)
4019 x += desired_glyph->pixel_width;
4020 current_x += current_glyph->pixel_width;
4021 ++desired_glyph, ++current_glyph, ++i;
4022 skip_first_p = 0;
4025 if (i == start_hpos || x != current_x)
4027 i = start_hpos;
4028 x = start_x;
4029 desired_glyph = start;
4030 break;
4033 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
4034 rif->write_glyphs (start, i - start_hpos);
4035 changed_p = 1;
4039 /* Write the rest. */
4040 if (i < desired_row->used[TEXT_AREA])
4042 rif->cursor_to (vpos, i, desired_row->y, x);
4043 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
4044 changed_p = 1;
4047 /* Maybe clear to end of line. */
4048 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
4050 /* If new row extends to the end of the text area, nothing
4051 has to be cleared, if and only if we did a write_glyphs
4052 above. This is made sure by setting desired_stop_pos
4053 appropriately above. */
4054 xassert (i < desired_row->used[TEXT_AREA]
4055 || ((desired_row->used[TEXT_AREA]
4056 == current_row->used[TEXT_AREA])
4057 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
4059 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
4061 /* If old row extends to the end of the text area, clear. */
4062 if (i >= desired_row->used[TEXT_AREA])
4063 rif->cursor_to (vpos, i, desired_row->y,
4064 desired_row->pixel_width);
4065 rif->clear_end_of_line (-1);
4066 changed_p = 1;
4068 else if (desired_row->pixel_width < current_row->pixel_width)
4070 /* Otherwise clear to the end of the old row. Everything
4071 after that position should be clear already. */
4072 int x;
4074 if (i >= desired_row->used[TEXT_AREA])
4075 rif->cursor_to (vpos, i, desired_row->y,
4076 desired_row->pixel_width);
4078 /* If cursor is displayed at the end of the line, make sure
4079 it's cleared. Nowadays we don't have a phys_cursor_glyph
4080 with which to erase the cursor (because this method
4081 doesn't work with lbearing/rbearing), so we must do it
4082 this way. */
4083 if (vpos == w->phys_cursor.vpos
4084 && (desired_row->reversed_p
4085 ? (w->phys_cursor.hpos < 0)
4086 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
4088 w->phys_cursor_on_p = 0;
4089 x = -1;
4091 else
4092 x = current_row->pixel_width;
4093 rif->clear_end_of_line (x);
4094 changed_p = 1;
4098 return changed_p;
4102 /* Update row VPOS in window W. Value is non-zero if display has been
4103 changed. */
4105 static int
4106 update_window_line (struct window *w, int vpos, int *mouse_face_overwritten_p)
4108 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
4109 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
4110 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4111 int changed_p = 0;
4113 /* Set the row being updated. This is important to let xterm.c
4114 know what line height values are in effect. */
4115 updated_row = desired_row;
4117 /* A row can be completely invisible in case a desired matrix was
4118 built with a vscroll and then make_cursor_line_fully_visible shifts
4119 the matrix. Make sure to make such rows current anyway, since
4120 we need the correct y-position, for example, in the current matrix. */
4121 if (desired_row->mode_line_p
4122 || desired_row->visible_height > 0)
4124 xassert (desired_row->enabled_p);
4126 /* Update display of the left margin area, if there is one. */
4127 if (!desired_row->full_width_p
4128 && !NILP (w->left_margin_cols))
4130 changed_p = 1;
4131 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
4134 /* Update the display of the text area. */
4135 if (update_text_area (w, vpos))
4137 changed_p = 1;
4138 if (current_row->mouse_face_p)
4139 *mouse_face_overwritten_p = 1;
4142 /* Update display of the right margin area, if there is one. */
4143 if (!desired_row->full_width_p
4144 && !NILP (w->right_margin_cols))
4146 changed_p = 1;
4147 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
4150 /* Draw truncation marks etc. */
4151 if (!current_row->enabled_p
4152 || desired_row->y != current_row->y
4153 || desired_row->visible_height != current_row->visible_height
4154 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
4155 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
4156 || current_row->redraw_fringe_bitmaps_p
4157 || desired_row->mode_line_p != current_row->mode_line_p
4158 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
4159 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
4160 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
4161 rif->after_update_window_line_hook (desired_row);
4164 /* Update current_row from desired_row. */
4165 make_current (w->desired_matrix, w->current_matrix, vpos);
4166 updated_row = NULL;
4167 return changed_p;
4171 /* Set the cursor after an update of window W. This function may only
4172 be called from update_window. */
4174 static void
4175 set_window_cursor_after_update (struct window *w)
4177 struct frame *f = XFRAME (w->frame);
4178 struct redisplay_interface *rif = FRAME_RIF (f);
4179 int cx, cy, vpos, hpos;
4181 /* Not intended for frame matrix updates. */
4182 xassert (FRAME_WINDOW_P (f));
4184 if (cursor_in_echo_area
4185 && !NILP (echo_area_buffer[0])
4186 /* If we are showing a message instead of the mini-buffer,
4187 show the cursor for the message instead. */
4188 && XWINDOW (minibuf_window) == w
4189 && EQ (minibuf_window, echo_area_window)
4190 /* These cases apply only to the frame that contains
4191 the active mini-buffer window. */
4192 && FRAME_HAS_MINIBUF_P (f)
4193 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4195 cx = cy = vpos = hpos = 0;
4197 if (cursor_in_echo_area >= 0)
4199 /* If the mini-buffer is several lines high, find the last
4200 line that has any text on it. Note: either all lines
4201 are enabled or none. Otherwise we wouldn't be able to
4202 determine Y. */
4203 struct glyph_row *row, *last_row;
4204 struct glyph *glyph;
4205 int yb = window_text_bottom_y (w);
4207 last_row = NULL;
4208 row = w->current_matrix->rows;
4209 while (row->enabled_p
4210 && (last_row == NULL
4211 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
4213 if (row->used[TEXT_AREA]
4214 && row->glyphs[TEXT_AREA][0].charpos >= 0)
4215 last_row = row;
4216 ++row;
4219 if (last_row)
4221 struct glyph *start = last_row->glyphs[TEXT_AREA];
4222 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
4224 while (last > start && last->charpos < 0)
4225 --last;
4227 for (glyph = start; glyph < last; ++glyph)
4229 cx += glyph->pixel_width;
4230 ++hpos;
4233 cy = last_row->y;
4234 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
4238 else
4240 cx = w->cursor.x;
4241 cy = w->cursor.y;
4242 hpos = w->cursor.hpos;
4243 vpos = w->cursor.vpos;
4246 /* Window cursor can be out of sync for horizontally split windows. */
4247 hpos = max (-1, hpos); /* -1 is for when cursor is on the left fringe */
4248 hpos = min (w->current_matrix->matrix_w - 1, hpos);
4249 vpos = max (0, vpos);
4250 vpos = min (w->current_matrix->nrows - 1, vpos);
4251 rif->cursor_to (vpos, hpos, cy, cx);
4255 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4256 tree rooted at W. */
4258 void
4259 set_window_update_flags (struct window *w, int on_p)
4261 while (w)
4263 if (!NILP (w->hchild))
4264 set_window_update_flags (XWINDOW (w->hchild), on_p);
4265 else if (!NILP (w->vchild))
4266 set_window_update_flags (XWINDOW (w->vchild), on_p);
4267 else
4268 w->must_be_updated_p = on_p;
4270 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4276 /***********************************************************************
4277 Window-Based Scrolling
4278 ***********************************************************************/
4280 /* Structure describing rows in scrolling_window. */
4282 struct row_entry
4284 /* Number of occurrences of this row in desired and current matrix. */
4285 int old_uses, new_uses;
4287 /* Vpos of row in new matrix. */
4288 int new_line_number;
4290 /* Bucket index of this row_entry in the hash table row_table. */
4291 int bucket;
4293 /* The row described by this entry. */
4294 struct glyph_row *row;
4296 /* Hash collision chain. */
4297 struct row_entry *next;
4300 /* A pool to allocate row_entry structures from, and the size of the
4301 pool. The pool is reallocated in scrolling_window when we find
4302 that we need a larger one. */
4304 static struct row_entry *row_entry_pool;
4305 static int row_entry_pool_size;
4307 /* Index of next free entry in row_entry_pool. */
4309 static int row_entry_idx;
4311 /* The hash table used during scrolling, and the table's size. This
4312 table is used to quickly identify equal rows in the desired and
4313 current matrix. */
4315 static struct row_entry **row_table;
4316 static int row_table_size;
4318 /* Vectors of pointers to row_entry structures belonging to the
4319 current and desired matrix, and the size of the vectors. */
4321 static struct row_entry **old_lines, **new_lines;
4322 static int old_lines_size, new_lines_size;
4324 /* A pool to allocate run structures from, and its size. */
4326 static struct run *run_pool;
4327 static int runs_size;
4329 /* A vector of runs of lines found during scrolling. */
4331 static struct run **runs;
4333 /* Add glyph row ROW to the scrolling hash table during the scrolling
4334 of window W. */
4336 static INLINE struct row_entry *
4337 add_row_entry (struct window *w, struct glyph_row *row)
4339 struct row_entry *entry;
4340 int i = row->hash % row_table_size;
4342 entry = row_table[i];
4343 while (entry && !row_equal_p (w, entry->row, row, 1))
4344 entry = entry->next;
4346 if (entry == NULL)
4348 entry = row_entry_pool + row_entry_idx++;
4349 entry->row = row;
4350 entry->old_uses = entry->new_uses = 0;
4351 entry->new_line_number = 0;
4352 entry->bucket = i;
4353 entry->next = row_table[i];
4354 row_table[i] = entry;
4357 return entry;
4361 /* Try to reuse part of the current display of W by scrolling lines.
4362 HEADER_LINE_P non-zero means W has a header line.
4364 The algorithm is taken from Communications of the ACM, Apr78 "A
4365 Technique for Isolating Differences Between Files." It should take
4366 O(N) time.
4368 A short outline of the steps of the algorithm
4370 1. Skip lines equal at the start and end of both matrices.
4372 2. Enter rows in the current and desired matrix into a symbol
4373 table, counting how often they appear in both matrices.
4375 3. Rows that appear exactly once in both matrices serve as anchors,
4376 i.e. we assume that such lines are likely to have been moved.
4378 4. Starting from anchor lines, extend regions to be scrolled both
4379 forward and backward.
4381 Value is
4383 -1 if all rows were found to be equal.
4384 0 to indicate that we did not scroll the display, or
4385 1 if we did scroll. */
4387 static int
4388 scrolling_window (struct window *w, int header_line_p)
4390 struct glyph_matrix *desired_matrix = w->desired_matrix;
4391 struct glyph_matrix *current_matrix = w->current_matrix;
4392 int yb = window_text_bottom_y (w);
4393 int i, j, first_old, first_new, last_old, last_new;
4394 int nruns, nbytes, n, run_idx;
4395 struct row_entry *entry;
4396 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4398 /* Skip over rows equal at the start. */
4399 for (i = header_line_p ? 1 : 0; i < current_matrix->nrows - 1; ++i)
4401 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4402 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4404 if (c->enabled_p
4405 && d->enabled_p
4406 && !d->redraw_fringe_bitmaps_p
4407 && c->y == d->y
4408 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4409 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4410 && row_equal_p (w, c, d, 1))
4412 assign_row (c, d);
4413 d->enabled_p = 0;
4415 else
4416 break;
4419 /* Give up if some rows in the desired matrix are not enabled. */
4420 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4421 return -1;
4423 first_old = first_new = i;
4425 /* Set last_new to the index + 1 of the last enabled row in the
4426 desired matrix. */
4427 i = first_new + 1;
4428 while (i < desired_matrix->nrows - 1
4429 && MATRIX_ROW (desired_matrix, i)->enabled_p
4430 && MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i)) <= yb)
4431 ++i;
4433 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4434 return 0;
4436 last_new = i;
4438 /* Set last_old to the index + 1 of the last enabled row in the
4439 current matrix. We don't look at the enabled flag here because
4440 we plan to reuse part of the display even if other parts are
4441 disabled. */
4442 i = first_old + 1;
4443 while (i < current_matrix->nrows - 1)
4445 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4446 if (bottom <= yb)
4447 ++i;
4448 if (bottom >= yb)
4449 break;
4452 last_old = i;
4454 /* Skip over rows equal at the bottom. */
4455 i = last_new;
4456 j = last_old;
4457 while (i - 1 > first_new
4458 && j - 1 > first_old
4459 && MATRIX_ROW (current_matrix, i - 1)->enabled_p
4460 && (MATRIX_ROW (current_matrix, i - 1)->y
4461 == MATRIX_ROW (desired_matrix, j - 1)->y)
4462 && !MATRIX_ROW (desired_matrix, j - 1)->redraw_fringe_bitmaps_p
4463 && row_equal_p (w,
4464 MATRIX_ROW (desired_matrix, i - 1),
4465 MATRIX_ROW (current_matrix, j - 1), 1))
4466 --i, --j;
4467 last_new = i;
4468 last_old = j;
4470 /* Nothing to do if all rows are equal. */
4471 if (last_new == first_new)
4472 return 0;
4474 /* Reallocate vectors, tables etc. if necessary. */
4476 if (current_matrix->nrows > old_lines_size)
4478 old_lines_size = current_matrix->nrows;
4479 nbytes = old_lines_size * sizeof *old_lines;
4480 old_lines = (struct row_entry **) xrealloc (old_lines, nbytes);
4483 if (desired_matrix->nrows > new_lines_size)
4485 new_lines_size = desired_matrix->nrows;
4486 nbytes = new_lines_size * sizeof *new_lines;
4487 new_lines = (struct row_entry **) xrealloc (new_lines, nbytes);
4490 n = desired_matrix->nrows + current_matrix->nrows;
4491 if (3 * n > row_table_size)
4493 row_table_size = next_almost_prime (3 * n);
4494 nbytes = row_table_size * sizeof *row_table;
4495 row_table = (struct row_entry **) xrealloc (row_table, nbytes);
4496 memset (row_table, 0, nbytes);
4499 if (n > row_entry_pool_size)
4501 row_entry_pool_size = n;
4502 nbytes = row_entry_pool_size * sizeof *row_entry_pool;
4503 row_entry_pool = (struct row_entry *) xrealloc (row_entry_pool, nbytes);
4506 if (desired_matrix->nrows > runs_size)
4508 runs_size = desired_matrix->nrows;
4509 nbytes = runs_size * sizeof *runs;
4510 runs = (struct run **) xrealloc (runs, nbytes);
4511 nbytes = runs_size * sizeof *run_pool;
4512 run_pool = (struct run *) xrealloc (run_pool, nbytes);
4515 nruns = run_idx = 0;
4516 row_entry_idx = 0;
4518 /* Add rows from the current and desired matrix to the hash table
4519 row_hash_table to be able to find equal ones quickly. */
4521 for (i = first_old; i < last_old; ++i)
4523 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4525 entry = add_row_entry (w, MATRIX_ROW (current_matrix, i));
4526 old_lines[i] = entry;
4527 ++entry->old_uses;
4529 else
4530 old_lines[i] = NULL;
4533 for (i = first_new; i < last_new; ++i)
4535 xassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4536 entry = add_row_entry (w, MATRIX_ROW (desired_matrix, i));
4537 ++entry->new_uses;
4538 entry->new_line_number = i;
4539 new_lines[i] = entry;
4542 /* Identify moves based on lines that are unique and equal
4543 in both matrices. */
4544 for (i = first_old; i < last_old;)
4545 if (old_lines[i]
4546 && old_lines[i]->old_uses == 1
4547 && old_lines[i]->new_uses == 1)
4549 int j, k;
4550 int new_line = old_lines[i]->new_line_number;
4551 struct run *run = run_pool + run_idx++;
4553 /* Record move. */
4554 run->current_vpos = i;
4555 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4556 run->desired_vpos = new_line;
4557 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4558 run->nrows = 1;
4559 run->height = MATRIX_ROW (current_matrix, i)->height;
4561 /* Extend backward. */
4562 j = i - 1;
4563 k = new_line - 1;
4564 while (j > first_old
4565 && k > first_new
4566 && old_lines[j] == new_lines[k])
4568 int h = MATRIX_ROW (current_matrix, j)->height;
4569 --run->current_vpos;
4570 --run->desired_vpos;
4571 ++run->nrows;
4572 run->height += h;
4573 run->desired_y -= h;
4574 run->current_y -= h;
4575 --j, --k;
4578 /* Extend forward. */
4579 j = i + 1;
4580 k = new_line + 1;
4581 while (j < last_old
4582 && k < last_new
4583 && old_lines[j] == new_lines[k])
4585 int h = MATRIX_ROW (current_matrix, j)->height;
4586 ++run->nrows;
4587 run->height += h;
4588 ++j, ++k;
4591 /* Insert run into list of all runs. Order runs by copied
4592 pixel lines. Note that we record runs that don't have to
4593 be copied because they are already in place. This is done
4594 because we can avoid calling update_window_line in this
4595 case. */
4596 for (j = 0; j < nruns && runs[j]->height > run->height; ++j)
4598 for (k = nruns; k > j; --k)
4599 runs[k] = runs[k - 1];
4600 runs[j] = run;
4601 ++nruns;
4603 i += run->nrows;
4605 else
4606 ++i;
4608 /* Do the moves. Do it in a way that we don't overwrite something
4609 we want to copy later on. This is not solvable in general
4610 because there is only one display and we don't have a way to
4611 exchange areas on this display. Example:
4613 +-----------+ +-----------+
4614 | A | | B |
4615 +-----------+ --> +-----------+
4616 | B | | A |
4617 +-----------+ +-----------+
4619 Instead, prefer bigger moves, and invalidate moves that would
4620 copy from where we copied to. */
4622 for (i = 0; i < nruns; ++i)
4623 if (runs[i]->nrows > 0)
4625 struct run *r = runs[i];
4627 /* Copy on the display. */
4628 if (r->current_y != r->desired_y)
4630 rif->scroll_run_hook (w, r);
4632 /* Invalidate runs that copy from where we copied to. */
4633 for (j = i + 1; j < nruns; ++j)
4635 struct run *p = runs[j];
4637 if ((p->current_y >= r->desired_y
4638 && p->current_y < r->desired_y + r->height)
4639 || (p->current_y + p->height >= r->desired_y
4640 && (p->current_y + p->height
4641 < r->desired_y + r->height)))
4642 p->nrows = 0;
4646 /* Assign matrix rows. */
4647 for (j = 0; j < r->nrows; ++j)
4649 struct glyph_row *from, *to;
4650 int to_overlapped_p;
4652 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4653 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4654 to_overlapped_p = to->overlapped_p;
4655 if (!from->mode_line_p && !w->pseudo_window_p
4656 && (to->left_fringe_bitmap != from->left_fringe_bitmap
4657 || to->right_fringe_bitmap != from->right_fringe_bitmap
4658 || to->left_fringe_face_id != from->left_fringe_face_id
4659 || to->right_fringe_face_id != from->right_fringe_face_id
4660 || to->overlay_arrow_bitmap != from->overlay_arrow_bitmap))
4661 from->redraw_fringe_bitmaps_p = 1;
4662 assign_row (to, from);
4663 to->enabled_p = 1, from->enabled_p = 0;
4664 to->overlapped_p = to_overlapped_p;
4668 /* Clear the hash table, for the next time. */
4669 for (i = 0; i < row_entry_idx; ++i)
4670 row_table[row_entry_pool[i].bucket] = NULL;
4672 /* Value is > 0 to indicate that we scrolled the display. */
4673 return nruns;
4678 /************************************************************************
4679 Frame-Based Updates
4680 ************************************************************************/
4682 /* Update the desired frame matrix of frame F.
4684 FORCE_P non-zero means that the update should not be stopped by
4685 pending input. INHIBIT_HAIRY_ID_P non-zero means that scrolling
4686 should not be tried.
4688 Value is non-zero if update was stopped due to pending input. */
4690 static int
4691 update_frame_1 (struct frame *f, int force_p, int inhibit_id_p)
4693 /* Frame matrices to work on. */
4694 struct glyph_matrix *current_matrix = f->current_matrix;
4695 struct glyph_matrix *desired_matrix = f->desired_matrix;
4696 int i;
4697 int pause;
4698 int preempt_count = baud_rate / 2400 + 1;
4700 xassert (current_matrix && desired_matrix);
4702 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4703 calculate_costs (f);
4705 if (preempt_count <= 0)
4706 preempt_count = 1;
4708 #if !PERIODIC_PREEMPTION_CHECKING
4709 if (!force_p && detect_input_pending_ignore_squeezables ())
4711 pause = 1;
4712 goto do_pause;
4714 #endif
4716 /* If we cannot insert/delete lines, it's no use trying it. */
4717 if (!FRAME_LINE_INS_DEL_OK (f))
4718 inhibit_id_p = 1;
4720 /* See if any of the desired lines are enabled; don't compute for
4721 i/d line if just want cursor motion. */
4722 for (i = 0; i < desired_matrix->nrows; i++)
4723 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4724 break;
4726 /* Try doing i/d line, if not yet inhibited. */
4727 if (!inhibit_id_p && i < desired_matrix->nrows)
4728 force_p |= scrolling (f);
4730 /* Update the individual lines as needed. Do bottom line first. */
4731 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4732 update_frame_line (f, desired_matrix->nrows - 1);
4734 /* Now update the rest of the lines. */
4735 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4737 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4739 if (FRAME_TERMCAP_P (f))
4741 /* Flush out every so many lines.
4742 Also flush out if likely to have more than 1k buffered
4743 otherwise. I'm told that some telnet connections get
4744 really screwed by more than 1k output at once. */
4745 FILE *display_output = FRAME_TTY (f)->output;
4746 if (display_output)
4748 int outq = PENDING_OUTPUT_COUNT (display_output);
4749 if (outq > 900
4750 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4752 fflush (display_output);
4753 if (preempt_count == 1)
4755 #ifdef EMACS_OUTQSIZE
4756 if (EMACS_OUTQSIZE (0, &outq) < 0)
4757 /* Probably not a tty. Ignore the error and reset
4758 the outq count. */
4759 outq = PENDING_OUTPUT_COUNT (FRAME_TTY (f->output));
4760 #endif
4761 outq *= 10;
4762 if (baud_rate <= outq && baud_rate > 0)
4763 sleep (outq / baud_rate);
4769 #if PERIODIC_PREEMPTION_CHECKING
4770 if (!force_p)
4772 EMACS_TIME tm, dif;
4773 EMACS_GET_TIME (tm);
4774 EMACS_SUB_TIME (dif, preemption_next_check, tm);
4775 if (EMACS_TIME_NEG_P (dif))
4777 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
4778 if (detect_input_pending_ignore_squeezables ())
4779 break;
4782 #else
4783 if (!force_p && (i - 1) % preempt_count == 0)
4784 detect_input_pending_ignore_squeezables ();
4785 #endif
4787 update_frame_line (f, i);
4791 pause = (i < FRAME_LINES (f) - 1) ? i : 0;
4793 /* Now just clean up termcap drivers and set cursor, etc. */
4794 if (!pause)
4796 if ((cursor_in_echo_area
4797 /* If we are showing a message instead of the mini-buffer,
4798 show the cursor for the message instead of for the
4799 (now hidden) mini-buffer contents. */
4800 || (EQ (minibuf_window, selected_window)
4801 && EQ (minibuf_window, echo_area_window)
4802 && !NILP (echo_area_buffer[0])))
4803 /* These cases apply only to the frame that contains
4804 the active mini-buffer window. */
4805 && FRAME_HAS_MINIBUF_P (f)
4806 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4808 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4809 int row, col;
4811 if (cursor_in_echo_area < 0)
4813 /* Negative value of cursor_in_echo_area means put
4814 cursor at beginning of line. */
4815 row = top;
4816 col = 0;
4818 else
4820 /* Positive value of cursor_in_echo_area means put
4821 cursor at the end of the prompt. If the mini-buffer
4822 is several lines high, find the last line that has
4823 any text on it. */
4824 row = FRAME_LINES (f);
4827 --row;
4828 col = 0;
4830 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4832 /* Frame rows are filled up with spaces that
4833 must be ignored here. */
4834 struct glyph_row *r = MATRIX_ROW (current_matrix,
4835 row);
4836 struct glyph *start = r->glyphs[TEXT_AREA];
4837 struct glyph *last = start + r->used[TEXT_AREA];
4839 while (last > start
4840 && (last - 1)->charpos < 0)
4841 --last;
4843 col = last - start;
4846 while (row > top && col == 0);
4848 /* Make sure COL is not out of range. */
4849 if (col >= FRAME_CURSOR_X_LIMIT (f))
4851 /* If we have another row, advance cursor into it. */
4852 if (row < FRAME_LINES (f) - 1)
4854 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4855 row++;
4857 /* Otherwise move it back in range. */
4858 else
4859 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4863 cursor_to (f, row, col);
4865 else
4867 /* We have only one cursor on terminal frames. Use it to
4868 display the cursor of the selected window. */
4869 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4870 if (w->cursor.vpos >= 0
4871 /* The cursor vpos may be temporarily out of bounds
4872 in the following situation: There is one window,
4873 with the cursor in the lower half of it. The window
4874 is split, and a message causes a redisplay before
4875 a new cursor position has been computed. */
4876 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4878 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4879 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4881 if (INTEGERP (w->left_margin_cols))
4882 x += XFASTINT (w->left_margin_cols);
4884 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4885 cursor_to (f, y, x);
4890 #if !PERIODIC_PREEMPTION_CHECKING
4891 do_pause:
4892 #endif
4894 clear_desired_matrices (f);
4895 return pause;
4899 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4902 scrolling (struct frame *frame)
4904 int unchanged_at_top, unchanged_at_bottom;
4905 int window_size;
4906 int changed_lines;
4907 int *old_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4908 int *new_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4909 int *draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4910 int *old_draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4911 register int i;
4912 int free_at_end_vpos = FRAME_LINES (frame);
4913 struct glyph_matrix *current_matrix = frame->current_matrix;
4914 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4916 if (!current_matrix)
4917 abort ();
4919 /* Compute hash codes of all the lines. Also calculate number of
4920 changed lines, number of unchanged lines at the beginning, and
4921 number of unchanged lines at the end. */
4922 changed_lines = 0;
4923 unchanged_at_top = 0;
4924 unchanged_at_bottom = FRAME_LINES (frame);
4925 for (i = 0; i < FRAME_LINES (frame); i++)
4927 /* Give up on this scrolling if some old lines are not enabled. */
4928 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4929 return 0;
4930 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4931 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4933 /* This line cannot be redrawn, so don't let scrolling mess it. */
4934 new_hash[i] = old_hash[i];
4935 #define INFINITY 1000000 /* Taken from scroll.c */
4936 draw_cost[i] = INFINITY;
4938 else
4940 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4941 draw_cost[i] = line_draw_cost (desired_matrix, i);
4944 if (old_hash[i] != new_hash[i])
4946 changed_lines++;
4947 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4949 else if (i == unchanged_at_top)
4950 unchanged_at_top++;
4951 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4954 /* If changed lines are few, don't allow preemption, don't scroll. */
4955 if ((!FRAME_SCROLL_REGION_OK (frame)
4956 && changed_lines < baud_rate / 2400)
4957 || unchanged_at_bottom == FRAME_LINES (frame))
4958 return 1;
4960 window_size = (FRAME_LINES (frame) - unchanged_at_top
4961 - unchanged_at_bottom);
4963 if (FRAME_SCROLL_REGION_OK (frame))
4964 free_at_end_vpos -= unchanged_at_bottom;
4965 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4966 free_at_end_vpos = -1;
4968 /* If large window, fast terminal and few lines in common between
4969 current frame and desired frame, don't bother with i/d calc. */
4970 if (!FRAME_SCROLL_REGION_OK (frame)
4971 && window_size >= 18 && baud_rate > 2400
4972 && (window_size >=
4973 10 * scrolling_max_lines_saved (unchanged_at_top,
4974 FRAME_LINES (frame) - unchanged_at_bottom,
4975 old_hash, new_hash, draw_cost)))
4976 return 0;
4978 if (window_size < 2)
4979 return 0;
4981 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4982 draw_cost + unchanged_at_top - 1,
4983 old_draw_cost + unchanged_at_top - 1,
4984 old_hash + unchanged_at_top - 1,
4985 new_hash + unchanged_at_top - 1,
4986 free_at_end_vpos - unchanged_at_top);
4988 return 0;
4992 /* Count the number of blanks at the start of the vector of glyphs R
4993 which is LEN glyphs long. */
4995 static int
4996 count_blanks (struct glyph *r, int len)
4998 int i;
5000 for (i = 0; i < len; ++i)
5001 if (!CHAR_GLYPH_SPACE_P (r[i]))
5002 break;
5004 return i;
5008 /* Count the number of glyphs in common at the start of the glyph
5009 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
5010 of STR2. Value is the number of equal glyphs equal at the start. */
5012 static int
5013 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
5015 struct glyph *p1 = str1;
5016 struct glyph *p2 = str2;
5018 while (p1 < end1
5019 && p2 < end2
5020 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
5021 ++p1, ++p2;
5023 return p1 - str1;
5027 /* Char insertion/deletion cost vector, from term.c */
5029 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS((f))])
5032 /* Perform a frame-based update on line VPOS in frame FRAME. */
5034 static void
5035 update_frame_line (struct frame *f, int vpos)
5037 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
5038 int tem;
5039 int osp, nsp, begmatch, endmatch, olen, nlen;
5040 struct glyph_matrix *current_matrix = f->current_matrix;
5041 struct glyph_matrix *desired_matrix = f->desired_matrix;
5042 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
5043 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
5044 int must_write_whole_line_p;
5045 int write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
5046 int colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
5047 != FACE_TTY_DEFAULT_BG_COLOR);
5049 if (colored_spaces_p)
5050 write_spaces_p = 1;
5052 /* Current row not enabled means it has unknown contents. We must
5053 write the whole desired line in that case. */
5054 must_write_whole_line_p = !current_row->enabled_p;
5055 if (must_write_whole_line_p)
5057 obody = 0;
5058 olen = 0;
5060 else
5062 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
5063 olen = current_row->used[TEXT_AREA];
5065 /* Ignore trailing spaces, if we can. */
5066 if (!write_spaces_p)
5067 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
5068 olen--;
5071 current_row->enabled_p = 1;
5072 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
5074 /* If desired line is empty, just clear the line. */
5075 if (!desired_row->enabled_p)
5077 nlen = 0;
5078 goto just_erase;
5081 nbody = desired_row->glyphs[TEXT_AREA];
5082 nlen = desired_row->used[TEXT_AREA];
5083 nend = nbody + nlen;
5085 /* If display line has unknown contents, write the whole line. */
5086 if (must_write_whole_line_p)
5088 /* Ignore spaces at the end, if we can. */
5089 if (!write_spaces_p)
5090 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5091 --nlen;
5093 /* Write the contents of the desired line. */
5094 if (nlen)
5096 cursor_to (f, vpos, 0);
5097 write_glyphs (f, nbody, nlen);
5100 /* Don't call clear_end_of_line if we already wrote the whole
5101 line. The cursor will not be at the right margin in that
5102 case but in the line below. */
5103 if (nlen < FRAME_TOTAL_COLS (f))
5105 cursor_to (f, vpos, nlen);
5106 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
5108 else
5109 /* Make sure we are in the right row, otherwise cursor movement
5110 with cmgoto might use `ch' in the wrong row. */
5111 cursor_to (f, vpos, 0);
5113 make_current (desired_matrix, current_matrix, vpos);
5114 return;
5117 /* Pretend trailing spaces are not there at all,
5118 unless for one reason or another we must write all spaces. */
5119 if (!write_spaces_p)
5120 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5121 nlen--;
5123 /* If there's no i/d char, quickly do the best we can without it. */
5124 if (!FRAME_CHAR_INS_DEL_OK (f))
5126 int i, j;
5128 /* Find the first glyph in desired row that doesn't agree with
5129 a glyph in the current row, and write the rest from there on. */
5130 for (i = 0; i < nlen; i++)
5132 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
5134 /* Find the end of the run of different glyphs. */
5135 j = i + 1;
5136 while (j < nlen
5137 && (j >= olen
5138 || !GLYPH_EQUAL_P (nbody + j, obody + j)
5139 || CHAR_GLYPH_PADDING_P (nbody[j])))
5140 ++j;
5142 /* Output this run of non-matching chars. */
5143 cursor_to (f, vpos, i);
5144 write_glyphs (f, nbody + i, j - i);
5145 i = j - 1;
5147 /* Now find the next non-match. */
5151 /* Clear the rest of the line, or the non-clear part of it. */
5152 if (olen > nlen)
5154 cursor_to (f, vpos, nlen);
5155 clear_end_of_line (f, olen);
5158 /* Make current row = desired row. */
5159 make_current (desired_matrix, current_matrix, vpos);
5160 return;
5163 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5164 characters in a row. */
5166 if (!olen)
5168 /* If current line is blank, skip over initial spaces, if
5169 possible, and write the rest. */
5170 if (write_spaces_p)
5171 nsp = 0;
5172 else
5173 nsp = count_blanks (nbody, nlen);
5175 if (nlen > nsp)
5177 cursor_to (f, vpos, nsp);
5178 write_glyphs (f, nbody + nsp, nlen - nsp);
5181 /* Exchange contents between current_frame and new_frame. */
5182 make_current (desired_matrix, current_matrix, vpos);
5183 return;
5186 /* Compute number of leading blanks in old and new contents. */
5187 osp = count_blanks (obody, olen);
5188 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
5190 /* Compute number of matching chars starting with first non-blank. */
5191 begmatch = count_match (obody + osp, obody + olen,
5192 nbody + nsp, nbody + nlen);
5194 /* Spaces in new match implicit space past the end of old. */
5195 /* A bug causing this to be a no-op was fixed in 18.29. */
5196 if (!write_spaces_p && osp + begmatch == olen)
5198 np1 = nbody + nsp;
5199 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
5200 ++begmatch;
5203 /* Avoid doing insert/delete char
5204 just cause number of leading spaces differs
5205 when the following text does not match. */
5206 if (begmatch == 0 && osp != nsp)
5207 osp = nsp = min (osp, nsp);
5209 /* Find matching characters at end of line */
5210 op1 = obody + olen;
5211 np1 = nbody + nlen;
5212 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5213 while (op1 > op2
5214 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
5216 op1--;
5217 np1--;
5219 endmatch = obody + olen - op1;
5221 /* tem gets the distance to insert or delete.
5222 endmatch is how many characters we save by doing so.
5223 Is it worth it? */
5225 tem = (nlen - nsp) - (olen - osp);
5226 if (endmatch && tem
5227 && (!FRAME_CHAR_INS_DEL_OK (f)
5228 || endmatch <= char_ins_del_cost (f)[tem]))
5229 endmatch = 0;
5231 /* nsp - osp is the distance to insert or delete.
5232 If that is nonzero, begmatch is known to be nonzero also.
5233 begmatch + endmatch is how much we save by doing the ins/del.
5234 Is it worth it? */
5236 if (nsp != osp
5237 && (!FRAME_CHAR_INS_DEL_OK (f)
5238 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
5240 begmatch = 0;
5241 endmatch = 0;
5242 osp = nsp = min (osp, nsp);
5245 /* Now go through the line, inserting, writing and
5246 deleting as appropriate. */
5248 if (osp > nsp)
5250 cursor_to (f, vpos, nsp);
5251 delete_glyphs (f, osp - nsp);
5253 else if (nsp > osp)
5255 /* If going to delete chars later in line
5256 and insert earlier in the line,
5257 must delete first to avoid losing data in the insert */
5258 if (endmatch && nlen < olen + nsp - osp)
5260 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5261 delete_glyphs (f, olen + nsp - osp - nlen);
5262 olen = nlen - (nsp - osp);
5264 cursor_to (f, vpos, osp);
5265 insert_glyphs (f, 0, nsp - osp);
5267 olen += nsp - osp;
5269 tem = nsp + begmatch + endmatch;
5270 if (nlen != tem || olen != tem)
5272 if (!endmatch || nlen == olen)
5274 /* If new text being written reaches right margin, there is
5275 no need to do clear-to-eol at the end of this function
5276 (and it would not be safe, since cursor is not going to
5277 be "at the margin" after the text is done). */
5278 if (nlen == FRAME_TOTAL_COLS (f))
5279 olen = 0;
5281 /* Function write_glyphs is prepared to do nothing
5282 if passed a length <= 0. Check it here to avoid
5283 unnecessary cursor movement. */
5284 if (nlen - tem > 0)
5286 cursor_to (f, vpos, nsp + begmatch);
5287 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5290 else if (nlen > olen)
5292 /* Here, we used to have the following simple code:
5293 ----------------------------------------
5294 write_glyphs (nbody + nsp + begmatch, olen - tem);
5295 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5296 ----------------------------------------
5297 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5298 is a padding glyph. */
5299 int out = olen - tem; /* Columns to be overwritten originally. */
5300 int del;
5302 cursor_to (f, vpos, nsp + begmatch);
5304 /* Calculate columns we can actually overwrite. */
5305 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5306 out--;
5307 write_glyphs (f, nbody + nsp + begmatch, out);
5309 /* If we left columns to be overwritten, we must delete them. */
5310 del = olen - tem - out;
5311 if (del > 0)
5312 delete_glyphs (f, del);
5314 /* At last, we insert columns not yet written out. */
5315 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5316 olen = nlen;
5318 else if (olen > nlen)
5320 cursor_to (f, vpos, nsp + begmatch);
5321 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5322 delete_glyphs (f, olen - nlen);
5323 olen = nlen;
5327 just_erase:
5328 /* If any unerased characters remain after the new line, erase them. */
5329 if (olen > nlen)
5331 cursor_to (f, vpos, nlen);
5332 clear_end_of_line (f, olen);
5335 /* Exchange contents between current_frame and new_frame. */
5336 make_current (desired_matrix, current_matrix, vpos);
5341 /***********************************************************************
5342 X/Y Position -> Buffer Position
5343 ***********************************************************************/
5345 /* Determine what's under window-relative pixel position (*X, *Y).
5346 Return the OBJECT (string or buffer) that's there.
5347 Return in *POS the position in that object.
5348 Adjust *X and *Y to character positions.
5349 Return in *DX and *DY the pixel coordinates of the click,
5350 relative to the top left corner of OBJECT, or relative to
5351 the top left corner of the character glyph at (*X, *Y)
5352 if OBJECT is nil.
5353 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5354 if the coordinates point to an empty area of the display. */
5356 Lisp_Object
5357 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)
5359 struct it it;
5360 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5361 struct text_pos startp;
5362 Lisp_Object string;
5363 struct glyph_row *row;
5364 #ifdef HAVE_WINDOW_SYSTEM
5365 struct image *img = 0;
5366 #endif
5367 int x0, x1, to_x;
5369 /* We used to set current_buffer directly here, but that does the
5370 wrong thing with `face-remapping-alist' (bug#2044). */
5371 Fset_buffer (w->buffer);
5372 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5373 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5374 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5375 start_display (&it, w, startp);
5377 x0 = *x;
5379 /* First, move to the beginning of the row corresponding to *Y. We
5380 need to be in that row to get the correct value of base paragraph
5381 direction for the text at (*X, *Y). */
5382 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5384 /* TO_X is the pixel position that the iterator will compute for the
5385 glyph at *X. We add it.first_visible_x because iterator
5386 positions include the hscroll. */
5387 to_x = x0 + it.first_visible_x;
5388 if (it.bidi_it.paragraph_dir == R2L)
5389 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5390 text area. This is because the iterator, even in R2L
5391 paragraphs, delivers glyphs as if they started at the left
5392 margin of the window. (When we actually produce glyphs for
5393 display, we reverse their order in PRODUCE_GLYPHS, but the
5394 iterator doesn't know about that.) The following line adjusts
5395 the pixel position to the iterator geometry, which is what
5396 move_it_* routines use. (The -1 is because in a window whose
5397 text-area width is W, the rightmost pixel position is W-1, and
5398 it should be mirrored into zero pixel position.) */
5399 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5401 /* Now move horizontally in the row to the glyph under *X. Second
5402 argument is ZV to prevent move_it_in_display_line from matching
5403 based on buffer positions. */
5404 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5406 Fset_buffer (old_current_buffer);
5408 *dx = x0 + it.first_visible_x - it.current_x;
5409 *dy = *y - it.current_y;
5411 string = w->buffer;
5412 if (STRINGP (it.string))
5413 string = it.string;
5414 *pos = it.current;
5415 if (it.what == IT_COMPOSITION
5416 && it.cmp_it.nchars > 1
5417 && it.cmp_it.reversed_p)
5419 /* The current display element is a grapheme cluster in a
5420 composition. In that case, we need the position of the first
5421 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5422 it.current points to the last character of the cluster, thus
5423 we must move back to the first character of the same
5424 cluster. */
5425 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5426 if (STRINGP (it.string))
5427 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5428 else
5429 BYTEPOS (pos->pos) = CHAR_TO_BYTE (CHARPOS (pos->pos));
5432 #ifdef HAVE_WINDOW_SYSTEM
5433 if (it.what == IT_IMAGE)
5435 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5436 && !NILP (img->spec))
5437 *object = img->spec;
5439 #endif
5441 if (it.vpos < w->current_matrix->nrows
5442 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5443 row->enabled_p))
5445 if (it.hpos < row->used[TEXT_AREA])
5447 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5448 #ifdef HAVE_WINDOW_SYSTEM
5449 if (img)
5451 *dy -= row->ascent - glyph->ascent;
5452 *dx += glyph->slice.img.x;
5453 *dy += glyph->slice.img.y;
5454 /* Image slices positions are still relative to the entire image */
5455 *width = img->width;
5456 *height = img->height;
5458 else
5459 #endif
5461 *width = glyph->pixel_width;
5462 *height = glyph->ascent + glyph->descent;
5465 else
5467 *width = 0;
5468 *height = row->height;
5471 else
5473 *width = *height = 0;
5476 /* Add extra (default width) columns if clicked after EOL. */
5477 x1 = max(0, it.current_x + it.pixel_width - it.first_visible_x);
5478 if (x0 > x1)
5479 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5481 *x = it.hpos;
5482 *y = it.vpos;
5484 return string;
5488 /* Value is the string under window-relative coordinates X/Y in the
5489 mode line or header line (PART says which) of window W, or nil if none.
5490 *CHARPOS is set to the position in the string returned. */
5492 Lisp_Object
5493 mode_line_string (struct window *w, enum window_part part,
5494 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5495 int *dx, int *dy, int *width, int *height)
5497 struct glyph_row *row;
5498 struct glyph *glyph, *end;
5499 int x0, y0;
5500 Lisp_Object string = Qnil;
5502 if (part == ON_MODE_LINE)
5503 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5504 else
5505 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5506 y0 = *y - row->y;
5507 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5509 if (row->mode_line_p && row->enabled_p)
5511 /* Find the glyph under X. If we find one with a string object,
5512 it's the one we were looking for. */
5513 glyph = row->glyphs[TEXT_AREA];
5514 end = glyph + row->used[TEXT_AREA];
5515 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5516 x0 -= glyph->pixel_width;
5517 *x = glyph - row->glyphs[TEXT_AREA];
5518 if (glyph < end)
5520 string = glyph->object;
5521 *charpos = glyph->charpos;
5522 *width = glyph->pixel_width;
5523 *height = glyph->ascent + glyph->descent;
5524 #ifdef HAVE_WINDOW_SYSTEM
5525 if (glyph->type == IMAGE_GLYPH)
5527 struct image *img;
5528 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5529 if (img != NULL)
5530 *object = img->spec;
5531 y0 -= row->ascent - glyph->ascent;
5533 #endif
5535 else
5537 /* Add extra (default width) columns if clicked after EOL. */
5538 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5539 *width = 0;
5540 *height = row->height;
5543 else
5545 *x = 0;
5546 x0 = 0;
5547 *width = *height = 0;
5550 *dx = x0;
5551 *dy = y0;
5553 return string;
5557 /* Value is the string under window-relative coordinates X/Y in either
5558 marginal area, or nil if none. *CHARPOS is set to the position in
5559 the string returned. */
5561 Lisp_Object
5562 marginal_area_string (struct window *w, enum window_part part,
5563 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5564 int *dx, int *dy, int *width, int *height)
5566 struct glyph_row *row = w->current_matrix->rows;
5567 struct glyph *glyph, *end;
5568 int x0, y0, i, wy = *y;
5569 int area;
5570 Lisp_Object string = Qnil;
5572 if (part == ON_LEFT_MARGIN)
5573 area = LEFT_MARGIN_AREA;
5574 else if (part == ON_RIGHT_MARGIN)
5575 area = RIGHT_MARGIN_AREA;
5576 else
5577 abort ();
5579 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5580 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5581 break;
5582 y0 = *y - row->y;
5583 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5585 if (row->enabled_p)
5587 /* Find the glyph under X. If we find one with a string object,
5588 it's the one we were looking for. */
5589 if (area == RIGHT_MARGIN_AREA)
5590 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5591 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5592 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5593 + window_box_width (w, LEFT_MARGIN_AREA)
5594 + window_box_width (w, TEXT_AREA));
5595 else
5596 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5597 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5598 : 0);
5600 glyph = row->glyphs[area];
5601 end = glyph + row->used[area];
5602 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5603 x0 -= glyph->pixel_width;
5604 *x = glyph - row->glyphs[area];
5605 if (glyph < end)
5607 string = glyph->object;
5608 *charpos = glyph->charpos;
5609 *width = glyph->pixel_width;
5610 *height = glyph->ascent + glyph->descent;
5611 #ifdef HAVE_WINDOW_SYSTEM
5612 if (glyph->type == IMAGE_GLYPH)
5614 struct image *img;
5615 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5616 if (img != NULL)
5617 *object = img->spec;
5618 y0 -= row->ascent - glyph->ascent;
5619 x0 += glyph->slice.img.x;
5620 y0 += glyph->slice.img.y;
5622 #endif
5624 else
5626 /* Add extra (default width) columns if clicked after EOL. */
5627 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5628 *width = 0;
5629 *height = row->height;
5632 else
5634 x0 = 0;
5635 *x = 0;
5636 *width = *height = 0;
5639 *dx = x0;
5640 *dy = y0;
5642 return string;
5646 /***********************************************************************
5647 Changing Frame Sizes
5648 ***********************************************************************/
5650 #ifdef SIGWINCH
5652 SIGTYPE
5653 window_change_signal (int signalnum) /* If we don't have an argument, */
5654 /* some compilers complain in signal calls. */
5656 int width, height;
5657 int old_errno = errno;
5659 struct tty_display_info *tty;
5661 signal (SIGWINCH, window_change_signal);
5662 SIGNAL_THREAD_CHECK (signalnum);
5664 /* The frame size change obviously applies to a single
5665 termcap-controlled terminal, but we can't decide which.
5666 Therefore, we resize the frames corresponding to each tty.
5668 for (tty = tty_list; tty; tty = tty->next) {
5670 if (! tty->term_initted)
5671 continue;
5673 /* Suspended tty frames have tty->input == NULL avoid trying to
5674 use it. */
5675 if (!tty->input)
5676 continue;
5678 get_tty_size (fileno (tty->input), &width, &height);
5680 if (width > 5 && height > 2) {
5681 Lisp_Object tail, frame;
5683 FOR_EACH_FRAME (tail, frame)
5684 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5685 /* Record the new sizes, but don't reallocate the data
5686 structures now. Let that be done later outside of the
5687 signal handler. */
5688 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5692 errno = old_errno;
5694 #endif /* SIGWINCH */
5697 /* Do any change in frame size that was requested by a signal. SAFE
5698 non-zero means this function is called from a place where it is
5699 safe to change frame sizes while a redisplay is in progress. */
5701 void
5702 do_pending_window_change (int safe)
5704 /* If window_change_signal should have run before, run it now. */
5705 if (redisplaying_p && !safe)
5706 return;
5708 while (delayed_size_change)
5710 Lisp_Object tail, frame;
5712 delayed_size_change = 0;
5714 FOR_EACH_FRAME (tail, frame)
5716 struct frame *f = XFRAME (frame);
5718 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5719 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5720 0, 0, safe);
5726 /* Change the frame height and/or width. Values may be given as zero to
5727 indicate no change is to take place.
5729 If DELAY is non-zero, then assume we're being called from a signal
5730 handler, and queue the change for later - perhaps the next
5731 redisplay. Since this tries to resize windows, we can't call it
5732 from a signal handler.
5734 SAFE non-zero means this function is called from a place where it's
5735 safe to change frame sizes while a redisplay is in progress. */
5737 void
5738 change_frame_size (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5740 Lisp_Object tail, frame;
5742 if (FRAME_MSDOS_P (f))
5744 /* On MS-DOS, all frames use the same screen, so a change in
5745 size affects all frames. Termcap now supports multiple
5746 ttys. */
5747 FOR_EACH_FRAME (tail, frame)
5748 if (! FRAME_WINDOW_P (XFRAME (frame)))
5749 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5750 pretend, delay, safe);
5752 else
5753 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5756 static void
5757 change_frame_size_1 (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5759 int new_frame_total_cols;
5760 int count = SPECPDL_INDEX ();
5762 /* If we can't deal with the change now, queue it for later. */
5763 if (delay || (redisplaying_p && !safe))
5765 f->new_text_lines = newheight;
5766 f->new_text_cols = newwidth;
5767 delayed_size_change = 1;
5768 return;
5771 /* This size-change overrides any pending one for this frame. */
5772 f->new_text_lines = 0;
5773 f->new_text_cols = 0;
5775 /* If an argument is zero, set it to the current value. */
5776 if (newheight == 0)
5777 newheight = FRAME_LINES (f);
5778 if (newwidth == 0)
5779 newwidth = FRAME_COLS (f);
5781 /* Compute width of windows in F.
5782 This is the width of the frame without vertical scroll bars. */
5783 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5785 /* Round up to the smallest acceptable size. */
5786 check_frame_size (f, &newheight, &newwidth);
5788 /* If we're not changing the frame size, quit now. */
5789 /* Frame width may be unchanged but the text portion may change, for example,
5790 fullscreen and remove/add scroll bar. */
5791 if (newheight == FRAME_LINES (f)
5792 && newwidth == FRAME_COLS (f) // text portion unchanged
5793 && new_frame_total_cols == FRAME_TOTAL_COLS (f)) // frame width unchanged
5794 return;
5796 BLOCK_INPUT;
5798 #ifdef MSDOS
5799 /* We only can set screen dimensions to certain values supported
5800 by our video hardware. Try to find the smallest size greater
5801 or equal to the requested dimensions. */
5802 dos_set_window_size (&newheight, &newwidth);
5803 #endif
5805 if (newheight != FRAME_LINES (f))
5807 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
5809 /* Frame has both root and mini-buffer. */
5810 XSETFASTINT (XWINDOW (FRAME_ROOT_WINDOW (f))->top_line,
5811 FRAME_TOP_MARGIN (f));
5812 set_window_height (FRAME_ROOT_WINDOW (f),
5813 (newheight
5815 - FRAME_TOP_MARGIN (f)),
5817 XSETFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top_line,
5818 newheight - 1);
5819 set_window_height (FRAME_MINIBUF_WINDOW (f), 1, 0);
5821 else
5822 /* Frame has just one top-level window. */
5823 set_window_height (FRAME_ROOT_WINDOW (f),
5824 newheight - FRAME_TOP_MARGIN (f), 2);
5826 /* MSDOS frames cannot PRETEND, as they change frame size by
5827 manipulating video hardware. */
5828 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5829 FrameRows (FRAME_TTY (f)) = newheight;
5832 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5834 set_window_width (FRAME_ROOT_WINDOW (f), new_frame_total_cols, 2);
5835 if (FRAME_HAS_MINIBUF_P (f))
5836 set_window_width (FRAME_MINIBUF_WINDOW (f), new_frame_total_cols, 0);
5838 /* MSDOS frames cannot PRETEND, as they change frame size by
5839 manipulating video hardware. */
5840 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5841 FrameCols (FRAME_TTY (f)) = newwidth;
5843 if (WINDOWP (f->tool_bar_window))
5844 XSETFASTINT (XWINDOW (f->tool_bar_window)->total_cols, newwidth);
5847 FRAME_LINES (f) = newheight;
5848 SET_FRAME_COLS (f, newwidth);
5851 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5852 int text_area_x, text_area_y, text_area_width, text_area_height;
5854 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5855 &text_area_height);
5856 if (w->cursor.x >= text_area_x + text_area_width)
5857 w->cursor.hpos = w->cursor.x = 0;
5858 if (w->cursor.y >= text_area_y + text_area_height)
5859 w->cursor.vpos = w->cursor.y = 0;
5862 adjust_glyphs (f);
5863 calculate_costs (f);
5864 SET_FRAME_GARBAGED (f);
5865 f->resized_p = 1;
5867 UNBLOCK_INPUT;
5869 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5871 run_window_configuration_change_hook (f);
5873 unbind_to (count, Qnil);
5878 /***********************************************************************
5879 Terminal Related Lisp Functions
5880 ***********************************************************************/
5882 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5883 1, 1, "FOpen termscript file: ",
5884 doc: /* Start writing all terminal output to FILE as well as the terminal.
5885 FILE = nil means just close any termscript file currently open. */)
5886 (Lisp_Object file)
5888 struct tty_display_info *tty;
5890 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5891 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5892 error ("Current frame is not on a tty device");
5894 tty = CURTTY ();
5896 if (tty->termscript != 0)
5898 BLOCK_INPUT;
5899 fclose (tty->termscript);
5900 UNBLOCK_INPUT;
5902 tty->termscript = 0;
5904 if (! NILP (file))
5906 file = Fexpand_file_name (file, Qnil);
5907 tty->termscript = fopen (SDATA (file), "w");
5908 if (tty->termscript == 0)
5909 report_file_error ("Opening termscript", Fcons (file, Qnil));
5911 return Qnil;
5915 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5916 Ssend_string_to_terminal, 1, 2, 0,
5917 doc: /* Send STRING to the terminal without alteration.
5918 Control characters in STRING will have terminal-dependent effects.
5920 Optional parameter TERMINAL specifies the tty terminal device to use.
5921 It may be a terminal object, a frame, or nil for the terminal used by
5922 the currently selected frame. In batch mode, STRING is sent to stdout
5923 when TERMINAL is nil. */)
5924 (Lisp_Object string, Lisp_Object terminal)
5926 struct terminal *t = get_terminal (terminal, 1);
5927 FILE *out;
5929 /* ??? Perhaps we should do something special for multibyte strings here. */
5930 CHECK_STRING (string);
5931 BLOCK_INPUT;
5933 if (!t)
5934 error ("Unknown terminal device");
5936 if (t->type == output_initial)
5937 out = stdout;
5938 else if (t->type != output_termcap && t->type != output_msdos_raw)
5939 error ("Device %d is not a termcap terminal device", t->id);
5940 else
5942 struct tty_display_info *tty = t->display_info.tty;
5944 if (! tty->output)
5945 error ("Terminal is currently suspended");
5947 if (tty->termscript)
5949 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5950 fflush (tty->termscript);
5952 out = tty->output;
5954 fwrite (SDATA (string), 1, SBYTES (string), out);
5955 fflush (out);
5956 UNBLOCK_INPUT;
5957 return Qnil;
5961 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5962 doc: /* Beep, or flash the screen.
5963 Also, unless an argument is given,
5964 terminate any keyboard macro currently executing. */)
5965 (Lisp_Object arg)
5967 if (!NILP (arg))
5969 if (noninteractive)
5970 putchar (07);
5971 else
5972 ring_bell (XFRAME (selected_frame));
5974 else
5975 bitch_at_user ();
5977 return Qnil;
5980 void
5981 bitch_at_user (void)
5983 if (noninteractive)
5984 putchar (07);
5985 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5986 error ("Keyboard macro terminated by a command ringing the bell");
5987 else
5988 ring_bell (XFRAME (selected_frame));
5993 /***********************************************************************
5994 Sleeping, Waiting
5995 ***********************************************************************/
5997 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5998 doc: /* Pause, without updating display, for SECONDS seconds.
5999 SECONDS may be a floating-point value, meaning that you can wait for a
6000 fraction of a second. Optional second arg MILLISECONDS specifies an
6001 additional wait period, in milliseconds; this may be useful if your
6002 Emacs was built without floating point support.
6003 \(Not all operating systems support waiting for a fraction of a second.) */)
6004 (Lisp_Object seconds, Lisp_Object milliseconds)
6006 int sec, usec;
6008 if (NILP (milliseconds))
6009 XSETINT (milliseconds, 0);
6010 else
6011 CHECK_NUMBER (milliseconds);
6012 usec = XINT (milliseconds) * 1000;
6015 double duration = extract_float (seconds);
6016 sec = (int) duration;
6017 usec += (duration - sec) * 1000000;
6020 #ifndef EMACS_HAS_USECS
6021 if (sec == 0 && usec != 0)
6022 error ("Millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
6023 #endif
6025 /* Assure that 0 <= usec < 1000000. */
6026 if (usec < 0)
6028 /* We can't rely on the rounding being correct if usec is negative. */
6029 if (-1000000 < usec)
6030 sec--, usec += 1000000;
6031 else
6032 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
6034 else
6035 sec += usec / 1000000, usec %= 1000000;
6037 if (sec < 0 || (sec == 0 && usec == 0))
6038 return Qnil;
6040 wait_reading_process_output (sec, usec, 0, 0, Qnil, NULL, 0);
6042 return Qnil;
6046 /* This is just like wait_reading_process_output, except that
6047 it does redisplay.
6049 TIMEOUT is number of seconds to wait (float or integer),
6050 or t to wait forever.
6051 READING is 1 if reading input.
6052 If DO_DISPLAY is >0 display process output while waiting.
6053 If DO_DISPLAY is >1 perform an initial redisplay before waiting.
6056 Lisp_Object
6057 sit_for (Lisp_Object timeout, int reading, int do_display)
6059 int sec, usec;
6061 swallow_events (do_display);
6063 if ((detect_input_pending_run_timers (do_display))
6064 || !NILP (Vexecuting_kbd_macro))
6065 return Qnil;
6067 if (do_display >= 2)
6068 redisplay_preserve_echo_area (2);
6070 if (INTEGERP (timeout))
6072 sec = XINT (timeout);
6073 usec = 0;
6075 else if (FLOATP (timeout))
6077 double seconds = XFLOAT_DATA (timeout);
6078 sec = (int) seconds;
6079 usec = (int) ((seconds - sec) * 1000000);
6081 else if (EQ (timeout, Qt))
6083 sec = 0;
6084 usec = 0;
6086 else
6087 wrong_type_argument (Qnumberp, timeout);
6089 if (sec == 0 && usec == 0 && !EQ (timeout, Qt))
6090 return Qt;
6092 #ifdef SIGIO
6093 gobble_input (0);
6094 #endif
6096 wait_reading_process_output (sec, usec, reading ? -1 : 1, do_display,
6097 Qnil, NULL, 0);
6099 return detect_input_pending () ? Qnil : Qt;
6103 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
6104 doc: /* Perform redisplay if no input is available.
6105 If optional arg FORCE is non-nil or `redisplay-dont-pause' is non-nil,
6106 perform a full redisplay even if input is available.
6107 Return t if redisplay was performed, nil otherwise. */)
6108 (Lisp_Object force)
6110 int count;
6112 swallow_events (1);
6113 if ((detect_input_pending_run_timers (1)
6114 && NILP (force) && !redisplay_dont_pause)
6115 || !NILP (Vexecuting_kbd_macro))
6116 return Qnil;
6118 count = SPECPDL_INDEX ();
6119 if (!NILP (force) && !redisplay_dont_pause)
6120 specbind (Qredisplay_dont_pause, Qt);
6121 redisplay_preserve_echo_area (2);
6122 unbind_to (count, Qnil);
6123 return Qt;
6128 /***********************************************************************
6129 Other Lisp Functions
6130 ***********************************************************************/
6132 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
6133 session's frames, frame names, buffers, buffer-read-only flags, and
6134 buffer-modified-flags. */
6136 static Lisp_Object frame_and_buffer_state;
6139 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
6140 Sframe_or_buffer_changed_p, 0, 1, 0,
6141 doc: /* Return non-nil if the frame and buffer state appears to have changed.
6142 VARIABLE is a variable name whose value is either nil or a state vector
6143 that will be updated to contain all frames and buffers,
6144 aside from buffers whose names start with space,
6145 along with the buffers' read-only and modified flags. This allows a fast
6146 check to see whether buffer menus might need to be recomputed.
6147 If this function returns non-nil, it updates the internal vector to reflect
6148 the current state.
6150 If VARIABLE is nil, an internal variable is used. Users should not
6151 pass nil for VARIABLE. */)
6152 (Lisp_Object variable)
6154 Lisp_Object state, tail, frame, buf;
6155 Lisp_Object *vecp, *end;
6156 int n;
6158 if (! NILP (variable))
6160 CHECK_SYMBOL (variable);
6161 state = Fsymbol_value (variable);
6162 if (! VECTORP (state))
6163 goto changed;
6165 else
6166 state = frame_and_buffer_state;
6168 vecp = XVECTOR (state)->contents;
6169 end = vecp + XVECTOR (state)->size;
6171 FOR_EACH_FRAME (tail, frame)
6173 if (vecp == end)
6174 goto changed;
6175 if (!EQ (*vecp++, frame))
6176 goto changed;
6177 if (vecp == end)
6178 goto changed;
6179 if (!EQ (*vecp++, XFRAME (frame)->name))
6180 goto changed;
6182 /* Check that the buffer info matches. */
6183 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6185 buf = XCDR (XCAR (tail));
6186 /* Ignore buffers that aren't included in buffer lists. */
6187 if (SREF (XBUFFER (buf)->name, 0) == ' ')
6188 continue;
6189 if (vecp == end)
6190 goto changed;
6191 if (!EQ (*vecp++, buf))
6192 goto changed;
6193 if (vecp == end)
6194 goto changed;
6195 if (!EQ (*vecp++, XBUFFER (buf)->read_only))
6196 goto changed;
6197 if (vecp == end)
6198 goto changed;
6199 if (!EQ (*vecp++, Fbuffer_modified_p (buf)))
6200 goto changed;
6202 if (vecp == end)
6203 goto changed;
6204 /* Detect deletion of a buffer at the end of the list. */
6205 if (EQ (*vecp, Qlambda))
6206 return Qnil;
6208 /* Come here if we decide the data has changed. */
6209 changed:
6210 /* Count the size we will need.
6211 Start with 1 so there is room for at least one lambda at the end. */
6212 n = 1;
6213 FOR_EACH_FRAME (tail, frame)
6214 n += 2;
6215 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6216 n += 3;
6217 /* Reallocate the vector if data has grown to need it,
6218 or if it has shrunk a lot. */
6219 if (! VECTORP (state)
6220 || n > XVECTOR (state)->size
6221 || n + 20 < XVECTOR (state)->size / 2)
6222 /* Add 20 extra so we grow it less often. */
6224 state = Fmake_vector (make_number (n + 20), Qlambda);
6225 if (! NILP (variable))
6226 Fset (variable, state);
6227 else
6228 frame_and_buffer_state = state;
6231 /* Record the new data in the (possibly reallocated) vector. */
6232 vecp = XVECTOR (state)->contents;
6233 FOR_EACH_FRAME (tail, frame)
6235 *vecp++ = frame;
6236 *vecp++ = XFRAME (frame)->name;
6238 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6240 buf = XCDR (XCAR (tail));
6241 /* Ignore buffers that aren't included in buffer lists. */
6242 if (SREF (XBUFFER (buf)->name, 0) == ' ')
6243 continue;
6244 *vecp++ = buf;
6245 *vecp++ = XBUFFER (buf)->read_only;
6246 *vecp++ = Fbuffer_modified_p (buf);
6248 /* Fill up the vector with lambdas (always at least one). */
6249 *vecp++ = Qlambda;
6250 while (vecp - XVECTOR (state)->contents
6251 < XVECTOR (state)->size)
6252 *vecp++ = Qlambda;
6253 /* Make sure we didn't overflow the vector. */
6254 if (vecp - XVECTOR (state)->contents
6255 > XVECTOR (state)->size)
6256 abort ();
6257 return Qt;
6262 /***********************************************************************
6263 Initialization
6264 ***********************************************************************/
6266 /* Initialization done when Emacs fork is started, before doing stty.
6267 Determine terminal type and set terminal_driver. Then invoke its
6268 decoding routine to set up variables in the terminal package. */
6270 void
6271 init_display (void)
6273 char *terminal_type;
6275 /* Construct the space glyph. */
6276 space_glyph.type = CHAR_GLYPH;
6277 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
6278 space_glyph.charpos = -1;
6280 inverse_video = 0;
6281 cursor_in_echo_area = 0;
6282 terminal_type = (char *) 0;
6284 /* Now is the time to initialize this; it's used by init_sys_modes
6285 during startup. */
6286 Vinitial_window_system = Qnil;
6288 /* SIGWINCH needs to be handled no matter what display we start
6289 with. Otherwise newly opened tty frames will not resize
6290 automatically. */
6291 #ifdef SIGWINCH
6292 #ifndef CANNOT_DUMP
6293 if (initialized)
6294 #endif /* CANNOT_DUMP */
6295 signal (SIGWINCH, window_change_signal);
6296 #endif /* SIGWINCH */
6298 /* If running as a daemon, no need to initialize any frames/terminal. */
6299 if (IS_DAEMON)
6300 return;
6302 /* If the user wants to use a window system, we shouldn't bother
6303 initializing the terminal. This is especially important when the
6304 terminal is so dumb that emacs gives up before and doesn't bother
6305 using the window system.
6307 If the DISPLAY environment variable is set and nonempty,
6308 try to use X, and die with an error message if that doesn't work. */
6310 #ifdef HAVE_X_WINDOWS
6311 if (! inhibit_window_system && ! display_arg)
6313 char *display;
6314 display = getenv ("DISPLAY");
6315 display_arg = (display != 0 && *display != 0);
6317 if (display_arg && !x_display_ok (display))
6319 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6320 display);
6321 inhibit_window_system = 1;
6325 if (!inhibit_window_system && display_arg
6326 #ifndef CANNOT_DUMP
6327 && initialized
6328 #endif
6331 Vinitial_window_system = Qx;
6332 #ifdef HAVE_X11
6333 Vwindow_system_version = make_number (11);
6334 #endif
6335 #if defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
6336 /* In some versions of ncurses,
6337 tputs crashes if we have not called tgetent.
6338 So call tgetent. */
6339 { char b[2044]; tgetent (b, "xterm");}
6340 #endif
6341 adjust_frame_glyphs_initially ();
6342 return;
6344 #endif /* HAVE_X_WINDOWS */
6346 #ifdef HAVE_NTGUI
6347 if (!inhibit_window_system)
6349 Vinitial_window_system = Qw32;
6350 Vwindow_system_version = make_number (1);
6351 adjust_frame_glyphs_initially ();
6352 return;
6354 #endif /* HAVE_NTGUI */
6356 #ifdef HAVE_NS
6357 if (!inhibit_window_system
6358 #ifndef CANNOT_DUMP
6359 && initialized
6360 #endif
6363 Vinitial_window_system = Qns;
6364 Vwindow_system_version = make_number(10);
6365 adjust_frame_glyphs_initially ();
6366 return;
6368 #endif
6370 /* If no window system has been specified, try to use the terminal. */
6371 if (! isatty (0))
6373 fatal ("standard input is not a tty");
6374 exit (1);
6377 #ifdef WINDOWSNT
6378 terminal_type = "w32console";
6379 #else
6380 /* Look at the TERM variable. */
6381 terminal_type = (char *) getenv ("TERM");
6382 #endif
6383 if (!terminal_type)
6385 #ifdef HAVE_WINDOW_SYSTEM
6386 if (! inhibit_window_system)
6387 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6388 else
6389 #endif /* HAVE_WINDOW_SYSTEM */
6390 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6391 exit (1);
6395 struct terminal *t;
6396 struct frame *f = XFRAME (selected_frame);
6398 /* Open a display on the controlling tty. */
6399 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6401 /* Convert the initial frame to use the new display. */
6402 if (f->output_method != output_initial)
6403 abort ();
6404 f->output_method = t->type;
6405 f->terminal = t;
6407 t->reference_count++;
6408 #ifdef MSDOS
6409 f->output_data.tty->display_info = &the_only_display_info;
6410 #else
6411 if (f->output_method == output_termcap)
6412 create_tty_output (f);
6413 #endif
6414 t->display_info.tty->top_frame = selected_frame;
6415 change_frame_size (XFRAME (selected_frame),
6416 FrameRows (t->display_info.tty),
6417 FrameCols (t->display_info.tty), 0, 0, 1);
6419 /* Delete the initial terminal. */
6420 if (--initial_terminal->reference_count == 0
6421 && initial_terminal->delete_terminal_hook)
6422 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6424 /* Update frame parameters to reflect the new type. */
6425 Fmodify_frame_parameters
6426 (selected_frame, Fcons (Fcons (Qtty_type,
6427 Ftty_type (selected_frame)), Qnil));
6428 if (t->display_info.tty->name)
6429 Fmodify_frame_parameters (selected_frame,
6430 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6431 Qnil));
6432 else
6433 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6434 Qnil));
6438 struct frame *sf = SELECTED_FRAME ();
6439 int width = FRAME_TOTAL_COLS (sf);
6440 int height = FRAME_LINES (sf);
6442 unsigned int total_glyphs = height * (width + 2) * sizeof (struct glyph);
6444 /* If these sizes are so big they cause overflow, just ignore the
6445 change. It's not clear what better we could do. */
6446 if (total_glyphs / sizeof (struct glyph) / height != width + 2)
6447 fatal ("screen size %dx%d too big", width, height);
6450 adjust_frame_glyphs_initially ();
6451 calculate_costs (XFRAME (selected_frame));
6453 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6454 if (initialized
6455 && !noninteractive
6456 && NILP (Vinitial_window_system))
6458 /* For the initial frame, we don't have any way of knowing what
6459 are the foreground and background colors of the terminal. */
6460 struct frame *sf = SELECTED_FRAME();
6462 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6463 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6464 call0 (intern ("tty-set-up-initial-frame-faces"));
6470 /***********************************************************************
6471 Blinking cursor
6472 ***********************************************************************/
6474 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6475 Sinternal_show_cursor, 2, 2, 0,
6476 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6477 WINDOW nil means use the selected window. SHOW non-nil means
6478 show a cursor in WINDOW in the next redisplay. SHOW nil means
6479 don't show a cursor. */)
6480 (Lisp_Object window, Lisp_Object show)
6482 /* Don't change cursor state while redisplaying. This could confuse
6483 output routines. */
6484 if (!redisplaying_p)
6486 if (NILP (window))
6487 window = selected_window;
6488 else
6489 CHECK_WINDOW (window);
6491 XWINDOW (window)->cursor_off_p = NILP (show);
6494 return Qnil;
6498 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6499 Sinternal_show_cursor_p, 0, 1, 0,
6500 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6501 WINDOW nil or omitted means report on the selected window. */)
6502 (Lisp_Object window)
6504 struct window *w;
6506 if (NILP (window))
6507 window = selected_window;
6508 else
6509 CHECK_WINDOW (window);
6511 w = XWINDOW (window);
6512 return w->cursor_off_p ? Qnil : Qt;
6515 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6516 Slast_nonminibuf_frame, 0, 0, 0,
6517 doc: /* Value is last nonminibuffer frame. */)
6518 (void)
6520 Lisp_Object frame = Qnil;
6522 if (last_nonminibuf_frame)
6523 XSETFRAME (frame, last_nonminibuf_frame);
6525 return frame;
6528 /***********************************************************************
6529 Initialization
6530 ***********************************************************************/
6532 void
6533 syms_of_display (void)
6535 defsubr (&Sredraw_frame);
6536 defsubr (&Sredraw_display);
6537 defsubr (&Sframe_or_buffer_changed_p);
6538 defsubr (&Sopen_termscript);
6539 defsubr (&Sding);
6540 defsubr (&Sredisplay);
6541 defsubr (&Ssleep_for);
6542 defsubr (&Ssend_string_to_terminal);
6543 defsubr (&Sinternal_show_cursor);
6544 defsubr (&Sinternal_show_cursor_p);
6545 defsubr (&Slast_nonminibuf_frame);
6547 #if GLYPH_DEBUG
6548 defsubr (&Sdump_redisplay_history);
6549 #endif
6551 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6552 staticpro (&frame_and_buffer_state);
6554 Qdisplay_table = intern_c_string ("display-table");
6555 staticpro (&Qdisplay_table);
6556 Qredisplay_dont_pause = intern_c_string ("redisplay-dont-pause");
6557 staticpro (&Qredisplay_dont_pause);
6559 DEFVAR_INT ("baud-rate", &baud_rate,
6560 doc: /* *The output baud rate of the terminal.
6561 On most systems, changing this value will affect the amount of padding
6562 and the other strategic decisions made during redisplay. */);
6564 DEFVAR_BOOL ("inverse-video", &inverse_video,
6565 doc: /* *Non-nil means invert the entire frame display.
6566 This means everything is in inverse video which otherwise would not be. */);
6568 DEFVAR_BOOL ("visible-bell", &visible_bell,
6569 doc: /* *Non-nil means try to flash the frame to represent a bell.
6571 See also `ring-bell-function'. */);
6573 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
6574 doc: /* *Non-nil means no need to redraw entire frame after suspending.
6575 A non-nil value is useful if the terminal can automatically preserve
6576 Emacs's frame display when you reenter Emacs.
6577 It is up to you to set this variable if your terminal can do that. */);
6579 DEFVAR_LISP ("initial-window-system", &Vinitial_window_system,
6580 doc: /* Name of the window system that Emacs uses for the first frame.
6581 The value is a symbol:
6582 nil for a termcap frame (a character-only terminal),
6583 'x' for an Emacs frame that is really an X window,
6584 'w32' for an Emacs frame that is a window on MS-Windows display,
6585 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6586 'pc' for a direct-write MS-DOS frame.
6588 Use of this variable as a boolean is deprecated. Instead,
6589 use `display-graphic-p' or any of the other `display-*-p'
6590 predicates which report frame's specific UI-related capabilities. */);
6592 DEFVAR_KBOARD ("window-system", Vwindow_system,
6593 doc: /* Name of window system through which the selected frame is displayed.
6594 The value is a symbol:
6595 nil for a termcap frame (a character-only terminal),
6596 'x' for an Emacs frame that is really an X window,
6597 'w32' for an Emacs frame that is a window on MS-Windows display,
6598 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6599 'pc' for a direct-write MS-DOS frame.
6601 Use of this variable as a boolean is deprecated. Instead,
6602 use `display-graphic-p' or any of the other `display-*-p'
6603 predicates which report frame's specific UI-related capabilities. */);
6605 DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
6606 doc: /* The version number of the window system in use.
6607 For X windows, this is 11. */);
6609 DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
6610 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6612 DEFVAR_LISP ("glyph-table", &Vglyph_table,
6613 doc: /* Table defining how to output a glyph code to the frame.
6614 If not nil, this is a vector indexed by glyph code to define the glyph.
6615 Each element can be:
6616 integer: a glyph code which this glyph is an alias for.
6617 string: output this glyph using that string (not impl. in X windows).
6618 nil: this glyph mod 524288 is the code of a character to output,
6619 and this glyph / 524288 is the face number (see `face-id') to use
6620 while outputting it. */);
6621 Vglyph_table = Qnil;
6623 DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
6624 doc: /* Display table to use for buffers that specify none.
6625 See `buffer-display-table' for more information. */);
6626 Vstandard_display_table = Qnil;
6628 DEFVAR_BOOL ("redisplay-dont-pause", &redisplay_dont_pause,
6629 doc: /* *Non-nil means update isn't paused when input is detected. */);
6630 redisplay_dont_pause = 0;
6632 #if PERIODIC_PREEMPTION_CHECKING
6633 DEFVAR_LISP ("redisplay-preemption-period", &Vredisplay_preemption_period,
6634 doc: /* *The period in seconds between checking for input during redisplay.
6635 If input is detected, redisplay is pre-empted, and the input is processed.
6636 If nil, never pre-empt redisplay. */);
6637 Vredisplay_preemption_period = make_float (0.10);
6638 #endif
6640 #ifdef CANNOT_DUMP
6641 if (noninteractive)
6642 #endif
6644 Vinitial_window_system = Qnil;
6645 Vwindow_system_version = Qnil;
6649 /* arch-tag: 8d812b1f-04a2-4195-a9c4-381f8457a413
6650 (do not change this comment) */