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