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