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