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