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