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