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