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