* vc/vc-svn.el (vc-svn-retrieve-tag): Mark unused argument.
[bpt/emacs.git] / src / dispnew.c
1 /* Updating of data structures for redisplay.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22
23 #define DISPEXTERN_INLINE EXTERN_INLINE
24
25 #include "sysstdio.h"
26 #include <unistd.h>
27
28 #include "lisp.h"
29 #include "termchar.h"
30 /* cm.h must come after dispextern.h on Windows. */
31 #include "dispextern.h"
32 #include "cm.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "keyboard.h"
36 #include "frame.h"
37 #include "termhooks.h"
38 #include "window.h"
39 #include "commands.h"
40 #include "disptab.h"
41 #include "indent.h"
42 #include "intervals.h"
43 #include "blockinput.h"
44 #include "process.h"
45
46 #include "syssignal.h"
47
48 #ifdef HAVE_WINDOW_SYSTEM
49 #include TERM_HEADER
50 #endif /* HAVE_WINDOW_SYSTEM */
51
52 #include <errno.h>
53
54 #include <fpending.h>
55 #include <timespec.h>
56
57 #if defined (HAVE_TERM_H) && defined (GNU_LINUX)
58 #include <term.h> /* for tgetent */
59 #endif
60
61 #ifdef WINDOWSNT
62 #include "w32.h"
63 #endif
64 \f
65 /* Structure to pass dimensions around. Used for character bounding
66 boxes, glyph matrix dimensions and alike. */
67
68 struct dim
69 {
70 int width;
71 int height;
72 };
73
74 \f
75 /* Function prototypes. */
76
77 static void update_frame_line (struct frame *, int);
78 static int required_matrix_height (struct window *);
79 static int required_matrix_width (struct window *);
80 static void change_frame_size_1 (struct frame *, int, int, bool, bool, bool);
81 static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
82 static void fill_up_frame_row_with_spaces (struct glyph_row *, int);
83 static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
84 struct window *);
85 static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
86 struct window *);
87 static void adjust_decode_mode_spec_buffer (struct frame *);
88 static void fill_up_glyph_row_with_spaces (struct glyph_row *);
89 static void clear_window_matrices (struct window *, bool);
90 static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
91 static int scrolling_window (struct window *, bool);
92 static bool update_window_line (struct window *, int, bool *);
93 static void mirror_make_current (struct window *, int);
94 #ifdef GLYPH_DEBUG
95 static void check_matrix_pointers (struct glyph_matrix *,
96 struct glyph_matrix *);
97 #endif
98 static void mirror_line_dance (struct window *, int, int, int *, char *);
99 static bool update_window_tree (struct window *, bool);
100 static bool update_window (struct window *, bool);
101 static bool update_frame_1 (struct frame *, bool, bool);
102 static bool scrolling (struct frame *);
103 static void set_window_cursor_after_update (struct window *);
104 static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
105 static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
106
107 /* True upon entry to redisplay means do not assume anything about
108 current contents of actual terminal frame; clear and redraw it. */
109
110 bool frame_garbaged;
111
112 /* True means last display completed. False means it was preempted. */
113
114 bool display_completed;
115
116 Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
117
118 \f
119 /* The currently selected frame. In a single-frame version, this
120 variable always equals the_only_frame. */
121
122 Lisp_Object selected_frame;
123
124 /* A frame which is not just a mini-buffer, or 0 if there are no such
125 frames. This is usually the most recent such frame that was
126 selected. In a single-frame version, this variable always holds
127 the address of the_only_frame. */
128
129 struct frame *last_nonminibuf_frame;
130
131 /* True means SIGWINCH happened when not safe. */
132
133 static bool delayed_size_change;
134
135 /* A glyph for a space. */
136
137 struct glyph space_glyph;
138
139 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
140
141 /* Counts of allocated structures. These counts serve to diagnose
142 memory leaks and double frees. */
143
144 static int glyph_matrix_count;
145 static int glyph_pool_count;
146
147 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
148
149 /* If non-null, the frame whose frame matrices are manipulated. If
150 null, window matrices are worked on. */
151
152 static struct frame *frame_matrix_frame;
153
154 /* Convert vpos and hpos from frame to window and vice versa.
155 This may only be used for terminal frames. */
156
157 #ifdef GLYPH_DEBUG
158
159 static int window_to_frame_vpos (struct window *, int);
160 static int window_to_frame_hpos (struct window *, int);
161 #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
162 #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
163
164 /* One element of the ring buffer containing redisplay history
165 information. */
166
167 struct redisplay_history
168 {
169 char trace[512 + 100];
170 };
171
172 /* The size of the history buffer. */
173
174 #define REDISPLAY_HISTORY_SIZE 30
175
176 /* The redisplay history buffer. */
177
178 static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
179
180 /* Next free entry in redisplay_history. */
181
182 static int history_idx;
183
184 /* A tick that's incremented each time something is added to the
185 history. */
186
187 static uprintmax_t history_tick;
188 \f
189 /* Add to the redisplay history how window W has been displayed.
190 MSG is a trace containing the information how W's glyph matrix
191 has been constructed. PAUSED_P means that the update
192 has been interrupted for pending input. */
193
194 static void
195 add_window_display_history (struct window *w, const char *msg, bool paused_p)
196 {
197 char *buf;
198 void *ptr = w;
199
200 if (history_idx >= REDISPLAY_HISTORY_SIZE)
201 history_idx = 0;
202 buf = redisplay_history[history_idx].trace;
203 ++history_idx;
204
205 snprintf (buf, sizeof redisplay_history[0].trace,
206 "%"pMu": window %p (`%s')%s\n%s",
207 history_tick++,
208 ptr,
209 ((BUFFERP (w->contents)
210 && STRINGP (BVAR (XBUFFER (w->contents), name)))
211 ? SSDATA (BVAR (XBUFFER (w->contents), name))
212 : "???"),
213 paused_p ? " ***paused***" : "",
214 msg);
215 }
216
217
218 /* Add to the redisplay history that frame F has been displayed.
219 PAUSED_P means that the update has been interrupted for
220 pending input. */
221
222 static void
223 add_frame_display_history (struct frame *f, bool paused_p)
224 {
225 char *buf;
226 void *ptr = f;
227
228 if (history_idx >= REDISPLAY_HISTORY_SIZE)
229 history_idx = 0;
230 buf = redisplay_history[history_idx].trace;
231 ++history_idx;
232
233 sprintf (buf, "%"pMu": update frame %p%s",
234 history_tick++,
235 ptr, paused_p ? " ***paused***" : "");
236 }
237
238
239 DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
240 Sdump_redisplay_history, 0, 0, "",
241 doc: /* Dump redisplay history to stderr. */)
242 (void)
243 {
244 int i;
245
246 for (i = history_idx - 1; i != history_idx; --i)
247 {
248 if (i < 0)
249 i = REDISPLAY_HISTORY_SIZE - 1;
250 fprintf (stderr, "%s\n", redisplay_history[i].trace);
251 }
252
253 return Qnil;
254 }
255
256
257 #else /* not GLYPH_DEBUG */
258
259 #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
260 #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
261
262 #endif /* GLYPH_DEBUG */
263
264
265 #if (defined PROFILING \
266 && (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
267 && !HAVE___EXECUTABLE_START)
268 /* This function comes first in the Emacs executable and is used only
269 to estimate the text start for profiling. */
270 void
271 __executable_start (void)
272 {
273 emacs_abort ();
274 }
275 #endif
276 \f
277 /***********************************************************************
278 Glyph Matrices
279 ***********************************************************************/
280
281 /* Allocate and return a glyph_matrix structure. POOL is the glyph
282 pool from which memory for the matrix should be allocated, or null
283 for window-based redisplay where no glyph pools are used. The
284 member `pool' of the glyph matrix structure returned is set to
285 POOL, the structure is otherwise zeroed. */
286
287 static struct glyph_matrix *
288 new_glyph_matrix (struct glyph_pool *pool)
289 {
290 struct glyph_matrix *result = xzalloc (sizeof *result);
291
292 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
293 /* Increment number of allocated matrices. This count is used
294 to detect memory leaks. */
295 ++glyph_matrix_count;
296 #endif
297
298 /* Set pool and return. */
299 result->pool = pool;
300 return result;
301 }
302
303
304 /* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
305
306 If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
307 glyph_matrix_count is decremented when a matrix is freed. If the count
308 gets negative, more structures were freed than allocated, i.e. one matrix
309 was freed more than once or a bogus pointer was passed to this function.
310
311 If MATRIX->pool is null, this means that the matrix manages its own
312 glyph memory---this is done for matrices on X frames. Freeing the
313 matrix also frees the glyph memory in this case. */
314
315 static void
316 free_glyph_matrix (struct glyph_matrix *matrix)
317 {
318 if (matrix)
319 {
320 int i;
321
322 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
323 /* Detect the case that more matrices are freed than were
324 allocated. */
325 --glyph_matrix_count;
326 eassert (glyph_matrix_count >= 0);
327 #endif
328
329 /* Free glyph memory if MATRIX owns it. */
330 if (matrix->pool == NULL)
331 for (i = 0; i < matrix->rows_allocated; ++i)
332 xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
333
334 /* Free row structures and the matrix itself. */
335 xfree (matrix->rows);
336 xfree (matrix);
337 }
338 }
339
340
341 /* Return the number of glyphs to reserve for a marginal area of
342 window W. TOTAL_GLYPHS is the number of glyphs in a complete
343 display line of window W. MARGIN gives the width of the marginal
344 area in canonical character units. */
345
346 static int
347 margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
348 {
349 if (margin > 0)
350 {
351 int width = w->total_cols;
352 double d = max (0, margin);
353 d = min (width / 2 - 1, d);
354 return (int) ((double) total_glyphs / width * d);
355 }
356 return 0;
357 }
358
359 /* Return true if ROW's hash value is correct.
360 Optimized away if ENABLE_CHECKING is not defined. */
361
362 static bool
363 verify_row_hash (struct glyph_row *row)
364 {
365 return row->hash == row_hash (row);
366 }
367
368 /* Adjust glyph matrix MATRIX on window W or on a frame to changed
369 window sizes.
370
371 W is null if the function is called for a frame glyph matrix.
372 Otherwise it is the window MATRIX is a member of. X and Y are the
373 indices of the first column and row of MATRIX within the frame
374 matrix, if such a matrix exists. They are zero for purely
375 window-based redisplay. DIM is the needed size of the matrix.
376
377 In window-based redisplay, where no frame matrices exist, glyph
378 matrices manage their own glyph storage. Otherwise, they allocate
379 storage from a common frame glyph pool which can be found in
380 MATRIX->pool.
381
382 The reason for this memory management strategy is to avoid complete
383 frame redraws if possible. When we allocate from a common pool, a
384 change of the location or size of a sub-matrix within the pool
385 requires a complete redisplay of the frame because we cannot easily
386 make sure that the current matrices of all windows still agree with
387 what is displayed on the screen. While this is usually fast, it
388 leads to screen flickering. */
389
390 static void
391 adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
392 {
393 int i;
394 int new_rows;
395 bool marginal_areas_changed_p = 0;
396 bool header_line_changed_p = 0;
397 bool header_line_p = 0;
398 int left = -1, right = -1;
399 int window_width = -1, window_height = -1;
400
401 /* See if W had a header line that has disappeared now, or vice versa.
402 Get W's size. */
403 if (w)
404 {
405 window_box (w, ANY_AREA, 0, 0, &window_width, &window_height);
406
407 header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
408 header_line_changed_p = header_line_p != matrix->header_line_p;
409 }
410 matrix->header_line_p = header_line_p;
411
412 /* If POOL is null, MATRIX is a window matrix for window-based redisplay.
413 Do nothing if MATRIX' size, position, vscroll, and marginal areas
414 haven't changed. This optimization is important because preserving
415 the matrix means preventing redisplay. */
416 if (matrix->pool == NULL)
417 {
418 left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
419 right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
420 eassert (left >= 0 && right >= 0);
421 marginal_areas_changed_p = (left != matrix->left_margin_glyphs
422 || right != matrix->right_margin_glyphs);
423
424 if (!marginal_areas_changed_p
425 && !XFRAME (w->frame)->fonts_changed
426 && !header_line_changed_p
427 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
428 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
429 && matrix->window_height == window_height
430 && matrix->window_vscroll == w->vscroll
431 && matrix->window_width == window_width)
432 return;
433 }
434
435 /* Enlarge MATRIX->rows if necessary. New rows are cleared. */
436 if (matrix->rows_allocated < dim.height)
437 {
438 int old_alloc = matrix->rows_allocated;
439 new_rows = dim.height - matrix->rows_allocated;
440 matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
441 new_rows, INT_MAX, sizeof *matrix->rows);
442 memset (matrix->rows + old_alloc, 0,
443 (matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
444 }
445 else
446 new_rows = 0;
447
448 /* If POOL is not null, MATRIX is a frame matrix or a window matrix
449 on a frame not using window-based redisplay. Set up pointers for
450 each row into the glyph pool. */
451 if (matrix->pool)
452 {
453 eassert (matrix->pool->glyphs);
454
455 if (w)
456 {
457 left = margin_glyphs_to_reserve (w, dim.width,
458 w->left_margin_cols);
459 right = margin_glyphs_to_reserve (w, dim.width,
460 w->right_margin_cols);
461 }
462 else
463 left = right = 0;
464
465 for (i = 0; i < dim.height; ++i)
466 {
467 struct glyph_row *row = &matrix->rows[i];
468
469 row->glyphs[LEFT_MARGIN_AREA]
470 = (matrix->pool->glyphs
471 + (y + i) * matrix->pool->ncolumns
472 + x);
473
474 if (w == NULL
475 || row == matrix->rows + dim.height - 1
476 || (row == matrix->rows && matrix->header_line_p))
477 {
478 row->glyphs[TEXT_AREA]
479 = row->glyphs[LEFT_MARGIN_AREA];
480 row->glyphs[RIGHT_MARGIN_AREA]
481 = row->glyphs[TEXT_AREA] + dim.width;
482 row->glyphs[LAST_AREA]
483 = row->glyphs[RIGHT_MARGIN_AREA];
484 }
485 else
486 {
487 row->glyphs[TEXT_AREA]
488 = row->glyphs[LEFT_MARGIN_AREA] + left;
489 row->glyphs[RIGHT_MARGIN_AREA]
490 = row->glyphs[TEXT_AREA] + dim.width - left - right;
491 row->glyphs[LAST_AREA]
492 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
493 }
494 }
495
496 matrix->left_margin_glyphs = left;
497 matrix->right_margin_glyphs = right;
498 }
499 else
500 {
501 /* If MATRIX->pool is null, MATRIX is responsible for managing
502 its own memory. It is a window matrix for window-based redisplay.
503 Allocate glyph memory from the heap. */
504 if (dim.width > matrix->matrix_w
505 || new_rows
506 || header_line_changed_p
507 || marginal_areas_changed_p)
508 {
509 struct glyph_row *row = matrix->rows;
510 struct glyph_row *end = row + matrix->rows_allocated;
511
512 while (row < end)
513 {
514 row->glyphs[LEFT_MARGIN_AREA]
515 = xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
516 dim.width, sizeof (struct glyph));
517
518 /* The mode line never has marginal areas. */
519 if (row == matrix->rows + dim.height - 1
520 || (row == matrix->rows && matrix->header_line_p))
521 {
522 row->glyphs[TEXT_AREA]
523 = row->glyphs[LEFT_MARGIN_AREA];
524 row->glyphs[RIGHT_MARGIN_AREA]
525 = row->glyphs[TEXT_AREA] + dim.width;
526 row->glyphs[LAST_AREA]
527 = row->glyphs[RIGHT_MARGIN_AREA];
528 }
529 else
530 {
531 row->glyphs[TEXT_AREA]
532 = row->glyphs[LEFT_MARGIN_AREA] + left;
533 row->glyphs[RIGHT_MARGIN_AREA]
534 = row->glyphs[TEXT_AREA] + dim.width - left - right;
535 row->glyphs[LAST_AREA]
536 = row->glyphs[LEFT_MARGIN_AREA] + dim.width;
537 }
538 ++row;
539 }
540 }
541
542 eassert (left >= 0 && right >= 0);
543 matrix->left_margin_glyphs = left;
544 matrix->right_margin_glyphs = right;
545 }
546
547 /* Number of rows to be used by MATRIX. */
548 matrix->nrows = dim.height;
549 eassert (matrix->nrows >= 0);
550
551 if (w)
552 {
553 if (matrix == w->current_matrix)
554 {
555 /* Mark rows in a current matrix of a window as not having
556 valid contents. It's important to not do this for
557 desired matrices. When Emacs starts, it may already be
558 building desired matrices when this function runs. */
559 if (window_width < 0)
560 window_width = window_box_width (w, -1);
561
562 /* Optimize the case that only the height has changed (C-x 2,
563 upper window). Invalidate all rows that are no longer part
564 of the window. */
565 if (!marginal_areas_changed_p
566 && !header_line_changed_p
567 && new_rows == 0
568 && dim.width == matrix->matrix_w
569 && matrix->window_left_col == WINDOW_LEFT_EDGE_COL (w)
570 && matrix->window_top_line == WINDOW_TOP_EDGE_LINE (w)
571 && matrix->window_width == window_width)
572 {
573 /* Find the last row in the window. */
574 for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
575 if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
576 {
577 ++i;
578 break;
579 }
580
581 /* Window end is invalid, if inside of the rows that
582 are invalidated below. */
583 if (w->window_end_vpos >= i)
584 w->window_end_valid = 0;
585
586 while (i < matrix->nrows)
587 matrix->rows[i++].enabled_p = 0;
588 }
589 else
590 {
591 for (i = 0; i < matrix->nrows; ++i)
592 matrix->rows[i].enabled_p = 0;
593 }
594 }
595 else if (matrix == w->desired_matrix)
596 {
597 /* Rows in desired matrices always have to be cleared;
598 redisplay expects this is the case when it runs, so it
599 had better be the case when we adjust matrices between
600 redisplays. */
601 for (i = 0; i < matrix->nrows; ++i)
602 matrix->rows[i].enabled_p = 0;
603 }
604 }
605
606
607 /* Remember last values to be able to optimize frame redraws. */
608 matrix->matrix_x = x;
609 matrix->matrix_y = y;
610 matrix->matrix_w = dim.width;
611 matrix->matrix_h = dim.height;
612
613 /* Record the top y location and height of W at the time the matrix
614 was last adjusted. This is used to optimize redisplay above. */
615 if (w)
616 {
617 matrix->window_left_col = WINDOW_LEFT_EDGE_COL (w);
618 matrix->window_top_line = WINDOW_TOP_EDGE_LINE (w);
619 matrix->window_height = window_height;
620 matrix->window_width = window_width;
621 matrix->window_vscroll = w->vscroll;
622 }
623 }
624
625
626 /* Reverse the contents of rows in MATRIX between START and END. The
627 contents of the row at END - 1 end up at START, END - 2 at START +
628 1 etc. This is part of the implementation of rotate_matrix (see
629 below). */
630
631 static void
632 reverse_rows (struct glyph_matrix *matrix, int start, int end)
633 {
634 int i, j;
635
636 for (i = start, j = end - 1; i < j; ++i, --j)
637 {
638 /* Non-ISO HP/UX compiler doesn't like auto struct
639 initialization. */
640 struct glyph_row temp;
641 temp = matrix->rows[i];
642 matrix->rows[i] = matrix->rows[j];
643 matrix->rows[j] = temp;
644 }
645 }
646
647
648 /* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
649 1 by BY positions. BY < 0 means rotate left, i.e. towards lower
650 indices. (Note: this does not copy glyphs, only glyph pointers in
651 row structures are moved around).
652
653 The algorithm used for rotating the vector was, I believe, first
654 described by Kernighan. See the vector R as consisting of two
655 sub-vectors AB, where A has length BY for BY >= 0. The result
656 after rotating is then BA. Reverse both sub-vectors to get ArBr
657 and reverse the result to get (ArBr)r which is BA. Similar for
658 rotating right. */
659
660 void
661 rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
662 {
663 if (by < 0)
664 {
665 /* Up (rotate left, i.e. towards lower indices). */
666 by = -by;
667 reverse_rows (matrix, first, first + by);
668 reverse_rows (matrix, first + by, last);
669 reverse_rows (matrix, first, last);
670 }
671 else if (by > 0)
672 {
673 /* Down (rotate right, i.e. towards higher indices). */
674 reverse_rows (matrix, last - by, last);
675 reverse_rows (matrix, first, last - by);
676 reverse_rows (matrix, first, last);
677 }
678 }
679
680
681 /* Increment buffer positions in glyph rows of MATRIX. Do it for rows
682 with indices START <= index < END. Increment positions by DELTA/
683 DELTA_BYTES. */
684
685 void
686 increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
687 ptrdiff_t delta, ptrdiff_t delta_bytes)
688 {
689 /* Check that START and END are reasonable values. */
690 eassert (start >= 0 && start <= matrix->nrows);
691 eassert (end >= 0 && end <= matrix->nrows);
692 eassert (start <= end);
693
694 for (; start < end; ++start)
695 increment_row_positions (matrix->rows + start, delta, delta_bytes);
696 }
697
698
699 /* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
700 START and END are the row indices of the first and last + 1 row to clear. */
701
702 void
703 clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
704 {
705 eassert (start <= end);
706 eassert (start >= 0 && start < matrix->nrows);
707 eassert (end >= 0 && end <= matrix->nrows);
708
709 for (; start < end; ++start)
710 matrix->rows[start].enabled_p = 0;
711 }
712
713
714 /* Clear MATRIX.
715
716 Empty all rows in MATRIX by clearing their enabled_p flags.
717 The function prepare_desired_row will eventually really clear a row
718 when it sees one with a false enabled_p flag.
719
720 Reset update hints to default values. The only update hint
721 currently present is the flag MATRIX->no_scrolling_p. */
722
723 void
724 clear_glyph_matrix (struct glyph_matrix *matrix)
725 {
726 if (matrix)
727 {
728 clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
729 matrix->no_scrolling_p = 0;
730 }
731 }
732
733
734 /* Shift part of the glyph matrix MATRIX of window W up or down.
735 Increment y-positions in glyph rows between START and END by DY,
736 and recompute their visible height. */
737
738 void
739 shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
740 {
741 int min_y, max_y;
742
743 eassert (start <= end);
744 eassert (start >= 0 && start < matrix->nrows);
745 eassert (end >= 0 && end <= matrix->nrows);
746
747 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
748 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
749
750 for (; start < end; ++start)
751 {
752 struct glyph_row *row = &matrix->rows[start];
753
754 row->y += dy;
755 row->visible_height = row->height;
756
757 if (row->y < min_y)
758 row->visible_height -= min_y - row->y;
759 if (row->y + row->height > max_y)
760 row->visible_height -= row->y + row->height - max_y;
761 if (row->fringe_bitmap_periodic_p)
762 row->redraw_fringe_bitmaps_p = 1;
763 }
764 }
765
766
767 /* Mark all rows in current matrices of frame F as invalid. Marking
768 invalid is done by setting enabled_p to zero for all rows in a
769 current matrix. */
770
771 void
772 clear_current_matrices (register struct frame *f)
773 {
774 /* Clear frame current matrix, if we have one. */
775 if (f->current_matrix)
776 clear_glyph_matrix (f->current_matrix);
777
778 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
779 /* Clear the matrix of the menu bar window, if such a window exists.
780 The menu bar window is currently used to display menus on X when
781 no toolkit support is compiled in. */
782 if (WINDOWP (f->menu_bar_window))
783 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
784 #endif
785
786 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
787 /* Clear the matrix of the tool-bar window, if any. */
788 if (WINDOWP (f->tool_bar_window))
789 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
790 #endif
791
792 /* Clear current window matrices. */
793 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
794 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
795 }
796
797
798 /* Clear out all display lines of F for a coming redisplay. */
799
800 void
801 clear_desired_matrices (register struct frame *f)
802 {
803 if (f->desired_matrix)
804 clear_glyph_matrix (f->desired_matrix);
805
806 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
807 if (WINDOWP (f->menu_bar_window))
808 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
809 #endif
810
811 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
812 if (WINDOWP (f->tool_bar_window))
813 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
814 #endif
815
816 /* Do it for window matrices. */
817 eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
818 clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
819 }
820
821
822 /* Clear matrices in window tree rooted in W. If DESIRED_P,
823 clear desired matrices, otherwise clear current matrices. */
824
825 static void
826 clear_window_matrices (struct window *w, bool desired_p)
827 {
828 while (w)
829 {
830 if (WINDOWP (w->contents))
831 clear_window_matrices (XWINDOW (w->contents), desired_p);
832 else
833 {
834 if (desired_p)
835 clear_glyph_matrix (w->desired_matrix);
836 else
837 {
838 clear_glyph_matrix (w->current_matrix);
839 w->window_end_valid = 0;
840 }
841 }
842
843 w = NILP (w->next) ? 0 : XWINDOW (w->next);
844 }
845 }
846
847
848 \f
849 /***********************************************************************
850 Glyph Rows
851
852 See dispextern.h for an overall explanation of glyph rows.
853 ***********************************************************************/
854
855 /* Clear glyph row ROW. Do it in a way that makes it robust against
856 changes in the glyph_row structure, i.e. addition or removal of
857 structure members. */
858
859 static struct glyph_row null_row;
860
861 void
862 clear_glyph_row (struct glyph_row *row)
863 {
864 struct glyph *p[1 + LAST_AREA];
865
866 /* Save pointers. */
867 p[LEFT_MARGIN_AREA] = row->glyphs[LEFT_MARGIN_AREA];
868 p[TEXT_AREA] = row->glyphs[TEXT_AREA];
869 p[RIGHT_MARGIN_AREA] = row->glyphs[RIGHT_MARGIN_AREA];
870 p[LAST_AREA] = row->glyphs[LAST_AREA];
871
872 /* Clear. */
873 *row = null_row;
874
875 /* Restore pointers. */
876 row->glyphs[LEFT_MARGIN_AREA] = p[LEFT_MARGIN_AREA];
877 row->glyphs[TEXT_AREA] = p[TEXT_AREA];
878 row->glyphs[RIGHT_MARGIN_AREA] = p[RIGHT_MARGIN_AREA];
879 row->glyphs[LAST_AREA] = p[LAST_AREA];
880
881 #if 0 /* At some point, some bit-fields of struct glyph were not set,
882 which made glyphs unequal when compared with GLYPH_EQUAL_P.
883 Redisplay outputs such glyphs, and flickering effects were
884 the result. This also depended on the contents of memory
885 returned by xmalloc. If flickering happens again, activate
886 the code below. If the flickering is gone with that, chances
887 are that the flickering has the same reason as here. */
888 memset (p[0], 0, (char *) p[LAST_AREA] - (char *) p[0]);
889 #endif
890 }
891
892
893 /* Make ROW an empty, enabled row of canonical character height,
894 in window W starting at y-position Y. */
895
896 void
897 blank_row (struct window *w, struct glyph_row *row, int y)
898 {
899 int min_y, max_y;
900
901 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
902 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
903
904 clear_glyph_row (row);
905 row->y = y;
906 row->ascent = row->phys_ascent = 0;
907 row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
908 row->visible_height = row->height;
909
910 if (row->y < min_y)
911 row->visible_height -= min_y - row->y;
912 if (row->y + row->height > max_y)
913 row->visible_height -= row->y + row->height - max_y;
914
915 row->enabled_p = 1;
916 }
917
918
919 /* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
920 are the amounts by which to change positions. Note that the first
921 glyph of the text area of a row can have a buffer position even if
922 the used count of the text area is zero. Such rows display line
923 ends. */
924
925 static void
926 increment_row_positions (struct glyph_row *row,
927 ptrdiff_t delta, ptrdiff_t delta_bytes)
928 {
929 int area, i;
930
931 /* Increment start and end positions. */
932 MATRIX_ROW_START_CHARPOS (row) += delta;
933 MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
934 MATRIX_ROW_END_CHARPOS (row) += delta;
935 MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
936 CHARPOS (row->start.pos) += delta;
937 BYTEPOS (row->start.pos) += delta_bytes;
938 CHARPOS (row->end.pos) += delta;
939 BYTEPOS (row->end.pos) += delta_bytes;
940
941 if (!row->enabled_p)
942 return;
943
944 /* Increment positions in glyphs. */
945 for (area = 0; area < LAST_AREA; ++area)
946 for (i = 0; i < row->used[area]; ++i)
947 if (BUFFERP (row->glyphs[area][i].object)
948 && row->glyphs[area][i].charpos > 0)
949 row->glyphs[area][i].charpos += delta;
950
951 /* Capture the case of rows displaying a line end. */
952 if (row->used[TEXT_AREA] == 0
953 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
954 row->glyphs[TEXT_AREA]->charpos += delta;
955 }
956
957
958 #if 0
959 /* Swap glyphs between two glyph rows A and B. This exchanges glyph
960 contents, i.e. glyph structure contents are exchanged between A and
961 B without changing glyph pointers in A and B. */
962
963 static void
964 swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
965 {
966 int area;
967
968 for (area = 0; area < LAST_AREA; ++area)
969 {
970 /* Number of glyphs to swap. */
971 int max_used = max (a->used[area], b->used[area]);
972
973 /* Start of glyphs in area of row A. */
974 struct glyph *glyph_a = a->glyphs[area];
975
976 /* End + 1 of glyphs in area of row A. */
977 struct glyph *glyph_a_end = a->glyphs[max_used];
978
979 /* Start of glyphs in area of row B. */
980 struct glyph *glyph_b = b->glyphs[area];
981
982 while (glyph_a < glyph_a_end)
983 {
984 /* Non-ISO HP/UX compiler doesn't like auto struct
985 initialization. */
986 struct glyph temp;
987 temp = *glyph_a;
988 *glyph_a = *glyph_b;
989 *glyph_b = temp;
990 ++glyph_a;
991 ++glyph_b;
992 }
993 }
994 }
995
996 #endif /* 0 */
997
998 /* Exchange pointers to glyph memory between glyph rows A and B. Also
999 exchange the used[] array and the hash values of the rows, because
1000 these should all go together for the row's hash value to be
1001 correct. */
1002
1003 static void
1004 swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
1005 {
1006 int i;
1007 unsigned hash_tem = a->hash;
1008
1009 for (i = 0; i < LAST_AREA + 1; ++i)
1010 {
1011 struct glyph *temp = a->glyphs[i];
1012
1013 a->glyphs[i] = b->glyphs[i];
1014 b->glyphs[i] = temp;
1015 if (i < LAST_AREA)
1016 {
1017 short used_tem = a->used[i];
1018
1019 a->used[i] = b->used[i];
1020 b->used[i] = used_tem;
1021 }
1022 }
1023 a->hash = b->hash;
1024 b->hash = hash_tem;
1025 }
1026
1027
1028 /* Copy glyph row structure FROM to glyph row structure TO, except
1029 that glyph pointers, the `used' counts, and the hash values in the
1030 structures are left unchanged. */
1031
1032 static void
1033 copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
1034 {
1035 struct glyph *pointers[1 + LAST_AREA];
1036 short used[LAST_AREA];
1037 unsigned hashval;
1038
1039 /* Save glyph pointers of TO. */
1040 memcpy (pointers, to->glyphs, sizeof to->glyphs);
1041 memcpy (used, to->used, sizeof to->used);
1042 hashval = to->hash;
1043
1044 /* Do a structure assignment. */
1045 *to = *from;
1046
1047 /* Restore original pointers of TO. */
1048 memcpy (to->glyphs, pointers, sizeof to->glyphs);
1049 memcpy (to->used, used, sizeof to->used);
1050 to->hash = hashval;
1051 }
1052
1053
1054 /* Assign glyph row FROM to glyph row TO. This works like a structure
1055 assignment TO = FROM, except that glyph pointers are not copied but
1056 exchanged between TO and FROM. Pointers must be exchanged to avoid
1057 a memory leak. */
1058
1059 static void
1060 assign_row (struct glyph_row *to, struct glyph_row *from)
1061 {
1062 swap_glyph_pointers (to, from);
1063 copy_row_except_pointers (to, from);
1064 }
1065
1066
1067 /* Test whether the glyph memory of the glyph row WINDOW_ROW, which is
1068 a row in a window matrix, is a slice of the glyph memory of the
1069 glyph row FRAME_ROW which is a row in a frame glyph matrix. Value
1070 is true if the glyph memory of WINDOW_ROW is part of the glyph
1071 memory of FRAME_ROW. */
1072
1073 #ifdef GLYPH_DEBUG
1074
1075 static bool
1076 glyph_row_slice_p (struct glyph_row *window_row, struct glyph_row *frame_row)
1077 {
1078 struct glyph *window_glyph_start = window_row->glyphs[0];
1079 struct glyph *frame_glyph_start = frame_row->glyphs[0];
1080 struct glyph *frame_glyph_end = frame_row->glyphs[LAST_AREA];
1081
1082 return (frame_glyph_start <= window_glyph_start
1083 && window_glyph_start < frame_glyph_end);
1084 }
1085
1086 #endif /* GLYPH_DEBUG */
1087
1088 #if 0
1089
1090 /* Find the row in the window glyph matrix WINDOW_MATRIX being a slice
1091 of ROW in the frame matrix FRAME_MATRIX. Value is null if no row
1092 in WINDOW_MATRIX is found satisfying the condition. */
1093
1094 static struct glyph_row *
1095 find_glyph_row_slice (struct glyph_matrix *window_matrix,
1096 struct glyph_matrix *frame_matrix, int row)
1097 {
1098 int i;
1099
1100 eassert (row >= 0 && row < frame_matrix->nrows);
1101
1102 for (i = 0; i < window_matrix->nrows; ++i)
1103 if (glyph_row_slice_p (window_matrix->rows + i,
1104 frame_matrix->rows + row))
1105 break;
1106
1107 return i < window_matrix->nrows ? window_matrix->rows + i : 0;
1108 }
1109
1110 #endif /* 0 */
1111
1112 /* Prepare ROW for display. Desired rows are cleared lazily,
1113 i.e. they are only marked as to be cleared by setting their
1114 enabled_p flag to zero. When a row is to be displayed, a prior
1115 call to this function really clears it. */
1116
1117 void
1118 prepare_desired_row (struct glyph_row *row)
1119 {
1120 if (!row->enabled_p)
1121 {
1122 bool rp = row->reversed_p;
1123
1124 clear_glyph_row (row);
1125 row->enabled_p = 1;
1126 row->reversed_p = rp;
1127 }
1128 }
1129
1130
1131 /* Return a hash code for glyph row ROW. */
1132
1133 static int
1134 line_hash_code (struct glyph_row *row)
1135 {
1136 int hash = 0;
1137
1138 if (row->enabled_p)
1139 {
1140 struct glyph *glyph = row->glyphs[TEXT_AREA];
1141 struct glyph *end = glyph + row->used[TEXT_AREA];
1142
1143 while (glyph < end)
1144 {
1145 int c = glyph->u.ch;
1146 int face_id = glyph->face_id;
1147 if (FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1148 c -= SPACEGLYPH;
1149 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c;
1150 hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + face_id;
1151 ++glyph;
1152 }
1153
1154 if (hash == 0)
1155 hash = 1;
1156 }
1157
1158 return hash;
1159 }
1160
1161
1162 /* Return the cost of drawing line VPOS in MATRIX. The cost equals
1163 the number of characters in the line. If must_write_spaces is
1164 zero, leading and trailing spaces are ignored. */
1165
1166 static int
1167 line_draw_cost (struct glyph_matrix *matrix, int vpos)
1168 {
1169 struct glyph_row *row = matrix->rows + vpos;
1170 struct glyph *beg = row->glyphs[TEXT_AREA];
1171 struct glyph *end = beg + row->used[TEXT_AREA];
1172 int len;
1173 Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE;
1174 ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH;
1175
1176 /* Ignore trailing and leading spaces if we can. */
1177 if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */
1178 {
1179 /* Skip from the end over trailing spaces. */
1180 while (end > beg && CHAR_GLYPH_SPACE_P (*(end - 1)))
1181 --end;
1182
1183 /* All blank line. */
1184 if (end == beg)
1185 return 0;
1186
1187 /* Skip over leading spaces. */
1188 while (CHAR_GLYPH_SPACE_P (*beg))
1189 ++beg;
1190 }
1191
1192 /* If we don't have a glyph-table, each glyph is one character,
1193 so return the number of glyphs. */
1194 if (glyph_table_base == 0)
1195 len = end - beg;
1196 else
1197 {
1198 /* Otherwise, scan the glyphs and accumulate their total length
1199 in LEN. */
1200 len = 0;
1201 while (beg < end)
1202 {
1203 GLYPH g;
1204
1205 SET_GLYPH_FROM_CHAR_GLYPH (g, *beg);
1206
1207 if (GLYPH_INVALID_P (g)
1208 || GLYPH_SIMPLE_P (glyph_table_base, glyph_table_len, g))
1209 len += 1;
1210 else
1211 len += GLYPH_LENGTH (glyph_table_base, g);
1212
1213 ++beg;
1214 }
1215 }
1216
1217 return len;
1218 }
1219
1220
1221 /* Return true if the glyph rows A and B have equal contents.
1222 MOUSE_FACE_P means compare the mouse_face_p flags of A and B, too. */
1223
1224 static bool
1225 row_equal_p (struct glyph_row *a, struct glyph_row *b, bool mouse_face_p)
1226 {
1227 eassert (verify_row_hash (a));
1228 eassert (verify_row_hash (b));
1229
1230 if (a == b)
1231 return 1;
1232 else if (a->hash != b->hash)
1233 return 0;
1234 else
1235 {
1236 struct glyph *a_glyph, *b_glyph, *a_end;
1237 int area;
1238
1239 if (mouse_face_p && a->mouse_face_p != b->mouse_face_p)
1240 return 0;
1241
1242 /* Compare glyphs. */
1243 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
1244 {
1245 if (a->used[area] != b->used[area])
1246 return 0;
1247
1248 a_glyph = a->glyphs[area];
1249 a_end = a_glyph + a->used[area];
1250 b_glyph = b->glyphs[area];
1251
1252 while (a_glyph < a_end
1253 && GLYPH_EQUAL_P (a_glyph, b_glyph))
1254 ++a_glyph, ++b_glyph;
1255
1256 if (a_glyph != a_end)
1257 return 0;
1258 }
1259
1260 if (a->fill_line_p != b->fill_line_p
1261 || a->cursor_in_fringe_p != b->cursor_in_fringe_p
1262 || a->left_fringe_bitmap != b->left_fringe_bitmap
1263 || a->left_fringe_face_id != b->left_fringe_face_id
1264 || a->left_fringe_offset != b->left_fringe_offset
1265 || a->right_fringe_bitmap != b->right_fringe_bitmap
1266 || a->right_fringe_face_id != b->right_fringe_face_id
1267 || a->right_fringe_offset != b->right_fringe_offset
1268 || a->fringe_bitmap_periodic_p != b->fringe_bitmap_periodic_p
1269 || a->overlay_arrow_bitmap != b->overlay_arrow_bitmap
1270 || a->exact_window_width_line_p != b->exact_window_width_line_p
1271 || a->overlapped_p != b->overlapped_p
1272 || (MATRIX_ROW_CONTINUATION_LINE_P (a)
1273 != MATRIX_ROW_CONTINUATION_LINE_P (b))
1274 || a->reversed_p != b->reversed_p
1275 /* Different partially visible characters on left margin. */
1276 || a->x != b->x
1277 /* Different height. */
1278 || a->ascent != b->ascent
1279 || a->phys_ascent != b->phys_ascent
1280 || a->phys_height != b->phys_height
1281 || a->visible_height != b->visible_height)
1282 return 0;
1283 }
1284
1285 return 1;
1286 }
1287
1288
1289 \f
1290 /***********************************************************************
1291 Glyph Pool
1292
1293 See dispextern.h for an overall explanation of glyph pools.
1294 ***********************************************************************/
1295
1296 /* Allocate a glyph_pool structure. The structure returned is initialized
1297 with zeros. If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global
1298 variable glyph_pool_count is incremented for each pool allocated. */
1299
1300 static struct glyph_pool *
1301 new_glyph_pool (void)
1302 {
1303 struct glyph_pool *result = xzalloc (sizeof *result);
1304
1305 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1306 /* For memory leak and double deletion checking. */
1307 ++glyph_pool_count;
1308 #endif
1309
1310 return result;
1311 }
1312
1313
1314 /* Free a glyph_pool structure POOL. The function may be called with
1315 a null POOL pointer. If GLYPH_DEBUG and ENABLE_CHECKING are in effect,
1316 global variable glyph_pool_count is decremented with every pool structure
1317 freed. If this count gets negative, more structures were freed than
1318 allocated, i.e. one structure must have been freed more than once or
1319 a bogus pointer was passed to free_glyph_pool. */
1320
1321 static void
1322 free_glyph_pool (struct glyph_pool *pool)
1323 {
1324 if (pool)
1325 {
1326 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
1327 /* More freed than allocated? */
1328 --glyph_pool_count;
1329 eassert (glyph_pool_count >= 0);
1330 #endif
1331 xfree (pool->glyphs);
1332 xfree (pool);
1333 }
1334 }
1335
1336
1337 /* Enlarge a glyph pool POOL. MATRIX_DIM gives the number of rows and
1338 columns we need. This function never shrinks a pool. The only
1339 case in which this would make sense, would be when a frame's size
1340 is changed from a large value to a smaller one. But, if someone
1341 does it once, we can expect that he will do it again.
1342
1343 Return true if the pool changed in a way which makes
1344 re-adjusting window glyph matrices necessary. */
1345
1346 static bool
1347 realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim)
1348 {
1349 ptrdiff_t needed;
1350 bool changed_p;
1351
1352 changed_p = (pool->glyphs == 0
1353 || matrix_dim.height != pool->nrows
1354 || matrix_dim.width != pool->ncolumns);
1355
1356 /* Enlarge the glyph pool. */
1357 needed = matrix_dim.width;
1358 if (INT_MULTIPLY_OVERFLOW (needed, matrix_dim.height))
1359 memory_full (SIZE_MAX);
1360 needed *= matrix_dim.height;
1361 if (needed > pool->nglyphs)
1362 {
1363 ptrdiff_t old_nglyphs = pool->nglyphs;
1364 pool->glyphs = xpalloc (pool->glyphs, &pool->nglyphs,
1365 needed - old_nglyphs, -1, sizeof *pool->glyphs);
1366 memset (pool->glyphs + old_nglyphs, 0,
1367 (pool->nglyphs - old_nglyphs) * sizeof *pool->glyphs);
1368 }
1369
1370 /* Remember the number of rows and columns because (a) we use them
1371 to do sanity checks, and (b) the number of columns determines
1372 where rows in the frame matrix start---this must be available to
1373 determine pointers to rows of window sub-matrices. */
1374 pool->nrows = matrix_dim.height;
1375 pool->ncolumns = matrix_dim.width;
1376
1377 return changed_p;
1378 }
1379
1380
1381 \f
1382 /***********************************************************************
1383 Debug Code
1384 ***********************************************************************/
1385
1386 #ifdef GLYPH_DEBUG
1387
1388
1389 /* Flush standard output. This is sometimes useful to call from the debugger.
1390 XXX Maybe this should be changed to flush the current terminal instead of
1391 stdout.
1392 */
1393
1394 void flush_stdout (void) EXTERNALLY_VISIBLE;
1395
1396 void
1397 flush_stdout (void)
1398 {
1399 fflush (stdout);
1400 }
1401
1402
1403 /* Check that no glyph pointers have been lost in MATRIX. If a
1404 pointer has been lost, e.g. by using a structure assignment between
1405 rows, at least one pointer must occur more than once in the rows of
1406 MATRIX. */
1407
1408 void
1409 check_matrix_pointer_lossage (struct glyph_matrix *matrix)
1410 {
1411 int i, j;
1412
1413 for (i = 0; i < matrix->nrows; ++i)
1414 for (j = 0; j < matrix->nrows; ++j)
1415 eassert (i == j
1416 || (matrix->rows[i].glyphs[TEXT_AREA]
1417 != matrix->rows[j].glyphs[TEXT_AREA]));
1418 }
1419
1420
1421 /* Get a pointer to glyph row ROW in MATRIX, with bounds checks. */
1422
1423 struct glyph_row *
1424 matrix_row (struct glyph_matrix *matrix, int row)
1425 {
1426 eassert (matrix && matrix->rows);
1427 eassert (row >= 0 && row < matrix->nrows);
1428
1429 /* That's really too slow for normal testing because this function
1430 is called almost everywhere. Although---it's still astonishingly
1431 fast, so it is valuable to have for debugging purposes. */
1432 #if 0
1433 check_matrix_pointer_lossage (matrix);
1434 #endif
1435
1436 return matrix->rows + row;
1437 }
1438
1439
1440 #if 0 /* This function makes invalid assumptions when text is
1441 partially invisible. But it might come handy for debugging
1442 nevertheless. */
1443
1444 /* Check invariants that must hold for an up to date current matrix of
1445 window W. */
1446
1447 static void
1448 check_matrix_invariants (struct window *w)
1449 {
1450 struct glyph_matrix *matrix = w->current_matrix;
1451 int yb = window_text_bottom_y (w);
1452 struct glyph_row *row = matrix->rows;
1453 struct glyph_row *last_text_row = NULL;
1454 struct buffer *saved = current_buffer;
1455 struct buffer *buffer = XBUFFER (w->contents);
1456 int c;
1457
1458 /* This can sometimes happen for a fresh window. */
1459 if (matrix->nrows < 2)
1460 return;
1461
1462 set_buffer_temp (buffer);
1463
1464 /* Note: last row is always reserved for the mode line. */
1465 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
1466 && MATRIX_ROW_BOTTOM_Y (row) < yb)
1467 {
1468 struct glyph_row *next = row + 1;
1469
1470 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
1471 last_text_row = row;
1472
1473 /* Check that character and byte positions are in sync. */
1474 eassert (MATRIX_ROW_START_BYTEPOS (row)
1475 == CHAR_TO_BYTE (MATRIX_ROW_START_CHARPOS (row)));
1476 eassert (BYTEPOS (row->start.pos)
1477 == CHAR_TO_BYTE (CHARPOS (row->start.pos)));
1478
1479 /* CHAR_TO_BYTE aborts when invoked for a position > Z. We can
1480 have such a position temporarily in case of a minibuffer
1481 displaying something like `[Sole completion]' at its end. */
1482 if (MATRIX_ROW_END_CHARPOS (row) < BUF_ZV (current_buffer))
1483 {
1484 eassert (MATRIX_ROW_END_BYTEPOS (row)
1485 == CHAR_TO_BYTE (MATRIX_ROW_END_CHARPOS (row)));
1486 eassert (BYTEPOS (row->end.pos)
1487 == CHAR_TO_BYTE (CHARPOS (row->end.pos)));
1488 }
1489
1490 /* Check that end position of `row' is equal to start position
1491 of next row. */
1492 if (next->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (next))
1493 {
1494 eassert (MATRIX_ROW_END_CHARPOS (row)
1495 == MATRIX_ROW_START_CHARPOS (next));
1496 eassert (MATRIX_ROW_END_BYTEPOS (row)
1497 == MATRIX_ROW_START_BYTEPOS (next));
1498 eassert (CHARPOS (row->end.pos) == CHARPOS (next->start.pos));
1499 eassert (BYTEPOS (row->end.pos) == BYTEPOS (next->start.pos));
1500 }
1501 row = next;
1502 }
1503
1504 eassert (w->current_matrix->nrows == w->desired_matrix->nrows);
1505 eassert (w->desired_matrix->rows != NULL);
1506 set_buffer_temp (saved);
1507 }
1508
1509 #endif /* 0 */
1510
1511 #endif /* GLYPH_DEBUG */
1512
1513
1514 \f
1515 /**********************************************************************
1516 Allocating/ Adjusting Glyph Matrices
1517 **********************************************************************/
1518
1519 /* Allocate glyph matrices over a window tree for a frame-based
1520 redisplay
1521
1522 X and Y are column/row within the frame glyph matrix where
1523 sub-matrices for the window tree rooted at WINDOW must be
1524 allocated. DIM_ONLY_P means that the caller of this
1525 function is only interested in the result matrix dimension, and
1526 matrix adjustments should not be performed.
1527
1528 The function returns the total width/height of the sub-matrices of
1529 the window tree. If called on a frame root window, the computation
1530 will take the mini-buffer window into account.
1531
1532 *WINDOW_CHANGE_FLAGS is set to a bit mask with bits
1533
1534 NEW_LEAF_MATRIX set if any window in the tree did not have a
1535 glyph matrices yet, and
1536
1537 CHANGED_LEAF_MATRIX set if the dimension or location of a matrix of
1538 any window in the tree will be changed or have been changed (see
1539 DIM_ONLY_P)
1540
1541 *WINDOW_CHANGE_FLAGS must be initialized by the caller of this
1542 function.
1543
1544 Windows are arranged into chains of windows on the same level
1545 through the next fields of window structures. Such a level can be
1546 either a sequence of horizontally adjacent windows from left to
1547 right, or a sequence of vertically adjacent windows from top to
1548 bottom. Each window in a horizontal sequence can be either a leaf
1549 window or a vertical sequence; a window in a vertical sequence can
1550 be either a leaf or a horizontal sequence. All windows in a
1551 horizontal sequence have the same height, and all windows in a
1552 vertical sequence have the same width.
1553
1554 This function uses, for historical reasons, a more general
1555 algorithm to determine glyph matrix dimensions that would be
1556 necessary.
1557
1558 The matrix height of a horizontal sequence is determined by the
1559 maximum height of any matrix in the sequence. The matrix width of
1560 a horizontal sequence is computed by adding up matrix widths of
1561 windows in the sequence.
1562
1563 |<------- result width ------->|
1564 +---------+----------+---------+ ---
1565 | | | | |
1566 | | | |
1567 +---------+ | | result height
1568 | +---------+
1569 | | |
1570 +----------+ ---
1571
1572 The matrix width of a vertical sequence is the maximum matrix width
1573 of any window in the sequence. Its height is computed by adding up
1574 matrix heights of windows in the sequence.
1575
1576 |<---- result width -->|
1577 +---------+ ---
1578 | | |
1579 | | |
1580 +---------+--+ |
1581 | | |
1582 | | result height
1583 | |
1584 +------------+---------+ |
1585 | | |
1586 | | |
1587 +------------+---------+ --- */
1588
1589 /* Bit indicating that a new matrix will be allocated or has been
1590 allocated. */
1591
1592 #define NEW_LEAF_MATRIX (1 << 0)
1593
1594 /* Bit indicating that a matrix will or has changed its location or
1595 size. */
1596
1597 #define CHANGED_LEAF_MATRIX (1 << 1)
1598
1599 static struct dim
1600 allocate_matrices_for_frame_redisplay (Lisp_Object window, int x, int y,
1601 bool dim_only_p, int *window_change_flags)
1602 {
1603 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1604 int x0 = x, y0 = y;
1605 int wmax = 0, hmax = 0;
1606 struct dim total;
1607 struct dim dim;
1608 struct window *w;
1609 bool in_horz_combination_p;
1610
1611 /* What combination is WINDOW part of? Compute this once since the
1612 result is the same for all windows in the `next' chain. The
1613 special case of a root window (parent equal to nil) is treated
1614 like a vertical combination because a root window's `next'
1615 points to the mini-buffer window, if any, which is arranged
1616 vertically below other windows. */
1617 in_horz_combination_p
1618 = (!NILP (XWINDOW (window)->parent)
1619 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (XWINDOW (window)->parent)));
1620
1621 /* For WINDOW and all windows on the same level. */
1622 do
1623 {
1624 w = XWINDOW (window);
1625
1626 /* Get the dimension of the window sub-matrix for W, depending
1627 on whether this is a combination or a leaf window. */
1628 if (WINDOWP (w->contents))
1629 dim = allocate_matrices_for_frame_redisplay (w->contents, x, y,
1630 dim_only_p,
1631 window_change_flags);
1632 else
1633 {
1634 /* If not already done, allocate sub-matrix structures. */
1635 if (w->desired_matrix == NULL)
1636 {
1637 w->desired_matrix = new_glyph_matrix (f->desired_pool);
1638 w->current_matrix = new_glyph_matrix (f->current_pool);
1639 *window_change_flags |= NEW_LEAF_MATRIX;
1640 }
1641
1642 /* Width and height MUST be chosen so that there are no
1643 holes in the frame matrix. */
1644 dim.width = required_matrix_width (w);
1645 dim.height = required_matrix_height (w);
1646
1647 /* Will matrix be re-allocated? */
1648 if (x != w->desired_matrix->matrix_x
1649 || y != w->desired_matrix->matrix_y
1650 || dim.width != w->desired_matrix->matrix_w
1651 || dim.height != w->desired_matrix->matrix_h
1652 || (margin_glyphs_to_reserve (w, dim.width,
1653 w->left_margin_cols)
1654 != w->desired_matrix->left_margin_glyphs)
1655 || (margin_glyphs_to_reserve (w, dim.width,
1656 w->right_margin_cols)
1657 != w->desired_matrix->right_margin_glyphs))
1658 *window_change_flags |= CHANGED_LEAF_MATRIX;
1659
1660 /* Actually change matrices, if allowed. Do not consider
1661 CHANGED_LEAF_MATRIX computed above here because the pool
1662 may have been changed which we don't now here. We trust
1663 that we only will be called with DIM_ONLY_P when
1664 necessary. */
1665 if (!dim_only_p)
1666 {
1667 adjust_glyph_matrix (w, w->desired_matrix, x, y, dim);
1668 adjust_glyph_matrix (w, w->current_matrix, x, y, dim);
1669 }
1670 }
1671
1672 /* If we are part of a horizontal combination, advance x for
1673 windows to the right of W; otherwise advance y for windows
1674 below W. */
1675 if (in_horz_combination_p)
1676 x += dim.width;
1677 else
1678 y += dim.height;
1679
1680 /* Remember maximum glyph matrix dimensions. */
1681 wmax = max (wmax, dim.width);
1682 hmax = max (hmax, dim.height);
1683
1684 /* Next window on same level. */
1685 window = w->next;
1686 }
1687 while (!NILP (window));
1688
1689 /* Set `total' to the total glyph matrix dimension of this window
1690 level. In a vertical combination, the width is the width of the
1691 widest window; the height is the y we finally reached, corrected
1692 by the y we started with. In a horizontal combination, the total
1693 height is the height of the tallest window, and the width is the
1694 x we finally reached, corrected by the x we started with. */
1695 if (in_horz_combination_p)
1696 {
1697 total.width = x - x0;
1698 total.height = hmax;
1699 }
1700 else
1701 {
1702 total.width = wmax;
1703 total.height = y - y0;
1704 }
1705
1706 return total;
1707 }
1708
1709
1710 /* Return the required height of glyph matrices for window W. */
1711
1712 static int
1713 required_matrix_height (struct window *w)
1714 {
1715 #ifdef HAVE_WINDOW_SYSTEM
1716 struct frame *f = XFRAME (w->frame);
1717
1718 if (FRAME_WINDOW_P (f))
1719 {
1720 int ch_height = FRAME_SMALLEST_FONT_HEIGHT (f);
1721 int window_pixel_height = window_box_height (w) + eabs (w->vscroll);
1722 return (((window_pixel_height + ch_height - 1)
1723 / ch_height) * w->nrows_scale_factor
1724 /* One partially visible line at the top and
1725 bottom of the window. */
1726 + 2
1727 /* 2 for header and mode line. */
1728 + 2);
1729 }
1730 #endif /* HAVE_WINDOW_SYSTEM */
1731
1732 return WINDOW_TOTAL_LINES (w);
1733 }
1734
1735
1736 /* Return the required width of glyph matrices for window W. */
1737
1738 static int
1739 required_matrix_width (struct window *w)
1740 {
1741 #ifdef HAVE_WINDOW_SYSTEM
1742 struct frame *f = XFRAME (w->frame);
1743 if (FRAME_WINDOW_P (f))
1744 {
1745 int ch_width = FRAME_SMALLEST_CHAR_WIDTH (f);
1746 int window_pixel_width = WINDOW_TOTAL_WIDTH (w);
1747
1748 /* Compute number of glyphs needed in a glyph row. */
1749 return (((window_pixel_width + ch_width - 1)
1750 / ch_width) * w->ncols_scale_factor
1751 /* 2 partially visible columns in the text area. */
1752 + 2
1753 /* One partially visible column at the right
1754 edge of each marginal area. */
1755 + 1 + 1);
1756 }
1757 #endif /* HAVE_WINDOW_SYSTEM */
1758
1759 return w->total_cols;
1760 }
1761
1762
1763 /* Allocate window matrices for window-based redisplay. W is the
1764 window whose matrices must be allocated/reallocated. */
1765
1766 static void
1767 allocate_matrices_for_window_redisplay (struct window *w)
1768 {
1769 while (w)
1770 {
1771 if (WINDOWP (w->contents))
1772 allocate_matrices_for_window_redisplay (XWINDOW (w->contents));
1773 else
1774 {
1775 /* W is a leaf window. */
1776 struct dim dim;
1777
1778 /* If matrices are not yet allocated, allocate them now. */
1779 if (w->desired_matrix == NULL)
1780 {
1781 w->desired_matrix = new_glyph_matrix (NULL);
1782 w->current_matrix = new_glyph_matrix (NULL);
1783 }
1784
1785 dim.width = required_matrix_width (w);
1786 dim.height = required_matrix_height (w);
1787 adjust_glyph_matrix (w, w->desired_matrix, 0, 0, dim);
1788 adjust_glyph_matrix (w, w->current_matrix, 0, 0, dim);
1789 }
1790
1791 w = NILP (w->next) ? NULL : XWINDOW (w->next);
1792 }
1793 }
1794
1795 /* Allocate/reallocate glyph matrices of a single frame F.
1796 This function must be called when a new frame is created,
1797 its size changes, or its window configuration changes. */
1798
1799 void
1800 adjust_frame_glyphs (struct frame *f)
1801 {
1802 /* Block input so that expose events and other events that access
1803 glyph matrices are not processed while we are changing them. */
1804 block_input ();
1805
1806 if (FRAME_WINDOW_P (f))
1807 adjust_frame_glyphs_for_window_redisplay (f);
1808 else
1809 adjust_frame_glyphs_for_frame_redisplay (f);
1810
1811 /* Don't forget the buffer for decode_mode_spec. */
1812 adjust_decode_mode_spec_buffer (f);
1813
1814 f->glyphs_initialized_p = 1;
1815
1816 unblock_input ();
1817 }
1818
1819 /* Return true if any window in the tree has nonzero window margins. See
1820 the hack at the end of adjust_frame_glyphs_for_frame_redisplay. */
1821 static bool
1822 showing_window_margins_p (struct window *w)
1823 {
1824 while (w)
1825 {
1826 if (WINDOWP (w->contents))
1827 {
1828 if (showing_window_margins_p (XWINDOW (w->contents)))
1829 return 1;
1830 }
1831 else if (w->left_margin_cols > 0 || w->right_margin_cols > 0)
1832 return 1;
1833
1834 w = NILP (w->next) ? 0 : XWINDOW (w->next);
1835 }
1836 return 0;
1837 }
1838
1839
1840 /* In the window tree with root W, build current matrices of leaf
1841 windows from the frame's current matrix. */
1842
1843 static void
1844 fake_current_matrices (Lisp_Object window)
1845 {
1846 struct window *w;
1847
1848 for (; !NILP (window); window = w->next)
1849 {
1850 w = XWINDOW (window);
1851
1852 if (WINDOWP (w->contents))
1853 fake_current_matrices (w->contents);
1854 else
1855 {
1856 int i;
1857 struct frame *f = XFRAME (w->frame);
1858 struct glyph_matrix *m = w->current_matrix;
1859 struct glyph_matrix *fm = f->current_matrix;
1860
1861 eassert (m->matrix_h == WINDOW_TOTAL_LINES (w));
1862 eassert (m->matrix_w == WINDOW_TOTAL_COLS (w));
1863
1864 for (i = 0; i < m->matrix_h; ++i)
1865 {
1866 struct glyph_row *r = m->rows + i;
1867 struct glyph_row *fr = fm->rows + i + WINDOW_TOP_EDGE_LINE (w);
1868
1869 eassert (r->glyphs[TEXT_AREA] >= fr->glyphs[TEXT_AREA]
1870 && r->glyphs[LAST_AREA] <= fr->glyphs[LAST_AREA]);
1871
1872 r->enabled_p = fr->enabled_p;
1873 if (r->enabled_p)
1874 {
1875 r->used[LEFT_MARGIN_AREA] = m->left_margin_glyphs;
1876 r->used[RIGHT_MARGIN_AREA] = m->right_margin_glyphs;
1877 r->used[TEXT_AREA] = (m->matrix_w
1878 - r->used[LEFT_MARGIN_AREA]
1879 - r->used[RIGHT_MARGIN_AREA]);
1880 r->mode_line_p = 0;
1881 }
1882 }
1883 }
1884 }
1885 }
1886
1887
1888 /* Save away the contents of frame F's current frame matrix. Value is
1889 a glyph matrix holding the contents of F's current frame matrix. */
1890
1891 static struct glyph_matrix *
1892 save_current_matrix (struct frame *f)
1893 {
1894 int i;
1895 struct glyph_matrix *saved = xzalloc (sizeof *saved);
1896 saved->nrows = f->current_matrix->nrows;
1897 saved->rows = xzalloc (saved->nrows * sizeof *saved->rows);
1898
1899 for (i = 0; i < saved->nrows; ++i)
1900 {
1901 struct glyph_row *from = f->current_matrix->rows + i;
1902 struct glyph_row *to = saved->rows + i;
1903 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1904 to->glyphs[TEXT_AREA] = xmalloc (nbytes);
1905 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1906 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1907 }
1908
1909 return saved;
1910 }
1911
1912
1913 /* Restore the contents of frame F's current frame matrix from SAVED,
1914 and free memory associated with SAVED. */
1915
1916 static void
1917 restore_current_matrix (struct frame *f, struct glyph_matrix *saved)
1918 {
1919 int i;
1920
1921 for (i = 0; i < saved->nrows; ++i)
1922 {
1923 struct glyph_row *from = saved->rows + i;
1924 struct glyph_row *to = f->current_matrix->rows + i;
1925 ptrdiff_t nbytes = from->used[TEXT_AREA] * sizeof (struct glyph);
1926 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA], nbytes);
1927 to->used[TEXT_AREA] = from->used[TEXT_AREA];
1928 xfree (from->glyphs[TEXT_AREA]);
1929 }
1930
1931 xfree (saved->rows);
1932 xfree (saved);
1933 }
1934
1935
1936
1937 /* Allocate/reallocate glyph matrices of a single frame F for
1938 frame-based redisplay. */
1939
1940 static void
1941 adjust_frame_glyphs_for_frame_redisplay (struct frame *f)
1942 {
1943 struct dim matrix_dim;
1944 bool pool_changed_p;
1945 int window_change_flags;
1946 int top_window_y;
1947
1948 if (!FRAME_LIVE_P (f))
1949 return;
1950
1951 top_window_y = FRAME_TOP_MARGIN (f);
1952
1953 /* Allocate glyph pool structures if not already done. */
1954 if (f->desired_pool == NULL)
1955 {
1956 f->desired_pool = new_glyph_pool ();
1957 f->current_pool = new_glyph_pool ();
1958 }
1959
1960 /* Allocate frames matrix structures if needed. */
1961 if (f->desired_matrix == NULL)
1962 {
1963 f->desired_matrix = new_glyph_matrix (f->desired_pool);
1964 f->current_matrix = new_glyph_matrix (f->current_pool);
1965 }
1966
1967 /* Compute window glyph matrices. (This takes the mini-buffer
1968 window into account). The result is the size of the frame glyph
1969 matrix needed. The variable window_change_flags is set to a bit
1970 mask indicating whether new matrices will be allocated or
1971 existing matrices change their size or location within the frame
1972 matrix. */
1973 window_change_flags = 0;
1974 matrix_dim
1975 = allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
1976 0, top_window_y,
1977 1,
1978 &window_change_flags);
1979
1980 /* Add in menu bar lines, if any. */
1981 matrix_dim.height += top_window_y;
1982
1983 /* Enlarge pools as necessary. */
1984 pool_changed_p = realloc_glyph_pool (f->desired_pool, matrix_dim);
1985 realloc_glyph_pool (f->current_pool, matrix_dim);
1986
1987 /* Set up glyph pointers within window matrices. Do this only if
1988 absolutely necessary since it requires a frame redraw. */
1989 if (pool_changed_p || window_change_flags)
1990 {
1991 /* Do it for window matrices. */
1992 allocate_matrices_for_frame_redisplay (FRAME_ROOT_WINDOW (f),
1993 0, top_window_y, 0,
1994 &window_change_flags);
1995
1996 /* Size of frame matrices must equal size of frame. Note
1997 that we are called for X frames with window widths NOT equal
1998 to the frame width (from CHANGE_FRAME_SIZE_1). */
1999 eassert (matrix_dim.width == FRAME_COLS (f)
2000 && matrix_dim.height == FRAME_LINES (f));
2001
2002 /* Pointers to glyph memory in glyph rows are exchanged during
2003 the update phase of redisplay, which means in general that a
2004 frame's current matrix consists of pointers into both the
2005 desired and current glyph pool of the frame. Adjusting a
2006 matrix sets the frame matrix up so that pointers are all into
2007 the same pool. If we want to preserve glyph contents of the
2008 current matrix over a call to adjust_glyph_matrix, we must
2009 make a copy of the current glyphs, and restore the current
2010 matrix' contents from that copy. */
2011 if (display_completed
2012 && !FRAME_GARBAGED_P (f)
2013 && matrix_dim.width == f->current_matrix->matrix_w
2014 && matrix_dim.height == f->current_matrix->matrix_h
2015 /* For some reason, the frame glyph matrix gets corrupted if
2016 any of the windows contain margins. I haven't been able
2017 to hunt down the reason, but for the moment this prevents
2018 the problem from manifesting. -- cyd */
2019 && !showing_window_margins_p (XWINDOW (FRAME_ROOT_WINDOW (f))))
2020 {
2021 struct glyph_matrix *copy = save_current_matrix (f);
2022 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2023 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2024 restore_current_matrix (f, copy);
2025 fake_current_matrices (FRAME_ROOT_WINDOW (f));
2026 }
2027 else
2028 {
2029 adjust_glyph_matrix (NULL, f->desired_matrix, 0, 0, matrix_dim);
2030 adjust_glyph_matrix (NULL, f->current_matrix, 0, 0, matrix_dim);
2031 SET_FRAME_GARBAGED (f);
2032 }
2033 }
2034 }
2035
2036
2037 /* Allocate/reallocate glyph matrices of a single frame F for
2038 window-based redisplay. */
2039
2040 static void
2041 adjust_frame_glyphs_for_window_redisplay (struct frame *f)
2042 {
2043 eassert (FRAME_WINDOW_P (f) && FRAME_LIVE_P (f));
2044
2045 /* Allocate/reallocate window matrices. */
2046 allocate_matrices_for_window_redisplay (XWINDOW (FRAME_ROOT_WINDOW (f)));
2047
2048 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2049 /* Allocate/ reallocate matrices of the dummy window used to display
2050 the menu bar under X when no X toolkit support is available. */
2051 {
2052 /* Allocate a dummy window if not already done. */
2053 struct window *w;
2054 if (NILP (f->menu_bar_window))
2055 {
2056 Lisp_Object frame;
2057 fset_menu_bar_window (f, make_window ());
2058 w = XWINDOW (f->menu_bar_window);
2059 XSETFRAME (frame, f);
2060 wset_frame (w, frame);
2061 w->pseudo_window_p = 1;
2062 }
2063 else
2064 w = XWINDOW (f->menu_bar_window);
2065
2066 /* Set window dimensions to frame dimensions and allocate or
2067 adjust glyph matrices of W. */
2068 w->top_line = 0;
2069 w->left_col = 0;
2070 w->total_lines = FRAME_MENU_BAR_LINES (f);
2071 w->total_cols = FRAME_TOTAL_COLS (f);
2072 allocate_matrices_for_window_redisplay (w);
2073 }
2074 #endif
2075
2076 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
2077 {
2078 /* Allocate/ reallocate matrices of the tool bar window. If we
2079 don't have a tool bar window yet, make one. */
2080 struct window *w;
2081 if (NILP (f->tool_bar_window))
2082 {
2083 Lisp_Object frame;
2084 fset_tool_bar_window (f, make_window ());
2085 w = XWINDOW (f->tool_bar_window);
2086 XSETFRAME (frame, f);
2087 wset_frame (w, frame);
2088 w->pseudo_window_p = 1;
2089 }
2090 else
2091 w = XWINDOW (f->tool_bar_window);
2092
2093 w->top_line = FRAME_MENU_BAR_LINES (f);
2094 w->left_col = 0;
2095 w->total_lines = FRAME_TOOL_BAR_LINES (f);
2096 w->total_cols = FRAME_TOTAL_COLS (f);
2097 allocate_matrices_for_window_redisplay (w);
2098 }
2099 #endif
2100 }
2101
2102
2103 /* Re-allocate buffer for decode_mode_spec on frame F. */
2104
2105 static void
2106 adjust_decode_mode_spec_buffer (struct frame *f)
2107 {
2108 f->decode_mode_spec_buffer = xrealloc (f->decode_mode_spec_buffer,
2109 FRAME_MESSAGE_BUF_SIZE (f) + 1);
2110 }
2111
2112
2113 \f
2114 /**********************************************************************
2115 Freeing Glyph Matrices
2116 **********************************************************************/
2117
2118 /* Free glyph memory for a frame F. F may be null. This function can
2119 be called for the same frame more than once. The root window of
2120 F may be nil when this function is called. This is the case when
2121 the function is called when F is destroyed. */
2122
2123 void
2124 free_glyphs (struct frame *f)
2125 {
2126 if (f && f->glyphs_initialized_p)
2127 {
2128 /* Block interrupt input so that we don't get surprised by an X
2129 event while we're in an inconsistent state. */
2130 block_input ();
2131 f->glyphs_initialized_p = 0;
2132
2133 /* Release window sub-matrices. */
2134 if (!NILP (f->root_window))
2135 free_window_matrices (XWINDOW (f->root_window));
2136
2137 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
2138 /* Free the dummy window for menu bars without X toolkit and its
2139 glyph matrices. */
2140 if (!NILP (f->menu_bar_window))
2141 {
2142 struct window *w = XWINDOW (f->menu_bar_window);
2143 free_glyph_matrix (w->desired_matrix);
2144 free_glyph_matrix (w->current_matrix);
2145 w->desired_matrix = w->current_matrix = NULL;
2146 fset_menu_bar_window (f, Qnil);
2147 }
2148 #endif
2149
2150 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
2151 /* Free the tool bar window and its glyph matrices. */
2152 if (!NILP (f->tool_bar_window))
2153 {
2154 struct window *w = XWINDOW (f->tool_bar_window);
2155 free_glyph_matrix (w->desired_matrix);
2156 free_glyph_matrix (w->current_matrix);
2157 w->desired_matrix = w->current_matrix = NULL;
2158 fset_tool_bar_window (f, Qnil);
2159 }
2160 #endif
2161
2162 /* Release frame glyph matrices. Reset fields to zero in
2163 case we are called a second time. */
2164 if (f->desired_matrix)
2165 {
2166 free_glyph_matrix (f->desired_matrix);
2167 free_glyph_matrix (f->current_matrix);
2168 f->desired_matrix = f->current_matrix = NULL;
2169 }
2170
2171 /* Release glyph pools. */
2172 if (f->desired_pool)
2173 {
2174 free_glyph_pool (f->desired_pool);
2175 free_glyph_pool (f->current_pool);
2176 f->desired_pool = f->current_pool = NULL;
2177 }
2178
2179 unblock_input ();
2180 }
2181 }
2182
2183
2184 /* Free glyph sub-matrices in the window tree rooted at W. This
2185 function may be called with a null pointer, and it may be called on
2186 the same tree more than once. */
2187
2188 void
2189 free_window_matrices (struct window *w)
2190 {
2191 while (w)
2192 {
2193 if (WINDOWP (w->contents))
2194 free_window_matrices (XWINDOW (w->contents));
2195 else
2196 {
2197 /* This is a leaf window. Free its memory and reset fields
2198 to zero in case this function is called a second time for
2199 W. */
2200 free_glyph_matrix (w->current_matrix);
2201 free_glyph_matrix (w->desired_matrix);
2202 w->current_matrix = w->desired_matrix = NULL;
2203 }
2204
2205 /* Next window on same level. */
2206 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2207 }
2208 }
2209
2210
2211 /* Check glyph memory leaks. This function is called from
2212 shut_down_emacs. Note that frames are not destroyed when Emacs
2213 exits. We therefore free all glyph memory for all active frames
2214 explicitly and check that nothing is left allocated. */
2215
2216 void
2217 check_glyph_memory (void)
2218 {
2219 Lisp_Object tail, frame;
2220
2221 /* Free glyph memory for all frames. */
2222 FOR_EACH_FRAME (tail, frame)
2223 free_glyphs (XFRAME (frame));
2224
2225 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2226 /* Check that nothing is left allocated. */
2227 eassert (glyph_matrix_count == 0);
2228 eassert (glyph_pool_count == 0);
2229 #endif
2230 }
2231
2232
2233 \f
2234 /**********************************************************************
2235 Building a Frame Matrix
2236 **********************************************************************/
2237
2238 /* Most of the redisplay code works on glyph matrices attached to
2239 windows. This is a good solution most of the time, but it is not
2240 suitable for terminal code. Terminal output functions cannot rely
2241 on being able to set an arbitrary terminal window. Instead they
2242 must be provided with a view of the whole frame, i.e. the whole
2243 screen. We build such a view by constructing a frame matrix from
2244 window matrices in this section.
2245
2246 Windows that must be updated have their must_be_updated_p flag set.
2247 For all such windows, their desired matrix is made part of the
2248 desired frame matrix. For other windows, their current matrix is
2249 made part of the desired frame matrix.
2250
2251 +-----------------+----------------+
2252 | desired | desired |
2253 | | |
2254 +-----------------+----------------+
2255 | current |
2256 | |
2257 +----------------------------------+
2258
2259 Desired window matrices can be made part of the frame matrix in a
2260 cheap way: We exploit the fact that the desired frame matrix and
2261 desired window matrices share their glyph memory. This is not
2262 possible for current window matrices. Their glyphs are copied to
2263 the desired frame matrix. The latter is equivalent to
2264 preserve_other_columns in the old redisplay.
2265
2266 Used glyphs counters for frame matrix rows are the result of adding
2267 up glyph lengths of the window matrices. A line in the frame
2268 matrix is enabled, if a corresponding line in a window matrix is
2269 enabled.
2270
2271 After building the desired frame matrix, it will be passed to
2272 terminal code, which will manipulate both the desired and current
2273 frame matrix. Changes applied to the frame's current matrix have
2274 to be visible in current window matrices afterwards, of course.
2275
2276 This problem is solved like this:
2277
2278 1. Window and frame matrices share glyphs. Window matrices are
2279 constructed in a way that their glyph contents ARE the glyph
2280 contents needed in a frame matrix. Thus, any modification of
2281 glyphs done in terminal code will be reflected in window matrices
2282 automatically.
2283
2284 2. Exchanges of rows in a frame matrix done by terminal code are
2285 intercepted by hook functions so that corresponding row operations
2286 on window matrices can be performed. This is necessary because we
2287 use pointers to glyphs in glyph row structures. To satisfy the
2288 assumption of point 1 above that glyphs are updated implicitly in
2289 window matrices when they are manipulated via the frame matrix,
2290 window and frame matrix must of course agree where to find the
2291 glyphs for their rows. Possible manipulations that must be
2292 mirrored are assignments of rows of the desired frame matrix to the
2293 current frame matrix and scrolling the current frame matrix. */
2294
2295 /* Build frame F's desired matrix from window matrices. Only windows
2296 which have the flag must_be_updated_p set have to be updated. Menu
2297 bar lines of a frame are not covered by window matrices, so make
2298 sure not to touch them in this function. */
2299
2300 static void
2301 build_frame_matrix (struct frame *f)
2302 {
2303 int i;
2304
2305 /* F must have a frame matrix when this function is called. */
2306 eassert (!FRAME_WINDOW_P (f));
2307
2308 /* Clear all rows in the frame matrix covered by window matrices.
2309 Menu bar lines are not covered by windows. */
2310 for (i = FRAME_TOP_MARGIN (f); i < f->desired_matrix->nrows; ++i)
2311 clear_glyph_row (MATRIX_ROW (f->desired_matrix, i));
2312
2313 /* Build the matrix by walking the window tree. */
2314 build_frame_matrix_from_window_tree (f->desired_matrix,
2315 XWINDOW (FRAME_ROOT_WINDOW (f)));
2316 }
2317
2318
2319 /* Walk a window tree, building a frame matrix MATRIX from window
2320 matrices. W is the root of a window tree. */
2321
2322 static void
2323 build_frame_matrix_from_window_tree (struct glyph_matrix *matrix, struct window *w)
2324 {
2325 while (w)
2326 {
2327 if (WINDOWP (w->contents))
2328 build_frame_matrix_from_window_tree (matrix, XWINDOW (w->contents));
2329 else
2330 build_frame_matrix_from_leaf_window (matrix, w);
2331
2332 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2333 }
2334 }
2335
2336
2337 /* Add a window's matrix to a frame matrix. FRAME_MATRIX is the
2338 desired frame matrix built. W is a leaf window whose desired or
2339 current matrix is to be added to FRAME_MATRIX. W's flag
2340 must_be_updated_p determines which matrix it contributes to
2341 FRAME_MATRIX. If W->must_be_updated_p, W's desired matrix
2342 is added to FRAME_MATRIX, otherwise W's current matrix is added.
2343 Adding a desired matrix means setting up used counters and such in
2344 frame rows, while adding a current window matrix to FRAME_MATRIX
2345 means copying glyphs. The latter case corresponds to
2346 preserve_other_columns in the old redisplay. */
2347
2348 static void
2349 build_frame_matrix_from_leaf_window (struct glyph_matrix *frame_matrix, struct window *w)
2350 {
2351 struct glyph_matrix *window_matrix;
2352 int window_y, frame_y;
2353 /* If non-zero, a glyph to insert at the right border of W. */
2354 GLYPH right_border_glyph;
2355
2356 SET_GLYPH_FROM_CHAR (right_border_glyph, 0);
2357
2358 /* Set window_matrix to the matrix we have to add to FRAME_MATRIX. */
2359 if (w->must_be_updated_p)
2360 {
2361 window_matrix = w->desired_matrix;
2362
2363 /* Decide whether we want to add a vertical border glyph. */
2364 if (!WINDOW_RIGHTMOST_P (w))
2365 {
2366 struct Lisp_Char_Table *dp = window_display_table (w);
2367 Lisp_Object gc;
2368
2369 SET_GLYPH_FROM_CHAR (right_border_glyph, '|');
2370 if (dp
2371 && (gc = DISP_BORDER_GLYPH (dp), GLYPH_CODE_P (gc)))
2372 {
2373 SET_GLYPH_FROM_GLYPH_CODE (right_border_glyph, gc);
2374 spec_glyph_lookup_face (w, &right_border_glyph);
2375 }
2376
2377 if (GLYPH_FACE (right_border_glyph) <= 0)
2378 SET_GLYPH_FACE (right_border_glyph, VERTICAL_BORDER_FACE_ID);
2379 }
2380 }
2381 else
2382 window_matrix = w->current_matrix;
2383
2384 /* For all rows in the window matrix and corresponding rows in the
2385 frame matrix. */
2386 window_y = 0;
2387 frame_y = window_matrix->matrix_y;
2388 while (window_y < window_matrix->nrows)
2389 {
2390 struct glyph_row *frame_row = frame_matrix->rows + frame_y;
2391 struct glyph_row *window_row = window_matrix->rows + window_y;
2392 bool current_row_p = window_matrix == w->current_matrix;
2393
2394 /* Fill up the frame row with spaces up to the left margin of the
2395 window row. */
2396 fill_up_frame_row_with_spaces (frame_row, window_matrix->matrix_x);
2397
2398 /* Fill up areas in the window matrix row with spaces. */
2399 fill_up_glyph_row_with_spaces (window_row);
2400
2401 /* If only part of W's desired matrix has been built, and
2402 window_row wasn't displayed, use the corresponding current
2403 row instead. */
2404 if (window_matrix == w->desired_matrix
2405 && !window_row->enabled_p)
2406 {
2407 window_row = w->current_matrix->rows + window_y;
2408 current_row_p = 1;
2409 }
2410
2411 if (current_row_p)
2412 {
2413 /* Copy window row to frame row. */
2414 memcpy (frame_row->glyphs[TEXT_AREA] + window_matrix->matrix_x,
2415 window_row->glyphs[0],
2416 window_matrix->matrix_w * sizeof (struct glyph));
2417 }
2418 else
2419 {
2420 eassert (window_row->enabled_p);
2421
2422 /* Only when a desired row has been displayed, we want
2423 the corresponding frame row to be updated. */
2424 frame_row->enabled_p = 1;
2425
2426 /* Maybe insert a vertical border between horizontally adjacent
2427 windows. */
2428 if (GLYPH_CHAR (right_border_glyph) != 0)
2429 {
2430 struct glyph *border = window_row->glyphs[LAST_AREA] - 1;
2431 SET_CHAR_GLYPH_FROM_GLYPH (*border, right_border_glyph);
2432 }
2433
2434 #ifdef GLYPH_DEBUG
2435 /* Window row window_y must be a slice of frame row
2436 frame_y. */
2437 eassert (glyph_row_slice_p (window_row, frame_row));
2438
2439 /* If rows are in sync, we don't have to copy glyphs because
2440 frame and window share glyphs. */
2441
2442 strcpy (w->current_matrix->method, w->desired_matrix->method);
2443 add_window_display_history (w, w->current_matrix->method, 0);
2444 #endif
2445 }
2446
2447 /* Set number of used glyphs in the frame matrix. Since we fill
2448 up with spaces, and visit leaf windows from left to right it
2449 can be done simply. */
2450 frame_row->used[TEXT_AREA]
2451 = window_matrix->matrix_x + window_matrix->matrix_w;
2452
2453 /* Next row. */
2454 ++window_y;
2455 ++frame_y;
2456 }
2457 }
2458
2459 /* Given a user-specified glyph, possibly including a Lisp-level face
2460 ID, return a glyph that has a realized face ID.
2461 This is used for glyphs displayed specially and not part of the text;
2462 for instance, vertical separators, truncation markers, etc. */
2463
2464 void
2465 spec_glyph_lookup_face (struct window *w, GLYPH *glyph)
2466 {
2467 int lface_id = GLYPH_FACE (*glyph);
2468 /* Convert the glyph's specified face to a realized (cache) face. */
2469 if (lface_id > 0)
2470 {
2471 int face_id = merge_faces (XFRAME (w->frame),
2472 Qt, lface_id, DEFAULT_FACE_ID);
2473 SET_GLYPH_FACE (*glyph, face_id);
2474 }
2475 }
2476
2477 /* Add spaces to a glyph row ROW in a window matrix.
2478
2479 Each row has the form:
2480
2481 +---------+-----------------------------+------------+
2482 | left | text | right |
2483 +---------+-----------------------------+------------+
2484
2485 Left and right marginal areas are optional. This function adds
2486 spaces to areas so that there are no empty holes between areas.
2487 In other words: If the right area is not empty, the text area
2488 is filled up with spaces up to the right area. If the text area
2489 is not empty, the left area is filled up.
2490
2491 To be called for frame-based redisplay, only. */
2492
2493 static void
2494 fill_up_glyph_row_with_spaces (struct glyph_row *row)
2495 {
2496 fill_up_glyph_row_area_with_spaces (row, LEFT_MARGIN_AREA);
2497 fill_up_glyph_row_area_with_spaces (row, TEXT_AREA);
2498 fill_up_glyph_row_area_with_spaces (row, RIGHT_MARGIN_AREA);
2499 }
2500
2501
2502 /* Fill area AREA of glyph row ROW with spaces. To be called for
2503 frame-based redisplay only. */
2504
2505 static void
2506 fill_up_glyph_row_area_with_spaces (struct glyph_row *row, int area)
2507 {
2508 if (row->glyphs[area] < row->glyphs[area + 1])
2509 {
2510 struct glyph *end = row->glyphs[area + 1];
2511 struct glyph *text = row->glyphs[area] + row->used[area];
2512
2513 while (text < end)
2514 *text++ = space_glyph;
2515 row->used[area] = text - row->glyphs[area];
2516 }
2517 }
2518
2519
2520 /* Add spaces to the end of ROW in a frame matrix until index UPTO is
2521 reached. In frame matrices only one area, TEXT_AREA, is used. */
2522
2523 static void
2524 fill_up_frame_row_with_spaces (struct glyph_row *row, int upto)
2525 {
2526 int i = row->used[TEXT_AREA];
2527 struct glyph *glyph = row->glyphs[TEXT_AREA];
2528
2529 while (i < upto)
2530 glyph[i++] = space_glyph;
2531
2532 row->used[TEXT_AREA] = i;
2533 }
2534
2535
2536 \f
2537 /**********************************************************************
2538 Mirroring operations on frame matrices in window matrices
2539 **********************************************************************/
2540
2541 /* Set frame being updated via frame-based redisplay to F. This
2542 function must be called before updates to make explicit that we are
2543 working on frame matrices or not. */
2544
2545 static void
2546 set_frame_matrix_frame (struct frame *f)
2547 {
2548 frame_matrix_frame = f;
2549 }
2550
2551
2552 /* Make sure glyph row ROW in CURRENT_MATRIX is up to date.
2553 DESIRED_MATRIX is the desired matrix corresponding to
2554 CURRENT_MATRIX. The update is done by exchanging glyph pointers
2555 between rows in CURRENT_MATRIX and DESIRED_MATRIX. If
2556 frame_matrix_frame is non-null, this indicates that the exchange is
2557 done in frame matrices, and that we have to perform analogous
2558 operations in window matrices of frame_matrix_frame. */
2559
2560 static void
2561 make_current (struct glyph_matrix *desired_matrix, struct glyph_matrix *current_matrix, int row)
2562 {
2563 struct glyph_row *current_row = MATRIX_ROW (current_matrix, row);
2564 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, row);
2565 bool mouse_face_p = current_row->mouse_face_p;
2566
2567 /* Do current_row = desired_row. This exchanges glyph pointers
2568 between both rows, and does a structure assignment otherwise. */
2569 assign_row (current_row, desired_row);
2570
2571 /* Enable current_row to mark it as valid. */
2572 current_row->enabled_p = 1;
2573 current_row->mouse_face_p = mouse_face_p;
2574
2575 /* If we are called on frame matrices, perform analogous operations
2576 for window matrices. */
2577 if (frame_matrix_frame)
2578 mirror_make_current (XWINDOW (frame_matrix_frame->root_window), row);
2579 }
2580
2581
2582 /* W is the root of a window tree. FRAME_ROW is the index of a row in
2583 W's frame which has been made current (by swapping pointers between
2584 current and desired matrix). Perform analogous operations in the
2585 matrices of leaf windows in the window tree rooted at W. */
2586
2587 static void
2588 mirror_make_current (struct window *w, int frame_row)
2589 {
2590 while (w)
2591 {
2592 if (WINDOWP (w->contents))
2593 mirror_make_current (XWINDOW (w->contents), frame_row);
2594 else
2595 {
2596 /* Row relative to window W. Don't use FRAME_TO_WINDOW_VPOS
2597 here because the checks performed in debug mode there
2598 will not allow the conversion. */
2599 int row = frame_row - w->desired_matrix->matrix_y;
2600
2601 /* If FRAME_ROW is within W, assign the desired row to the
2602 current row (exchanging glyph pointers). */
2603 if (row >= 0 && row < w->desired_matrix->matrix_h)
2604 {
2605 struct glyph_row *current_row
2606 = MATRIX_ROW (w->current_matrix, row);
2607 struct glyph_row *desired_row
2608 = MATRIX_ROW (w->desired_matrix, row);
2609
2610 if (desired_row->enabled_p)
2611 assign_row (current_row, desired_row);
2612 else
2613 swap_glyph_pointers (desired_row, current_row);
2614 current_row->enabled_p = 1;
2615
2616 /* Set the Y coordinate of the mode/header line's row.
2617 It is needed in draw_row_with_mouse_face to find the
2618 screen coordinates. (Window-based redisplay sets
2619 this in update_window, but no one seems to do that
2620 for frame-based redisplay.) */
2621 if (current_row->mode_line_p)
2622 current_row->y = row;
2623 }
2624 }
2625
2626 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2627 }
2628 }
2629
2630
2631 /* Perform row dance after scrolling. We are working on the range of
2632 lines UNCHANGED_AT_TOP + 1 to UNCHANGED_AT_TOP + NLINES (not
2633 including) in MATRIX. COPY_FROM is a vector containing, for each
2634 row I in the range 0 <= I < NLINES, the index of the original line
2635 to move to I. This index is relative to the row range, i.e. 0 <=
2636 index < NLINES. RETAINED_P is a vector containing zero for each
2637 row 0 <= I < NLINES which is empty.
2638
2639 This function is called from do_scrolling and do_direct_scrolling. */
2640
2641 void
2642 mirrored_line_dance (struct glyph_matrix *matrix, int unchanged_at_top, int nlines,
2643 int *copy_from, char *retained_p)
2644 {
2645 /* A copy of original rows. */
2646 struct glyph_row *old_rows;
2647
2648 /* Rows to assign to. */
2649 struct glyph_row *new_rows = MATRIX_ROW (matrix, unchanged_at_top);
2650
2651 int i;
2652
2653 /* Make a copy of the original rows. */
2654 old_rows = alloca (nlines * sizeof *old_rows);
2655 memcpy (old_rows, new_rows, nlines * sizeof *old_rows);
2656
2657 /* Assign new rows, maybe clear lines. */
2658 for (i = 0; i < nlines; ++i)
2659 {
2660 bool enabled_before_p = new_rows[i].enabled_p;
2661
2662 eassert (i + unchanged_at_top < matrix->nrows);
2663 eassert (unchanged_at_top + copy_from[i] < matrix->nrows);
2664 new_rows[i] = old_rows[copy_from[i]];
2665 new_rows[i].enabled_p = enabled_before_p;
2666
2667 /* RETAINED_P is zero for empty lines. */
2668 if (!retained_p[copy_from[i]])
2669 new_rows[i].enabled_p = 0;
2670 }
2671
2672 /* Do the same for window matrices, if MATRIX is a frame matrix. */
2673 if (frame_matrix_frame)
2674 mirror_line_dance (XWINDOW (frame_matrix_frame->root_window),
2675 unchanged_at_top, nlines, copy_from, retained_p);
2676 }
2677
2678
2679 /* Synchronize glyph pointers in the current matrix of window W with
2680 the current frame matrix. */
2681
2682 static void
2683 sync_window_with_frame_matrix_rows (struct window *w)
2684 {
2685 struct frame *f = XFRAME (w->frame);
2686 struct glyph_row *window_row, *window_row_end, *frame_row;
2687 int left, right, x, width;
2688
2689 /* Preconditions: W must be a live window on a tty frame. */
2690 eassert (BUFFERP (w->contents));
2691 eassert (!FRAME_WINDOW_P (f));
2692
2693 left = margin_glyphs_to_reserve (w, 1, w->left_margin_cols);
2694 right = margin_glyphs_to_reserve (w, 1, w->right_margin_cols);
2695 x = w->current_matrix->matrix_x;
2696 width = w->current_matrix->matrix_w;
2697
2698 window_row = w->current_matrix->rows;
2699 window_row_end = window_row + w->current_matrix->nrows;
2700 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
2701
2702 for (; window_row < window_row_end; ++window_row, ++frame_row)
2703 {
2704 window_row->glyphs[LEFT_MARGIN_AREA]
2705 = frame_row->glyphs[0] + x;
2706 window_row->glyphs[TEXT_AREA]
2707 = window_row->glyphs[LEFT_MARGIN_AREA] + left;
2708 window_row->glyphs[LAST_AREA]
2709 = window_row->glyphs[LEFT_MARGIN_AREA] + width;
2710 window_row->glyphs[RIGHT_MARGIN_AREA]
2711 = window_row->glyphs[LAST_AREA] - right;
2712 }
2713 }
2714
2715
2716 /* Return the window in the window tree rooted in W containing frame
2717 row ROW. Value is null if none is found. */
2718
2719 static struct window *
2720 frame_row_to_window (struct window *w, int row)
2721 {
2722 struct window *found = NULL;
2723
2724 while (w && !found)
2725 {
2726 if (WINDOWP (w->contents))
2727 found = frame_row_to_window (XWINDOW (w->contents), row);
2728 else if (row >= WINDOW_TOP_EDGE_LINE (w)
2729 && row < WINDOW_BOTTOM_EDGE_LINE (w))
2730 found = w;
2731
2732 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2733 }
2734
2735 return found;
2736 }
2737
2738
2739 /* Perform a line dance in the window tree rooted at W, after
2740 scrolling a frame matrix in mirrored_line_dance.
2741
2742 We are working on the range of lines UNCHANGED_AT_TOP + 1 to
2743 UNCHANGED_AT_TOP + NLINES (not including) in W's frame matrix.
2744 COPY_FROM is a vector containing, for each row I in the range 0 <=
2745 I < NLINES, the index of the original line to move to I. This
2746 index is relative to the row range, i.e. 0 <= index < NLINES.
2747 RETAINED_P is a vector containing zero for each row 0 <= I < NLINES
2748 which is empty. */
2749
2750 static void
2751 mirror_line_dance (struct window *w, int unchanged_at_top, int nlines, int *copy_from, char *retained_p)
2752 {
2753 while (w)
2754 {
2755 if (WINDOWP (w->contents))
2756 mirror_line_dance (XWINDOW (w->contents), unchanged_at_top,
2757 nlines, copy_from, retained_p);
2758 else
2759 {
2760 /* W is a leaf window, and we are working on its current
2761 matrix m. */
2762 struct glyph_matrix *m = w->current_matrix;
2763 int i;
2764 bool sync_p = 0;
2765 struct glyph_row *old_rows;
2766
2767 /* Make a copy of the original rows of matrix m. */
2768 old_rows = alloca (m->nrows * sizeof *old_rows);
2769 memcpy (old_rows, m->rows, m->nrows * sizeof *old_rows);
2770
2771 for (i = 0; i < nlines; ++i)
2772 {
2773 /* Frame relative line assigned to. */
2774 int frame_to = i + unchanged_at_top;
2775
2776 /* Frame relative line assigned. */
2777 int frame_from = copy_from[i] + unchanged_at_top;
2778
2779 /* Window relative line assigned to. */
2780 int window_to = frame_to - m->matrix_y;
2781
2782 /* Window relative line assigned. */
2783 int window_from = frame_from - m->matrix_y;
2784
2785 /* Is assigned line inside window? */
2786 bool from_inside_window_p
2787 = window_from >= 0 && window_from < m->matrix_h;
2788
2789 /* Is assigned to line inside window? */
2790 bool to_inside_window_p
2791 = window_to >= 0 && window_to < m->matrix_h;
2792
2793 if (from_inside_window_p && to_inside_window_p)
2794 {
2795 /* Do the assignment. The enabled_p flag is saved
2796 over the assignment because the old redisplay did
2797 that. */
2798 bool enabled_before_p = m->rows[window_to].enabled_p;
2799 m->rows[window_to] = old_rows[window_from];
2800 m->rows[window_to].enabled_p = enabled_before_p;
2801
2802 /* If frame line is empty, window line is empty, too. */
2803 if (!retained_p[copy_from[i]])
2804 m->rows[window_to].enabled_p = 0;
2805 }
2806 else if (to_inside_window_p)
2807 {
2808 /* A copy between windows. This is an infrequent
2809 case not worth optimizing. */
2810 struct frame *f = XFRAME (w->frame);
2811 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
2812 struct window *w2;
2813 struct glyph_matrix *m2;
2814 int m2_from;
2815
2816 w2 = frame_row_to_window (root, frame_from);
2817 /* ttn@surf.glug.org: when enabling menu bar using `emacs
2818 -nw', FROM_FRAME sometimes has no associated window.
2819 This check avoids a segfault if W2 is null. */
2820 if (w2)
2821 {
2822 m2 = w2->current_matrix;
2823 m2_from = frame_from - m2->matrix_y;
2824 copy_row_except_pointers (m->rows + window_to,
2825 m2->rows + m2_from);
2826
2827 /* If frame line is empty, window line is empty, too. */
2828 if (!retained_p[copy_from[i]])
2829 m->rows[window_to].enabled_p = 0;
2830 }
2831 sync_p = 1;
2832 }
2833 else if (from_inside_window_p)
2834 sync_p = 1;
2835 }
2836
2837 /* If there was a copy between windows, make sure glyph
2838 pointers are in sync with the frame matrix. */
2839 if (sync_p)
2840 sync_window_with_frame_matrix_rows (w);
2841
2842 /* Check that no pointers are lost. */
2843 CHECK_MATRIX (m);
2844 }
2845
2846 /* Next window on same level. */
2847 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2848 }
2849 }
2850
2851
2852 #ifdef GLYPH_DEBUG
2853
2854 /* Check that window and frame matrices agree about their
2855 understanding where glyphs of the rows are to find. For each
2856 window in the window tree rooted at W, check that rows in the
2857 matrices of leaf window agree with their frame matrices about
2858 glyph pointers. */
2859
2860 static void
2861 check_window_matrix_pointers (struct window *w)
2862 {
2863 while (w)
2864 {
2865 if (WINDOWP (w->contents))
2866 check_window_matrix_pointers (XWINDOW (w->contents));
2867 else
2868 {
2869 struct frame *f = XFRAME (w->frame);
2870 check_matrix_pointers (w->desired_matrix, f->desired_matrix);
2871 check_matrix_pointers (w->current_matrix, f->current_matrix);
2872 }
2873
2874 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2875 }
2876 }
2877
2878
2879 /* Check that window rows are slices of frame rows. WINDOW_MATRIX is
2880 a window and FRAME_MATRIX is the corresponding frame matrix. For
2881 each row in WINDOW_MATRIX check that it's a slice of the
2882 corresponding frame row. If it isn't, abort. */
2883
2884 static void
2885 check_matrix_pointers (struct glyph_matrix *window_matrix,
2886 struct glyph_matrix *frame_matrix)
2887 {
2888 /* Row number in WINDOW_MATRIX. */
2889 int i = 0;
2890
2891 /* Row number corresponding to I in FRAME_MATRIX. */
2892 int j = window_matrix->matrix_y;
2893
2894 /* For all rows check that the row in the window matrix is a
2895 slice of the row in the frame matrix. If it isn't we didn't
2896 mirror an operation on the frame matrix correctly. */
2897 while (i < window_matrix->nrows)
2898 {
2899 if (!glyph_row_slice_p (window_matrix->rows + i,
2900 frame_matrix->rows + j))
2901 emacs_abort ();
2902 ++i, ++j;
2903 }
2904 }
2905
2906 #endif /* GLYPH_DEBUG */
2907
2908
2909 \f
2910 /**********************************************************************
2911 VPOS and HPOS translations
2912 **********************************************************************/
2913
2914 #ifdef GLYPH_DEBUG
2915
2916 /* Translate vertical position VPOS which is relative to window W to a
2917 vertical position relative to W's frame. */
2918
2919 static int
2920 window_to_frame_vpos (struct window *w, int vpos)
2921 {
2922 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
2923 eassert (vpos >= 0 && vpos <= w->desired_matrix->nrows);
2924 vpos += WINDOW_TOP_EDGE_LINE (w);
2925 eassert (vpos >= 0 && vpos <= FRAME_LINES (XFRAME (w->frame)));
2926 return vpos;
2927 }
2928
2929
2930 /* Translate horizontal position HPOS which is relative to window W to
2931 a horizontal position relative to W's frame. */
2932
2933 static int
2934 window_to_frame_hpos (struct window *w, int hpos)
2935 {
2936 eassert (!FRAME_WINDOW_P (XFRAME (w->frame)));
2937 hpos += WINDOW_LEFT_EDGE_COL (w);
2938 return hpos;
2939 }
2940
2941 #endif /* GLYPH_DEBUG */
2942
2943
2944 \f
2945 /**********************************************************************
2946 Redrawing Frames
2947 **********************************************************************/
2948
2949 /* Redraw frame F. */
2950
2951 void
2952 redraw_frame (struct frame *f)
2953 {
2954 /* Error if F has no glyphs. */
2955 eassert (f->glyphs_initialized_p);
2956 update_begin (f);
2957 #ifdef MSDOS
2958 if (FRAME_MSDOS_P (f))
2959 FRAME_TERMINAL (f)->set_terminal_modes_hook (FRAME_TERMINAL (f));
2960 #endif
2961 clear_frame (f);
2962 clear_current_matrices (f);
2963 update_end (f);
2964 if (FRAME_TERMCAP_P (f))
2965 fflush (FRAME_TTY (f)->output);
2966 windows_or_buffers_changed++;
2967 /* Mark all windows as inaccurate, so that every window will have
2968 its redisplay done. */
2969 mark_window_display_accurate (FRAME_ROOT_WINDOW (f), 0);
2970 set_window_update_flags (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
2971 f->garbaged = 0;
2972 }
2973
2974 DEFUN ("redraw-frame", Fredraw_frame, Sredraw_frame, 0, 1, 0,
2975 doc: /* Clear frame FRAME and output again what is supposed to appear on it.
2976 If FRAME is omitted or nil, the selected frame is used. */)
2977 (Lisp_Object frame)
2978 {
2979 redraw_frame (decode_live_frame (frame));
2980 return Qnil;
2981 }
2982
2983 DEFUN ("redraw-display", Fredraw_display, Sredraw_display, 0, 0, "",
2984 doc: /* Clear and redisplay all visible frames. */)
2985 (void)
2986 {
2987 Lisp_Object tail, frame;
2988
2989 FOR_EACH_FRAME (tail, frame)
2990 if (FRAME_VISIBLE_P (XFRAME (frame)))
2991 redraw_frame (XFRAME (frame));
2992
2993 return Qnil;
2994 }
2995
2996
2997 \f
2998 /***********************************************************************
2999 Frame Update
3000 ***********************************************************************/
3001
3002 /* Update frame F based on the data in desired matrices.
3003
3004 If FORCE_P, don't let redisplay be stopped by detecting pending input.
3005 If INHIBIT_HAIRY_ID_P, don't try scrolling.
3006
3007 Value is true if redisplay was stopped due to pending input. */
3008
3009 bool
3010 update_frame (struct frame *f, bool force_p, bool inhibit_hairy_id_p)
3011 {
3012 /* True means display has been paused because of pending input. */
3013 bool paused_p;
3014 struct window *root_window = XWINDOW (f->root_window);
3015
3016 if (redisplay_dont_pause)
3017 force_p = 1;
3018 else if (!force_p && detect_input_pending_ignore_squeezables ())
3019 {
3020 paused_p = 1;
3021 goto do_pause;
3022 }
3023
3024 if (FRAME_WINDOW_P (f))
3025 {
3026 /* We are working on window matrix basis. All windows whose
3027 flag must_be_updated_p is set have to be updated. */
3028
3029 /* Record that we are not working on frame matrices. */
3030 set_frame_matrix_frame (NULL);
3031
3032 /* Update all windows in the window tree of F, maybe stopping
3033 when pending input is detected. */
3034 update_begin (f);
3035
3036 #if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
3037 /* Update the menu bar on X frames that don't have toolkit
3038 support. */
3039 if (WINDOWP (f->menu_bar_window))
3040 update_window (XWINDOW (f->menu_bar_window), 1);
3041 #endif
3042
3043 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
3044 /* Update the tool-bar window, if present. */
3045 if (WINDOWP (f->tool_bar_window))
3046 {
3047 struct window *w = XWINDOW (f->tool_bar_window);
3048
3049 /* Update tool-bar window. */
3050 if (w->must_be_updated_p)
3051 {
3052 Lisp_Object tem;
3053
3054 update_window (w, 1);
3055 w->must_be_updated_p = 0;
3056
3057 /* Swap tool-bar strings. We swap because we want to
3058 reuse strings. */
3059 tem = f->current_tool_bar_string;
3060 fset_current_tool_bar_string (f, f->desired_tool_bar_string);
3061 fset_desired_tool_bar_string (f, tem);
3062 }
3063 }
3064 #endif
3065
3066 /* Update windows. */
3067 paused_p = update_window_tree (root_window, force_p);
3068 update_end (f);
3069 }
3070 else
3071 {
3072 /* We are working on frame matrix basis. Set the frame on whose
3073 frame matrix we operate. */
3074 set_frame_matrix_frame (f);
3075
3076 /* Build F's desired matrix from window matrices. */
3077 build_frame_matrix (f);
3078
3079 /* Update the display */
3080 update_begin (f);
3081 paused_p = update_frame_1 (f, force_p, inhibit_hairy_id_p);
3082 update_end (f);
3083
3084 if (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
3085 {
3086 if (FRAME_TTY (f)->termscript)
3087 fflush (FRAME_TTY (f)->termscript);
3088 if (FRAME_TERMCAP_P (f))
3089 fflush (FRAME_TTY (f)->output);
3090 }
3091
3092 /* Check window matrices for lost pointers. */
3093 #ifdef GLYPH_DEBUG
3094 check_window_matrix_pointers (root_window);
3095 add_frame_display_history (f, paused_p);
3096 #endif
3097 }
3098
3099 do_pause:
3100 /* Reset flags indicating that a window should be updated. */
3101 set_window_update_flags (root_window, 0);
3102
3103 display_completed = !paused_p;
3104 return paused_p;
3105 }
3106
3107
3108 \f
3109 /************************************************************************
3110 Window-based updates
3111 ************************************************************************/
3112
3113 /* Perform updates in window tree rooted at W.
3114 If FORCE_P, don't stop updating if input is pending. */
3115
3116 static bool
3117 update_window_tree (struct window *w, bool force_p)
3118 {
3119 bool paused_p = 0;
3120
3121 while (w && !paused_p)
3122 {
3123 if (WINDOWP (w->contents))
3124 paused_p |= update_window_tree (XWINDOW (w->contents), force_p);
3125 else if (w->must_be_updated_p)
3126 paused_p |= update_window (w, force_p);
3127
3128 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3129 }
3130
3131 return paused_p;
3132 }
3133
3134
3135 /* Update window W if its flag must_be_updated_p is set.
3136 If FORCE_P, don't stop updating if input is pending. */
3137
3138 void
3139 update_single_window (struct window *w, bool force_p)
3140 {
3141 if (w->must_be_updated_p)
3142 {
3143 struct frame *f = XFRAME (WINDOW_FRAME (w));
3144
3145 /* Record that this is not a frame-based redisplay. */
3146 set_frame_matrix_frame (NULL);
3147
3148 if (redisplay_dont_pause)
3149 force_p = 1;
3150
3151 /* Update W. */
3152 update_begin (f);
3153 update_window (w, force_p);
3154 update_end (f);
3155
3156 /* Reset flag in W. */
3157 w->must_be_updated_p = 0;
3158 }
3159 }
3160
3161 #ifdef HAVE_WINDOW_SYSTEM
3162
3163 /* Redraw lines from the current matrix of window W that are
3164 overlapped by other rows. YB is bottom-most y-position in W. */
3165
3166 static void
3167 redraw_overlapped_rows (struct window *w, int yb)
3168 {
3169 int i;
3170 struct frame *f = XFRAME (WINDOW_FRAME (w));
3171
3172 /* If rows overlapping others have been changed, the rows being
3173 overlapped have to be redrawn. This won't draw lines that have
3174 already been drawn in update_window_line because overlapped_p in
3175 desired rows is 0, so after row assignment overlapped_p in
3176 current rows is 0. */
3177 for (i = 0; i < w->current_matrix->nrows; ++i)
3178 {
3179 struct glyph_row *row = w->current_matrix->rows + i;
3180
3181 if (!row->enabled_p)
3182 break;
3183 else if (row->mode_line_p)
3184 continue;
3185
3186 if (row->overlapped_p)
3187 {
3188 enum glyph_row_area area;
3189
3190 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
3191 {
3192 output_cursor_to (w, i, 0, row->y,
3193 area == TEXT_AREA ? row->x : 0);
3194 if (row->used[area])
3195 FRAME_RIF (f)->write_glyphs (w, row, row->glyphs[area],
3196 area, row->used[area]);
3197 FRAME_RIF (f)->clear_end_of_line (w, row, area, -1);
3198 }
3199
3200 row->overlapped_p = 0;
3201 }
3202
3203 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3204 break;
3205 }
3206 }
3207
3208
3209 /* Redraw lines from the current matrix of window W that overlap
3210 others. YB is bottom-most y-position in W. */
3211
3212 static void
3213 redraw_overlapping_rows (struct window *w, int yb)
3214 {
3215 int i, bottom_y;
3216 struct glyph_row *row;
3217 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3218
3219 for (i = 0; i < w->current_matrix->nrows; ++i)
3220 {
3221 row = w->current_matrix->rows + i;
3222
3223 if (!row->enabled_p)
3224 break;
3225 else if (row->mode_line_p)
3226 continue;
3227
3228 bottom_y = MATRIX_ROW_BOTTOM_Y (row);
3229
3230 if (row->overlapping_p)
3231 {
3232 int overlaps = 0;
3233
3234 if (MATRIX_ROW_OVERLAPS_PRED_P (row) && i > 0
3235 && !MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p)
3236 overlaps |= OVERLAPS_PRED;
3237 if (MATRIX_ROW_OVERLAPS_SUCC_P (row) && bottom_y < yb
3238 && !MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p)
3239 overlaps |= OVERLAPS_SUCC;
3240
3241 if (overlaps)
3242 {
3243 if (row->used[LEFT_MARGIN_AREA])
3244 rif->fix_overlapping_area (w, row, LEFT_MARGIN_AREA, overlaps);
3245
3246 if (row->used[TEXT_AREA])
3247 rif->fix_overlapping_area (w, row, TEXT_AREA, overlaps);
3248
3249 if (row->used[RIGHT_MARGIN_AREA])
3250 rif->fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, overlaps);
3251
3252 /* Record in neighbor rows that ROW overwrites part of
3253 their display. */
3254 if (overlaps & OVERLAPS_PRED)
3255 MATRIX_ROW (w->current_matrix, i - 1)->overlapped_p = 1;
3256 if (overlaps & OVERLAPS_SUCC)
3257 MATRIX_ROW (w->current_matrix, i + 1)->overlapped_p = 1;
3258 }
3259 }
3260
3261 if (bottom_y >= yb)
3262 break;
3263 }
3264 }
3265
3266 #endif /* HAVE_WINDOW_SYSTEM */
3267
3268
3269 #if defined GLYPH_DEBUG && 0
3270
3271 /* Check that no row in the current matrix of window W is enabled
3272 which is below what's displayed in the window. */
3273
3274 static void
3275 check_current_matrix_flags (struct window *w)
3276 {
3277 bool last_seen_p = 0;
3278 int i, yb = window_text_bottom_y (w);
3279
3280 for (i = 0; i < w->current_matrix->nrows - 1; ++i)
3281 {
3282 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
3283 if (!last_seen_p && MATRIX_ROW_BOTTOM_Y (row) >= yb)
3284 last_seen_p = 1;
3285 else if (last_seen_p && row->enabled_p)
3286 emacs_abort ();
3287 }
3288 }
3289
3290 #endif /* GLYPH_DEBUG */
3291
3292
3293 /* Update display of window W.
3294 If FORCE_P, don't stop updating when input is pending. */
3295
3296 static bool
3297 update_window (struct window *w, bool force_p)
3298 {
3299 struct glyph_matrix *desired_matrix = w->desired_matrix;
3300 bool paused_p;
3301 int preempt_count = baud_rate / 2400 + 1;
3302 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3303 #ifdef GLYPH_DEBUG
3304 /* Check that W's frame doesn't have glyph matrices. */
3305 eassert (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))));
3306 #endif
3307
3308 /* Check pending input the first time so that we can quickly return. */
3309 if (!force_p)
3310 detect_input_pending_ignore_squeezables ();
3311
3312 /* If forced to complete the update, or if no input is pending, do
3313 the update. */
3314 if (force_p || !input_pending || !NILP (do_mouse_tracking))
3315 {
3316 struct glyph_row *row, *end;
3317 struct glyph_row *mode_line_row;
3318 struct glyph_row *header_line_row;
3319 int yb;
3320 bool changed_p = 0, mouse_face_overwritten_p = 0;
3321 int n_updated = 0;
3322
3323 rif->update_window_begin_hook (w);
3324 yb = window_text_bottom_y (w);
3325 row = MATRIX_ROW (desired_matrix, 0);
3326 end = MATRIX_MODE_LINE_ROW (desired_matrix);
3327
3328 /* Take note of the header line, if there is one. We will
3329 update it below, after updating all of the window's lines. */
3330 if (row->mode_line_p)
3331 {
3332 header_line_row = row;
3333 ++row;
3334 }
3335 else
3336 header_line_row = NULL;
3337
3338 /* Update the mode line, if necessary. */
3339 mode_line_row = MATRIX_MODE_LINE_ROW (desired_matrix);
3340 if (mode_line_row->mode_line_p && mode_line_row->enabled_p)
3341 {
3342 mode_line_row->y = yb;
3343 update_window_line (w, MATRIX_ROW_VPOS (mode_line_row,
3344 desired_matrix),
3345 &mouse_face_overwritten_p);
3346 }
3347
3348 /* Find first enabled row. Optimizations in redisplay_internal
3349 may lead to an update with only one row enabled. There may
3350 be also completely empty matrices. */
3351 while (row < end && !row->enabled_p)
3352 ++row;
3353
3354 /* Try reusing part of the display by copying. */
3355 if (row < end && !desired_matrix->no_scrolling_p)
3356 {
3357 int rc = scrolling_window (w, header_line_row != NULL);
3358 if (rc < 0)
3359 {
3360 /* All rows were found to be equal. */
3361 paused_p = 0;
3362 goto set_cursor;
3363 }
3364 else if (rc > 0)
3365 {
3366 /* We've scrolled the display. */
3367 force_p = 1;
3368 changed_p = 1;
3369 }
3370 }
3371
3372 /* Update the rest of the lines. */
3373 for (; row < end && (force_p || !input_pending); ++row)
3374 /* scrolling_window resets the enabled_p flag of the rows it
3375 reuses from current_matrix. */
3376 if (row->enabled_p)
3377 {
3378 int vpos = MATRIX_ROW_VPOS (row, desired_matrix);
3379 int i;
3380
3381 /* We'll have to play a little bit with when to
3382 detect_input_pending. If it's done too often,
3383 scrolling large windows with repeated scroll-up
3384 commands will too quickly pause redisplay. */
3385 if (!force_p && ++n_updated % preempt_count == 0)
3386 detect_input_pending_ignore_squeezables ();
3387 changed_p |= update_window_line (w, vpos,
3388 &mouse_face_overwritten_p);
3389
3390 /* Mark all rows below the last visible one in the current
3391 matrix as invalid. This is necessary because of
3392 variable line heights. Consider the case of three
3393 successive redisplays, where the first displays 5
3394 lines, the second 3 lines, and the third 5 lines again.
3395 If the second redisplay wouldn't mark rows in the
3396 current matrix invalid, the third redisplay might be
3397 tempted to optimize redisplay based on lines displayed
3398 in the first redisplay. */
3399 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
3400 for (i = vpos + 1; i < w->current_matrix->nrows - 1; ++i)
3401 MATRIX_ROW (w->current_matrix, i)->enabled_p = 0;
3402 }
3403
3404 /* Was display preempted? */
3405 paused_p = row < end;
3406
3407 set_cursor:
3408
3409 /* Update the header line after scrolling because a new header
3410 line would otherwise overwrite lines at the top of the window
3411 that can be scrolled. */
3412 if (header_line_row && header_line_row->enabled_p)
3413 {
3414 header_line_row->y = 0;
3415 update_window_line (w, 0, &mouse_face_overwritten_p);
3416 }
3417
3418 /* Fix the appearance of overlapping/overlapped rows. */
3419 if (!paused_p && !w->pseudo_window_p)
3420 {
3421 #ifdef HAVE_WINDOW_SYSTEM
3422 if (changed_p && rif->fix_overlapping_area)
3423 {
3424 redraw_overlapped_rows (w, yb);
3425 redraw_overlapping_rows (w, yb);
3426 }
3427 #endif
3428
3429 /* Make cursor visible at cursor position of W. */
3430 set_window_cursor_after_update (w);
3431
3432 #if 0 /* Check that current matrix invariants are satisfied. This is
3433 for debugging only. See the comment of check_matrix_invariants. */
3434 IF_DEBUG (check_matrix_invariants (w));
3435 #endif
3436 }
3437
3438 #ifdef GLYPH_DEBUG
3439 /* Remember the redisplay method used to display the matrix. */
3440 strcpy (w->current_matrix->method, w->desired_matrix->method);
3441 #endif
3442
3443 #ifdef HAVE_WINDOW_SYSTEM
3444 update_window_fringes (w, 0);
3445 #endif
3446
3447 /* End the update of window W. Don't set the cursor if we
3448 paused updating the display because in this case,
3449 set_window_cursor_after_update hasn't been called, and
3450 W->output_cursor doesn't contain the cursor location. */
3451 rif->update_window_end_hook (w, !paused_p, mouse_face_overwritten_p);
3452 }
3453 else
3454 paused_p = 1;
3455
3456 #ifdef GLYPH_DEBUG
3457 /* check_current_matrix_flags (w); */
3458 add_window_display_history (w, w->current_matrix->method, paused_p);
3459 #endif
3460
3461 clear_glyph_matrix (desired_matrix);
3462
3463 return paused_p;
3464 }
3465
3466
3467 /* Update the display of area AREA in window W, row number VPOS.
3468 AREA can be either LEFT_MARGIN_AREA or RIGHT_MARGIN_AREA. */
3469
3470 static void
3471 update_marginal_area (struct window *w, struct glyph_row *updated_row,
3472 enum glyph_row_area area, int vpos)
3473 {
3474 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3475 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3476
3477 /* Set cursor to start of glyphs, write them, and clear to the end
3478 of the area. I don't think that something more sophisticated is
3479 necessary here, since marginal areas will not be the default. */
3480 output_cursor_to (w, vpos, 0, desired_row->y, 0);
3481 if (desired_row->used[area])
3482 rif->write_glyphs (w, updated_row, desired_row->glyphs[area],
3483 area, desired_row->used[area]);
3484 rif->clear_end_of_line (w, updated_row, area, -1);
3485 }
3486
3487
3488 /* Update the display of the text area of row VPOS in window W.
3489 Value is true if display has changed. */
3490
3491 static bool
3492 update_text_area (struct window *w, struct glyph_row *updated_row, int vpos)
3493 {
3494 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3495 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3496 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3497 bool changed_p = 0;
3498
3499 /* If rows are at different X or Y, or rows have different height,
3500 or the current row is marked invalid, write the entire line. */
3501 if (!current_row->enabled_p
3502 || desired_row->y != current_row->y
3503 || desired_row->ascent != current_row->ascent
3504 || desired_row->phys_ascent != current_row->phys_ascent
3505 || desired_row->phys_height != current_row->phys_height
3506 || desired_row->visible_height != current_row->visible_height
3507 || current_row->overlapped_p
3508 /* This next line is necessary for correctly redrawing
3509 mouse-face areas after scrolling and other operations.
3510 However, it causes excessive flickering when mouse is moved
3511 across the mode line. Luckily, turning it off for the mode
3512 line doesn't seem to hurt anything. -- cyd.
3513 But it is still needed for the header line. -- kfs. */
3514 || (current_row->mouse_face_p
3515 && !(current_row->mode_line_p && vpos > 0))
3516 || current_row->x != desired_row->x)
3517 {
3518 output_cursor_to (w, vpos, 0, desired_row->y, desired_row->x);
3519
3520 if (desired_row->used[TEXT_AREA])
3521 rif->write_glyphs (w, updated_row, desired_row->glyphs[TEXT_AREA],
3522 TEXT_AREA, desired_row->used[TEXT_AREA]);
3523
3524 /* Clear to end of window. */
3525 rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
3526 changed_p = 1;
3527
3528 /* This erases the cursor. We do this here because
3529 notice_overwritten_cursor cannot easily check this, which
3530 might indicate that the whole functionality of
3531 notice_overwritten_cursor would better be implemented here.
3532 On the other hand, we need notice_overwritten_cursor as long
3533 as mouse highlighting is done asynchronously outside of
3534 redisplay. */
3535 if (vpos == w->phys_cursor.vpos)
3536 w->phys_cursor_on_p = 0;
3537 }
3538 else
3539 {
3540 int stop, i, x;
3541 struct glyph *current_glyph = current_row->glyphs[TEXT_AREA];
3542 struct glyph *desired_glyph = desired_row->glyphs[TEXT_AREA];
3543 bool overlapping_glyphs_p = current_row->contains_overlapping_glyphs_p;
3544 int desired_stop_pos = desired_row->used[TEXT_AREA];
3545 bool abort_skipping = 0;
3546
3547 /* If the desired row extends its face to the text area end, and
3548 unless the current row also does so at the same position,
3549 make sure we write at least one glyph, so that the face
3550 extension actually takes place. */
3551 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row)
3552 && (desired_stop_pos < current_row->used[TEXT_AREA]
3553 || (desired_stop_pos == current_row->used[TEXT_AREA]
3554 && !MATRIX_ROW_EXTENDS_FACE_P (current_row))))
3555 --desired_stop_pos;
3556
3557 stop = min (current_row->used[TEXT_AREA], desired_stop_pos);
3558 i = 0;
3559 x = desired_row->x;
3560
3561 /* Loop over glyphs that current and desired row may have
3562 in common. */
3563 while (i < stop)
3564 {
3565 bool can_skip_p = !abort_skipping;
3566
3567 /* Skip over glyphs that both rows have in common. These
3568 don't have to be written. We can't skip if the last
3569 current glyph overlaps the glyph to its right. For
3570 example, consider a current row of `if ' with the `f' in
3571 Courier bold so that it overlaps the ` ' to its right.
3572 If the desired row is ` ', we would skip over the space
3573 after the `if' and there would remain a pixel from the
3574 `f' on the screen. */
3575 if (overlapping_glyphs_p && i > 0)
3576 {
3577 struct glyph *glyph = &current_row->glyphs[TEXT_AREA][i - 1];
3578 int left, right;
3579
3580 rif->get_glyph_overhangs (glyph, XFRAME (w->frame),
3581 &left, &right);
3582 can_skip_p = (right == 0 && !abort_skipping);
3583 }
3584
3585 if (can_skip_p)
3586 {
3587 int start_hpos = i;
3588
3589 while (i < stop
3590 && GLYPH_EQUAL_P (desired_glyph, current_glyph))
3591 {
3592 x += desired_glyph->pixel_width;
3593 ++desired_glyph, ++current_glyph, ++i;
3594 }
3595
3596 /* Consider the case that the current row contains "xxx
3597 ppp ggg" in italic Courier font, and the desired row
3598 is "xxx ggg". The character `p' has lbearing, `g'
3599 has not. The loop above will stop in front of the
3600 first `p' in the current row. If we would start
3601 writing glyphs there, we wouldn't erase the lbearing
3602 of the `p'. The rest of the lbearing problem is then
3603 taken care of by draw_glyphs. */
3604 if (overlapping_glyphs_p
3605 && i > 0
3606 && i < current_row->used[TEXT_AREA]
3607 && (current_row->used[TEXT_AREA]
3608 != desired_row->used[TEXT_AREA]))
3609 {
3610 int left, right;
3611
3612 rif->get_glyph_overhangs (current_glyph,
3613 XFRAME (w->frame),
3614 &left, &right);
3615 while (left > 0 && i > 0)
3616 {
3617 --i, --desired_glyph, --current_glyph;
3618 x -= desired_glyph->pixel_width;
3619 left -= desired_glyph->pixel_width;
3620 }
3621
3622 /* Abort the skipping algorithm if we end up before
3623 our starting point, to avoid looping (bug#1070).
3624 This can happen when the lbearing is larger than
3625 the pixel width. */
3626 abort_skipping = (i < start_hpos);
3627 }
3628 }
3629
3630 /* Try to avoid writing the entire rest of the desired row
3631 by looking for a resync point. This mainly prevents
3632 mode line flickering in the case the mode line is in
3633 fixed-pitch font, which it usually will be. */
3634 if (i < desired_row->used[TEXT_AREA])
3635 {
3636 int start_x = x, start_hpos = i;
3637 struct glyph *start = desired_glyph;
3638 int current_x = x;
3639 bool skip_first_p = !can_skip_p;
3640
3641 /* Find the next glyph that's equal again. */
3642 while (i < stop
3643 && (skip_first_p
3644 || !GLYPH_EQUAL_P (desired_glyph, current_glyph))
3645 && x == current_x)
3646 {
3647 x += desired_glyph->pixel_width;
3648 current_x += current_glyph->pixel_width;
3649 ++desired_glyph, ++current_glyph, ++i;
3650 skip_first_p = 0;
3651 }
3652
3653 if (i == start_hpos || x != current_x)
3654 {
3655 i = start_hpos;
3656 x = start_x;
3657 desired_glyph = start;
3658 break;
3659 }
3660
3661 output_cursor_to (w, vpos, start_hpos, desired_row->y, start_x);
3662 rif->write_glyphs (w, updated_row, start,
3663 TEXT_AREA, i - start_hpos);
3664 changed_p = 1;
3665 }
3666 }
3667
3668 /* Write the rest. */
3669 if (i < desired_row->used[TEXT_AREA])
3670 {
3671 output_cursor_to (w, vpos, i, desired_row->y, x);
3672 rif->write_glyphs (w, updated_row, desired_glyph,
3673 TEXT_AREA, desired_row->used[TEXT_AREA] - i);
3674 changed_p = 1;
3675 }
3676
3677 /* Maybe clear to end of line. */
3678 if (MATRIX_ROW_EXTENDS_FACE_P (desired_row))
3679 {
3680 /* If new row extends to the end of the text area, nothing
3681 has to be cleared, if and only if we did a write_glyphs
3682 above. This is made sure by setting desired_stop_pos
3683 appropriately above. */
3684 eassert (i < desired_row->used[TEXT_AREA]
3685 || ((desired_row->used[TEXT_AREA]
3686 == current_row->used[TEXT_AREA])
3687 && MATRIX_ROW_EXTENDS_FACE_P (current_row)));
3688 }
3689 else if (MATRIX_ROW_EXTENDS_FACE_P (current_row))
3690 {
3691 /* If old row extends to the end of the text area, clear. */
3692 if (i >= desired_row->used[TEXT_AREA])
3693 output_cursor_to (w, vpos, i, desired_row->y,
3694 desired_row->pixel_width);
3695 rif->clear_end_of_line (w, updated_row, TEXT_AREA, -1);
3696 changed_p = 1;
3697 }
3698 else if (desired_row->pixel_width < current_row->pixel_width)
3699 {
3700 /* Otherwise clear to the end of the old row. Everything
3701 after that position should be clear already. */
3702 int xlim;
3703
3704 if (i >= desired_row->used[TEXT_AREA])
3705 output_cursor_to (w, vpos, i, desired_row->y,
3706 desired_row->pixel_width);
3707
3708 /* If cursor is displayed at the end of the line, make sure
3709 it's cleared. Nowadays we don't have a phys_cursor_glyph
3710 with which to erase the cursor (because this method
3711 doesn't work with lbearing/rbearing), so we must do it
3712 this way. */
3713 if (vpos == w->phys_cursor.vpos
3714 && (desired_row->reversed_p
3715 ? (w->phys_cursor.hpos < 0)
3716 : (w->phys_cursor.hpos >= desired_row->used[TEXT_AREA])))
3717 {
3718 w->phys_cursor_on_p = 0;
3719 xlim = -1;
3720 }
3721 else
3722 xlim = current_row->pixel_width;
3723 rif->clear_end_of_line (w, updated_row, TEXT_AREA, xlim);
3724 changed_p = 1;
3725 }
3726 }
3727
3728 return changed_p;
3729 }
3730
3731
3732 /* Update row VPOS in window W. Value is true if display has been changed. */
3733
3734 static bool
3735 update_window_line (struct window *w, int vpos, bool *mouse_face_overwritten_p)
3736 {
3737 struct glyph_row *current_row = MATRIX_ROW (w->current_matrix, vpos);
3738 struct glyph_row *desired_row = MATRIX_ROW (w->desired_matrix, vpos);
3739 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
3740 bool changed_p = 0;
3741
3742 /* A row can be completely invisible in case a desired matrix was
3743 built with a vscroll and then make_cursor_line_fully_visible shifts
3744 the matrix. Make sure to make such rows current anyway, since
3745 we need the correct y-position, for example, in the current matrix. */
3746 if (desired_row->mode_line_p
3747 || desired_row->visible_height > 0)
3748 {
3749 eassert (desired_row->enabled_p);
3750
3751 /* Update display of the left margin area, if there is one. */
3752 if (!desired_row->full_width_p && w->left_margin_cols > 0)
3753 {
3754 changed_p = 1;
3755 update_marginal_area (w, desired_row, LEFT_MARGIN_AREA, vpos);
3756 /* Setting this flag will ensure the vertical border, if
3757 any, between this window and the one on its left will be
3758 redrawn. This is necessary because updating the left
3759 margin area can potentially draw over the border. */
3760 current_row->redraw_fringe_bitmaps_p = 1;
3761 }
3762
3763 /* Update the display of the text area. */
3764 if (update_text_area (w, desired_row, vpos))
3765 {
3766 changed_p = 1;
3767 if (current_row->mouse_face_p)
3768 *mouse_face_overwritten_p = 1;
3769 }
3770
3771 /* Update display of the right margin area, if there is one. */
3772 if (!desired_row->full_width_p && w->right_margin_cols > 0)
3773 {
3774 changed_p = 1;
3775 update_marginal_area (w, desired_row, RIGHT_MARGIN_AREA, vpos);
3776 }
3777
3778 /* Draw truncation marks etc. */
3779 if (!current_row->enabled_p
3780 || desired_row->y != current_row->y
3781 || desired_row->visible_height != current_row->visible_height
3782 || desired_row->cursor_in_fringe_p != current_row->cursor_in_fringe_p
3783 || desired_row->overlay_arrow_bitmap != current_row->overlay_arrow_bitmap
3784 || current_row->redraw_fringe_bitmaps_p
3785 || desired_row->mode_line_p != current_row->mode_line_p
3786 || desired_row->exact_window_width_line_p != current_row->exact_window_width_line_p
3787 || (MATRIX_ROW_CONTINUATION_LINE_P (desired_row)
3788 != MATRIX_ROW_CONTINUATION_LINE_P (current_row)))
3789 rif->after_update_window_line_hook (w, desired_row);
3790 }
3791
3792 /* Update current_row from desired_row. */
3793 make_current (w->desired_matrix, w->current_matrix, vpos);
3794 return changed_p;
3795 }
3796
3797
3798 /* Set the cursor after an update of window W. This function may only
3799 be called from update_window. */
3800
3801 static void
3802 set_window_cursor_after_update (struct window *w)
3803 {
3804 struct frame *f = XFRAME (w->frame);
3805 int cx, cy, vpos, hpos;
3806
3807 /* Not intended for frame matrix updates. */
3808 eassert (FRAME_WINDOW_P (f));
3809
3810 if (cursor_in_echo_area
3811 && !NILP (echo_area_buffer[0])
3812 /* If we are showing a message instead of the mini-buffer,
3813 show the cursor for the message instead. */
3814 && XWINDOW (minibuf_window) == w
3815 && EQ (minibuf_window, echo_area_window)
3816 /* These cases apply only to the frame that contains
3817 the active mini-buffer window. */
3818 && FRAME_HAS_MINIBUF_P (f)
3819 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
3820 {
3821 cx = cy = vpos = hpos = 0;
3822
3823 if (cursor_in_echo_area >= 0)
3824 {
3825 /* If the mini-buffer is several lines high, find the last
3826 line that has any text on it. Note: either all lines
3827 are enabled or none. Otherwise we wouldn't be able to
3828 determine Y. */
3829 struct glyph_row *row, *last_row;
3830 struct glyph *glyph;
3831 int yb = window_text_bottom_y (w);
3832
3833 last_row = NULL;
3834 row = w->current_matrix->rows;
3835 while (row->enabled_p
3836 && (last_row == NULL
3837 || MATRIX_ROW_BOTTOM_Y (row) <= yb))
3838 {
3839 if (row->used[TEXT_AREA]
3840 && row->glyphs[TEXT_AREA][0].charpos >= 0)
3841 last_row = row;
3842 ++row;
3843 }
3844
3845 if (last_row)
3846 {
3847 struct glyph *start = last_row->glyphs[TEXT_AREA];
3848 struct glyph *last = start + last_row->used[TEXT_AREA] - 1;
3849
3850 while (last > start && last->charpos < 0)
3851 --last;
3852
3853 for (glyph = start; glyph < last; ++glyph)
3854 {
3855 cx += glyph->pixel_width;
3856 ++hpos;
3857 }
3858
3859 cy = last_row->y;
3860 vpos = MATRIX_ROW_VPOS (last_row, w->current_matrix);
3861 }
3862 }
3863 }
3864 else
3865 {
3866 cx = w->cursor.x;
3867 cy = w->cursor.y;
3868 hpos = w->cursor.hpos;
3869 vpos = w->cursor.vpos;
3870 }
3871
3872 /* Window cursor can be out of sync for horizontally split windows.
3873 Horizontal position is -1 when cursor is on the left fringe. */
3874 hpos = clip_to_bounds (-1, hpos, w->current_matrix->matrix_w - 1);
3875 vpos = clip_to_bounds (0, vpos, w->current_matrix->nrows - 1);
3876 output_cursor_to (w, vpos, hpos, cy, cx);
3877 }
3878
3879
3880 /* Set WINDOW->must_be_updated_p to ON_P for all windows in the window
3881 tree rooted at W. */
3882
3883 void
3884 set_window_update_flags (struct window *w, bool on_p)
3885 {
3886 while (w)
3887 {
3888 if (WINDOWP (w->contents))
3889 set_window_update_flags (XWINDOW (w->contents), on_p);
3890 else
3891 w->must_be_updated_p = on_p;
3892
3893 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3894 }
3895 }
3896
3897
3898 \f
3899 /***********************************************************************
3900 Window-Based Scrolling
3901 ***********************************************************************/
3902
3903 /* Structure describing rows in scrolling_window. */
3904
3905 struct row_entry
3906 {
3907 /* Number of occurrences of this row in desired and current matrix. */
3908 int old_uses, new_uses;
3909
3910 /* Vpos of row in new matrix. */
3911 int new_line_number;
3912
3913 /* Bucket index of this row_entry in the hash table row_table. */
3914 ptrdiff_t bucket;
3915
3916 /* The row described by this entry. */
3917 struct glyph_row *row;
3918
3919 /* Hash collision chain. */
3920 struct row_entry *next;
3921 };
3922
3923 /* A pool to allocate row_entry structures from, and the size of the
3924 pool. The pool is reallocated in scrolling_window when we find
3925 that we need a larger one. */
3926
3927 static struct row_entry *row_entry_pool;
3928 static ptrdiff_t row_entry_pool_size;
3929
3930 /* Index of next free entry in row_entry_pool. */
3931
3932 static ptrdiff_t row_entry_idx;
3933
3934 /* The hash table used during scrolling, and the table's size. This
3935 table is used to quickly identify equal rows in the desired and
3936 current matrix. */
3937
3938 static struct row_entry **row_table;
3939 static ptrdiff_t row_table_size;
3940
3941 /* Vectors of pointers to row_entry structures belonging to the
3942 current and desired matrix, and the size of the vectors. */
3943
3944 static struct row_entry **old_lines, **new_lines;
3945 static ptrdiff_t old_lines_size, new_lines_size;
3946
3947 /* A pool to allocate run structures from, and its size. */
3948
3949 static struct run *run_pool;
3950 static ptrdiff_t runs_size;
3951
3952 /* A vector of runs of lines found during scrolling. */
3953
3954 static struct run **runs;
3955
3956 /* Add glyph row ROW to the scrolling hash table. */
3957
3958 static struct row_entry *
3959 add_row_entry (struct glyph_row *row)
3960 {
3961 struct row_entry *entry;
3962 ptrdiff_t i = row->hash % row_table_size;
3963
3964 entry = row_table[i];
3965 eassert (entry || verify_row_hash (row));
3966 while (entry && !row_equal_p (entry->row, row, 1))
3967 entry = entry->next;
3968
3969 if (entry == NULL)
3970 {
3971 entry = row_entry_pool + row_entry_idx++;
3972 entry->row = row;
3973 entry->old_uses = entry->new_uses = 0;
3974 entry->new_line_number = 0;
3975 entry->bucket = i;
3976 entry->next = row_table[i];
3977 row_table[i] = entry;
3978 }
3979
3980 return entry;
3981 }
3982
3983
3984 /* Try to reuse part of the current display of W by scrolling lines.
3985 HEADER_LINE_P means W has a header line.
3986
3987 The algorithm is taken from Communications of the ACM, Apr78 "A
3988 Technique for Isolating Differences Between Files." It should take
3989 O(N) time.
3990
3991 A short outline of the steps of the algorithm
3992
3993 1. Skip lines equal at the start and end of both matrices.
3994
3995 2. Enter rows in the current and desired matrix into a symbol
3996 table, counting how often they appear in both matrices.
3997
3998 3. Rows that appear exactly once in both matrices serve as anchors,
3999 i.e. we assume that such lines are likely to have been moved.
4000
4001 4. Starting from anchor lines, extend regions to be scrolled both
4002 forward and backward.
4003
4004 Value is
4005
4006 -1 if all rows were found to be equal.
4007 0 to indicate that we did not scroll the display, or
4008 1 if we did scroll. */
4009
4010 static int
4011 scrolling_window (struct window *w, bool header_line_p)
4012 {
4013 struct glyph_matrix *desired_matrix = w->desired_matrix;
4014 struct glyph_matrix *current_matrix = w->current_matrix;
4015 int yb = window_text_bottom_y (w);
4016 ptrdiff_t i;
4017 int j, first_old, first_new, last_old, last_new;
4018 int nruns, run_idx;
4019 ptrdiff_t n;
4020 struct row_entry *entry;
4021 struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w)));
4022
4023 /* Skip over rows equal at the start. */
4024 for (i = header_line_p; i < current_matrix->nrows - 1; ++i)
4025 {
4026 struct glyph_row *d = MATRIX_ROW (desired_matrix, i);
4027 struct glyph_row *c = MATRIX_ROW (current_matrix, i);
4028
4029 if (c->enabled_p
4030 && d->enabled_p
4031 && !d->redraw_fringe_bitmaps_p
4032 && c->y == d->y
4033 && MATRIX_ROW_BOTTOM_Y (c) <= yb
4034 && MATRIX_ROW_BOTTOM_Y (d) <= yb
4035 && row_equal_p (c, d, 1))
4036 {
4037 assign_row (c, d);
4038 d->enabled_p = 0;
4039 }
4040 else
4041 break;
4042 }
4043
4044 /* Give up if some rows in the desired matrix are not enabled. */
4045 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4046 return -1;
4047
4048 first_old = first_new = i;
4049
4050 /* Set last_new to the index + 1 of the row that reaches the
4051 bottom boundary in the desired matrix. Give up if we find a
4052 disabled row before we reach the bottom boundary. */
4053 i = first_new + 1;
4054 while (i < desired_matrix->nrows - 1)
4055 {
4056 int bottom;
4057
4058 if (!MATRIX_ROW (desired_matrix, i)->enabled_p)
4059 return 0;
4060 bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (desired_matrix, i));
4061 if (bottom <= yb)
4062 ++i;
4063 if (bottom >= yb)
4064 break;
4065 }
4066
4067 last_new = i;
4068
4069 /* Set last_old to the index + 1 of the row that reaches the bottom
4070 boundary in the current matrix. We don't look at the enabled
4071 flag here because we plan to reuse part of the display even if
4072 other parts are disabled. */
4073 i = first_old + 1;
4074 while (i < current_matrix->nrows - 1)
4075 {
4076 int bottom = MATRIX_ROW_BOTTOM_Y (MATRIX_ROW (current_matrix, i));
4077 if (bottom <= yb)
4078 ++i;
4079 if (bottom >= yb)
4080 break;
4081 }
4082
4083 last_old = i;
4084
4085 /* Skip over rows equal at the bottom. */
4086 i = last_new;
4087 j = last_old;
4088 while (i - 1 > first_new
4089 && j - 1 > first_old
4090 && MATRIX_ROW (current_matrix, j - 1)->enabled_p
4091 && (MATRIX_ROW (current_matrix, j - 1)->y
4092 == MATRIX_ROW (desired_matrix, i - 1)->y)
4093 && !MATRIX_ROW (desired_matrix, i - 1)->redraw_fringe_bitmaps_p
4094 && row_equal_p (MATRIX_ROW (desired_matrix, i - 1),
4095 MATRIX_ROW (current_matrix, j - 1), 1))
4096 --i, --j;
4097 last_new = i;
4098 last_old = j;
4099
4100 /* Nothing to do if all rows are equal. */
4101 if (last_new == first_new)
4102 return 0;
4103
4104 /* Check for integer overflow in size calculation.
4105
4106 If next_almost_prime checks (N) for divisibility by 2..10, then
4107 it can return at most N + 10, e.g., next_almost_prime (1) == 11.
4108 So, set next_almost_prime_increment_max to 10.
4109
4110 It's just a coincidence that next_almost_prime_increment_max ==
4111 NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were
4112 13, then next_almost_prime_increment_max would be 14, e.g.,
4113 because next_almost_prime (113) would be 127. */
4114 {
4115 verify (NEXT_ALMOST_PRIME_LIMIT == 11);
4116 enum { next_almost_prime_increment_max = 10 };
4117 ptrdiff_t row_table_max =
4118 (min (PTRDIFF_MAX, SIZE_MAX) / (3 * sizeof *row_table)
4119 - next_almost_prime_increment_max);
4120 ptrdiff_t current_nrows_max = row_table_max - desired_matrix->nrows;
4121 if (current_nrows_max < current_matrix->nrows)
4122 memory_full (SIZE_MAX);
4123 }
4124
4125 /* Reallocate vectors, tables etc. if necessary. */
4126
4127 if (current_matrix->nrows > old_lines_size)
4128 old_lines = xpalloc (old_lines, &old_lines_size,
4129 current_matrix->nrows - old_lines_size,
4130 INT_MAX, sizeof *old_lines);
4131
4132 if (desired_matrix->nrows > new_lines_size)
4133 new_lines = xpalloc (new_lines, &new_lines_size,
4134 desired_matrix->nrows - new_lines_size,
4135 INT_MAX, sizeof *new_lines);
4136
4137 n = desired_matrix->nrows;
4138 n += current_matrix->nrows;
4139 if (row_table_size < 3 * n)
4140 {
4141 ptrdiff_t size = next_almost_prime (3 * n);
4142 row_table = xnrealloc (row_table, size, sizeof *row_table);
4143 row_table_size = size;
4144 memset (row_table, 0, size * sizeof *row_table);
4145 }
4146
4147 if (n > row_entry_pool_size)
4148 row_entry_pool = xpalloc (row_entry_pool, &row_entry_pool_size,
4149 n - row_entry_pool_size,
4150 -1, sizeof *row_entry_pool);
4151
4152 if (desired_matrix->nrows > runs_size)
4153 {
4154 runs = xnrealloc (runs, desired_matrix->nrows, sizeof *runs);
4155 run_pool = xnrealloc (run_pool, desired_matrix->nrows, sizeof *run_pool);
4156 runs_size = desired_matrix->nrows;
4157 }
4158
4159 nruns = run_idx = 0;
4160 row_entry_idx = 0;
4161
4162 /* Add rows from the current and desired matrix to the hash table
4163 row_hash_table to be able to find equal ones quickly. */
4164
4165 for (i = first_old; i < last_old; ++i)
4166 {
4167 if (MATRIX_ROW (current_matrix, i)->enabled_p)
4168 {
4169 entry = add_row_entry (MATRIX_ROW (current_matrix, i));
4170 old_lines[i] = entry;
4171 ++entry->old_uses;
4172 }
4173 else
4174 old_lines[i] = NULL;
4175 }
4176
4177 for (i = first_new; i < last_new; ++i)
4178 {
4179 eassert (MATRIX_ROW_ENABLED_P (desired_matrix, i));
4180 entry = add_row_entry (MATRIX_ROW (desired_matrix, i));
4181 ++entry->new_uses;
4182 entry->new_line_number = i;
4183 new_lines[i] = entry;
4184 }
4185
4186 /* Identify moves based on lines that are unique and equal
4187 in both matrices. */
4188 for (i = first_old; i < last_old;)
4189 if (old_lines[i]
4190 && old_lines[i]->old_uses == 1
4191 && old_lines[i]->new_uses == 1)
4192 {
4193 int p, q;
4194 int new_line = old_lines[i]->new_line_number;
4195 struct run *run = run_pool + run_idx++;
4196
4197 /* Record move. */
4198 run->current_vpos = i;
4199 run->current_y = MATRIX_ROW (current_matrix, i)->y;
4200 run->desired_vpos = new_line;
4201 run->desired_y = MATRIX_ROW (desired_matrix, new_line)->y;
4202 run->nrows = 1;
4203 run->height = MATRIX_ROW (current_matrix, i)->height;
4204
4205 /* Extend backward. */
4206 p = i - 1;
4207 q = new_line - 1;
4208 while (p > first_old
4209 && q > first_new
4210 && old_lines[p] == new_lines[q])
4211 {
4212 int h = MATRIX_ROW (current_matrix, p)->height;
4213 --run->current_vpos;
4214 --run->desired_vpos;
4215 ++run->nrows;
4216 run->height += h;
4217 run->desired_y -= h;
4218 run->current_y -= h;
4219 --p, --q;
4220 }
4221
4222 /* Extend forward. */
4223 p = i + 1;
4224 q = new_line + 1;
4225 while (p < last_old
4226 && q < last_new
4227 && old_lines[p] == new_lines[q])
4228 {
4229 int h = MATRIX_ROW (current_matrix, p)->height;
4230 ++run->nrows;
4231 run->height += h;
4232 ++p, ++q;
4233 }
4234
4235 /* Insert run into list of all runs. Order runs by copied
4236 pixel lines. Note that we record runs that don't have to
4237 be copied because they are already in place. This is done
4238 because we can avoid calling update_window_line in this
4239 case. */
4240 for (p = 0; p < nruns && runs[p]->height > run->height; ++p)
4241 ;
4242 for (q = nruns; q > p; --q)
4243 runs[q] = runs[q - 1];
4244 runs[p] = run;
4245 ++nruns;
4246
4247 i += run->nrows;
4248 }
4249 else
4250 ++i;
4251
4252 /* Do the moves. Do it in a way that we don't overwrite something
4253 we want to copy later on. This is not solvable in general
4254 because there is only one display and we don't have a way to
4255 exchange areas on this display. Example:
4256
4257 +-----------+ +-----------+
4258 | A | | B |
4259 +-----------+ --> +-----------+
4260 | B | | A |
4261 +-----------+ +-----------+
4262
4263 Instead, prefer bigger moves, and invalidate moves that would
4264 copy from where we copied to. */
4265
4266 for (i = 0; i < nruns; ++i)
4267 if (runs[i]->nrows > 0)
4268 {
4269 struct run *r = runs[i];
4270
4271 /* Copy on the display. */
4272 if (r->current_y != r->desired_y)
4273 {
4274 rif->clear_window_mouse_face (w);
4275 rif->scroll_run_hook (w, r);
4276 }
4277
4278 /* Truncate runs that copy to where we copied to, and
4279 invalidate runs that copy from where we copied to. */
4280 for (j = nruns - 1; j > i; --j)
4281 {
4282 struct run *p = runs[j];
4283 bool truncated_p = 0;
4284
4285 if (p->nrows > 0
4286 && p->desired_y < r->desired_y + r->height
4287 && p->desired_y + p->height > r->desired_y)
4288 {
4289 if (p->desired_y < r->desired_y)
4290 {
4291 p->nrows = r->desired_vpos - p->desired_vpos;
4292 p->height = r->desired_y - p->desired_y;
4293 truncated_p = 1;
4294 }
4295 else
4296 {
4297 int nrows_copied = (r->desired_vpos + r->nrows
4298 - p->desired_vpos);
4299
4300 if (p->nrows <= nrows_copied)
4301 p->nrows = 0;
4302 else
4303 {
4304 int height_copied = (r->desired_y + r->height
4305 - p->desired_y);
4306
4307 p->current_vpos += nrows_copied;
4308 p->desired_vpos += nrows_copied;
4309 p->nrows -= nrows_copied;
4310 p->current_y += height_copied;
4311 p->desired_y += height_copied;
4312 p->height -= height_copied;
4313 truncated_p = 1;
4314 }
4315 }
4316 }
4317
4318 if (r->current_y != r->desired_y
4319 /* The condition below is equivalent to
4320 ((p->current_y >= r->desired_y
4321 && p->current_y < r->desired_y + r->height)
4322 || (p->current_y + p->height > r->desired_y
4323 && (p->current_y + p->height
4324 <= r->desired_y + r->height)))
4325 because we have 0 < p->height <= r->height. */
4326 && p->current_y < r->desired_y + r->height
4327 && p->current_y + p->height > r->desired_y)
4328 p->nrows = 0;
4329
4330 /* Reorder runs by copied pixel lines if truncated. */
4331 if (truncated_p && p->nrows > 0)
4332 {
4333 int k = nruns - 1;
4334
4335 while (runs[k]->nrows == 0 || runs[k]->height < p->height)
4336 k--;
4337 memmove (runs + j, runs + j + 1, (k - j) * sizeof (*runs));
4338 runs[k] = p;
4339 }
4340 }
4341
4342 /* Assign matrix rows. */
4343 for (j = 0; j < r->nrows; ++j)
4344 {
4345 struct glyph_row *from, *to;
4346 bool to_overlapped_p;
4347
4348 to = MATRIX_ROW (current_matrix, r->desired_vpos + j);
4349 from = MATRIX_ROW (desired_matrix, r->desired_vpos + j);
4350 to_overlapped_p = to->overlapped_p;
4351 from->redraw_fringe_bitmaps_p = from->fringe_bitmap_periodic_p;
4352 assign_row (to, from);
4353 /* The above `assign_row' actually does swap, so if we had
4354 an overlap in the copy destination of two runs, then
4355 the second run would assign a previously disabled bogus
4356 row. But thanks to the truncation code in the
4357 preceding for-loop, we no longer have such an overlap,
4358 and thus the assigned row should always be enabled. */
4359 eassert (to->enabled_p);
4360 from->enabled_p = 0;
4361 to->overlapped_p = to_overlapped_p;
4362 }
4363 }
4364
4365 /* Clear the hash table, for the next time. */
4366 for (i = 0; i < row_entry_idx; ++i)
4367 row_table[row_entry_pool[i].bucket] = NULL;
4368
4369 /* Value is 1 to indicate that we scrolled the display. */
4370 return nruns > 0;
4371 }
4372
4373
4374 \f
4375 /************************************************************************
4376 Frame-Based Updates
4377 ************************************************************************/
4378
4379 /* Update the desired frame matrix of frame F.
4380
4381 FORCE_P means that the update should not be stopped by pending input.
4382 INHIBIT_HAIRY_ID_P means that scrolling should not be tried.
4383
4384 Value is true if update was stopped due to pending input. */
4385
4386 static bool
4387 update_frame_1 (struct frame *f, bool force_p, bool inhibit_id_p)
4388 {
4389 /* Frame matrices to work on. */
4390 struct glyph_matrix *current_matrix = f->current_matrix;
4391 struct glyph_matrix *desired_matrix = f->desired_matrix;
4392 int i;
4393 bool pause_p;
4394 int preempt_count = baud_rate / 2400 + 1;
4395
4396 eassert (current_matrix && desired_matrix);
4397
4398 if (baud_rate != FRAME_COST_BAUD_RATE (f))
4399 calculate_costs (f);
4400
4401 if (preempt_count <= 0)
4402 preempt_count = 1;
4403
4404 if (!force_p && detect_input_pending_ignore_squeezables ())
4405 {
4406 pause_p = 1;
4407 goto do_pause;
4408 }
4409
4410 /* If we cannot insert/delete lines, it's no use trying it. */
4411 if (!FRAME_LINE_INS_DEL_OK (f))
4412 inhibit_id_p = 1;
4413
4414 /* See if any of the desired lines are enabled; don't compute for
4415 i/d line if just want cursor motion. */
4416 for (i = 0; i < desired_matrix->nrows; i++)
4417 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4418 break;
4419
4420 /* Try doing i/d line, if not yet inhibited. */
4421 if (!inhibit_id_p && i < desired_matrix->nrows)
4422 force_p |= scrolling (f);
4423
4424 /* Update the individual lines as needed. Do bottom line first. */
4425 if (MATRIX_ROW_ENABLED_P (desired_matrix, desired_matrix->nrows - 1))
4426 update_frame_line (f, desired_matrix->nrows - 1);
4427
4428 /* Now update the rest of the lines. */
4429 for (i = 0; i < desired_matrix->nrows - 1 && (force_p || !input_pending); i++)
4430 {
4431 if (MATRIX_ROW_ENABLED_P (desired_matrix, i))
4432 {
4433 if (FRAME_TERMCAP_P (f))
4434 {
4435 /* Flush out every so many lines.
4436 Also flush out if likely to have more than 1k buffered
4437 otherwise. I'm told that some telnet connections get
4438 really screwed by more than 1k output at once. */
4439 FILE *display_output = FRAME_TTY (f)->output;
4440 if (display_output)
4441 {
4442 ptrdiff_t outq = __fpending (display_output);
4443 if (outq > 900
4444 || (outq > 20 && ((i - 1) % preempt_count == 0)))
4445 fflush (display_output);
4446 }
4447 }
4448
4449 if (!force_p && (i - 1) % preempt_count == 0)
4450 detect_input_pending_ignore_squeezables ();
4451
4452 update_frame_line (f, i);
4453 }
4454 }
4455
4456 lint_assume (0 <= FRAME_LINES (f));
4457 pause_p = 0 < i && i < FRAME_LINES (f) - 1;
4458
4459 /* Now just clean up termcap drivers and set cursor, etc. */
4460 if (!pause_p)
4461 {
4462 if ((cursor_in_echo_area
4463 /* If we are showing a message instead of the mini-buffer,
4464 show the cursor for the message instead of for the
4465 (now hidden) mini-buffer contents. */
4466 || (EQ (minibuf_window, selected_window)
4467 && EQ (minibuf_window, echo_area_window)
4468 && !NILP (echo_area_buffer[0])))
4469 /* These cases apply only to the frame that contains
4470 the active mini-buffer window. */
4471 && FRAME_HAS_MINIBUF_P (f)
4472 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
4473 {
4474 int top = WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f)));
4475 int row, col;
4476
4477 if (cursor_in_echo_area < 0)
4478 {
4479 /* Negative value of cursor_in_echo_area means put
4480 cursor at beginning of line. */
4481 row = top;
4482 col = 0;
4483 }
4484 else
4485 {
4486 /* Positive value of cursor_in_echo_area means put
4487 cursor at the end of the prompt. If the mini-buffer
4488 is several lines high, find the last line that has
4489 any text on it. */
4490 row = FRAME_LINES (f);
4491 do
4492 {
4493 --row;
4494 col = 0;
4495
4496 if (MATRIX_ROW_ENABLED_P (current_matrix, row))
4497 {
4498 /* Frame rows are filled up with spaces that
4499 must be ignored here. */
4500 struct glyph_row *r = MATRIX_ROW (current_matrix,
4501 row);
4502 struct glyph *start = r->glyphs[TEXT_AREA];
4503 struct glyph *last = start + r->used[TEXT_AREA];
4504
4505 while (last > start
4506 && (last - 1)->charpos < 0)
4507 --last;
4508
4509 col = last - start;
4510 }
4511 }
4512 while (row > top && col == 0);
4513
4514 /* Make sure COL is not out of range. */
4515 if (col >= FRAME_CURSOR_X_LIMIT (f))
4516 {
4517 /* If we have another row, advance cursor into it. */
4518 if (row < FRAME_LINES (f) - 1)
4519 {
4520 col = FRAME_LEFT_SCROLL_BAR_COLS (f);
4521 row++;
4522 }
4523 /* Otherwise move it back in range. */
4524 else
4525 col = FRAME_CURSOR_X_LIMIT (f) - 1;
4526 }
4527 }
4528
4529 cursor_to (f, row, col);
4530 }
4531 else
4532 {
4533 /* We have only one cursor on terminal frames. Use it to
4534 display the cursor of the selected window. */
4535 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
4536 if (w->cursor.vpos >= 0
4537 /* The cursor vpos may be temporarily out of bounds
4538 in the following situation: There is one window,
4539 with the cursor in the lower half of it. The window
4540 is split, and a message causes a redisplay before
4541 a new cursor position has been computed. */
4542 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
4543 {
4544 int x = WINDOW_TO_FRAME_HPOS (w, w->cursor.hpos);
4545 int y = WINDOW_TO_FRAME_VPOS (w, w->cursor.vpos);
4546
4547 x += max (0, w->left_margin_cols);
4548 cursor_to (f, y, x);
4549 }
4550 }
4551 }
4552
4553 do_pause:
4554
4555 clear_desired_matrices (f);
4556 return pause_p;
4557 }
4558
4559
4560 /* Do line insertions/deletions on frame F for frame-based redisplay. */
4561
4562 static bool
4563 scrolling (struct frame *frame)
4564 {
4565 int unchanged_at_top, unchanged_at_bottom;
4566 int window_size;
4567 int changed_lines;
4568 int *old_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4569 int *new_hash = alloca (FRAME_LINES (frame) * sizeof (int));
4570 int *draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4571 int *old_draw_cost = alloca (FRAME_LINES (frame) * sizeof (int));
4572 register int i;
4573 int free_at_end_vpos = FRAME_LINES (frame);
4574 struct glyph_matrix *current_matrix = frame->current_matrix;
4575 struct glyph_matrix *desired_matrix = frame->desired_matrix;
4576
4577 if (!current_matrix)
4578 emacs_abort ();
4579
4580 /* Compute hash codes of all the lines. Also calculate number of
4581 changed lines, number of unchanged lines at the beginning, and
4582 number of unchanged lines at the end. */
4583 changed_lines = 0;
4584 unchanged_at_top = 0;
4585 unchanged_at_bottom = FRAME_LINES (frame);
4586 for (i = 0; i < FRAME_LINES (frame); i++)
4587 {
4588 /* Give up on this scrolling if some old lines are not enabled. */
4589 if (!MATRIX_ROW_ENABLED_P (current_matrix, i))
4590 return 0;
4591 old_hash[i] = line_hash_code (MATRIX_ROW (current_matrix, i));
4592 if (! MATRIX_ROW_ENABLED_P (desired_matrix, i))
4593 {
4594 /* This line cannot be redrawn, so don't let scrolling mess it. */
4595 new_hash[i] = old_hash[i];
4596 #define INFINITY 1000000 /* Taken from scroll.c */
4597 draw_cost[i] = INFINITY;
4598 }
4599 else
4600 {
4601 new_hash[i] = line_hash_code (MATRIX_ROW (desired_matrix, i));
4602 draw_cost[i] = line_draw_cost (desired_matrix, i);
4603 }
4604
4605 if (old_hash[i] != new_hash[i])
4606 {
4607 changed_lines++;
4608 unchanged_at_bottom = FRAME_LINES (frame) - i - 1;
4609 }
4610 else if (i == unchanged_at_top)
4611 unchanged_at_top++;
4612 old_draw_cost[i] = line_draw_cost (current_matrix, i);
4613 }
4614
4615 /* If changed lines are few, don't allow preemption, don't scroll. */
4616 if ((!FRAME_SCROLL_REGION_OK (frame)
4617 && changed_lines < baud_rate / 2400)
4618 || unchanged_at_bottom == FRAME_LINES (frame))
4619 return 1;
4620
4621 window_size = (FRAME_LINES (frame) - unchanged_at_top
4622 - unchanged_at_bottom);
4623
4624 if (FRAME_SCROLL_REGION_OK (frame))
4625 free_at_end_vpos -= unchanged_at_bottom;
4626 else if (FRAME_MEMORY_BELOW_FRAME (frame))
4627 free_at_end_vpos = -1;
4628
4629 /* If large window, fast terminal and few lines in common between
4630 current frame and desired frame, don't bother with i/d calc. */
4631 if (!FRAME_SCROLL_REGION_OK (frame)
4632 && window_size >= 18 && baud_rate > 2400
4633 && (window_size >=
4634 10 * scrolling_max_lines_saved (unchanged_at_top,
4635 FRAME_LINES (frame) - unchanged_at_bottom,
4636 old_hash, new_hash, draw_cost)))
4637 return 0;
4638
4639 if (window_size < 2)
4640 return 0;
4641
4642 scrolling_1 (frame, window_size, unchanged_at_top, unchanged_at_bottom,
4643 draw_cost + unchanged_at_top - 1,
4644 old_draw_cost + unchanged_at_top - 1,
4645 old_hash + unchanged_at_top - 1,
4646 new_hash + unchanged_at_top - 1,
4647 free_at_end_vpos - unchanged_at_top);
4648
4649 return 0;
4650 }
4651
4652
4653 /* Count the number of blanks at the start of the vector of glyphs R
4654 which is LEN glyphs long. */
4655
4656 static int
4657 count_blanks (struct glyph *r, int len)
4658 {
4659 int i;
4660
4661 for (i = 0; i < len; ++i)
4662 if (!CHAR_GLYPH_SPACE_P (r[i]))
4663 break;
4664
4665 return i;
4666 }
4667
4668
4669 /* Count the number of glyphs in common at the start of the glyph
4670 vectors STR1 and STR2. END1 is the end of STR1 and END2 is the end
4671 of STR2. Value is the number of equal glyphs equal at the start. */
4672
4673 static int
4674 count_match (struct glyph *str1, struct glyph *end1, struct glyph *str2, struct glyph *end2)
4675 {
4676 struct glyph *p1 = str1;
4677 struct glyph *p2 = str2;
4678
4679 while (p1 < end1
4680 && p2 < end2
4681 && GLYPH_CHAR_AND_FACE_EQUAL_P (p1, p2))
4682 ++p1, ++p2;
4683
4684 return p1 - str1;
4685 }
4686
4687
4688 /* Char insertion/deletion cost vector, from term.c */
4689
4690 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_TOTAL_COLS ((f))])
4691
4692
4693 /* Perform a frame-based update on line VPOS in frame FRAME. */
4694
4695 static void
4696 update_frame_line (struct frame *f, int vpos)
4697 {
4698 struct glyph *obody, *nbody, *op1, *op2, *np1, *nend;
4699 int tem;
4700 int osp, nsp, begmatch, endmatch, olen, nlen;
4701 struct glyph_matrix *current_matrix = f->current_matrix;
4702 struct glyph_matrix *desired_matrix = f->desired_matrix;
4703 struct glyph_row *current_row = MATRIX_ROW (current_matrix, vpos);
4704 struct glyph_row *desired_row = MATRIX_ROW (desired_matrix, vpos);
4705 bool must_write_whole_line_p;
4706 bool write_spaces_p = FRAME_MUST_WRITE_SPACES (f);
4707 bool colored_spaces_p = (FACE_FROM_ID (f, DEFAULT_FACE_ID)->background
4708 != FACE_TTY_DEFAULT_BG_COLOR);
4709
4710 if (colored_spaces_p)
4711 write_spaces_p = 1;
4712
4713 /* Current row not enabled means it has unknown contents. We must
4714 write the whole desired line in that case. */
4715 must_write_whole_line_p = !current_row->enabled_p;
4716 if (must_write_whole_line_p)
4717 {
4718 obody = 0;
4719 olen = 0;
4720 }
4721 else
4722 {
4723 obody = MATRIX_ROW_GLYPH_START (current_matrix, vpos);
4724 olen = current_row->used[TEXT_AREA];
4725
4726 /* Ignore trailing spaces, if we can. */
4727 if (!write_spaces_p)
4728 while (olen > 0 && CHAR_GLYPH_SPACE_P (obody[olen-1]))
4729 olen--;
4730 }
4731
4732 current_row->enabled_p = 1;
4733 current_row->used[TEXT_AREA] = desired_row->used[TEXT_AREA];
4734
4735 /* If desired line is empty, just clear the line. */
4736 if (!desired_row->enabled_p)
4737 {
4738 nlen = 0;
4739 goto just_erase;
4740 }
4741
4742 nbody = desired_row->glyphs[TEXT_AREA];
4743 nlen = desired_row->used[TEXT_AREA];
4744 nend = nbody + nlen;
4745
4746 /* If display line has unknown contents, write the whole line. */
4747 if (must_write_whole_line_p)
4748 {
4749 /* Ignore spaces at the end, if we can. */
4750 if (!write_spaces_p)
4751 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4752 --nlen;
4753
4754 /* Write the contents of the desired line. */
4755 if (nlen)
4756 {
4757 cursor_to (f, vpos, 0);
4758 write_glyphs (f, nbody, nlen);
4759 }
4760
4761 /* Don't call clear_end_of_line if we already wrote the whole
4762 line. The cursor will not be at the right margin in that
4763 case but in the line below. */
4764 if (nlen < FRAME_TOTAL_COLS (f))
4765 {
4766 cursor_to (f, vpos, nlen);
4767 clear_end_of_line (f, FRAME_TOTAL_COLS (f));
4768 }
4769 else
4770 /* Make sure we are in the right row, otherwise cursor movement
4771 with cmgoto might use `ch' in the wrong row. */
4772 cursor_to (f, vpos, 0);
4773
4774 make_current (desired_matrix, current_matrix, vpos);
4775 return;
4776 }
4777
4778 /* Pretend trailing spaces are not there at all,
4779 unless for one reason or another we must write all spaces. */
4780 if (!write_spaces_p)
4781 while (nlen > 0 && CHAR_GLYPH_SPACE_P (nbody[nlen - 1]))
4782 nlen--;
4783
4784 /* If there's no i/d char, quickly do the best we can without it. */
4785 if (!FRAME_CHAR_INS_DEL_OK (f))
4786 {
4787 int i, j;
4788
4789 /* Find the first glyph in desired row that doesn't agree with
4790 a glyph in the current row, and write the rest from there on. */
4791 for (i = 0; i < nlen; i++)
4792 {
4793 if (i >= olen || !GLYPH_EQUAL_P (nbody + i, obody + i))
4794 {
4795 /* Find the end of the run of different glyphs. */
4796 j = i + 1;
4797 while (j < nlen
4798 && (j >= olen
4799 || !GLYPH_EQUAL_P (nbody + j, obody + j)
4800 || CHAR_GLYPH_PADDING_P (nbody[j])))
4801 ++j;
4802
4803 /* Output this run of non-matching chars. */
4804 cursor_to (f, vpos, i);
4805 write_glyphs (f, nbody + i, j - i);
4806 i = j - 1;
4807
4808 /* Now find the next non-match. */
4809 }
4810 }
4811
4812 /* Clear the rest of the line, or the non-clear part of it. */
4813 if (olen > nlen)
4814 {
4815 cursor_to (f, vpos, nlen);
4816 clear_end_of_line (f, olen);
4817 }
4818
4819 /* Make current row = desired row. */
4820 make_current (desired_matrix, current_matrix, vpos);
4821 return;
4822 }
4823
4824 /* Here when CHAR_INS_DEL_OK != 0, i.e. we can insert or delete
4825 characters in a row. */
4826
4827 if (!olen)
4828 {
4829 /* If current line is blank, skip over initial spaces, if
4830 possible, and write the rest. */
4831 if (write_spaces_p)
4832 nsp = 0;
4833 else
4834 nsp = count_blanks (nbody, nlen);
4835
4836 if (nlen > nsp)
4837 {
4838 cursor_to (f, vpos, nsp);
4839 write_glyphs (f, nbody + nsp, nlen - nsp);
4840 }
4841
4842 /* Exchange contents between current_frame and new_frame. */
4843 make_current (desired_matrix, current_matrix, vpos);
4844 return;
4845 }
4846
4847 /* Compute number of leading blanks in old and new contents. */
4848 osp = count_blanks (obody, olen);
4849 nsp = (colored_spaces_p ? 0 : count_blanks (nbody, nlen));
4850
4851 /* Compute number of matching chars starting with first non-blank. */
4852 begmatch = count_match (obody + osp, obody + olen,
4853 nbody + nsp, nbody + nlen);
4854
4855 /* Spaces in new match implicit space past the end of old. */
4856 /* A bug causing this to be a no-op was fixed in 18.29. */
4857 if (!write_spaces_p && osp + begmatch == olen)
4858 {
4859 np1 = nbody + nsp;
4860 while (np1 + begmatch < nend && CHAR_GLYPH_SPACE_P (np1[begmatch]))
4861 ++begmatch;
4862 }
4863
4864 /* Avoid doing insert/delete char
4865 just cause number of leading spaces differs
4866 when the following text does not match. */
4867 if (begmatch == 0 && osp != nsp)
4868 osp = nsp = min (osp, nsp);
4869
4870 /* Find matching characters at end of line */
4871 op1 = obody + olen;
4872 np1 = nbody + nlen;
4873 op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
4874 while (op1 > op2
4875 && GLYPH_EQUAL_P (op1 - 1, np1 - 1))
4876 {
4877 op1--;
4878 np1--;
4879 }
4880 endmatch = obody + olen - op1;
4881
4882 /* tem gets the distance to insert or delete.
4883 endmatch is how many characters we save by doing so.
4884 Is it worth it? */
4885
4886 tem = (nlen - nsp) - (olen - osp);
4887 if (endmatch && tem
4888 && (!FRAME_CHAR_INS_DEL_OK (f)
4889 || endmatch <= char_ins_del_cost (f)[tem]))
4890 endmatch = 0;
4891
4892 /* nsp - osp is the distance to insert or delete.
4893 If that is nonzero, begmatch is known to be nonzero also.
4894 begmatch + endmatch is how much we save by doing the ins/del.
4895 Is it worth it? */
4896
4897 if (nsp != osp
4898 && (!FRAME_CHAR_INS_DEL_OK (f)
4899 || begmatch + endmatch <= char_ins_del_cost (f)[nsp - osp]))
4900 {
4901 begmatch = 0;
4902 endmatch = 0;
4903 osp = nsp = min (osp, nsp);
4904 }
4905
4906 /* Now go through the line, inserting, writing and
4907 deleting as appropriate. */
4908
4909 if (osp > nsp)
4910 {
4911 cursor_to (f, vpos, nsp);
4912 delete_glyphs (f, osp - nsp);
4913 }
4914 else if (nsp > osp)
4915 {
4916 /* If going to delete chars later in line
4917 and insert earlier in the line,
4918 must delete first to avoid losing data in the insert */
4919 if (endmatch && nlen < olen + nsp - osp)
4920 {
4921 cursor_to (f, vpos, nlen - endmatch + osp - nsp);
4922 delete_glyphs (f, olen + nsp - osp - nlen);
4923 olen = nlen - (nsp - osp);
4924 }
4925 cursor_to (f, vpos, osp);
4926 insert_glyphs (f, 0, nsp - osp);
4927 }
4928 olen += nsp - osp;
4929
4930 tem = nsp + begmatch + endmatch;
4931 if (nlen != tem || olen != tem)
4932 {
4933 if (!endmatch || nlen == olen)
4934 {
4935 /* If new text being written reaches right margin, there is
4936 no need to do clear-to-eol at the end of this function
4937 (and it would not be safe, since cursor is not going to
4938 be "at the margin" after the text is done). */
4939 if (nlen == FRAME_TOTAL_COLS (f))
4940 olen = 0;
4941
4942 /* Function write_glyphs is prepared to do nothing
4943 if passed a length <= 0. Check it here to avoid
4944 unnecessary cursor movement. */
4945 if (nlen - tem > 0)
4946 {
4947 cursor_to (f, vpos, nsp + begmatch);
4948 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
4949 }
4950 }
4951 else if (nlen > olen)
4952 {
4953 /* Here, we used to have the following simple code:
4954 ----------------------------------------
4955 write_glyphs (nbody + nsp + begmatch, olen - tem);
4956 insert_glyphs (nbody + nsp + begmatch + olen - tem, nlen - olen);
4957 ----------------------------------------
4958 but it doesn't work if nbody[nsp + begmatch + olen - tem]
4959 is a padding glyph. */
4960 int out = olen - tem; /* Columns to be overwritten originally. */
4961 int del;
4962
4963 cursor_to (f, vpos, nsp + begmatch);
4964
4965 /* Calculate columns we can actually overwrite. */
4966 while (CHAR_GLYPH_PADDING_P (nbody[nsp + begmatch + out]))
4967 out--;
4968 write_glyphs (f, nbody + nsp + begmatch, out);
4969
4970 /* If we left columns to be overwritten, we must delete them. */
4971 del = olen - tem - out;
4972 if (del > 0)
4973 delete_glyphs (f, del);
4974
4975 /* At last, we insert columns not yet written out. */
4976 insert_glyphs (f, nbody + nsp + begmatch + out, nlen - olen + del);
4977 olen = nlen;
4978 }
4979 else if (olen > nlen)
4980 {
4981 cursor_to (f, vpos, nsp + begmatch);
4982 write_glyphs (f, nbody + nsp + begmatch, nlen - tem);
4983 delete_glyphs (f, olen - nlen);
4984 olen = nlen;
4985 }
4986 }
4987
4988 just_erase:
4989 /* If any unerased characters remain after the new line, erase them. */
4990 if (olen > nlen)
4991 {
4992 cursor_to (f, vpos, nlen);
4993 clear_end_of_line (f, olen);
4994 }
4995
4996 /* Exchange contents between current_frame and new_frame. */
4997 make_current (desired_matrix, current_matrix, vpos);
4998 }
4999
5000
5001 \f
5002 /***********************************************************************
5003 X/Y Position -> Buffer Position
5004 ***********************************************************************/
5005
5006 /* Determine what's under window-relative pixel position (*X, *Y).
5007 Return the OBJECT (string or buffer) that's there.
5008 Return in *POS the position in that object.
5009 Adjust *X and *Y to character positions.
5010 Return in *DX and *DY the pixel coordinates of the click,
5011 relative to the top left corner of OBJECT, or relative to
5012 the top left corner of the character glyph at (*X, *Y)
5013 if OBJECT is nil.
5014 Return WIDTH and HEIGHT of the object at (*X, *Y), or zero
5015 if the coordinates point to an empty area of the display. */
5016
5017 Lisp_Object
5018 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)
5019 {
5020 struct it it;
5021 Lisp_Object old_current_buffer = Fcurrent_buffer ();
5022 struct text_pos startp;
5023 Lisp_Object string;
5024 struct glyph_row *row;
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 struct image *img = 0;
5027 #endif
5028 int x0, x1, to_x;
5029 void *itdata = NULL;
5030
5031 /* We used to set current_buffer directly here, but that does the
5032 wrong thing with `face-remapping-alist' (bug#2044). */
5033 Fset_buffer (w->contents);
5034 itdata = bidi_shelve_cache ();
5035 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
5036 start_display (&it, w, startp);
5037 /* start_display takes into account the header-line row, but IT's
5038 vpos still counts from the glyph row that includes the window's
5039 start position. Adjust for a possible header-line row. */
5040 it.vpos += WINDOW_WANTS_HEADER_LINE_P (w);
5041
5042 x0 = *x;
5043
5044 /* First, move to the beginning of the row corresponding to *Y. We
5045 need to be in that row to get the correct value of base paragraph
5046 direction for the text at (*X, *Y). */
5047 move_it_to (&it, -1, 0, *y, -1, MOVE_TO_X | MOVE_TO_Y);
5048
5049 /* TO_X is the pixel position that the iterator will compute for the
5050 glyph at *X. We add it.first_visible_x because iterator
5051 positions include the hscroll. */
5052 to_x = x0 + it.first_visible_x;
5053 if (it.bidi_it.paragraph_dir == R2L)
5054 /* For lines in an R2L paragraph, we need to mirror TO_X wrt the
5055 text area. This is because the iterator, even in R2L
5056 paragraphs, delivers glyphs as if they started at the left
5057 margin of the window. (When we actually produce glyphs for
5058 display, we reverse their order in PRODUCE_GLYPHS, but the
5059 iterator doesn't know about that.) The following line adjusts
5060 the pixel position to the iterator geometry, which is what
5061 move_it_* routines use. (The -1 is because in a window whose
5062 text-area width is W, the rightmost pixel position is W-1, and
5063 it should be mirrored into zero pixel position.) */
5064 to_x = window_box_width (w, TEXT_AREA) - to_x - 1;
5065
5066 /* Now move horizontally in the row to the glyph under *X. Second
5067 argument is ZV to prevent move_it_in_display_line from matching
5068 based on buffer positions. */
5069 move_it_in_display_line (&it, ZV, to_x, MOVE_TO_X);
5070 bidi_unshelve_cache (itdata, 0);
5071
5072 Fset_buffer (old_current_buffer);
5073
5074 *dx = x0 + it.first_visible_x - it.current_x;
5075 *dy = *y - it.current_y;
5076
5077 string = w->contents;
5078 if (STRINGP (it.string))
5079 string = it.string;
5080 *pos = it.current;
5081 if (it.what == IT_COMPOSITION
5082 && it.cmp_it.nchars > 1
5083 && it.cmp_it.reversed_p)
5084 {
5085 /* The current display element is a grapheme cluster in a
5086 composition. In that case, we need the position of the first
5087 character of the cluster. But, as it.cmp_it.reversed_p is 1,
5088 it.current points to the last character of the cluster, thus
5089 we must move back to the first character of the same
5090 cluster. */
5091 CHARPOS (pos->pos) -= it.cmp_it.nchars - 1;
5092 if (STRINGP (it.string))
5093 BYTEPOS (pos->pos) = string_char_to_byte (string, CHARPOS (pos->pos));
5094 else
5095 BYTEPOS (pos->pos) = buf_charpos_to_bytepos (XBUFFER (w->contents),
5096 CHARPOS (pos->pos));
5097 }
5098
5099 #ifdef HAVE_WINDOW_SYSTEM
5100 if (it.what == IT_IMAGE)
5101 {
5102 if ((img = IMAGE_FROM_ID (it.f, it.image_id)) != NULL
5103 && !NILP (img->spec))
5104 *object = img->spec;
5105 }
5106 #endif
5107
5108 if (it.vpos < w->current_matrix->nrows
5109 && (row = MATRIX_ROW (w->current_matrix, it.vpos),
5110 row->enabled_p))
5111 {
5112 if (it.hpos < row->used[TEXT_AREA])
5113 {
5114 struct glyph *glyph = row->glyphs[TEXT_AREA] + it.hpos;
5115 #ifdef HAVE_WINDOW_SYSTEM
5116 if (img)
5117 {
5118 *dy -= row->ascent - glyph->ascent;
5119 *dx += glyph->slice.img.x;
5120 *dy += glyph->slice.img.y;
5121 /* Image slices positions are still relative to the entire image */
5122 *width = img->width;
5123 *height = img->height;
5124 }
5125 else
5126 #endif
5127 {
5128 *width = glyph->pixel_width;
5129 *height = glyph->ascent + glyph->descent;
5130 }
5131 }
5132 else
5133 {
5134 *width = 0;
5135 *height = row->height;
5136 }
5137 }
5138 else
5139 {
5140 *width = *height = 0;
5141 }
5142
5143 /* Add extra (default width) columns if clicked after EOL. */
5144 x1 = max (0, it.current_x + it.pixel_width - it.first_visible_x);
5145 if (x0 > x1)
5146 it.hpos += (x0 - x1) / WINDOW_FRAME_COLUMN_WIDTH (w);
5147
5148 *x = it.hpos;
5149 *y = it.vpos;
5150
5151 return string;
5152 }
5153
5154
5155 /* Value is the string under window-relative coordinates X/Y in the
5156 mode line or header line (PART says which) of window W, or nil if none.
5157 *CHARPOS is set to the position in the string returned. */
5158
5159 Lisp_Object
5160 mode_line_string (struct window *w, enum window_part part,
5161 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5162 int *dx, int *dy, int *width, int *height)
5163 {
5164 struct glyph_row *row;
5165 struct glyph *glyph, *end;
5166 int x0, y0;
5167 Lisp_Object string = Qnil;
5168
5169 if (part == ON_MODE_LINE)
5170 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
5171 else
5172 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
5173 y0 = *y - row->y;
5174 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5175
5176 if (row->mode_line_p && row->enabled_p)
5177 {
5178 /* Find the glyph under X. If we find one with a string object,
5179 it's the one we were looking for. */
5180 glyph = row->glyphs[TEXT_AREA];
5181 end = glyph + row->used[TEXT_AREA];
5182 for (x0 = *x; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5183 x0 -= glyph->pixel_width;
5184 *x = glyph - row->glyphs[TEXT_AREA];
5185 if (glyph < end)
5186 {
5187 string = glyph->object;
5188 *charpos = glyph->charpos;
5189 *width = glyph->pixel_width;
5190 *height = glyph->ascent + glyph->descent;
5191 #ifdef HAVE_WINDOW_SYSTEM
5192 if (glyph->type == IMAGE_GLYPH)
5193 {
5194 struct image *img;
5195 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5196 if (img != NULL)
5197 *object = img->spec;
5198 y0 -= row->ascent - glyph->ascent;
5199 }
5200 #endif
5201 }
5202 else
5203 {
5204 /* Add extra (default width) columns if clicked after EOL. */
5205 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5206 *width = 0;
5207 *height = row->height;
5208 }
5209 }
5210 else
5211 {
5212 *x = 0;
5213 x0 = 0;
5214 *width = *height = 0;
5215 }
5216
5217 *dx = x0;
5218 *dy = y0;
5219
5220 return string;
5221 }
5222
5223
5224 /* Value is the string under window-relative coordinates X/Y in either
5225 marginal area, or nil if none. *CHARPOS is set to the position in
5226 the string returned. */
5227
5228 Lisp_Object
5229 marginal_area_string (struct window *w, enum window_part part,
5230 int *x, int *y, ptrdiff_t *charpos, Lisp_Object *object,
5231 int *dx, int *dy, int *width, int *height)
5232 {
5233 struct glyph_row *row = w->current_matrix->rows;
5234 struct glyph *glyph, *end;
5235 int x0, y0, i, wy = *y;
5236 int area;
5237 Lisp_Object string = Qnil;
5238
5239 if (part == ON_LEFT_MARGIN)
5240 area = LEFT_MARGIN_AREA;
5241 else if (part == ON_RIGHT_MARGIN)
5242 area = RIGHT_MARGIN_AREA;
5243 else
5244 emacs_abort ();
5245
5246 for (i = 0; row->enabled_p && i < w->current_matrix->nrows; ++i, ++row)
5247 if (wy >= row->y && wy < MATRIX_ROW_BOTTOM_Y (row))
5248 break;
5249 y0 = *y - row->y;
5250 *y = row - MATRIX_FIRST_TEXT_ROW (w->current_matrix);
5251
5252 if (row->enabled_p)
5253 {
5254 /* Find the glyph under X. If we find one with a string object,
5255 it's the one we were looking for. */
5256 if (area == RIGHT_MARGIN_AREA)
5257 x0 = ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5258 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5259 : WINDOW_TOTAL_FRINGE_WIDTH (w))
5260 + window_box_width (w, LEFT_MARGIN_AREA)
5261 + window_box_width (w, TEXT_AREA));
5262 else
5263 x0 = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
5264 ? WINDOW_LEFT_FRINGE_WIDTH (w)
5265 : 0);
5266
5267 glyph = row->glyphs[area];
5268 end = glyph + row->used[area];
5269 for (x0 = *x - x0; glyph < end && x0 >= glyph->pixel_width; ++glyph)
5270 x0 -= glyph->pixel_width;
5271 *x = glyph - row->glyphs[area];
5272 if (glyph < end)
5273 {
5274 string = glyph->object;
5275 *charpos = glyph->charpos;
5276 *width = glyph->pixel_width;
5277 *height = glyph->ascent + glyph->descent;
5278 #ifdef HAVE_WINDOW_SYSTEM
5279 if (glyph->type == IMAGE_GLYPH)
5280 {
5281 struct image *img;
5282 img = IMAGE_FROM_ID (WINDOW_XFRAME (w), glyph->u.img_id);
5283 if (img != NULL)
5284 *object = img->spec;
5285 y0 -= row->ascent - glyph->ascent;
5286 x0 += glyph->slice.img.x;
5287 y0 += glyph->slice.img.y;
5288 }
5289 #endif
5290 }
5291 else
5292 {
5293 /* Add extra (default width) columns if clicked after EOL. */
5294 *x += x0 / WINDOW_FRAME_COLUMN_WIDTH (w);
5295 *width = 0;
5296 *height = row->height;
5297 }
5298 }
5299 else
5300 {
5301 x0 = 0;
5302 *x = 0;
5303 *width = *height = 0;
5304 }
5305
5306 *dx = x0;
5307 *dy = y0;
5308
5309 return string;
5310 }
5311
5312
5313 /***********************************************************************
5314 Changing Frame Sizes
5315 ***********************************************************************/
5316
5317 #ifdef SIGWINCH
5318
5319 static void deliver_window_change_signal (int);
5320
5321 static void
5322 handle_window_change_signal (int sig)
5323 {
5324 int width, height;
5325 struct tty_display_info *tty;
5326
5327 /* The frame size change obviously applies to a single
5328 termcap-controlled terminal, but we can't decide which.
5329 Therefore, we resize the frames corresponding to each tty.
5330 */
5331 for (tty = tty_list; tty; tty = tty->next) {
5332
5333 if (! tty->term_initted)
5334 continue;
5335
5336 /* Suspended tty frames have tty->input == NULL avoid trying to
5337 use it. */
5338 if (!tty->input)
5339 continue;
5340
5341 get_tty_size (fileno (tty->input), &width, &height);
5342
5343 if (width > 5 && height > 2) {
5344 Lisp_Object tail, frame;
5345
5346 FOR_EACH_FRAME (tail, frame)
5347 if (FRAME_TERMCAP_P (XFRAME (frame)) && FRAME_TTY (XFRAME (frame)) == tty)
5348 /* Record the new sizes, but don't reallocate the data
5349 structures now. Let that be done later outside of the
5350 signal handler. */
5351 change_frame_size (XFRAME (frame), height, width, 0, 1, 0);
5352 }
5353 }
5354 }
5355
5356 static void
5357 deliver_window_change_signal (int sig)
5358 {
5359 deliver_process_signal (sig, handle_window_change_signal);
5360 }
5361 #endif /* SIGWINCH */
5362
5363
5364 /* Do any change in frame size that was requested by a signal.
5365 SAFE means this function is called from a place where it is
5366 safe to change frame sizes while a redisplay is in progress. */
5367
5368 void
5369 do_pending_window_change (bool safe)
5370 {
5371 /* If window change signal handler should have run before, run it now. */
5372 if (redisplaying_p && !safe)
5373 return;
5374
5375 while (delayed_size_change)
5376 {
5377 Lisp_Object tail, frame;
5378
5379 delayed_size_change = 0;
5380
5381 FOR_EACH_FRAME (tail, frame)
5382 {
5383 struct frame *f = XFRAME (frame);
5384
5385 if (f->new_text_lines != 0 || f->new_text_cols != 0)
5386 change_frame_size (f, f->new_text_lines, f->new_text_cols,
5387 0, 0, safe);
5388 }
5389 }
5390 }
5391
5392
5393 /* Change the frame height and/or width. Values may be given as zero to
5394 indicate no change is to take place.
5395
5396 If DELAY, assume we're being called from a signal handler, and
5397 queue the change for later - perhaps the next redisplay.
5398 Since this tries to resize windows, we can't call it
5399 from a signal handler.
5400
5401 SAFE means this function is called from a place where it's
5402 safe to change frame sizes while a redisplay is in progress. */
5403
5404 void
5405 change_frame_size (struct frame *f, int newheight, int newwidth,
5406 bool pretend, bool delay, bool safe)
5407 {
5408 Lisp_Object tail, frame;
5409
5410 if (FRAME_MSDOS_P (f))
5411 {
5412 /* On MS-DOS, all frames use the same screen, so a change in
5413 size affects all frames. Termcap now supports multiple
5414 ttys. */
5415 FOR_EACH_FRAME (tail, frame)
5416 if (! FRAME_WINDOW_P (XFRAME (frame)))
5417 change_frame_size_1 (XFRAME (frame), newheight, newwidth,
5418 pretend, delay, safe);
5419 }
5420 else
5421 change_frame_size_1 (f, newheight, newwidth, pretend, delay, safe);
5422 }
5423
5424 static void
5425 change_frame_size_1 (struct frame *f, int newheight, int newwidth,
5426 bool pretend, bool delay, bool safe)
5427 {
5428 int new_frame_total_cols;
5429 ptrdiff_t count = SPECPDL_INDEX ();
5430
5431 /* If we can't deal with the change now, queue it for later. */
5432 if (delay || (redisplaying_p && !safe))
5433 {
5434 f->new_text_lines = newheight;
5435 f->new_text_cols = newwidth;
5436 delayed_size_change = 1;
5437 return;
5438 }
5439
5440 /* This size-change overrides any pending one for this frame. */
5441 f->new_text_lines = 0;
5442 f->new_text_cols = 0;
5443
5444 /* If an argument is zero, set it to the current value. */
5445 if (newheight == 0)
5446 newheight = FRAME_LINES (f);
5447 if (newwidth == 0)
5448 newwidth = FRAME_COLS (f);
5449
5450 /* Compute width of windows in F. */
5451 /* Round up to the smallest acceptable size. */
5452 check_frame_size (f, &newheight, &newwidth);
5453
5454 /* This is the width of the frame with vertical scroll bars and fringe
5455 columns. Do this after rounding - see discussion of bug#9723. */
5456 new_frame_total_cols = FRAME_TOTAL_COLS_ARG (f, newwidth);
5457
5458 /* If we're not changing the frame size, quit now. */
5459 /* Frame width may be unchanged but the text portion may change, for
5460 example, fullscreen and remove/add scroll bar. */
5461 if (newheight == FRAME_LINES (f)
5462 /* Text portion unchanged? */
5463 && newwidth == FRAME_COLS (f)
5464 /* Frame width unchanged? */
5465 && new_frame_total_cols == FRAME_TOTAL_COLS (f))
5466 return;
5467
5468 block_input ();
5469
5470 #ifdef MSDOS
5471 /* We only can set screen dimensions to certain values supported
5472 by our video hardware. Try to find the smallest size greater
5473 or equal to the requested dimensions. */
5474 dos_set_window_size (&newheight, &newwidth);
5475 #endif
5476
5477 if (newheight != FRAME_LINES (f))
5478 {
5479 resize_frame_windows (f, newheight, 0);
5480
5481 /* MSDOS frames cannot PRETEND, as they change frame size by
5482 manipulating video hardware. */
5483 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5484 FrameRows (FRAME_TTY (f)) = newheight;
5485 }
5486
5487 if (new_frame_total_cols != FRAME_TOTAL_COLS (f))
5488 {
5489 resize_frame_windows (f, new_frame_total_cols, 1);
5490
5491 /* MSDOS frames cannot PRETEND, as they change frame size by
5492 manipulating video hardware. */
5493 if ((FRAME_TERMCAP_P (f) && !pretend) || FRAME_MSDOS_P (f))
5494 FrameCols (FRAME_TTY (f)) = newwidth;
5495
5496 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5497 if (WINDOWP (f->tool_bar_window))
5498 XWINDOW (f->tool_bar_window)->total_cols = newwidth;
5499 #endif
5500 }
5501
5502 FRAME_LINES (f) = newheight;
5503 SET_FRAME_COLS (f, newwidth);
5504
5505 {
5506 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
5507 int text_area_x, text_area_y, text_area_width, text_area_height;
5508
5509 window_box (w, TEXT_AREA, &text_area_x, &text_area_y, &text_area_width,
5510 &text_area_height);
5511 if (w->cursor.x >= text_area_x + text_area_width)
5512 w->cursor.hpos = w->cursor.x = 0;
5513 if (w->cursor.y >= text_area_y + text_area_height)
5514 w->cursor.vpos = w->cursor.y = 0;
5515 }
5516
5517 adjust_frame_glyphs (f);
5518 calculate_costs (f);
5519 SET_FRAME_GARBAGED (f);
5520 f->resized_p = 1;
5521
5522 unblock_input ();
5523
5524 record_unwind_current_buffer ();
5525
5526 run_window_configuration_change_hook (f);
5527
5528 unbind_to (count, Qnil);
5529 }
5530
5531
5532 \f
5533 /***********************************************************************
5534 Terminal Related Lisp Functions
5535 ***********************************************************************/
5536
5537 DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
5538 1, 1, "FOpen termscript file: ",
5539 doc: /* Start writing all terminal output to FILE as well as the terminal.
5540 FILE = nil means just close any termscript file currently open. */)
5541 (Lisp_Object file)
5542 {
5543 struct tty_display_info *tty;
5544
5545 if (! FRAME_TERMCAP_P (SELECTED_FRAME ())
5546 && ! FRAME_MSDOS_P (SELECTED_FRAME ()))
5547 error ("Current frame is not on a tty device");
5548
5549 tty = CURTTY ();
5550
5551 if (tty->termscript != 0)
5552 {
5553 block_input ();
5554 fclose (tty->termscript);
5555 tty->termscript = 0;
5556 unblock_input ();
5557 }
5558
5559 if (! NILP (file))
5560 {
5561 file = Fexpand_file_name (file, Qnil);
5562 tty->termscript = emacs_fopen (SSDATA (file), "w");
5563 if (tty->termscript == 0)
5564 report_file_error ("Opening termscript", file);
5565 }
5566 return Qnil;
5567 }
5568
5569
5570 DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
5571 Ssend_string_to_terminal, 1, 2, 0,
5572 doc: /* Send STRING to the terminal without alteration.
5573 Control characters in STRING will have terminal-dependent effects.
5574
5575 Optional parameter TERMINAL specifies the tty terminal device to use.
5576 It may be a terminal object, a frame, or nil for the terminal used by
5577 the currently selected frame. In batch mode, STRING is sent to stdout
5578 when TERMINAL is nil. */)
5579 (Lisp_Object string, Lisp_Object terminal)
5580 {
5581 struct terminal *t = get_terminal (terminal, 1);
5582 FILE *out;
5583
5584 /* ??? Perhaps we should do something special for multibyte strings here. */
5585 CHECK_STRING (string);
5586 block_input ();
5587
5588 if (!t)
5589 error ("Unknown terminal device");
5590
5591 if (t->type == output_initial)
5592 out = stdout;
5593 else if (t->type != output_termcap && t->type != output_msdos_raw)
5594 error ("Device %d is not a termcap terminal device", t->id);
5595 else
5596 {
5597 struct tty_display_info *tty = t->display_info.tty;
5598
5599 if (! tty->output)
5600 error ("Terminal is currently suspended");
5601
5602 if (tty->termscript)
5603 {
5604 fwrite (SDATA (string), 1, SBYTES (string), tty->termscript);
5605 fflush (tty->termscript);
5606 }
5607 out = tty->output;
5608 }
5609 fwrite (SDATA (string), 1, SBYTES (string), out);
5610 fflush (out);
5611 unblock_input ();
5612 return Qnil;
5613 }
5614
5615
5616 DEFUN ("ding", Fding, Sding, 0, 1, 0,
5617 doc: /* Beep, or flash the screen.
5618 Also, unless an argument is given,
5619 terminate any keyboard macro currently executing. */)
5620 (Lisp_Object arg)
5621 {
5622 if (!NILP (arg))
5623 {
5624 if (noninteractive)
5625 putchar (07);
5626 else
5627 ring_bell (XFRAME (selected_frame));
5628 }
5629 else
5630 bitch_at_user ();
5631
5632 return Qnil;
5633 }
5634
5635 void
5636 bitch_at_user (void)
5637 {
5638 if (noninteractive)
5639 putchar (07);
5640 else if (!INTERACTIVE) /* Stop executing a keyboard macro. */
5641 {
5642 const char *msg
5643 = "Keyboard macro terminated by a command ringing the bell";
5644 Fsignal (Quser_error, list1 (build_string (msg)));
5645 }
5646 else
5647 ring_bell (XFRAME (selected_frame));
5648 }
5649
5650
5651 \f
5652 /***********************************************************************
5653 Sleeping, Waiting
5654 ***********************************************************************/
5655
5656 DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 2, 0,
5657 doc: /* Pause, without updating display, for SECONDS seconds.
5658 SECONDS may be a floating-point value, meaning that you can wait for a
5659 fraction of a second. Optional second arg MILLISECONDS specifies an
5660 additional wait period, in milliseconds; this is for backwards compatibility.
5661 \(Not all operating systems support waiting for a fraction of a second.) */)
5662 (Lisp_Object seconds, Lisp_Object milliseconds)
5663 {
5664 double duration = extract_float (seconds);
5665
5666 if (!NILP (milliseconds))
5667 {
5668 CHECK_NUMBER (milliseconds);
5669 duration += XINT (milliseconds) / 1000.0;
5670 }
5671
5672 if (duration > 0)
5673 {
5674 struct timespec t = dtotimespec (duration);
5675 wait_reading_process_output (min (t.tv_sec, WAIT_READING_MAX),
5676 t.tv_nsec, 0, 0, Qnil, NULL, 0);
5677 }
5678
5679 return Qnil;
5680 }
5681
5682
5683 /* This is just like wait_reading_process_output, except that
5684 it does redisplay.
5685
5686 TIMEOUT is number of seconds to wait (float or integer),
5687 or t to wait forever.
5688 READING is true if reading input.
5689 If DISPLAY_OPTION is >0 display process output while waiting.
5690 If DISPLAY_OPTION is >1 perform an initial redisplay before waiting.
5691 */
5692
5693 Lisp_Object
5694 sit_for (Lisp_Object timeout, bool reading, int display_option)
5695 {
5696 intmax_t sec;
5697 int nsec;
5698 bool do_display = display_option > 0;
5699
5700 swallow_events (do_display);
5701
5702 if ((detect_input_pending_run_timers (do_display))
5703 || !NILP (Vexecuting_kbd_macro))
5704 return Qnil;
5705
5706 if (display_option > 1)
5707 redisplay_preserve_echo_area (2);
5708
5709 if (INTEGERP (timeout))
5710 {
5711 sec = XINT (timeout);
5712 if (sec <= 0)
5713 return Qt;
5714 nsec = 0;
5715 }
5716 else if (FLOATP (timeout))
5717 {
5718 double seconds = XFLOAT_DATA (timeout);
5719 if (! (0 < seconds))
5720 return Qt;
5721 else
5722 {
5723 struct timespec t = dtotimespec (seconds);
5724 sec = min (t.tv_sec, WAIT_READING_MAX);
5725 nsec = t.tv_nsec;
5726 }
5727 }
5728 else if (EQ (timeout, Qt))
5729 {
5730 sec = 0;
5731 nsec = 0;
5732 }
5733 else
5734 wrong_type_argument (Qnumberp, timeout);
5735
5736
5737 #ifdef USABLE_SIGIO
5738 gobble_input ();
5739 #endif
5740
5741 wait_reading_process_output (sec, nsec, reading ? -1 : 1, do_display,
5742 Qnil, NULL, 0);
5743
5744 return detect_input_pending () ? Qnil : Qt;
5745 }
5746
5747
5748 DEFUN ("redisplay", Fredisplay, Sredisplay, 0, 1, 0,
5749 doc: /* Perform redisplay.
5750 Optional arg FORCE, if non-nil, prevents redisplay from being
5751 preempted by arriving input, even if `redisplay-dont-pause' is nil.
5752 If `redisplay-dont-pause' is non-nil (the default), redisplay is never
5753 preempted by arriving input, so FORCE does nothing.
5754
5755 Return t if redisplay was performed, nil if redisplay was preempted
5756 immediately by pending input. */)
5757 (Lisp_Object force)
5758 {
5759 ptrdiff_t count;
5760
5761 swallow_events (1);
5762 if ((detect_input_pending_run_timers (1)
5763 && NILP (force) && !redisplay_dont_pause)
5764 || !NILP (Vexecuting_kbd_macro))
5765 return Qnil;
5766
5767 count = SPECPDL_INDEX ();
5768 if (!NILP (force) && !redisplay_dont_pause)
5769 specbind (Qredisplay_dont_pause, Qt);
5770 redisplay_preserve_echo_area (2);
5771 unbind_to (count, Qnil);
5772 return Qt;
5773 }
5774
5775
5776 \f
5777 /***********************************************************************
5778 Other Lisp Functions
5779 ***********************************************************************/
5780
5781 /* A vector of size >= 2 * NFRAMES + 3 * NBUFFERS + 1, containing the
5782 session's frames, frame names, buffers, buffer-read-only flags, and
5783 buffer-modified-flags. */
5784
5785 static Lisp_Object frame_and_buffer_state;
5786
5787
5788 DEFUN ("frame-or-buffer-changed-p", Fframe_or_buffer_changed_p,
5789 Sframe_or_buffer_changed_p, 0, 1, 0,
5790 doc: /* Return non-nil if the frame and buffer state appears to have changed.
5791 VARIABLE is a variable name whose value is either nil or a state vector
5792 that will be updated to contain all frames and buffers,
5793 aside from buffers whose names start with space,
5794 along with the buffers' read-only and modified flags. This allows a fast
5795 check to see whether buffer menus might need to be recomputed.
5796 If this function returns non-nil, it updates the internal vector to reflect
5797 the current state.
5798
5799 If VARIABLE is nil, an internal variable is used. Users should not
5800 pass nil for VARIABLE. */)
5801 (Lisp_Object variable)
5802 {
5803 Lisp_Object state, tail, frame, buf;
5804 ptrdiff_t n, idx;
5805
5806 if (! NILP (variable))
5807 {
5808 CHECK_SYMBOL (variable);
5809 state = Fsymbol_value (variable);
5810 if (! VECTORP (state))
5811 goto changed;
5812 }
5813 else
5814 state = frame_and_buffer_state;
5815
5816 idx = 0;
5817 FOR_EACH_FRAME (tail, frame)
5818 {
5819 if (idx == ASIZE (state))
5820 goto changed;
5821 if (!EQ (AREF (state, idx++), frame))
5822 goto changed;
5823 if (idx == ASIZE (state))
5824 goto changed;
5825 if (!EQ (AREF (state, idx++), XFRAME (frame)->name))
5826 goto changed;
5827 }
5828 /* Check that the buffer info matches. */
5829 FOR_EACH_LIVE_BUFFER (tail, buf)
5830 {
5831 /* Ignore buffers that aren't included in buffer lists. */
5832 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5833 continue;
5834 if (idx == ASIZE (state))
5835 goto changed;
5836 if (!EQ (AREF (state, idx++), buf))
5837 goto changed;
5838 if (idx == ASIZE (state))
5839 goto changed;
5840 if (!EQ (AREF (state, idx++), BVAR (XBUFFER (buf), read_only)))
5841 goto changed;
5842 if (idx == ASIZE (state))
5843 goto changed;
5844 if (!EQ (AREF (state, idx++), Fbuffer_modified_p (buf)))
5845 goto changed;
5846 }
5847 if (idx == ASIZE (state))
5848 goto changed;
5849 /* Detect deletion of a buffer at the end of the list. */
5850 if (EQ (AREF (state, idx), Qlambda))
5851 return Qnil;
5852
5853 /* Come here if we decide the data has changed. */
5854 changed:
5855 /* Count the size we will need.
5856 Start with 1 so there is room for at least one lambda at the end. */
5857 n = 1;
5858 FOR_EACH_FRAME (tail, frame)
5859 n += 2;
5860 FOR_EACH_LIVE_BUFFER (tail, buf)
5861 n += 3;
5862 /* Reallocate the vector if data has grown to need it,
5863 or if it has shrunk a lot. */
5864 if (! VECTORP (state)
5865 || n > ASIZE (state)
5866 || n + 20 < ASIZE (state) / 2)
5867 /* Add 20 extra so we grow it less often. */
5868 {
5869 state = Fmake_vector (make_number (n + 20), Qlambda);
5870 if (! NILP (variable))
5871 Fset (variable, state);
5872 else
5873 frame_and_buffer_state = state;
5874 }
5875
5876 /* Record the new data in the (possibly reallocated) vector. */
5877 idx = 0;
5878 FOR_EACH_FRAME (tail, frame)
5879 {
5880 ASET (state, idx, frame);
5881 idx++;
5882 ASET (state, idx, XFRAME (frame)->name);
5883 idx++;
5884 }
5885 FOR_EACH_LIVE_BUFFER (tail, buf)
5886 {
5887 /* Ignore buffers that aren't included in buffer lists. */
5888 if (SREF (BVAR (XBUFFER (buf), name), 0) == ' ')
5889 continue;
5890 ASET (state, idx, buf);
5891 idx++;
5892 ASET (state, idx, BVAR (XBUFFER (buf), read_only));
5893 idx++;
5894 ASET (state, idx, Fbuffer_modified_p (buf));
5895 idx++;
5896 }
5897 /* Fill up the vector with lambdas (always at least one). */
5898 ASET (state, idx, Qlambda);
5899 idx++;
5900 while (idx < ASIZE (state))
5901 {
5902 ASET (state, idx, Qlambda);
5903 idx++;
5904 }
5905 /* Make sure we didn't overflow the vector. */
5906 eassert (idx <= ASIZE (state));
5907 return Qt;
5908 }
5909
5910
5911 \f
5912 /***********************************************************************
5913 Initialization
5914 ***********************************************************************/
5915
5916 /* Initialization done when Emacs fork is started, before doing stty.
5917 Determine terminal type and set terminal_driver. Then invoke its
5918 decoding routine to set up variables in the terminal package. */
5919
5920 void
5921 init_display (void)
5922 {
5923 char *terminal_type;
5924
5925 /* Construct the space glyph. */
5926 space_glyph.type = CHAR_GLYPH;
5927 SET_CHAR_GLYPH (space_glyph, ' ', DEFAULT_FACE_ID, 0);
5928 space_glyph.charpos = -1;
5929
5930 inverse_video = 0;
5931 cursor_in_echo_area = 0;
5932
5933 /* Now is the time to initialize this; it's used by init_sys_modes
5934 during startup. */
5935 Vinitial_window_system = Qnil;
5936
5937 /* SIGWINCH needs to be handled no matter what display we start
5938 with. Otherwise newly opened tty frames will not resize
5939 automatically. */
5940 #ifdef SIGWINCH
5941 #ifndef CANNOT_DUMP
5942 if (initialized)
5943 #endif /* CANNOT_DUMP */
5944 {
5945 struct sigaction action;
5946 emacs_sigaction_init (&action, deliver_window_change_signal);
5947 sigaction (SIGWINCH, &action, 0);
5948 }
5949 #endif /* SIGWINCH */
5950
5951 /* If running as a daemon, no need to initialize any frames/terminal. */
5952 if (IS_DAEMON)
5953 return;
5954
5955 /* If the user wants to use a window system, we shouldn't bother
5956 initializing the terminal. This is especially important when the
5957 terminal is so dumb that emacs gives up before and doesn't bother
5958 using the window system.
5959
5960 If the DISPLAY environment variable is set and nonempty,
5961 try to use X, and die with an error message if that doesn't work. */
5962
5963 #ifdef HAVE_X_WINDOWS
5964 if (! inhibit_window_system && ! display_arg)
5965 {
5966 char *display;
5967 display = getenv ("DISPLAY");
5968 display_arg = (display != 0 && *display != 0);
5969
5970 if (display_arg && !x_display_ok (display))
5971 {
5972 fprintf (stderr, "Display %s unavailable, simulating -nw\n",
5973 display);
5974 inhibit_window_system = 1;
5975 }
5976 }
5977
5978 if (!inhibit_window_system && display_arg)
5979 {
5980 Vinitial_window_system = Qx;
5981 #ifdef HAVE_X11
5982 Vwindow_system_version = make_number (11);
5983 #endif
5984 #ifdef USE_NCURSES
5985 /* In some versions of ncurses,
5986 tputs crashes if we have not called tgetent.
5987 So call tgetent. */
5988 { char b[2044]; tgetent (b, "xterm");}
5989 #endif
5990 return;
5991 }
5992 #endif /* HAVE_X_WINDOWS */
5993
5994 #ifdef HAVE_NTGUI
5995 if (!inhibit_window_system)
5996 {
5997 Vinitial_window_system = Qw32;
5998 Vwindow_system_version = make_number (1);
5999 return;
6000 }
6001 #endif /* HAVE_NTGUI */
6002
6003 #ifdef HAVE_NS
6004 if (!inhibit_window_system
6005 #ifndef CANNOT_DUMP
6006 && initialized
6007 #endif
6008 )
6009 {
6010 Vinitial_window_system = Qns;
6011 Vwindow_system_version = make_number (10);
6012 return;
6013 }
6014 #endif
6015
6016 /* If no window system has been specified, try to use the terminal. */
6017 if (! isatty (0))
6018 fatal ("standard input is not a tty");
6019
6020 #ifdef WINDOWSNT
6021 terminal_type = "w32console";
6022 #else
6023 terminal_type = getenv ("TERM");
6024 #endif
6025 if (!terminal_type)
6026 {
6027 #ifdef HAVE_WINDOW_SYSTEM
6028 if (! inhibit_window_system)
6029 fprintf (stderr, "Please set the environment variable DISPLAY or TERM (see `tset').\n");
6030 else
6031 #endif /* HAVE_WINDOW_SYSTEM */
6032 fprintf (stderr, "Please set the environment variable TERM; see `tset'.\n");
6033 exit (1);
6034 }
6035
6036 {
6037 struct terminal *t;
6038 struct frame *f = XFRAME (selected_frame);
6039
6040 init_foreground_group ();
6041
6042 /* Open a display on the controlling tty. */
6043 t = init_tty (0, terminal_type, 1); /* Errors are fatal. */
6044
6045 /* Convert the initial frame to use the new display. */
6046 if (f->output_method != output_initial)
6047 emacs_abort ();
6048 f->output_method = t->type;
6049 f->terminal = t;
6050
6051 t->reference_count++;
6052 #ifdef MSDOS
6053 f->output_data.tty->display_info = &the_only_display_info;
6054 #else
6055 if (f->output_method == output_termcap)
6056 create_tty_output (f);
6057 #endif
6058 t->display_info.tty->top_frame = selected_frame;
6059 change_frame_size (XFRAME (selected_frame),
6060 FrameRows (t->display_info.tty),
6061 FrameCols (t->display_info.tty), 0, 0, 1);
6062
6063 /* Delete the initial terminal. */
6064 if (--initial_terminal->reference_count == 0
6065 && initial_terminal->delete_terminal_hook)
6066 (*initial_terminal->delete_terminal_hook) (initial_terminal);
6067
6068 /* Update frame parameters to reflect the new type. */
6069 Fmodify_frame_parameters
6070 (selected_frame, list1 (Fcons (Qtty_type,
6071 Ftty_type (selected_frame))));
6072 if (t->display_info.tty->name)
6073 Fmodify_frame_parameters
6074 (selected_frame,
6075 list1 (Fcons (Qtty, build_string (t->display_info.tty->name))));
6076 else
6077 Fmodify_frame_parameters (selected_frame, list1 (Fcons (Qtty, Qnil)));
6078 }
6079
6080 {
6081 struct frame *sf = SELECTED_FRAME ();
6082 int width = FRAME_TOTAL_COLS (sf);
6083 int height = FRAME_LINES (sf);
6084
6085 /* If these sizes are so big they cause overflow, just ignore the
6086 change. It's not clear what better we could do. The rest of
6087 the code assumes that (width + 2) * height * sizeof (struct glyph)
6088 does not overflow and does not exceed PTRDIFF_MAX or SIZE_MAX. */
6089 if (INT_ADD_RANGE_OVERFLOW (width, 2, INT_MIN, INT_MAX)
6090 || INT_MULTIPLY_RANGE_OVERFLOW (width + 2, height, INT_MIN, INT_MAX)
6091 || (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph)
6092 < (width + 2) * height))
6093 fatal ("screen size %dx%d too big", width, height);
6094 }
6095
6096 calculate_costs (XFRAME (selected_frame));
6097
6098 /* Set up faces of the initial terminal frame of a dumped Emacs. */
6099 if (initialized
6100 && !noninteractive
6101 && NILP (Vinitial_window_system))
6102 {
6103 /* For the initial frame, we don't have any way of knowing what
6104 are the foreground and background colors of the terminal. */
6105 struct frame *sf = SELECTED_FRAME ();
6106
6107 FRAME_FOREGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_FG_COLOR;
6108 FRAME_BACKGROUND_PIXEL (sf) = FACE_TTY_DEFAULT_BG_COLOR;
6109 call0 (intern ("tty-set-up-initial-frame-faces"));
6110 }
6111 }
6112
6113
6114 \f
6115 /***********************************************************************
6116 Blinking cursor
6117 ***********************************************************************/
6118
6119 DEFUN ("internal-show-cursor", Finternal_show_cursor,
6120 Sinternal_show_cursor, 2, 2, 0,
6121 doc: /* Set the cursor-visibility flag of WINDOW to SHOW.
6122 WINDOW nil means use the selected window. SHOW non-nil means
6123 show a cursor in WINDOW in the next redisplay. SHOW nil means
6124 don't show a cursor. */)
6125 (Lisp_Object window, Lisp_Object show)
6126 {
6127 /* Don't change cursor state while redisplaying. This could confuse
6128 output routines. */
6129 if (!redisplaying_p)
6130 decode_any_window (window)->cursor_off_p = NILP (show);
6131 return Qnil;
6132 }
6133
6134
6135 DEFUN ("internal-show-cursor-p", Finternal_show_cursor_p,
6136 Sinternal_show_cursor_p, 0, 1, 0,
6137 doc: /* Value is non-nil if next redisplay will display a cursor in WINDOW.
6138 WINDOW nil or omitted means report on the selected window. */)
6139 (Lisp_Object window)
6140 {
6141 return decode_any_window (window)->cursor_off_p ? Qnil : Qt;
6142 }
6143 \f
6144 /***********************************************************************
6145 Initialization
6146 ***********************************************************************/
6147
6148 void
6149 syms_of_display (void)
6150 {
6151 defsubr (&Sredraw_frame);
6152 defsubr (&Sredraw_display);
6153 defsubr (&Sframe_or_buffer_changed_p);
6154 defsubr (&Sopen_termscript);
6155 defsubr (&Sding);
6156 defsubr (&Sredisplay);
6157 defsubr (&Ssleep_for);
6158 defsubr (&Ssend_string_to_terminal);
6159 defsubr (&Sinternal_show_cursor);
6160 defsubr (&Sinternal_show_cursor_p);
6161
6162 #ifdef GLYPH_DEBUG
6163 defsubr (&Sdump_redisplay_history);
6164 #endif
6165
6166 frame_and_buffer_state = Fmake_vector (make_number (20), Qlambda);
6167 staticpro (&frame_and_buffer_state);
6168
6169 DEFSYM (Qdisplay_table, "display-table");
6170 DEFSYM (Qredisplay_dont_pause, "redisplay-dont-pause");
6171
6172 DEFVAR_INT ("baud-rate", baud_rate,
6173 doc: /* The output baud rate of the terminal.
6174 On most systems, changing this value will affect the amount of padding
6175 and the other strategic decisions made during redisplay. */);
6176
6177 DEFVAR_BOOL ("inverse-video", inverse_video,
6178 doc: /* Non-nil means invert the entire frame display.
6179 This means everything is in inverse video which otherwise would not be. */);
6180
6181 DEFVAR_BOOL ("visible-bell", visible_bell,
6182 doc: /* Non-nil means try to flash the frame to represent a bell.
6183
6184 See also `ring-bell-function'. */);
6185
6186 DEFVAR_BOOL ("no-redraw-on-reenter", no_redraw_on_reenter,
6187 doc: /* Non-nil means no need to redraw entire frame after suspending.
6188 A non-nil value is useful if the terminal can automatically preserve
6189 Emacs's frame display when you reenter Emacs.
6190 It is up to you to set this variable if your terminal can do that. */);
6191
6192 DEFVAR_LISP ("initial-window-system", Vinitial_window_system,
6193 doc: /* Name of the window system that Emacs uses for the first frame.
6194 The value is a symbol:
6195 nil for a termcap frame (a character-only terminal),
6196 'x' for an Emacs frame that is really an X window,
6197 'w32' for an Emacs frame that is a window on MS-Windows display,
6198 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6199 'pc' for a direct-write MS-DOS frame.
6200
6201 Use of this variable as a boolean is deprecated. Instead,
6202 use `display-graphic-p' or any of the other `display-*-p'
6203 predicates which report frame's specific UI-related capabilities. */);
6204
6205 DEFVAR_KBOARD ("window-system", Vwindow_system,
6206 doc: /* Name of window system through which the selected frame is displayed.
6207 The value is a symbol:
6208 nil for a termcap frame (a character-only terminal),
6209 'x' for an Emacs frame that is really an X window,
6210 'w32' for an Emacs frame that is a window on MS-Windows display,
6211 'ns' for an Emacs frame on a GNUstep or Macintosh Cocoa display,
6212 'pc' for a direct-write MS-DOS frame.
6213
6214 Use of this variable as a boolean is deprecated. Instead,
6215 use `display-graphic-p' or any of the other `display-*-p'
6216 predicates which report frame's specific UI-related capabilities. */);
6217
6218 DEFVAR_LISP ("window-system-version", Vwindow_system_version,
6219 doc: /* The version number of the window system in use.
6220 For X windows, this is 11. */);
6221
6222 DEFVAR_BOOL ("cursor-in-echo-area", cursor_in_echo_area,
6223 doc: /* Non-nil means put cursor in minibuffer, at end of any message there. */);
6224
6225 DEFVAR_LISP ("glyph-table", Vglyph_table,
6226 doc: /* Table defining how to output a glyph code to the frame.
6227 If not nil, this is a vector indexed by glyph code to define the glyph.
6228 Each element can be:
6229 integer: a glyph code which this glyph is an alias for.
6230 string: output this glyph using that string (not impl. in X windows).
6231 nil: this glyph mod 524288 is the code of a character to output,
6232 and this glyph / 524288 is the face number (see `face-id') to use
6233 while outputting it. */);
6234 Vglyph_table = Qnil;
6235
6236 DEFVAR_LISP ("standard-display-table", Vstandard_display_table,
6237 doc: /* Display table to use for buffers that specify none.
6238 See `buffer-display-table' for more information. */);
6239 Vstandard_display_table = Qnil;
6240
6241 DEFVAR_BOOL ("redisplay-dont-pause", redisplay_dont_pause,
6242 doc: /* Non-nil means display update isn't paused when input is detected. */);
6243 redisplay_dont_pause = 1;
6244
6245 #ifdef CANNOT_DUMP
6246 if (noninteractive)
6247 #endif
6248 {
6249 Vinitial_window_system = Qnil;
6250 Vwindow_system_version = Qnil;
6251 }
6252 }