1 /* Updating of data structures for redisplay.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #define DISPEXTERN_INLINE EXTERN_INLINE
33 /* cm.h must come after dispextern.h on Windows. */
34 #include "dispextern.h"
36 #include "character.h"
40 #include "termhooks.h"
45 #include "intervals.h"
46 #include "blockinput.h"
49 #include "syssignal.h"
53 #endif /* HAVE_X_WINDOWS */
57 #endif /* HAVE_NTGUI */
63 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
68 /* Get number of chars of output now in the buffer of a stdio stream.
69 This ought to be built in stdio, but it isn't. Some s- files
70 override this because their stdio internals differ. */
71 #ifdef __GNU_LIBRARY__
73 /* The s- file might have overridden the definition with one that
74 works for the system's C library. But we are using the GNU C
75 library, so this is the right definition for every system. */
76 #ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
77 #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
79 #undef PENDING_OUTPUT_COUNT
80 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
83 /* not __GNU_LIBRARY__ and no PENDING_OUTPUT_COUNT defined */
84 #elif !defined (PENDING_OUTPUT_COUNT)
86 #if HAVE_STDIO_EXT_H && HAVE___FPENDING
87 #include <stdio_ext.h>
88 #define PENDING_OUTPUT_COUNT(FILE) __fpending (FILE)
90 #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
93 #endif /* not __GNU_LIBRARY__ and no PENDING_OUTPUT_COUNT defined */
95 #if defined (HAVE_TERM_H) && defined (GNU_LINUX)
96 #include <term.h> /* for tgetent */
99 /* Structure to pass dimensions around. Used for character bounding
100 boxes, glyph matrix dimensions and alike. */
109 /* Function prototypes. */
111 static void update_frame_line (struct frame
*, int);
112 static int required_matrix_height (struct window
*);
113 static int required_matrix_width (struct window
*);
114 static void adjust_frame_glyphs (struct frame
*);
115 static void change_frame_size_1 (struct frame
*, int, int, int, int, int);
116 static void increment_row_positions (struct glyph_row
*, ptrdiff_t, ptrdiff_t);
117 static void fill_up_frame_row_with_spaces (struct glyph_row
*, int);
118 static void build_frame_matrix_from_window_tree (struct glyph_matrix
*,
120 static void build_frame_matrix_from_leaf_window (struct glyph_matrix
*,
122 static void adjust_frame_message_buffer (struct frame
*);
123 static void adjust_decode_mode_spec_buffer (struct frame
*);
124 static void fill_up_glyph_row_with_spaces (struct glyph_row
*);
125 static void clear_window_matrices (struct window
*, int);
126 static void fill_up_glyph_row_area_with_spaces (struct glyph_row
*, int);
127 static int scrolling_window (struct window
*, int);
128 static int update_window_line (struct window
*, int, int *);
129 static void mirror_make_current (struct window
*, int);
131 static void check_matrix_pointers (struct glyph_matrix
*,
132 struct glyph_matrix
*);
134 static void mirror_line_dance (struct window
*, int, int, int *, char *);
135 static int update_window_tree (struct window
*, int);
136 static int update_window (struct window
*, int);
137 static int update_frame_1 (struct frame
*, int, int);
138 static int scrolling (struct frame
*);
139 static void set_window_cursor_after_update (struct window
*);
140 static void adjust_frame_glyphs_for_window_redisplay (struct frame
*);
141 static void adjust_frame_glyphs_for_frame_redisplay (struct frame
*);
144 /* Redisplay preemption timers. */
146 static EMACS_TIME preemption_period
;
147 static EMACS_TIME preemption_next_check
;
149 /* Nonzero upon entry to redisplay means do not assume anything about
150 current contents of actual terminal frame; clear and redraw it. */
154 /* Nonzero means last display completed. Zero means it was preempted. */
156 int display_completed
;
158 Lisp_Object Qdisplay_table
, Qredisplay_dont_pause
;
161 /* The currently selected frame. In a single-frame version, this
162 variable always equals the_only_frame. */
164 Lisp_Object selected_frame
;
166 /* A frame which is not just a mini-buffer, or 0 if there are no such
167 frames. This is usually the most recent such frame that was
168 selected. In a single-frame version, this variable always holds
169 the address of the_only_frame. */
171 struct frame
*last_nonminibuf_frame
;
173 /* 1 means SIGWINCH happened when not safe. */
175 static int delayed_size_change
;
177 /* 1 means glyph initialization has been completed at startup. */
179 static int glyphs_initialized_initially_p
;
181 /* Updated window if != 0. Set by update_window. */
183 struct window
*updated_window
;
185 /* Glyph row updated in update_window_line, and area that is updated. */
187 struct glyph_row
*updated_row
;
190 /* A glyph for a space. */
192 struct glyph space_glyph
;
194 /* Counts of allocated structures. These counts serve to diagnose
195 memory leaks and double frees. */
197 static int glyph_matrix_count
;
198 static int glyph_pool_count
;
200 /* If non-null, the frame whose frame matrices are manipulated. If
201 null, window matrices are worked on. */
203 static struct frame
*frame_matrix_frame
;
205 /* Non-zero means that fonts have been loaded since the last glyph
206 matrix adjustments. Redisplay must stop, and glyph matrices must
207 be adjusted when this flag becomes non-zero during display. The
208 reason fonts can be loaded so late is that fonts of fontsets are
209 loaded on demand. Another reason is that a line contains many
210 characters displayed by zero width or very narrow glyphs of
211 variable-width fonts. */
215 /* Convert vpos and hpos from frame to window and vice versa.
216 This may only be used for terminal frames. */
220 static int window_to_frame_vpos (struct window
*, int);
221 static int window_to_frame_hpos (struct window
*, int);
222 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
223 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
225 /* One element of the ring buffer containing redisplay history
228 struct redisplay_history
230 char trace
[512 + 100];
233 /* The size of the history buffer. */
235 #define REDISPLAY_HISTORY_SIZE 30
237 /* The redisplay history buffer. */
239 static struct redisplay_history redisplay_history
[REDISPLAY_HISTORY_SIZE
];
241 /* Next free entry in redisplay_history. */
243 static int history_idx
;
245 /* A tick that's incremented each time something is added to the
248 static uprintmax_t history_tick
;
250 static void add_frame_display_history (struct frame
*, int);
252 /* Add to the redisplay history how window W has been displayed.
253 MSG is a trace containing the information how W's glyph matrix
254 has been constructed. PAUSED_P non-zero means that the update
255 has been interrupted for pending input. */
258 add_window_display_history (struct window
*w
, const char *msg
, int paused_p
)
262 if (history_idx
>= REDISPLAY_HISTORY_SIZE
)
264 buf
= redisplay_history
[history_idx
].trace
;
267 snprintf (buf
, sizeof redisplay_history
[0].trace
,
268 "%"pMu
": window %p (`%s')%s\n%s",
271 ((BUFFERP (WVAR (w
, buffer
))
272 && STRINGP (BVAR (XBUFFER (WVAR (w
, buffer
)), name
)))
273 ? SSDATA (BVAR (XBUFFER (WVAR (w
, buffer
)), name
))
275 paused_p
? " ***paused***" : "",
280 /* Add to the redisplay history that frame F has been displayed.
281 PAUSED_P non-zero means that the update has been interrupted for
285 add_frame_display_history (struct frame
*f
, int paused_p
)
289 if (history_idx
>= REDISPLAY_HISTORY_SIZE
)
291 buf
= redisplay_history
[history_idx
].trace
;
294 sprintf (buf
, "%"pMu
": update frame %p%s",
296 f
, paused_p
? " ***paused***" : "");
300 DEFUN ("dump-redisplay-history", Fdump_redisplay_history
,
301 Sdump_redisplay_history
, 0, 0, "",
302 doc
: /* Dump redisplay history to stderr. */)
307 for (i
= history_idx
- 1; i
!= history_idx
; --i
)
310 i
= REDISPLAY_HISTORY_SIZE
- 1;
311 fprintf (stderr
, "%s\n", redisplay_history
[i
].trace
);
318 #else /* not GLYPH_DEBUG */
320 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
321 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
323 #endif /* GLYPH_DEBUG */
326 #if (defined PROFILING \
327 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
328 && !HAVE___EXECUTABLE_START)
329 /* This function comes first in the Emacs executable and is used only
330 to estimate the text start for profiling. */
332 __executable_start (void)
338 /***********************************************************************
340 ***********************************************************************/
342 /* Allocate and return a glyph_matrix structure. POOL is the glyph
343 pool from which memory for the matrix should be allocated, or null
344 for window-based redisplay where no glyph pools are used. The
345 member `pool' of the glyph matrix structure returned is set to
346 POOL, the structure is otherwise zeroed. */
348 static struct glyph_matrix
*
349 new_glyph_matrix (struct glyph_pool
*pool
)
351 struct glyph_matrix
*result
= xzalloc (sizeof *result
);
353 /* Increment number of allocated matrices. This count is used
354 to detect memory leaks. */
355 ++glyph_matrix_count
;
357 /* Set pool and return. */
363 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
365 The global counter glyph_matrix_count is decremented when a matrix
366 is freed. If the count gets negative, more structures were freed
367 than allocated, i.e. one matrix was freed more than once or a bogus
368 pointer was passed to this function.
370 If MATRIX->pool is null, this means that the matrix manages its own
371 glyph memory---this is done for matrices on X frames. Freeing the
372 matrix also frees the glyph memory in this case. */
375 free_glyph_matrix (struct glyph_matrix
*matrix
)
381 /* Detect the case that more matrices are freed than were
383 if (--glyph_matrix_count
< 0)
386 /* Free glyph memory if MATRIX owns it. */
387 if (matrix
->pool
== NULL
)
388 for (i
= 0; i
< matrix
->rows_allocated
; ++i
)
389 xfree (matrix
->rows
[i
].glyphs
[LEFT_MARGIN_AREA
]);
391 /* Free row structures and the matrix itself. */
392 xfree (matrix
->rows
);
398 /* Return the number of glyphs to reserve for a marginal area of
399 window W. TOTAL_GLYPHS is the number of glyphs in a complete
400 display line of window W. MARGIN gives the width of the marginal
401 area in canonical character units. MARGIN should be an integer
405 margin_glyphs_to_reserve (struct window
*w
, int total_glyphs
, Lisp_Object margin
)
409 if (NUMBERP (margin
))
411 int width
= XFASTINT (WVAR (w
, total_cols
));
412 double d
= max (0, XFLOATINT (margin
));
413 d
= min (width
/ 2 - 1, d
);
414 n
= (int) ((double) total_glyphs
/ width
* d
);
422 /* Return non-zero if ROW's hash value is correct, zero if not.
423 Optimized away if ENABLE_CHECKING is not defined. */
426 verify_row_hash (struct glyph_row
*row
)
428 return row
->hash
== row_hash (row
);
431 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
434 W is null if the function is called for a frame glyph matrix.
435 Otherwise it is the window MATRIX is a member of. X and Y are the
436 indices of the first column and row of MATRIX within the frame
437 matrix, if such a matrix exists. They are zero for purely
438 window-based redisplay. DIM is the needed size of the matrix.
440 In window-based redisplay, where no frame matrices exist, glyph
441 matrices manage their own glyph storage. Otherwise, they allocate
442 storage from a common frame glyph pool which can be found in
445 The reason for this memory management strategy is to avoid complete
446 frame redraws if possible. When we allocate from a common pool, a
447 change of the location or size of a sub-matrix within the pool
448 requires a complete redisplay of the frame because we cannot easily
449 make sure that the current matrices of all windows still agree with
450 what is displayed on the screen. While this is usually fast, it
451 leads to screen flickering. */
454 adjust_glyph_matrix (struct window
*w
, struct glyph_matrix
*matrix
, int x
, int y
, struct dim dim
)
458 int marginal_areas_changed_p
= 0;
459 int header_line_changed_p
= 0;
460 int header_line_p
= 0;
461 int left
= -1, right
= -1;
462 int window_width
= -1, window_height
= -1;
464 /* See if W had a header line that has disappeared now, or vice versa.
468 window_box (w
, -1, 0, 0, &window_width
, &window_height
);
470 header_line_p
= WINDOW_WANTS_HEADER_LINE_P (w
);
471 header_line_changed_p
= header_line_p
!= matrix
->header_line_p
;
473 matrix
->header_line_p
= header_line_p
;
475 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
476 Do nothing if MATRIX' size, position, vscroll, and marginal areas
477 haven't changed. This optimization is important because preserving
478 the matrix means preventing redisplay. */
479 if (matrix
->pool
== NULL
)
481 left
= margin_glyphs_to_reserve (w
, dim
.width
, WVAR (w
, left_margin_cols
));
482 right
= margin_glyphs_to_reserve (w
, dim
.width
, WVAR (w
, right_margin_cols
));
483 eassert (left
>= 0 && right
>= 0);
484 marginal_areas_changed_p
= (left
!= matrix
->left_margin_glyphs
485 || right
!= matrix
->right_margin_glyphs
);
487 if (!marginal_areas_changed_p
489 && !header_line_changed_p
490 && matrix
->window_left_col
== WINDOW_LEFT_EDGE_COL (w
)
491 && matrix
->window_top_line
== WINDOW_TOP_EDGE_LINE (w
)
492 && matrix
->window_height
== window_height
493 && matrix
->window_vscroll
== w
->vscroll
494 && matrix
->window_width
== window_width
)
498 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
499 if (matrix
->rows_allocated
< dim
.height
)
501 int old_alloc
= matrix
->rows_allocated
;
502 new_rows
= dim
.height
- matrix
->rows_allocated
;
503 matrix
->rows
= xpalloc (matrix
->rows
, &matrix
->rows_allocated
,
504 new_rows
, INT_MAX
, sizeof *matrix
->rows
);
505 memset (matrix
->rows
+ old_alloc
, 0,
506 (matrix
->rows_allocated
- old_alloc
) * sizeof *matrix
->rows
);
511 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
512 on a frame not using window-based redisplay. Set up pointers for
513 each row into the glyph pool. */
516 eassert (matrix
->pool
->glyphs
);
520 left
= margin_glyphs_to_reserve (w
, dim
.width
,
521 WVAR (w
, left_margin_cols
));
522 right
= margin_glyphs_to_reserve (w
, dim
.width
,
523 WVAR (w
, right_margin_cols
));
528 for (i
= 0; i
< dim
.height
; ++i
)
530 struct glyph_row
*row
= &matrix
->rows
[i
];
532 row
->glyphs
[LEFT_MARGIN_AREA
]
533 = (matrix
->pool
->glyphs
534 + (y
+ i
) * matrix
->pool
->ncolumns
538 || row
== matrix
->rows
+ dim
.height
- 1
539 || (row
== matrix
->rows
&& matrix
->header_line_p
))
541 row
->glyphs
[TEXT_AREA
]
542 = row
->glyphs
[LEFT_MARGIN_AREA
];
543 row
->glyphs
[RIGHT_MARGIN_AREA
]
544 = row
->glyphs
[TEXT_AREA
] + dim
.width
;
545 row
->glyphs
[LAST_AREA
]
546 = row
->glyphs
[RIGHT_MARGIN_AREA
];
550 row
->glyphs
[TEXT_AREA
]
551 = row
->glyphs
[LEFT_MARGIN_AREA
] + left
;
552 row
->glyphs
[RIGHT_MARGIN_AREA
]
553 = row
->glyphs
[TEXT_AREA
] + dim
.width
- left
- right
;
554 row
->glyphs
[LAST_AREA
]
555 = row
->glyphs
[LEFT_MARGIN_AREA
] + dim
.width
;
559 matrix
->left_margin_glyphs
= left
;
560 matrix
->right_margin_glyphs
= right
;
564 /* If MATRIX->pool is null, MATRIX is responsible for managing
565 its own memory. It is a window matrix for window-based redisplay.
566 Allocate glyph memory from the heap. */
567 if (dim
.width
> matrix
->matrix_w
569 || header_line_changed_p
570 || marginal_areas_changed_p
)
572 struct glyph_row
*row
= matrix
->rows
;
573 struct glyph_row
*end
= row
+ matrix
->rows_allocated
;
577 row
->glyphs
[LEFT_MARGIN_AREA
]
578 = xnrealloc (row
->glyphs
[LEFT_MARGIN_AREA
],
579 dim
.width
, sizeof (struct glyph
));
581 /* The mode line never has marginal areas. */
582 if (row
== matrix
->rows
+ dim
.height
- 1
583 || (row
== matrix
->rows
&& matrix
->header_line_p
))
585 row
->glyphs
[TEXT_AREA
]
586 = row
->glyphs
[LEFT_MARGIN_AREA
];
587 row
->glyphs
[RIGHT_MARGIN_AREA
]
588 = row
->glyphs
[TEXT_AREA
] + dim
.width
;
589 row
->glyphs
[LAST_AREA
]
590 = row
->glyphs
[RIGHT_MARGIN_AREA
];
594 row
->glyphs
[TEXT_AREA
]
595 = row
->glyphs
[LEFT_MARGIN_AREA
] + left
;
596 row
->glyphs
[RIGHT_MARGIN_AREA
]
597 = row
->glyphs
[TEXT_AREA
] + dim
.width
- left
- right
;
598 row
->glyphs
[LAST_AREA
]
599 = row
->glyphs
[LEFT_MARGIN_AREA
] + dim
.width
;
605 eassert (left
>= 0 && right
>= 0);
606 matrix
->left_margin_glyphs
= left
;
607 matrix
->right_margin_glyphs
= right
;
610 /* Number of rows to be used by MATRIX. */
611 matrix
->nrows
= dim
.height
;
612 eassert (matrix
->nrows
>= 0);
616 if (matrix
== w
->current_matrix
)
618 /* Mark rows in a current matrix of a window as not having
619 valid contents. It's important to not do this for
620 desired matrices. When Emacs starts, it may already be
621 building desired matrices when this function runs. */
622 if (window_width
< 0)
623 window_width
= window_box_width (w
, -1);
625 /* Optimize the case that only the height has changed (C-x 2,
626 upper window). Invalidate all rows that are no longer part
628 if (!marginal_areas_changed_p
629 && !header_line_changed_p
631 && dim
.width
== matrix
->matrix_w
632 && matrix
->window_left_col
== WINDOW_LEFT_EDGE_COL (w
)
633 && matrix
->window_top_line
== WINDOW_TOP_EDGE_LINE (w
)
634 && matrix
->window_width
== window_width
)
636 /* Find the last row in the window. */
637 for (i
= 0; i
< matrix
->nrows
&& matrix
->rows
[i
].enabled_p
; ++i
)
638 if (MATRIX_ROW_BOTTOM_Y (matrix
->rows
+ i
) >= window_height
)
644 /* Window end is invalid, if inside of the rows that
645 are invalidated below. */
646 if (INTEGERP (WVAR (w
, window_end_vpos
))
647 && XFASTINT (WVAR (w
, window_end_vpos
)) >= i
)
648 WVAR (w
, window_end_valid
) = Qnil
;
650 while (i
< matrix
->nrows
)
651 matrix
->rows
[i
++].enabled_p
= 0;
655 for (i
= 0; i
< matrix
->nrows
; ++i
)
656 matrix
->rows
[i
].enabled_p
= 0;
659 else if (matrix
== w
->desired_matrix
)
661 /* Rows in desired matrices always have to be cleared;
662 redisplay expects this is the case when it runs, so it
663 had better be the case when we adjust matrices between
665 for (i
= 0; i
< matrix
->nrows
; ++i
)
666 matrix
->rows
[i
].enabled_p
= 0;
671 /* Remember last values to be able to optimize frame redraws. */
672 matrix
->matrix_x
= x
;
673 matrix
->matrix_y
= y
;
674 matrix
->matrix_w
= dim
.width
;
675 matrix
->matrix_h
= dim
.height
;
677 /* Record the top y location and height of W at the time the matrix
678 was last adjusted. This is used to optimize redisplay above. */
681 matrix
->window_left_col
= WINDOW_LEFT_EDGE_COL (w
);
682 matrix
->window_top_line
= WINDOW_TOP_EDGE_LINE (w
);
683 matrix
->window_height
= window_height
;
684 matrix
->window_width
= window_width
;
685 matrix
->window_vscroll
= w
->vscroll
;
690 /* Reverse the contents of rows in MATRIX between START and END. The
691 contents of the row at END - 1 end up at START, END - 2 at START +
692 1 etc. This is part of the implementation of rotate_matrix (see
696 reverse_rows (struct glyph_matrix
*matrix
, int start
, int end
)
700 for (i
= start
, j
= end
- 1; i
< j
; ++i
, --j
)
702 /* Non-ISO HP/UX compiler doesn't like auto struct
704 struct glyph_row temp
;
705 temp
= matrix
->rows
[i
];
706 matrix
->rows
[i
] = matrix
->rows
[j
];
707 matrix
->rows
[j
] = temp
;
712 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
713 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
714 indices. (Note: this does not copy glyphs, only glyph pointers in
715 row structures are moved around).
717 The algorithm used for rotating the vector was, I believe, first
718 described by Kernighan. See the vector R as consisting of two
719 sub-vectors AB, where A has length BY for BY >= 0. The result
720 after rotating is then BA. Reverse both sub-vectors to get ArBr
721 and reverse the result to get (ArBr)r which is BA. Similar for
725 rotate_matrix (struct glyph_matrix
*matrix
, int first
, int last
, int by
)
729 /* Up (rotate left, i.e. towards lower indices). */
731 reverse_rows (matrix
, first
, first
+ by
);
732 reverse_rows (matrix
, first
+ by
, last
);
733 reverse_rows (matrix
, first
, last
);
737 /* Down (rotate right, i.e. towards higher indices). */
738 reverse_rows (matrix
, last
- by
, last
);
739 reverse_rows (matrix
, first
, last
- by
);
740 reverse_rows (matrix
, first
, last
);
745 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
746 with indices START <= index < END. Increment positions by DELTA/
750 increment_matrix_positions (struct glyph_matrix
*matrix
, int start
, int end
,
751 ptrdiff_t delta
, ptrdiff_t delta_bytes
)
753 /* Check that START and END are reasonable values. */
754 eassert (start
>= 0 && start
<= matrix
->nrows
);
755 eassert (end
>= 0 && end
<= matrix
->nrows
);
756 eassert (start
<= end
);
758 for (; start
< end
; ++start
)
759 increment_row_positions (matrix
->rows
+ start
, delta
, delta_bytes
);
763 /* Enable a range of rows in glyph matrix MATRIX. START and END are
764 the row indices of the first and last + 1 row to enable. If
765 ENABLED_P is non-zero, enabled_p flags in rows will be set to 1. */
768 enable_glyph_matrix_rows (struct glyph_matrix
*matrix
, int start
, int end
, int enabled_p
)
770 eassert (start
<= end
);
771 eassert (start
>= 0 && start
< matrix
->nrows
);
772 eassert (end
>= 0 && end
<= matrix
->nrows
);
774 for (; start
< end
; ++start
)
775 matrix
->rows
[start
].enabled_p
= enabled_p
!= 0;
781 This empties all rows in MATRIX by setting the enabled_p flag for
782 all rows of the matrix to zero. The function prepare_desired_row
783 will eventually really clear a row when it sees one with a zero
786 Resets update hints to defaults value. The only update hint
787 currently present is the flag MATRIX->no_scrolling_p. */
790 clear_glyph_matrix (struct glyph_matrix
*matrix
)
794 enable_glyph_matrix_rows (matrix
, 0, matrix
->nrows
, 0);
795 matrix
->no_scrolling_p
= 0;
800 /* Shift part of the glyph matrix MATRIX of window W up or down.
801 Increment y-positions in glyph rows between START and END by DY,
802 and recompute their visible height. */
805 shift_glyph_matrix (struct window
*w
, struct glyph_matrix
*matrix
, int start
, int end
, int dy
)
809 eassert (start
<= end
);
810 eassert (start
>= 0 && start
< matrix
->nrows
);
811 eassert (end
>= 0 && end
<= matrix
->nrows
);
813 min_y
= WINDOW_HEADER_LINE_HEIGHT (w
);
814 max_y
= WINDOW_BOX_HEIGHT_NO_MODE_LINE (w
);
816 for (; start
< end
; ++start
)
818 struct glyph_row
*row
= &matrix
->rows
[start
];
821 row
->visible_height
= row
->height
;
824 row
->visible_height
-= min_y
- row
->y
;
825 if (row
->y
+ row
->height
> max_y
)
826 row
->visible_height
-= row
->y
+ row
->height
- max_y
;
827 if (row
->fringe_bitmap_periodic_p
)
828 row
->redraw_fringe_bitmaps_p
= 1;
833 /* Mark all rows in current matrices of frame F as invalid. Marking
834 invalid is done by setting enabled_p to zero for all rows in a
838 clear_current_matrices (register struct frame
*f
)
840 /* Clear frame current matrix, if we have one. */
841 if (f
->current_matrix
)
842 clear_glyph_matrix (f
->current_matrix
);
844 /* Clear the matrix of the menu bar window, if such a window exists.
845 The menu bar window is currently used to display menus on X when
846 no toolkit support is compiled in. */
847 if (WINDOWP (FVAR (f
, menu_bar_window
)))
848 clear_glyph_matrix (XWINDOW (FVAR (f
, menu_bar_window
))->current_matrix
);
850 /* Clear the matrix of the tool-bar window, if any. */
851 if (WINDOWP (FVAR (f
, tool_bar_window
)))
852 clear_glyph_matrix (XWINDOW (FVAR (f
, tool_bar_window
))->current_matrix
);
854 /* Clear current window matrices. */
855 eassert (WINDOWP (FRAME_ROOT_WINDOW (f
)));
856 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f
)), 0);
860 /* Clear out all display lines of F for a coming redisplay. */
863 clear_desired_matrices (register struct frame
*f
)
865 if (f
->desired_matrix
)
866 clear_glyph_matrix (f
->desired_matrix
);
868 if (WINDOWP (FVAR (f
, menu_bar_window
)))
869 clear_glyph_matrix (XWINDOW (FVAR (f
, menu_bar_window
))->desired_matrix
);
871 if (WINDOWP (FVAR (f
, tool_bar_window
)))
872 clear_glyph_matrix (XWINDOW (FVAR (f
, tool_bar_window
))->desired_matrix
);
874 /* Do it for window matrices. */
875 eassert (WINDOWP (FRAME_ROOT_WINDOW (f
)));
876 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f
)), 1);
880 /* Clear matrices in window tree rooted in W. If DESIRED_P is
881 non-zero clear desired matrices, otherwise clear current matrices. */
884 clear_window_matrices (struct window
*w
, int desired_p
)
888 if (!NILP (WVAR (w
, hchild
)))
890 eassert (WINDOWP (WVAR (w
, hchild
)));
891 clear_window_matrices (XWINDOW (WVAR (w
, hchild
)), desired_p
);
893 else if (!NILP (WVAR (w
, vchild
)))
895 eassert (WINDOWP (WVAR (w
, vchild
)));
896 clear_window_matrices (XWINDOW (WVAR (w
, vchild
)), desired_p
);
901 clear_glyph_matrix (w
->desired_matrix
);
904 clear_glyph_matrix (w
->current_matrix
);
905 WVAR (w
, window_end_valid
) = Qnil
;
909 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
915 /***********************************************************************
918 See dispextern.h for an overall explanation of glyph rows.
919 ***********************************************************************/
921 /* Clear glyph row ROW. Do it in a way that makes it robust against
922 changes in the glyph_row structure, i.e. addition or removal of
923 structure members. */
925 static struct glyph_row null_row
;
928 clear_glyph_row (struct glyph_row
*row
)
930 struct glyph
*p
[1 + LAST_AREA
];
933 p
[LEFT_MARGIN_AREA
] = row
->glyphs
[LEFT_MARGIN_AREA
];
934 p
[TEXT_AREA
] = row
->glyphs
[TEXT_AREA
];
935 p
[RIGHT_MARGIN_AREA
] = row
->glyphs
[RIGHT_MARGIN_AREA
];
936 p
[LAST_AREA
] = row
->glyphs
[LAST_AREA
];
941 /* Restore pointers. */
942 row
->glyphs
[LEFT_MARGIN_AREA
] = p
[LEFT_MARGIN_AREA
];
943 row
->glyphs
[TEXT_AREA
] = p
[TEXT_AREA
];
944 row
->glyphs
[RIGHT_MARGIN_AREA
] = p
[RIGHT_MARGIN_AREA
];
945 row
->glyphs
[LAST_AREA
] = p
[LAST_AREA
];
947 #if 0 /* At some point, some bit-fields of struct glyph were not set,
948 which made glyphs unequal when compared with GLYPH_EQUAL_P.
949 Redisplay outputs such glyphs, and flickering effects were
950 the result. This also depended on the contents of memory
951 returned by xmalloc. If flickering happens again, activate
952 the code below. If the flickering is gone with that, chances
953 are that the flickering has the same reason as here. */
954 memset (p
[0], 0, (char *) p
[LAST_AREA
] - (char *) p
[0]);
959 /* Make ROW an empty, enabled row of canonical character height,
960 in window W starting at y-position Y. */
963 blank_row (struct window
*w
, struct glyph_row
*row
, int y
)
967 min_y
= WINDOW_HEADER_LINE_HEIGHT (w
);
968 max_y
= WINDOW_BOX_HEIGHT_NO_MODE_LINE (w
);
970 clear_glyph_row (row
);
972 row
->ascent
= row
->phys_ascent
= 0;
973 row
->height
= row
->phys_height
= FRAME_LINE_HEIGHT (XFRAME (WVAR (w
, frame
)));
974 row
->visible_height
= row
->height
;
977 row
->visible_height
-= min_y
- row
->y
;
978 if (row
->y
+ row
->height
> max_y
)
979 row
->visible_height
-= row
->y
+ row
->height
- max_y
;
985 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
986 are the amounts by which to change positions. Note that the first
987 glyph of the text area of a row can have a buffer position even if
988 the used count of the text area is zero. Such rows display line
992 increment_row_positions (struct glyph_row
*row
,
993 ptrdiff_t delta
, ptrdiff_t delta_bytes
)
997 /* Increment start and end positions. */
998 MATRIX_ROW_START_CHARPOS (row
) += delta
;
999 MATRIX_ROW_START_BYTEPOS (row
) += delta_bytes
;
1000 MATRIX_ROW_END_CHARPOS (row
) += delta
;
1001 MATRIX_ROW_END_BYTEPOS (row
) += delta_bytes
;
1002 CHARPOS (row
->start
.pos
) += delta
;
1003 BYTEPOS (row
->start
.pos
) += delta_bytes
;
1004 CHARPOS (row
->end
.pos
) += delta
;
1005 BYTEPOS (row
->end
.pos
) += delta_bytes
;
1007 if (!row
->enabled_p
)
1010 /* Increment positions in glyphs. */
1011 for (area
= 0; area
< LAST_AREA
; ++area
)
1012 for (i
= 0; i
< row
->used
[area
]; ++i
)
1013 if (BUFFERP (row
->glyphs
[area
][i
].object
)
1014 && row
->glyphs
[area
][i
].charpos
> 0)
1015 row
->glyphs
[area
][i
].charpos
+= delta
;
1017 /* Capture the case of rows displaying a line end. */
1018 if (row
->used
[TEXT_AREA
] == 0
1019 && MATRIX_ROW_DISPLAYS_TEXT_P (row
))
1020 row
->glyphs
[TEXT_AREA
]->charpos
+= delta
;
1025 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
1026 contents, i.e. glyph structure contents are exchanged between A and
1027 B without changing glyph pointers in A and B. */
1030 swap_glyphs_in_rows (struct glyph_row
*a
, struct glyph_row
*b
)
1034 for (area
= 0; area
< LAST_AREA
; ++area
)
1036 /* Number of glyphs to swap. */
1037 int max_used
= max (a
->used
[area
], b
->used
[area
]);
1039 /* Start of glyphs in area of row A. */
1040 struct glyph
*glyph_a
= a
->glyphs
[area
];
1042 /* End + 1 of glyphs in area of row A. */
1043 struct glyph
*glyph_a_end
= a
->glyphs
[max_used
];
1045 /* Start of glyphs in area of row B. */
1046 struct glyph
*glyph_b
= b
->glyphs
[area
];
1048 while (glyph_a
< glyph_a_end
)
1050 /* Non-ISO HP/UX compiler doesn't like auto struct
1054 *glyph_a
= *glyph_b
;
1064 /* Exchange pointers to glyph memory between glyph rows A and B. Also
1065 exchange the used[] array and the hash values of the rows, because
1066 these should all go together for the row's hash value to be
1070 swap_glyph_pointers (struct glyph_row
*a
, struct glyph_row
*b
)
1073 unsigned hash_tem
= a
->hash
;
1075 for (i
= 0; i
< LAST_AREA
+ 1; ++i
)
1077 struct glyph
*temp
= a
->glyphs
[i
];
1079 a
->glyphs
[i
] = b
->glyphs
[i
];
1080 b
->glyphs
[i
] = temp
;
1083 short used_tem
= a
->used
[i
];
1085 a
->used
[i
] = b
->used
[i
];
1086 b
->used
[i
] = used_tem
;
1094 /* Copy glyph row structure FROM to glyph row structure TO, except
1095 that glyph pointers, the `used' counts, and the hash values in the
1096 structures are left unchanged. */
1099 copy_row_except_pointers (struct glyph_row
*to
, struct glyph_row
*from
)
1101 struct glyph
*pointers
[1 + LAST_AREA
];
1102 short used
[LAST_AREA
];
1105 /* Save glyph pointers of TO. */
1106 memcpy (pointers
, to
->glyphs
, sizeof to
->glyphs
);
1107 memcpy (used
, to
->used
, sizeof to
->used
);
1110 /* Do a structure assignment. */
1113 /* Restore original pointers of TO. */
1114 memcpy (to
->glyphs
, pointers
, sizeof to
->glyphs
);
1115 memcpy (to
->used
, used
, sizeof to
->used
);
1120 /* Assign glyph row FROM to glyph row TO. This works like a structure
1121 assignment TO = FROM, except that glyph pointers are not copied but
1122 exchanged between TO and FROM. Pointers must be exchanged to avoid
1126 assign_row (struct glyph_row
*to
, struct glyph_row
*from
)
1128 swap_glyph_pointers (to
, from
);
1129 copy_row_except_pointers (to
, from
);
1133 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1134 a row in a window matrix, is a slice of the glyph memory of the
1135 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1136 is non-zero if the glyph memory of WINDOW_ROW is part of the glyph
1137 memory of FRAME_ROW. */
1142 glyph_row_slice_p (struct glyph_row
*window_row
, struct glyph_row
*frame_row
)
1144 struct glyph
*window_glyph_start
= window_row
->glyphs
[0];
1145 struct glyph
*frame_glyph_start
= frame_row
->glyphs
[0];
1146 struct glyph
*frame_glyph_end
= frame_row
->glyphs
[LAST_AREA
];
1148 return (frame_glyph_start
<= window_glyph_start
1149 && window_glyph_start
< frame_glyph_end
);
1152 #endif /* GLYPH_DEBUG */
1156 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1157 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1158 in WINDOW_MATRIX is found satisfying the condition. */
1160 static struct glyph_row
*
1161 find_glyph_row_slice (struct glyph_matrix
*window_matrix
,
1162 struct glyph_matrix
*frame_matrix
, int row
)
1166 eassert (row
>= 0 && row
< frame_matrix
->nrows
);
1168 for (i
= 0; i
< window_matrix
->nrows
; ++i
)
1169 if (glyph_row_slice_p (window_matrix
->rows
+ i
,
1170 frame_matrix
->rows
+ row
))
1173 return i
< window_matrix
->nrows
? window_matrix
->rows
+ i
: 0;
1178 /* Prepare ROW for display. Desired rows are cleared lazily,
1179 i.e. they are only marked as to be cleared by setting their
1180 enabled_p flag to zero. When a row is to be displayed, a prior
1181 call to this function really clears it. */
1184 prepare_desired_row (struct glyph_row
*row
)
1186 if (!row
->enabled_p
)
1188 int rp
= row
->reversed_p
;
1190 clear_glyph_row (row
);
1192 row
->reversed_p
= rp
;
1197 /* Return a hash code for glyph row ROW. */
1200 line_hash_code (struct glyph_row
*row
)
1206 struct glyph
*glyph
= row
->glyphs
[TEXT_AREA
];
1207 struct glyph
*end
= glyph
+ row
->used
[TEXT_AREA
];
1211 int c
= glyph
->u
.ch
;
1212 int face_id
= glyph
->face_id
;
1213 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1215 hash
= (((hash
<< 4) + (hash
>> 24)) & 0x0fffffff) + c
;
1216 hash
= (((hash
<< 4) + (hash
>> 24)) & 0x0fffffff) + face_id
;
1228 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1229 the number of characters in the line. If must_write_spaces is
1230 zero, leading and trailing spaces are ignored. */
1233 line_draw_cost (struct glyph_matrix
*matrix
, int vpos
)
1235 struct glyph_row
*row
= matrix
->rows
+ vpos
;
1236 struct glyph
*beg
= row
->glyphs
[TEXT_AREA
];
1237 struct glyph
*end
= beg
+ row
->used
[TEXT_AREA
];
1239 Lisp_Object
*glyph_table_base
= GLYPH_TABLE_BASE
;
1240 ptrdiff_t glyph_table_len
= GLYPH_TABLE_LENGTH
;
1242 /* Ignore trailing and leading spaces if we can. */
1243 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1245 /* Skip from the end over trailing spaces. */
1246 while (end
> beg
&& CHAR_GLYPH_SPACE_P (*(end
- 1)))
1249 /* All blank line. */
1253 /* Skip over leading spaces. */
1254 while (CHAR_GLYPH_SPACE_P (*beg
))
1258 /* If we don't have a glyph-table, each glyph is one character,
1259 so return the number of glyphs. */
1260 if (glyph_table_base
== 0)
1264 /* Otherwise, scan the glyphs and accumulate their total length
1271 SET_GLYPH_FROM_CHAR_GLYPH (g
, *beg
);
1273 if (GLYPH_INVALID_P (g
)
1274 || GLYPH_SIMPLE_P (glyph_table_base
, glyph_table_len
, g
))
1277 len
+= GLYPH_LENGTH (glyph_table_base
, g
);
1287 /* Test two glyph rows A and B for equality. Value is non-zero if A
1288 and B have equal contents. MOUSE_FACE_P non-zero means compare the
1289 mouse_face_p flags of A and B, too. */
1292 row_equal_p (struct glyph_row
*a
, struct glyph_row
*b
, int mouse_face_p
)
1294 eassert (verify_row_hash (a
));
1295 eassert (verify_row_hash (b
));
1299 else if (a
->hash
!= b
->hash
)
1303 struct glyph
*a_glyph
, *b_glyph
, *a_end
;
1306 if (mouse_face_p
&& a
->mouse_face_p
!= b
->mouse_face_p
)
1309 /* Compare glyphs. */
1310 for (area
= LEFT_MARGIN_AREA
; area
< LAST_AREA
; ++area
)
1312 if (a
->used
[area
] != b
->used
[area
])
1315 a_glyph
= a
->glyphs
[area
];
1316 a_end
= a_glyph
+ a
->used
[area
];
1317 b_glyph
= b
->glyphs
[area
];
1319 while (a_glyph
< a_end
1320 && GLYPH_EQUAL_P (a_glyph
, b_glyph
))
1321 ++a_glyph
, ++b_glyph
;
1323 if (a_glyph
!= a_end
)
1327 if (a
->fill_line_p
!= b
->fill_line_p
1328 || a
->cursor_in_fringe_p
!= b
->cursor_in_fringe_p
1329 || a
->left_fringe_bitmap
!= b
->left_fringe_bitmap
1330 || a
->left_fringe_face_id
!= b
->left_fringe_face_id
1331 || a
->left_fringe_offset
!= b
->left_fringe_offset
1332 || a
->right_fringe_bitmap
!= b
->right_fringe_bitmap
1333 || a
->right_fringe_face_id
!= b
->right_fringe_face_id
1334 || a
->right_fringe_offset
!= b
->right_fringe_offset
1335 || a
->fringe_bitmap_periodic_p
!= b
->fringe_bitmap_periodic_p
1336 || a
->overlay_arrow_bitmap
!= b
->overlay_arrow_bitmap
1337 || a
->exact_window_width_line_p
!= b
->exact_window_width_line_p
1338 || a
->overlapped_p
!= b
->overlapped_p
1339 || (MATRIX_ROW_CONTINUATION_LINE_P (a
)
1340 != MATRIX_ROW_CONTINUATION_LINE_P (b
))
1341 || a
->reversed_p
!= b
->reversed_p
1342 /* Different partially visible characters on left margin. */
1344 /* Different height. */
1345 || a
->ascent
!= b
->ascent
1346 || a
->phys_ascent
!= b
->phys_ascent
1347 || a
->phys_height
!= b
->phys_height
1348 || a
->visible_height
!= b
->visible_height
)
1357 /***********************************************************************
1360 See dispextern.h for an overall explanation of glyph pools.
1361 ***********************************************************************/
1363 /* Allocate a glyph_pool structure. The structure returned is
1364 initialized with zeros. The global variable glyph_pool_count is
1365 incremented for each pool allocated. */
1367 static struct glyph_pool
*
1368 new_glyph_pool (void)
1370 struct glyph_pool
*result
= xzalloc (sizeof *result
);
1372 /* For memory leak and double deletion checking. */
1379 /* Free a glyph_pool structure POOL. The function may be called with
1380 a null POOL pointer. The global variable glyph_pool_count is
1381 decremented with every pool structure freed. If this count gets
1382 negative, more structures were freed than allocated, i.e. one
1383 structure must have been freed more than once or a bogus pointer
1384 was passed to free_glyph_pool. */
1387 free_glyph_pool (struct glyph_pool
*pool
)
1391 /* More freed than allocated? */
1393 eassert (glyph_pool_count
>= 0);
1395 xfree (pool
->glyphs
);
1401 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1402 columns we need. This function never shrinks a pool. The only
1403 case in which this would make sense, would be when a frame's size
1404 is changed from a large value to a smaller one. But, if someone
1405 does it once, we can expect that he will do it again.
1407 Value is non-zero if the pool changed in a way which makes
1408 re-adjusting window glyph matrices necessary. */
1411 realloc_glyph_pool (struct glyph_pool
*pool
, struct dim matrix_dim
)
1416 changed_p
= (pool
->glyphs
== 0
1417 || matrix_dim
.height
!= pool
->nrows
1418 || matrix_dim
.width
!= pool
->ncolumns
);
1420 /* Enlarge the glyph pool. */
1421 needed
= matrix_dim
.width
;
1422 if (INT_MULTIPLY_OVERFLOW (needed
, matrix_dim
.height
))
1423 memory_full (SIZE_MAX
);
1424 needed
*= matrix_dim
.height
;
1425 if (needed
> pool
->nglyphs
)
1427 ptrdiff_t old_nglyphs
= pool
->nglyphs
;
1428 pool
->glyphs
= xpalloc (pool
->glyphs
, &pool
->nglyphs
,
1429 needed
- old_nglyphs
, -1, sizeof *pool
->glyphs
);
1430 memset (pool
->glyphs
+ old_nglyphs
, 0,
1431 (pool
->nglyphs
- old_nglyphs
) * sizeof *pool
->glyphs
);
1434 /* Remember the number of rows and columns because (a) we use them
1435 to do sanity checks, and (b) the number of columns determines
1436 where rows in the frame matrix start---this must be available to
1437 determine pointers to rows of window sub-matrices. */
1438 pool
->nrows
= matrix_dim
.height
;
1439 pool
->ncolumns
= matrix_dim
.width
;
1446 /***********************************************************************
1448 ***********************************************************************/
1453 /* Flush standard output. This is sometimes useful to call from the debugger.
1454 XXX Maybe this should be changed to flush the current terminal instead of
1458 void flush_stdout (void) EXTERNALLY_VISIBLE
;
1467 /* Check that no glyph pointers have been lost in MATRIX. If a
1468 pointer has been lost, e.g. by using a structure assignment between
1469 rows, at least one pointer must occur more than once in the rows of
1473 check_matrix_pointer_lossage (struct glyph_matrix
*matrix
)
1477 for (i
= 0; i
< matrix
->nrows
; ++i
)
1478 for (j
= 0; j
< matrix
->nrows
; ++j
)
1480 || (matrix
->rows
[i
].glyphs
[TEXT_AREA
]
1481 != matrix
->rows
[j
].glyphs
[TEXT_AREA
]));
1485 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1488 matrix_row (struct glyph_matrix
*matrix
, int row
)
1490 eassert (matrix
&& matrix
->rows
);
1491 eassert (row
>= 0 && row
< matrix
->nrows
);
1493 /* That's really too slow for normal testing because this function
1494 is called almost everywhere. Although---it's still astonishingly
1495 fast, so it is valuable to have for debugging purposes. */
1497 check_matrix_pointer_lossage (matrix
);
1500 return matrix
->rows
+ row
;
1504 #if 0 /* This function makes invalid assumptions when text is
1505 partially invisible. But it might come handy for debugging
1508 /* Check invariants that must hold for an up to date current matrix of
1512 check_matrix_invariants (struct window
*w
)
1514 struct glyph_matrix
*matrix
= w
->current_matrix
;
1515 int yb
= window_text_bottom_y (w
);
1516 struct glyph_row
*row
= matrix
->rows
;
1517 struct glyph_row
*last_text_row
= NULL
;
1518 struct buffer
*saved
= current_buffer
;
1519 struct buffer
*buffer
= XBUFFER (WVAR (w
, buffer
));
1522 /* This can sometimes happen for a fresh window. */
1523 if (matrix
->nrows
< 2)
1526 set_buffer_temp (buffer
);
1528 /* Note: last row is always reserved for the mode line. */
1529 while (MATRIX_ROW_DISPLAYS_TEXT_P (row
)
1530 && MATRIX_ROW_BOTTOM_Y (row
) < yb
)
1532 struct glyph_row
*next
= row
+ 1;
1534 if (MATRIX_ROW_DISPLAYS_TEXT_P (row
))
1535 last_text_row
= row
;
1537 /* Check that character and byte positions are in sync. */
1538 eassert (MATRIX_ROW_START_BYTEPOS (row
)
1539 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row
)));
1540 eassert (BYTEPOS (row
->start
.pos
)
1541 == CHAR_TO_BYTE (CHARPOS (row
->start
.pos
)));
1543 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1544 have such a position temporarily in case of a minibuffer
1545 displaying something like `[Sole completion]' at its end. */
1546 if (MATRIX_ROW_END_CHARPOS (row
) < BUF_ZV (current_buffer
))
1548 eassert (MATRIX_ROW_END_BYTEPOS (row
)
1549 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row
)));
1550 eassert (BYTEPOS (row
->end
.pos
)
1551 == CHAR_TO_BYTE (CHARPOS (row
->end
.pos
)));
1554 /* Check that end position of `row' is equal to start position
1556 if (next
->enabled_p
&& MATRIX_ROW_DISPLAYS_TEXT_P (next
))
1558 eassert (MATRIX_ROW_END_CHARPOS (row
)
1559 == MATRIX_ROW_START_CHARPOS (next
));
1560 eassert (MATRIX_ROW_END_BYTEPOS (row
)
1561 == MATRIX_ROW_START_BYTEPOS (next
));
1562 eassert (CHARPOS (row
->end
.pos
) == CHARPOS (next
->start
.pos
));
1563 eassert (BYTEPOS (row
->end
.pos
) == BYTEPOS (next
->start
.pos
));
1568 eassert (w
->current_matrix
->nrows
== w
->desired_matrix
->nrows
);
1569 eassert (w
->desired_matrix
->rows
!= NULL
);
1570 set_buffer_temp (saved
);
1575 #endif /* GLYPH_DEBUG */
1579 /**********************************************************************
1580 Allocating/ Adjusting Glyph Matrices
1581 **********************************************************************/
1583 /* Allocate glyph matrices over a window tree for a frame-based
1586 X and Y are column/row within the frame glyph matrix where
1587 sub-matrices for the window tree rooted at WINDOW must be
1588 allocated. DIM_ONLY_P non-zero means that the caller of this
1589 function is only interested in the result matrix dimension, and
1590 matrix adjustments should not be performed.
1592 The function returns the total width/height of the sub-matrices of
1593 the window tree. If called on a frame root window, the computation
1594 will take the mini-buffer window into account.
1596 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1598 NEW_LEAF_MATRIX set if any window in the tree did not have a
1599 glyph matrices yet, and
1601 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1602 any window in the tree will be changed or have been changed (see
1605 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1608 Windows are arranged into chains of windows on the same level
1609 through the next fields of window structures. Such a level can be
1610 either a sequence of horizontally adjacent windows from left to
1611 right, or a sequence of vertically adjacent windows from top to
1612 bottom. Each window in a horizontal sequence can be either a leaf
1613 window or a vertical sequence; a window in a vertical sequence can
1614 be either a leaf or a horizontal sequence. All windows in a
1615 horizontal sequence have the same height, and all windows in a
1616 vertical sequence have the same width.
1618 This function uses, for historical reasons, a more general
1619 algorithm to determine glyph matrix dimensions that would be
1622 The matrix height of a horizontal sequence is determined by the
1623 maximum height of any matrix in the sequence. The matrix width of
1624 a horizontal sequence is computed by adding up matrix widths of
1625 windows in the sequence.
1627 |<------- result width ------->|
1628 +---------+----------+---------+ ---
1631 +---------+ | | result height
1636 The matrix width of a vertical sequence is the maximum matrix width
1637 of any window in the sequence. Its height is computed by adding up
1638 matrix heights of windows in the sequence.
1640 |<---- result width -->|
1648 +------------+---------+ |
1651 +------------+---------+ --- */
1653 /* Bit indicating that a new matrix will be allocated or has been
1656 #define NEW_LEAF_MATRIX (1 << 0)
1658 /* Bit indicating that a matrix will or has changed its location or
1661 #define CHANGED_LEAF_MATRIX (1 << 1)
1664 allocate_matrices_for_frame_redisplay (Lisp_Object window
, int x
, int y
,
1665 int dim_only_p
, int *window_change_flags
)
1667 struct frame
*f
= XFRAME (WINDOW_FRAME (XWINDOW (window
)));
1669 int wmax
= 0, hmax
= 0;
1673 int in_horz_combination_p
;
1675 /* What combination is WINDOW part of? Compute this once since the
1676 result is the same for all windows in the `next' chain. The
1677 special case of a root window (parent equal to nil) is treated
1678 like a vertical combination because a root window's `next'
1679 points to the mini-buffer window, if any, which is arranged
1680 vertically below other windows. */
1681 in_horz_combination_p
1682 = (!NILP (WVAR (XWINDOW (window
), parent
))
1683 && !NILP (WVAR (XWINDOW (WVAR (XWINDOW (window
), parent
)), hchild
)));
1685 /* For WINDOW and all windows on the same level. */
1688 w
= XWINDOW (window
);
1690 /* Get the dimension of the window sub-matrix for W, depending
1691 on whether this is a combination or a leaf window. */
1692 if (!NILP (WVAR (w
, hchild
)))
1693 dim
= allocate_matrices_for_frame_redisplay (WVAR (w
, hchild
), x
, y
,
1695 window_change_flags
);
1696 else if (!NILP (WVAR (w
, vchild
)))
1697 dim
= allocate_matrices_for_frame_redisplay (WVAR (w
, vchild
), x
, y
,
1699 window_change_flags
);
1702 /* If not already done, allocate sub-matrix structures. */
1703 if (w
->desired_matrix
== NULL
)
1705 w
->desired_matrix
= new_glyph_matrix (f
->desired_pool
);
1706 w
->current_matrix
= new_glyph_matrix (f
->current_pool
);
1707 *window_change_flags
|= NEW_LEAF_MATRIX
;
1710 /* Width and height MUST be chosen so that there are no
1711 holes in the frame matrix. */
1712 dim
.width
= required_matrix_width (w
);
1713 dim
.height
= required_matrix_height (w
);
1715 /* Will matrix be re-allocated? */
1716 if (x
!= w
->desired_matrix
->matrix_x
1717 || y
!= w
->desired_matrix
->matrix_y
1718 || dim
.width
!= w
->desired_matrix
->matrix_w
1719 || dim
.height
!= w
->desired_matrix
->matrix_h
1720 || (margin_glyphs_to_reserve (w
, dim
.width
,
1721 WVAR (w
, left_margin_cols
))
1722 != w
->desired_matrix
->left_margin_glyphs
)
1723 || (margin_glyphs_to_reserve (w
, dim
.width
,
1724 WVAR (w
, right_margin_cols
))
1725 != w
->desired_matrix
->right_margin_glyphs
))
1726 *window_change_flags
|= CHANGED_LEAF_MATRIX
;
1728 /* Actually change matrices, if allowed. Do not consider
1729 CHANGED_LEAF_MATRIX computed above here because the pool
1730 may have been changed which we don't now here. We trust
1731 that we only will be called with DIM_ONLY_P != 0 when
1735 adjust_glyph_matrix (w
, w
->desired_matrix
, x
, y
, dim
);
1736 adjust_glyph_matrix (w
, w
->current_matrix
, x
, y
, dim
);
1740 /* If we are part of a horizontal combination, advance x for
1741 windows to the right of W; otherwise advance y for windows
1743 if (in_horz_combination_p
)
1748 /* Remember maximum glyph matrix dimensions. */
1749 wmax
= max (wmax
, dim
.width
);
1750 hmax
= max (hmax
, dim
.height
);
1752 /* Next window on same level. */
1753 window
= WVAR (w
, next
);
1755 while (!NILP (window
));
1757 /* Set `total' to the total glyph matrix dimension of this window
1758 level. In a vertical combination, the width is the width of the
1759 widest window; the height is the y we finally reached, corrected
1760 by the y we started with. In a horizontal combination, the total
1761 height is the height of the tallest window, and the width is the
1762 x we finally reached, corrected by the x we started with. */
1763 if (in_horz_combination_p
)
1765 total
.width
= x
- x0
;
1766 total
.height
= hmax
;
1771 total
.height
= y
- y0
;
1778 /* Return the required height of glyph matrices for window W. */
1781 required_matrix_height (struct window
*w
)
1783 #ifdef HAVE_WINDOW_SYSTEM
1784 struct frame
*f
= XFRAME (WVAR (w
, frame
));
1786 if (FRAME_WINDOW_P (f
))
1788 int ch_height
= FRAME_SMALLEST_FONT_HEIGHT (f
);
1789 int window_pixel_height
= window_box_height (w
) + eabs (w
->vscroll
);
1790 return (((window_pixel_height
+ ch_height
- 1)
1791 / ch_height
) * w
->nrows_scale_factor
1792 /* One partially visible line at the top and
1793 bottom of the window. */
1795 /* 2 for header and mode line. */
1798 #endif /* HAVE_WINDOW_SYSTEM */
1800 return WINDOW_TOTAL_LINES (w
);
1804 /* Return the required width of glyph matrices for window W. */
1807 required_matrix_width (struct window
*w
)
1809 #ifdef HAVE_WINDOW_SYSTEM
1810 struct frame
*f
= XFRAME (WVAR (w
, frame
));
1811 if (FRAME_WINDOW_P (f
))
1813 int ch_width
= FRAME_SMALLEST_CHAR_WIDTH (f
);
1814 int window_pixel_width
= WINDOW_TOTAL_WIDTH (w
);
1816 /* Compute number of glyphs needed in a glyph row. */
1817 return (((window_pixel_width
+ ch_width
- 1)
1818 / ch_width
) * w
->ncols_scale_factor
1819 /* 2 partially visible columns in the text area. */
1821 /* One partially visible column at the right
1822 edge of each marginal area. */
1825 #endif /* HAVE_WINDOW_SYSTEM */
1827 return XINT (WVAR (w
, total_cols
));
1831 /* Allocate window matrices for window-based redisplay. W is the
1832 window whose matrices must be allocated/reallocated. */
1835 allocate_matrices_for_window_redisplay (struct window
*w
)
1839 if (!NILP (WVAR (w
, vchild
)))
1840 allocate_matrices_for_window_redisplay (XWINDOW (WVAR (w
, vchild
)));
1841 else if (!NILP (WVAR (w
, hchild
)))
1842 allocate_matrices_for_window_redisplay (XWINDOW (WVAR (w
, hchild
)));
1845 /* W is a leaf window. */
1848 /* If matrices are not yet allocated, allocate them now. */
1849 if (w
->desired_matrix
== NULL
)
1851 w
->desired_matrix
= new_glyph_matrix (NULL
);
1852 w
->current_matrix
= new_glyph_matrix (NULL
);
1855 dim
.width
= required_matrix_width (w
);
1856 dim
.height
= required_matrix_height (w
);
1857 adjust_glyph_matrix (w
, w
->desired_matrix
, 0, 0, dim
);
1858 adjust_glyph_matrix (w
, w
->current_matrix
, 0, 0, dim
);
1861 w
= NILP (WVAR (w
, next
)) ? NULL
: XWINDOW (WVAR (w
, next
));
1866 /* Re-allocate/ re-compute glyph matrices on frame F. If F is null,
1867 do it for all frames; otherwise do it just for the given frame.
1868 This function must be called when a new frame is created, its size
1869 changes, or its window configuration changes. */
1872 adjust_glyphs (struct frame
*f
)
1874 /* Block input so that expose events and other events that access
1875 glyph matrices are not processed while we are changing them. */
1879 adjust_frame_glyphs (f
);
1882 Lisp_Object tail
, lisp_frame
;
1884 FOR_EACH_FRAME (tail
, lisp_frame
)
1885 adjust_frame_glyphs (XFRAME (lisp_frame
));
1892 /* Adjust frame glyphs when Emacs is initialized.
1894 To be called from init_display.
1896 We need a glyph matrix because redraw will happen soon.
1897 Unfortunately, window sizes on selected_frame are not yet set to
1898 meaningful values. I believe we can assume that there are only two
1899 windows on the frame---the mini-buffer and the root window. Frame
1900 height and width seem to be correct so far. So, set the sizes of
1901 windows to estimated values. */
1904 adjust_frame_glyphs_initially (void)
1906 struct frame
*sf
= SELECTED_FRAME ();
1907 struct window
*root
= XWINDOW (FVAR (sf
, root_window
));
1908 struct window
*mini
= XWINDOW (WVAR (root
, next
));
1909 int frame_lines
= FRAME_LINES (sf
);
1910 int frame_cols
= FRAME_COLS (sf
);
1911 int top_margin
= FRAME_TOP_MARGIN (sf
);
1913 /* Do it for the root window. */
1914 XSETFASTINT (WVAR (root
, top_line
), top_margin
);
1915 XSETFASTINT (WVAR (root
, total_lines
), frame_lines
- 1 - top_margin
);
1916 XSETFASTINT (WVAR (root
, total_cols
), frame_cols
);
1918 /* Do it for the mini-buffer window. */
1919 XSETFASTINT (WVAR (mini
, top_line
), frame_lines
- 1);
1920 XSETFASTINT (WVAR (mini
, total_lines
), 1);
1921 XSETFASTINT (WVAR (mini
, total_cols
), frame_cols
);
1923 adjust_frame_glyphs (sf
);
1924 glyphs_initialized_initially_p
= 1;
1928 /* Allocate/reallocate glyph matrices of a single frame F. */
1931 adjust_frame_glyphs (struct frame
*f
)
1933 if (FRAME_WINDOW_P (f
))
1934 adjust_frame_glyphs_for_window_redisplay (f
);
1936 adjust_frame_glyphs_for_frame_redisplay (f
);
1938 /* Don't forget the message buffer and the buffer for
1939 decode_mode_spec. */
1940 adjust_frame_message_buffer (f
);
1941 adjust_decode_mode_spec_buffer (f
);
1943 f
->glyphs_initialized_p
= 1;
1946 /* Return 1 if any window in the tree has nonzero window margins. See
1947 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1949 showing_window_margins_p (struct window
*w
)
1953 if (!NILP (WVAR (w
, hchild
)))
1955 if (showing_window_margins_p (XWINDOW (WVAR (w
, hchild
))))
1958 else if (!NILP (WVAR (w
, vchild
)))
1960 if (showing_window_margins_p (XWINDOW (WVAR (w
, vchild
))))
1963 else if (!NILP (WVAR (w
, left_margin_cols
))
1964 || !NILP (WVAR (w
, right_margin_cols
)))
1967 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
1973 /* In the window tree with root W, build current matrices of leaf
1974 windows from the frame's current matrix. */
1977 fake_current_matrices (Lisp_Object window
)
1981 for (; !NILP (window
); window
= WVAR (w
, next
))
1983 w
= XWINDOW (window
);
1985 if (!NILP (WVAR (w
, hchild
)))
1986 fake_current_matrices (WVAR (w
, hchild
));
1987 else if (!NILP (WVAR (w
, vchild
)))
1988 fake_current_matrices (WVAR (w
, vchild
));
1992 struct frame
*f
= XFRAME (WVAR (w
, frame
));
1993 struct glyph_matrix
*m
= w
->current_matrix
;
1994 struct glyph_matrix
*fm
= f
->current_matrix
;
1996 eassert (m
->matrix_h
== WINDOW_TOTAL_LINES (w
));
1997 eassert (m
->matrix_w
== WINDOW_TOTAL_COLS (w
));
1999 for (i
= 0; i
< m
->matrix_h
; ++i
)
2001 struct glyph_row
*r
= m
->rows
+ i
;
2002 struct glyph_row
*fr
= fm
->rows
+ i
+ WINDOW_TOP_EDGE_LINE (w
);
2004 eassert (r
->glyphs
[TEXT_AREA
] >= fr
->glyphs
[TEXT_AREA
]
2005 && r
->glyphs
[LAST_AREA
] <= fr
->glyphs
[LAST_AREA
]);
2007 r
->enabled_p
= fr
->enabled_p
;
2010 r
->used
[LEFT_MARGIN_AREA
] = m
->left_margin_glyphs
;
2011 r
->used
[RIGHT_MARGIN_AREA
] = m
->right_margin_glyphs
;
2012 r
->used
[TEXT_AREA
] = (m
->matrix_w
2013 - r
->used
[LEFT_MARGIN_AREA
]
2014 - r
->used
[RIGHT_MARGIN_AREA
]);
2023 /* Save away the contents of frame F's current frame matrix. Value is
2024 a glyph matrix holding the contents of F's current frame matrix. */
2026 static struct glyph_matrix
*
2027 save_current_matrix (struct frame
*f
)
2030 struct glyph_matrix
*saved
= xzalloc (sizeof *saved
);
2031 saved
->nrows
= f
->current_matrix
->nrows
;
2032 saved
->rows
= xzalloc (saved
->nrows
* sizeof *saved
->rows
);
2034 for (i
= 0; i
< saved
->nrows
; ++i
)
2036 struct glyph_row
*from
= f
->current_matrix
->rows
+ i
;
2037 struct glyph_row
*to
= saved
->rows
+ i
;
2038 ptrdiff_t nbytes
= from
->used
[TEXT_AREA
] * sizeof (struct glyph
);
2039 to
->glyphs
[TEXT_AREA
] = xmalloc (nbytes
);
2040 memcpy (to
->glyphs
[TEXT_AREA
], from
->glyphs
[TEXT_AREA
], nbytes
);
2041 to
->used
[TEXT_AREA
] = from
->used
[TEXT_AREA
];
2048 /* Restore the contents of frame F's current frame matrix from SAVED,
2049 and free memory associated with SAVED. */
2052 restore_current_matrix (struct frame
*f
, struct glyph_matrix
*saved
)
2056 for (i
= 0; i
< saved
->nrows
; ++i
)
2058 struct glyph_row
*from
= saved
->rows
+ i
;
2059 struct glyph_row
*to
= f
->current_matrix
->rows
+ i
;
2060 ptrdiff_t nbytes
= from
->used
[TEXT_AREA
] * sizeof (struct glyph
);
2061 memcpy (to
->glyphs
[TEXT_AREA
], from
->glyphs
[TEXT_AREA
], nbytes
);
2062 to
->used
[TEXT_AREA
] = from
->used
[TEXT_AREA
];
2063 xfree (from
->glyphs
[TEXT_AREA
]);
2066 xfree (saved
->rows
);
2072 /* Allocate/reallocate glyph matrices of a single frame F for
2073 frame-based redisplay. */
2076 adjust_frame_glyphs_for_frame_redisplay (struct frame
*f
)
2078 struct dim matrix_dim
;
2080 int window_change_flags
;
2083 if (!FRAME_LIVE_P (f
))
2086 top_window_y
= FRAME_TOP_MARGIN (f
);
2088 /* Allocate glyph pool structures if not already done. */
2089 if (f
->desired_pool
== NULL
)
2091 f
->desired_pool
= new_glyph_pool ();
2092 f
->current_pool
= new_glyph_pool ();
2095 /* Allocate frames matrix structures if needed. */
2096 if (f
->desired_matrix
== NULL
)
2098 f
->desired_matrix
= new_glyph_matrix (f
->desired_pool
);
2099 f
->current_matrix
= new_glyph_matrix (f
->current_pool
);
2102 /* Compute window glyph matrices. (This takes the mini-buffer
2103 window into account). The result is the size of the frame glyph
2104 matrix needed. The variable window_change_flags is set to a bit
2105 mask indicating whether new matrices will be allocated or
2106 existing matrices change their size or location within the frame
2108 window_change_flags
= 0;
2110 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f
),
2113 &window_change_flags
);
2115 /* Add in menu bar lines, if any. */
2116 matrix_dim
.height
+= top_window_y
;
2118 /* Enlarge pools as necessary. */
2119 pool_changed_p
= realloc_glyph_pool (f
->desired_pool
, matrix_dim
);
2120 realloc_glyph_pool (f
->current_pool
, matrix_dim
);
2122 /* Set up glyph pointers within window matrices. Do this only if
2123 absolutely necessary since it requires a frame redraw. */
2124 if (pool_changed_p
|| window_change_flags
)
2126 /* Do it for window matrices. */
2127 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f
),
2129 &window_change_flags
);
2131 /* Size of frame matrices must equal size of frame. Note
2132 that we are called for X frames with window widths NOT equal
2133 to the frame width (from CHANGE_FRAME_SIZE_1). */
2134 eassert (matrix_dim
.width
== FRAME_COLS (f
)
2135 && matrix_dim
.height
== FRAME_LINES (f
));
2137 /* Pointers to glyph memory in glyph rows are exchanged during
2138 the update phase of redisplay, which means in general that a
2139 frame's current matrix consists of pointers into both the
2140 desired and current glyph pool of the frame. Adjusting a
2141 matrix sets the frame matrix up so that pointers are all into
2142 the same pool. If we want to preserve glyph contents of the
2143 current matrix over a call to adjust_glyph_matrix, we must
2144 make a copy of the current glyphs, and restore the current
2145 matrix' contents from that copy. */
2146 if (display_completed
2147 && !FRAME_GARBAGED_P (f
)
2148 && matrix_dim
.width
== f
->current_matrix
->matrix_w
2149 && matrix_dim
.height
== f
->current_matrix
->matrix_h
2150 /* For some reason, the frame glyph matrix gets corrupted if
2151 any of the windows contain margins. I haven't been able
2152 to hunt down the reason, but for the moment this prevents
2153 the problem from manifesting. -- cyd */
2154 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f
))))
2156 struct glyph_matrix
*copy
= save_current_matrix (f
);
2157 adjust_glyph_matrix (NULL
, f
->desired_matrix
, 0, 0, matrix_dim
);
2158 adjust_glyph_matrix (NULL
, f
->current_matrix
, 0, 0, matrix_dim
);
2159 restore_current_matrix (f
, copy
);
2160 fake_current_matrices (FRAME_ROOT_WINDOW (f
));
2164 adjust_glyph_matrix (NULL
, f
->desired_matrix
, 0, 0, matrix_dim
);
2165 adjust_glyph_matrix (NULL
, f
->current_matrix
, 0, 0, matrix_dim
);
2166 SET_FRAME_GARBAGED (f
);
2172 /* Allocate/reallocate glyph matrices of a single frame F for
2173 window-based redisplay. */
2176 adjust_frame_glyphs_for_window_redisplay (struct frame
*f
)
2178 eassert (FRAME_WINDOW_P (f
) && FRAME_LIVE_P (f
));
2180 /* Allocate/reallocate window matrices. */
2181 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f
)));
2183 #ifdef HAVE_X_WINDOWS
2184 /* Allocate/ reallocate matrices of the dummy window used to display
2185 the menu bar under X when no X toolkit support is available. */
2186 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2188 /* Allocate a dummy window if not already done. */
2190 if (NILP (FVAR (f
, menu_bar_window
)))
2192 FVAR (f
, menu_bar_window
) = make_window ();
2193 w
= XWINDOW (FVAR (f
, menu_bar_window
));
2194 XSETFRAME (WVAR (w
, frame
), f
);
2195 w
->pseudo_window_p
= 1;
2198 w
= XWINDOW (FVAR (f
, menu_bar_window
));
2200 /* Set window dimensions to frame dimensions and allocate or
2201 adjust glyph matrices of W. */
2202 XSETFASTINT (WVAR (w
, top_line
), 0);
2203 XSETFASTINT (WVAR (w
, left_col
), 0);
2204 XSETFASTINT (WVAR (w
, total_lines
), FRAME_MENU_BAR_LINES (f
));
2205 XSETFASTINT (WVAR (w
, total_cols
), FRAME_TOTAL_COLS (f
));
2206 allocate_matrices_for_window_redisplay (w
);
2208 #endif /* not USE_X_TOOLKIT && not USE_GTK */
2209 #endif /* HAVE_X_WINDOWS */
2213 /* Allocate/ reallocate matrices of the tool bar window. If we
2214 don't have a tool bar window yet, make one. */
2216 if (NILP (FVAR (f
, tool_bar_window
)))
2218 FVAR (f
, tool_bar_window
) = make_window ();
2219 w
= XWINDOW (FVAR (f
, tool_bar_window
));
2220 XSETFRAME (WVAR (w
, frame
), f
);
2221 w
->pseudo_window_p
= 1;
2224 w
= XWINDOW (FVAR (f
, tool_bar_window
));
2226 XSETFASTINT (WVAR (w
, top_line
), FRAME_MENU_BAR_LINES (f
));
2227 XSETFASTINT (WVAR (w
, left_col
), 0);
2228 XSETFASTINT (WVAR (w
, total_lines
), FRAME_TOOL_BAR_LINES (f
));
2229 XSETFASTINT (WVAR (w
, total_cols
), FRAME_TOTAL_COLS (f
));
2230 allocate_matrices_for_window_redisplay (w
);
2236 /* Adjust/ allocate message buffer of frame F.
2238 Note that the message buffer is never freed. Since I could not
2239 find a free in 19.34, I assume that freeing it would be
2240 problematic in some way and don't do it either.
2242 (Implementation note: It should be checked if we can free it
2243 eventually without causing trouble). */
2246 adjust_frame_message_buffer (struct frame
*f
)
2248 FRAME_MESSAGE_BUF (f
) = xrealloc (FRAME_MESSAGE_BUF (f
),
2249 FRAME_MESSAGE_BUF_SIZE (f
) + 1);
2253 /* Re-allocate buffer for decode_mode_spec on frame F. */
2256 adjust_decode_mode_spec_buffer (struct frame
*f
)
2258 f
->decode_mode_spec_buffer
= xrealloc (f
->decode_mode_spec_buffer
,
2259 FRAME_MESSAGE_BUF_SIZE (f
) + 1);
2264 /**********************************************************************
2265 Freeing Glyph Matrices
2266 **********************************************************************/
2268 /* Free glyph memory for a frame F. F may be null. This function can
2269 be called for the same frame more than once. The root window of
2270 F may be nil when this function is called. This is the case when
2271 the function is called when F is destroyed. */
2274 free_glyphs (struct frame
*f
)
2276 if (f
&& f
->glyphs_initialized_p
)
2278 /* Block interrupt input so that we don't get surprised by an X
2279 event while we're in an inconsistent state. */
2281 f
->glyphs_initialized_p
= 0;
2283 /* Release window sub-matrices. */
2284 if (!NILP (FVAR (f
, root_window
)))
2285 free_window_matrices (XWINDOW (FVAR (f
, root_window
)));
2287 /* Free the dummy window for menu bars without X toolkit and its
2289 if (!NILP (FVAR (f
, menu_bar_window
)))
2291 struct window
*w
= XWINDOW (FVAR (f
, menu_bar_window
));
2292 free_glyph_matrix (w
->desired_matrix
);
2293 free_glyph_matrix (w
->current_matrix
);
2294 w
->desired_matrix
= w
->current_matrix
= NULL
;
2295 FVAR (f
, menu_bar_window
) = Qnil
;
2298 /* Free the tool bar window and its glyph matrices. */
2299 if (!NILP (FVAR (f
, tool_bar_window
)))
2301 struct window
*w
= XWINDOW (FVAR (f
, tool_bar_window
));
2302 free_glyph_matrix (w
->desired_matrix
);
2303 free_glyph_matrix (w
->current_matrix
);
2304 w
->desired_matrix
= w
->current_matrix
= NULL
;
2305 FVAR (f
, tool_bar_window
) = Qnil
;
2308 /* Release frame glyph matrices. Reset fields to zero in
2309 case we are called a second time. */
2310 if (f
->desired_matrix
)
2312 free_glyph_matrix (f
->desired_matrix
);
2313 free_glyph_matrix (f
->current_matrix
);
2314 f
->desired_matrix
= f
->current_matrix
= NULL
;
2317 /* Release glyph pools. */
2318 if (f
->desired_pool
)
2320 free_glyph_pool (f
->desired_pool
);
2321 free_glyph_pool (f
->current_pool
);
2322 f
->desired_pool
= f
->current_pool
= NULL
;
2330 /* Free glyph sub-matrices in the window tree rooted at W. This
2331 function may be called with a null pointer, and it may be called on
2332 the same tree more than once. */
2335 free_window_matrices (struct window
*w
)
2339 if (!NILP (WVAR (w
, hchild
)))
2340 free_window_matrices (XWINDOW (WVAR (w
, hchild
)));
2341 else if (!NILP (WVAR (w
, vchild
)))
2342 free_window_matrices (XWINDOW (WVAR (w
, vchild
)));
2345 /* This is a leaf window. Free its memory and reset fields
2346 to zero in case this function is called a second time for
2348 free_glyph_matrix (w
->current_matrix
);
2349 free_glyph_matrix (w
->desired_matrix
);
2350 w
->current_matrix
= w
->desired_matrix
= NULL
;
2353 /* Next window on same level. */
2354 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
2359 /* Check glyph memory leaks. This function is called from
2360 shut_down_emacs. Note that frames are not destroyed when Emacs
2361 exits. We therefore free all glyph memory for all active frames
2362 explicitly and check that nothing is left allocated. */
2365 check_glyph_memory (void)
2367 Lisp_Object tail
, frame
;
2369 /* Free glyph memory for all frames. */
2370 FOR_EACH_FRAME (tail
, frame
)
2371 free_glyphs (XFRAME (frame
));
2373 /* Check that nothing is left allocated. */
2374 if (glyph_matrix_count
)
2376 if (glyph_pool_count
)
2382 /**********************************************************************
2383 Building a Frame Matrix
2384 **********************************************************************/
2386 /* Most of the redisplay code works on glyph matrices attached to
2387 windows. This is a good solution most of the time, but it is not
2388 suitable for terminal code. Terminal output functions cannot rely
2389 on being able to set an arbitrary terminal window. Instead they
2390 must be provided with a view of the whole frame, i.e. the whole
2391 screen. We build such a view by constructing a frame matrix from
2392 window matrices in this section.
2394 Windows that must be updated have their must_be_update_p flag set.
2395 For all such windows, their desired matrix is made part of the
2396 desired frame matrix. For other windows, their current matrix is
2397 made part of the desired frame matrix.
2399 +-----------------+----------------+
2400 | desired | desired |
2402 +-----------------+----------------+
2405 +----------------------------------+
2407 Desired window matrices can be made part of the frame matrix in a
2408 cheap way: We exploit the fact that the desired frame matrix and
2409 desired window matrices share their glyph memory. This is not
2410 possible for current window matrices. Their glyphs are copied to
2411 the desired frame matrix. The latter is equivalent to
2412 preserve_other_columns in the old redisplay.
2414 Used glyphs counters for frame matrix rows are the result of adding
2415 up glyph lengths of the window matrices. A line in the frame
2416 matrix is enabled, if a corresponding line in a window matrix is
2419 After building the desired frame matrix, it will be passed to
2420 terminal code, which will manipulate both the desired and current
2421 frame matrix. Changes applied to the frame's current matrix have
2422 to be visible in current window matrices afterwards, of course.
2424 This problem is solved like this:
2426 1. Window and frame matrices share glyphs. Window matrices are
2427 constructed in a way that their glyph contents ARE the glyph
2428 contents needed in a frame matrix. Thus, any modification of
2429 glyphs done in terminal code will be reflected in window matrices
2432 2. Exchanges of rows in a frame matrix done by terminal code are
2433 intercepted by hook functions so that corresponding row operations
2434 on window matrices can be performed. This is necessary because we
2435 use pointers to glyphs in glyph row structures. To satisfy the
2436 assumption of point 1 above that glyphs are updated implicitly in
2437 window matrices when they are manipulated via the frame matrix,
2438 window and frame matrix must of course agree where to find the
2439 glyphs for their rows. Possible manipulations that must be
2440 mirrored are assignments of rows of the desired frame matrix to the
2441 current frame matrix and scrolling the current frame matrix. */
2443 /* Build frame F's desired matrix from window matrices. Only windows
2444 which have the flag must_be_updated_p set have to be updated. Menu
2445 bar lines of a frame are not covered by window matrices, so make
2446 sure not to touch them in this function. */
2449 build_frame_matrix (struct frame
*f
)
2453 /* F must have a frame matrix when this function is called. */
2454 eassert (!FRAME_WINDOW_P (f
));
2456 /* Clear all rows in the frame matrix covered by window matrices.
2457 Menu bar lines are not covered by windows. */
2458 for (i
= FRAME_TOP_MARGIN (f
); i
< f
->desired_matrix
->nrows
; ++i
)
2459 clear_glyph_row (MATRIX_ROW (f
->desired_matrix
, i
));
2461 /* Build the matrix by walking the window tree. */
2462 build_frame_matrix_from_window_tree (f
->desired_matrix
,
2463 XWINDOW (FRAME_ROOT_WINDOW (f
)));
2467 /* Walk a window tree, building a frame matrix MATRIX from window
2468 matrices. W is the root of a window tree. */
2471 build_frame_matrix_from_window_tree (struct glyph_matrix
*matrix
, struct window
*w
)
2475 if (!NILP (WVAR (w
, hchild
)))
2476 build_frame_matrix_from_window_tree (matrix
, XWINDOW (WVAR (w
, hchild
)));
2477 else if (!NILP (WVAR (w
, vchild
)))
2478 build_frame_matrix_from_window_tree (matrix
, XWINDOW (WVAR (w
, vchild
)));
2480 build_frame_matrix_from_leaf_window (matrix
, w
);
2482 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
2487 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2488 desired frame matrix built. W is a leaf window whose desired or
2489 current matrix is to be added to FRAME_MATRIX. W's flag
2490 must_be_updated_p determines which matrix it contributes to
2491 FRAME_MATRIX. If must_be_updated_p is non-zero, W's desired matrix
2492 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2493 Adding a desired matrix means setting up used counters and such in
2494 frame rows, while adding a current window matrix to FRAME_MATRIX
2495 means copying glyphs. The latter case corresponds to
2496 preserve_other_columns in the old redisplay. */
2499 build_frame_matrix_from_leaf_window (struct glyph_matrix
*frame_matrix
, struct window
*w
)
2501 struct glyph_matrix
*window_matrix
;
2502 int window_y
, frame_y
;
2503 /* If non-zero, a glyph to insert at the right border of W. */
2504 GLYPH right_border_glyph
;
2506 SET_GLYPH_FROM_CHAR (right_border_glyph
, 0);
2508 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2509 if (w
->must_be_updated_p
)
2511 window_matrix
= w
->desired_matrix
;
2513 /* Decide whether we want to add a vertical border glyph. */
2514 if (!WINDOW_RIGHTMOST_P (w
))
2516 struct Lisp_Char_Table
*dp
= window_display_table (w
);
2519 SET_GLYPH_FROM_CHAR (right_border_glyph
, '|');
2521 && (gc
= DISP_BORDER_GLYPH (dp
), GLYPH_CODE_P (gc
)))
2523 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph
, gc
);
2524 spec_glyph_lookup_face (w
, &right_border_glyph
);
2527 if (GLYPH_FACE (right_border_glyph
) <= 0)
2528 SET_GLYPH_FACE (right_border_glyph
, VERTICAL_BORDER_FACE_ID
);
2532 window_matrix
= w
->current_matrix
;
2534 /* For all rows in the window matrix and corresponding rows in the
2537 frame_y
= window_matrix
->matrix_y
;
2538 while (window_y
< window_matrix
->nrows
)
2540 struct glyph_row
*frame_row
= frame_matrix
->rows
+ frame_y
;
2541 struct glyph_row
*window_row
= window_matrix
->rows
+ window_y
;
2542 int current_row_p
= window_matrix
== w
->current_matrix
;
2544 /* Fill up the frame row with spaces up to the left margin of the
2546 fill_up_frame_row_with_spaces (frame_row
, window_matrix
->matrix_x
);
2548 /* Fill up areas in the window matrix row with spaces. */
2549 fill_up_glyph_row_with_spaces (window_row
);
2551 /* If only part of W's desired matrix has been built, and
2552 window_row wasn't displayed, use the corresponding current
2554 if (window_matrix
== w
->desired_matrix
2555 && !window_row
->enabled_p
)
2557 window_row
= w
->current_matrix
->rows
+ window_y
;
2563 /* Copy window row to frame row. */
2564 memcpy (frame_row
->glyphs
[TEXT_AREA
] + window_matrix
->matrix_x
,
2565 window_row
->glyphs
[0],
2566 window_matrix
->matrix_w
* sizeof (struct glyph
));
2570 eassert (window_row
->enabled_p
);
2572 /* Only when a desired row has been displayed, we want
2573 the corresponding frame row to be updated. */
2574 frame_row
->enabled_p
= 1;
2576 /* Maybe insert a vertical border between horizontally adjacent
2578 if (GLYPH_CHAR (right_border_glyph
) != 0)
2580 struct glyph
*border
= window_row
->glyphs
[LAST_AREA
] - 1;
2581 SET_CHAR_GLYPH_FROM_GLYPH (*border
, right_border_glyph
);
2585 /* Window row window_y must be a slice of frame row
2587 eassert (glyph_row_slice_p (window_row
, frame_row
));
2589 /* If rows are in sync, we don't have to copy glyphs because
2590 frame and window share glyphs. */
2592 strcpy (w
->current_matrix
->method
, w
->desired_matrix
->method
);
2593 add_window_display_history (w
, w
->current_matrix
->method
, 0);
2597 /* Set number of used glyphs in the frame matrix. Since we fill
2598 up with spaces, and visit leaf windows from left to right it
2599 can be done simply. */
2600 frame_row
->used
[TEXT_AREA
]
2601 = window_matrix
->matrix_x
+ window_matrix
->matrix_w
;
2609 /* Given a user-specified glyph, possibly including a Lisp-level face
2610 ID, return a glyph that has a realized face ID.
2611 This is used for glyphs displayed specially and not part of the text;
2612 for instance, vertical separators, truncation markers, etc. */
2615 spec_glyph_lookup_face (struct window
*w
, GLYPH
*glyph
)
2617 int lface_id
= GLYPH_FACE (*glyph
);
2618 /* Convert the glyph's specified face to a realized (cache) face. */
2621 int face_id
= merge_faces (XFRAME (WVAR (w
, frame
)),
2622 Qt
, lface_id
, DEFAULT_FACE_ID
);
2623 SET_GLYPH_FACE (*glyph
, face_id
);
2627 /* Add spaces to a glyph row ROW in a window matrix.
2629 Each row has the form:
2631 +---------+-----------------------------+------------+
2632 | left | text | right |
2633 +---------+-----------------------------+------------+
2635 Left and right marginal areas are optional. This function adds
2636 spaces to areas so that there are no empty holes between areas.
2637 In other words: If the right area is not empty, the text area
2638 is filled up with spaces up to the right area. If the text area
2639 is not empty, the left area is filled up.
2641 To be called for frame-based redisplay, only. */
2644 fill_up_glyph_row_with_spaces (struct glyph_row
*row
)
2646 fill_up_glyph_row_area_with_spaces (row
, LEFT_MARGIN_AREA
);
2647 fill_up_glyph_row_area_with_spaces (row
, TEXT_AREA
);
2648 fill_up_glyph_row_area_with_spaces (row
, RIGHT_MARGIN_AREA
);
2652 /* Fill area AREA of glyph row ROW with spaces. To be called for
2653 frame-based redisplay only. */
2656 fill_up_glyph_row_area_with_spaces (struct glyph_row
*row
, int area
)
2658 if (row
->glyphs
[area
] < row
->glyphs
[area
+ 1])
2660 struct glyph
*end
= row
->glyphs
[area
+ 1];
2661 struct glyph
*text
= row
->glyphs
[area
] + row
->used
[area
];
2664 *text
++ = space_glyph
;
2665 row
->used
[area
] = text
- row
->glyphs
[area
];
2670 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2671 reached. In frame matrices only one area, TEXT_AREA, is used. */
2674 fill_up_frame_row_with_spaces (struct glyph_row
*row
, int upto
)
2676 int i
= row
->used
[TEXT_AREA
];
2677 struct glyph
*glyph
= row
->glyphs
[TEXT_AREA
];
2680 glyph
[i
++] = space_glyph
;
2682 row
->used
[TEXT_AREA
] = i
;
2687 /**********************************************************************
2688 Mirroring operations on frame matrices in window matrices
2689 **********************************************************************/
2691 /* Set frame being updated via frame-based redisplay to F. This
2692 function must be called before updates to make explicit that we are
2693 working on frame matrices or not. */
2696 set_frame_matrix_frame (struct frame
*f
)
2698 frame_matrix_frame
= f
;
2702 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2703 DESIRED_MATRIX is the desired matrix corresponding to
2704 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2705 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2706 frame_matrix_frame is non-null, this indicates that the exchange is
2707 done in frame matrices, and that we have to perform analogous
2708 operations in window matrices of frame_matrix_frame. */
2711 make_current (struct glyph_matrix
*desired_matrix
, struct glyph_matrix
*current_matrix
, int row
)
2713 struct glyph_row
*current_row
= MATRIX_ROW (current_matrix
, row
);
2714 struct glyph_row
*desired_row
= MATRIX_ROW (desired_matrix
, row
);
2715 int mouse_face_p
= current_row
->mouse_face_p
;
2717 /* Do current_row = desired_row. This exchanges glyph pointers
2718 between both rows, and does a structure assignment otherwise. */
2719 assign_row (current_row
, desired_row
);
2721 /* Enable current_row to mark it as valid. */
2722 current_row
->enabled_p
= 1;
2723 current_row
->mouse_face_p
= mouse_face_p
;
2725 /* If we are called on frame matrices, perform analogous operations
2726 for window matrices. */
2727 if (frame_matrix_frame
)
2728 mirror_make_current (XWINDOW (FVAR (frame_matrix_frame
, root_window
)), row
);
2732 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2733 W's frame which has been made current (by swapping pointers between
2734 current and desired matrix). Perform analogous operations in the
2735 matrices of leaf windows in the window tree rooted at W. */
2738 mirror_make_current (struct window
*w
, int frame_row
)
2742 if (!NILP (WVAR (w
, hchild
)))
2743 mirror_make_current (XWINDOW (WVAR (w
, hchild
)), frame_row
);
2744 else if (!NILP (WVAR (w
, vchild
)))
2745 mirror_make_current (XWINDOW (WVAR (w
, vchild
)), frame_row
);
2748 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2749 here because the checks performed in debug mode there
2750 will not allow the conversion. */
2751 int row
= frame_row
- w
->desired_matrix
->matrix_y
;
2753 /* If FRAME_ROW is within W, assign the desired row to the
2754 current row (exchanging glyph pointers). */
2755 if (row
>= 0 && row
< w
->desired_matrix
->matrix_h
)
2757 struct glyph_row
*current_row
2758 = MATRIX_ROW (w
->current_matrix
, row
);
2759 struct glyph_row
*desired_row
2760 = MATRIX_ROW (w
->desired_matrix
, row
);
2762 if (desired_row
->enabled_p
)
2763 assign_row (current_row
, desired_row
);
2765 swap_glyph_pointers (desired_row
, current_row
);
2766 current_row
->enabled_p
= 1;
2768 /* Set the Y coordinate of the mode/header line's row.
2769 It is needed in draw_row_with_mouse_face to find the
2770 screen coordinates. (Window-based redisplay sets
2771 this in update_window, but no one seems to do that
2772 for frame-based redisplay.) */
2773 if (current_row
->mode_line_p
)
2774 current_row
->y
= row
;
2778 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
2783 /* Perform row dance after scrolling. We are working on the range of
2784 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2785 including) in MATRIX. COPY_FROM is a vector containing, for each
2786 row I in the range 0 <= I < NLINES, the index of the original line
2787 to move to I. This index is relative to the row range, i.e. 0 <=
2788 index < NLINES. RETAINED_P is a vector containing zero for each
2789 row 0 <= I < NLINES which is empty.
2791 This function is called from do_scrolling and do_direct_scrolling. */
2794 mirrored_line_dance (struct glyph_matrix
*matrix
, int unchanged_at_top
, int nlines
,
2795 int *copy_from
, char *retained_p
)
2797 /* A copy of original rows. */
2798 struct glyph_row
*old_rows
;
2800 /* Rows to assign to. */
2801 struct glyph_row
*new_rows
= MATRIX_ROW (matrix
, unchanged_at_top
);
2805 /* Make a copy of the original rows. */
2806 old_rows
= alloca (nlines
* sizeof *old_rows
);
2807 memcpy (old_rows
, new_rows
, nlines
* sizeof *old_rows
);
2809 /* Assign new rows, maybe clear lines. */
2810 for (i
= 0; i
< nlines
; ++i
)
2812 int enabled_before_p
= new_rows
[i
].enabled_p
;
2814 eassert (i
+ unchanged_at_top
< matrix
->nrows
);
2815 eassert (unchanged_at_top
+ copy_from
[i
] < matrix
->nrows
);
2816 new_rows
[i
] = old_rows
[copy_from
[i
]];
2817 new_rows
[i
].enabled_p
= enabled_before_p
;
2819 /* RETAINED_P is zero for empty lines. */
2820 if (!retained_p
[copy_from
[i
]])
2821 new_rows
[i
].enabled_p
= 0;
2824 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2825 if (frame_matrix_frame
)
2826 mirror_line_dance (XWINDOW (FVAR (frame_matrix_frame
, root_window
)),
2827 unchanged_at_top
, nlines
, copy_from
, retained_p
);
2831 /* Synchronize glyph pointers in the current matrix of window W with
2832 the current frame matrix. */
2835 sync_window_with_frame_matrix_rows (struct window
*w
)
2837 struct frame
*f
= XFRAME (WVAR (w
, frame
));
2838 struct glyph_row
*window_row
, *window_row_end
, *frame_row
;
2839 int left
, right
, x
, width
;
2841 /* Preconditions: W must be a leaf window on a tty frame. */
2842 eassert (NILP (WVAR (w
, hchild
)) && NILP (WVAR (w
, vchild
)));
2843 eassert (!FRAME_WINDOW_P (f
));
2845 left
= margin_glyphs_to_reserve (w
, 1, WVAR (w
, left_margin_cols
));
2846 right
= margin_glyphs_to_reserve (w
, 1, WVAR (w
, right_margin_cols
));
2847 x
= w
->current_matrix
->matrix_x
;
2848 width
= w
->current_matrix
->matrix_w
;
2850 window_row
= w
->current_matrix
->rows
;
2851 window_row_end
= window_row
+ w
->current_matrix
->nrows
;
2852 frame_row
= f
->current_matrix
->rows
+ WINDOW_TOP_EDGE_LINE (w
);
2854 for (; window_row
< window_row_end
; ++window_row
, ++frame_row
)
2856 window_row
->glyphs
[LEFT_MARGIN_AREA
]
2857 = frame_row
->glyphs
[0] + x
;
2858 window_row
->glyphs
[TEXT_AREA
]
2859 = window_row
->glyphs
[LEFT_MARGIN_AREA
] + left
;
2860 window_row
->glyphs
[LAST_AREA
]
2861 = window_row
->glyphs
[LEFT_MARGIN_AREA
] + width
;
2862 window_row
->glyphs
[RIGHT_MARGIN_AREA
]
2863 = window_row
->glyphs
[LAST_AREA
] - right
;
2868 /* Return the window in the window tree rooted in W containing frame
2869 row ROW. Value is null if none is found. */
2871 static struct window
*
2872 frame_row_to_window (struct window
*w
, int row
)
2874 struct window
*found
= NULL
;
2878 if (!NILP (WVAR (w
, hchild
)))
2879 found
= frame_row_to_window (XWINDOW (WVAR (w
, hchild
)), row
);
2880 else if (!NILP (WVAR (w
, vchild
)))
2881 found
= frame_row_to_window (XWINDOW (WVAR (w
, vchild
)), row
);
2882 else if (row
>= WINDOW_TOP_EDGE_LINE (w
)
2883 && row
< WINDOW_BOTTOM_EDGE_LINE (w
))
2886 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
2893 /* Perform a line dance in the window tree rooted at W, after
2894 scrolling a frame matrix in mirrored_line_dance.
2896 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2897 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2898 COPY_FROM is a vector containing, for each row I in the range 0 <=
2899 I < NLINES, the index of the original line to move to I. This
2900 index is relative to the row range, i.e. 0 <= index < NLINES.
2901 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2905 mirror_line_dance (struct window
*w
, int unchanged_at_top
, int nlines
, int *copy_from
, char *retained_p
)
2909 if (!NILP (WVAR (w
, hchild
)))
2910 mirror_line_dance (XWINDOW (WVAR (w
, hchild
)), unchanged_at_top
,
2911 nlines
, copy_from
, retained_p
);
2912 else if (!NILP (WVAR (w
, vchild
)))
2913 mirror_line_dance (XWINDOW (WVAR (w
, vchild
)), unchanged_at_top
,
2914 nlines
, copy_from
, retained_p
);
2917 /* W is a leaf window, and we are working on its current
2919 struct glyph_matrix
*m
= w
->current_matrix
;
2921 struct glyph_row
*old_rows
;
2923 /* Make a copy of the original rows of matrix m. */
2924 old_rows
= alloca (m
->nrows
* sizeof *old_rows
);
2925 memcpy (old_rows
, m
->rows
, m
->nrows
* sizeof *old_rows
);
2927 for (i
= 0; i
< nlines
; ++i
)
2929 /* Frame relative line assigned to. */
2930 int frame_to
= i
+ unchanged_at_top
;
2932 /* Frame relative line assigned. */
2933 int frame_from
= copy_from
[i
] + unchanged_at_top
;
2935 /* Window relative line assigned to. */
2936 int window_to
= frame_to
- m
->matrix_y
;
2938 /* Window relative line assigned. */
2939 int window_from
= frame_from
- m
->matrix_y
;
2941 /* Is assigned line inside window? */
2942 int from_inside_window_p
2943 = window_from
>= 0 && window_from
< m
->matrix_h
;
2945 /* Is assigned to line inside window? */
2946 int to_inside_window_p
2947 = window_to
>= 0 && window_to
< m
->matrix_h
;
2949 if (from_inside_window_p
&& to_inside_window_p
)
2951 /* Enabled setting before assignment. */
2952 int enabled_before_p
;
2954 /* Do the assignment. The enabled_p flag is saved
2955 over the assignment because the old redisplay did
2957 enabled_before_p
= m
->rows
[window_to
].enabled_p
;
2958 m
->rows
[window_to
] = old_rows
[window_from
];
2959 m
->rows
[window_to
].enabled_p
= enabled_before_p
;
2961 /* If frame line is empty, window line is empty, too. */
2962 if (!retained_p
[copy_from
[i
]])
2963 m
->rows
[window_to
].enabled_p
= 0;
2965 else if (to_inside_window_p
)
2967 /* A copy between windows. This is an infrequent
2968 case not worth optimizing. */
2969 struct frame
*f
= XFRAME (WVAR (w
, frame
));
2970 struct window
*root
= XWINDOW (FRAME_ROOT_WINDOW (f
));
2972 struct glyph_matrix
*m2
;
2975 w2
= frame_row_to_window (root
, frame_from
);
2976 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2977 -nw', FROM_FRAME sometimes has no associated window.
2978 This check avoids a segfault if W2 is null. */
2981 m2
= w2
->current_matrix
;
2982 m2_from
= frame_from
- m2
->matrix_y
;
2983 copy_row_except_pointers (m
->rows
+ window_to
,
2984 m2
->rows
+ m2_from
);
2986 /* If frame line is empty, window line is empty, too. */
2987 if (!retained_p
[copy_from
[i
]])
2988 m
->rows
[window_to
].enabled_p
= 0;
2992 else if (from_inside_window_p
)
2996 /* If there was a copy between windows, make sure glyph
2997 pointers are in sync with the frame matrix. */
2999 sync_window_with_frame_matrix_rows (w
);
3001 /* Check that no pointers are lost. */
3005 /* Next window on same level. */
3006 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
3013 /* Check that window and frame matrices agree about their
3014 understanding where glyphs of the rows are to find. For each
3015 window in the window tree rooted at W, check that rows in the
3016 matrices of leaf window agree with their frame matrices about
3020 check_window_matrix_pointers (struct window
*w
)
3024 if (!NILP (WVAR (w
, hchild
)))
3025 check_window_matrix_pointers (XWINDOW (WVAR (w
, hchild
)));
3026 else if (!NILP (WVAR (w
, vchild
)))
3027 check_window_matrix_pointers (XWINDOW (WVAR (w
, vchild
)));
3030 struct frame
*f
= XFRAME (WVAR (w
, frame
));
3031 check_matrix_pointers (w
->desired_matrix
, f
->desired_matrix
);
3032 check_matrix_pointers (w
->current_matrix
, f
->current_matrix
);
3035 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
3040 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
3041 a window and FRAME_MATRIX is the corresponding frame matrix. For
3042 each row in WINDOW_MATRIX check that it's a slice of the
3043 corresponding frame row. If it isn't, abort. */
3046 check_matrix_pointers (struct glyph_matrix
*window_matrix
,
3047 struct glyph_matrix
*frame_matrix
)
3049 /* Row number in WINDOW_MATRIX. */
3052 /* Row number corresponding to I in FRAME_MATRIX. */
3053 int j
= window_matrix
->matrix_y
;
3055 /* For all rows check that the row in the window matrix is a
3056 slice of the row in the frame matrix. If it isn't we didn't
3057 mirror an operation on the frame matrix correctly. */
3058 while (i
< window_matrix
->nrows
)
3060 if (!glyph_row_slice_p (window_matrix
->rows
+ i
,
3061 frame_matrix
->rows
+ j
))
3067 #endif /* GLYPH_DEBUG */
3071 /**********************************************************************
3072 VPOS and HPOS translations
3073 **********************************************************************/
3077 /* Translate vertical position VPOS which is relative to window W to a
3078 vertical position relative to W's frame. */
3081 window_to_frame_vpos (struct window
*w
, int vpos
)
3083 eassert (!FRAME_WINDOW_P (XFRAME (WVAR (w
, frame
))));
3084 eassert (vpos
>= 0 && vpos
<= w
->desired_matrix
->nrows
);
3085 vpos
+= WINDOW_TOP_EDGE_LINE (w
);
3086 eassert (vpos
>= 0 && vpos
<= FRAME_LINES (XFRAME (WVAR (w
, frame
))));
3091 /* Translate horizontal position HPOS which is relative to window W to
3092 a horizontal position relative to W's frame. */
3095 window_to_frame_hpos (struct window
*w
, int hpos
)
3097 eassert (!FRAME_WINDOW_P (XFRAME (WVAR (w
, frame
))));
3098 hpos
+= WINDOW_LEFT_EDGE_COL (w
);
3102 #endif /* GLYPH_DEBUG */
3106 /**********************************************************************
3108 **********************************************************************/
3110 DEFUN ("redraw-frame", Fredraw_frame
, Sredraw_frame
, 1, 1, 0,
3111 doc
: /* Clear frame FRAME and output again what is supposed to appear on it. */)
3116 CHECK_LIVE_FRAME (frame
);
3119 /* Ignore redraw requests, if frame has no glyphs yet.
3120 (Implementation note: It still has to be checked why we are
3121 called so early here). */
3122 if (!glyphs_initialized_initially_p
)
3127 if (FRAME_MSDOS_P (f
))
3128 FRAME_TERMINAL (f
)->set_terminal_modes_hook (FRAME_TERMINAL (f
));
3131 clear_current_matrices (f
);
3133 if (FRAME_TERMCAP_P (f
))
3134 fflush (FRAME_TTY (f
)->output
);
3135 windows_or_buffers_changed
++;
3136 /* Mark all windows as inaccurate, so that every window will have
3137 its redisplay done. */
3138 mark_window_display_accurate (FRAME_ROOT_WINDOW (f
), 0);
3139 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f
)), 1);
3145 /* Redraw frame F. This is nothing more than a call to the Lisp
3146 function redraw-frame. */
3149 redraw_frame (struct frame
*f
)
3152 XSETFRAME (frame
, f
);
3153 Fredraw_frame (frame
);
3157 DEFUN ("redraw-display", Fredraw_display
, Sredraw_display
, 0, 0, "",
3158 doc
: /* Clear and redisplay all visible frames. */)
3161 Lisp_Object tail
, frame
;
3163 FOR_EACH_FRAME (tail
, frame
)
3164 if (FRAME_VISIBLE_P (XFRAME (frame
)))
3165 Fredraw_frame (frame
);
3172 /***********************************************************************
3174 ***********************************************************************/
3176 /* Update frame F based on the data in desired matrices.
3178 If FORCE_P is non-zero, don't let redisplay be stopped by detecting
3179 pending input. If INHIBIT_HAIRY_ID_P is non-zero, don't try
3182 Value is non-zero if redisplay was stopped due to pending input. */
3185 update_frame (struct frame
*f
, int force_p
, int inhibit_hairy_id_p
)
3187 /* 1 means display has been paused because of pending input. */
3189 struct window
*root_window
= XWINDOW (FVAR (f
, root_window
));
3191 if (redisplay_dont_pause
)
3193 else if (NILP (Vredisplay_preemption_period
))
3195 else if (!force_p
&& NUMBERP (Vredisplay_preemption_period
))
3197 double p
= XFLOATINT (Vredisplay_preemption_period
);
3199 if (detect_input_pending_ignore_squeezables ())
3205 preemption_period
= EMACS_TIME_FROM_DOUBLE (p
);
3206 preemption_next_check
= add_emacs_time (current_emacs_time (),
3210 if (FRAME_WINDOW_P (f
))
3212 /* We are working on window matrix basis. All windows whose
3213 flag must_be_updated_p is set have to be updated. */
3215 /* Record that we are not working on frame matrices. */
3216 set_frame_matrix_frame (NULL
);
3218 /* Update all windows in the window tree of F, maybe stopping
3219 when pending input is detected. */
3222 /* Update the menu bar on X frames that don't have toolkit
3224 if (WINDOWP (FVAR (f
, menu_bar_window
)))
3225 update_window (XWINDOW (FVAR (f
, menu_bar_window
)), 1);
3227 /* Update the tool-bar window, if present. */
3228 if (WINDOWP (FVAR (f
, tool_bar_window
)))
3230 struct window
*w
= XWINDOW (FVAR (f
, tool_bar_window
));
3232 /* Update tool-bar window. */
3233 if (w
->must_be_updated_p
)
3237 update_window (w
, 1);
3238 w
->must_be_updated_p
= 0;
3240 /* Swap tool-bar strings. We swap because we want to
3242 tem
= FVAR (f
, current_tool_bar_string
);
3243 FVAR (f
, current_tool_bar_string
) = FVAR (f
,
3244 desired_tool_bar_string
);
3245 FVAR (f
, desired_tool_bar_string
) = tem
;
3250 /* Update windows. */
3251 paused_p
= update_window_tree (root_window
, force_p
);
3254 /* This flush is a performance bottleneck under X,
3255 and it doesn't seem to be necessary anyway (in general).
3256 It is necessary when resizing the window with the mouse, or
3257 at least the fringes are not redrawn in a timely manner. ++kfs */
3258 if (f
->force_flush_display_p
)
3260 FRAME_RIF (f
)->flush_display (f
);
3261 f
->force_flush_display_p
= 0;
3266 /* We are working on frame matrix basis. Set the frame on whose
3267 frame matrix we operate. */
3268 set_frame_matrix_frame (f
);
3270 /* Build F's desired matrix from window matrices. */
3271 build_frame_matrix (f
);
3273 /* Update the display */
3275 paused_p
= update_frame_1 (f
, force_p
, inhibit_hairy_id_p
);
3278 if (FRAME_TERMCAP_P (f
) || FRAME_MSDOS_P (f
))
3280 if (FRAME_TTY (f
)->termscript
)
3281 fflush (FRAME_TTY (f
)->termscript
);
3282 if (FRAME_TERMCAP_P (f
))
3283 fflush (FRAME_TTY (f
)->output
);
3286 /* Check window matrices for lost pointers. */
3288 check_window_matrix_pointers (root_window
);
3289 add_frame_display_history (f
, paused_p
);
3294 /* Reset flags indicating that a window should be updated. */
3295 set_window_update_flags (root_window
, 0);
3297 display_completed
= !paused_p
;
3303 /************************************************************************
3304 Window-based updates
3305 ************************************************************************/
3307 /* Perform updates in window tree rooted at W. FORCE_P non-zero means
3308 don't stop updating when input is pending. */
3311 update_window_tree (struct window
*w
, int force_p
)
3315 while (w
&& !paused_p
)
3317 if (!NILP (WVAR (w
, hchild
)))
3318 paused_p
|= update_window_tree (XWINDOW (WVAR (w
, hchild
)), force_p
);
3319 else if (!NILP (WVAR (w
, vchild
)))
3320 paused_p
|= update_window_tree (XWINDOW (WVAR (w
, vchild
)), force_p
);
3321 else if (w
->must_be_updated_p
)
3322 paused_p
|= update_window (w
, force_p
);
3324 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
3331 /* Update window W if its flag must_be_updated_p is non-zero. If
3332 FORCE_P is non-zero, don't stop updating if input is pending. */
3335 update_single_window (struct window
*w
, int force_p
)
3337 if (w
->must_be_updated_p
)
3339 struct frame
*f
= XFRAME (WINDOW_FRAME (w
));
3341 /* Record that this is not a frame-based redisplay. */
3342 set_frame_matrix_frame (NULL
);
3344 if (redisplay_dont_pause
)
3346 else if (NILP (Vredisplay_preemption_period
))
3348 else if (!force_p
&& NUMBERP (Vredisplay_preemption_period
))
3350 double p
= XFLOATINT (Vredisplay_preemption_period
);
3351 preemption_period
= EMACS_TIME_FROM_DOUBLE (p
);
3352 preemption_next_check
= add_emacs_time (current_emacs_time (),
3358 update_window (w
, force_p
);
3361 /* Reset flag in W. */
3362 w
->must_be_updated_p
= 0;
3366 #ifdef HAVE_WINDOW_SYSTEM
3368 /* Redraw lines from the current matrix of window W that are
3369 overlapped by other rows. YB is bottom-most y-position in W. */
3372 redraw_overlapped_rows (struct window
*w
, int yb
)
3375 struct frame
*f
= XFRAME (WINDOW_FRAME (w
));
3377 /* If rows overlapping others have been changed, the rows being
3378 overlapped have to be redrawn. This won't draw lines that have
3379 already been drawn in update_window_line because overlapped_p in
3380 desired rows is 0, so after row assignment overlapped_p in
3381 current rows is 0. */
3382 for (i
= 0; i
< w
->current_matrix
->nrows
; ++i
)
3384 struct glyph_row
*row
= w
->current_matrix
->rows
+ i
;
3386 if (!row
->enabled_p
)
3388 else if (row
->mode_line_p
)
3391 if (row
->overlapped_p
)
3393 enum glyph_row_area area
;
3395 for (area
= LEFT_MARGIN_AREA
; area
< LAST_AREA
; ++area
)
3398 updated_area
= area
;
3399 FRAME_RIF (f
)->cursor_to (i
, 0, row
->y
,
3400 area
== TEXT_AREA
? row
->x
: 0);
3401 if (row
->used
[area
])
3402 FRAME_RIF (f
)->write_glyphs (row
->glyphs
[area
],
3404 FRAME_RIF (f
)->clear_end_of_line (-1);
3407 row
->overlapped_p
= 0;
3410 if (MATRIX_ROW_BOTTOM_Y (row
) >= yb
)
3416 /* Redraw lines from the current matrix of window W that overlap
3417 others. YB is bottom-most y-position in W. */
3420 redraw_overlapping_rows (struct window
*w
, int yb
)
3423 struct glyph_row
*row
;
3424 struct redisplay_interface
*rif
= FRAME_RIF (XFRAME (WINDOW_FRAME (w
)));
3426 for (i
= 0; i
< w
->current_matrix
->nrows
; ++i
)
3428 row
= w
->current_matrix
->rows
+ i
;
3430 if (!row
->enabled_p
)
3432 else if (row
->mode_line_p
)
3435 bottom_y
= MATRIX_ROW_BOTTOM_Y (row
);
3437 if (row
->overlapping_p
)
3441 if (MATRIX_ROW_OVERLAPS_PRED_P (row
) && i
> 0
3442 && !MATRIX_ROW (w
->current_matrix
, i
- 1)->overlapped_p
)
3443 overlaps
|= OVERLAPS_PRED
;
3444 if (MATRIX_ROW_OVERLAPS_SUCC_P (row
) && bottom_y
< yb
3445 && !MATRIX_ROW (w
->current_matrix
, i
+ 1)->overlapped_p
)
3446 overlaps
|= OVERLAPS_SUCC
;
3450 if (row
->used
[LEFT_MARGIN_AREA
])
3451 rif
->fix_overlapping_area (w
, row
, LEFT_MARGIN_AREA
, overlaps
);
3453 if (row
->used
[TEXT_AREA
])
3454 rif
->fix_overlapping_area (w
, row
, TEXT_AREA
, overlaps
);
3456 if (row
->used
[RIGHT_MARGIN_AREA
])
3457 rif
->fix_overlapping_area (w
, row
, RIGHT_MARGIN_AREA
, overlaps
);
3459 /* Record in neighbor rows that ROW overwrites part of
3461 if (overlaps
& OVERLAPS_PRED
)
3462 MATRIX_ROW (w
->current_matrix
, i
- 1)->overlapped_p
= 1;
3463 if (overlaps
& OVERLAPS_SUCC
)
3464 MATRIX_ROW (w
->current_matrix
, i
+ 1)->overlapped_p
= 1;
3473 #endif /* HAVE_WINDOW_SYSTEM */
3476 #if defined GLYPH_DEBUG && 0
3478 /* Check that no row in the current matrix of window W is enabled
3479 which is below what's displayed in the window. */
3482 check_current_matrix_flags (struct window
*w
)
3484 int last_seen_p
= 0;
3485 int i
, yb
= window_text_bottom_y (w
);
3487 for (i
= 0; i
< w
->current_matrix
->nrows
- 1; ++i
)
3489 struct glyph_row
*row
= MATRIX_ROW (w
->current_matrix
, i
);
3490 if (!last_seen_p
&& MATRIX_ROW_BOTTOM_Y (row
) >= yb
)
3492 else if (last_seen_p
&& row
->enabled_p
)
3497 #endif /* GLYPH_DEBUG */
3500 /* Update display of window W. FORCE_P non-zero means that we should
3501 not stop when detecting pending input. */
3504 update_window (struct window
*w
, int force_p
)
3506 struct glyph_matrix
*desired_matrix
= w
->desired_matrix
;
3508 #if !PERIODIC_PREEMPTION_CHECKING
3509 int preempt_count
= baud_rate
/ 2400 + 1;
3511 struct redisplay_interface
*rif
= FRAME_RIF (XFRAME (WINDOW_FRAME (w
)));
3513 /* Check that W's frame doesn't have glyph matrices. */
3514 eassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w
))));
3517 /* Check pending input the first time so that we can quickly return. */
3518 #if !PERIODIC_PREEMPTION_CHECKING
3520 detect_input_pending_ignore_squeezables ();
3523 /* If forced to complete the update, or if no input is pending, do
3525 if (force_p
|| !input_pending
|| !NILP (do_mouse_tracking
))
3527 struct glyph_row
*row
, *end
;
3528 struct glyph_row
*mode_line_row
;
3529 struct glyph_row
*header_line_row
;
3530 int yb
, changed_p
= 0, mouse_face_overwritten_p
= 0;
3531 #if ! PERIODIC_PREEMPTION_CHECKING
3535 rif
->update_window_begin_hook (w
);
3536 yb
= window_text_bottom_y (w
);
3537 row
= desired_matrix
->rows
;
3538 end
= row
+ desired_matrix
->nrows
- 1;
3540 /* Take note of the header line, if there is one. We will
3541 update it below, after updating all of the window's lines. */
3542 if (row
->mode_line_p
)
3544 header_line_row
= row
;
3548 header_line_row
= NULL
;
3550 /* Update the mode line, if necessary. */
3551 mode_line_row
= MATRIX_MODE_LINE_ROW (desired_matrix
);
3552 if (mode_line_row
->mode_line_p
&& mode_line_row
->enabled_p
)
3554 mode_line_row
->y
= yb
;
3555 update_window_line (w
, MATRIX_ROW_VPOS (mode_line_row
,
3557 &mouse_face_overwritten_p
);
3560 /* Find first enabled row. Optimizations in redisplay_internal
3561 may lead to an update with only one row enabled. There may
3562 be also completely empty matrices. */
3563 while (row
< end
&& !row
->enabled_p
)
3566 /* Try reusing part of the display by copying. */
3567 if (row
< end
&& !desired_matrix
->no_scrolling_p
)
3569 int rc
= scrolling_window (w
, header_line_row
!= NULL
);
3572 /* All rows were found to be equal. */
3578 /* We've scrolled the display. */
3584 /* Update the rest of the lines. */
3585 for (; row
< end
&& (force_p
|| !input_pending
); ++row
)
3586 /* scrolling_window resets the enabled_p flag of the rows it
3587 reuses from current_matrix. */
3590 int vpos
= MATRIX_ROW_VPOS (row
, desired_matrix
);
3593 /* We'll have to play a little bit with when to
3594 detect_input_pending. If it's done too often,
3595 scrolling large windows with repeated scroll-up
3596 commands will too quickly pause redisplay. */
3597 #if PERIODIC_PREEMPTION_CHECKING
3600 EMACS_TIME tm
= current_emacs_time ();
3601 if (EMACS_TIME_LT (preemption_next_check
, tm
))
3603 preemption_next_check
= add_emacs_time (tm
,
3605 if (detect_input_pending_ignore_squeezables ())
3610 if (!force_p
&& ++n_updated
% preempt_count
== 0)
3611 detect_input_pending_ignore_squeezables ();
3613 changed_p
|= update_window_line (w
, vpos
,
3614 &mouse_face_overwritten_p
);
3616 /* Mark all rows below the last visible one in the current
3617 matrix as invalid. This is necessary because of
3618 variable line heights. Consider the case of three
3619 successive redisplays, where the first displays 5
3620 lines, the second 3 lines, and the third 5 lines again.
3621 If the second redisplay wouldn't mark rows in the
3622 current matrix invalid, the third redisplay might be
3623 tempted to optimize redisplay based on lines displayed
3624 in the first redisplay. */
3625 if (MATRIX_ROW_BOTTOM_Y (row
) >= yb
)
3626 for (i
= vpos
+ 1; i
< w
->current_matrix
->nrows
- 1; ++i
)
3627 MATRIX_ROW (w
->current_matrix
, i
)->enabled_p
= 0;
3630 /* Was display preempted? */
3631 paused_p
= row
< end
;
3635 /* Update the header line after scrolling because a new header
3636 line would otherwise overwrite lines at the top of the window
3637 that can be scrolled. */
3638 if (header_line_row
&& header_line_row
->enabled_p
)
3640 header_line_row
->y
= 0;
3641 update_window_line (w
, 0, &mouse_face_overwritten_p
);
3644 /* Fix the appearance of overlapping/overlapped rows. */
3645 if (!paused_p
&& !w
->pseudo_window_p
)
3647 #ifdef HAVE_WINDOW_SYSTEM
3648 if (changed_p
&& rif
->fix_overlapping_area
)
3650 redraw_overlapped_rows (w
, yb
);
3651 redraw_overlapping_rows (w
, yb
);
3655 /* Make cursor visible at cursor position of W. */
3656 set_window_cursor_after_update (w
);
3658 #if 0 /* Check that current matrix invariants are satisfied. This is
3659 for debugging only. See the comment of check_matrix_invariants. */
3660 IF_DEBUG (check_matrix_invariants (w
));
3665 /* Remember the redisplay method used to display the matrix. */
3666 strcpy (w
->current_matrix
->method
, w
->desired_matrix
->method
);
3669 #ifdef HAVE_WINDOW_SYSTEM
3670 update_window_fringes (w
, 0);
3673 /* End the update of window W. Don't set the cursor if we
3674 paused updating the display because in this case,
3675 set_window_cursor_after_update hasn't been called, and
3676 output_cursor doesn't contain the cursor location. */
3677 rif
->update_window_end_hook (w
, !paused_p
, mouse_face_overwritten_p
);
3683 /* check_current_matrix_flags (w); */
3684 add_window_display_history (w
, w
->current_matrix
->method
, paused_p
);
3687 clear_glyph_matrix (desired_matrix
);
3693 /* Update the display of area AREA in window W, row number VPOS.
3694 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3697 update_marginal_area (struct window
*w
, int area
, int vpos
)
3699 struct glyph_row
*desired_row
= MATRIX_ROW (w
->desired_matrix
, vpos
);
3700 struct redisplay_interface
*rif
= FRAME_RIF (XFRAME (WINDOW_FRAME (w
)));
3702 /* Let functions in xterm.c know what area subsequent X positions
3703 will be relative to. */
3704 updated_area
= area
;
3706 /* Set cursor to start of glyphs, write them, and clear to the end
3707 of the area. I don't think that something more sophisticated is
3708 necessary here, since marginal areas will not be the default. */
3709 rif
->cursor_to (vpos
, 0, desired_row
->y
, 0);
3710 if (desired_row
->used
[area
])
3711 rif
->write_glyphs (desired_row
->glyphs
[area
], desired_row
->used
[area
]);
3712 rif
->clear_end_of_line (-1);
3716 /* Update the display of the text area of row VPOS in window W.
3717 Value is non-zero if display has changed. */
3720 update_text_area (struct window
*w
, int vpos
)
3722 struct glyph_row
*current_row
= MATRIX_ROW (w
->current_matrix
, vpos
);
3723 struct glyph_row
*desired_row
= MATRIX_ROW (w
->desired_matrix
, vpos
);
3724 struct redisplay_interface
*rif
= FRAME_RIF (XFRAME (WINDOW_FRAME (w
)));
3727 /* Let functions in xterm.c know what area subsequent X positions
3728 will be relative to. */
3729 updated_area
= TEXT_AREA
;
3731 /* If rows are at different X or Y, or rows have different height,
3732 or the current row is marked invalid, write the entire line. */
3733 if (!current_row
->enabled_p
3734 || desired_row
->y
!= current_row
->y
3735 || desired_row
->ascent
!= current_row
->ascent
3736 || desired_row
->phys_ascent
!= current_row
->phys_ascent
3737 || desired_row
->phys_height
!= current_row
->phys_height
3738 || desired_row
->visible_height
!= current_row
->visible_height
3739 || current_row
->overlapped_p
3740 /* This next line is necessary for correctly redrawing
3741 mouse-face areas after scrolling and other operations.
3742 However, it causes excessive flickering when mouse is moved
3743 across the mode line. Luckily, turning it off for the mode
3744 line doesn't seem to hurt anything. -- cyd.
3745 But it is still needed for the header line. -- kfs. */
3746 || (current_row
->mouse_face_p
3747 && !(current_row
->mode_line_p
&& vpos
> 0))
3748 || current_row
->x
!= desired_row
->x
)
3750 rif
->cursor_to (vpos
, 0, desired_row
->y
, desired_row
->x
);
3752 if (desired_row
->used
[TEXT_AREA
])
3753 rif
->write_glyphs (desired_row
->glyphs
[TEXT_AREA
],
3754 desired_row
->used
[TEXT_AREA
]);
3756 /* Clear to end of window. */
3757 rif
->clear_end_of_line (-1);
3760 /* This erases the cursor. We do this here because
3761 notice_overwritten_cursor cannot easily check this, which
3762 might indicate that the whole functionality of
3763 notice_overwritten_cursor would better be implemented here.
3764 On the other hand, we need notice_overwritten_cursor as long
3765 as mouse highlighting is done asynchronously outside of
3767 if (vpos
== w
->phys_cursor
.vpos
)
3768 w
->phys_cursor_on_p
= 0;
3773 struct glyph
*current_glyph
= current_row
->glyphs
[TEXT_AREA
];
3774 struct glyph
*desired_glyph
= desired_row
->glyphs
[TEXT_AREA
];
3775 int overlapping_glyphs_p
= current_row
->contains_overlapping_glyphs_p
;
3776 int desired_stop_pos
= desired_row
->used
[TEXT_AREA
];
3777 int abort_skipping
= 0;
3779 /* If the desired row extends its face to the text area end, and
3780 unless the current row also does so at the same position,
3781 make sure we write at least one glyph, so that the face
3782 extension actually takes place. */
3783 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row
)
3784 && (desired_stop_pos
< current_row
->used
[TEXT_AREA
]
3785 || (desired_stop_pos
== current_row
->used
[TEXT_AREA
]
3786 && !MATRIX_ROW_EXTENDS_FACE_P (current_row
))))
3789 stop
= min (current_row
->used
[TEXT_AREA
], desired_stop_pos
);
3793 /* Loop over glyphs that current and desired row may have
3797 int can_skip_p
= !abort_skipping
;
3799 /* Skip over glyphs that both rows have in common. These
3800 don't have to be written. We can't skip if the last
3801 current glyph overlaps the glyph to its right. For
3802 example, consider a current row of `if ' with the `f' in
3803 Courier bold so that it overlaps the ` ' to its right.
3804 If the desired row is ` ', we would skip over the space
3805 after the `if' and there would remain a pixel from the
3806 `f' on the screen. */
3807 if (overlapping_glyphs_p
&& i
> 0)
3809 struct glyph
*glyph
= ¤t_row
->glyphs
[TEXT_AREA
][i
- 1];
3812 rif
->get_glyph_overhangs (glyph
, XFRAME (WVAR (w
, frame
)),
3814 can_skip_p
= (right
== 0 && !abort_skipping
);
3822 && GLYPH_EQUAL_P (desired_glyph
, current_glyph
))
3824 x
+= desired_glyph
->pixel_width
;
3825 ++desired_glyph
, ++current_glyph
, ++i
;
3828 /* Consider the case that the current row contains "xxx
3829 ppp ggg" in italic Courier font, and the desired row
3830 is "xxx ggg". The character `p' has lbearing, `g'
3831 has not. The loop above will stop in front of the
3832 first `p' in the current row. If we would start
3833 writing glyphs there, we wouldn't erase the lbearing
3834 of the `p'. The rest of the lbearing problem is then
3835 taken care of by draw_glyphs. */
3836 if (overlapping_glyphs_p
3838 && i
< current_row
->used
[TEXT_AREA
]
3839 && (current_row
->used
[TEXT_AREA
]
3840 != desired_row
->used
[TEXT_AREA
]))
3844 rif
->get_glyph_overhangs (current_glyph
,
3845 XFRAME (WVAR (w
, frame
)),
3847 while (left
> 0 && i
> 0)
3849 --i
, --desired_glyph
, --current_glyph
;
3850 x
-= desired_glyph
->pixel_width
;
3851 left
-= desired_glyph
->pixel_width
;
3854 /* Abort the skipping algorithm if we end up before
3855 our starting point, to avoid looping (bug#1070).
3856 This can happen when the lbearing is larger than
3858 abort_skipping
= (i
< start_hpos
);
3862 /* Try to avoid writing the entire rest of the desired row
3863 by looking for a resync point. This mainly prevents
3864 mode line flickering in the case the mode line is in
3865 fixed-pitch font, which it usually will be. */
3866 if (i
< desired_row
->used
[TEXT_AREA
])
3868 int start_x
= x
, start_hpos
= i
;
3869 struct glyph
*start
= desired_glyph
;
3871 int skip_first_p
= !can_skip_p
;
3873 /* Find the next glyph that's equal again. */
3876 || !GLYPH_EQUAL_P (desired_glyph
, current_glyph
))
3879 x
+= desired_glyph
->pixel_width
;
3880 current_x
+= current_glyph
->pixel_width
;
3881 ++desired_glyph
, ++current_glyph
, ++i
;
3885 if (i
== start_hpos
|| x
!= current_x
)
3889 desired_glyph
= start
;
3893 rif
->cursor_to (vpos
, start_hpos
, desired_row
->y
, start_x
);
3894 rif
->write_glyphs (start
, i
- start_hpos
);
3899 /* Write the rest. */
3900 if (i
< desired_row
->used
[TEXT_AREA
])
3902 rif
->cursor_to (vpos
, i
, desired_row
->y
, x
);
3903 rif
->write_glyphs (desired_glyph
, desired_row
->used
[TEXT_AREA
] - i
);
3907 /* Maybe clear to end of line. */
3908 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row
))
3910 /* If new row extends to the end of the text area, nothing
3911 has to be cleared, if and only if we did a write_glyphs
3912 above. This is made sure by setting desired_stop_pos
3913 appropriately above. */
3914 eassert (i
< desired_row
->used
[TEXT_AREA
]
3915 || ((desired_row
->used
[TEXT_AREA
]
3916 == current_row
->used
[TEXT_AREA
])
3917 && MATRIX_ROW_EXTENDS_FACE_P (current_row
)));
3919 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row
))
3921 /* If old row extends to the end of the text area, clear. */
3922 if (i
>= desired_row
->used
[TEXT_AREA
])
3923 rif
->cursor_to (vpos
, i
, desired_row
->y
,
3924 desired_row
->pixel_width
);
3925 rif
->clear_end_of_line (-1);
3928 else if (desired_row
->pixel_width
< current_row
->pixel_width
)
3930 /* Otherwise clear to the end of the old row. Everything
3931 after that position should be clear already. */
3934 if (i
>= desired_row
->used
[TEXT_AREA
])
3935 rif
->cursor_to (vpos
, i
, desired_row
->y
,
3936 desired_row
->pixel_width
);
3938 /* If cursor is displayed at the end of the line, make sure
3939 it's cleared. Nowadays we don't have a phys_cursor_glyph
3940 with which to erase the cursor (because this method
3941 doesn't work with lbearing/rbearing), so we must do it
3943 if (vpos
== w
->phys_cursor
.vpos
3944 && (desired_row
->reversed_p
3945 ? (w
->phys_cursor
.hpos
< 0)
3946 : (w
->phys_cursor
.hpos
>= desired_row
->used
[TEXT_AREA
])))
3948 w
->phys_cursor_on_p
= 0;
3952 xlim
= current_row
->pixel_width
;
3953 rif
->clear_end_of_line (xlim
);
3962 /* Update row VPOS in window W. Value is non-zero if display has been
3966 update_window_line (struct window
*w
, int vpos
, int *mouse_face_overwritten_p
)
3968 struct glyph_row
*current_row
= MATRIX_ROW (w
->current_matrix
, vpos
);
3969 struct glyph_row
*desired_row
= MATRIX_ROW (w
->desired_matrix
, vpos
);
3970 struct redisplay_interface
*rif
= FRAME_RIF (XFRAME (WINDOW_FRAME (w
)));
3973 /* Set the row being updated. This is important to let xterm.c
3974 know what line height values are in effect. */
3975 updated_row
= desired_row
;
3977 /* A row can be completely invisible in case a desired matrix was
3978 built with a vscroll and then make_cursor_line_fully_visible shifts
3979 the matrix. Make sure to make such rows current anyway, since
3980 we need the correct y-position, for example, in the current matrix. */
3981 if (desired_row
->mode_line_p
3982 || desired_row
->visible_height
> 0)
3984 eassert (desired_row
->enabled_p
);
3986 /* Update display of the left margin area, if there is one. */
3987 if (!desired_row
->full_width_p
3988 && !NILP (WVAR (w
, left_margin_cols
)))
3991 update_marginal_area (w
, LEFT_MARGIN_AREA
, vpos
);
3994 /* Update the display of the text area. */
3995 if (update_text_area (w
, vpos
))
3998 if (current_row
->mouse_face_p
)
3999 *mouse_face_overwritten_p
= 1;
4002 /* Update display of the right margin area, if there is one. */
4003 if (!desired_row
->full_width_p
4004 && !NILP (WVAR (w
, right_margin_cols
)))
4007 update_marginal_area (w
, RIGHT_MARGIN_AREA
, vpos
);
4010 /* Draw truncation marks etc. */
4011 if (!current_row
->enabled_p
4012 || desired_row
->y
!= current_row
->y
4013 || desired_row
->visible_height
!= current_row
->visible_height
4014 || desired_row
->cursor_in_fringe_p
!= current_row
->cursor_in_fringe_p
4015 || desired_row
->overlay_arrow_bitmap
!= current_row
->overlay_arrow_bitmap
4016 || current_row
->redraw_fringe_bitmaps_p
4017 || desired_row
->mode_line_p
!= current_row
->mode_line_p
4018 || desired_row
->exact_window_width_line_p
!= current_row
->exact_window_width_line_p
4019 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row
)
4020 != MATRIX_ROW_CONTINUATION_LINE_P (current_row
)))
4021 rif
->after_update_window_line_hook (desired_row
);
4024 /* Update current_row from desired_row. */
4025 make_current (w
->desired_matrix
, w
->current_matrix
, vpos
);
4031 /* Set the cursor after an update of window W. This function may only
4032 be called from update_window. */
4035 set_window_cursor_after_update (struct window
*w
)
4037 struct frame
*f
= XFRAME (WVAR (w
, frame
));
4038 struct redisplay_interface
*rif
= FRAME_RIF (f
);
4039 int cx
, cy
, vpos
, hpos
;
4041 /* Not intended for frame matrix updates. */
4042 eassert (FRAME_WINDOW_P (f
));
4044 if (cursor_in_echo_area
4045 && !NILP (echo_area_buffer
[0])
4046 /* If we are showing a message instead of the mini-buffer,
4047 show the cursor for the message instead. */
4048 && XWINDOW (minibuf_window
) == w
4049 && EQ (minibuf_window
, echo_area_window
)
4050 /* These cases apply only to the frame that contains
4051 the active mini-buffer window. */
4052 && FRAME_HAS_MINIBUF_P (f
)
4053 && EQ (FRAME_MINIBUF_WINDOW (f
), echo_area_window
))
4055 cx
= cy
= vpos
= hpos
= 0;
4057 if (cursor_in_echo_area
>= 0)
4059 /* If the mini-buffer is several lines high, find the last
4060 line that has any text on it. Note: either all lines
4061 are enabled or none. Otherwise we wouldn't be able to
4063 struct glyph_row
*row
, *last_row
;
4064 struct glyph
*glyph
;
4065 int yb
= window_text_bottom_y (w
);
4068 row
= w
->current_matrix
->rows
;
4069 while (row
->enabled_p
4070 && (last_row
== NULL
4071 || MATRIX_ROW_BOTTOM_Y (row
) <= yb
))
4073 if (row
->used
[TEXT_AREA
]
4074 && row
->glyphs
[TEXT_AREA
][0].charpos
>= 0)
4081 struct glyph
*start
= last_row
->glyphs
[TEXT_AREA
];
4082 struct glyph
*last
= start
+ last_row
->used
[TEXT_AREA
] - 1;
4084 while (last
> start
&& last
->charpos
< 0)
4087 for (glyph
= start
; glyph
< last
; ++glyph
)
4089 cx
+= glyph
->pixel_width
;
4094 vpos
= MATRIX_ROW_VPOS (last_row
, w
->current_matrix
);
4102 hpos
= w
->cursor
.hpos
;
4103 vpos
= w
->cursor
.vpos
;
4106 /* Window cursor can be out of sync for horizontally split windows. */
4107 hpos
= max (-1, hpos
); /* -1 is for when cursor is on the left fringe */
4108 hpos
= min (w
->current_matrix
->matrix_w
- 1, hpos
);
4109 vpos
= max (0, vpos
);
4110 vpos
= min (w
->current_matrix
->nrows
- 1, vpos
);
4111 rif
->cursor_to (vpos
, hpos
, cy
, cx
);
4115 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
4116 tree rooted at W. */
4119 set_window_update_flags (struct window
*w
, int on_p
)
4123 if (!NILP (WVAR (w
, hchild
)))
4124 set_window_update_flags (XWINDOW (WVAR (w
, hchild
)), on_p
);
4125 else if (!NILP (WVAR (w
, vchild
)))
4126 set_window_update_flags (XWINDOW (WVAR (w
, vchild
)), on_p
);
4128 w
->must_be_updated_p
= on_p
;
4130 w
= NILP (WVAR (w
, next
)) ? 0 : XWINDOW (WVAR (w
, next
));
4136 /***********************************************************************
4137 Window-Based Scrolling
4138 ***********************************************************************/
4140 /* Structure describing rows in scrolling_window. */
4144 /* Number of occurrences of this row in desired and current matrix. */
4145 int old_uses
, new_uses
;
4147 /* Vpos of row in new matrix. */
4148 int new_line_number
;
4150 /* Bucket index of this row_entry in the hash table row_table. */
4153 /* The row described by this entry. */
4154 struct glyph_row
*row
;
4156 /* Hash collision chain. */
4157 struct row_entry
*next
;
4160 /* A pool to allocate row_entry structures from, and the size of the
4161 pool. The pool is reallocated in scrolling_window when we find
4162 that we need a larger one. */
4164 static struct row_entry
*row_entry_pool
;
4165 static ptrdiff_t row_entry_pool_size
;
4167 /* Index of next free entry in row_entry_pool. */
4169 static ptrdiff_t row_entry_idx
;
4171 /* The hash table used during scrolling, and the table's size. This
4172 table is used to quickly identify equal rows in the desired and
4175 static struct row_entry
**row_table
;
4176 static ptrdiff_t row_table_size
;
4178 /* Vectors of pointers to row_entry structures belonging to the
4179 current and desired matrix, and the size of the vectors. */
4181 static struct row_entry
**old_lines
, **new_lines
;
4182 static ptrdiff_t old_lines_size
, new_lines_size
;
4184 /* A pool to allocate run structures from, and its size. */
4186 static struct run
*run_pool
;
4187 static ptrdiff_t runs_size
;
4189 /* A vector of runs of lines found during scrolling. */
4191 static struct run
**runs
;
4193 /* Add glyph row ROW to the scrolling hash table. */
4195 static inline struct row_entry
*
4196 add_row_entry (struct glyph_row
*row
)
4198 struct row_entry
*entry
;
4199 ptrdiff_t i
= row
->hash
% row_table_size
;
4201 entry
= row_table
[i
];
4202 eassert (entry
|| verify_row_hash (row
));
4203 while (entry
&& !row_equal_p (entry
->row
, row
, 1))
4204 entry
= entry
->next
;
4208 entry
= row_entry_pool
+ row_entry_idx
++;
4210 entry
->old_uses
= entry
->new_uses
= 0;
4211 entry
->new_line_number
= 0;
4213 entry
->next
= row_table
[i
];
4214 row_table
[i
] = entry
;
4221 /* Try to reuse part of the current display of W by scrolling lines.
4222 HEADER_LINE_P non-zero means W has a header line.
4224 The algorithm is taken from Communications of the ACM, Apr78 "A
4225 Technique for Isolating Differences Between Files." It should take
4228 A short outline of the steps of the algorithm
4230 1. Skip lines equal at the start and end of both matrices.
4232 2. Enter rows in the current and desired matrix into a symbol
4233 table, counting how often they appear in both matrices.
4235 3. Rows that appear exactly once in both matrices serve as anchors,
4236 i.e. we assume that such lines are likely to have been moved.
4238 4. Starting from anchor lines, extend regions to be scrolled both
4239 forward and backward.
4243 -1 if all rows were found to be equal.
4244 0 to indicate that we did not scroll the display, or
4245 1 if we did scroll. */
4248 scrolling_window (struct window
*w
, int header_line_p
)
4250 struct glyph_matrix
*desired_matrix
= w
->desired_matrix
;
4251 struct glyph_matrix
*current_matrix
= w
->current_matrix
;
4252 int yb
= window_text_bottom_y (w
);
4254 int j
, first_old
, first_new
, last_old
, last_new
;
4257 struct row_entry
*entry
;
4258 struct redisplay_interface
*rif
= FRAME_RIF (XFRAME (WINDOW_FRAME (w
)));
4260 /* Skip over rows equal at the start. */
4261 for (i
= header_line_p
? 1 : 0; i
< current_matrix
->nrows
- 1; ++i
)
4263 struct glyph_row
*d
= MATRIX_ROW (desired_matrix
, i
);
4264 struct glyph_row
*c
= MATRIX_ROW (current_matrix
, i
);
4268 && !d
->redraw_fringe_bitmaps_p
4270 && MATRIX_ROW_BOTTOM_Y (c
) <= yb
4271 && MATRIX_ROW_BOTTOM_Y (d
) <= yb
4272 && row_equal_p (c
, d
, 1))
4281 /* Give up if some rows in the desired matrix are not enabled. */
4282 if (!MATRIX_ROW (desired_matrix
, i
)->enabled_p
)
4285 first_old
= first_new
= i
;
4287 /* Set last_new to the index + 1 of the row that reaches the
4288 bottom boundary in the desired matrix. Give up if we find a
4289 disabled row before we reach the bottom boundary. */
4291 while (i
< desired_matrix
->nrows
- 1)
4295 if (!MATRIX_ROW (desired_matrix
, i
)->enabled_p
)
4297 bottom
= MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix
, i
));
4306 /* Set last_old to the index + 1 of the row that reaches the bottom
4307 boundary in the current matrix. We don't look at the enabled
4308 flag here because we plan to reuse part of the display even if
4309 other parts are disabled. */
4311 while (i
< current_matrix
->nrows
- 1)
4313 int bottom
= MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix
, i
));
4322 /* Skip over rows equal at the bottom. */
4325 while (i
- 1 > first_new
4326 && j
- 1 > first_old
4327 && MATRIX_ROW (current_matrix
, j
- 1)->enabled_p
4328 && (MATRIX_ROW (current_matrix
, j
- 1)->y
4329 == MATRIX_ROW (desired_matrix
, i
- 1)->y
)
4330 && !MATRIX_ROW (desired_matrix
, i
- 1)->redraw_fringe_bitmaps_p
4331 && row_equal_p (MATRIX_ROW (desired_matrix
, i
- 1),
4332 MATRIX_ROW (current_matrix
, j
- 1), 1))
4337 /* Nothing to do if all rows are equal. */
4338 if (last_new
== first_new
)
4341 /* Check for integer overflow in size calculation.
4343 If next_almost_prime checks (N) for divisibility by 2..10, then
4344 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4345 So, set next_almost_prime_increment_max to 10.
4347 It's just a coincidence that next_almost_prime_increment_max ==
4348 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4349 13, then next_almost_prime_increment_max would be 14, e.g.,
4350 because next_almost_prime (113) would be 127. */
4352 verify (NEXT_ALMOST_PRIME_LIMIT
== 11);
4353 enum { next_almost_prime_increment_max
= 10 };
4354 ptrdiff_t row_table_max
=
4355 (min (PTRDIFF_MAX
, SIZE_MAX
) / (3 * sizeof *row_table
)
4356 - next_almost_prime_increment_max
);
4357 ptrdiff_t current_nrows_max
= row_table_max
- desired_matrix
->nrows
;
4358 if (current_nrows_max
< current_matrix
->nrows
)
4359 memory_full (SIZE_MAX
);
4362 /* Reallocate vectors, tables etc. if necessary. */
4364 if (current_matrix
->nrows
> old_lines_size
)
4365 old_lines
= xpalloc (old_lines
, &old_lines_size
,
4366 current_matrix
->nrows
- old_lines_size
,
4367 INT_MAX
, sizeof *old_lines
);
4369 if (desired_matrix
->nrows
> new_lines_size
)
4370 new_lines
= xpalloc (new_lines
, &new_lines_size
,
4371 desired_matrix
->nrows
- new_lines_size
,
4372 INT_MAX
, sizeof *new_lines
);
4374 n
= desired_matrix
->nrows
;
4375 n
+= current_matrix
->nrows
;
4376 if (row_table_size
< 3 * n
)
4378 ptrdiff_t size
= next_almost_prime (3 * n
);
4379 row_table
= xnrealloc (row_table
, size
, sizeof *row_table
);
4380 row_table_size
= size
;
4381 memset (row_table
, 0, size
* sizeof *row_table
);
4384 if (n
> row_entry_pool_size
)
4385 row_entry_pool
= xpalloc (row_entry_pool
, &row_entry_pool_size
,
4386 n
- row_entry_pool_size
,
4387 -1, sizeof *row_entry_pool
);
4389 if (desired_matrix
->nrows
> runs_size
)
4391 runs
= xnrealloc (runs
, desired_matrix
->nrows
, sizeof *runs
);
4392 run_pool
= xnrealloc (run_pool
, desired_matrix
->nrows
, sizeof *run_pool
);
4393 runs_size
= desired_matrix
->nrows
;
4396 nruns
= run_idx
= 0;
4399 /* Add rows from the current and desired matrix to the hash table
4400 row_hash_table to be able to find equal ones quickly. */
4402 for (i
= first_old
; i
< last_old
; ++i
)
4404 if (MATRIX_ROW (current_matrix
, i
)->enabled_p
)
4406 entry
= add_row_entry (MATRIX_ROW (current_matrix
, i
));
4407 old_lines
[i
] = entry
;
4411 old_lines
[i
] = NULL
;
4414 for (i
= first_new
; i
< last_new
; ++i
)
4416 eassert (MATRIX_ROW_ENABLED_P (desired_matrix
, i
));
4417 entry
= add_row_entry (MATRIX_ROW (desired_matrix
, i
));
4419 entry
->new_line_number
= i
;
4420 new_lines
[i
] = entry
;
4423 /* Identify moves based on lines that are unique and equal
4424 in both matrices. */
4425 for (i
= first_old
; i
< last_old
;)
4427 && old_lines
[i
]->old_uses
== 1
4428 && old_lines
[i
]->new_uses
== 1)
4431 int new_line
= old_lines
[i
]->new_line_number
;
4432 struct run
*run
= run_pool
+ run_idx
++;
4435 run
->current_vpos
= i
;
4436 run
->current_y
= MATRIX_ROW (current_matrix
, i
)->y
;
4437 run
->desired_vpos
= new_line
;
4438 run
->desired_y
= MATRIX_ROW (desired_matrix
, new_line
)->y
;
4440 run
->height
= MATRIX_ROW (current_matrix
, i
)->height
;
4442 /* Extend backward. */
4445 while (p
> first_old
4447 && old_lines
[p
] == new_lines
[q
])
4449 int h
= MATRIX_ROW (current_matrix
, p
)->height
;
4450 --run
->current_vpos
;
4451 --run
->desired_vpos
;
4454 run
->desired_y
-= h
;
4455 run
->current_y
-= h
;
4459 /* Extend forward. */
4464 && old_lines
[p
] == new_lines
[q
])
4466 int h
= MATRIX_ROW (current_matrix
, p
)->height
;
4472 /* Insert run into list of all runs. Order runs by copied
4473 pixel lines. Note that we record runs that don't have to
4474 be copied because they are already in place. This is done
4475 because we can avoid calling update_window_line in this
4477 for (p
= 0; p
< nruns
&& runs
[p
]->height
> run
->height
; ++p
)
4479 for (q
= nruns
; q
> p
; --q
)
4480 runs
[q
] = runs
[q
- 1];
4489 /* Do the moves. Do it in a way that we don't overwrite something
4490 we want to copy later on. This is not solvable in general
4491 because there is only one display and we don't have a way to
4492 exchange areas on this display. Example:
4494 +-----------+ +-----------+
4496 +-----------+ --> +-----------+
4498 +-----------+ +-----------+
4500 Instead, prefer bigger moves, and invalidate moves that would
4501 copy from where we copied to. */
4503 for (i
= 0; i
< nruns
; ++i
)
4504 if (runs
[i
]->nrows
> 0)
4506 struct run
*r
= runs
[i
];
4508 /* Copy on the display. */
4509 if (r
->current_y
!= r
->desired_y
)
4511 rif
->clear_window_mouse_face (w
);
4512 rif
->scroll_run_hook (w
, r
);
4515 /* Truncate runs that copy to where we copied to, and
4516 invalidate runs that copy from where we copied to. */
4517 for (j
= nruns
- 1; j
> i
; --j
)
4519 struct run
*p
= runs
[j
];
4520 int truncated_p
= 0;
4523 && p
->desired_y
< r
->desired_y
+ r
->height
4524 && p
->desired_y
+ p
->height
> r
->desired_y
)
4526 if (p
->desired_y
< r
->desired_y
)
4528 p
->nrows
= r
->desired_vpos
- p
->desired_vpos
;
4529 p
->height
= r
->desired_y
- p
->desired_y
;
4534 int nrows_copied
= (r
->desired_vpos
+ r
->nrows
4537 if (p
->nrows
<= nrows_copied
)
4541 int height_copied
= (r
->desired_y
+ r
->height
4544 p
->current_vpos
+= nrows_copied
;
4545 p
->desired_vpos
+= nrows_copied
;
4546 p
->nrows
-= nrows_copied
;
4547 p
->current_y
+= height_copied
;
4548 p
->desired_y
+= height_copied
;
4549 p
->height
-= height_copied
;
4555 if (r
->current_y
!= r
->desired_y
4556 /* The condition below is equivalent to
4557 ((p->current_y >= r->desired_y
4558 && p->current_y < r->desired_y + r->height)
4559 || (p->current_y + p->height > r->desired_y
4560 && (p->current_y + p->height
4561 <= r->desired_y + r->height)))
4562 because we have 0 < p->height <= r->height. */
4563 && p
->current_y
< r
->desired_y
+ r
->height
4564 && p
->current_y
+ p
->height
> r
->desired_y
)
4567 /* Reorder runs by copied pixel lines if truncated. */
4568 if (truncated_p
&& p
->nrows
> 0)
4572 while (runs
[k
]->nrows
== 0 || runs
[k
]->height
< p
->height
)
4574 memmove (runs
+ j
, runs
+ j
+ 1, (k
- j
) * sizeof (*runs
));
4579 /* Assign matrix rows. */
4580 for (j
= 0; j
< r
->nrows
; ++j
)
4582 struct glyph_row
*from
, *to
;
4583 int to_overlapped_p
;
4585 to
= MATRIX_ROW (current_matrix
, r
->desired_vpos
+ j
);
4586 from
= MATRIX_ROW (desired_matrix
, r
->desired_vpos
+ j
);
4587 to_overlapped_p
= to
->overlapped_p
;
4588 from
->redraw_fringe_bitmaps_p
= from
->fringe_bitmap_periodic_p
;
4589 assign_row (to
, from
);
4590 /* The above `assign_row' actually does swap, so if we had
4591 an overlap in the copy destination of two runs, then
4592 the second run would assign a previously disabled bogus
4593 row. But thanks to the truncation code in the
4594 preceding for-loop, we no longer have such an overlap,
4595 and thus the assigned row should always be enabled. */
4596 eassert (to
->enabled_p
);
4597 from
->enabled_p
= 0;
4598 to
->overlapped_p
= to_overlapped_p
;
4602 /* Clear the hash table, for the next time. */
4603 for (i
= 0; i
< row_entry_idx
; ++i
)
4604 row_table
[row_entry_pool
[i
].bucket
] = NULL
;
4606 /* Value is 1 to indicate that we scrolled the display. */
4612 /************************************************************************
4614 ************************************************************************/
4616 /* Update the desired frame matrix of frame F.
4618 FORCE_P non-zero means that the update should not be stopped by
4619 pending input. INHIBIT_HAIRY_ID_P non-zero means that scrolling
4620 should not be tried.
4622 Value is non-zero if update was stopped due to pending input. */
4625 update_frame_1 (struct frame
*f
, int force_p
, int inhibit_id_p
)
4627 /* Frame matrices to work on. */
4628 struct glyph_matrix
*current_matrix
= f
->current_matrix
;
4629 struct glyph_matrix
*desired_matrix
= f
->desired_matrix
;
4632 int preempt_count
= baud_rate
/ 2400 + 1;
4634 eassert (current_matrix
&& desired_matrix
);
4636 if (baud_rate
!= FRAME_COST_BAUD_RATE (f
))
4637 calculate_costs (f
);
4639 if (preempt_count
<= 0)
4642 #if !PERIODIC_PREEMPTION_CHECKING
4643 if (!force_p
&& detect_input_pending_ignore_squeezables ())
4650 /* If we cannot insert/delete lines, it's no use trying it. */
4651 if (!FRAME_LINE_INS_DEL_OK (f
))
4654 /* See if any of the desired lines are enabled; don't compute for
4655 i/d line if just want cursor motion. */
4656 for (i
= 0; i
< desired_matrix
->nrows
; i
++)
4657 if (MATRIX_ROW_ENABLED_P (desired_matrix
, i
))
4660 /* Try doing i/d line, if not yet inhibited. */
4661 if (!inhibit_id_p
&& i
< desired_matrix
->nrows
)
4662 force_p
|= scrolling (f
);
4664 /* Update the individual lines as needed. Do bottom line first. */
4665 if (MATRIX_ROW_ENABLED_P (desired_matrix
, desired_matrix
->nrows
- 1))
4666 update_frame_line (f
, desired_matrix
->nrows
- 1);
4668 /* Now update the rest of the lines. */
4669 for (i
= 0; i
< desired_matrix
->nrows
- 1 && (force_p
|| !input_pending
); i
++)
4671 if (MATRIX_ROW_ENABLED_P (desired_matrix
, i
))
4673 if (FRAME_TERMCAP_P (f
))
4675 /* Flush out every so many lines.
4676 Also flush out if likely to have more than 1k buffered
4677 otherwise. I'm told that some telnet connections get
4678 really screwed by more than 1k output at once. */
4679 FILE *display_output
= FRAME_TTY (f
)->output
;
4682 int outq
= PENDING_OUTPUT_COUNT (display_output
);
4684 || (outq
> 20 && ((i
- 1) % preempt_count
== 0)))
4686 fflush (display_output
);
4687 if (preempt_count
== 1)
4689 #ifdef EMACS_OUTQSIZE
4690 if (EMACS_OUTQSIZE (0, &outq
) < 0)
4691 /* Probably not a tty. Ignore the error and reset
4693 outq
= PENDING_OUTPUT_COUNT (FRAME_TTY (f
->output
));
4696 if (baud_rate
<= outq
&& baud_rate
> 0)
4697 sleep (outq
/ baud_rate
);
4703 #if PERIODIC_PREEMPTION_CHECKING
4706 EMACS_TIME tm
= current_emacs_time ();
4707 if (EMACS_TIME_LT (preemption_next_check
, tm
))
4709 preemption_next_check
= add_emacs_time (tm
, preemption_period
);
4710 if (detect_input_pending_ignore_squeezables ())
4715 if (!force_p
&& (i
- 1) % preempt_count
== 0)
4716 detect_input_pending_ignore_squeezables ();
4719 update_frame_line (f
, i
);
4723 pause_p
= (i
< FRAME_LINES (f
) - 1) ? i
: 0;
4725 /* Now just clean up termcap drivers and set cursor, etc. */
4728 if ((cursor_in_echo_area
4729 /* If we are showing a message instead of the mini-buffer,
4730 show the cursor for the message instead of for the
4731 (now hidden) mini-buffer contents. */
4732 || (EQ (minibuf_window
, selected_window
)
4733 && EQ (minibuf_window
, echo_area_window
)
4734 && !NILP (echo_area_buffer
[0])))
4735 /* These cases apply only to the frame that contains
4736 the active mini-buffer window. */
4737 && FRAME_HAS_MINIBUF_P (f
)
4738 && EQ (FRAME_MINIBUF_WINDOW (f
), echo_area_window
))
4740 int top
= WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f
)));
4743 if (cursor_in_echo_area
< 0)
4745 /* Negative value of cursor_in_echo_area means put
4746 cursor at beginning of line. */
4752 /* Positive value of cursor_in_echo_area means put
4753 cursor at the end of the prompt. If the mini-buffer
4754 is several lines high, find the last line that has
4756 row
= FRAME_LINES (f
);
4762 if (MATRIX_ROW_ENABLED_P (current_matrix
, row
))
4764 /* Frame rows are filled up with spaces that
4765 must be ignored here. */
4766 struct glyph_row
*r
= MATRIX_ROW (current_matrix
,
4768 struct glyph
*start
= r
->glyphs
[TEXT_AREA
];
4769 struct glyph
*last
= start
+ r
->used
[TEXT_AREA
];
4772 && (last
- 1)->charpos
< 0)
4778 while (row
> top
&& col
== 0);
4780 /* Make sure COL is not out of range. */
4781 if (col
>= FRAME_CURSOR_X_LIMIT (f
))
4783 /* If we have another row, advance cursor into it. */
4784 if (row
< FRAME_LINES (f
) - 1)
4786 col
= FRAME_LEFT_SCROLL_BAR_COLS (f
);
4789 /* Otherwise move it back in range. */
4791 col
= FRAME_CURSOR_X_LIMIT (f
) - 1;
4795 cursor_to (f
, row
, col
);
4799 /* We have only one cursor on terminal frames. Use it to
4800 display the cursor of the selected window. */
4801 struct window
*w
= XWINDOW (FRAME_SELECTED_WINDOW (f
));
4802 if (w
->cursor
.vpos
>= 0
4803 /* The cursor vpos may be temporarily out of bounds
4804 in the following situation: There is one window,
4805 with the cursor in the lower half of it. The window
4806 is split, and a message causes a redisplay before
4807 a new cursor position has been computed. */
4808 && w
->cursor
.vpos
< WINDOW_TOTAL_LINES (w
))
4810 int x
= WINDOW_TO_FRAME_HPOS (w
, w
->cursor
.hpos
);
4811 int y
= WINDOW_TO_FRAME_VPOS (w
, w
->cursor
.vpos
);
4813 if (INTEGERP (WVAR (w
, left_margin_cols
)))
4814 x
+= XFASTINT (WVAR (w
, left_margin_cols
));
4816 /* x = max (min (x, FRAME_TOTAL_COLS (f) - 1), 0); */
4817 cursor_to (f
, y
, x
);
4822 #if !PERIODIC_PREEMPTION_CHECKING
4826 clear_desired_matrices (f
);
4831 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4834 scrolling (struct frame
*frame
)
4836 int unchanged_at_top
, unchanged_at_bottom
;
4839 int *old_hash
= alloca (FRAME_LINES (frame
) * sizeof (int));
4840 int *new_hash
= alloca (FRAME_LINES (frame
) * sizeof (int));
4841 int *draw_cost
= alloca (FRAME_LINES (frame
) * sizeof (int));
4842 int *old_draw_cost
= alloca (FRAME_LINES (frame
) * sizeof (int));
4844 int free_at_end_vpos
= FRAME_LINES (frame
);
4845 struct glyph_matrix
*current_matrix
= frame
->current_matrix
;
4846 struct glyph_matrix
*desired_matrix
= frame
->desired_matrix
;
4848 if (!current_matrix
)
4851 /* Compute hash codes of all the lines. Also calculate number of
4852 changed lines, number of unchanged lines at the beginning, and
4853 number of unchanged lines at the end. */
4855 unchanged_at_top
= 0;
4856 unchanged_at_bottom
= FRAME_LINES (frame
);
4857 for (i
= 0; i
< FRAME_LINES (frame
); i
++)
4859 /* Give up on this scrolling if some old lines are not enabled. */
4860 if (!MATRIX_ROW_ENABLED_P (current_matrix
, i
))
4862 old_hash
[i
] = line_hash_code (MATRIX_ROW (current_matrix
, i
));
4863 if (! MATRIX_ROW_ENABLED_P (desired_matrix
, i
))
4865 /* This line cannot be redrawn, so don't let scrolling mess it. */
4866 new_hash
[i
] = old_hash
[i
];
4867 #define INFINITY 1000000 /* Taken from scroll.c */
4868 draw_cost
[i
] = INFINITY
;
4872 new_hash
[i
] = line_hash_code (MATRIX_ROW (desired_matrix
, i
));
4873 draw_cost
[i
] = line_draw_cost (desired_matrix
, i
);
4876 if (old_hash
[i
] != new_hash
[i
])
4879 unchanged_at_bottom
= FRAME_LINES (frame
) - i
- 1;
4881 else if (i
== unchanged_at_top
)
4883 old_draw_cost
[i
] = line_draw_cost (current_matrix
, i
);
4886 /* If changed lines are few, don't allow preemption, don't scroll. */
4887 if ((!FRAME_SCROLL_REGION_OK (frame
)
4888 && changed_lines
< baud_rate
/ 2400)
4889 || unchanged_at_bottom
== FRAME_LINES (frame
))
4892 window_size
= (FRAME_LINES (frame
) - unchanged_at_top
4893 - unchanged_at_bottom
);
4895 if (FRAME_SCROLL_REGION_OK (frame
))
4896 free_at_end_vpos
-= unchanged_at_bottom
;
4897 else if (FRAME_MEMORY_BELOW_FRAME (frame
))
4898 free_at_end_vpos
= -1;
4900 /* If large window, fast terminal and few lines in common between
4901 current frame and desired frame, don't bother with i/d calc. */
4902 if (!FRAME_SCROLL_REGION_OK (frame
)
4903 && window_size
>= 18 && baud_rate
> 2400
4905 10 * scrolling_max_lines_saved (unchanged_at_top
,
4906 FRAME_LINES (frame
) - unchanged_at_bottom
,
4907 old_hash
, new_hash
, draw_cost
)))
4910 if (window_size
< 2)
4913 scrolling_1 (frame
, window_size
, unchanged_at_top
, unchanged_at_bottom
,
4914 draw_cost
+ unchanged_at_top
- 1,
4915 old_draw_cost
+ unchanged_at_top
- 1,
4916 old_hash
+ unchanged_at_top
- 1,
4917 new_hash
+ unchanged_at_top
- 1,
4918 free_at_end_vpos
- unchanged_at_top
);
4924 /* Count the number of blanks at the start of the vector of glyphs R
4925 which is LEN glyphs long. */
4928 count_blanks (struct glyph
*r
, int len
)
4932 for (i
= 0; i
< len
; ++i
)
4933 if (!CHAR_GLYPH_SPACE_P (r
[i
]))
4940 /* Count the number of glyphs in common at the start of the glyph
4941 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4942 of STR2. Value is the number of equal glyphs equal at the start. */
4945 count_match (struct glyph
*str1
, struct glyph
*end1
, struct glyph
*str2
, struct glyph
*end2
)
4947 struct glyph
*p1
= str1
;
4948 struct glyph
*p2
= str2
;
4952 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1
, p2
))
4959 /* Char insertion/deletion cost vector, from term.c */
4961 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4964 /* Perform a frame-based update on line VPOS in frame FRAME. */
4967 update_frame_line (struct frame
*f
, int vpos
)
4969 struct glyph
*obody
, *nbody
, *op1
, *op2
, *np1
, *nend
;
4971 int osp
, nsp
, begmatch
, endmatch
, olen
, nlen
;
4972 struct glyph_matrix
*current_matrix
= f
->current_matrix
;
4973 struct glyph_matrix
*desired_matrix
= f
->desired_matrix
;
4974 struct glyph_row
*current_row
= MATRIX_ROW (current_matrix
, vpos
);
4975 struct glyph_row
*desired_row
= MATRIX_ROW (desired_matrix
, vpos
);
4976 int must_write_whole_line_p
;
4977 int write_spaces_p
= FRAME_MUST_WRITE_SPACES (f
);
4978 int colored_spaces_p
= (FACE_FROM_ID (f
, DEFAULT_FACE_ID
)->background
4979 != FACE_TTY_DEFAULT_BG_COLOR
);
4981 if (colored_spaces_p
)
4984 /* Current row not enabled means it has unknown contents. We must
4985 write the whole desired line in that case. */
4986 must_write_whole_line_p
= !current_row
->enabled_p
;
4987 if (must_write_whole_line_p
)
4994 obody
= MATRIX_ROW_GLYPH_START (current_matrix
, vpos
);
4995 olen
= current_row
->used
[TEXT_AREA
];
4997 /* Ignore trailing spaces, if we can. */
4998 if (!write_spaces_p
)
4999 while (olen
> 0 && CHAR_GLYPH_SPACE_P (obody
[olen
-1]))
5003 current_row
->enabled_p
= 1;
5004 current_row
->used
[TEXT_AREA
] = desired_row
->used
[TEXT_AREA
];
5006 /* If desired line is empty, just clear the line. */
5007 if (!desired_row
->enabled_p
)
5013 nbody
= desired_row
->glyphs
[TEXT_AREA
];
5014 nlen
= desired_row
->used
[TEXT_AREA
];
5015 nend
= nbody
+ nlen
;
5017 /* If display line has unknown contents, write the whole line. */
5018 if (must_write_whole_line_p
)
5020 /* Ignore spaces at the end, if we can. */
5021 if (!write_spaces_p
)
5022 while (nlen
> 0 && CHAR_GLYPH_SPACE_P (nbody
[nlen
- 1]))
5025 /* Write the contents of the desired line. */
5028 cursor_to (f
, vpos
, 0);
5029 write_glyphs (f
, nbody
, nlen
);
5032 /* Don't call clear_end_of_line if we already wrote the whole
5033 line. The cursor will not be at the right margin in that
5034 case but in the line below. */
5035 if (nlen
< FRAME_TOTAL_COLS (f
))
5037 cursor_to (f
, vpos
, nlen
);
5038 clear_end_of_line (f
, FRAME_TOTAL_COLS (f
));
5041 /* Make sure we are in the right row, otherwise cursor movement
5042 with cmgoto might use `ch' in the wrong row. */
5043 cursor_to (f
, vpos
, 0);
5045 make_current (desired_matrix
, current_matrix
, vpos
);
5049 /* Pretend trailing spaces are not there at all,
5050 unless for one reason or another we must write all spaces. */
5051 if (!write_spaces_p
)
5052 while (nlen
> 0 && CHAR_GLYPH_SPACE_P (nbody
[nlen
- 1]))
5055 /* If there's no i/d char, quickly do the best we can without it. */
5056 if (!FRAME_CHAR_INS_DEL_OK (f
))
5060 /* Find the first glyph in desired row that doesn't agree with
5061 a glyph in the current row, and write the rest from there on. */
5062 for (i
= 0; i
< nlen
; i
++)
5064 if (i
>= olen
|| !GLYPH_EQUAL_P (nbody
+ i
, obody
+ i
))
5066 /* Find the end of the run of different glyphs. */
5070 || !GLYPH_EQUAL_P (nbody
+ j
, obody
+ j
)
5071 || CHAR_GLYPH_PADDING_P (nbody
[j
])))
5074 /* Output this run of non-matching chars. */
5075 cursor_to (f
, vpos
, i
);
5076 write_glyphs (f
, nbody
+ i
, j
- i
);
5079 /* Now find the next non-match. */
5083 /* Clear the rest of the line, or the non-clear part of it. */
5086 cursor_to (f
, vpos
, nlen
);
5087 clear_end_of_line (f
, olen
);
5090 /* Make current row = desired row. */
5091 make_current (desired_matrix
, current_matrix
, vpos
);
5095 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
5096 characters in a row. */
5100 /* If current line is blank, skip over initial spaces, if
5101 possible, and write the rest. */
5105 nsp
= count_blanks (nbody
, nlen
);
5109 cursor_to (f
, vpos
, nsp
);
5110 write_glyphs (f
, nbody
+ nsp
, nlen
- nsp
);
5113 /* Exchange contents between current_frame and new_frame. */
5114 make_current (desired_matrix
, current_matrix
, vpos
);
5118 /* Compute number of leading blanks in old and new contents. */
5119 osp
= count_blanks (obody
, olen
);
5120 nsp
= (colored_spaces_p
? 0 : count_blanks (nbody
, nlen
));
5122 /* Compute number of matching chars starting with first non-blank. */
5123 begmatch
= count_match (obody
+ osp
, obody
+ olen
,
5124 nbody
+ nsp
, nbody
+ nlen
);
5126 /* Spaces in new match implicit space past the end of old. */
5127 /* A bug causing this to be a no-op was fixed in 18.29. */
5128 if (!write_spaces_p
&& osp
+ begmatch
== olen
)
5131 while (np1
+ begmatch
< nend
&& CHAR_GLYPH_SPACE_P (np1
[begmatch
]))
5135 /* Avoid doing insert/delete char
5136 just cause number of leading spaces differs
5137 when the following text does not match. */
5138 if (begmatch
== 0 && osp
!= nsp
)
5139 osp
= nsp
= min (osp
, nsp
);
5141 /* Find matching characters at end of line */
5144 op2
= op1
+ begmatch
- min (olen
- osp
, nlen
- nsp
);
5146 && GLYPH_EQUAL_P (op1
- 1, np1
- 1))
5151 endmatch
= obody
+ olen
- op1
;
5153 /* tem gets the distance to insert or delete.
5154 endmatch is how many characters we save by doing so.
5157 tem
= (nlen
- nsp
) - (olen
- osp
);
5159 && (!FRAME_CHAR_INS_DEL_OK (f
)
5160 || endmatch
<= char_ins_del_cost (f
)[tem
]))
5163 /* nsp - osp is the distance to insert or delete.
5164 If that is nonzero, begmatch is known to be nonzero also.
5165 begmatch + endmatch is how much we save by doing the ins/del.
5169 && (!FRAME_CHAR_INS_DEL_OK (f
)
5170 || begmatch
+ endmatch
<= char_ins_del_cost (f
)[nsp
- osp
]))
5174 osp
= nsp
= min (osp
, nsp
);
5177 /* Now go through the line, inserting, writing and
5178 deleting as appropriate. */
5182 cursor_to (f
, vpos
, nsp
);
5183 delete_glyphs (f
, osp
- nsp
);
5187 /* If going to delete chars later in line
5188 and insert earlier in the line,
5189 must delete first to avoid losing data in the insert */
5190 if (endmatch
&& nlen
< olen
+ nsp
- osp
)
5192 cursor_to (f
, vpos
, nlen
- endmatch
+ osp
- nsp
);
5193 delete_glyphs (f
, olen
+ nsp
- osp
- nlen
);
5194 olen
= nlen
- (nsp
- osp
);
5196 cursor_to (f
, vpos
, osp
);
5197 insert_glyphs (f
, 0, nsp
- osp
);
5201 tem
= nsp
+ begmatch
+ endmatch
;
5202 if (nlen
!= tem
|| olen
!= tem
)
5204 if (!endmatch
|| nlen
== olen
)
5206 /* If new text being written reaches right margin, there is
5207 no need to do clear-to-eol at the end of this function
5208 (and it would not be safe, since cursor is not going to
5209 be "at the margin" after the text is done). */
5210 if (nlen
== FRAME_TOTAL_COLS (f
))
5213 /* Function write_glyphs is prepared to do nothing
5214 if passed a length <= 0. Check it here to avoid
5215 unnecessary cursor movement. */
5218 cursor_to (f
, vpos
, nsp
+ begmatch
);
5219 write_glyphs (f
, nbody
+ nsp
+ begmatch
, nlen
- tem
);
5222 else if (nlen
> olen
)
5224 /* Here, we used to have the following simple code:
5225 ----------------------------------------
5226 write_glyphs (nbody + nsp + begmatch, olen - tem);
5227 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
5228 ----------------------------------------
5229 but it doesn't work if nbody[nsp + begmatch + olen - tem]
5230 is a padding glyph. */
5231 int out
= olen
- tem
; /* Columns to be overwritten originally. */
5234 cursor_to (f
, vpos
, nsp
+ begmatch
);
5236 /* Calculate columns we can actually overwrite. */
5237 while (CHAR_GLYPH_PADDING_P (nbody
[nsp
+ begmatch
+ out
]))
5239 write_glyphs (f
, nbody
+ nsp
+ begmatch
, out
);
5241 /* If we left columns to be overwritten, we must delete them. */
5242 del
= olen
- tem
- out
;
5244 delete_glyphs (f
, del
);
5246 /* At last, we insert columns not yet written out. */
5247 insert_glyphs (f
, nbody
+ nsp
+ begmatch
+ out
, nlen
- olen
+ del
);
5250 else if (olen
> nlen
)
5252 cursor_to (f
, vpos
, nsp
+ begmatch
);
5253 write_glyphs (f
, nbody
+ nsp
+ begmatch
, nlen
- tem
);
5254 delete_glyphs (f
, olen
- nlen
);
5260 /* If any unerased characters remain after the new line, erase them. */
5263 cursor_to (f
, vpos
, nlen
);
5264 clear_end_of_line (f
, olen
);
5267 /* Exchange contents between current_frame and new_frame. */
5268 make_current (desired_matrix
, current_matrix
, vpos
);
5273 /***********************************************************************
5274 X/Y Position -> Buffer Position
5275 ***********************************************************************/
5277 /* Determine what's under window-relative pixel position (*X, *Y).
5278 Return the OBJECT (string or buffer) that's there.
5279 Return in *POS the position in that object.
5280 Adjust *X and *Y to character positions.
5281 Return in *DX and *DY the pixel coordinates of the click,
5282 relative to the top left corner of OBJECT, or relative to
5283 the top left corner of the character glyph at (*X, *Y)
5285 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5286 if the coordinates point to an empty area of the display. */
5289 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
)
5292 Lisp_Object old_current_buffer
= Fcurrent_buffer ();
5293 struct text_pos startp
;
5295 struct glyph_row
*row
;
5296 #ifdef HAVE_WINDOW_SYSTEM
5297 struct image
*img
= 0;
5300 void *itdata
= NULL
;
5302 /* We used to set current_buffer directly here, but that does the
5303 wrong thing with `face-remapping-alist' (bug#2044). */
5304 Fset_buffer (WVAR (w
, buffer
));
5305 itdata
= bidi_shelve_cache ();
5306 SET_TEXT_POS_FROM_MARKER (startp
, WVAR (w
, start
));
5307 CHARPOS (startp
) = min (ZV
, max (BEGV
, CHARPOS (startp
)));
5308 BYTEPOS (startp
) = min (ZV_BYTE
, max (BEGV_BYTE
, BYTEPOS (startp
)));
5309 start_display (&it
, w
, startp
);
5310 /* start_display takes into account the header-line row, but IT's
5311 vpos still counts from the glyph row that includes the window's
5312 start position. Adjust for a possible header-line row. */
5313 it
.vpos
+= WINDOW_WANTS_HEADER_LINE_P (w
) ? 1 : 0;
5317 /* First, move to the beginning of the row corresponding to *Y. We
5318 need to be in that row to get the correct value of base paragraph
5319 direction for the text at (*X, *Y). */
5320 move_it_to (&it
, -1, 0, *y
, -1, MOVE_TO_X
| MOVE_TO_Y
);
5322 /* TO_X is the pixel position that the iterator will compute for the
5323 glyph at *X. We add it.first_visible_x because iterator
5324 positions include the hscroll. */
5325 to_x
= x0
+ it
.first_visible_x
;
5326 if (it
.bidi_it
.paragraph_dir
== R2L
)
5327 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5328 text area. This is because the iterator, even in R2L
5329 paragraphs, delivers glyphs as if they started at the left
5330 margin of the window. (When we actually produce glyphs for
5331 display, we reverse their order in PRODUCE_GLYPHS, but the
5332 iterator doesn't know about that.) The following line adjusts
5333 the pixel position to the iterator geometry, which is what
5334 move_it_* routines use. (The -1 is because in a window whose
5335 text-area width is W, the rightmost pixel position is W-1, and
5336 it should be mirrored into zero pixel position.) */
5337 to_x
= window_box_width (w
, TEXT_AREA
) - to_x
- 1;
5339 /* Now move horizontally in the row to the glyph under *X. Second
5340 argument is ZV to prevent move_it_in_display_line from matching
5341 based on buffer positions. */
5342 move_it_in_display_line (&it
, ZV
, to_x
, MOVE_TO_X
);
5343 bidi_unshelve_cache (itdata
, 0);
5345 Fset_buffer (old_current_buffer
);
5347 *dx
= x0
+ it
.first_visible_x
- it
.current_x
;
5348 *dy
= *y
- it
.current_y
;
5350 string
= WVAR (w
, buffer
);
5351 if (STRINGP (it
.string
))
5354 if (it
.what
== IT_COMPOSITION
5355 && it
.cmp_it
.nchars
> 1
5356 && it
.cmp_it
.reversed_p
)
5358 /* The current display element is a grapheme cluster in a
5359 composition. In that case, we need the position of the first
5360 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5361 it.current points to the last character of the cluster, thus
5362 we must move back to the first character of the same
5364 CHARPOS (pos
->pos
) -= it
.cmp_it
.nchars
- 1;
5365 if (STRINGP (it
.string
))
5366 BYTEPOS (pos
->pos
) = string_char_to_byte (string
, CHARPOS (pos
->pos
));
5368 BYTEPOS (pos
->pos
) = buf_charpos_to_bytepos (XBUFFER (WVAR (w
, buffer
)),
5369 CHARPOS (pos
->pos
));
5372 #ifdef HAVE_WINDOW_SYSTEM
5373 if (it
.what
== IT_IMAGE
)
5375 if ((img
= IMAGE_FROM_ID (it
.f
, it
.image_id
)) != NULL
5376 && !NILP (img
->spec
))
5377 *object
= img
->spec
;
5381 if (it
.vpos
< w
->current_matrix
->nrows
5382 && (row
= MATRIX_ROW (w
->current_matrix
, it
.vpos
),
5385 if (it
.hpos
< row
->used
[TEXT_AREA
])
5387 struct glyph
*glyph
= row
->glyphs
[TEXT_AREA
] + it
.hpos
;
5388 #ifdef HAVE_WINDOW_SYSTEM
5391 *dy
-= row
->ascent
- glyph
->ascent
;
5392 *dx
+= glyph
->slice
.img
.x
;
5393 *dy
+= glyph
->slice
.img
.y
;
5394 /* Image slices positions are still relative to the entire image */
5395 *width
= img
->width
;
5396 *height
= img
->height
;
5401 *width
= glyph
->pixel_width
;
5402 *height
= glyph
->ascent
+ glyph
->descent
;
5408 *height
= row
->height
;
5413 *width
= *height
= 0;
5416 /* Add extra (default width) columns if clicked after EOL. */
5417 x1
= max (0, it
.current_x
+ it
.pixel_width
- it
.first_visible_x
);
5419 it
.hpos
+= (x0
- x1
) / WINDOW_FRAME_COLUMN_WIDTH (w
);
5428 /* Value is the string under window-relative coordinates X/Y in the
5429 mode line or header line (PART says which) of window W, or nil if none.
5430 *CHARPOS is set to the position in the string returned. */
5433 mode_line_string (struct window
*w
, enum window_part part
,
5434 int *x
, int *y
, ptrdiff_t *charpos
, Lisp_Object
*object
,
5435 int *dx
, int *dy
, int *width
, int *height
)
5437 struct glyph_row
*row
;
5438 struct glyph
*glyph
, *end
;
5440 Lisp_Object string
= Qnil
;
5442 if (part
== ON_MODE_LINE
)
5443 row
= MATRIX_MODE_LINE_ROW (w
->current_matrix
);
5445 row
= MATRIX_HEADER_LINE_ROW (w
->current_matrix
);
5447 *y
= row
- MATRIX_FIRST_TEXT_ROW (w
->current_matrix
);
5449 if (row
->mode_line_p
&& row
->enabled_p
)
5451 /* Find the glyph under X. If we find one with a string object,
5452 it's the one we were looking for. */
5453 glyph
= row
->glyphs
[TEXT_AREA
];
5454 end
= glyph
+ row
->used
[TEXT_AREA
];
5455 for (x0
= *x
; glyph
< end
&& x0
>= glyph
->pixel_width
; ++glyph
)
5456 x0
-= glyph
->pixel_width
;
5457 *x
= glyph
- row
->glyphs
[TEXT_AREA
];
5460 string
= glyph
->object
;
5461 *charpos
= glyph
->charpos
;
5462 *width
= glyph
->pixel_width
;
5463 *height
= glyph
->ascent
+ glyph
->descent
;
5464 #ifdef HAVE_WINDOW_SYSTEM
5465 if (glyph
->type
== IMAGE_GLYPH
)
5468 img
= IMAGE_FROM_ID (WINDOW_XFRAME (w
), glyph
->u
.img_id
);
5470 *object
= img
->spec
;
5471 y0
-= row
->ascent
- glyph
->ascent
;
5477 /* Add extra (default width) columns if clicked after EOL. */
5478 *x
+= x0
/ WINDOW_FRAME_COLUMN_WIDTH (w
);
5480 *height
= row
->height
;
5487 *width
= *height
= 0;
5497 /* Value is the string under window-relative coordinates X/Y in either
5498 marginal area, or nil if none. *CHARPOS is set to the position in
5499 the string returned. */
5502 marginal_area_string (struct window
*w
, enum window_part part
,
5503 int *x
, int *y
, ptrdiff_t *charpos
, Lisp_Object
*object
,
5504 int *dx
, int *dy
, int *width
, int *height
)
5506 struct glyph_row
*row
= w
->current_matrix
->rows
;
5507 struct glyph
*glyph
, *end
;
5508 int x0
, y0
, i
, wy
= *y
;
5510 Lisp_Object string
= Qnil
;
5512 if (part
== ON_LEFT_MARGIN
)
5513 area
= LEFT_MARGIN_AREA
;
5514 else if (part
== ON_RIGHT_MARGIN
)
5515 area
= RIGHT_MARGIN_AREA
;
5519 for (i
= 0; row
->enabled_p
&& i
< w
->current_matrix
->nrows
; ++i
, ++row
)
5520 if (wy
>= row
->y
&& wy
< MATRIX_ROW_BOTTOM_Y (row
))
5523 *y
= row
- MATRIX_FIRST_TEXT_ROW (w
->current_matrix
);
5527 /* Find the glyph under X. If we find one with a string object,
5528 it's the one we were looking for. */
5529 if (area
== RIGHT_MARGIN_AREA
)
5530 x0
= ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w
)
5531 ? WINDOW_LEFT_FRINGE_WIDTH (w
)
5532 : WINDOW_TOTAL_FRINGE_WIDTH (w
))
5533 + window_box_width (w
, LEFT_MARGIN_AREA
)
5534 + window_box_width (w
, TEXT_AREA
));
5536 x0
= (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w
)
5537 ? WINDOW_LEFT_FRINGE_WIDTH (w
)
5540 glyph
= row
->glyphs
[area
];
5541 end
= glyph
+ row
->used
[area
];
5542 for (x0
= *x
- x0
; glyph
< end
&& x0
>= glyph
->pixel_width
; ++glyph
)
5543 x0
-= glyph
->pixel_width
;
5544 *x
= glyph
- row
->glyphs
[area
];
5547 string
= glyph
->object
;
5548 *charpos
= glyph
->charpos
;
5549 *width
= glyph
->pixel_width
;
5550 *height
= glyph
->ascent
+ glyph
->descent
;
5551 #ifdef HAVE_WINDOW_SYSTEM
5552 if (glyph
->type
== IMAGE_GLYPH
)
5555 img
= IMAGE_FROM_ID (WINDOW_XFRAME (w
), glyph
->u
.img_id
);
5557 *object
= img
->spec
;
5558 y0
-= row
->ascent
- glyph
->ascent
;
5559 x0
+= glyph
->slice
.img
.x
;
5560 y0
+= glyph
->slice
.img
.y
;
5566 /* Add extra (default width) columns if clicked after EOL. */
5567 *x
+= x0
/ WINDOW_FRAME_COLUMN_WIDTH (w
);
5569 *height
= row
->height
;
5576 *width
= *height
= 0;
5586 /***********************************************************************
5587 Changing Frame Sizes
5588 ***********************************************************************/
5593 window_change_signal (int signalnum
) /* If we don't have an argument, */
5594 /* some compilers complain in signal calls. */
5597 int old_errno
= errno
;
5599 struct tty_display_info
*tty
;
5601 signal (SIGWINCH
, window_change_signal
);
5602 SIGNAL_THREAD_CHECK (signalnum
);
5604 /* The frame size change obviously applies to a single
5605 termcap-controlled terminal, but we can't decide which.
5606 Therefore, we resize the frames corresponding to each tty.
5608 for (tty
= tty_list
; tty
; tty
= tty
->next
) {
5610 if (! tty
->term_initted
)
5613 /* Suspended tty frames have tty->input == NULL avoid trying to
5618 get_tty_size (fileno (tty
->input
), &width
, &height
);
5620 if (width
> 5 && height
> 2) {
5621 Lisp_Object tail
, frame
;
5623 FOR_EACH_FRAME (tail
, frame
)
5624 if (FRAME_TERMCAP_P (XFRAME (frame
)) && FRAME_TTY (XFRAME (frame
)) == tty
)
5625 /* Record the new sizes, but don't reallocate the data
5626 structures now. Let that be done later outside of the
5628 change_frame_size (XFRAME (frame
), height
, width
, 0, 1, 0);
5634 #endif /* SIGWINCH */
5637 /* Do any change in frame size that was requested by a signal. SAFE
5638 non-zero means this function is called from a place where it is
5639 safe to change frame sizes while a redisplay is in progress. */
5642 do_pending_window_change (int safe
)
5644 /* If window_change_signal should have run before, run it now. */
5645 if (redisplaying_p
&& !safe
)
5648 while (delayed_size_change
)
5650 Lisp_Object tail
, frame
;
5652 delayed_size_change
= 0;
5654 FOR_EACH_FRAME (tail
, frame
)
5656 struct frame
*f
= XFRAME (frame
);
5658 if (f
->new_text_lines
!= 0 || f
->new_text_cols
!= 0)
5659 change_frame_size (f
, f
->new_text_lines
, f
->new_text_cols
,
5666 /* Change the frame height and/or width. Values may be given as zero to
5667 indicate no change is to take place.
5669 If DELAY is non-zero, then assume we're being called from a signal
5670 handler, and queue the change for later - perhaps the next
5671 redisplay. Since this tries to resize windows, we can't call it
5672 from a signal handler.
5674 SAFE non-zero means this function is called from a place where it's
5675 safe to change frame sizes while a redisplay is in progress. */
5678 change_frame_size (register struct frame
*f
, int newheight
, int newwidth
, int pretend
, int delay
, int safe
)
5680 Lisp_Object tail
, frame
;
5682 if (FRAME_MSDOS_P (f
))
5684 /* On MS-DOS, all frames use the same screen, so a change in
5685 size affects all frames. Termcap now supports multiple
5687 FOR_EACH_FRAME (tail
, frame
)
5688 if (! FRAME_WINDOW_P (XFRAME (frame
)))
5689 change_frame_size_1 (XFRAME (frame
), newheight
, newwidth
,
5690 pretend
, delay
, safe
);
5693 change_frame_size_1 (f
, newheight
, newwidth
, pretend
, delay
, safe
);
5697 change_frame_size_1 (register struct frame
*f
, int newheight
, int newwidth
, int pretend
, int delay
, int safe
)
5699 int new_frame_total_cols
;
5700 ptrdiff_t count
= SPECPDL_INDEX ();
5702 /* If we can't deal with the change now, queue it for later. */
5703 if (delay
|| (redisplaying_p
&& !safe
))
5705 f
->new_text_lines
= newheight
;
5706 f
->new_text_cols
= newwidth
;
5707 delayed_size_change
= 1;
5711 /* This size-change overrides any pending one for this frame. */
5712 f
->new_text_lines
= 0;
5713 f
->new_text_cols
= 0;
5715 /* If an argument is zero, set it to the current value. */
5717 newheight
= FRAME_LINES (f
);
5719 newwidth
= FRAME_COLS (f
);
5721 /* Compute width of windows in F. */
5722 /* Round up to the smallest acceptable size. */
5723 check_frame_size (f
, &newheight
, &newwidth
);
5725 /* This is the width of the frame with vertical scroll bars and fringe
5726 columns. Do this after rounding - see discussion of bug#9723. */
5727 new_frame_total_cols
= FRAME_TOTAL_COLS_ARG (f
, newwidth
);
5729 /* If we're not changing the frame size, quit now. */
5730 /* Frame width may be unchanged but the text portion may change, for
5731 example, fullscreen and remove/add scroll bar. */
5732 if (newheight
== FRAME_LINES (f
)
5733 /* Text portion unchanged? */
5734 && newwidth
== FRAME_COLS (f
)
5735 /* Frame width unchanged? */
5736 && new_frame_total_cols
== FRAME_TOTAL_COLS (f
))
5742 /* We only can set screen dimensions to certain values supported
5743 by our video hardware. Try to find the smallest size greater
5744 or equal to the requested dimensions. */
5745 dos_set_window_size (&newheight
, &newwidth
);
5748 if (newheight
!= FRAME_LINES (f
))
5750 resize_frame_windows (f
, newheight
, 0);
5752 /* MSDOS frames cannot PRETEND, as they change frame size by
5753 manipulating video hardware. */
5754 if ((FRAME_TERMCAP_P (f
) && !pretend
) || FRAME_MSDOS_P (f
))
5755 FrameRows (FRAME_TTY (f
)) = newheight
;
5758 if (new_frame_total_cols
!= FRAME_TOTAL_COLS (f
))
5760 resize_frame_windows (f
, new_frame_total_cols
, 1);
5762 /* MSDOS frames cannot PRETEND, as they change frame size by
5763 manipulating video hardware. */
5764 if ((FRAME_TERMCAP_P (f
) && !pretend
) || FRAME_MSDOS_P (f
))
5765 FrameCols (FRAME_TTY (f
)) = newwidth
;
5767 if (WINDOWP (FVAR (f
, tool_bar_window
)))
5768 XSETFASTINT (WVAR (XWINDOW (FVAR (f
, tool_bar_window
)), total_cols
), newwidth
);
5771 FRAME_LINES (f
) = newheight
;
5772 SET_FRAME_COLS (f
, newwidth
);
5775 struct window
*w
= XWINDOW (FRAME_SELECTED_WINDOW (f
));
5776 int text_area_x
, text_area_y
, text_area_width
, text_area_height
;
5778 window_box (w
, TEXT_AREA
, &text_area_x
, &text_area_y
, &text_area_width
,
5780 if (w
->cursor
.x
>= text_area_x
+ text_area_width
)
5781 w
->cursor
.hpos
= w
->cursor
.x
= 0;
5782 if (w
->cursor
.y
>= text_area_y
+ text_area_height
)
5783 w
->cursor
.vpos
= w
->cursor
.y
= 0;
5787 calculate_costs (f
);
5788 SET_FRAME_GARBAGED (f
);
5793 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
5795 run_window_configuration_change_hook (f
);
5797 unbind_to (count
, Qnil
);
5802 /***********************************************************************
5803 Terminal Related Lisp Functions
5804 ***********************************************************************/
5806 DEFUN ("open-termscript", Fopen_termscript
, Sopen_termscript
,
5807 1, 1, "FOpen termscript file: ",
5808 doc
: /* Start writing all terminal output to FILE as well as the terminal.
5809 FILE = nil means just close any termscript file currently open. */)
5812 struct tty_display_info
*tty
;
5814 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5815 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5816 error ("Current frame is not on a tty device");
5820 if (tty
->termscript
!= 0)
5823 fclose (tty
->termscript
);
5826 tty
->termscript
= 0;
5830 file
= Fexpand_file_name (file
, Qnil
);
5831 tty
->termscript
= fopen (SSDATA (file
), "w");
5832 if (tty
->termscript
== 0)
5833 report_file_error ("Opening termscript", Fcons (file
, Qnil
));
5839 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal
,
5840 Ssend_string_to_terminal
, 1, 2, 0,
5841 doc
: /* Send STRING to the terminal without alteration.
5842 Control characters in STRING will have terminal-dependent effects.
5844 Optional parameter TERMINAL specifies the tty terminal device to use.
5845 It may be a terminal object, a frame, or nil for the terminal used by
5846 the currently selected frame. In batch mode, STRING is sent to stdout
5847 when TERMINAL is nil. */)
5848 (Lisp_Object string
, Lisp_Object terminal
)
5850 struct terminal
*t
= get_terminal (terminal
, 1);
5853 /* ??? Perhaps we should do something special for multibyte strings here. */
5854 CHECK_STRING (string
);
5858 error ("Unknown terminal device");
5860 if (t
->type
== output_initial
)
5862 else if (t
->type
!= output_termcap
&& t
->type
!= output_msdos_raw
)
5863 error ("Device %d is not a termcap terminal device", t
->id
);
5866 struct tty_display_info
*tty
= t
->display_info
.tty
;
5869 error ("Terminal is currently suspended");
5871 if (tty
->termscript
)
5873 fwrite (SDATA (string
), 1, SBYTES (string
), tty
->termscript
);
5874 fflush (tty
->termscript
);
5878 fwrite (SDATA (string
), 1, SBYTES (string
), out
);
5885 DEFUN ("ding", Fding
, Sding
, 0, 1, 0,
5886 doc
: /* Beep, or flash the screen.
5887 Also, unless an argument is given,
5888 terminate any keyboard macro currently executing. */)
5896 ring_bell (XFRAME (selected_frame
));
5905 bitch_at_user (void)
5909 else if (!INTERACTIVE
) /* Stop executing a keyboard macro. */
5910 error ("Keyboard macro terminated by a command ringing the bell");
5912 ring_bell (XFRAME (selected_frame
));
5917 /***********************************************************************
5919 ***********************************************************************/
5921 DEFUN ("sleep-for", Fsleep_for
, Ssleep_for
, 1, 2, 0,
5922 doc
: /* Pause, without updating display, for SECONDS seconds.
5923 SECONDS may be a floating-point value, meaning that you can wait for a
5924 fraction of a second. Optional second arg MILLISECONDS specifies an
5925 additional wait period, in milliseconds; this is for backwards compatibility.
5926 \(Not all operating systems support waiting for a fraction of a second.) */)
5927 (Lisp_Object seconds
, Lisp_Object milliseconds
)
5929 double duration
= extract_float (seconds
);
5931 if (!NILP (milliseconds
))
5933 CHECK_NUMBER (milliseconds
);
5934 duration
+= XINT (milliseconds
) / 1000.0;
5939 EMACS_TIME t
= EMACS_TIME_FROM_DOUBLE (duration
);
5940 wait_reading_process_output (min (EMACS_SECS (t
), WAIT_READING_MAX
),
5941 EMACS_NSECS (t
), 0, 0, Qnil
, NULL
, 0);
5948 /* This is just like wait_reading_process_output, except that
5951 TIMEOUT is number of seconds to wait (float or integer),
5952 or t to wait forever.
5953 READING is 1 if reading input.
5954 If DO_DISPLAY is >0 display process output while waiting.
5955 If DO_DISPLAY is >1 perform an initial redisplay before waiting.
5959 sit_for (Lisp_Object timeout
, int reading
, int do_display
)
5964 swallow_events (do_display
);
5966 if ((detect_input_pending_run_timers (do_display
))
5967 || !NILP (Vexecuting_kbd_macro
))
5970 if (do_display
>= 2)
5971 redisplay_preserve_echo_area (2);
5973 if (INTEGERP (timeout
))
5975 sec
= XINT (timeout
);
5980 else if (FLOATP (timeout
))
5982 double seconds
= XFLOAT_DATA (timeout
);
5983 if (! (0 < seconds
))
5987 EMACS_TIME t
= EMACS_TIME_FROM_DOUBLE (seconds
);
5988 sec
= min (EMACS_SECS (t
), WAIT_READING_MAX
);
5989 nsec
= EMACS_NSECS (t
);
5992 else if (EQ (timeout
, Qt
))
5998 wrong_type_argument (Qnumberp
, timeout
);
6005 wait_reading_process_output (sec
, nsec
, reading
? -1 : 1, do_display
,
6008 return detect_input_pending () ? Qnil
: Qt
;
6012 DEFUN ("redisplay", Fredisplay
, Sredisplay
, 0, 1, 0,
6013 doc
: /* Perform redisplay.
6014 Optional arg FORCE, if non-nil, prevents redisplay from being
6015 preempted by arriving input, even if `redisplay-dont-pause' is nil.
6016 If `redisplay-dont-pause' is non-nil (the default), redisplay is never
6017 preempted by arriving input, so FORCE does nothing.
6019 Return t if redisplay was performed, nil if redisplay was preempted
6020 immediately by pending input. */)
6026 if ((detect_input_pending_run_timers (1)
6027 && NILP (force
) && !redisplay_dont_pause
)
6028 || !NILP (Vexecuting_kbd_macro
))
6031 count
= SPECPDL_INDEX ();
6032 if (!NILP (force
) && !redisplay_dont_pause
)
6033 specbind (Qredisplay_dont_pause
, Qt
);
6034 redisplay_preserve_echo_area (2);
6035 unbind_to (count
, Qnil
);
6041 /***********************************************************************
6042 Other Lisp Functions
6043 ***********************************************************************/
6045 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
6046 session's frames, frame names, buffers, buffer-read-only flags, and
6047 buffer-modified-flags. */
6049 static Lisp_Object frame_and_buffer_state
;
6052 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p
,
6053 Sframe_or_buffer_changed_p
, 0, 1, 0,
6054 doc
: /* Return non-nil if the frame and buffer state appears to have changed.
6055 VARIABLE is a variable name whose value is either nil or a state vector
6056 that will be updated to contain all frames and buffers,
6057 aside from buffers whose names start with space,
6058 along with the buffers' read-only and modified flags. This allows a fast
6059 check to see whether buffer menus might need to be recomputed.
6060 If this function returns non-nil, it updates the internal vector to reflect
6063 If VARIABLE is nil, an internal variable is used. Users should not
6064 pass nil for VARIABLE. */)
6065 (Lisp_Object variable
)
6067 Lisp_Object state
, tail
, frame
, buf
;
6068 Lisp_Object
*vecp
, *end
;
6071 if (! NILP (variable
))
6073 CHECK_SYMBOL (variable
);
6074 state
= Fsymbol_value (variable
);
6075 if (! VECTORP (state
))
6079 state
= frame_and_buffer_state
;
6081 vecp
= XVECTOR (state
)->contents
;
6082 end
= vecp
+ ASIZE (state
);
6084 FOR_EACH_FRAME (tail
, frame
)
6088 if (!EQ (*vecp
++, frame
))
6092 if (!EQ (*vecp
++, FVAR (XFRAME (frame
), name
)))
6095 /* Check that the buffer info matches. */
6096 for (tail
= Vbuffer_alist
; CONSP (tail
); tail
= XCDR (tail
))
6098 buf
= XCDR (XCAR (tail
));
6099 /* Ignore buffers that aren't included in buffer lists. */
6100 if (SREF (BVAR (XBUFFER (buf
), name
), 0) == ' ')
6104 if (!EQ (*vecp
++, buf
))
6108 if (!EQ (*vecp
++, BVAR (XBUFFER (buf
), read_only
)))
6112 if (!EQ (*vecp
++, Fbuffer_modified_p (buf
)))
6117 /* Detect deletion of a buffer at the end of the list. */
6118 if (EQ (*vecp
, Qlambda
))
6121 /* Come here if we decide the data has changed. */
6123 /* Count the size we will need.
6124 Start with 1 so there is room for at least one lambda at the end. */
6126 FOR_EACH_FRAME (tail
, frame
)
6128 for (tail
= Vbuffer_alist
; CONSP (tail
); tail
= XCDR (tail
))
6130 /* Reallocate the vector if data has grown to need it,
6131 or if it has shrunk a lot. */
6132 if (! VECTORP (state
)
6133 || n
> ASIZE (state
)
6134 || n
+ 20 < ASIZE (state
) / 2)
6135 /* Add 20 extra so we grow it less often. */
6137 state
= Fmake_vector (make_number (n
+ 20), Qlambda
);
6138 if (! NILP (variable
))
6139 Fset (variable
, state
);
6141 frame_and_buffer_state
= state
;
6144 /* Record the new data in the (possibly reallocated) vector. */
6145 vecp
= XVECTOR (state
)->contents
;
6146 FOR_EACH_FRAME (tail
, frame
)
6149 *vecp
++ = FVAR (XFRAME (frame
), name
);
6151 for (tail
= Vbuffer_alist
; CONSP (tail
); tail
= XCDR (tail
))
6153 buf
= XCDR (XCAR (tail
));
6154 /* Ignore buffers that aren't included in buffer lists. */
6155 if (SREF (BVAR (XBUFFER (buf
), name
), 0) == ' ')
6158 *vecp
++ = BVAR (XBUFFER (buf
), read_only
);
6159 *vecp
++ = Fbuffer_modified_p (buf
);
6161 /* Fill up the vector with lambdas (always at least one). */
6163 while (vecp
- XVECTOR (state
)->contents
6166 /* Make sure we didn't overflow the vector. */
6167 if (vecp
- XVECTOR (state
)->contents
6175 /***********************************************************************
6177 ***********************************************************************/
6179 /* Initialization done when Emacs fork is started, before doing stty.
6180 Determine terminal type and set terminal_driver. Then invoke its
6181 decoding routine to set up variables in the terminal package. */
6186 char *terminal_type
;
6188 /* Construct the space glyph. */
6189 space_glyph
.type
= CHAR_GLYPH
;
6190 SET_CHAR_GLYPH (space_glyph
, ' ', DEFAULT_FACE_ID
, 0);
6191 space_glyph
.charpos
= -1;
6194 cursor_in_echo_area
= 0;
6195 terminal_type
= (char *) 0;
6197 /* Now is the time to initialize this; it's used by init_sys_modes
6199 Vinitial_window_system
= Qnil
;
6201 /* SIGWINCH needs to be handled no matter what display we start
6202 with. Otherwise newly opened tty frames will not resize
6207 #endif /* CANNOT_DUMP */
6208 signal (SIGWINCH
, window_change_signal
);
6209 #endif /* SIGWINCH */
6211 /* If running as a daemon, no need to initialize any frames/terminal. */
6215 /* If the user wants to use a window system, we shouldn't bother
6216 initializing the terminal. This is especially important when the
6217 terminal is so dumb that emacs gives up before and doesn't bother
6218 using the window system.
6220 If the DISPLAY environment variable is set and nonempty,
6221 try to use X, and die with an error message if that doesn't work. */
6223 #ifdef HAVE_X_WINDOWS
6224 if (! inhibit_window_system
&& ! display_arg
)
6227 display
= getenv ("DISPLAY");
6228 display_arg
= (display
!= 0 && *display
!= 0);
6230 if (display_arg
&& !x_display_ok (display
))
6232 fprintf (stderr
, "Display %s unavailable, simulating -nw\n",
6234 inhibit_window_system
= 1;
6238 if (!inhibit_window_system
&& display_arg
)
6240 Vinitial_window_system
= Qx
;
6242 Vwindow_system_version
= make_number (11);
6245 /* In some versions of ncurses,
6246 tputs crashes if we have not called tgetent.
6248 { char b
[2044]; tgetent (b
, "xterm");}
6250 adjust_frame_glyphs_initially ();
6253 #endif /* HAVE_X_WINDOWS */
6256 if (!inhibit_window_system
)
6258 Vinitial_window_system
= Qw32
;
6259 Vwindow_system_version
= make_number (1);
6260 adjust_frame_glyphs_initially ();
6263 #endif /* HAVE_NTGUI */
6266 if (!inhibit_window_system
6272 Vinitial_window_system
= Qns
;
6273 Vwindow_system_version
= make_number (10);
6274 adjust_frame_glyphs_initially ();
6279 /* If no window system has been specified, try to use the terminal. */
6282 fatal ("standard input is not a tty");
6287 terminal_type
= "w32console";
6289 /* Look at the TERM variable. */
6290 terminal_type
= (char *) getenv ("TERM");
6294 #ifdef HAVE_WINDOW_SYSTEM
6295 if (! inhibit_window_system
)
6296 fprintf (stderr
, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6298 #endif /* HAVE_WINDOW_SYSTEM */
6299 fprintf (stderr
, "Please set the environment variable TERM; see `tset'.\n");
6305 struct frame
*f
= XFRAME (selected_frame
);
6307 /* Open a display on the controlling tty. */
6308 t
= init_tty (0, terminal_type
, 1); /* Errors are fatal. */
6310 /* Convert the initial frame to use the new display. */
6311 if (f
->output_method
!= output_initial
)
6313 f
->output_method
= t
->type
;
6316 t
->reference_count
++;
6318 f
->output_data
.tty
->display_info
= &the_only_display_info
;
6320 if (f
->output_method
== output_termcap
)
6321 create_tty_output (f
);
6323 t
->display_info
.tty
->top_frame
= selected_frame
;
6324 change_frame_size (XFRAME (selected_frame
),
6325 FrameRows (t
->display_info
.tty
),
6326 FrameCols (t
->display_info
.tty
), 0, 0, 1);
6328 /* Delete the initial terminal. */
6329 if (--initial_terminal
->reference_count
== 0
6330 && initial_terminal
->delete_terminal_hook
)
6331 (*initial_terminal
->delete_terminal_hook
) (initial_terminal
);
6333 /* Update frame parameters to reflect the new type. */
6334 Fmodify_frame_parameters
6335 (selected_frame
, Fcons (Fcons (Qtty_type
,
6336 Ftty_type (selected_frame
)), Qnil
));
6337 if (t
->display_info
.tty
->name
)
6338 Fmodify_frame_parameters (selected_frame
,
6339 Fcons (Fcons (Qtty
, build_string (t
->display_info
.tty
->name
)),
6342 Fmodify_frame_parameters (selected_frame
, Fcons (Fcons (Qtty
, Qnil
),
6347 struct frame
*sf
= SELECTED_FRAME ();
6348 int width
= FRAME_TOTAL_COLS (sf
);
6349 int height
= FRAME_LINES (sf
);
6351 /* If these sizes are so big they cause overflow, just ignore the
6352 change. It's not clear what better we could do. The rest of
6353 the code assumes that (width + 2) * height * sizeof (struct glyph)
6354 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6355 if (INT_ADD_RANGE_OVERFLOW (width
, 2, INT_MIN
, INT_MAX
)
6356 || INT_MULTIPLY_RANGE_OVERFLOW (width
+ 2, height
, INT_MIN
, INT_MAX
)
6357 || (min (PTRDIFF_MAX
, SIZE_MAX
) / sizeof (struct glyph
)
6358 < (width
+ 2) * height
))
6359 fatal ("screen size %dx%d too big", width
, height
);
6362 adjust_frame_glyphs_initially ();
6363 calculate_costs (XFRAME (selected_frame
));
6365 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6368 && NILP (Vinitial_window_system
))
6370 /* For the initial frame, we don't have any way of knowing what
6371 are the foreground and background colors of the terminal. */
6372 struct frame
*sf
= SELECTED_FRAME ();
6374 FRAME_FOREGROUND_PIXEL (sf
) = FACE_TTY_DEFAULT_FG_COLOR
;
6375 FRAME_BACKGROUND_PIXEL (sf
) = FACE_TTY_DEFAULT_BG_COLOR
;
6376 call0 (intern ("tty-set-up-initial-frame-faces"));
6382 /***********************************************************************
6384 ***********************************************************************/
6386 DEFUN ("internal-show-cursor", Finternal_show_cursor
,
6387 Sinternal_show_cursor
, 2, 2, 0,
6388 doc
: /* Set the cursor-visibility flag of WINDOW to SHOW.
6389 WINDOW nil means use the selected window. SHOW non-nil means
6390 show a cursor in WINDOW in the next redisplay. SHOW nil means
6391 don't show a cursor. */)
6392 (Lisp_Object window
, Lisp_Object show
)
6394 /* Don't change cursor state while redisplaying. This could confuse
6396 if (!redisplaying_p
)
6399 window
= selected_window
;
6401 CHECK_WINDOW (window
);
6403 XWINDOW (window
)->cursor_off_p
= NILP (show
);
6410 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p
,
6411 Sinternal_show_cursor_p
, 0, 1, 0,
6412 doc
: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6413 WINDOW nil or omitted means report on the selected window. */)
6414 (Lisp_Object window
)
6419 window
= selected_window
;
6421 CHECK_WINDOW (window
);
6423 w
= XWINDOW (window
);
6424 return w
->cursor_off_p
? Qnil
: Qt
;
6427 DEFUN ("last-nonminibuffer-frame", Flast_nonminibuf_frame
,
6428 Slast_nonminibuf_frame
, 0, 0, 0,
6429 doc
: /* Value is last nonminibuffer frame. */)
6432 Lisp_Object frame
= Qnil
;
6434 if (last_nonminibuf_frame
)
6435 XSETFRAME (frame
, last_nonminibuf_frame
);
6440 /***********************************************************************
6442 ***********************************************************************/
6445 syms_of_display (void)
6447 defsubr (&Sredraw_frame
);
6448 defsubr (&Sredraw_display
);
6449 defsubr (&Sframe_or_buffer_changed_p
);
6450 defsubr (&Sopen_termscript
);
6452 defsubr (&Sredisplay
);
6453 defsubr (&Ssleep_for
);
6454 defsubr (&Ssend_string_to_terminal
);
6455 defsubr (&Sinternal_show_cursor
);
6456 defsubr (&Sinternal_show_cursor_p
);
6457 defsubr (&Slast_nonminibuf_frame
);
6460 defsubr (&Sdump_redisplay_history
);
6463 frame_and_buffer_state
= Fmake_vector (make_number (20), Qlambda
);
6464 staticpro (&frame_and_buffer_state
);
6466 DEFSYM (Qdisplay_table
, "display-table");
6467 DEFSYM (Qredisplay_dont_pause
, "redisplay-dont-pause");
6469 DEFVAR_INT ("baud-rate", baud_rate
,
6470 doc
: /* The output baud rate of the terminal.
6471 On most systems, changing this value will affect the amount of padding
6472 and the other strategic decisions made during redisplay. */);
6474 DEFVAR_BOOL ("inverse-video", inverse_video
,
6475 doc
: /* Non-nil means invert the entire frame display.
6476 This means everything is in inverse video which otherwise would not be. */);
6478 DEFVAR_BOOL ("visible-bell", visible_bell
,
6479 doc
: /* Non-nil means try to flash the frame to represent a bell.
6481 See also `ring-bell-function'. */);
6483 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter
,
6484 doc
: /* Non-nil means no need to redraw entire frame after suspending.
6485 A non-nil value is useful if the terminal can automatically preserve
6486 Emacs's frame display when you reenter Emacs.
6487 It is up to you to set this variable if your terminal can do that. */);
6489 DEFVAR_LISP ("initial-window-system", Vinitial_window_system
,
6490 doc
: /* Name of the window system that Emacs uses for the first frame.
6491 The value is a symbol:
6492 nil for a termcap frame (a character-only terminal),
6493 'x' for an Emacs frame that is really an X window,
6494 'w32' for an Emacs frame that is a window on MS-Windows display,
6495 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6496 'pc' for a direct-write MS-DOS frame.
6498 Use of this variable as a boolean is deprecated. Instead,
6499 use `display-graphic-p' or any of the other `display-*-p'
6500 predicates which report frame's specific UI-related capabilities. */);
6502 DEFVAR_KBOARD ("window-system", Vwindow_system
,
6503 doc
: /* Name of window system through which the selected frame is displayed.
6504 The value is a symbol:
6505 nil for a termcap frame (a character-only terminal),
6506 'x' for an Emacs frame that is really an X window,
6507 'w32' for an Emacs frame that is a window on MS-Windows display,
6508 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6509 'pc' for a direct-write MS-DOS frame.
6511 Use of this variable as a boolean is deprecated. Instead,
6512 use `display-graphic-p' or any of the other `display-*-p'
6513 predicates which report frame's specific UI-related capabilities. */);
6515 DEFVAR_LISP ("window-system-version", Vwindow_system_version
,
6516 doc
: /* The version number of the window system in use.
6517 For X windows, this is 11. */);
6519 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area
,
6520 doc
: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6522 DEFVAR_LISP ("glyph-table", Vglyph_table
,
6523 doc
: /* Table defining how to output a glyph code to the frame.
6524 If not nil, this is a vector indexed by glyph code to define the glyph.
6525 Each element can be:
6526 integer: a glyph code which this glyph is an alias for.
6527 string: output this glyph using that string (not impl. in X windows).
6528 nil: this glyph mod 524288 is the code of a character to output,
6529 and this glyph / 524288 is the face number (see `face-id') to use
6530 while outputting it. */);
6531 Vglyph_table
= Qnil
;
6533 DEFVAR_LISP ("standard-display-table", Vstandard_display_table
,
6534 doc
: /* Display table to use for buffers that specify none.
6535 See `buffer-display-table' for more information. */);
6536 Vstandard_display_table
= Qnil
;
6538 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause
,
6539 doc
: /* Non-nil means display update isn't paused when input is detected. */);
6540 redisplay_dont_pause
= 1;
6542 #if PERIODIC_PREEMPTION_CHECKING
6543 DEFVAR_LISP ("redisplay-preemption-period", Vredisplay_preemption_period
,
6544 doc
: /* Period in seconds between checking for input during redisplay.
6545 This has an effect only if `redisplay-dont-pause' is nil; in that
6546 case, arriving input preempts redisplay until the input is processed.
6547 If the value is nil, redisplay is never preempted. */);
6548 Vredisplay_preemption_period
= make_float (0.10);
6555 Vinitial_window_system
= Qnil
;
6556 Vwindow_system_version
= Qnil
;