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