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