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