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