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