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