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