Nuke arch-tags.
[emacs.git] / src / dispnew.c
blobfa2e3b5c5238a17d7d7d85bfd654d250f55fd789
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>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
31 #include "lisp.h"
32 #include "termchar.h"
33 #include "termopts.h"
34 /* cm.h must come after dispextern.h on Windows. */
35 #include "dispextern.h"
36 #include "cm.h"
37 #include "buffer.h"
38 #include "character.h"
39 #include "keyboard.h"
40 #include "frame.h"
41 #include "termhooks.h"
42 #include "window.h"
43 #include "commands.h"
44 #include "disptab.h"
45 #include "indent.h"
46 #include "intervals.h"
47 #include "blockinput.h"
48 #include "process.h"
50 #include "syssignal.h"
52 #ifdef HAVE_X_WINDOWS
53 #include "xterm.h"
54 #endif /* HAVE_X_WINDOWS */
56 #ifdef HAVE_NTGUI
57 #include "w32term.h"
58 #endif /* HAVE_NTGUI */
60 #ifdef HAVE_NS
61 #include "nsterm.h"
62 #endif
64 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
66 #include "systime.h"
67 #include <errno.h>
69 /* Get number of chars of output now in the buffer of a stdio stream.
70 This ought to be built in in stdio, but it isn't. Some s- files
71 override this because their stdio internals differ. */
73 #ifdef __GNU_LIBRARY__
75 /* The s- file might have overridden the definition with one that
76 works for the system's C library. But we are using the GNU C
77 library, so this is the right definition for every system. */
79 #ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
80 #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
81 #else
82 #undef PENDING_OUTPUT_COUNT
83 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
84 #endif
85 #else /* not __GNU_LIBRARY__ */
86 #if !defined (PENDING_OUTPUT_COUNT) && HAVE_STDIO_EXT_H && HAVE___FPENDING
87 #include <stdio_ext.h>
88 #define PENDING_OUTPUT_COUNT(FILE) __fpending (FILE)
89 #endif
90 #ifndef PENDING_OUTPUT_COUNT
91 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
92 #endif
93 #endif /* not __GNU_LIBRARY__ */
95 #if defined(HAVE_TERM_H) && defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
96 #include <term.h> /* for tgetent */
97 #endif
99 /* Structure to pass dimensions around. Used for character bounding
100 boxes, glyph matrix dimensions and alike. */
102 struct dim
104 int width;
105 int height;
109 /* Function prototypes. */
111 static struct glyph_matrix *save_current_matrix (struct frame *);
112 static void restore_current_matrix (struct frame *, struct glyph_matrix *);
113 static int showing_window_margins_p (struct window *);
114 static void fake_current_matrices (Lisp_Object);
115 static void redraw_overlapping_rows (struct window *, int);
116 static void redraw_overlapped_rows (struct window *, int);
117 static int count_blanks (struct glyph *, int);
118 static int count_match (struct glyph *, struct glyph *,
119 struct glyph *, struct glyph *);
120 static unsigned line_draw_cost (struct glyph_matrix *, int);
121 static void update_frame_line (struct frame *, int);
122 static struct dim allocate_matrices_for_frame_redisplay
123 (Lisp_Object, int, int, int, int *);
124 static int required_matrix_height (struct window *);
125 static int required_matrix_width (struct window *);
126 static void allocate_matrices_for_window_redisplay (struct window *);
127 static int realloc_glyph_pool (struct glyph_pool *, struct dim);
128 static void adjust_frame_glyphs (struct frame *);
129 struct glyph_matrix *new_glyph_matrix (struct glyph_pool *);
130 static void free_glyph_matrix (struct glyph_matrix *);
131 static void adjust_glyph_matrix (struct window *, struct glyph_matrix *,
132 int, int, struct dim);
133 static void change_frame_size_1 (struct frame *, int, int, int, int, int);
134 static void swap_glyph_pointers (struct glyph_row *, struct glyph_row *);
135 #if GLYPH_DEBUG
136 static int glyph_row_slice_p (struct glyph_row *, struct glyph_row *);
137 #endif
138 static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
139 static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
140 struct window *);
141 static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
142 struct window *);
143 static struct glyph_pool *new_glyph_pool (void);
144 static void free_glyph_pool (struct glyph_pool *);
145 static void adjust_frame_glyphs_initially (void);
146 static void adjust_frame_message_buffer (struct frame *);
147 static void adjust_decode_mode_spec_buffer (struct frame *);
148 static void fill_up_glyph_row_with_spaces (struct glyph_row *);
149 static void build_frame_matrix (struct frame *);
150 void clear_current_matrices (struct frame *);
151 void scroll_glyph_matrix_range (struct glyph_matrix *, int, int,
152 int, int);
153 static void clear_window_matrices (struct window *, int);
154 static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
155 static int scrolling_window (struct window *, int);
156 static int update_window_line (struct window *, int, int *);
157 static void update_marginal_area (struct window *, int, int);
158 static int update_text_area (struct window *, int);
159 static void make_current (struct glyph_matrix *, struct glyph_matrix *,
160 int);
161 static void mirror_make_current (struct window *, int);
162 void check_window_matrix_pointers (struct window *);
163 #if GLYPH_DEBUG
164 static void check_matrix_pointers (struct glyph_matrix *,
165 struct glyph_matrix *);
166 #endif
167 static void mirror_line_dance (struct window *, int, int, int *, char *);
168 static int update_window_tree (struct window *, int);
169 static int update_window (struct window *, int);
170 static int update_frame_1 (struct frame *, int, int);
171 static void set_window_cursor_after_update (struct window *);
172 static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
173 static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
176 /* Non-zero means don't pause redisplay for pending input. (This is
177 for debugging and for a future implementation of EDT-like
178 scrolling. */
180 int redisplay_dont_pause;
182 /* Define PERIODIC_PREEMPTION_CHECKING to 1, if micro-second timers
183 are supported, so we can check for input during redisplay at
184 regular intervals. */
185 #ifdef EMACS_HAS_USECS
186 #define PERIODIC_PREEMPTION_CHECKING 1
187 #else
188 #define PERIODIC_PREEMPTION_CHECKING 0
189 #endif
191 #if PERIODIC_PREEMPTION_CHECKING
193 /* If a number (float), check for user input every N seconds. */
195 Lisp_Object Vredisplay_preemption_period;
197 /* Redisplay preemption timers. */
199 static EMACS_TIME preemption_period;
200 static EMACS_TIME preemption_next_check;
202 #endif
204 /* Nonzero upon entry to redisplay means do not assume anything about
205 current contents of actual terminal frame; clear and redraw it. */
207 int frame_garbaged;
209 /* Nonzero means last display completed. Zero means it was preempted. */
211 int display_completed;
213 /* Lisp variable visible-bell; enables use of screen-flash instead of
214 audible bell. */
216 int visible_bell;
218 /* Invert the color of the whole frame, at a low level. */
220 int inverse_video;
222 /* Line speed of the terminal. */
224 EMACS_INT baud_rate;
226 /* Either nil or a symbol naming the window system under which Emacs
227 creates the first frame. */
229 Lisp_Object Vinitial_window_system;
231 /* Version number of X windows: 10, 11 or nil. */
233 Lisp_Object Vwindow_system_version;
235 /* Vector of glyph definitions. Indexed by glyph number, the contents
236 are a string which is how to output the glyph.
238 If Vglyph_table is nil, a glyph is output by using its low 8 bits
239 as a character code.
241 This is an obsolete feature that is no longer used. The variable
242 is retained for compatibility. */
244 Lisp_Object Vglyph_table;
246 /* Display table to use for vectors that don't specify their own. */
248 Lisp_Object Vstandard_display_table;
250 /* Nonzero means reading single-character input with prompt so put
251 cursor on mini-buffer after the prompt. Positive means at end of
252 text in echo area; negative means at beginning of line. */
254 int cursor_in_echo_area;
256 Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
259 /* The currently selected frame. In a single-frame version, this
260 variable always equals the_only_frame. */
262 Lisp_Object selected_frame;
264 /* A frame which is not just a mini-buffer, or 0 if there are no such
265 frames. This is usually the most recent such frame that was
266 selected. In a single-frame version, this variable always holds
267 the address of the_only_frame. */
269 struct frame *last_nonminibuf_frame;
271 /* 1 means SIGWINCH happened when not safe. */
273 int delayed_size_change;
275 /* 1 means glyph initialization has been completed at startup. */
277 static int glyphs_initialized_initially_p;
279 /* Updated window if != 0. Set by update_window. */
281 struct window *updated_window;
283 /* Glyph row updated in update_window_line, and area that is updated. */
285 struct glyph_row *updated_row;
286 int updated_area;
288 /* A glyph for a space. */
290 struct glyph space_glyph;
292 /* Counts of allocated structures. These counts serve to diagnose
293 memory leaks and double frees. */
295 int glyph_matrix_count;
296 int glyph_pool_count;
298 /* If non-null, the frame whose frame matrices are manipulated. If
299 null, window matrices are worked on. */
301 static struct frame *frame_matrix_frame;
303 /* Non-zero means that fonts have been loaded since the last glyph
304 matrix adjustments. Redisplay must stop, and glyph matrices must
305 be adjusted when this flag becomes non-zero during display. The
306 reason fonts can be loaded so late is that fonts of fontsets are
307 loaded on demand. Another reason is that a line contains many
308 characters displayed by zero width or very narrow glyphs of
309 variable-width fonts. */
311 int fonts_changed_p;
313 /* Convert vpos and hpos from frame to window and vice versa.
314 This may only be used for terminal frames. */
316 #if GLYPH_DEBUG
318 static int window_to_frame_vpos (struct window *, int);
319 static int window_to_frame_hpos (struct window *, int);
320 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
321 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
323 /* One element of the ring buffer containing redisplay history
324 information. */
326 struct redisplay_history
328 char trace[512 + 100];
331 /* The size of the history buffer. */
333 #define REDISPLAY_HISTORY_SIZE 30
335 /* The redisplay history buffer. */
337 static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
339 /* Next free entry in redisplay_history. */
341 static int history_idx;
343 /* A tick that's incremented each time something is added to the
344 history. */
346 static unsigned history_tick;
348 static void add_frame_display_history (struct frame *, int);
349 static void add_window_display_history (struct window *, char *, int);
351 /* Add to the redisplay history how window W has been displayed.
352 MSG is a trace containing the information how W's glyph matrix
353 has been constructed. PAUSED_P non-zero means that the update
354 has been interrupted for pending input. */
356 static void
357 add_window_display_history (struct window *w, char *msg, int paused_p)
359 char *buf;
361 if (history_idx >= REDISPLAY_HISTORY_SIZE)
362 history_idx = 0;
363 buf = redisplay_history[history_idx].trace;
364 ++history_idx;
366 sprintf (buf, "%d: window %p (`%s')%s\n",
367 history_tick++,
369 ((BUFFERP (w->buffer)
370 && STRINGP (XBUFFER (w->buffer)->name))
371 ? (char *) SDATA (XBUFFER (w->buffer)->name)
372 : "???"),
373 paused_p ? " ***paused***" : "");
374 strcat (buf, msg);
378 /* Add to the redisplay history that frame F has been displayed.
379 PAUSED_P non-zero means that the update has been interrupted for
380 pending input. */
382 static void
383 add_frame_display_history (struct frame *f, int paused_p)
385 char *buf;
387 if (history_idx >= REDISPLAY_HISTORY_SIZE)
388 history_idx = 0;
389 buf = redisplay_history[history_idx].trace;
390 ++history_idx;
392 sprintf (buf, "%d: update frame %p%s",
393 history_tick++,
394 f, paused_p ? " ***paused***" : "");
398 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
399 Sdump_redisplay_history, 0, 0, "",
400 doc: /* Dump redisplay history to stderr. */)
401 (void)
403 int i;
405 for (i = history_idx - 1; i != history_idx; --i)
407 if (i < 0)
408 i = REDISPLAY_HISTORY_SIZE - 1;
409 fprintf (stderr, "%s\n", redisplay_history[i].trace);
412 return Qnil;
416 #else /* GLYPH_DEBUG == 0 */
418 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
419 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
421 #endif /* GLYPH_DEBUG == 0 */
424 #if defined PROFILING && !HAVE___EXECUTABLE_START
425 /* FIXME: only used to find text start for profiling. */
427 void
428 safe_bcopy (const char *from, char *to, int size)
430 abort ();
432 #endif
434 /***********************************************************************
435 Glyph Matrices
436 ***********************************************************************/
438 /* Allocate and return a glyph_matrix structure. POOL is the glyph
439 pool from which memory for the matrix should be allocated, or null
440 for window-based redisplay where no glyph pools are used. The
441 member `pool' of the glyph matrix structure returned is set to
442 POOL, the structure is otherwise zeroed. */
444 struct glyph_matrix *
445 new_glyph_matrix (struct glyph_pool *pool)
447 struct glyph_matrix *result;
449 /* Allocate and clear. */
450 result = (struct glyph_matrix *) xmalloc (sizeof *result);
451 memset (result, 0, sizeof *result);
453 /* Increment number of allocated matrices. This count is used
454 to detect memory leaks. */
455 ++glyph_matrix_count;
457 /* Set pool and return. */
458 result->pool = pool;
459 return result;
463 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
465 The global counter glyph_matrix_count is decremented when a matrix
466 is freed. If the count gets negative, more structures were freed
467 than allocated, i.e. one matrix was freed more than once or a bogus
468 pointer was passed to this function.
470 If MATRIX->pool is null, this means that the matrix manages its own
471 glyph memory---this is done for matrices on X frames. Freeing the
472 matrix also frees the glyph memory in this case. */
474 static void
475 free_glyph_matrix (struct glyph_matrix *matrix)
477 if (matrix)
479 int i;
481 /* Detect the case that more matrices are freed than were
482 allocated. */
483 if (--glyph_matrix_count < 0)
484 abort ();
486 /* Free glyph memory if MATRIX owns it. */
487 if (matrix->pool == NULL)
488 for (i = 0; i < matrix->rows_allocated; ++i)
489 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
491 /* Free row structures and the matrix itself. */
492 xfree (matrix->rows);
493 xfree (matrix);
498 /* Return the number of glyphs to reserve for a marginal area of
499 window W. TOTAL_GLYPHS is the number of glyphs in a complete
500 display line of window W. MARGIN gives the width of the marginal
501 area in canonical character units. MARGIN should be an integer
502 or a float. */
504 static int
505 margin_glyphs_to_reserve (struct window *w, int total_glyphs, Lisp_Object margin)
507 int n;
509 if (NUMBERP (margin))
511 int width = XFASTINT (w->total_cols);
512 double d = max (0, XFLOATINT (margin));
513 d = min (width / 2 - 1, d);
514 n = (int) ((double) total_glyphs / width * d);
516 else
517 n = 0;
519 return n;
523 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
524 window sizes.
526 W is null if the function is called for a frame glyph matrix.
527 Otherwise it is the window MATRIX is a member of. X and Y are the
528 indices of the first column and row of MATRIX within the frame
529 matrix, if such a matrix exists. They are zero for purely
530 window-based redisplay. DIM is the needed size of the matrix.
532 In window-based redisplay, where no frame matrices exist, glyph
533 matrices manage their own glyph storage. Otherwise, they allocate
534 storage from a common frame glyph pool which can be found in
535 MATRIX->pool.
537 The reason for this memory management strategy is to avoid complete
538 frame redraws if possible. When we allocate from a common pool, a
539 change of the location or size of a sub-matrix within the pool
540 requires a complete redisplay of the frame because we cannot easily
541 make sure that the current matrices of all windows still agree with
542 what is displayed on the screen. While this is usually fast, it
543 leads to screen flickering. */
545 static void
546 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
548 int i;
549 int new_rows;
550 int marginal_areas_changed_p = 0;
551 int header_line_changed_p = 0;
552 int header_line_p = 0;
553 int left = -1, right = -1;
554 int window_width = -1, window_height = -1;
556 /* See if W had a header line that has disappeared now, or vice versa.
557 Get W's size. */
558 if (w)
560 window_box (w, -1, 0, 0, &window_width, &window_height);
562 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
563 header_line_changed_p = header_line_p != matrix->header_line_p;
565 matrix->header_line_p = header_line_p;
567 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
568 Do nothing if MATRIX' size, position, vscroll, and marginal areas
569 haven't changed. This optimization is important because preserving
570 the matrix means preventing redisplay. */
571 if (matrix->pool == NULL)
573 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
574 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
575 xassert (left >= 0 && right >= 0);
576 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
577 || right != matrix->right_margin_glyphs);
579 if (!marginal_areas_changed_p
580 && !fonts_changed_p
581 && !header_line_changed_p
582 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
583 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
584 && matrix->window_height == window_height
585 && matrix->window_vscroll == w->vscroll
586 && matrix->window_width == window_width)
587 return;
590 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
591 if (matrix->rows_allocated < dim.height)
593 int size = dim.height * sizeof (struct glyph_row);
594 new_rows = dim.height - matrix->rows_allocated;
595 matrix->rows = (struct glyph_row *) xrealloc (matrix->rows, size);
596 memset (matrix->rows + matrix->rows_allocated, 0,
597 new_rows * sizeof *matrix->rows);
598 matrix->rows_allocated = dim.height;
600 else
601 new_rows = 0;
603 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
604 on a frame not using window-based redisplay. Set up pointers for
605 each row into the glyph pool. */
606 if (matrix->pool)
608 xassert (matrix->pool->glyphs);
610 if (w)
612 left = margin_glyphs_to_reserve (w, dim.width,
613 w->left_margin_cols);
614 right = margin_glyphs_to_reserve (w, dim.width,
615 w->right_margin_cols);
617 else
618 left = right = 0;
620 for (i = 0; i < dim.height; ++i)
622 struct glyph_row *row = &matrix->rows[i];
624 row->glyphs[LEFT_MARGIN_AREA]
625 = (matrix->pool->glyphs
626 + (y + i) * matrix->pool->ncolumns
627 + x);
629 if (w == NULL
630 || row == matrix->rows + dim.height - 1
631 || (row == matrix->rows && matrix->header_line_p))
633 row->glyphs[TEXT_AREA]
634 = row->glyphs[LEFT_MARGIN_AREA];
635 row->glyphs[RIGHT_MARGIN_AREA]
636 = row->glyphs[TEXT_AREA] + dim.width;
637 row->glyphs[LAST_AREA]
638 = row->glyphs[RIGHT_MARGIN_AREA];
640 else
642 row->glyphs[TEXT_AREA]
643 = row->glyphs[LEFT_MARGIN_AREA] + left;
644 row->glyphs[RIGHT_MARGIN_AREA]
645 = row->glyphs[TEXT_AREA] + dim.width - left - right;
646 row->glyphs[LAST_AREA]
647 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
651 matrix->left_margin_glyphs = left;
652 matrix->right_margin_glyphs = right;
654 else
656 /* If MATRIX->pool is null, MATRIX is responsible for managing
657 its own memory. It is a window matrix for window-based redisplay.
658 Allocate glyph memory from the heap. */
659 if (dim.width > matrix->matrix_w
660 || new_rows
661 || header_line_changed_p
662 || marginal_areas_changed_p)
664 struct glyph_row *row = matrix->rows;
665 struct glyph_row *end = row + matrix->rows_allocated;
667 while (row < end)
669 row->glyphs[LEFT_MARGIN_AREA]
670 = (struct glyph *) xrealloc (row->glyphs[LEFT_MARGIN_AREA],
671 (dim.width
672 * sizeof (struct glyph)));
674 /* The mode line never has marginal areas. */
675 if (row == matrix->rows + dim.height - 1
676 || (row == matrix->rows && matrix->header_line_p))
678 row->glyphs[TEXT_AREA]
679 = row->glyphs[LEFT_MARGIN_AREA];
680 row->glyphs[RIGHT_MARGIN_AREA]
681 = row->glyphs[TEXT_AREA] + dim.width;
682 row->glyphs[LAST_AREA]
683 = row->glyphs[RIGHT_MARGIN_AREA];
685 else
687 row->glyphs[TEXT_AREA]
688 = row->glyphs[LEFT_MARGIN_AREA] + left;
689 row->glyphs[RIGHT_MARGIN_AREA]
690 = row->glyphs[TEXT_AREA] + dim.width - left - right;
691 row->glyphs[LAST_AREA]
692 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
694 ++row;
698 xassert (left >= 0 && right >= 0);
699 matrix->left_margin_glyphs = left;
700 matrix->right_margin_glyphs = right;
703 /* Number of rows to be used by MATRIX. */
704 matrix->nrows = dim.height;
705 xassert (matrix->nrows >= 0);
707 if (w)
709 if (matrix == w->current_matrix)
711 /* Mark rows in a current matrix of a window as not having
712 valid contents. It's important to not do this for
713 desired matrices. When Emacs starts, it may already be
714 building desired matrices when this function runs. */
715 if (window_width < 0)
716 window_width = window_box_width (w, -1);
718 /* Optimize the case that only the height has changed (C-x 2,
719 upper window). Invalidate all rows that are no longer part
720 of the window. */
721 if (!marginal_areas_changed_p
722 && !header_line_changed_p
723 && new_rows == 0
724 && dim.width == matrix->matrix_w
725 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
726 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
727 && matrix->window_width == window_width)
729 /* Find the last row in the window. */
730 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
731 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
733 ++i;
734 break;
737 /* Window end is invalid, if inside of the rows that
738 are invalidated below. */
739 if (INTEGERP (w->window_end_vpos)
740 && XFASTINT (w->window_end_vpos) >= i)
741 w->window_end_valid = Qnil;
743 while (i < matrix->nrows)
744 matrix->rows[i++].enabled_p = 0;
746 else
748 for (i = 0; i < matrix->nrows; ++i)
749 matrix->rows[i].enabled_p = 0;
752 else if (matrix == w->desired_matrix)
754 /* Rows in desired matrices always have to be cleared;
755 redisplay expects this is the case when it runs, so it
756 had better be the case when we adjust matrices between
757 redisplays. */
758 for (i = 0; i < matrix->nrows; ++i)
759 matrix->rows[i].enabled_p = 0;
764 /* Remember last values to be able to optimize frame redraws. */
765 matrix->matrix_x = x;
766 matrix->matrix_y = y;
767 matrix->matrix_w = dim.width;
768 matrix->matrix_h = dim.height;
770 /* Record the top y location and height of W at the time the matrix
771 was last adjusted. This is used to optimize redisplay above. */
772 if (w)
774 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
775 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
776 matrix->window_height = window_height;
777 matrix->window_width = window_width;
778 matrix->window_vscroll = w->vscroll;
783 /* Reverse the contents of rows in MATRIX between START and END. The
784 contents of the row at END - 1 end up at START, END - 2 at START +
785 1 etc. This is part of the implementation of rotate_matrix (see
786 below). */
788 static void
789 reverse_rows (struct glyph_matrix *matrix, int start, int end)
791 int i, j;
793 for (i = start, j = end - 1; i < j; ++i, --j)
795 /* Non-ISO HP/UX compiler doesn't like auto struct
796 initialization. */
797 struct glyph_row temp;
798 temp = matrix->rows[i];
799 matrix->rows[i] = matrix->rows[j];
800 matrix->rows[j] = temp;
805 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
806 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
807 indices. (Note: this does not copy glyphs, only glyph pointers in
808 row structures are moved around).
810 The algorithm used for rotating the vector was, I believe, first
811 described by Kernighan. See the vector R as consisting of two
812 sub-vectors AB, where A has length BY for BY >= 0. The result
813 after rotating is then BA. Reverse both sub-vectors to get ArBr
814 and reverse the result to get (ArBr)r which is BA. Similar for
815 rotating right. */
817 void
818 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
820 if (by < 0)
822 /* Up (rotate left, i.e. towards lower indices). */
823 by = -by;
824 reverse_rows (matrix, first, first + by);
825 reverse_rows (matrix, first + by, last);
826 reverse_rows (matrix, first, last);
828 else if (by > 0)
830 /* Down (rotate right, i.e. towards higher indices). */
831 reverse_rows (matrix, last - by, last);
832 reverse_rows (matrix, first, last - by);
833 reverse_rows (matrix, first, last);
838 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
839 with indices START <= index < END. Increment positions by DELTA/
840 DELTA_BYTES. */
842 void
843 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
844 EMACS_INT delta, EMACS_INT delta_bytes)
846 /* Check that START and END are reasonable values. */
847 xassert (start >= 0 && start <= matrix->nrows);
848 xassert (end >= 0 && end <= matrix->nrows);
849 xassert (start <= end);
851 for (; start < end; ++start)
852 increment_row_positions (matrix->rows + start, delta, delta_bytes);
856 /* Enable a range of rows in glyph matrix MATRIX. START and END are
857 the row indices of the first and last + 1 row to enable. If
858 ENABLED_P is non-zero, enabled_p flags in rows will be set to 1. */
860 void
861 enable_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end, int enabled_p)
863 xassert (start <= end);
864 xassert (start >= 0 && start < matrix->nrows);
865 xassert (end >= 0 && end <= matrix->nrows);
867 for (; start < end; ++start)
868 matrix->rows[start].enabled_p = enabled_p != 0;
872 /* Clear MATRIX.
874 This empties all rows in MATRIX by setting the enabled_p flag for
875 all rows of the matrix to zero. The function prepare_desired_row
876 will eventually really clear a row when it sees one with a zero
877 enabled_p flag.
879 Resets update hints to defaults value. The only update hint
880 currently present is the flag MATRIX->no_scrolling_p. */
882 void
883 clear_glyph_matrix (struct glyph_matrix *matrix)
885 if (matrix)
887 enable_glyph_matrix_rows (matrix, 0, matrix->nrows, 0);
888 matrix->no_scrolling_p = 0;
893 /* Shift part of the glyph matrix MATRIX of window W up or down.
894 Increment y-positions in glyph rows between START and END by DY,
895 and recompute their visible height. */
897 void
898 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
900 int min_y, max_y;
902 xassert (start <= end);
903 xassert (start >= 0 && start < matrix->nrows);
904 xassert (end >= 0 && end <= matrix->nrows);
906 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
907 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
909 for (; start < end; ++start)
911 struct glyph_row *row = &matrix->rows[start];
913 row->y += dy;
914 row->visible_height = row->height;
916 if (row->y < min_y)
917 row->visible_height -= min_y - row->y;
918 if (row->y + row->height > max_y)
919 row->visible_height -= row->y + row->height - max_y;
924 /* Mark all rows in current matrices of frame F as invalid. Marking
925 invalid is done by setting enabled_p to zero for all rows in a
926 current matrix. */
928 void
929 clear_current_matrices (register struct frame *f)
931 /* Clear frame current matrix, if we have one. */
932 if (f->current_matrix)
933 clear_glyph_matrix (f->current_matrix);
935 /* Clear the matrix of the menu bar window, if such a window exists.
936 The menu bar window is currently used to display menus on X when
937 no toolkit support is compiled in. */
938 if (WINDOWP (f->menu_bar_window))
939 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
941 /* Clear the matrix of the tool-bar window, if any. */
942 if (WINDOWP (f->tool_bar_window))
943 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
945 /* Clear current window matrices. */
946 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
947 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
951 /* Clear out all display lines of F for a coming redisplay. */
953 void
954 clear_desired_matrices (register struct frame *f)
956 if (f->desired_matrix)
957 clear_glyph_matrix (f->desired_matrix);
959 if (WINDOWP (f->menu_bar_window))
960 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
962 if (WINDOWP (f->tool_bar_window))
963 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
965 /* Do it for window matrices. */
966 xassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
967 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
971 /* Clear matrices in window tree rooted in W. If DESIRED_P is
972 non-zero clear desired matrices, otherwise clear current matrices. */
974 static void
975 clear_window_matrices (struct window *w, int desired_p)
977 while (w)
979 if (!NILP (w->hchild))
981 xassert (WINDOWP (w->hchild));
982 clear_window_matrices (XWINDOW (w->hchild), desired_p);
984 else if (!NILP (w->vchild))
986 xassert (WINDOWP (w->vchild));
987 clear_window_matrices (XWINDOW (w->vchild), desired_p);
989 else
991 if (desired_p)
992 clear_glyph_matrix (w->desired_matrix);
993 else
995 clear_glyph_matrix (w->current_matrix);
996 w->window_end_valid = Qnil;
1000 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1006 /***********************************************************************
1007 Glyph Rows
1009 See dispextern.h for an overall explanation of glyph rows.
1010 ***********************************************************************/
1012 /* Clear glyph row ROW. Do it in a way that makes it robust against
1013 changes in the glyph_row structure, i.e. addition or removal of
1014 structure members. */
1016 static struct glyph_row null_row;
1018 void
1019 clear_glyph_row (struct glyph_row *row)
1021 struct glyph *p[1 + LAST_AREA];
1023 /* Save pointers. */
1024 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
1025 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
1026 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
1027 p[LAST_AREA] = row->glyphs[LAST_AREA];
1029 /* Clear. */
1030 *row = null_row;
1032 /* Restore pointers. */
1033 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
1034 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
1035 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
1036 row->glyphs[LAST_AREA] = p[LAST_AREA];
1038 #if 0 /* At some point, some bit-fields of struct glyph were not set,
1039 which made glyphs unequal when compared with GLYPH_EQUAL_P.
1040 Redisplay outputs such glyphs, and flickering effects were
1041 the result. This also depended on the contents of memory
1042 returned by xmalloc. If flickering happens again, activate
1043 the code below. If the flickering is gone with that, chances
1044 are that the flickering has the same reason as here. */
1045 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
1046 #endif
1050 /* Make ROW an empty, enabled row of canonical character height,
1051 in window W starting at y-position Y. */
1053 void
1054 blank_row (struct window *w, struct glyph_row *row, int y)
1056 int min_y, max_y;
1058 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
1059 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
1061 clear_glyph_row (row);
1062 row->y = y;
1063 row->ascent = row->phys_ascent = 0;
1064 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
1065 row->visible_height = row->height;
1067 if (row->y < min_y)
1068 row->visible_height -= min_y - row->y;
1069 if (row->y + row->height > max_y)
1070 row->visible_height -= row->y + row->height - max_y;
1072 row->enabled_p = 1;
1076 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
1077 are the amounts by which to change positions. Note that the first
1078 glyph of the text area of a row can have a buffer position even if
1079 the used count of the text area is zero. Such rows display line
1080 ends. */
1082 void
1083 increment_row_positions (struct glyph_row *row,
1084 EMACS_INT delta, EMACS_INT delta_bytes)
1086 int area, i;
1088 /* Increment start and end positions. */
1089 MATRIX_ROW_START_CHARPOS (row) += delta;
1090 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
1091 MATRIX_ROW_END_CHARPOS (row) += delta;
1092 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
1093 CHARPOS (row->start.pos) += delta;
1094 BYTEPOS (row->start.pos) += delta_bytes;
1095 CHARPOS (row->end.pos) += delta;
1096 BYTEPOS (row->end.pos) += delta_bytes;
1098 if (!row->enabled_p)
1099 return;
1101 /* Increment positions in glyphs. */
1102 for (area = 0; area < LAST_AREA; ++area)
1103 for (i = 0; i < row->used[area]; ++i)
1104 if (BUFFERP (row->glyphs[area][i].object)
1105 && row->glyphs[area][i].charpos > 0)
1106 row->glyphs[area][i].charpos += delta;
1108 /* Capture the case of rows displaying a line end. */
1109 if (row->used[TEXT_AREA] == 0
1110 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
1111 row->glyphs[TEXT_AREA]->charpos += delta;
1115 #if 0
1116 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
1117 contents, i.e. glyph structure contents are exchanged between A and
1118 B without changing glyph pointers in A and B. */
1120 static void
1121 swap_glyphs_in_rows (a, b)
1122 struct glyph_row *a, *b;
1124 int area;
1126 for (area = 0; area < LAST_AREA; ++area)
1128 /* Number of glyphs to swap. */
1129 int max_used = max (a->used[area], b->used[area]);
1131 /* Start of glyphs in area of row A. */
1132 struct glyph *glyph_a = a->glyphs[area];
1134 /* End + 1 of glyphs in area of row A. */
1135 struct glyph *glyph_a_end = a->glyphs[max_used];
1137 /* Start of glyphs in area of row B. */
1138 struct glyph *glyph_b = b->glyphs[area];
1140 while (glyph_a < glyph_a_end)
1142 /* Non-ISO HP/UX compiler doesn't like auto struct
1143 initialization. */
1144 struct glyph temp;
1145 temp = *glyph_a;
1146 *glyph_a = *glyph_b;
1147 *glyph_b = temp;
1148 ++glyph_a;
1149 ++glyph_b;
1154 #endif /* 0 */
1156 /* Exchange pointers to glyph memory between glyph rows A and B. */
1158 static INLINE void
1159 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1161 int i;
1162 for (i = 0; i < LAST_AREA + 1; ++i)
1164 struct glyph *temp = a->glyphs[i];
1165 a->glyphs[i] = b->glyphs[i];
1166 b->glyphs[i] = temp;
1171 /* Copy glyph row structure FROM to glyph row structure TO, except
1172 that glyph pointers in the structures are left unchanged. */
1174 static INLINE void
1175 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1177 struct glyph *pointers[1 + LAST_AREA];
1179 /* Save glyph pointers of TO. */
1180 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1182 /* Do a structure assignment. */
1183 *to = *from;
1185 /* Restore original pointers of TO. */
1186 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1190 /* Copy contents of glyph row FROM to glyph row TO. Glyph pointers in
1191 TO and FROM are left unchanged. Glyph contents are copied from the
1192 glyph memory of FROM to the glyph memory of TO. Increment buffer
1193 positions in row TO by DELTA/ DELTA_BYTES. */
1195 void
1196 copy_glyph_row_contents (struct glyph_row *to, struct glyph_row *from,
1197 EMACS_INT delta, EMACS_INT delta_bytes)
1199 int area;
1201 /* This is like a structure assignment TO = FROM, except that
1202 glyph pointers in the rows are left unchanged. */
1203 copy_row_except_pointers (to, from);
1205 /* Copy glyphs from FROM to TO. */
1206 for (area = 0; area < LAST_AREA; ++area)
1207 if (from->used[area])
1208 memcpy (to->glyphs[area], from->glyphs[area],
1209 from->used[area] * sizeof (struct glyph));
1211 /* Increment buffer positions in TO by DELTA. */
1212 increment_row_positions (to, delta, delta_bytes);
1216 /* Assign glyph row FROM to glyph row TO. This works like a structure
1217 assignment TO = FROM, except that glyph pointers are not copied but
1218 exchanged between TO and FROM. Pointers must be exchanged to avoid
1219 a memory leak. */
1221 static INLINE void
1222 assign_row (struct glyph_row *to, struct glyph_row *from)
1224 swap_glyph_pointers (to, from);
1225 copy_row_except_pointers (to, from);
1229 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1230 a row in a window matrix, is a slice of the glyph memory of the
1231 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1232 is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
1233 memory of FRAME_ROW. */
1235 #if GLYPH_DEBUG
1237 static int
1238 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1240 struct glyph *window_glyph_start = window_row->glyphs[0];
1241 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1242 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1244 return (frame_glyph_start <= window_glyph_start
1245 && window_glyph_start < frame_glyph_end);
1248 #endif /* GLYPH_DEBUG */
1250 #if 0
1252 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1253 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1254 in WINDOW_MATRIX is found satisfying the condition. */
1256 static struct glyph_row *
1257 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1258 struct glyph_matrix *frame_matrix, int row)
1260 int i;
1262 xassert (row >= 0 && row < frame_matrix->nrows);
1264 for (i = 0; i < window_matrix->nrows; ++i)
1265 if (glyph_row_slice_p (window_matrix->rows + i,
1266 frame_matrix->rows + row))
1267 break;
1269 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1272 #endif /* 0 */
1274 /* Prepare ROW for display. Desired rows are cleared lazily,
1275 i.e. they are only marked as to be cleared by setting their
1276 enabled_p flag to zero. When a row is to be displayed, a prior
1277 call to this function really clears it. */
1279 void
1280 prepare_desired_row (struct glyph_row *row)
1282 if (!row->enabled_p)
1284 unsigned rp = row->reversed_p;
1286 clear_glyph_row (row);
1287 row->enabled_p = 1;
1288 row->reversed_p = rp;
1293 /* Return a hash code for glyph row ROW. */
1295 static int
1296 line_hash_code (struct glyph_row *row)
1298 int hash = 0;
1300 if (row->enabled_p)
1302 struct glyph *glyph = row->glyphs[TEXT_AREA];
1303 struct glyph *end = glyph + row->used[TEXT_AREA];
1305 while (glyph < end)
1307 int c = glyph->u.ch;
1308 int face_id = glyph->face_id;
1309 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1310 c -= SPACEGLYPH;
1311 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1312 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1313 ++glyph;
1316 if (hash == 0)
1317 hash = 1;
1320 return hash;
1324 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1325 the number of characters in the line. If must_write_spaces is
1326 zero, leading and trailing spaces are ignored. */
1328 static unsigned int
1329 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1331 struct glyph_row *row = matrix->rows + vpos;
1332 struct glyph *beg = row->glyphs[TEXT_AREA];
1333 struct glyph *end = beg + row->used[TEXT_AREA];
1334 int len;
1335 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1336 int glyph_table_len = GLYPH_TABLE_LENGTH;
1338 /* Ignore trailing and leading spaces if we can. */
1339 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1341 /* Skip from the end over trailing spaces. */
1342 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1343 --end;
1345 /* All blank line. */
1346 if (end == beg)
1347 return 0;
1349 /* Skip over leading spaces. */
1350 while (CHAR_GLYPH_SPACE_P (*beg))
1351 ++beg;
1354 /* If we don't have a glyph-table, each glyph is one character,
1355 so return the number of glyphs. */
1356 if (glyph_table_base == 0)
1357 len = end - beg;
1358 else
1360 /* Otherwise, scan the glyphs and accumulate their total length
1361 in LEN. */
1362 len = 0;
1363 while (beg < end)
1365 GLYPH g;
1367 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1369 if (GLYPH_INVALID_P (g)
1370 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1371 len += 1;
1372 else
1373 len += GLYPH_LENGTH (glyph_table_base, g);
1375 ++beg;
1379 return len;
1383 /* Test two glyph rows A and B for equality. Value is non-zero if A
1384 and B have equal contents. W is the window to which the glyphs
1385 rows A and B belong. It is needed here to test for partial row
1386 visibility. MOUSE_FACE_P non-zero means compare the mouse_face_p
1387 flags of A and B, too. */
1389 static INLINE int
1390 row_equal_p (struct window *w, struct glyph_row *a, struct glyph_row *b, int mouse_face_p)
1392 if (a == b)
1393 return 1;
1394 else if (a->hash != b->hash)
1395 return 0;
1396 else
1398 struct glyph *a_glyph, *b_glyph, *a_end;
1399 int area;
1401 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1402 return 0;
1404 /* Compare glyphs. */
1405 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1407 if (a->used[area] != b->used[area])
1408 return 0;
1410 a_glyph = a->glyphs[area];
1411 a_end = a_glyph + a->used[area];
1412 b_glyph = b->glyphs[area];
1414 while (a_glyph < a_end
1415 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1416 ++a_glyph, ++b_glyph;
1418 if (a_glyph != a_end)
1419 return 0;
1422 if (a->fill_line_p != b->fill_line_p
1423 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1424 || a->left_fringe_bitmap != b->left_fringe_bitmap
1425 || a->left_fringe_face_id != b->left_fringe_face_id
1426 || a->right_fringe_bitmap != b->right_fringe_bitmap
1427 || a->right_fringe_face_id != b->right_fringe_face_id
1428 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1429 || a->exact_window_width_line_p != b->exact_window_width_line_p
1430 || a->overlapped_p != b->overlapped_p
1431 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1432 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1433 || a->reversed_p != b->reversed_p
1434 /* Different partially visible characters on left margin. */
1435 || a->x != b->x
1436 /* Different height. */
1437 || a->ascent != b->ascent
1438 || a->phys_ascent != b->phys_ascent
1439 || a->phys_height != b->phys_height
1440 || a->visible_height != b->visible_height)
1441 return 0;
1444 return 1;
1449 /***********************************************************************
1450 Glyph Pool
1452 See dispextern.h for an overall explanation of glyph pools.
1453 ***********************************************************************/
1455 /* Allocate a glyph_pool structure. The structure returned is
1456 initialized with zeros. The global variable glyph_pool_count is
1457 incremented for each pool allocated. */
1459 static struct glyph_pool *
1460 new_glyph_pool (void)
1462 struct glyph_pool *result;
1464 /* Allocate a new glyph_pool and clear it. */
1465 result = (struct glyph_pool *) xmalloc (sizeof *result);
1466 memset (result, 0, sizeof *result);
1468 /* For memory leak and double deletion checking. */
1469 ++glyph_pool_count;
1471 return result;
1475 /* Free a glyph_pool structure POOL. The function may be called with
1476 a null POOL pointer. The global variable glyph_pool_count is
1477 decremented with every pool structure freed. If this count gets
1478 negative, more structures were freed than allocated, i.e. one
1479 structure must have been freed more than once or a bogus pointer
1480 was passed to free_glyph_pool. */
1482 static void
1483 free_glyph_pool (struct glyph_pool *pool)
1485 if (pool)
1487 /* More freed than allocated? */
1488 --glyph_pool_count;
1489 xassert (glyph_pool_count >= 0);
1491 xfree (pool->glyphs);
1492 xfree (pool);
1497 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1498 columns we need. This function never shrinks a pool. The only
1499 case in which this would make sense, would be when a frame's size
1500 is changed from a large value to a smaller one. But, if someone
1501 does it once, we can expect that he will do it again.
1503 Value is non-zero if the pool changed in a way which makes
1504 re-adjusting window glyph matrices necessary. */
1506 static int
1507 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1509 int needed;
1510 int changed_p;
1512 changed_p = (pool->glyphs == 0
1513 || matrix_dim.height != pool->nrows
1514 || matrix_dim.width != pool->ncolumns);
1516 /* Enlarge the glyph pool. */
1517 needed = matrix_dim.width * matrix_dim.height;
1518 if (needed > pool->nglyphs)
1520 int size = needed * sizeof (struct glyph);
1522 if (pool->glyphs)
1524 pool->glyphs = (struct glyph *) xrealloc (pool->glyphs, size);
1525 memset (pool->glyphs + pool->nglyphs, 0,
1526 size - pool->nglyphs * sizeof (struct glyph));
1528 else
1530 pool->glyphs = (struct glyph *) xmalloc (size);
1531 memset (pool->glyphs, 0, size);
1534 pool->nglyphs = needed;
1537 /* Remember the number of rows and columns because (a) we use them
1538 to do sanity checks, and (b) the number of columns determines
1539 where rows in the frame matrix start---this must be available to
1540 determine pointers to rows of window sub-matrices. */
1541 pool->nrows = matrix_dim.height;
1542 pool->ncolumns = matrix_dim.width;
1544 return changed_p;
1549 /***********************************************************************
1550 Debug Code
1551 ***********************************************************************/
1553 #if GLYPH_DEBUG
1556 /* Flush standard output. This is sometimes useful to call from the debugger.
1557 XXX Maybe this should be changed to flush the current terminal instead of
1558 stdout.
1561 void
1562 flush_stdout (void)
1564 fflush (stdout);
1568 /* Check that no glyph pointers have been lost in MATRIX. If a
1569 pointer has been lost, e.g. by using a structure assignment between
1570 rows, at least one pointer must occur more than once in the rows of
1571 MATRIX. */
1573 void
1574 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1576 int i, j;
1578 for (i = 0; i < matrix->nrows; ++i)
1579 for (j = 0; j < matrix->nrows; ++j)
1580 xassert (i == j
1581 || (matrix->rows[i].glyphs[TEXT_AREA]
1582 != matrix->rows[j].glyphs[TEXT_AREA]));
1586 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1588 struct glyph_row *
1589 matrix_row (struct glyph_matrix *matrix, int row)
1591 xassert (matrix && matrix->rows);
1592 xassert (row >= 0 && row < matrix->nrows);
1594 /* That's really too slow for normal testing because this function
1595 is called almost everywhere. Although---it's still astonishingly
1596 fast, so it is valuable to have for debugging purposes. */
1597 #if 0
1598 check_matrix_pointer_lossage (matrix);
1599 #endif
1601 return matrix->rows + row;
1605 #if 0 /* This function makes invalid assumptions when text is
1606 partially invisible. But it might come handy for debugging
1607 nevertheless. */
1609 /* Check invariants that must hold for an up to date current matrix of
1610 window W. */
1612 static void
1613 check_matrix_invariants (struct window *w)
1615 struct glyph_matrix *matrix = w->current_matrix;
1616 int yb = window_text_bottom_y (w);
1617 struct glyph_row *row = matrix->rows;
1618 struct glyph_row *last_text_row = NULL;
1619 struct buffer *saved = current_buffer;
1620 struct buffer *buffer = XBUFFER (w->buffer);
1621 int c;
1623 /* This can sometimes happen for a fresh window. */
1624 if (matrix->nrows < 2)
1625 return;
1627 set_buffer_temp (buffer);
1629 /* Note: last row is always reserved for the mode line. */
1630 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1631 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1633 struct glyph_row *next = row + 1;
1635 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1636 last_text_row = row;
1638 /* Check that character and byte positions are in sync. */
1639 xassert (MATRIX_ROW_START_BYTEPOS (row)
1640 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1641 xassert (BYTEPOS (row->start.pos)
1642 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1644 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1645 have such a position temporarily in case of a minibuffer
1646 displaying something like `[Sole completion]' at its end. */
1647 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1649 xassert (MATRIX_ROW_END_BYTEPOS (row)
1650 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1651 xassert (BYTEPOS (row->end.pos)
1652 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1655 /* Check that end position of `row' is equal to start position
1656 of next row. */
1657 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1659 xassert (MATRIX_ROW_END_CHARPOS (row)
1660 == MATRIX_ROW_START_CHARPOS (next));
1661 xassert (MATRIX_ROW_END_BYTEPOS (row)
1662 == MATRIX_ROW_START_BYTEPOS (next));
1663 xassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1664 xassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1666 row = next;
1669 xassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1670 xassert (w->desired_matrix->rows != NULL);
1671 set_buffer_temp (saved);
1674 #endif /* 0 */
1676 #endif /* GLYPH_DEBUG != 0 */
1680 /**********************************************************************
1681 Allocating/ Adjusting Glyph Matrices
1682 **********************************************************************/
1684 /* Allocate glyph matrices over a window tree for a frame-based
1685 redisplay
1687 X and Y are column/row within the frame glyph matrix where
1688 sub-matrices for the window tree rooted at WINDOW must be
1689 allocated. DIM_ONLY_P non-zero means that the caller of this
1690 function is only interested in the result matrix dimension, and
1691 matrix adjustments should not be performed.
1693 The function returns the total width/height of the sub-matrices of
1694 the window tree. If called on a frame root window, the computation
1695 will take the mini-buffer window into account.
1697 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1699 NEW_LEAF_MATRIX set if any window in the tree did not have a
1700 glyph matrices yet, and
1702 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1703 any window in the tree will be changed or have been changed (see
1704 DIM_ONLY_P)
1706 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1707 function.
1709 Windows are arranged into chains of windows on the same level
1710 through the next fields of window structures. Such a level can be
1711 either a sequence of horizontally adjacent windows from left to
1712 right, or a sequence of vertically adjacent windows from top to
1713 bottom. Each window in a horizontal sequence can be either a leaf
1714 window or a vertical sequence; a window in a vertical sequence can
1715 be either a leaf or a horizontal sequence. All windows in a
1716 horizontal sequence have the same height, and all windows in a
1717 vertical sequence have the same width.
1719 This function uses, for historical reasons, a more general
1720 algorithm to determine glyph matrix dimensions that would be
1721 necessary.
1723 The matrix height of a horizontal sequence is determined by the
1724 maximum height of any matrix in the sequence. The matrix width of
1725 a horizontal sequence is computed by adding up matrix widths of
1726 windows in the sequence.
1728 |<------- result width ------->|
1729 +---------+----------+---------+ ---
1730 | | | | |
1731 | | | |
1732 +---------+ | | result height
1733 | +---------+
1734 | | |
1735 +----------+ ---
1737 The matrix width of a vertical sequence is the maximum matrix width
1738 of any window in the sequence. Its height is computed by adding up
1739 matrix heights of windows in the sequence.
1741 |<---- result width -->|
1742 +---------+ ---
1743 | | |
1744 | | |
1745 +---------+--+ |
1746 | | |
1747 | | result height
1749 +------------+---------+ |
1750 | | |
1751 | | |
1752 +------------+---------+ --- */
1754 /* Bit indicating that a new matrix will be allocated or has been
1755 allocated. */
1757 #define NEW_LEAF_MATRIX (1 << 0)
1759 /* Bit indicating that a matrix will or has changed its location or
1760 size. */
1762 #define CHANGED_LEAF_MATRIX (1 << 1)
1764 static struct dim
1765 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1766 int dim_only_p, int *window_change_flags)
1768 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1769 int x0 = x, y0 = y;
1770 int wmax = 0, hmax = 0;
1771 struct dim total;
1772 struct dim dim;
1773 struct window *w;
1774 int in_horz_combination_p;
1776 /* What combination is WINDOW part of? Compute this once since the
1777 result is the same for all windows in the `next' chain. The
1778 special case of a root window (parent equal to nil) is treated
1779 like a vertical combination because a root window's `next'
1780 points to the mini-buffer window, if any, which is arranged
1781 vertically below other windows. */
1782 in_horz_combination_p
1783 = (!NILP (XWINDOW (window)->parent)
1784 && !NILP (XWINDOW (XWINDOW (window)->parent)->hchild));
1786 /* For WINDOW and all windows on the same level. */
1789 w = XWINDOW (window);
1791 /* Get the dimension of the window sub-matrix for W, depending
1792 on whether this is a combination or a leaf window. */
1793 if (!NILP (w->hchild))
1794 dim = allocate_matrices_for_frame_redisplay (w->hchild, x, y,
1795 dim_only_p,
1796 window_change_flags);
1797 else if (!NILP (w->vchild))
1798 dim = allocate_matrices_for_frame_redisplay (w->vchild, x, y,
1799 dim_only_p,
1800 window_change_flags);
1801 else
1803 /* If not already done, allocate sub-matrix structures. */
1804 if (w->desired_matrix == NULL)
1806 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1807 w->current_matrix = new_glyph_matrix (f->current_pool);
1808 *window_change_flags |= NEW_LEAF_MATRIX;
1811 /* Width and height MUST be chosen so that there are no
1812 holes in the frame matrix. */
1813 dim.width = required_matrix_width (w);
1814 dim.height = required_matrix_height (w);
1816 /* Will matrix be re-allocated? */
1817 if (x != w->desired_matrix->matrix_x
1818 || y != w->desired_matrix->matrix_y
1819 || dim.width != w->desired_matrix->matrix_w
1820 || dim.height != w->desired_matrix->matrix_h
1821 || (margin_glyphs_to_reserve (w, dim.width,
1822 w->left_margin_cols)
1823 != w->desired_matrix->left_margin_glyphs)
1824 || (margin_glyphs_to_reserve (w, dim.width,
1825 w->right_margin_cols)
1826 != w->desired_matrix->right_margin_glyphs))
1827 *window_change_flags |= CHANGED_LEAF_MATRIX;
1829 /* Actually change matrices, if allowed. Do not consider
1830 CHANGED_LEAF_MATRIX computed above here because the pool
1831 may have been changed which we don't now here. We trust
1832 that we only will be called with DIM_ONLY_P != 0 when
1833 necessary. */
1834 if (!dim_only_p)
1836 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1837 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1841 /* If we are part of a horizontal combination, advance x for
1842 windows to the right of W; otherwise advance y for windows
1843 below W. */
1844 if (in_horz_combination_p)
1845 x += dim.width;
1846 else
1847 y += dim.height;
1849 /* Remember maximum glyph matrix dimensions. */
1850 wmax = max (wmax, dim.width);
1851 hmax = max (hmax, dim.height);
1853 /* Next window on same level. */
1854 window = w->next;
1856 while (!NILP (window));
1858 /* Set `total' to the total glyph matrix dimension of this window
1859 level. In a vertical combination, the width is the width of the
1860 widest window; the height is the y we finally reached, corrected
1861 by the y we started with. In a horizontal combination, the total
1862 height is the height of the tallest window, and the width is the
1863 x we finally reached, corrected by the x we started with. */
1864 if (in_horz_combination_p)
1866 total.width = x - x0;
1867 total.height = hmax;
1869 else
1871 total.width = wmax;
1872 total.height = y - y0;
1875 return total;
1879 /* Return the required height of glyph matrices for window W. */
1881 static int
1882 required_matrix_height (struct window *w)
1884 #ifdef HAVE_WINDOW_SYSTEM
1885 struct frame *f = XFRAME (w->frame);
1887 if (FRAME_WINDOW_P (f))
1889 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1890 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1891 return (((window_pixel_height + ch_height - 1)
1892 / ch_height) * w->nrows_scale_factor
1893 /* One partially visible line at the top and
1894 bottom of the window. */
1896 /* 2 for header and mode line. */
1897 + 2);
1899 #endif /* HAVE_WINDOW_SYSTEM */
1901 return WINDOW_TOTAL_LINES (w);
1905 /* Return the required width of glyph matrices for window W. */
1907 static int
1908 required_matrix_width (struct window *w)
1910 #ifdef HAVE_WINDOW_SYSTEM
1911 struct frame *f = XFRAME (w->frame);
1912 if (FRAME_WINDOW_P (f))
1914 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1915 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1917 /* Compute number of glyphs needed in a glyph row. */
1918 return (((window_pixel_width + ch_width - 1)
1919 / ch_width) * w->ncols_scale_factor
1920 /* 2 partially visible columns in the text area. */
1922 /* One partially visible column at the right
1923 edge of each marginal area. */
1924 + 1 + 1);
1926 #endif /* HAVE_WINDOW_SYSTEM */
1928 return XINT (w->total_cols);
1932 /* Allocate window matrices for window-based redisplay. W is the
1933 window whose matrices must be allocated/reallocated. */
1935 static void
1936 allocate_matrices_for_window_redisplay (struct window *w)
1938 while (w)
1940 if (!NILP (w->vchild))
1941 allocate_matrices_for_window_redisplay (XWINDOW (w->vchild));
1942 else if (!NILP (w->hchild))
1943 allocate_matrices_for_window_redisplay (XWINDOW (w->hchild));
1944 else
1946 /* W is a leaf window. */
1947 struct dim dim;
1949 /* If matrices are not yet allocated, allocate them now. */
1950 if (w->desired_matrix == NULL)
1952 w->desired_matrix = new_glyph_matrix (NULL);
1953 w->current_matrix = new_glyph_matrix (NULL);
1956 dim.width = required_matrix_width (w);
1957 dim.height = required_matrix_height (w);
1958 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1959 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1962 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1967 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1968 do it for all frames; otherwise do it just for the given frame.
1969 This function must be called when a new frame is created, its size
1970 changes, or its window configuration changes. */
1972 void
1973 adjust_glyphs (struct frame *f)
1975 /* Block input so that expose events and other events that access
1976 glyph matrices are not processed while we are changing them. */
1977 BLOCK_INPUT;
1979 if (f)
1980 adjust_frame_glyphs (f);
1981 else
1983 Lisp_Object tail, lisp_frame;
1985 FOR_EACH_FRAME (tail, lisp_frame)
1986 adjust_frame_glyphs (XFRAME (lisp_frame));
1989 UNBLOCK_INPUT;
1993 /* Adjust frame glyphs when Emacs is initialized.
1995 To be called from init_display.
1997 We need a glyph matrix because redraw will happen soon.
1998 Unfortunately, window sizes on selected_frame are not yet set to
1999 meaningful values. I believe we can assume that there are only two
2000 windows on the frame---the mini-buffer and the root window. Frame
2001 height and width seem to be correct so far. So, set the sizes of
2002 windows to estimated values. */
2004 static void
2005 adjust_frame_glyphs_initially (void)
2007 struct frame *sf = SELECTED_FRAME ();
2008 struct window *root = XWINDOW (sf->root_window);
2009 struct window *mini = XWINDOW (root->next);
2010 int frame_lines = FRAME_LINES (sf);
2011 int frame_cols = FRAME_COLS (sf);
2012 int top_margin = FRAME_TOP_MARGIN (sf);
2014 /* Do it for the root window. */
2015 XSETFASTINT (root->top_line, top_margin);
2016 XSETFASTINT (root->total_cols, frame_cols);
2017 set_window_height (sf->root_window, frame_lines - 1 - top_margin, 0);
2019 /* Do it for the mini-buffer window. */
2020 XSETFASTINT (mini->top_line, frame_lines - 1);
2021 XSETFASTINT (mini->total_cols, frame_cols);
2022 set_window_height (root->next, 1, 0);
2024 adjust_frame_glyphs (sf);
2025 glyphs_initialized_initially_p = 1;
2029 /* Allocate/reallocate glyph matrices of a single frame F. */
2031 static void
2032 adjust_frame_glyphs (struct frame *f)
2034 if (FRAME_WINDOW_P (f))
2035 adjust_frame_glyphs_for_window_redisplay (f);
2036 else
2037 adjust_frame_glyphs_for_frame_redisplay (f);
2039 /* Don't forget the message buffer and the buffer for
2040 decode_mode_spec. */
2041 adjust_frame_message_buffer (f);
2042 adjust_decode_mode_spec_buffer (f);
2044 f->glyphs_initialized_p = 1;
2047 /* Return 1 if any window in the tree has nonzero window margins. See
2048 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
2049 static int
2050 showing_window_margins_p (struct window *w)
2052 while (w)
2054 if (!NILP (w->hchild))
2056 if (showing_window_margins_p (XWINDOW (w->hchild)))
2057 return 1;
2059 else if (!NILP (w->vchild))
2061 if (showing_window_margins_p (XWINDOW (w->vchild)))
2062 return 1;
2064 else if (!NILP (w->left_margin_cols)
2065 || !NILP (w->right_margin_cols))
2066 return 1;
2068 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2070 return 0;
2074 /* In the window tree with root W, build current matrices of leaf
2075 windows from the frame's current matrix. */
2077 static void
2078 fake_current_matrices (Lisp_Object window)
2080 struct window *w;
2082 for (; !NILP (window); window = w->next)
2084 w = XWINDOW (window);
2086 if (!NILP (w->hchild))
2087 fake_current_matrices (w->hchild);
2088 else if (!NILP (w->vchild))
2089 fake_current_matrices (w->vchild);
2090 else
2092 int i;
2093 struct frame *f = XFRAME (w->frame);
2094 struct glyph_matrix *m = w->current_matrix;
2095 struct glyph_matrix *fm = f->current_matrix;
2097 xassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
2098 xassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
2100 for (i = 0; i < m->matrix_h; ++i)
2102 struct glyph_row *r = m->rows + i;
2103 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
2105 xassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
2106 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
2108 r->enabled_p = fr->enabled_p;
2109 if (r->enabled_p)
2111 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
2112 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
2113 r->used[TEXT_AREA] = (m->matrix_w
2114 - r->used[LEFT_MARGIN_AREA]
2115 - r->used[RIGHT_MARGIN_AREA]);
2116 r->mode_line_p = 0;
2124 /* Save away the contents of frame F's current frame matrix. Value is
2125 a glyph matrix holding the contents of F's current frame matrix. */
2127 static struct glyph_matrix *
2128 save_current_matrix (struct frame *f)
2130 int i;
2131 struct glyph_matrix *saved;
2133 saved = (struct glyph_matrix *) xmalloc (sizeof *saved);
2134 memset (saved, 0, sizeof *saved);
2135 saved->nrows = f->current_matrix->nrows;
2136 saved->rows = (struct glyph_row *) xmalloc (saved->nrows
2137 * sizeof *saved->rows);
2138 memset (saved->rows, 0, saved->nrows * sizeof *saved->rows);
2140 for (i = 0; i < saved->nrows; ++i)
2142 struct glyph_row *from = f->current_matrix->rows + i;
2143 struct glyph_row *to = saved->rows + i;
2144 size_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2145 to->glyphs[TEXT_AREA] = (struct glyph *) xmalloc (nbytes);
2146 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2147 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2150 return saved;
2154 /* Restore the contents of frame F's current frame matrix from SAVED,
2155 and free memory associated with SAVED. */
2157 static void
2158 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
2160 int i;
2162 for (i = 0; i < saved->nrows; ++i)
2164 struct glyph_row *from = saved->rows + i;
2165 struct glyph_row *to = f->current_matrix->rows + i;
2166 size_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
2167 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
2168 to->used[TEXT_AREA] = from->used[TEXT_AREA];
2169 xfree (from->glyphs[TEXT_AREA]);
2172 xfree (saved->rows);
2173 xfree (saved);
2178 /* Allocate/reallocate glyph matrices of a single frame F for
2179 frame-based redisplay. */
2181 static void
2182 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
2184 struct dim matrix_dim;
2185 int pool_changed_p;
2186 int window_change_flags;
2187 int top_window_y;
2189 if (!FRAME_LIVE_P (f))
2190 return;
2192 top_window_y = FRAME_TOP_MARGIN (f);
2194 /* Allocate glyph pool structures if not already done. */
2195 if (f->desired_pool == NULL)
2197 f->desired_pool = new_glyph_pool ();
2198 f->current_pool = new_glyph_pool ();
2201 /* Allocate frames matrix structures if needed. */
2202 if (f->desired_matrix == NULL)
2204 f->desired_matrix = new_glyph_matrix (f->desired_pool);
2205 f->current_matrix = new_glyph_matrix (f->current_pool);
2208 /* Compute window glyph matrices. (This takes the mini-buffer
2209 window into account). The result is the size of the frame glyph
2210 matrix needed. The variable window_change_flags is set to a bit
2211 mask indicating whether new matrices will be allocated or
2212 existing matrices change their size or location within the frame
2213 matrix. */
2214 window_change_flags = 0;
2215 matrix_dim
2216 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2217 0, top_window_y,
2219 &window_change_flags);
2221 /* Add in menu bar lines, if any. */
2222 matrix_dim.height += top_window_y;
2224 /* Enlarge pools as necessary. */
2225 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
2226 realloc_glyph_pool (f->current_pool, matrix_dim);
2228 /* Set up glyph pointers within window matrices. Do this only if
2229 absolutely necessary since it requires a frame redraw. */
2230 if (pool_changed_p || window_change_flags)
2232 /* Do it for window matrices. */
2233 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
2234 0, top_window_y, 0,
2235 &window_change_flags);
2237 /* Size of frame matrices must equal size of frame. Note
2238 that we are called for X frames with window widths NOT equal
2239 to the frame width (from CHANGE_FRAME_SIZE_1). */
2240 xassert (matrix_dim.width == FRAME_COLS (f)
2241 && matrix_dim.height == FRAME_LINES (f));
2243 /* Pointers to glyph memory in glyph rows are exchanged during
2244 the update phase of redisplay, which means in general that a
2245 frame's current matrix consists of pointers into both the
2246 desired and current glyph pool of the frame. Adjusting a
2247 matrix sets the frame matrix up so that pointers are all into
2248 the same pool. If we want to preserve glyph contents of the
2249 current matrix over a call to adjust_glyph_matrix, we must
2250 make a copy of the current glyphs, and restore the current
2251 matrix' contents from that copy. */
2252 if (display_completed
2253 && !FRAME_GARBAGED_P (f)
2254 && matrix_dim.width == f->current_matrix->matrix_w
2255 && matrix_dim.height == f->current_matrix->matrix_h
2256 /* For some reason, the frame glyph matrix gets corrupted if
2257 any of the windows contain margins. I haven't been able
2258 to hunt down the reason, but for the moment this prevents
2259 the problem from manifesting. -- cyd */
2260 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2262 struct glyph_matrix *copy = save_current_matrix (f);
2263 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2264 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2265 restore_current_matrix (f, copy);
2266 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2268 else
2270 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2271 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2272 SET_FRAME_GARBAGED (f);
2278 /* Allocate/reallocate glyph matrices of a single frame F for
2279 window-based redisplay. */
2281 static void
2282 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2284 struct window *w;
2286 xassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2288 /* Allocate/reallocate window matrices. */
2289 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2291 #ifdef HAVE_X_WINDOWS
2292 /* Allocate/ reallocate matrices of the dummy window used to display
2293 the menu bar under X when no X toolkit support is available. */
2294 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2296 /* Allocate a dummy window if not already done. */
2297 if (NILP (f->menu_bar_window))
2299 f->menu_bar_window = make_window ();
2300 w = XWINDOW (f->menu_bar_window);
2301 XSETFRAME (w->frame, f);
2302 w->pseudo_window_p = 1;
2304 else
2305 w = XWINDOW (f->menu_bar_window);
2307 /* Set window dimensions to frame dimensions and allocate or
2308 adjust glyph matrices of W. */
2309 XSETFASTINT (w->top_line, 0);
2310 XSETFASTINT (w->left_col, 0);
2311 XSETFASTINT (w->total_lines, FRAME_MENU_BAR_LINES (f));
2312 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2313 allocate_matrices_for_window_redisplay (w);
2315 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2316 #endif /* HAVE_X_WINDOWS */
2318 #ifndef USE_GTK
2319 /* Allocate/ reallocate matrices of the tool bar window. If we
2320 don't have a tool bar window yet, make one. */
2321 if (NILP (f->tool_bar_window))
2323 f->tool_bar_window = make_window ();
2324 w = XWINDOW (f->tool_bar_window);
2325 XSETFRAME (w->frame, f);
2326 w->pseudo_window_p = 1;
2328 else
2329 w = XWINDOW (f->tool_bar_window);
2331 XSETFASTINT (w->top_line, FRAME_MENU_BAR_LINES (f));
2332 XSETFASTINT (w->left_col, 0);
2333 XSETFASTINT (w->total_lines, FRAME_TOOL_BAR_LINES (f));
2334 XSETFASTINT (w->total_cols, FRAME_TOTAL_COLS (f));
2335 allocate_matrices_for_window_redisplay (w);
2336 #endif
2340 /* Adjust/ allocate message buffer of frame F.
2342 Note that the message buffer is never freed. Since I could not
2343 find a free in 19.34, I assume that freeing it would be
2344 problematic in some way and don't do it either.
2346 (Implementation note: It should be checked if we can free it
2347 eventually without causing trouble). */
2349 static void
2350 adjust_frame_message_buffer (struct frame *f)
2352 int size = FRAME_MESSAGE_BUF_SIZE (f) + 1;
2354 if (FRAME_MESSAGE_BUF (f))
2356 char *buffer = FRAME_MESSAGE_BUF (f);
2357 char *new_buffer = (char *) xrealloc (buffer, size);
2358 FRAME_MESSAGE_BUF (f) = new_buffer;
2360 else
2361 FRAME_MESSAGE_BUF (f) = (char *) xmalloc (size);
2365 /* Re-allocate buffer for decode_mode_spec on frame F. */
2367 static void
2368 adjust_decode_mode_spec_buffer (struct frame *f)
2370 f->decode_mode_spec_buffer
2371 = (char *) xrealloc (f->decode_mode_spec_buffer,
2372 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2377 /**********************************************************************
2378 Freeing Glyph Matrices
2379 **********************************************************************/
2381 /* Free glyph memory for a frame F. F may be null. This function can
2382 be called for the same frame more than once. The root window of
2383 F may be nil when this function is called. This is the case when
2384 the function is called when F is destroyed. */
2386 void
2387 free_glyphs (struct frame *f)
2389 if (f && f->glyphs_initialized_p)
2391 /* Block interrupt input so that we don't get surprised by an X
2392 event while we're in an inconsistent state. */
2393 BLOCK_INPUT;
2394 f->glyphs_initialized_p = 0;
2396 /* Release window sub-matrices. */
2397 if (!NILP (f->root_window))
2398 free_window_matrices (XWINDOW (f->root_window));
2400 /* Free the dummy window for menu bars without X toolkit and its
2401 glyph matrices. */
2402 if (!NILP (f->menu_bar_window))
2404 struct window *w = XWINDOW (f->menu_bar_window);
2405 free_glyph_matrix (w->desired_matrix);
2406 free_glyph_matrix (w->current_matrix);
2407 w->desired_matrix = w->current_matrix = NULL;
2408 f->menu_bar_window = Qnil;
2411 /* Free the tool bar window and its glyph matrices. */
2412 if (!NILP (f->tool_bar_window))
2414 struct window *w = XWINDOW (f->tool_bar_window);
2415 free_glyph_matrix (w->desired_matrix);
2416 free_glyph_matrix (w->current_matrix);
2417 w->desired_matrix = w->current_matrix = NULL;
2418 f->tool_bar_window = Qnil;
2421 /* Release frame glyph matrices. Reset fields to zero in
2422 case we are called a second time. */
2423 if (f->desired_matrix)
2425 free_glyph_matrix (f->desired_matrix);
2426 free_glyph_matrix (f->current_matrix);
2427 f->desired_matrix = f->current_matrix = NULL;
2430 /* Release glyph pools. */
2431 if (f->desired_pool)
2433 free_glyph_pool (f->desired_pool);
2434 free_glyph_pool (f->current_pool);
2435 f->desired_pool = f->current_pool = NULL;
2438 UNBLOCK_INPUT;
2443 /* Free glyph sub-matrices in the window tree rooted at W. This
2444 function may be called with a null pointer, and it may be called on
2445 the same tree more than once. */
2447 void
2448 free_window_matrices (struct window *w)
2450 while (w)
2452 if (!NILP (w->hchild))
2453 free_window_matrices (XWINDOW (w->hchild));
2454 else if (!NILP (w->vchild))
2455 free_window_matrices (XWINDOW (w->vchild));
2456 else
2458 /* This is a leaf window. Free its memory and reset fields
2459 to zero in case this function is called a second time for
2460 W. */
2461 free_glyph_matrix (w->current_matrix);
2462 free_glyph_matrix (w->desired_matrix);
2463 w->current_matrix = w->desired_matrix = NULL;
2466 /* Next window on same level. */
2467 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2472 /* Check glyph memory leaks. This function is called from
2473 shut_down_emacs. Note that frames are not destroyed when Emacs
2474 exits. We therefore free all glyph memory for all active frames
2475 explicitly and check that nothing is left allocated. */
2477 void
2478 check_glyph_memory (void)
2480 Lisp_Object tail, frame;
2482 /* Free glyph memory for all frames. */
2483 FOR_EACH_FRAME (tail, frame)
2484 free_glyphs (XFRAME (frame));
2486 /* Check that nothing is left allocated. */
2487 if (glyph_matrix_count)
2488 abort ();
2489 if (glyph_pool_count)
2490 abort ();
2495 /**********************************************************************
2496 Building a Frame Matrix
2497 **********************************************************************/
2499 /* Most of the redisplay code works on glyph matrices attached to
2500 windows. This is a good solution most of the time, but it is not
2501 suitable for terminal code. Terminal output functions cannot rely
2502 on being able to set an arbitrary terminal window. Instead they
2503 must be provided with a view of the whole frame, i.e. the whole
2504 screen. We build such a view by constructing a frame matrix from
2505 window matrices in this section.
2507 Windows that must be updated have their must_be_update_p flag set.
2508 For all such windows, their desired matrix is made part of the
2509 desired frame matrix. For other windows, their current matrix is
2510 made part of the desired frame matrix.
2512 +-----------------+----------------+
2513 | desired | desired |
2514 | | |
2515 +-----------------+----------------+
2516 | current |
2518 +----------------------------------+
2520 Desired window matrices can be made part of the frame matrix in a
2521 cheap way: We exploit the fact that the desired frame matrix and
2522 desired window matrices share their glyph memory. This is not
2523 possible for current window matrices. Their glyphs are copied to
2524 the desired frame matrix. The latter is equivalent to
2525 preserve_other_columns in the old redisplay.
2527 Used glyphs counters for frame matrix rows are the result of adding
2528 up glyph lengths of the window matrices. A line in the frame
2529 matrix is enabled, if a corresponding line in a window matrix is
2530 enabled.
2532 After building the desired frame matrix, it will be passed to
2533 terminal code, which will manipulate both the desired and current
2534 frame matrix. Changes applied to the frame's current matrix have
2535 to be visible in current window matrices afterwards, of course.
2537 This problem is solved like this:
2539 1. Window and frame matrices share glyphs. Window matrices are
2540 constructed in a way that their glyph contents ARE the glyph
2541 contents needed in a frame matrix. Thus, any modification of
2542 glyphs done in terminal code will be reflected in window matrices
2543 automatically.
2545 2. Exchanges of rows in a frame matrix done by terminal code are
2546 intercepted by hook functions so that corresponding row operations
2547 on window matrices can be performed. This is necessary because we
2548 use pointers to glyphs in glyph row structures. To satisfy the
2549 assumption of point 1 above that glyphs are updated implicitly in
2550 window matrices when they are manipulated via the frame matrix,
2551 window and frame matrix must of course agree where to find the
2552 glyphs for their rows. Possible manipulations that must be
2553 mirrored are assignments of rows of the desired frame matrix to the
2554 current frame matrix and scrolling the current frame matrix. */
2556 /* Build frame F's desired matrix from window matrices. Only windows
2557 which have the flag must_be_updated_p set have to be updated. Menu
2558 bar lines of a frame are not covered by window matrices, so make
2559 sure not to touch them in this function. */
2561 static void
2562 build_frame_matrix (struct frame *f)
2564 int i;
2566 /* F must have a frame matrix when this function is called. */
2567 xassert (!FRAME_WINDOW_P (f));
2569 /* Clear all rows in the frame matrix covered by window matrices.
2570 Menu bar lines are not covered by windows. */
2571 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2572 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2574 /* Build the matrix by walking the window tree. */
2575 build_frame_matrix_from_window_tree (f->desired_matrix,
2576 XWINDOW (FRAME_ROOT_WINDOW (f)));
2580 /* Walk a window tree, building a frame matrix MATRIX from window
2581 matrices. W is the root of a window tree. */
2583 static void
2584 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2586 while (w)
2588 if (!NILP (w->hchild))
2589 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->hchild));
2590 else if (!NILP (w->vchild))
2591 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->vchild));
2592 else
2593 build_frame_matrix_from_leaf_window (matrix, w);
2595 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2600 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2601 desired frame matrix built. W is a leaf window whose desired or
2602 current matrix is to be added to FRAME_MATRIX. W's flag
2603 must_be_updated_p determines which matrix it contributes to
2604 FRAME_MATRIX. If must_be_updated_p is non-zero, W's desired matrix
2605 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2606 Adding a desired matrix means setting up used counters and such in
2607 frame rows, while adding a current window matrix to FRAME_MATRIX
2608 means copying glyphs. The latter case corresponds to
2609 preserve_other_columns in the old redisplay. */
2611 static void
2612 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2614 struct glyph_matrix *window_matrix;
2615 int window_y, frame_y;
2616 /* If non-zero, a glyph to insert at the right border of W. */
2617 GLYPH right_border_glyph;
2619 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2621 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2622 if (w->must_be_updated_p)
2624 window_matrix = w->desired_matrix;
2626 /* Decide whether we want to add a vertical border glyph. */
2627 if (!WINDOW_RIGHTMOST_P (w))
2629 struct Lisp_Char_Table *dp = window_display_table (w);
2630 Lisp_Object gc;
2632 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2633 if (dp
2634 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc))
2635 && GLYPH_CODE_CHAR_VALID_P (gc))
2637 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2638 spec_glyph_lookup_face (w, &right_border_glyph);
2641 if (GLYPH_FACE (right_border_glyph) <= 0)
2642 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2645 else
2646 window_matrix = w->current_matrix;
2648 /* For all rows in the window matrix and corresponding rows in the
2649 frame matrix. */
2650 window_y = 0;
2651 frame_y = window_matrix->matrix_y;
2652 while (window_y < window_matrix->nrows)
2654 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2655 struct glyph_row *window_row = window_matrix->rows + window_y;
2656 int current_row_p = window_matrix == w->current_matrix;
2658 /* Fill up the frame row with spaces up to the left margin of the
2659 window row. */
2660 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2662 /* Fill up areas in the window matrix row with spaces. */
2663 fill_up_glyph_row_with_spaces (window_row);
2665 /* If only part of W's desired matrix has been built, and
2666 window_row wasn't displayed, use the corresponding current
2667 row instead. */
2668 if (window_matrix == w->desired_matrix
2669 && !window_row->enabled_p)
2671 window_row = w->current_matrix->rows + window_y;
2672 current_row_p = 1;
2675 if (current_row_p)
2677 /* Copy window row to frame row. */
2678 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2679 window_row->glyphs[0],
2680 window_matrix->matrix_w * sizeof (struct glyph));
2682 else
2684 xassert (window_row->enabled_p);
2686 /* Only when a desired row has been displayed, we want
2687 the corresponding frame row to be updated. */
2688 frame_row->enabled_p = 1;
2690 /* Maybe insert a vertical border between horizontally adjacent
2691 windows. */
2692 if (GLYPH_CHAR (right_border_glyph) != 0)
2694 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2695 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2698 #if GLYPH_DEBUG
2699 /* Window row window_y must be a slice of frame row
2700 frame_y. */
2701 xassert (glyph_row_slice_p (window_row, frame_row));
2703 /* If rows are in sync, we don't have to copy glyphs because
2704 frame and window share glyphs. */
2706 strcpy (w->current_matrix->method, w->desired_matrix->method);
2707 add_window_display_history (w, w->current_matrix->method, 0);
2708 #endif
2711 /* Set number of used glyphs in the frame matrix. Since we fill
2712 up with spaces, and visit leaf windows from left to right it
2713 can be done simply. */
2714 frame_row->used[TEXT_AREA]
2715 = window_matrix->matrix_x + window_matrix->matrix_w;
2717 /* Next row. */
2718 ++window_y;
2719 ++frame_y;
2723 /* Given a user-specified glyph, possibly including a Lisp-level face
2724 ID, return a glyph that has a realized face ID.
2725 This is used for glyphs displayed specially and not part of the text;
2726 for instance, vertical separators, truncation markers, etc. */
2728 void
2729 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2731 int lface_id = GLYPH_FACE (*glyph);
2732 /* Convert the glyph's specified face to a realized (cache) face. */
2733 if (lface_id > 0)
2735 int face_id = merge_faces (XFRAME (w->frame),
2736 Qt, lface_id, DEFAULT_FACE_ID);
2737 SET_GLYPH_FACE (*glyph, face_id);
2741 /* Add spaces to a glyph row ROW in a window matrix.
2743 Each row has the form:
2745 +---------+-----------------------------+------------+
2746 | left | text | right |
2747 +---------+-----------------------------+------------+
2749 Left and right marginal areas are optional. This function adds
2750 spaces to areas so that there are no empty holes between areas.
2751 In other words: If the right area is not empty, the text area
2752 is filled up with spaces up to the right area. If the text area
2753 is not empty, the left area is filled up.
2755 To be called for frame-based redisplay, only. */
2757 static void
2758 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2760 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2761 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2762 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2766 /* Fill area AREA of glyph row ROW with spaces. To be called for
2767 frame-based redisplay only. */
2769 static void
2770 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2772 if (row->glyphs[area] < row->glyphs[area + 1])
2774 struct glyph *end = row->glyphs[area + 1];
2775 struct glyph *text = row->glyphs[area] + row->used[area];
2777 while (text < end)
2778 *text++ = space_glyph;
2779 row->used[area] = text - row->glyphs[area];
2784 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2785 reached. In frame matrices only one area, TEXT_AREA, is used. */
2787 static void
2788 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2790 int i = row->used[TEXT_AREA];
2791 struct glyph *glyph = row->glyphs[TEXT_AREA];
2793 while (i < upto)
2794 glyph[i++] = space_glyph;
2796 row->used[TEXT_AREA] = i;
2801 /**********************************************************************
2802 Mirroring operations on frame matrices in window matrices
2803 **********************************************************************/
2805 /* Set frame being updated via frame-based redisplay to F. This
2806 function must be called before updates to make explicit that we are
2807 working on frame matrices or not. */
2809 static INLINE void
2810 set_frame_matrix_frame (struct frame *f)
2812 frame_matrix_frame = f;
2816 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2817 DESIRED_MATRIX is the desired matrix corresponding to
2818 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2819 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2820 frame_matrix_frame is non-null, this indicates that the exchange is
2821 done in frame matrices, and that we have to perform analogous
2822 operations in window matrices of frame_matrix_frame. */
2824 static INLINE void
2825 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2827 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2828 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2829 int mouse_face_p = current_row->mouse_face_p;
2831 /* Do current_row = desired_row. This exchanges glyph pointers
2832 between both rows, and does a structure assignment otherwise. */
2833 assign_row (current_row, desired_row);
2835 /* Enable current_row to mark it as valid. */
2836 current_row->enabled_p = 1;
2837 current_row->mouse_face_p = mouse_face_p;
2839 /* If we are called on frame matrices, perform analogous operations
2840 for window matrices. */
2841 if (frame_matrix_frame)
2842 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2846 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2847 W's frame which has been made current (by swapping pointers between
2848 current and desired matrix). Perform analogous operations in the
2849 matrices of leaf windows in the window tree rooted at W. */
2851 static void
2852 mirror_make_current (struct window *w, int frame_row)
2854 while (w)
2856 if (!NILP (w->hchild))
2857 mirror_make_current (XWINDOW (w->hchild), frame_row);
2858 else if (!NILP (w->vchild))
2859 mirror_make_current (XWINDOW (w->vchild), frame_row);
2860 else
2862 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2863 here because the checks performed in debug mode there
2864 will not allow the conversion. */
2865 int row = frame_row - w->desired_matrix->matrix_y;
2867 /* If FRAME_ROW is within W, assign the desired row to the
2868 current row (exchanging glyph pointers). */
2869 if (row >= 0 && row < w->desired_matrix->matrix_h)
2871 struct glyph_row *current_row
2872 = MATRIX_ROW (w->current_matrix, row);
2873 struct glyph_row *desired_row
2874 = MATRIX_ROW (w->desired_matrix, row);
2876 if (desired_row->enabled_p)
2877 assign_row (current_row, desired_row);
2878 else
2879 swap_glyph_pointers (desired_row, current_row);
2880 current_row->enabled_p = 1;
2882 /* Set the Y coordinate of the mode/header line's row.
2883 It is needed in draw_row_with_mouse_face to find the
2884 screen coordinates. (Window-based redisplay sets
2885 this in update_window, but no one seems to do that
2886 for frame-based redisplay.) */
2887 if (current_row->mode_line_p)
2888 current_row->y = row;
2892 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2897 /* Perform row dance after scrolling. We are working on the range of
2898 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2899 including) in MATRIX. COPY_FROM is a vector containing, for each
2900 row I in the range 0 <= I < NLINES, the index of the original line
2901 to move to I. This index is relative to the row range, i.e. 0 <=
2902 index < NLINES. RETAINED_P is a vector containing zero for each
2903 row 0 <= I < NLINES which is empty.
2905 This function is called from do_scrolling and do_direct_scrolling. */
2907 void
2908 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2909 int *copy_from, char *retained_p)
2911 /* A copy of original rows. */
2912 struct glyph_row *old_rows;
2914 /* Rows to assign to. */
2915 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2917 int i;
2919 /* Make a copy of the original rows. */
2920 old_rows = (struct glyph_row *) alloca (nlines * sizeof *old_rows);
2921 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2923 /* Assign new rows, maybe clear lines. */
2924 for (i = 0; i < nlines; ++i)
2926 int enabled_before_p = new_rows[i].enabled_p;
2928 xassert (i + unchanged_at_top < matrix->nrows);
2929 xassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2930 new_rows[i] = old_rows[copy_from[i]];
2931 new_rows[i].enabled_p = enabled_before_p;
2933 /* RETAINED_P is zero for empty lines. */
2934 if (!retained_p[copy_from[i]])
2935 new_rows[i].enabled_p = 0;
2938 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2939 if (frame_matrix_frame)
2940 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2941 unchanged_at_top, nlines, copy_from, retained_p);
2945 /* Synchronize glyph pointers in the current matrix of window W with
2946 the current frame matrix. */
2948 static void
2949 sync_window_with_frame_matrix_rows (struct window *w)
2951 struct frame *f = XFRAME (w->frame);
2952 struct glyph_row *window_row, *window_row_end, *frame_row;
2953 int left, right, x, width;
2955 /* Preconditions: W must be a leaf window on a tty frame. */
2956 xassert (NILP (w->hchild) && NILP (w->vchild));
2957 xassert (!FRAME_WINDOW_P (f));
2959 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2960 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2961 x = w->current_matrix->matrix_x;
2962 width = w->current_matrix->matrix_w;
2964 window_row = w->current_matrix->rows;
2965 window_row_end = window_row + w->current_matrix->nrows;
2966 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2968 for (; window_row < window_row_end; ++window_row, ++frame_row)
2970 window_row->glyphs[LEFT_MARGIN_AREA]
2971 = frame_row->glyphs[0] + x;
2972 window_row->glyphs[TEXT_AREA]
2973 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2974 window_row->glyphs[LAST_AREA]
2975 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2976 window_row->glyphs[RIGHT_MARGIN_AREA]
2977 = window_row->glyphs[LAST_AREA] - right;
2982 /* Return the window in the window tree rooted in W containing frame
2983 row ROW. Value is null if none is found. */
2985 struct window *
2986 frame_row_to_window (struct window *w, int row)
2988 struct window *found = NULL;
2990 while (w && !found)
2992 if (!NILP (w->hchild))
2993 found = frame_row_to_window (XWINDOW (w->hchild), row);
2994 else if (!NILP (w->vchild))
2995 found = frame_row_to_window (XWINDOW (w->vchild), row);
2996 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2997 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2998 found = w;
3000 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3003 return found;
3007 /* Perform a line dance in the window tree rooted at W, after
3008 scrolling a frame matrix in mirrored_line_dance.
3010 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
3011 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
3012 COPY_FROM is a vector containing, for each row I in the range 0 <=
3013 I < NLINES, the index of the original line to move to I. This
3014 index is relative to the row range, i.e. 0 <= index < NLINES.
3015 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
3016 which is empty. */
3018 static void
3019 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
3021 while (w)
3023 if (!NILP (w->hchild))
3024 mirror_line_dance (XWINDOW (w->hchild), unchanged_at_top,
3025 nlines, copy_from, retained_p);
3026 else if (!NILP (w->vchild))
3027 mirror_line_dance (XWINDOW (w->vchild), unchanged_at_top,
3028 nlines, copy_from, retained_p);
3029 else
3031 /* W is a leaf window, and we are working on its current
3032 matrix m. */
3033 struct glyph_matrix *m = w->current_matrix;
3034 int i, sync_p = 0;
3035 struct glyph_row *old_rows;
3037 /* Make a copy of the original rows of matrix m. */
3038 old_rows = (struct glyph_row *) alloca (m->nrows * sizeof *old_rows);
3039 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
3041 for (i = 0; i < nlines; ++i)
3043 /* Frame relative line assigned to. */
3044 int frame_to = i + unchanged_at_top;
3046 /* Frame relative line assigned. */
3047 int frame_from = copy_from[i] + unchanged_at_top;
3049 /* Window relative line assigned to. */
3050 int window_to = frame_to - m->matrix_y;
3052 /* Window relative line assigned. */
3053 int window_from = frame_from - m->matrix_y;
3055 /* Is assigned line inside window? */
3056 int from_inside_window_p
3057 = window_from >= 0 && window_from < m->matrix_h;
3059 /* Is assigned to line inside window? */
3060 int to_inside_window_p
3061 = window_to >= 0 && window_to < m->matrix_h;
3063 if (from_inside_window_p && to_inside_window_p)
3065 /* Enabled setting before assignment. */
3066 int enabled_before_p;
3068 /* Do the assignment. The enabled_p flag is saved
3069 over the assignment because the old redisplay did
3070 that. */
3071 enabled_before_p = m->rows[window_to].enabled_p;
3072 m->rows[window_to] = old_rows[window_from];
3073 m->rows[window_to].enabled_p = enabled_before_p;
3075 /* If frame line is empty, window line is empty, too. */
3076 if (!retained_p[copy_from[i]])
3077 m->rows[window_to].enabled_p = 0;
3079 else if (to_inside_window_p)
3081 /* A copy between windows. This is an infrequent
3082 case not worth optimizing. */
3083 struct frame *f = XFRAME (w->frame);
3084 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
3085 struct window *w2;
3086 struct glyph_matrix *m2;
3087 int m2_from;
3089 w2 = frame_row_to_window (root, frame_from);
3090 /* ttn@surf.glug.org: when enabling menu bar using `emacs
3091 -nw', FROM_FRAME sometimes has no associated window.
3092 This check avoids a segfault if W2 is null. */
3093 if (w2)
3095 m2 = w2->current_matrix;
3096 m2_from = frame_from - m2->matrix_y;
3097 copy_row_except_pointers (m->rows + window_to,
3098 m2->rows + m2_from);
3100 /* If frame line is empty, window line is empty, too. */
3101 if (!retained_p[copy_from[i]])
3102 m->rows[window_to].enabled_p = 0;
3104 sync_p = 1;
3106 else if (from_inside_window_p)
3107 sync_p = 1;
3110 /* If there was a copy between windows, make sure glyph
3111 pointers are in sync with the frame matrix. */
3112 if (sync_p)
3113 sync_window_with_frame_matrix_rows (w);
3115 /* Check that no pointers are lost. */
3116 CHECK_MATRIX (m);
3119 /* Next window on same level. */
3120 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3125 #if GLYPH_DEBUG
3127 /* Check that window and frame matrices agree about their
3128 understanding where glyphs of the rows are to find. For each
3129 window in the window tree rooted at W, check that rows in the
3130 matrices of leaf window agree with their frame matrices about
3131 glyph pointers. */
3133 void
3134 check_window_matrix_pointers (struct window *w)
3136 while (w)
3138 if (!NILP (w->hchild))
3139 check_window_matrix_pointers (XWINDOW (w->hchild));
3140 else if (!NILP (w->vchild))
3141 check_window_matrix_pointers (XWINDOW (w->vchild));
3142 else
3144 struct frame *f = XFRAME (w->frame);
3145 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
3146 check_matrix_pointers (w->current_matrix, f->current_matrix);
3149 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3154 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
3155 a window and FRAME_MATRIX is the corresponding frame matrix. For
3156 each row in WINDOW_MATRIX check that it's a slice of the
3157 corresponding frame row. If it isn't, abort. */
3159 static void
3160 check_matrix_pointers (struct glyph_matrix *window_matrix,
3161 struct glyph_matrix *frame_matrix)
3163 /* Row number in WINDOW_MATRIX. */
3164 int i = 0;
3166 /* Row number corresponding to I in FRAME_MATRIX. */
3167 int j = window_matrix->matrix_y;
3169 /* For all rows check that the row in the window matrix is a
3170 slice of the row in the frame matrix. If it isn't we didn't
3171 mirror an operation on the frame matrix correctly. */
3172 while (i < window_matrix->nrows)
3174 if (!glyph_row_slice_p (window_matrix->rows + i,
3175 frame_matrix->rows + j))
3176 abort ();
3177 ++i, ++j;
3181 #endif /* GLYPH_DEBUG != 0 */
3185 /**********************************************************************
3186 VPOS and HPOS translations
3187 **********************************************************************/
3189 #if GLYPH_DEBUG
3191 /* Translate vertical position VPOS which is relative to window W to a
3192 vertical position relative to W's frame. */
3194 static int
3195 window_to_frame_vpos (struct window *w, int vpos)
3197 struct frame *f = XFRAME (w->frame);
3199 xassert (!FRAME_WINDOW_P (f));
3200 xassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
3201 vpos += WINDOW_TOP_EDGE_LINE (w);
3202 xassert (vpos >= 0 && vpos <= FRAME_LINES (f));
3203 return vpos;
3207 /* Translate horizontal position HPOS which is relative to window W to
3208 a horizontal position relative to W's frame. */
3210 static int
3211 window_to_frame_hpos (struct window *w, int hpos)
3213 xassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
3214 hpos += WINDOW_LEFT_EDGE_COL (w);
3215 return hpos;
3218 #endif /* GLYPH_DEBUG */
3222 /**********************************************************************
3223 Redrawing Frames
3224 **********************************************************************/
3226 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 1, 1, 0,
3227 doc: /* Clear frame FRAME and output again what is supposed to appear on it. */)
3228 (Lisp_Object frame)
3230 struct frame *f;
3232 CHECK_LIVE_FRAME (frame);
3233 f = XFRAME (frame);
3235 /* Ignore redraw requests, if frame has no glyphs yet.
3236 (Implementation note: It still has to be checked why we are
3237 called so early here). */
3238 if (!glyphs_initialized_initially_p)
3239 return Qnil;
3241 update_begin (f);
3242 #ifdef MSDOS
3243 if (FRAME_MSDOS_P (f))
3244 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
3245 #endif
3246 clear_frame (f);
3247 clear_current_matrices (f);
3248 update_end (f);
3249 if (FRAME_TERMCAP_P (f))
3250 fflush (FRAME_TTY (f)->output);
3251 windows_or_buffers_changed++;
3252 /* Mark all windows as inaccurate, so that every window will have
3253 its redisplay done. */
3254 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
3255 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
3256 f->garbaged = 0;
3257 return Qnil;
3261 /* Redraw frame F. This is nothing more than a call to the Lisp
3262 function redraw-frame. */
3264 void
3265 redraw_frame (struct frame *f)
3267 Lisp_Object frame;
3268 XSETFRAME (frame, f);
3269 Fredraw_frame (frame);
3273 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
3274 doc: /* Clear and redisplay all visible frames. */)
3275 (void)
3277 Lisp_Object tail, frame;
3279 FOR_EACH_FRAME (tail, frame)
3280 if (FRAME_VISIBLE_P (XFRAME (frame)))
3281 Fredraw_frame (frame);
3283 return Qnil;
3287 /* This is used when frame_garbaged is set. Call Fredraw_frame on all
3288 visible frames marked as garbaged. */
3290 void
3291 redraw_garbaged_frames (void)
3293 Lisp_Object tail, frame;
3295 FOR_EACH_FRAME (tail, frame)
3296 if (FRAME_VISIBLE_P (XFRAME (frame))
3297 && FRAME_GARBAGED_P (XFRAME (frame)))
3298 Fredraw_frame (frame);
3303 /***********************************************************************
3304 Frame Update
3305 ***********************************************************************/
3307 /* Update frame F based on the data in desired matrices.
3309 If FORCE_P is non-zero, don't let redisplay be stopped by detecting
3310 pending input. If INHIBIT_HAIRY_ID_P is non-zero, don't try
3311 scrolling.
3313 Value is non-zero if redisplay was stopped due to pending input. */
3316 update_frame (struct frame *f, int force_p, int inhibit_hairy_id_p)
3318 /* 1 means display has been paused because of pending input. */
3319 int paused_p;
3320 struct window *root_window = XWINDOW (f->root_window);
3322 if (redisplay_dont_pause)
3323 force_p = 1;
3324 #if PERIODIC_PREEMPTION_CHECKING
3325 else if (NILP (Vredisplay_preemption_period))
3326 force_p = 1;
3327 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3329 EMACS_TIME tm;
3330 double p = XFLOATINT (Vredisplay_preemption_period);
3331 int sec, usec;
3333 if (detect_input_pending_ignore_squeezables ())
3335 paused_p = 1;
3336 goto do_pause;
3339 sec = (int) p;
3340 usec = (p - sec) * 1000000;
3342 EMACS_GET_TIME (tm);
3343 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3344 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3346 #endif
3348 if (FRAME_WINDOW_P (f))
3350 /* We are working on window matrix basis. All windows whose
3351 flag must_be_updated_p is set have to be updated. */
3353 /* Record that we are not working on frame matrices. */
3354 set_frame_matrix_frame (NULL);
3356 /* Update all windows in the window tree of F, maybe stopping
3357 when pending input is detected. */
3358 update_begin (f);
3360 /* Update the menu bar on X frames that don't have toolkit
3361 support. */
3362 if (WINDOWP (f->menu_bar_window))
3363 update_window (XWINDOW (f->menu_bar_window), 1);
3365 /* Update the tool-bar window, if present. */
3366 if (WINDOWP (f->tool_bar_window))
3368 struct window *w = XWINDOW (f->tool_bar_window);
3370 /* Update tool-bar window. */
3371 if (w->must_be_updated_p)
3373 Lisp_Object tem;
3375 update_window (w, 1);
3376 w->must_be_updated_p = 0;
3378 /* Swap tool-bar strings. We swap because we want to
3379 reuse strings. */
3380 tem = f->current_tool_bar_string;
3381 f->current_tool_bar_string = f->desired_tool_bar_string;
3382 f->desired_tool_bar_string = tem;
3387 /* Update windows. */
3388 paused_p = update_window_tree (root_window, force_p);
3389 update_end (f);
3391 /* This flush is a performance bottleneck under X,
3392 and it doesn't seem to be necessary anyway (in general).
3393 It is necessary when resizing the window with the mouse, or
3394 at least the fringes are not redrawn in a timely manner. ++kfs */
3395 if (f->force_flush_display_p)
3397 FRAME_RIF (f)->flush_display (f);
3398 f->force_flush_display_p = 0;
3401 else
3403 /* We are working on frame matrix basis. Set the frame on whose
3404 frame matrix we operate. */
3405 set_frame_matrix_frame (f);
3407 /* Build F's desired matrix from window matrices. */
3408 build_frame_matrix (f);
3410 /* Update the display */
3411 update_begin (f);
3412 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3413 update_end (f);
3415 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3417 if (FRAME_TTY (f)->termscript)
3418 fflush (FRAME_TTY (f)->termscript);
3419 if (FRAME_TERMCAP_P (f))
3420 fflush (FRAME_TTY (f)->output);
3423 /* Check window matrices for lost pointers. */
3424 #if GLYPH_DEBUG
3425 check_window_matrix_pointers (root_window);
3426 add_frame_display_history (f, paused_p);
3427 #endif
3430 #if PERIODIC_PREEMPTION_CHECKING
3431 do_pause:
3432 #endif
3433 /* Reset flags indicating that a window should be updated. */
3434 set_window_update_flags (root_window, 0);
3436 display_completed = !paused_p;
3437 return paused_p;
3442 /************************************************************************
3443 Window-based updates
3444 ************************************************************************/
3446 /* Perform updates in window tree rooted at W. FORCE_P non-zero means
3447 don't stop updating when input is pending. */
3449 static int
3450 update_window_tree (struct window *w, int force_p)
3452 int paused_p = 0;
3454 while (w && !paused_p)
3456 if (!NILP (w->hchild))
3457 paused_p |= update_window_tree (XWINDOW (w->hchild), force_p);
3458 else if (!NILP (w->vchild))
3459 paused_p |= update_window_tree (XWINDOW (w->vchild), force_p);
3460 else if (w->must_be_updated_p)
3461 paused_p |= update_window (w, force_p);
3463 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3466 return paused_p;
3470 /* Update window W if its flag must_be_updated_p is non-zero. If
3471 FORCE_P is non-zero, don't stop updating if input is pending. */
3473 void
3474 update_single_window (struct window *w, int force_p)
3476 if (w->must_be_updated_p)
3478 struct frame *f = XFRAME (WINDOW_FRAME (w));
3480 /* Record that this is not a frame-based redisplay. */
3481 set_frame_matrix_frame (NULL);
3483 if (redisplay_dont_pause)
3484 force_p = 1;
3485 #if PERIODIC_PREEMPTION_CHECKING
3486 else if (NILP (Vredisplay_preemption_period))
3487 force_p = 1;
3488 else if (!force_p && NUMBERP (Vredisplay_preemption_period))
3490 EMACS_TIME tm;
3491 double p = XFLOATINT (Vredisplay_preemption_period);
3492 int sec, usec;
3494 sec = (int) p;
3495 usec = (p - sec) * 1000000;
3497 EMACS_GET_TIME (tm);
3498 EMACS_SET_SECS_USECS (preemption_period, sec, usec);
3499 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3501 #endif
3503 /* Update W. */
3504 update_begin (f);
3505 update_window (w, force_p);
3506 update_end (f);
3508 /* Reset flag in W. */
3509 w->must_be_updated_p = 0;
3513 #ifdef HAVE_WINDOW_SYSTEM
3515 /* Redraw lines from the current matrix of window W that are
3516 overlapped by other rows. YB is bottom-most y-position in W. */
3518 static void
3519 redraw_overlapped_rows (struct window *w, int yb)
3521 int i;
3522 struct frame *f = XFRAME (WINDOW_FRAME (w));
3524 /* If rows overlapping others have been changed, the rows being
3525 overlapped have to be redrawn. This won't draw lines that have
3526 already been drawn in update_window_line because overlapped_p in
3527 desired rows is 0, so after row assignment overlapped_p in
3528 current rows is 0. */
3529 for (i = 0; i < w->current_matrix->nrows; ++i)
3531 struct glyph_row *row = w->current_matrix->rows + i;
3533 if (!row->enabled_p)
3534 break;
3535 else if (row->mode_line_p)
3536 continue;
3538 if (row->overlapped_p)
3540 enum glyph_row_area area;
3542 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3544 updated_row = row;
3545 updated_area = area;
3546 FRAME_RIF (f)->cursor_to (i, 0, row->y,
3547 area == TEXT_AREA ? row->x : 0);
3548 if (row->used[area])
3549 FRAME_RIF (f)->write_glyphs (row->glyphs[area],
3550 row->used[area]);
3551 FRAME_RIF (f)->clear_end_of_line (-1);
3554 row->overlapped_p = 0;
3557 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3558 break;
3563 /* Redraw lines from the current matrix of window W that overlap
3564 others. YB is bottom-most y-position in W. */
3566 static void
3567 redraw_overlapping_rows (struct window *w, int yb)
3569 int i, bottom_y;
3570 struct glyph_row *row;
3571 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3573 for (i = 0; i < w->current_matrix->nrows; ++i)
3575 row = w->current_matrix->rows + i;
3577 if (!row->enabled_p)
3578 break;
3579 else if (row->mode_line_p)
3580 continue;
3582 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3584 if (row->overlapping_p)
3586 int overlaps = 0;
3588 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3589 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3590 overlaps |= OVERLAPS_PRED;
3591 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3592 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3593 overlaps |= OVERLAPS_SUCC;
3595 if (overlaps)
3597 if (row->used[LEFT_MARGIN_AREA])
3598 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3600 if (row->used[TEXT_AREA])
3601 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3603 if (row->used[RIGHT_MARGIN_AREA])
3604 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3606 /* Record in neighbour rows that ROW overwrites part of
3607 their display. */
3608 if (overlaps & OVERLAPS_PRED)
3609 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3610 if (overlaps & OVERLAPS_SUCC)
3611 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3615 if (bottom_y >= yb)
3616 break;
3620 #endif /* HAVE_WINDOW_SYSTEM */
3623 #ifdef GLYPH_DEBUG
3625 /* Check that no row in the current matrix of window W is enabled
3626 which is below what's displayed in the window. */
3628 void
3629 check_current_matrix_flags (struct window *w)
3631 int last_seen_p = 0;
3632 int i, yb = window_text_bottom_y (w);
3634 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3636 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3637 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3638 last_seen_p = 1;
3639 else if (last_seen_p && row->enabled_p)
3640 abort ();
3644 #endif /* GLYPH_DEBUG */
3647 /* Update display of window W. FORCE_P non-zero means that we should
3648 not stop when detecting pending input. */
3650 static int
3651 update_window (struct window *w, int force_p)
3653 struct glyph_matrix *desired_matrix = w->desired_matrix;
3654 int paused_p;
3655 #if !PERIODIC_PREEMPTION_CHECKING
3656 int preempt_count = baud_rate / 2400 + 1;
3657 #endif
3658 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3659 #if GLYPH_DEBUG
3660 /* Check that W's frame doesn't have glyph matrices. */
3661 xassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3662 #endif
3664 /* Check pending input the first time so that we can quickly return. */
3665 #if !PERIODIC_PREEMPTION_CHECKING
3666 if (!force_p)
3667 detect_input_pending_ignore_squeezables ();
3668 #endif
3670 /* If forced to complete the update, or if no input is pending, do
3671 the update. */
3672 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3674 struct glyph_row *row, *end;
3675 struct glyph_row *mode_line_row;
3676 struct glyph_row *header_line_row;
3677 int yb, changed_p = 0, mouse_face_overwritten_p = 0, n_updated;
3679 rif->update_window_begin_hook (w);
3680 yb = window_text_bottom_y (w);
3682 /* If window has a header line, update it before everything else.
3683 Adjust y-positions of other rows by the header line height. */
3684 row = desired_matrix->rows;
3685 end = row + desired_matrix->nrows - 1;
3687 if (row->mode_line_p)
3689 header_line_row = row;
3690 ++row;
3692 else
3693 header_line_row = NULL;
3695 /* Update the mode line, if necessary. */
3696 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3697 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3699 mode_line_row->y = yb;
3700 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3701 desired_matrix),
3702 &mouse_face_overwritten_p);
3705 /* Find first enabled row. Optimizations in redisplay_internal
3706 may lead to an update with only one row enabled. There may
3707 be also completely empty matrices. */
3708 while (row < end && !row->enabled_p)
3709 ++row;
3711 /* Try reusing part of the display by copying. */
3712 if (row < end && !desired_matrix->no_scrolling_p)
3714 int rc = scrolling_window (w, header_line_row != NULL);
3715 if (rc < 0)
3717 /* All rows were found to be equal. */
3718 paused_p = 0;
3719 goto set_cursor;
3721 else if (rc > 0)
3723 /* We've scrolled the display. */
3724 force_p = 1;
3725 changed_p = 1;
3729 /* Update the rest of the lines. */
3730 for (n_updated = 0; row < end && (force_p || !input_pending); ++row)
3731 if (row->enabled_p)
3733 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3734 int i;
3736 /* We'll have to play a little bit with when to
3737 detect_input_pending. If it's done too often,
3738 scrolling large windows with repeated scroll-up
3739 commands will too quickly pause redisplay. */
3740 #if PERIODIC_PREEMPTION_CHECKING
3741 if (!force_p)
3743 EMACS_TIME tm, dif;
3744 EMACS_GET_TIME (tm);
3745 EMACS_SUB_TIME (dif, preemption_next_check, tm);
3746 if (EMACS_TIME_NEG_P (dif))
3748 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
3749 if (detect_input_pending_ignore_squeezables ())
3750 break;
3753 #else
3754 if (!force_p && ++n_updated % preempt_count == 0)
3755 detect_input_pending_ignore_squeezables ();
3756 #endif
3757 changed_p |= update_window_line (w, vpos,
3758 &mouse_face_overwritten_p);
3760 /* Mark all rows below the last visible one in the current
3761 matrix as invalid. This is necessary because of
3762 variable line heights. Consider the case of three
3763 successive redisplays, where the first displays 5
3764 lines, the second 3 lines, and the third 5 lines again.
3765 If the second redisplay wouldn't mark rows in the
3766 current matrix invalid, the third redisplay might be
3767 tempted to optimize redisplay based on lines displayed
3768 in the first redisplay. */
3769 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3770 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3771 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3774 /* Was display preempted? */
3775 paused_p = row < end;
3777 set_cursor:
3779 /* Update the header line after scrolling because a new header
3780 line would otherwise overwrite lines at the top of the window
3781 that can be scrolled. */
3782 if (header_line_row && header_line_row->enabled_p)
3784 header_line_row->y = 0;
3785 update_window_line (w, 0, &mouse_face_overwritten_p);
3788 /* Fix the appearance of overlapping/overlapped rows. */
3789 if (!paused_p && !w->pseudo_window_p)
3791 #ifdef HAVE_WINDOW_SYSTEM
3792 if (changed_p && rif->fix_overlapping_area)
3794 redraw_overlapped_rows (w, yb);
3795 redraw_overlapping_rows (w, yb);
3797 #endif
3799 /* Make cursor visible at cursor position of W. */
3800 set_window_cursor_after_update (w);
3802 #if 0 /* Check that current matrix invariants are satisfied. This is
3803 for debugging only. See the comment of check_matrix_invariants. */
3804 IF_DEBUG (check_matrix_invariants (w));
3805 #endif
3808 #if GLYPH_DEBUG
3809 /* Remember the redisplay method used to display the matrix. */
3810 strcpy (w->current_matrix->method, w->desired_matrix->method);
3811 #endif
3813 #ifdef HAVE_WINDOW_SYSTEM
3814 update_window_fringes (w, 0);
3815 #endif
3817 /* End the update of window W. Don't set the cursor if we
3818 paused updating the display because in this case,
3819 set_window_cursor_after_update hasn't been called, and
3820 output_cursor doesn't contain the cursor location. */
3821 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3823 else
3824 paused_p = 1;
3826 #if GLYPH_DEBUG
3827 /* check_current_matrix_flags (w); */
3828 add_window_display_history (w, w->current_matrix->method, paused_p);
3829 #endif
3831 clear_glyph_matrix (desired_matrix);
3833 return paused_p;
3837 /* Update the display of area AREA in window W, row number VPOS.
3838 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3840 static void
3841 update_marginal_area (struct window *w, int area, int vpos)
3843 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3844 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3846 /* Let functions in xterm.c know what area subsequent X positions
3847 will be relative to. */
3848 updated_area = area;
3850 /* Set cursor to start of glyphs, write them, and clear to the end
3851 of the area. I don't think that something more sophisticated is
3852 necessary here, since marginal areas will not be the default. */
3853 rif->cursor_to (vpos, 0, desired_row->y, 0);
3854 if (desired_row->used[area])
3855 rif->write_glyphs (desired_row->glyphs[area], desired_row->used[area]);
3856 rif->clear_end_of_line (-1);
3860 /* Update the display of the text area of row VPOS in window W.
3861 Value is non-zero if display has changed. */
3863 static int
3864 update_text_area (struct window *w, int vpos)
3866 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3867 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3868 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3869 int changed_p = 0;
3871 /* Let functions in xterm.c know what area subsequent X positions
3872 will be relative to. */
3873 updated_area = TEXT_AREA;
3875 /* If rows are at different X or Y, or rows have different height,
3876 or the current row is marked invalid, write the entire line. */
3877 if (!current_row->enabled_p
3878 || desired_row->y != current_row->y
3879 || desired_row->ascent != current_row->ascent
3880 || desired_row->phys_ascent != current_row->phys_ascent
3881 || desired_row->phys_height != current_row->phys_height
3882 || desired_row->visible_height != current_row->visible_height
3883 || current_row->overlapped_p
3884 /* This next line is necessary for correctly redrawing
3885 mouse-face areas after scrolling and other operations.
3886 However, it causes excessive flickering when mouse is moved
3887 across the mode line. Luckily, turning it off for the mode
3888 line doesn't seem to hurt anything. -- cyd.
3889 But it is still needed for the header line. -- kfs. */
3890 || (current_row->mouse_face_p
3891 && !(current_row->mode_line_p && vpos > 0))
3892 || current_row->x != desired_row->x)
3894 rif->cursor_to (vpos, 0, desired_row->y, desired_row->x);
3896 if (desired_row->used[TEXT_AREA])
3897 rif->write_glyphs (desired_row->glyphs[TEXT_AREA],
3898 desired_row->used[TEXT_AREA]);
3900 /* Clear to end of window. */
3901 rif->clear_end_of_line (-1);
3902 changed_p = 1;
3904 /* This erases the cursor. We do this here because
3905 notice_overwritten_cursor cannot easily check this, which
3906 might indicate that the whole functionality of
3907 notice_overwritten_cursor would better be implemented here.
3908 On the other hand, we need notice_overwritten_cursor as long
3909 as mouse highlighting is done asynchronously outside of
3910 redisplay. */
3911 if (vpos == w->phys_cursor.vpos)
3912 w->phys_cursor_on_p = 0;
3914 else
3916 int stop, i, x;
3917 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3918 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3919 int overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3920 int desired_stop_pos = desired_row->used[TEXT_AREA];
3921 int abort_skipping = 0;
3923 /* If the desired row extends its face to the text area end, and
3924 unless the current row also does so at the same position,
3925 make sure we write at least one glyph, so that the face
3926 extension actually takes place. */
3927 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3928 && (desired_stop_pos < current_row->used[TEXT_AREA]
3929 || (desired_stop_pos == current_row->used[TEXT_AREA]
3930 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3931 --desired_stop_pos;
3933 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3934 i = 0;
3935 x = desired_row->x;
3937 /* Loop over glyphs that current and desired row may have
3938 in common. */
3939 while (i < stop)
3941 int can_skip_p = !abort_skipping;
3943 /* Skip over glyphs that both rows have in common. These
3944 don't have to be written. We can't skip if the last
3945 current glyph overlaps the glyph to its right. For
3946 example, consider a current row of `if ' with the `f' in
3947 Courier bold so that it overlaps the ` ' to its right.
3948 If the desired row is ` ', we would skip over the space
3949 after the `if' and there would remain a pixel from the
3950 `f' on the screen. */
3951 if (overlapping_glyphs_p && i > 0)
3953 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3954 int left, right;
3956 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3957 &left, &right);
3958 can_skip_p = (right == 0 && !abort_skipping);
3961 if (can_skip_p)
3963 int start_hpos = i;
3965 while (i < stop
3966 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3968 x += desired_glyph->pixel_width;
3969 ++desired_glyph, ++current_glyph, ++i;
3972 /* Consider the case that the current row contains "xxx
3973 ppp ggg" in italic Courier font, and the desired row
3974 is "xxx ggg". The character `p' has lbearing, `g'
3975 has not. The loop above will stop in front of the
3976 first `p' in the current row. If we would start
3977 writing glyphs there, we wouldn't erase the lbearing
3978 of the `p'. The rest of the lbearing problem is then
3979 taken care of by draw_glyphs. */
3980 if (overlapping_glyphs_p
3981 && i > 0
3982 && i < current_row->used[TEXT_AREA]
3983 && (current_row->used[TEXT_AREA]
3984 != desired_row->used[TEXT_AREA]))
3986 int left, right;
3988 rif->get_glyph_overhangs (current_glyph, XFRAME (w->frame),
3989 &left, &right);
3990 while (left > 0 && i > 0)
3992 --i, --desired_glyph, --current_glyph;
3993 x -= desired_glyph->pixel_width;
3994 left -= desired_glyph->pixel_width;
3997 /* Abort the skipping algorithm if we end up before
3998 our starting point, to avoid looping (bug#1070).
3999 This can happen when the lbearing is larger than
4000 the pixel width. */
4001 abort_skipping = (i < start_hpos);
4005 /* Try to avoid writing the entire rest of the desired row
4006 by looking for a resync point. This mainly prevents
4007 mode line flickering in the case the mode line is in
4008 fixed-pitch font, which it usually will be. */
4009 if (i < desired_row->used[TEXT_AREA])
4011 int start_x = x, start_hpos = i;
4012 struct glyph *start = desired_glyph;
4013 int current_x = x;
4014 int skip_first_p = !can_skip_p;
4016 /* Find the next glyph that's equal again. */
4017 while (i < stop
4018 && (skip_first_p
4019 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
4020 && x == current_x)
4022 x += desired_glyph->pixel_width;
4023 current_x += current_glyph->pixel_width;
4024 ++desired_glyph, ++current_glyph, ++i;
4025 skip_first_p = 0;
4028 if (i == start_hpos || x != current_x)
4030 i = start_hpos;
4031 x = start_x;
4032 desired_glyph = start;
4033 break;
4036 rif->cursor_to (vpos, start_hpos, desired_row->y, start_x);
4037 rif->write_glyphs (start, i - start_hpos);
4038 changed_p = 1;
4042 /* Write the rest. */
4043 if (i < desired_row->used[TEXT_AREA])
4045 rif->cursor_to (vpos, i, desired_row->y, x);
4046 rif->write_glyphs (desired_glyph, desired_row->used[TEXT_AREA] - i);
4047 changed_p = 1;
4050 /* Maybe clear to end of line. */
4051 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
4053 /* If new row extends to the end of the text area, nothing
4054 has to be cleared, if and only if we did a write_glyphs
4055 above. This is made sure by setting desired_stop_pos
4056 appropriately above. */
4057 xassert (i < desired_row->used[TEXT_AREA]
4058 || ((desired_row->used[TEXT_AREA]
4059 == current_row->used[TEXT_AREA])
4060 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
4062 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
4064 /* If old row extends to the end of the text area, clear. */
4065 if (i >= desired_row->used[TEXT_AREA])
4066 rif->cursor_to (vpos, i, desired_row->y,
4067 desired_row->pixel_width);
4068 rif->clear_end_of_line (-1);
4069 changed_p = 1;
4071 else if (desired_row->pixel_width < current_row->pixel_width)
4073 /* Otherwise clear to the end of the old row. Everything
4074 after that position should be clear already. */
4075 int x;
4077 if (i >= desired_row->used[TEXT_AREA])
4078 rif->cursor_to (vpos, i, desired_row->y,
4079 desired_row->pixel_width);
4081 /* If cursor is displayed at the end of the line, make sure
4082 it's cleared. Nowadays we don't have a phys_cursor_glyph
4083 with which to erase the cursor (because this method
4084 doesn't work with lbearing/rbearing), so we must do it
4085 this way. */
4086 if (vpos == w->phys_cursor.vpos
4087 && (desired_row->reversed_p
4088 ? (w->phys_cursor.hpos < 0)
4089 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
4091 w->phys_cursor_on_p = 0;
4092 x = -1;
4094 else
4095 x = current_row->pixel_width;
4096 rif->clear_end_of_line (x);
4097 changed_p = 1;
4101 return changed_p;
4105 /* Update row VPOS in window W. Value is non-zero if display has been
4106 changed. */
4108 static int
4109 update_window_line (struct window *w, int vpos, int *mouse_face_overwritten_p)
4111 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
4112 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
4113 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4114 int changed_p = 0;
4116 /* Set the row being updated. This is important to let xterm.c
4117 know what line height values are in effect. */
4118 updated_row = desired_row;
4120 /* A row can be completely invisible in case a desired matrix was
4121 built with a vscroll and then make_cursor_line_fully_visible shifts
4122 the matrix. Make sure to make such rows current anyway, since
4123 we need the correct y-position, for example, in the current matrix. */
4124 if (desired_row->mode_line_p
4125 || desired_row->visible_height > 0)
4127 xassert (desired_row->enabled_p);
4129 /* Update display of the left margin area, if there is one. */
4130 if (!desired_row->full_width_p
4131 && !NILP (w->left_margin_cols))
4133 changed_p = 1;
4134 update_marginal_area (w, LEFT_MARGIN_AREA, vpos);
4137 /* Update the display of the text area. */
4138 if (update_text_area (w, vpos))
4140 changed_p = 1;
4141 if (current_row->mouse_face_p)
4142 *mouse_face_overwritten_p = 1;
4145 /* Update display of the right margin area, if there is one. */
4146 if (!desired_row->full_width_p
4147 && !NILP (w->right_margin_cols))
4149 changed_p = 1;
4150 update_marginal_area (w, RIGHT_MARGIN_AREA, vpos);
4153 /* Draw truncation marks etc. */
4154 if (!current_row->enabled_p
4155 || desired_row->y != current_row->y
4156 || desired_row->visible_height != current_row->visible_height
4157 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
4158 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
4159 || current_row->redraw_fringe_bitmaps_p
4160 || desired_row->mode_line_p != current_row->mode_line_p
4161 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
4162 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
4163 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
4164 rif->after_update_window_line_hook (desired_row);
4167 /* Update current_row from desired_row. */
4168 make_current (w->desired_matrix, w->current_matrix, vpos);
4169 updated_row = NULL;
4170 return changed_p;
4174 /* Set the cursor after an update of window W. This function may only
4175 be called from update_window. */
4177 static void
4178 set_window_cursor_after_update (struct window *w)
4180 struct frame *f = XFRAME (w->frame);
4181 struct redisplay_interface *rif = FRAME_RIF (f);
4182 int cx, cy, vpos, hpos;
4184 /* Not intended for frame matrix updates. */
4185 xassert (FRAME_WINDOW_P (f));
4187 if (cursor_in_echo_area
4188 && !NILP (echo_area_buffer[0])
4189 /* If we are showing a message instead of the mini-buffer,
4190 show the cursor for the message instead. */
4191 && XWINDOW (minibuf_window) == w
4192 && EQ (minibuf_window, echo_area_window)
4193 /* These cases apply only to the frame that contains
4194 the active mini-buffer window. */
4195 && FRAME_HAS_MINIBUF_P (f)
4196 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4198 cx = cy = vpos = hpos = 0;
4200 if (cursor_in_echo_area >= 0)
4202 /* If the mini-buffer is several lines high, find the last
4203 line that has any text on it. Note: either all lines
4204 are enabled or none. Otherwise we wouldn't be able to
4205 determine Y. */
4206 struct glyph_row *row, *last_row;
4207 struct glyph *glyph;
4208 int yb = window_text_bottom_y (w);
4210 last_row = NULL;
4211 row = w->current_matrix->rows;
4212 while (row->enabled_p
4213 && (last_row == NULL
4214 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
4216 if (row->used[TEXT_AREA]
4217 && row->glyphs[TEXT_AREA][0].charpos >= 0)
4218 last_row = row;
4219 ++row;
4222 if (last_row)
4224 struct glyph *start = last_row->glyphs[TEXT_AREA];
4225 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
4227 while (last > start && last->charpos < 0)
4228 --last;
4230 for (glyph = start; glyph < last; ++glyph)
4232 cx += glyph->pixel_width;
4233 ++hpos;
4236 cy = last_row->y;
4237 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
4241 else
4243 cx = w->cursor.x;
4244 cy = w->cursor.y;
4245 hpos = w->cursor.hpos;
4246 vpos = w->cursor.vpos;
4249 /* Window cursor can be out of sync for horizontally split windows. */
4250 hpos = max (-1, hpos); /* -1 is for when cursor is on the left fringe */
4251 hpos = min (w->current_matrix->matrix_w - 1, hpos);
4252 vpos = max (0, vpos);
4253 vpos = min (w->current_matrix->nrows - 1, vpos);
4254 rif->cursor_to (vpos, hpos, cy, cx);
4258 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4259 tree rooted at W. */
4261 void
4262 set_window_update_flags (struct window *w, int on_p)
4264 while (w)
4266 if (!NILP (w->hchild))
4267 set_window_update_flags (XWINDOW (w->hchild), on_p);
4268 else if (!NILP (w->vchild))
4269 set_window_update_flags (XWINDOW (w->vchild), on_p);
4270 else
4271 w->must_be_updated_p = on_p;
4273 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4279 /***********************************************************************
4280 Window-Based Scrolling
4281 ***********************************************************************/
4283 /* Structure describing rows in scrolling_window. */
4285 struct row_entry
4287 /* Number of occurrences of this row in desired and current matrix. */
4288 int old_uses, new_uses;
4290 /* Vpos of row in new matrix. */
4291 int new_line_number;
4293 /* Bucket index of this row_entry in the hash table row_table. */
4294 int bucket;
4296 /* The row described by this entry. */
4297 struct glyph_row *row;
4299 /* Hash collision chain. */
4300 struct row_entry *next;
4303 /* A pool to allocate row_entry structures from, and the size of the
4304 pool. The pool is reallocated in scrolling_window when we find
4305 that we need a larger one. */
4307 static struct row_entry *row_entry_pool;
4308 static int row_entry_pool_size;
4310 /* Index of next free entry in row_entry_pool. */
4312 static int row_entry_idx;
4314 /* The hash table used during scrolling, and the table's size. This
4315 table is used to quickly identify equal rows in the desired and
4316 current matrix. */
4318 static struct row_entry **row_table;
4319 static int row_table_size;
4321 /* Vectors of pointers to row_entry structures belonging to the
4322 current and desired matrix, and the size of the vectors. */
4324 static struct row_entry **old_lines, **new_lines;
4325 static int old_lines_size, new_lines_size;
4327 /* A pool to allocate run structures from, and its size. */
4329 static struct run *run_pool;
4330 static int runs_size;
4332 /* A vector of runs of lines found during scrolling. */
4334 static struct run **runs;
4336 /* Add glyph row ROW to the scrolling hash table during the scrolling
4337 of window W. */
4339 static INLINE struct row_entry *
4340 add_row_entry (struct window *w, struct glyph_row *row)
4342 struct row_entry *entry;
4343 int i = row->hash % row_table_size;
4345 entry = row_table[i];
4346 while (entry && !row_equal_p (w, entry->row, row, 1))
4347 entry = entry->next;
4349 if (entry == NULL)
4351 entry = row_entry_pool + row_entry_idx++;
4352 entry->row = row;
4353 entry->old_uses = entry->new_uses = 0;
4354 entry->new_line_number = 0;
4355 entry->bucket = i;
4356 entry->next = row_table[i];
4357 row_table[i] = entry;
4360 return entry;
4364 /* Try to reuse part of the current display of W by scrolling lines.
4365 HEADER_LINE_P non-zero means W has a header line.
4367 The algorithm is taken from Communications of the ACM, Apr78 "A
4368 Technique for Isolating Differences Between Files." It should take
4369 O(N) time.
4371 A short outline of the steps of the algorithm
4373 1. Skip lines equal at the start and end of both matrices.
4375 2. Enter rows in the current and desired matrix into a symbol
4376 table, counting how often they appear in both matrices.
4378 3. Rows that appear exactly once in both matrices serve as anchors,
4379 i.e. we assume that such lines are likely to have been moved.
4381 4. Starting from anchor lines, extend regions to be scrolled both
4382 forward and backward.
4384 Value is
4386 -1 if all rows were found to be equal.
4387 0 to indicate that we did not scroll the display, or
4388 1 if we did scroll. */
4390 static int
4391 scrolling_window (struct window *w, int header_line_p)
4393 struct glyph_matrix *desired_matrix = w->desired_matrix;
4394 struct glyph_matrix *current_matrix = w->current_matrix;
4395 int yb = window_text_bottom_y (w);
4396 int i, j, first_old, first_new, last_old, last_new;
4397 int nruns, nbytes, n, run_idx;
4398 struct row_entry *entry;
4399 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4401 /* Skip over rows equal at the start. */
4402 for (i = header_line_p ? 1 : 0; i < current_matrix->nrows - 1; ++i)
4404 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4405 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4407 if (c->enabled_p
4408 && d->enabled_p
4409 && !d->redraw_fringe_bitmaps_p
4410 && c->y == d->y
4411 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4412 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4413 && row_equal_p (w, c, d, 1))
4415 assign_row (c, d);
4416 d->enabled_p = 0;
4418 else
4419 break;
4422 /* Give up if some rows in the desired matrix are not enabled. */
4423 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4424 return -1;
4426 first_old = first_new = i;
4428 /* Set last_new to the index + 1 of the last enabled row in the
4429 desired matrix. */
4430 i = first_new + 1;
4431 while (i < desired_matrix->nrows - 1
4432 && MATRIX_ROW (desired_matrix, i)->enabled_p
4433 && MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i)) <= yb)
4434 ++i;
4436 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4437 return 0;
4439 last_new = i;
4441 /* Set last_old to the index + 1 of the last enabled row in the
4442 current matrix. We don't look at the enabled flag here because
4443 we plan to reuse part of the display even if other parts are
4444 disabled. */
4445 i = first_old + 1;
4446 while (i < current_matrix->nrows - 1)
4448 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4449 if (bottom <= yb)
4450 ++i;
4451 if (bottom >= yb)
4452 break;
4455 last_old = i;
4457 /* Skip over rows equal at the bottom. */
4458 i = last_new;
4459 j = last_old;
4460 while (i - 1 > first_new
4461 && j - 1 > first_old
4462 && MATRIX_ROW (current_matrix, i - 1)->enabled_p
4463 && (MATRIX_ROW (current_matrix, i - 1)->y
4464 == MATRIX_ROW (desired_matrix, j - 1)->y)
4465 && !MATRIX_ROW (desired_matrix, j - 1)->redraw_fringe_bitmaps_p
4466 && row_equal_p (w,
4467 MATRIX_ROW (desired_matrix, i - 1),
4468 MATRIX_ROW (current_matrix, j - 1), 1))
4469 --i, --j;
4470 last_new = i;
4471 last_old = j;
4473 /* Nothing to do if all rows are equal. */
4474 if (last_new == first_new)
4475 return 0;
4477 /* Reallocate vectors, tables etc. if necessary. */
4479 if (current_matrix->nrows > old_lines_size)
4481 old_lines_size = current_matrix->nrows;
4482 nbytes = old_lines_size * sizeof *old_lines;
4483 old_lines = (struct row_entry **) xrealloc (old_lines, nbytes);
4486 if (desired_matrix->nrows > new_lines_size)
4488 new_lines_size = desired_matrix->nrows;
4489 nbytes = new_lines_size * sizeof *new_lines;
4490 new_lines = (struct row_entry **) xrealloc (new_lines, nbytes);
4493 n = desired_matrix->nrows + current_matrix->nrows;
4494 if (3 * n > row_table_size)
4496 row_table_size = next_almost_prime (3 * n);
4497 nbytes = row_table_size * sizeof *row_table;
4498 row_table = (struct row_entry **) xrealloc (row_table, nbytes);
4499 memset (row_table, 0, nbytes);
4502 if (n > row_entry_pool_size)
4504 row_entry_pool_size = n;
4505 nbytes = row_entry_pool_size * sizeof *row_entry_pool;
4506 row_entry_pool = (struct row_entry *) xrealloc (row_entry_pool, nbytes);
4509 if (desired_matrix->nrows > runs_size)
4511 runs_size = desired_matrix->nrows;
4512 nbytes = runs_size * sizeof *runs;
4513 runs = (struct run **) xrealloc (runs, nbytes);
4514 nbytes = runs_size * sizeof *run_pool;
4515 run_pool = (struct run *) xrealloc (run_pool, nbytes);
4518 nruns = run_idx = 0;
4519 row_entry_idx = 0;
4521 /* Add rows from the current and desired matrix to the hash table
4522 row_hash_table to be able to find equal ones quickly. */
4524 for (i = first_old; i < last_old; ++i)
4526 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4528 entry = add_row_entry (w, MATRIX_ROW (current_matrix, i));
4529 old_lines[i] = entry;
4530 ++entry->old_uses;
4532 else
4533 old_lines[i] = NULL;
4536 for (i = first_new; i < last_new; ++i)
4538 xassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4539 entry = add_row_entry (w, MATRIX_ROW (desired_matrix, i));
4540 ++entry->new_uses;
4541 entry->new_line_number = i;
4542 new_lines[i] = entry;
4545 /* Identify moves based on lines that are unique and equal
4546 in both matrices. */
4547 for (i = first_old; i < last_old;)
4548 if (old_lines[i]
4549 && old_lines[i]->old_uses == 1
4550 && old_lines[i]->new_uses == 1)
4552 int j, k;
4553 int new_line = old_lines[i]->new_line_number;
4554 struct run *run = run_pool + run_idx++;
4556 /* Record move. */
4557 run->current_vpos = i;
4558 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4559 run->desired_vpos = new_line;
4560 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4561 run->nrows = 1;
4562 run->height = MATRIX_ROW (current_matrix, i)->height;
4564 /* Extend backward. */
4565 j = i - 1;
4566 k = new_line - 1;
4567 while (j > first_old
4568 && k > first_new
4569 && old_lines[j] == new_lines[k])
4571 int h = MATRIX_ROW (current_matrix, j)->height;
4572 --run->current_vpos;
4573 --run->desired_vpos;
4574 ++run->nrows;
4575 run->height += h;
4576 run->desired_y -= h;
4577 run->current_y -= h;
4578 --j, --k;
4581 /* Extend forward. */
4582 j = i + 1;
4583 k = new_line + 1;
4584 while (j < last_old
4585 && k < last_new
4586 && old_lines[j] == new_lines[k])
4588 int h = MATRIX_ROW (current_matrix, j)->height;
4589 ++run->nrows;
4590 run->height += h;
4591 ++j, ++k;
4594 /* Insert run into list of all runs. Order runs by copied
4595 pixel lines. Note that we record runs that don't have to
4596 be copied because they are already in place. This is done
4597 because we can avoid calling update_window_line in this
4598 case. */
4599 for (j = 0; j < nruns && runs[j]->height > run->height; ++j)
4601 for (k = nruns; k > j; --k)
4602 runs[k] = runs[k - 1];
4603 runs[j] = run;
4604 ++nruns;
4606 i += run->nrows;
4608 else
4609 ++i;
4611 /* Do the moves. Do it in a way that we don't overwrite something
4612 we want to copy later on. This is not solvable in general
4613 because there is only one display and we don't have a way to
4614 exchange areas on this display. Example:
4616 +-----------+ +-----------+
4617 | A | | B |
4618 +-----------+ --> +-----------+
4619 | B | | A |
4620 +-----------+ +-----------+
4622 Instead, prefer bigger moves, and invalidate moves that would
4623 copy from where we copied to. */
4625 for (i = 0; i < nruns; ++i)
4626 if (runs[i]->nrows > 0)
4628 struct run *r = runs[i];
4630 /* Copy on the display. */
4631 if (r->current_y != r->desired_y)
4633 rif->scroll_run_hook (w, r);
4635 /* Invalidate runs that copy from where we copied to. */
4636 for (j = i + 1; j < nruns; ++j)
4638 struct run *p = runs[j];
4640 if ((p->current_y >= r->desired_y
4641 && p->current_y < r->desired_y + r->height)
4642 || (p->current_y + p->height >= r->desired_y
4643 && (p->current_y + p->height
4644 < r->desired_y + r->height)))
4645 p->nrows = 0;
4649 /* Assign matrix rows. */
4650 for (j = 0; j < r->nrows; ++j)
4652 struct glyph_row *from, *to;
4653 int to_overlapped_p;
4655 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4656 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4657 to_overlapped_p = to->overlapped_p;
4658 if (!from->mode_line_p && !w->pseudo_window_p
4659 && (to->left_fringe_bitmap != from->left_fringe_bitmap
4660 || to->right_fringe_bitmap != from->right_fringe_bitmap
4661 || to->left_fringe_face_id != from->left_fringe_face_id
4662 || to->right_fringe_face_id != from->right_fringe_face_id
4663 || to->overlay_arrow_bitmap != from->overlay_arrow_bitmap))
4664 from->redraw_fringe_bitmaps_p = 1;
4665 assign_row (to, from);
4666 to->enabled_p = 1, from->enabled_p = 0;
4667 to->overlapped_p = to_overlapped_p;
4671 /* Clear the hash table, for the next time. */
4672 for (i = 0; i < row_entry_idx; ++i)
4673 row_table[row_entry_pool[i].bucket] = NULL;
4675 /* Value is > 0 to indicate that we scrolled the display. */
4676 return nruns;
4681 /************************************************************************
4682 Frame-Based Updates
4683 ************************************************************************/
4685 /* Update the desired frame matrix of frame F.
4687 FORCE_P non-zero means that the update should not be stopped by
4688 pending input. INHIBIT_HAIRY_ID_P non-zero means that scrolling
4689 should not be tried.
4691 Value is non-zero if update was stopped due to pending input. */
4693 static int
4694 update_frame_1 (struct frame *f, int force_p, int inhibit_id_p)
4696 /* Frame matrices to work on. */
4697 struct glyph_matrix *current_matrix = f->current_matrix;
4698 struct glyph_matrix *desired_matrix = f->desired_matrix;
4699 int i;
4700 int pause;
4701 int preempt_count = baud_rate / 2400 + 1;
4703 xassert (current_matrix && desired_matrix);
4705 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4706 calculate_costs (f);
4708 if (preempt_count <= 0)
4709 preempt_count = 1;
4711 #if !PERIODIC_PREEMPTION_CHECKING
4712 if (!force_p && detect_input_pending_ignore_squeezables ())
4714 pause = 1;
4715 goto do_pause;
4717 #endif
4719 /* If we cannot insert/delete lines, it's no use trying it. */
4720 if (!FRAME_LINE_INS_DEL_OK (f))
4721 inhibit_id_p = 1;
4723 /* See if any of the desired lines are enabled; don't compute for
4724 i/d line if just want cursor motion. */
4725 for (i = 0; i < desired_matrix->nrows; i++)
4726 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4727 break;
4729 /* Try doing i/d line, if not yet inhibited. */
4730 if (!inhibit_id_p && i < desired_matrix->nrows)
4731 force_p |= scrolling (f);
4733 /* Update the individual lines as needed. Do bottom line first. */
4734 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4735 update_frame_line (f, desired_matrix->nrows - 1);
4737 /* Now update the rest of the lines. */
4738 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4740 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4742 if (FRAME_TERMCAP_P (f))
4744 /* Flush out every so many lines.
4745 Also flush out if likely to have more than 1k buffered
4746 otherwise. I'm told that some telnet connections get
4747 really screwed by more than 1k output at once. */
4748 FILE *display_output = FRAME_TTY (f)->output;
4749 if (display_output)
4751 int outq = PENDING_OUTPUT_COUNT (display_output);
4752 if (outq > 900
4753 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4755 fflush (display_output);
4756 if (preempt_count == 1)
4758 #ifdef EMACS_OUTQSIZE
4759 if (EMACS_OUTQSIZE (0, &outq) < 0)
4760 /* Probably not a tty. Ignore the error and reset
4761 the outq count. */
4762 outq = PENDING_OUTPUT_COUNT (FRAME_TTY (f->output));
4763 #endif
4764 outq *= 10;
4765 if (baud_rate <= outq && baud_rate > 0)
4766 sleep (outq / baud_rate);
4772 #if PERIODIC_PREEMPTION_CHECKING
4773 if (!force_p)
4775 EMACS_TIME tm, dif;
4776 EMACS_GET_TIME (tm);
4777 EMACS_SUB_TIME (dif, preemption_next_check, tm);
4778 if (EMACS_TIME_NEG_P (dif))
4780 EMACS_ADD_TIME (preemption_next_check, tm, preemption_period);
4781 if (detect_input_pending_ignore_squeezables ())
4782 break;
4785 #else
4786 if (!force_p && (i - 1) % preempt_count == 0)
4787 detect_input_pending_ignore_squeezables ();
4788 #endif
4790 update_frame_line (f, i);
4794 pause = (i < FRAME_LINES (f) - 1) ? i : 0;
4796 /* Now just clean up termcap drivers and set cursor, etc. */
4797 if (!pause)
4799 if ((cursor_in_echo_area
4800 /* If we are showing a message instead of the mini-buffer,
4801 show the cursor for the message instead of for the
4802 (now hidden) mini-buffer contents. */
4803 || (EQ (minibuf_window, selected_window)
4804 && EQ (minibuf_window, echo_area_window)
4805 && !NILP (echo_area_buffer[0])))
4806 /* These cases apply only to the frame that contains
4807 the active mini-buffer window. */
4808 && FRAME_HAS_MINIBUF_P (f)
4809 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4811 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4812 int row, col;
4814 if (cursor_in_echo_area < 0)
4816 /* Negative value of cursor_in_echo_area means put
4817 cursor at beginning of line. */
4818 row = top;
4819 col = 0;
4821 else
4823 /* Positive value of cursor_in_echo_area means put
4824 cursor at the end of the prompt. If the mini-buffer
4825 is several lines high, find the last line that has
4826 any text on it. */
4827 row = FRAME_LINES (f);
4830 --row;
4831 col = 0;
4833 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4835 /* Frame rows are filled up with spaces that
4836 must be ignored here. */
4837 struct glyph_row *r = MATRIX_ROW (current_matrix,
4838 row);
4839 struct glyph *start = r->glyphs[TEXT_AREA];
4840 struct glyph *last = start + r->used[TEXT_AREA];
4842 while (last > start
4843 && (last - 1)->charpos < 0)
4844 --last;
4846 col = last - start;
4849 while (row > top && col == 0);
4851 /* Make sure COL is not out of range. */
4852 if (col >= FRAME_CURSOR_X_LIMIT (f))
4854 /* If we have another row, advance cursor into it. */
4855 if (row < FRAME_LINES (f) - 1)
4857 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4858 row++;
4860 /* Otherwise move it back in range. */
4861 else
4862 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4866 cursor_to (f, row, col);
4868 else
4870 /* We have only one cursor on terminal frames. Use it to
4871 display the cursor of the selected window. */
4872 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4873 if (w->cursor.vpos >= 0
4874 /* The cursor vpos may be temporarily out of bounds
4875 in the following situation: There is one window,
4876 with the cursor in the lower half of it. The window
4877 is split, and a message causes a redisplay before
4878 a new cursor position has been computed. */
4879 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4881 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4882 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4884 if (INTEGERP (w->left_margin_cols))
4885 x += XFASTINT (w->left_margin_cols);
4887 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4888 cursor_to (f, y, x);
4893 #if !PERIODIC_PREEMPTION_CHECKING
4894 do_pause:
4895 #endif
4897 clear_desired_matrices (f);
4898 return pause;
4902 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4905 scrolling (struct frame *frame)
4907 int unchanged_at_top, unchanged_at_bottom;
4908 int window_size;
4909 int changed_lines;
4910 int *old_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4911 int *new_hash = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4912 int *draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4913 int *old_draw_cost = (int *) alloca (FRAME_LINES (frame) * sizeof (int));
4914 register int i;
4915 int free_at_end_vpos = FRAME_LINES (frame);
4916 struct glyph_matrix *current_matrix = frame->current_matrix;
4917 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4919 if (!current_matrix)
4920 abort ();
4922 /* Compute hash codes of all the lines. Also calculate number of
4923 changed lines, number of unchanged lines at the beginning, and
4924 number of unchanged lines at the end. */
4925 changed_lines = 0;
4926 unchanged_at_top = 0;
4927 unchanged_at_bottom = FRAME_LINES (frame);
4928 for (i = 0; i < FRAME_LINES (frame); i++)
4930 /* Give up on this scrolling if some old lines are not enabled. */
4931 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4932 return 0;
4933 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4934 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4936 /* This line cannot be redrawn, so don't let scrolling mess it. */
4937 new_hash[i] = old_hash[i];
4938 #define INFINITY 1000000 /* Taken from scroll.c */
4939 draw_cost[i] = INFINITY;
4941 else
4943 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4944 draw_cost[i] = line_draw_cost (desired_matrix, i);
4947 if (old_hash[i] != new_hash[i])
4949 changed_lines++;
4950 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4952 else if (i == unchanged_at_top)
4953 unchanged_at_top++;
4954 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4957 /* If changed lines are few, don't allow preemption, don't scroll. */
4958 if ((!FRAME_SCROLL_REGION_OK (frame)
4959 && changed_lines < baud_rate / 2400)
4960 || unchanged_at_bottom == FRAME_LINES (frame))
4961 return 1;
4963 window_size = (FRAME_LINES (frame) - unchanged_at_top
4964 - unchanged_at_bottom);
4966 if (FRAME_SCROLL_REGION_OK (frame))
4967 free_at_end_vpos -= unchanged_at_bottom;
4968 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4969 free_at_end_vpos = -1;
4971 /* If large window, fast terminal and few lines in common between
4972 current frame and desired frame, don't bother with i/d calc. */
4973 if (!FRAME_SCROLL_REGION_OK (frame)
4974 && window_size >= 18 && baud_rate > 2400
4975 && (window_size >=
4976 10 * scrolling_max_lines_saved (unchanged_at_top,
4977 FRAME_LINES (frame) - unchanged_at_bottom,
4978 old_hash, new_hash, draw_cost)))
4979 return 0;
4981 if (window_size < 2)
4982 return 0;
4984 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4985 draw_cost + unchanged_at_top - 1,
4986 old_draw_cost + unchanged_at_top - 1,
4987 old_hash + unchanged_at_top - 1,
4988 new_hash + unchanged_at_top - 1,
4989 free_at_end_vpos - unchanged_at_top);
4991 return 0;
4995 /* Count the number of blanks at the start of the vector of glyphs R
4996 which is LEN glyphs long. */
4998 static int
4999 count_blanks (struct glyph *r, int len)
5001 int i;
5003 for (i = 0; i < len; ++i)
5004 if (!CHAR_GLYPH_SPACE_P (r[i]))
5005 break;
5007 return i;
5011 /* Count the number of glyphs in common at the start of the glyph
5012 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
5013 of STR2. Value is the number of equal glyphs equal at the start. */
5015 static int
5016 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
5018 struct glyph *p1 = str1;
5019 struct glyph *p2 = str2;
5021 while (p1 < end1
5022 && p2 < end2
5023 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
5024 ++p1, ++p2;
5026 return p1 - str1;
5030 /* Char insertion/deletion cost vector, from term.c */
5032 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS((f))])
5035 /* Perform a frame-based update on line VPOS in frame FRAME. */
5037 static void
5038 update_frame_line (struct frame *f, int vpos)
5040 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
5041 int tem;
5042 int osp, nsp, begmatch, endmatch, olen, nlen;
5043 struct glyph_matrix *current_matrix = f->current_matrix;
5044 struct glyph_matrix *desired_matrix = f->desired_matrix;
5045 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
5046 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
5047 int must_write_whole_line_p;
5048 int write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
5049 int colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
5050 != FACE_TTY_DEFAULT_BG_COLOR);
5052 if (colored_spaces_p)
5053 write_spaces_p = 1;
5055 /* Current row not enabled means it has unknown contents. We must
5056 write the whole desired line in that case. */
5057 must_write_whole_line_p = !current_row->enabled_p;
5058 if (must_write_whole_line_p)
5060 obody = 0;
5061 olen = 0;
5063 else
5065 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
5066 olen = current_row->used[TEXT_AREA];
5068 /* Ignore trailing spaces, if we can. */
5069 if (!write_spaces_p)
5070 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
5071 olen--;
5074 current_row->enabled_p = 1;
5075 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
5077 /* If desired line is empty, just clear the line. */
5078 if (!desired_row->enabled_p)
5080 nlen = 0;
5081 goto just_erase;
5084 nbody = desired_row->glyphs[TEXT_AREA];
5085 nlen = desired_row->used[TEXT_AREA];
5086 nend = nbody + nlen;
5088 /* If display line has unknown contents, write the whole line. */
5089 if (must_write_whole_line_p)
5091 /* Ignore spaces at the end, if we can. */
5092 if (!write_spaces_p)
5093 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5094 --nlen;
5096 /* Write the contents of the desired line. */
5097 if (nlen)
5099 cursor_to (f, vpos, 0);
5100 write_glyphs (f, nbody, nlen);
5103 /* Don't call clear_end_of_line if we already wrote the whole
5104 line. The cursor will not be at the right margin in that
5105 case but in the line below. */
5106 if (nlen < FRAME_TOTAL_COLS (f))
5108 cursor_to (f, vpos, nlen);
5109 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
5111 else
5112 /* Make sure we are in the right row, otherwise cursor movement
5113 with cmgoto might use `ch' in the wrong row. */
5114 cursor_to (f, vpos, 0);
5116 make_current (desired_matrix, current_matrix, vpos);
5117 return;
5120 /* Pretend trailing spaces are not there at all,
5121 unless for one reason or another we must write all spaces. */
5122 if (!write_spaces_p)
5123 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
5124 nlen--;
5126 /* If there's no i/d char, quickly do the best we can without it. */
5127 if (!FRAME_CHAR_INS_DEL_OK (f))
5129 int i, j;
5131 /* Find the first glyph in desired row that doesn't agree with
5132 a glyph in the current row, and write the rest from there on. */
5133 for (i = 0; i < nlen; i++)
5135 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
5137 /* Find the end of the run of different glyphs. */
5138 j = i + 1;
5139 while (j < nlen
5140 && (j >= olen
5141 || !GLYPH_EQUAL_P (nbody + j, obody + j)
5142 || CHAR_GLYPH_PADDING_P (nbody[j])))
5143 ++j;
5145 /* Output this run of non-matching chars. */
5146 cursor_to (f, vpos, i);
5147 write_glyphs (f, nbody + i, j - i);
5148 i = j - 1;
5150 /* Now find the next non-match. */
5154 /* Clear the rest of the line, or the non-clear part of it. */
5155 if (olen > nlen)
5157 cursor_to (f, vpos, nlen);
5158 clear_end_of_line (f, olen);
5161 /* Make current row = desired row. */
5162 make_current (desired_matrix, current_matrix, vpos);
5163 return;
5166 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5167 characters in a row. */
5169 if (!olen)
5171 /* If current line is blank, skip over initial spaces, if
5172 possible, and write the rest. */
5173 if (write_spaces_p)
5174 nsp = 0;
5175 else
5176 nsp = count_blanks (nbody, nlen);
5178 if (nlen > nsp)
5180 cursor_to (f, vpos, nsp);
5181 write_glyphs (f, nbody + nsp, nlen - nsp);
5184 /* Exchange contents between current_frame and new_frame. */
5185 make_current (desired_matrix, current_matrix, vpos);
5186 return;
5189 /* Compute number of leading blanks in old and new contents. */
5190 osp = count_blanks (obody, olen);
5191 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
5193 /* Compute number of matching chars starting with first non-blank. */
5194 begmatch = count_match (obody + osp, obody + olen,
5195 nbody + nsp, nbody + nlen);
5197 /* Spaces in new match implicit space past the end of old. */
5198 /* A bug causing this to be a no-op was fixed in 18.29. */
5199 if (!write_spaces_p && osp + begmatch == olen)
5201 np1 = nbody + nsp;
5202 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
5203 ++begmatch;
5206 /* Avoid doing insert/delete char
5207 just cause number of leading spaces differs
5208 when the following text does not match. */
5209 if (begmatch == 0 && osp != nsp)
5210 osp = nsp = min (osp, nsp);
5212 /* Find matching characters at end of line */
5213 op1 = obody + olen;
5214 np1 = nbody + nlen;
5215 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
5216 while (op1 > op2
5217 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
5219 op1--;
5220 np1--;
5222 endmatch = obody + olen - op1;
5224 /* tem gets the distance to insert or delete.
5225 endmatch is how many characters we save by doing so.
5226 Is it worth it? */
5228 tem = (nlen - nsp) - (olen - osp);
5229 if (endmatch && tem
5230 && (!FRAME_CHAR_INS_DEL_OK (f)
5231 || endmatch <= char_ins_del_cost (f)[tem]))
5232 endmatch = 0;
5234 /* nsp - osp is the distance to insert or delete.
5235 If that is nonzero, begmatch is known to be nonzero also.
5236 begmatch + endmatch is how much we save by doing the ins/del.
5237 Is it worth it? */
5239 if (nsp != osp
5240 && (!FRAME_CHAR_INS_DEL_OK (f)
5241 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
5243 begmatch = 0;
5244 endmatch = 0;
5245 osp = nsp = min (osp, nsp);
5248 /* Now go through the line, inserting, writing and
5249 deleting as appropriate. */
5251 if (osp > nsp)
5253 cursor_to (f, vpos, nsp);
5254 delete_glyphs (f, osp - nsp);
5256 else if (nsp > osp)
5258 /* If going to delete chars later in line
5259 and insert earlier in the line,
5260 must delete first to avoid losing data in the insert */
5261 if (endmatch && nlen < olen + nsp - osp)
5263 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
5264 delete_glyphs (f, olen + nsp - osp - nlen);
5265 olen = nlen - (nsp - osp);
5267 cursor_to (f, vpos, osp);
5268 insert_glyphs (f, 0, nsp - osp);
5270 olen += nsp - osp;
5272 tem = nsp + begmatch + endmatch;
5273 if (nlen != tem || olen != tem)
5275 if (!endmatch || nlen == olen)
5277 /* If new text being written reaches right margin, there is
5278 no need to do clear-to-eol at the end of this function
5279 (and it would not be safe, since cursor is not going to
5280 be "at the margin" after the text is done). */
5281 if (nlen == FRAME_TOTAL_COLS (f))
5282 olen = 0;
5284 /* Function write_glyphs is prepared to do nothing
5285 if passed a length <= 0. Check it here to avoid
5286 unnecessary cursor movement. */
5287 if (nlen - tem > 0)
5289 cursor_to (f, vpos, nsp + begmatch);
5290 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5293 else if (nlen > olen)
5295 /* Here, we used to have the following simple code:
5296 ----------------------------------------
5297 write_glyphs (nbody + nsp + begmatch, olen - tem);
5298 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5299 ----------------------------------------
5300 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5301 is a padding glyph. */
5302 int out = olen - tem; /* Columns to be overwritten originally. */
5303 int del;
5305 cursor_to (f, vpos, nsp + begmatch);
5307 /* Calculate columns we can actually overwrite. */
5308 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
5309 out--;
5310 write_glyphs (f, nbody + nsp + begmatch, out);
5312 /* If we left columns to be overwritten, we must delete them. */
5313 del = olen - tem - out;
5314 if (del > 0)
5315 delete_glyphs (f, del);
5317 /* At last, we insert columns not yet written out. */
5318 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
5319 olen = nlen;
5321 else if (olen > nlen)
5323 cursor_to (f, vpos, nsp + begmatch);
5324 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
5325 delete_glyphs (f, olen - nlen);
5326 olen = nlen;
5330 just_erase:
5331 /* If any unerased characters remain after the new line, erase them. */
5332 if (olen > nlen)
5334 cursor_to (f, vpos, nlen);
5335 clear_end_of_line (f, olen);
5338 /* Exchange contents between current_frame and new_frame. */
5339 make_current (desired_matrix, current_matrix, vpos);
5344 /***********************************************************************
5345 X/Y Position -> Buffer Position
5346 ***********************************************************************/
5348 /* Determine what's under window-relative pixel position (*X, *Y).
5349 Return the OBJECT (string or buffer) that's there.
5350 Return in *POS the position in that object.
5351 Adjust *X and *Y to character positions.
5352 Return in *DX and *DY the pixel coordinates of the click,
5353 relative to the top left corner of OBJECT, or relative to
5354 the top left corner of the character glyph at (*X, *Y)
5355 if OBJECT is nil.
5356 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5357 if the coordinates point to an empty area of the display. */
5359 Lisp_Object
5360 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)
5362 struct it it;
5363 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5364 struct text_pos startp;
5365 Lisp_Object string;
5366 struct glyph_row *row;
5367 #ifdef HAVE_WINDOW_SYSTEM
5368 struct image *img = 0;
5369 #endif
5370 int x0, x1, to_x;
5372 /* We used to set current_buffer directly here, but that does the
5373 wrong thing with `face-remapping-alist' (bug#2044). */
5374 Fset_buffer (w->buffer);
5375 SET_TEXT_POS_FROM_MARKER (startp, w->start);
5376 CHARPOS (startp) = min (ZV, max (BEGV, CHARPOS (startp)));
5377 BYTEPOS (startp) = min (ZV_BYTE, max (BEGV_BYTE, BYTEPOS (startp)));
5378 start_display (&it, w, startp);
5380 x0 = *x;
5382 /* First, move to the beginning of the row corresponding to *Y. We
5383 need to be in that row to get the correct value of base paragraph
5384 direction for the text at (*X, *Y). */
5385 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5387 /* TO_X is the pixel position that the iterator will compute for the
5388 glyph at *X. We add it.first_visible_x because iterator
5389 positions include the hscroll. */
5390 to_x = x0 + it.first_visible_x;
5391 if (it.bidi_it.paragraph_dir == R2L)
5392 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5393 text area. This is because the iterator, even in R2L
5394 paragraphs, delivers glyphs as if they started at the left
5395 margin of the window. (When we actually produce glyphs for
5396 display, we reverse their order in PRODUCE_GLYPHS, but the
5397 iterator doesn't know about that.) The following line adjusts
5398 the pixel position to the iterator geometry, which is what
5399 move_it_* routines use. (The -1 is because in a window whose
5400 text-area width is W, the rightmost pixel position is W-1, and
5401 it should be mirrored into zero pixel position.) */
5402 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5404 /* Now move horizontally in the row to the glyph under *X. Second
5405 argument is ZV to prevent move_it_in_display_line from matching
5406 based on buffer positions. */
5407 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5409 Fset_buffer (old_current_buffer);
5411 *dx = x0 + it.first_visible_x - it.current_x;
5412 *dy = *y - it.current_y;
5414 string = w->buffer;
5415 if (STRINGP (it.string))
5416 string = it.string;
5417 *pos = it.current;
5418 if (it.what == IT_COMPOSITION
5419 && it.cmp_it.nchars > 1
5420 && it.cmp_it.reversed_p)
5422 /* The current display element is a grapheme cluster in a
5423 composition. In that case, we need the position of the first
5424 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5425 it.current points to the last character of the cluster, thus
5426 we must move back to the first character of the same
5427 cluster. */
5428 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5429 if (STRINGP (it.string))
5430 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5431 else
5432 BYTEPOS (pos->pos) = CHAR_TO_BYTE (CHARPOS (pos->pos));
5435 #ifdef HAVE_WINDOW_SYSTEM
5436 if (it.what == IT_IMAGE)
5438 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5439 && !NILP (img->spec))
5440 *object = img->spec;
5442 #endif
5444 if (it.vpos < w->current_matrix->nrows
5445 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5446 row->enabled_p))
5448 if (it.hpos < row->used[TEXT_AREA])
5450 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5451 #ifdef HAVE_WINDOW_SYSTEM
5452 if (img)
5454 *dy -= row->ascent - glyph->ascent;
5455 *dx += glyph->slice.img.x;
5456 *dy += glyph->slice.img.y;
5457 /* Image slices positions are still relative to the entire image */
5458 *width = img->width;
5459 *height = img->height;
5461 else
5462 #endif
5464 *width = glyph->pixel_width;
5465 *height = glyph->ascent + glyph->descent;
5468 else
5470 *width = 0;
5471 *height = row->height;
5474 else
5476 *width = *height = 0;
5479 /* Add extra (default width) columns if clicked after EOL. */
5480 x1 = max(0, it.current_x + it.pixel_width - it.first_visible_x);
5481 if (x0 > x1)
5482 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5484 *x = it.hpos;
5485 *y = it.vpos;
5487 return string;
5491 /* Value is the string under window-relative coordinates X/Y in the
5492 mode line or header line (PART says which) of window W, or nil if none.
5493 *CHARPOS is set to the position in the string returned. */
5495 Lisp_Object
5496 mode_line_string (struct window *w, enum window_part part,
5497 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5498 int *dx, int *dy, int *width, int *height)
5500 struct glyph_row *row;
5501 struct glyph *glyph, *end;
5502 int x0, y0;
5503 Lisp_Object string = Qnil;
5505 if (part == ON_MODE_LINE)
5506 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5507 else
5508 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5509 y0 = *y - row->y;
5510 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5512 if (row->mode_line_p && row->enabled_p)
5514 /* Find the glyph under X. If we find one with a string object,
5515 it's the one we were looking for. */
5516 glyph = row->glyphs[TEXT_AREA];
5517 end = glyph + row->used[TEXT_AREA];
5518 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5519 x0 -= glyph->pixel_width;
5520 *x = glyph - row->glyphs[TEXT_AREA];
5521 if (glyph < end)
5523 string = glyph->object;
5524 *charpos = glyph->charpos;
5525 *width = glyph->pixel_width;
5526 *height = glyph->ascent + glyph->descent;
5527 #ifdef HAVE_WINDOW_SYSTEM
5528 if (glyph->type == IMAGE_GLYPH)
5530 struct image *img;
5531 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5532 if (img != NULL)
5533 *object = img->spec;
5534 y0 -= row->ascent - glyph->ascent;
5536 #endif
5538 else
5540 /* Add extra (default width) columns if clicked after EOL. */
5541 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5542 *width = 0;
5543 *height = row->height;
5546 else
5548 *x = 0;
5549 x0 = 0;
5550 *width = *height = 0;
5553 *dx = x0;
5554 *dy = y0;
5556 return string;
5560 /* Value is the string under window-relative coordinates X/Y in either
5561 marginal area, or nil if none. *CHARPOS is set to the position in
5562 the string returned. */
5564 Lisp_Object
5565 marginal_area_string (struct window *w, enum window_part part,
5566 int *x, int *y, EMACS_INT *charpos, Lisp_Object *object,
5567 int *dx, int *dy, int *width, int *height)
5569 struct glyph_row *row = w->current_matrix->rows;
5570 struct glyph *glyph, *end;
5571 int x0, y0, i, wy = *y;
5572 int area;
5573 Lisp_Object string = Qnil;
5575 if (part == ON_LEFT_MARGIN)
5576 area = LEFT_MARGIN_AREA;
5577 else if (part == ON_RIGHT_MARGIN)
5578 area = RIGHT_MARGIN_AREA;
5579 else
5580 abort ();
5582 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5583 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5584 break;
5585 y0 = *y - row->y;
5586 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5588 if (row->enabled_p)
5590 /* Find the glyph under X. If we find one with a string object,
5591 it's the one we were looking for. */
5592 if (area == RIGHT_MARGIN_AREA)
5593 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5594 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5595 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5596 + window_box_width (w, LEFT_MARGIN_AREA)
5597 + window_box_width (w, TEXT_AREA));
5598 else
5599 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5600 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5601 : 0);
5603 glyph = row->glyphs[area];
5604 end = glyph + row->used[area];
5605 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5606 x0 -= glyph->pixel_width;
5607 *x = glyph - row->glyphs[area];
5608 if (glyph < end)
5610 string = glyph->object;
5611 *charpos = glyph->charpos;
5612 *width = glyph->pixel_width;
5613 *height = glyph->ascent + glyph->descent;
5614 #ifdef HAVE_WINDOW_SYSTEM
5615 if (glyph->type == IMAGE_GLYPH)
5617 struct image *img;
5618 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5619 if (img != NULL)
5620 *object = img->spec;
5621 y0 -= row->ascent - glyph->ascent;
5622 x0 += glyph->slice.img.x;
5623 y0 += glyph->slice.img.y;
5625 #endif
5627 else
5629 /* Add extra (default width) columns if clicked after EOL. */
5630 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5631 *width = 0;
5632 *height = row->height;
5635 else
5637 x0 = 0;
5638 *x = 0;
5639 *width = *height = 0;
5642 *dx = x0;
5643 *dy = y0;
5645 return string;
5649 /***********************************************************************
5650 Changing Frame Sizes
5651 ***********************************************************************/
5653 #ifdef SIGWINCH
5655 SIGTYPE
5656 window_change_signal (int signalnum) /* If we don't have an argument, */
5657 /* some compilers complain in signal calls. */
5659 int width, height;
5660 int old_errno = errno;
5662 struct tty_display_info *tty;
5664 signal (SIGWINCH, window_change_signal);
5665 SIGNAL_THREAD_CHECK (signalnum);
5667 /* The frame size change obviously applies to a single
5668 termcap-controlled terminal, but we can't decide which.
5669 Therefore, we resize the frames corresponding to each tty.
5671 for (tty = tty_list; tty; tty = tty->next) {
5673 if (! tty->term_initted)
5674 continue;
5676 /* Suspended tty frames have tty->input == NULL avoid trying to
5677 use it. */
5678 if (!tty->input)
5679 continue;
5681 get_tty_size (fileno (tty->input), &width, &height);
5683 if (width > 5 && height > 2) {
5684 Lisp_Object tail, frame;
5686 FOR_EACH_FRAME (tail, frame)
5687 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5688 /* Record the new sizes, but don't reallocate the data
5689 structures now. Let that be done later outside of the
5690 signal handler. */
5691 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5695 errno = old_errno;
5697 #endif /* SIGWINCH */
5700 /* Do any change in frame size that was requested by a signal. SAFE
5701 non-zero means this function is called from a place where it is
5702 safe to change frame sizes while a redisplay is in progress. */
5704 void
5705 do_pending_window_change (int safe)
5707 /* If window_change_signal should have run before, run it now. */
5708 if (redisplaying_p && !safe)
5709 return;
5711 while (delayed_size_change)
5713 Lisp_Object tail, frame;
5715 delayed_size_change = 0;
5717 FOR_EACH_FRAME (tail, frame)
5719 struct frame *f = XFRAME (frame);
5721 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5722 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5723 0, 0, safe);
5729 /* Change the frame height and/or width. Values may be given as zero to
5730 indicate no change is to take place.
5732 If DELAY is non-zero, then assume we're being called from a signal
5733 handler, and queue the change for later - perhaps the next
5734 redisplay. Since this tries to resize windows, we can't call it
5735 from a signal handler.
5737 SAFE non-zero means this function is called from a place where it's
5738 safe to change frame sizes while a redisplay is in progress. */
5740 void
5741 change_frame_size (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5743 Lisp_Object tail, frame;
5745 if (FRAME_MSDOS_P (f))
5747 /* On MS-DOS, all frames use the same screen, so a change in
5748 size affects all frames. Termcap now supports multiple
5749 ttys. */
5750 FOR_EACH_FRAME (tail, frame)
5751 if (! FRAME_WINDOW_P (XFRAME (frame)))
5752 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5753 pretend, delay, safe);
5755 else
5756 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5759 static void
5760 change_frame_size_1 (register struct frame *f, int newheight, int newwidth, int pretend, int delay, int safe)
5762 int new_frame_total_cols;
5763 int count = SPECPDL_INDEX ();
5765 /* If we can't deal with the change now, queue it for later. */
5766 if (delay || (redisplaying_p && !safe))
5768 f->new_text_lines = newheight;
5769 f->new_text_cols = newwidth;
5770 delayed_size_change = 1;
5771 return;
5774 /* This size-change overrides any pending one for this frame. */
5775 f->new_text_lines = 0;
5776 f->new_text_cols = 0;
5778 /* If an argument is zero, set it to the current value. */
5779 if (newheight == 0)
5780 newheight = FRAME_LINES (f);
5781 if (newwidth == 0)
5782 newwidth = FRAME_COLS (f);
5784 /* Compute width of windows in F.
5785 This is the width of the frame without vertical scroll bars. */
5786 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5788 /* Round up to the smallest acceptable size. */
5789 check_frame_size (f, &newheight, &newwidth);
5791 /* If we're not changing the frame size, quit now. */
5792 /* Frame width may be unchanged but the text portion may change, for example,
5793 fullscreen and remove/add scroll bar. */
5794 if (newheight == FRAME_LINES (f)
5795 && newwidth == FRAME_COLS (f) // text portion unchanged
5796 && new_frame_total_cols == FRAME_TOTAL_COLS (f)) // frame width unchanged
5797 return;
5799 BLOCK_INPUT;
5801 #ifdef MSDOS
5802 /* We only can set screen dimensions to certain values supported
5803 by our video hardware. Try to find the smallest size greater
5804 or equal to the requested dimensions. */
5805 dos_set_window_size (&newheight, &newwidth);
5806 #endif
5808 if (newheight != FRAME_LINES (f))
5810 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
5812 /* Frame has both root and mini-buffer. */
5813 XSETFASTINT (XWINDOW (FRAME_ROOT_WINDOW (f))->top_line,
5814 FRAME_TOP_MARGIN (f));
5815 set_window_height (FRAME_ROOT_WINDOW (f),
5816 (newheight
5818 - FRAME_TOP_MARGIN (f)),
5820 XSETFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top_line,
5821 newheight - 1);
5822 set_window_height (FRAME_MINIBUF_WINDOW (f), 1, 0);
5824 else
5825 /* Frame has just one top-level window. */
5826 set_window_height (FRAME_ROOT_WINDOW (f),
5827 newheight - FRAME_TOP_MARGIN (f), 2);
5829 /* MSDOS frames cannot PRETEND, as they change frame size by
5830 manipulating video hardware. */
5831 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5832 FrameRows (FRAME_TTY (f)) = newheight;
5835 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5837 set_window_width (FRAME_ROOT_WINDOW (f), new_frame_total_cols, 2);
5838 if (FRAME_HAS_MINIBUF_P (f))
5839 set_window_width (FRAME_MINIBUF_WINDOW (f), new_frame_total_cols, 0);
5841 /* MSDOS frames cannot PRETEND, as they change frame size by
5842 manipulating video hardware. */
5843 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5844 FrameCols (FRAME_TTY (f)) = newwidth;
5846 if (WINDOWP (f->tool_bar_window))
5847 XSETFASTINT (XWINDOW (f->tool_bar_window)->total_cols, newwidth);
5850 FRAME_LINES (f) = newheight;
5851 SET_FRAME_COLS (f, newwidth);
5854 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5855 int text_area_x, text_area_y, text_area_width, text_area_height;
5857 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5858 &text_area_height);
5859 if (w->cursor.x >= text_area_x + text_area_width)
5860 w->cursor.hpos = w->cursor.x = 0;
5861 if (w->cursor.y >= text_area_y + text_area_height)
5862 w->cursor.vpos = w->cursor.y = 0;
5865 adjust_glyphs (f);
5866 calculate_costs (f);
5867 SET_FRAME_GARBAGED (f);
5868 f->resized_p = 1;
5870 UNBLOCK_INPUT;
5872 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5874 run_window_configuration_change_hook (f);
5876 unbind_to (count, Qnil);
5881 /***********************************************************************
5882 Terminal Related Lisp Functions
5883 ***********************************************************************/
5885 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5886 1, 1, "FOpen termscript file: ",
5887 doc: /* Start writing all terminal output to FILE as well as the terminal.
5888 FILE = nil means just close any termscript file currently open. */)
5889 (Lisp_Object file)
5891 struct tty_display_info *tty;
5893 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5894 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5895 error ("Current frame is not on a tty device");
5897 tty = CURTTY ();
5899 if (tty->termscript != 0)
5901 BLOCK_INPUT;
5902 fclose (tty->termscript);
5903 UNBLOCK_INPUT;
5905 tty->termscript = 0;
5907 if (! NILP (file))
5909 file = Fexpand_file_name (file, Qnil);
5910 tty->termscript = fopen (SDATA (file), "w");
5911 if (tty->termscript == 0)
5912 report_file_error ("Opening termscript", Fcons (file, Qnil));
5914 return Qnil;
5918 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5919 Ssend_string_to_terminal, 1, 2, 0,
5920 doc: /* Send STRING to the terminal without alteration.
5921 Control characters in STRING will have terminal-dependent effects.
5923 Optional parameter TERMINAL specifies the tty terminal device to use.
5924 It may be a terminal object, a frame, or nil for the terminal used by
5925 the currently selected frame. In batch mode, STRING is sent to stdout
5926 when TERMINAL is nil. */)
5927 (Lisp_Object string, Lisp_Object terminal)
5929 struct terminal *t = get_terminal (terminal, 1);
5930 FILE *out;
5932 /* ??? Perhaps we should do something special for multibyte strings here. */
5933 CHECK_STRING (string);
5934 BLOCK_INPUT;
5936 if (!t)
5937 error ("Unknown terminal device");
5939 if (t->type == output_initial)
5940 out = stdout;
5941 else if (t->type != output_termcap && t->type != output_msdos_raw)
5942 error ("Device %d is not a termcap terminal device", t->id);
5943 else
5945 struct tty_display_info *tty = t->display_info.tty;
5947 if (! tty->output)
5948 error ("Terminal is currently suspended");
5950 if (tty->termscript)
5952 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5953 fflush (tty->termscript);
5955 out = tty->output;
5957 fwrite (SDATA (string), 1, SBYTES (string), out);
5958 fflush (out);
5959 UNBLOCK_INPUT;
5960 return Qnil;
5964 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5965 doc: /* Beep, or flash the screen.
5966 Also, unless an argument is given,
5967 terminate any keyboard macro currently executing. */)
5968 (Lisp_Object arg)
5970 if (!NILP (arg))
5972 if (noninteractive)
5973 putchar (07);
5974 else
5975 ring_bell (XFRAME (selected_frame));
5977 else
5978 bitch_at_user ();
5980 return Qnil;
5983 void
5984 bitch_at_user (void)
5986 if (noninteractive)
5987 putchar (07);
5988 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5989 error ("Keyboard macro terminated by a command ringing the bell");
5990 else
5991 ring_bell (XFRAME (selected_frame));
5996 /***********************************************************************
5997 Sleeping, Waiting
5998 ***********************************************************************/
6000 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
6001 doc: /* Pause, without updating display, for SECONDS seconds.
6002 SECONDS may be a floating-point value, meaning that you can wait for a
6003 fraction of a second. Optional second arg MILLISECONDS specifies an
6004 additional wait period, in milliseconds; this may be useful if your
6005 Emacs was built without floating point support.
6006 \(Not all operating systems support waiting for a fraction of a second.) */)
6007 (Lisp_Object seconds, Lisp_Object milliseconds)
6009 int sec, usec;
6011 if (NILP (milliseconds))
6012 XSETINT (milliseconds, 0);
6013 else
6014 CHECK_NUMBER (milliseconds);
6015 usec = XINT (milliseconds) * 1000;
6018 double duration = extract_float (seconds);
6019 sec = (int) duration;
6020 usec += (duration - sec) * 1000000;
6023 #ifndef EMACS_HAS_USECS
6024 if (sec == 0 && usec != 0)
6025 error ("Millisecond `sleep-for' not supported on %s", SYSTEM_TYPE);
6026 #endif
6028 /* Assure that 0 <= usec < 1000000. */
6029 if (usec < 0)
6031 /* We can't rely on the rounding being correct if usec is negative. */
6032 if (-1000000 < usec)
6033 sec--, usec += 1000000;
6034 else
6035 sec -= -usec / 1000000, usec = 1000000 - (-usec % 1000000);
6037 else
6038 sec += usec / 1000000, usec %= 1000000;
6040 if (sec < 0 || (sec == 0 && usec == 0))
6041 return Qnil;
6043 wait_reading_process_output (sec, usec, 0, 0, Qnil, NULL, 0);
6045 return Qnil;
6049 /* This is just like wait_reading_process_output, except that
6050 it does redisplay.
6052 TIMEOUT is number of seconds to wait (float or integer),
6053 or t to wait forever.
6054 READING is 1 if reading input.
6055 If DO_DISPLAY is >0 display process output while waiting.
6056 If DO_DISPLAY is >1 perform an initial redisplay before waiting.
6059 Lisp_Object
6060 sit_for (Lisp_Object timeout, int reading, int do_display)
6062 int sec, usec;
6064 swallow_events (do_display);
6066 if ((detect_input_pending_run_timers (do_display))
6067 || !NILP (Vexecuting_kbd_macro))
6068 return Qnil;
6070 if (do_display >= 2)
6071 redisplay_preserve_echo_area (2);
6073 if (INTEGERP (timeout))
6075 sec = XINT (timeout);
6076 usec = 0;
6078 else if (FLOATP (timeout))
6080 double seconds = XFLOAT_DATA (timeout);
6081 sec = (int) seconds;
6082 usec = (int) ((seconds - sec) * 1000000);
6084 else if (EQ (timeout, Qt))
6086 sec = 0;
6087 usec = 0;
6089 else
6090 wrong_type_argument (Qnumberp, timeout);
6092 if (sec == 0 && usec == 0 && !EQ (timeout, Qt))
6093 return Qt;
6095 #ifdef SIGIO
6096 gobble_input (0);
6097 #endif
6099 wait_reading_process_output (sec, usec, reading ? -1 : 1, do_display,
6100 Qnil, NULL, 0);
6102 return detect_input_pending () ? Qnil : Qt;
6106 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
6107 doc: /* Perform redisplay if no input is available.
6108 If optional arg FORCE is non-nil or `redisplay-dont-pause' is non-nil,
6109 perform a full redisplay even if input is available.
6110 Return t if redisplay was performed, nil otherwise. */)
6111 (Lisp_Object force)
6113 int count;
6115 swallow_events (1);
6116 if ((detect_input_pending_run_timers (1)
6117 && NILP (force) && !redisplay_dont_pause)
6118 || !NILP (Vexecuting_kbd_macro))
6119 return Qnil;
6121 count = SPECPDL_INDEX ();
6122 if (!NILP (force) && !redisplay_dont_pause)
6123 specbind (Qredisplay_dont_pause, Qt);
6124 redisplay_preserve_echo_area (2);
6125 unbind_to (count, Qnil);
6126 return Qt;
6131 /***********************************************************************
6132 Other Lisp Functions
6133 ***********************************************************************/
6135 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
6136 session's frames, frame names, buffers, buffer-read-only flags, and
6137 buffer-modified-flags. */
6139 static Lisp_Object frame_and_buffer_state;
6142 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
6143 Sframe_or_buffer_changed_p, 0, 1, 0,
6144 doc: /* Return non-nil if the frame and buffer state appears to have changed.
6145 VARIABLE is a variable name whose value is either nil or a state vector
6146 that will be updated to contain all frames and buffers,
6147 aside from buffers whose names start with space,
6148 along with the buffers' read-only and modified flags. This allows a fast
6149 check to see whether buffer menus might need to be recomputed.
6150 If this function returns non-nil, it updates the internal vector to reflect
6151 the current state.
6153 If VARIABLE is nil, an internal variable is used. Users should not
6154 pass nil for VARIABLE. */)
6155 (Lisp_Object variable)
6157 Lisp_Object state, tail, frame, buf;
6158 Lisp_Object *vecp, *end;
6159 int n;
6161 if (! NILP (variable))
6163 CHECK_SYMBOL (variable);
6164 state = Fsymbol_value (variable);
6165 if (! VECTORP (state))
6166 goto changed;
6168 else
6169 state = frame_and_buffer_state;
6171 vecp = XVECTOR (state)->contents;
6172 end = vecp + XVECTOR (state)->size;
6174 FOR_EACH_FRAME (tail, frame)
6176 if (vecp == end)
6177 goto changed;
6178 if (!EQ (*vecp++, frame))
6179 goto changed;
6180 if (vecp == end)
6181 goto changed;
6182 if (!EQ (*vecp++, XFRAME (frame)->name))
6183 goto changed;
6185 /* Check that the buffer info matches. */
6186 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6188 buf = XCDR (XCAR (tail));
6189 /* Ignore buffers that aren't included in buffer lists. */
6190 if (SREF (XBUFFER (buf)->name, 0) == ' ')
6191 continue;
6192 if (vecp == end)
6193 goto changed;
6194 if (!EQ (*vecp++, buf))
6195 goto changed;
6196 if (vecp == end)
6197 goto changed;
6198 if (!EQ (*vecp++, XBUFFER (buf)->read_only))
6199 goto changed;
6200 if (vecp == end)
6201 goto changed;
6202 if (!EQ (*vecp++, Fbuffer_modified_p (buf)))
6203 goto changed;
6205 if (vecp == end)
6206 goto changed;
6207 /* Detect deletion of a buffer at the end of the list. */
6208 if (EQ (*vecp, Qlambda))
6209 return Qnil;
6211 /* Come here if we decide the data has changed. */
6212 changed:
6213 /* Count the size we will need.
6214 Start with 1 so there is room for at least one lambda at the end. */
6215 n = 1;
6216 FOR_EACH_FRAME (tail, frame)
6217 n += 2;
6218 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6219 n += 3;
6220 /* Reallocate the vector if data has grown to need it,
6221 or if it has shrunk a lot. */
6222 if (! VECTORP (state)
6223 || n > XVECTOR (state)->size
6224 || n + 20 < XVECTOR (state)->size / 2)
6225 /* Add 20 extra so we grow it less often. */
6227 state = Fmake_vector (make_number (n + 20), Qlambda);
6228 if (! NILP (variable))
6229 Fset (variable, state);
6230 else
6231 frame_and_buffer_state = state;
6234 /* Record the new data in the (possibly reallocated) vector. */
6235 vecp = XVECTOR (state)->contents;
6236 FOR_EACH_FRAME (tail, frame)
6238 *vecp++ = frame;
6239 *vecp++ = XFRAME (frame)->name;
6241 for (tail = Vbuffer_alist; CONSP (tail); tail = XCDR (tail))
6243 buf = XCDR (XCAR (tail));
6244 /* Ignore buffers that aren't included in buffer lists. */
6245 if (SREF (XBUFFER (buf)->name, 0) == ' ')
6246 continue;
6247 *vecp++ = buf;
6248 *vecp++ = XBUFFER (buf)->read_only;
6249 *vecp++ = Fbuffer_modified_p (buf);
6251 /* Fill up the vector with lambdas (always at least one). */
6252 *vecp++ = Qlambda;
6253 while (vecp - XVECTOR (state)->contents
6254 < XVECTOR (state)->size)
6255 *vecp++ = Qlambda;
6256 /* Make sure we didn't overflow the vector. */
6257 if (vecp - XVECTOR (state)->contents
6258 > XVECTOR (state)->size)
6259 abort ();
6260 return Qt;
6265 /***********************************************************************
6266 Initialization
6267 ***********************************************************************/
6269 /* Initialization done when Emacs fork is started, before doing stty.
6270 Determine terminal type and set terminal_driver. Then invoke its
6271 decoding routine to set up variables in the terminal package. */
6273 void
6274 init_display (void)
6276 char *terminal_type;
6278 /* Construct the space glyph. */
6279 space_glyph.type = CHAR_GLYPH;
6280 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
6281 space_glyph.charpos = -1;
6283 inverse_video = 0;
6284 cursor_in_echo_area = 0;
6285 terminal_type = (char *) 0;
6287 /* Now is the time to initialize this; it's used by init_sys_modes
6288 during startup. */
6289 Vinitial_window_system = Qnil;
6291 /* SIGWINCH needs to be handled no matter what display we start
6292 with. Otherwise newly opened tty frames will not resize
6293 automatically. */
6294 #ifdef SIGWINCH
6295 #ifndef CANNOT_DUMP
6296 if (initialized)
6297 #endif /* CANNOT_DUMP */
6298 signal (SIGWINCH, window_change_signal);
6299 #endif /* SIGWINCH */
6301 /* If running as a daemon, no need to initialize any frames/terminal. */
6302 if (IS_DAEMON)
6303 return;
6305 /* If the user wants to use a window system, we shouldn't bother
6306 initializing the terminal. This is especially important when the
6307 terminal is so dumb that emacs gives up before and doesn't bother
6308 using the window system.
6310 If the DISPLAY environment variable is set and nonempty,
6311 try to use X, and die with an error message if that doesn't work. */
6313 #ifdef HAVE_X_WINDOWS
6314 if (! inhibit_window_system && ! display_arg)
6316 char *display;
6317 display = getenv ("DISPLAY");
6318 display_arg = (display != 0 && *display != 0);
6320 if (display_arg && !x_display_ok (display))
6322 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
6323 display);
6324 inhibit_window_system = 1;
6328 if (!inhibit_window_system && display_arg
6329 #ifndef CANNOT_DUMP
6330 && initialized
6331 #endif
6334 Vinitial_window_system = Qx;
6335 #ifdef HAVE_X11
6336 Vwindow_system_version = make_number (11);
6337 #endif
6338 #if defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
6339 /* In some versions of ncurses,
6340 tputs crashes if we have not called tgetent.
6341 So call tgetent. */
6342 { char b[2044]; tgetent (b, "xterm");}
6343 #endif
6344 adjust_frame_glyphs_initially ();
6345 return;
6347 #endif /* HAVE_X_WINDOWS */
6349 #ifdef HAVE_NTGUI
6350 if (!inhibit_window_system)
6352 Vinitial_window_system = Qw32;
6353 Vwindow_system_version = make_number (1);
6354 adjust_frame_glyphs_initially ();
6355 return;
6357 #endif /* HAVE_NTGUI */
6359 #ifdef HAVE_NS
6360 if (!inhibit_window_system
6361 #ifndef CANNOT_DUMP
6362 && initialized
6363 #endif
6366 Vinitial_window_system = Qns;
6367 Vwindow_system_version = make_number(10);
6368 adjust_frame_glyphs_initially ();
6369 return;
6371 #endif
6373 /* If no window system has been specified, try to use the terminal. */
6374 if (! isatty (0))
6376 fatal ("standard input is not a tty");
6377 exit (1);
6380 #ifdef WINDOWSNT
6381 terminal_type = "w32console";
6382 #else
6383 /* Look at the TERM variable. */
6384 terminal_type = (char *) getenv ("TERM");
6385 #endif
6386 if (!terminal_type)
6388 #ifdef HAVE_WINDOW_SYSTEM
6389 if (! inhibit_window_system)
6390 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6391 else
6392 #endif /* HAVE_WINDOW_SYSTEM */
6393 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6394 exit (1);
6398 struct terminal *t;
6399 struct frame *f = XFRAME (selected_frame);
6401 /* Open a display on the controlling tty. */
6402 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6404 /* Convert the initial frame to use the new display. */
6405 if (f->output_method != output_initial)
6406 abort ();
6407 f->output_method = t->type;
6408 f->terminal = t;
6410 t->reference_count++;
6411 #ifdef MSDOS
6412 f->output_data.tty->display_info = &the_only_display_info;
6413 #else
6414 if (f->output_method == output_termcap)
6415 create_tty_output (f);
6416 #endif
6417 t->display_info.tty->top_frame = selected_frame;
6418 change_frame_size (XFRAME (selected_frame),
6419 FrameRows (t->display_info.tty),
6420 FrameCols (t->display_info.tty), 0, 0, 1);
6422 /* Delete the initial terminal. */
6423 if (--initial_terminal->reference_count == 0
6424 && initial_terminal->delete_terminal_hook)
6425 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6427 /* Update frame parameters to reflect the new type. */
6428 Fmodify_frame_parameters
6429 (selected_frame, Fcons (Fcons (Qtty_type,
6430 Ftty_type (selected_frame)), Qnil));
6431 if (t->display_info.tty->name)
6432 Fmodify_frame_parameters (selected_frame,
6433 Fcons (Fcons (Qtty, build_string (t->display_info.tty->name)),
6434 Qnil));
6435 else
6436 Fmodify_frame_parameters (selected_frame, Fcons (Fcons (Qtty, Qnil),
6437 Qnil));
6441 struct frame *sf = SELECTED_FRAME ();
6442 int width = FRAME_TOTAL_COLS (sf);
6443 int height = FRAME_LINES (sf);
6445 unsigned int total_glyphs = height * (width + 2) * sizeof (struct glyph);
6447 /* If these sizes are so big they cause overflow, just ignore the
6448 change. It's not clear what better we could do. */
6449 if (total_glyphs / sizeof (struct glyph) / height != width + 2)
6450 fatal ("screen size %dx%d too big", width, height);
6453 adjust_frame_glyphs_initially ();
6454 calculate_costs (XFRAME (selected_frame));
6456 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6457 if (initialized
6458 && !noninteractive
6459 && NILP (Vinitial_window_system))
6461 /* For the initial frame, we don't have any way of knowing what
6462 are the foreground and background colors of the terminal. */
6463 struct frame *sf = SELECTED_FRAME();
6465 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6466 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6467 call0 (intern ("tty-set-up-initial-frame-faces"));
6473 /***********************************************************************
6474 Blinking cursor
6475 ***********************************************************************/
6477 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6478 Sinternal_show_cursor, 2, 2, 0,
6479 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6480 WINDOW nil means use the selected window. SHOW non-nil means
6481 show a cursor in WINDOW in the next redisplay. SHOW nil means
6482 don't show a cursor. */)
6483 (Lisp_Object window, Lisp_Object show)
6485 /* Don't change cursor state while redisplaying. This could confuse
6486 output routines. */
6487 if (!redisplaying_p)
6489 if (NILP (window))
6490 window = selected_window;
6491 else
6492 CHECK_WINDOW (window);
6494 XWINDOW (window)->cursor_off_p = NILP (show);
6497 return Qnil;
6501 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6502 Sinternal_show_cursor_p, 0, 1, 0,
6503 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6504 WINDOW nil or omitted means report on the selected window. */)
6505 (Lisp_Object window)
6507 struct window *w;
6509 if (NILP (window))
6510 window = selected_window;
6511 else
6512 CHECK_WINDOW (window);
6514 w = XWINDOW (window);
6515 return w->cursor_off_p ? Qnil : Qt;
6518 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame,
6519 Slast_nonminibuf_frame, 0, 0, 0,
6520 doc: /* Value is last nonminibuffer frame. */)
6521 (void)
6523 Lisp_Object frame = Qnil;
6525 if (last_nonminibuf_frame)
6526 XSETFRAME (frame, last_nonminibuf_frame);
6528 return frame;
6531 /***********************************************************************
6532 Initialization
6533 ***********************************************************************/
6535 void
6536 syms_of_display (void)
6538 defsubr (&Sredraw_frame);
6539 defsubr (&Sredraw_display);
6540 defsubr (&Sframe_or_buffer_changed_p);
6541 defsubr (&Sopen_termscript);
6542 defsubr (&Sding);
6543 defsubr (&Sredisplay);
6544 defsubr (&Ssleep_for);
6545 defsubr (&Ssend_string_to_terminal);
6546 defsubr (&Sinternal_show_cursor);
6547 defsubr (&Sinternal_show_cursor_p);
6548 defsubr (&Slast_nonminibuf_frame);
6550 #if GLYPH_DEBUG
6551 defsubr (&Sdump_redisplay_history);
6552 #endif
6554 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6555 staticpro (&frame_and_buffer_state);
6557 Qdisplay_table = intern_c_string ("display-table");
6558 staticpro (&Qdisplay_table);
6559 Qredisplay_dont_pause = intern_c_string ("redisplay-dont-pause");
6560 staticpro (&Qredisplay_dont_pause);
6562 DEFVAR_INT ("baud-rate", &baud_rate,
6563 doc: /* *The output baud rate of the terminal.
6564 On most systems, changing this value will affect the amount of padding
6565 and the other strategic decisions made during redisplay. */);
6567 DEFVAR_BOOL ("inverse-video", &inverse_video,
6568 doc: /* *Non-nil means invert the entire frame display.
6569 This means everything is in inverse video which otherwise would not be. */);
6571 DEFVAR_BOOL ("visible-bell", &visible_bell,
6572 doc: /* *Non-nil means try to flash the frame to represent a bell.
6574 See also `ring-bell-function'. */);
6576 DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
6577 doc: /* *Non-nil means no need to redraw entire frame after suspending.
6578 A non-nil value is useful if the terminal can automatically preserve
6579 Emacs's frame display when you reenter Emacs.
6580 It is up to you to set this variable if your terminal can do that. */);
6582 DEFVAR_LISP ("initial-window-system", &Vinitial_window_system,
6583 doc: /* Name of the window system that Emacs uses for the first frame.
6584 The value is a symbol:
6585 nil for a termcap frame (a character-only terminal),
6586 'x' for an Emacs frame that is really an X window,
6587 'w32' for an Emacs frame that is a window on MS-Windows display,
6588 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6589 'pc' for a direct-write MS-DOS frame.
6591 Use of this variable as a boolean is deprecated. Instead,
6592 use `display-graphic-p' or any of the other `display-*-p'
6593 predicates which report frame's specific UI-related capabilities. */);
6595 DEFVAR_KBOARD ("window-system", Vwindow_system,
6596 doc: /* Name of window system through which the selected frame is displayed.
6597 The value is a symbol:
6598 nil for a termcap frame (a character-only terminal),
6599 'x' for an Emacs frame that is really an X window,
6600 'w32' for an Emacs frame that is a window on MS-Windows display,
6601 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6602 'pc' for a direct-write MS-DOS frame.
6604 Use of this variable as a boolean is deprecated. Instead,
6605 use `display-graphic-p' or any of the other `display-*-p'
6606 predicates which report frame's specific UI-related capabilities. */);
6608 DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
6609 doc: /* The version number of the window system in use.
6610 For X windows, this is 11. */);
6612 DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
6613 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6615 DEFVAR_LISP ("glyph-table", &Vglyph_table,
6616 doc: /* Table defining how to output a glyph code to the frame.
6617 If not nil, this is a vector indexed by glyph code to define the glyph.
6618 Each element can be:
6619 integer: a glyph code which this glyph is an alias for.
6620 string: output this glyph using that string (not impl. in X windows).
6621 nil: this glyph mod 524288 is the code of a character to output,
6622 and this glyph / 524288 is the face number (see `face-id') to use
6623 while outputting it. */);
6624 Vglyph_table = Qnil;
6626 DEFVAR_LISP ("standard-display-table", &Vstandard_display_table,
6627 doc: /* Display table to use for buffers that specify none.
6628 See `buffer-display-table' for more information. */);
6629 Vstandard_display_table = Qnil;
6631 DEFVAR_BOOL ("redisplay-dont-pause", &redisplay_dont_pause,
6632 doc: /* *Non-nil means update isn't paused when input is detected. */);
6633 redisplay_dont_pause = 0;
6635 #if PERIODIC_PREEMPTION_CHECKING
6636 DEFVAR_LISP ("redisplay-preemption-period", &Vredisplay_preemption_period,
6637 doc: /* *The period in seconds between checking for input during redisplay.
6638 If input is detected, redisplay is pre-empted, and the input is processed.
6639 If nil, never pre-empt redisplay. */);
6640 Vredisplay_preemption_period = make_float (0.10);
6641 #endif
6643 #ifdef CANNOT_DUMP
6644 if (noninteractive)
6645 #endif
6647 Vinitial_window_system = Qnil;
6648 Vwindow_system_version = Qnil;