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