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