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