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