mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
dispnew.c
6328 lines (5257 loc) · 190 KB
/
dispnew.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Updating of data structures for redisplay.
Copyright (C) 1985-1988, 1993-1995, 1997-2019 Free Software Foundation,
Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "sysstdio.h"
#include <stdlib.h>
#include <unistd.h>
#include "lisp.h"
#include "termchar.h"
/* cm.h must come after dispextern.h on Windows. */
#include "dispextern.h"
#include "cm.h"
#include "buffer.h"
#include "keyboard.h"
#include "frame.h"
#include "termhooks.h"
#include "window.h"
#include "commands.h"
#include "disptab.h"
#include "blockinput.h"
#include "syssignal.h"
#include "systime.h"
#include "tparam.h"
#include "xwidget.h"
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
#include <errno.h>
#include <fpending.h>
#ifdef WINDOWSNT
#include "w32.h"
#endif
/* Structure to pass dimensions around. Used for character bounding
boxes, glyph matrix dimensions and alike. */
struct dim
{
int width;
int height;
};
/* Function prototypes. */
static void update_frame_line (struct frame *, int, bool);
static int required_matrix_height (struct window *);
static int required_matrix_width (struct window *);
static void increment_row_positions (struct glyph_row *, ptrdiff_t, ptrdiff_t);
static void build_frame_matrix_from_window_tree (struct glyph_matrix *,
struct window *);
static void build_frame_matrix_from_leaf_window (struct glyph_matrix *,
struct window *);
static void adjust_decode_mode_spec_buffer (struct frame *);
static void fill_up_glyph_row_with_spaces (struct glyph_row *);
static void clear_window_matrices (struct window *, bool);
static void fill_up_glyph_row_area_with_spaces (struct glyph_row *, int);
static int scrolling_window (struct window *, bool);
static bool update_window_line (struct window *, int, bool *);
static void mirror_make_current (struct window *, int);
#ifdef GLYPH_DEBUG
static void check_matrix_pointers (struct glyph_matrix *,
struct glyph_matrix *);
#endif
static void mirror_line_dance (struct window *, int, int, int *, char *);
static bool update_window_tree (struct window *, bool);
static bool update_window (struct window *, bool);
static bool update_frame_1 (struct frame *, bool, bool, bool, bool);
static bool scrolling (struct frame *);
static void set_window_cursor_after_update (struct window *);
static void adjust_frame_glyphs_for_window_redisplay (struct frame *);
static void adjust_frame_glyphs_for_frame_redisplay (struct frame *);
static void set_window_update_flags (struct window *w, bool on_p);
/* True means last display completed. False means it was preempted. */
bool display_completed;
/* True means SIGWINCH happened when not safe. */
static bool delayed_size_change;
/* A glyph for a space. */
struct glyph space_glyph;
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* Counts of allocated structures. These counts serve to diagnose
memory leaks and double frees. */
static int glyph_matrix_count;
static int glyph_pool_count;
#endif /* GLYPH_DEBUG and ENABLE_CHECKING */
/* If non-null, the frame whose frame matrices are manipulated. If
null, window matrices are worked on. */
static struct frame *frame_matrix_frame;
/* Convert vpos and hpos from frame to window and vice versa.
This may only be used for terminal frames. */
#ifdef GLYPH_DEBUG
static int window_to_frame_vpos (struct window *, int);
static int window_to_frame_hpos (struct window *, int);
#define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
#define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
/* One element of the ring buffer containing redisplay history
information. */
struct redisplay_history
{
char trace[512 + 100];
};
/* The size of the history buffer. */
#define REDISPLAY_HISTORY_SIZE 30
/* The redisplay history buffer. */
static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
/* Next free entry in redisplay_history. */
static int history_idx;
/* A tick that's incremented each time something is added to the
history. */
static uprintmax_t history_tick;
/* Add to the redisplay history how window W has been displayed.
MSG is a trace containing the information how W's glyph matrix
has been constructed. PAUSED_P means that the update
has been interrupted for pending input. */
static void
add_window_display_history (struct window *w, const char *msg, bool paused_p)
{
char *buf;
void *ptr = w;
if (history_idx >= REDISPLAY_HISTORY_SIZE)
history_idx = 0;
buf = redisplay_history[history_idx].trace;
++history_idx;
snprintf (buf, sizeof redisplay_history[0].trace,
"%"pMu": window %p (%s)%s\n%s",
history_tick++,
ptr,
((BUFFERP (w->contents)
&& STRINGP (BVAR (XBUFFER (w->contents), name)))
? SSDATA (BVAR (XBUFFER (w->contents), name))
: "???"),
paused_p ? " ***paused***" : "",
msg);
}
/* Add to the redisplay history that frame F has been displayed.
PAUSED_P means that the update has been interrupted for
pending input. */
static void
add_frame_display_history (struct frame *f, bool paused_p)
{
char *buf;
void *ptr = f;
if (history_idx >= REDISPLAY_HISTORY_SIZE)
history_idx = 0;
buf = redisplay_history[history_idx].trace;
++history_idx;
sprintf (buf, "%"pMu": update frame %p%s",
history_tick++,
ptr, paused_p ? " ***paused***" : "");
}
DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
Sdump_redisplay_history, 0, 0, "",
doc: /* Dump redisplay history to stderr. */)
(void)
{
int i;
for (i = history_idx - 1; i != history_idx; --i)
{
if (i < 0)
i = REDISPLAY_HISTORY_SIZE - 1;
fprintf (stderr, "%s\n", redisplay_history[i].trace);
}
return Qnil;
}
#else /* not GLYPH_DEBUG */
#define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
#define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
#endif /* GLYPH_DEBUG */
#if (defined PROFILING \
&& (defined __FreeBSD__ || defined GNU_LINUX || defined __MINGW32__) \
&& !HAVE___EXECUTABLE_START)
/* This function comes first in the Emacs executable and is used only
to estimate the text start for profiling. */
void
__executable_start (void)
{
emacs_abort ();
}
#endif
/***********************************************************************
Glyph Matrices
***********************************************************************/
/* Allocate and return a glyph_matrix structure. POOL is the glyph
pool from which memory for the matrix should be allocated, or null
for window-based redisplay where no glyph pools are used. The
member `pool' of the glyph matrix structure returned is set to
POOL, the structure is otherwise zeroed. */
static struct glyph_matrix *
new_glyph_matrix (struct glyph_pool *pool)
{
struct glyph_matrix *result = xzalloc (sizeof *result);
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* Increment number of allocated matrices. This count is used
to detect memory leaks. */
++glyph_matrix_count;
#endif
/* Set pool and return. */
result->pool = pool;
return result;
}
/* Free glyph matrix MATRIX. Passing in a null MATRIX is allowed.
If GLYPH_DEBUG and ENABLE_CHECKING are in effect, the global counter
glyph_matrix_count is decremented when a matrix is freed. If the count
gets negative, more structures were freed than allocated, i.e. one matrix
was freed more than once or a bogus pointer was passed to this function.
If MATRIX->pool is null, this means that the matrix manages its own
glyph memory---this is done for matrices on X frames. Freeing the
matrix also frees the glyph memory in this case. */
static void
free_glyph_matrix (struct glyph_matrix *matrix)
{
if (matrix)
{
int i;
#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
/* Detect the case that more matrices are freed than were
allocated. */
--glyph_matrix_count;
eassert (glyph_matrix_count >= 0);
#endif
/* Free glyph memory if MATRIX owns it. */
if (matrix->pool == NULL)
for (i = 0; i < matrix->rows_allocated; ++i)
xfree (matrix->rows[i].glyphs[LEFT_MARGIN_AREA]);
/* Free row structures and the matrix itself. */
xfree (matrix->rows);
xfree (matrix);
}
}
/* Return the number of glyphs to reserve for a marginal area of
window W. TOTAL_GLYPHS is the number of glyphs in a complete
display line of window W. MARGIN gives the width of the marginal
area in canonical character units. */
static int
margin_glyphs_to_reserve (struct window *w, int total_glyphs, int margin)
{
if (margin > 0)
{
int width = w->total_cols;
double d = max (0, margin);
d = min (width / 2 - 1, d);
/* Since MARGIN is positive, we cannot possibly have less than
one glyph for the marginal area. */
return max (1, (int) ((double) total_glyphs / width * d));
}
return 0;
}
/* Return true if ROW's hash value is correct.
Optimized away if ENABLE_CHECKING is not defined. */
static bool
verify_row_hash (struct glyph_row *row)
{
return row->hash == row_hash (row);
}
/* Adjust glyph matrix MATRIX on window W or on a frame to changed
window sizes.
W is null if the function is called for a frame glyph matrix.
Otherwise it is the window MATRIX is a member of. X and Y are the
indices of the first column and row of MATRIX within the frame
matrix, if such a matrix exists. They are zero for purely
window-based redisplay. DIM is the needed size of the matrix.
In window-based redisplay, where no frame matrices exist, glyph
matrices manage their own glyph storage. Otherwise, they allocate
storage from a common frame glyph pool which can be found in
MATRIX->pool.
The reason for this memory management strategy is to avoid complete
frame redraws if possible. When we allocate from a common pool, a
change of the location or size of a sub-matrix within the pool
requires a complete redisplay of the frame because we cannot easily
make sure that the current matrices of all windows still agree with
what is displayed on the screen. While this is usually fast, it
leads to screen flickering. */
static void
adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y, struct dim dim)
{
int i;
int new_rows;
bool marginal_areas_changed_p = 0;
bool header_line_changed_p = 0;
bool header_line_p = 0;
int left = -1, right = -1;
int window_width = -1, window_height = -1;
/* See if W had a header line that has disappeared now, or vice versa.
Get W's size. */
if (w)
{
window_box (w, ANY_AREA, 0, 0, &window_width, &window_height);
header_line_p = window_wants_header_line (w);
header_line_changed_p = header_line_p != matrix->header_line_p;
}
matrix->header_line_p = header_line_p;
/* If POOL is null, MATRIX is a window matrix for window-based redisplay.
Do nothing if MATRIX' size, position, vscroll, and marginal areas
haven't changed. This optimization is important because preserving
the matrix means preventing redisplay. */
eassume (w != NULL || matrix->pool != NULL);
if (matrix->pool == NULL)
{
left = margin_glyphs_to_reserve (w, dim.width, w->left_margin_cols);
right = margin_glyphs_to_reserve (w, dim.width, w->right_margin_cols);
eassert (left >= 0 && right >= 0);
marginal_areas_changed_p = (left != matrix->left_margin_glyphs
|| right != matrix->right_margin_glyphs);
if (!marginal_areas_changed_p
&& !XFRAME (w->frame)->fonts_changed
&& !header_line_changed_p
&& matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
&& matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
&& matrix->window_height == window_height
&& matrix->window_vscroll == w->vscroll
&& matrix->window_width == window_width)
return;
}
/* Enlarge MATRIX->rows if necessary. New rows are cleared. */
if (matrix->rows_allocated < dim.height)
{
int old_alloc = matrix->rows_allocated;
new_rows = dim.height - matrix->rows_allocated;
matrix->rows = xpalloc (matrix->rows, &matrix->rows_allocated,
new_rows, INT_MAX, sizeof *matrix->rows);
memset (matrix->rows + old_alloc, 0,
(matrix->rows_allocated - old_alloc) * sizeof *matrix->rows);
}
else
new_rows = 0;
/* If POOL is not null, MATRIX is a frame matrix or a window matrix
on a frame not using window-based redisplay. Set up pointers for
each row into the glyph pool. */
if (matrix->pool)
{
eassert (matrix->pool->glyphs);
if (w)
{
left = margin_glyphs_to_reserve (w, dim.width,
w->left_margin_cols);
right = margin_glyphs_to_reserve (w, dim.width,
w->right_margin_cols);
}
else
left = right = 0;
for (i = 0; i < dim.height; ++i)
{
struct glyph_row *row = &matrix->rows[i];
row->glyphs[LEFT_MARGIN_AREA]
= (matrix->pool->glyphs
+ (y + i) * matrix->pool->ncolumns
+ x);
if (w == NULL
|| (row == matrix->rows + dim.height - 1
&& window_wants_mode_line (w))
|| (row == matrix->rows && matrix->header_line_p))
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA];
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width;
row->glyphs[LAST_AREA]
= row->glyphs[RIGHT_MARGIN_AREA];
}
else
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + left;
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width - left - right;
row->glyphs[LAST_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + dim.width;
}
}
matrix->left_margin_glyphs = left;
matrix->right_margin_glyphs = right;
}
else
{
/* If MATRIX->pool is null, MATRIX is responsible for managing
its own memory. It is a window matrix for window-based redisplay.
Allocate glyph memory from the heap. */
if (dim.width > matrix->matrix_w
|| new_rows
|| header_line_changed_p
|| marginal_areas_changed_p)
{
struct glyph_row *row = matrix->rows;
struct glyph_row *end = row + matrix->rows_allocated;
while (row < end)
{
row->glyphs[LEFT_MARGIN_AREA]
= xnrealloc (row->glyphs[LEFT_MARGIN_AREA],
dim.width, sizeof (struct glyph));
/* The mode line, if displayed, never has marginal areas. */
if ((row == matrix->rows + dim.height - 1
&& !(w && window_wants_mode_line (w)))
|| (row == matrix->rows && matrix->header_line_p))
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA];
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width;
row->glyphs[LAST_AREA]
= row->glyphs[RIGHT_MARGIN_AREA];
}
else
{
row->glyphs[TEXT_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + left;
row->glyphs[RIGHT_MARGIN_AREA]
= row->glyphs[TEXT_AREA] + dim.width - left - right;
row->glyphs[LAST_AREA]
= row->glyphs[LEFT_MARGIN_AREA] + dim.width;
}
++row;
}
}
eassert (left >= 0 && right >= 0);
matrix->left_margin_glyphs = left;
matrix->right_margin_glyphs = right;
}
/* Number of rows to be used by MATRIX. */
matrix->nrows = dim.height;
eassert (matrix->nrows >= 0);
if (w)
{
if (matrix == w->current_matrix)
{
/* Mark rows in a current matrix of a window as not having
valid contents. It's important to not do this for
desired matrices. When Emacs starts, it may already be
building desired matrices when this function runs. */
if (window_width < 0)
window_width = window_box_width (w, -1);
/* Optimize the case that only the height has changed (C-x 2,
upper window). Invalidate all rows that are no longer part
of the window. */
if (!marginal_areas_changed_p
&& !header_line_changed_p
&& new_rows == 0
&& dim.width == matrix->matrix_w
&& matrix->window_pixel_left == WINDOW_LEFT_PIXEL_EDGE (w)
&& matrix->window_pixel_top == WINDOW_TOP_PIXEL_EDGE (w)
&& matrix->window_width == window_width)
{
/* Find the last row in the window. */
for (i = 0; i < matrix->nrows && matrix->rows[i].enabled_p; ++i)
if (MATRIX_ROW_BOTTOM_Y (matrix->rows + i) >= window_height)
{
++i;
break;
}
/* Window end is invalid, if inside of the rows that
are invalidated below. */
if (w->window_end_vpos >= i)
w->window_end_valid = 0;
while (i < matrix->nrows)
matrix->rows[i++].enabled_p = false;
}
else
{
for (i = 0; i < matrix->nrows; ++i)
matrix->rows[i].enabled_p = false;
}
/* We've disabled the mode-line row, so force redrawing of
the mode line, if any, since otherwise it will remain
disabled in the current matrix, and expose events won't
redraw it. */
if (window_wants_mode_line (w))
w->update_mode_line = 1;
}
else if (matrix == w->desired_matrix)
{
/* Rows in desired matrices always have to be cleared;
redisplay expects this is the case when it runs, so it
had better be the case when we adjust matrices between
redisplays. */
for (i = 0; i < matrix->nrows; ++i)
matrix->rows[i].enabled_p = false;
}
}
/* Remember last values to be able to optimize frame redraws. */
matrix->matrix_x = x;
matrix->matrix_y = y;
matrix->matrix_w = dim.width;
matrix->matrix_h = dim.height;
/* Record the top y location and height of W at the time the matrix
was last adjusted. This is used to optimize redisplay above. */
if (w)
{
matrix->window_pixel_left = WINDOW_LEFT_PIXEL_EDGE (w);
matrix->window_pixel_top = WINDOW_TOP_PIXEL_EDGE (w);
matrix->window_height = window_height;
matrix->window_width = window_width;
matrix->window_vscroll = w->vscroll;
}
}
/* Reverse the contents of rows in MATRIX between START and END. The
contents of the row at END - 1 end up at START, END - 2 at START +
1 etc. This is part of the implementation of rotate_matrix (see
below). */
static void
reverse_rows (struct glyph_matrix *matrix, int start, int end)
{
int i, j;
for (i = start, j = end - 1; i < j; ++i, --j)
{
/* Non-ISO HP/UX compiler doesn't like auto struct
initialization. */
struct glyph_row temp;
temp = matrix->rows[i];
matrix->rows[i] = matrix->rows[j];
matrix->rows[j] = temp;
}
}
/* Rotate the contents of rows in MATRIX in the range FIRST .. LAST -
1 by BY positions. BY < 0 means rotate left, i.e. towards lower
indices. (Note: this does not copy glyphs, only glyph pointers in
row structures are moved around).
The algorithm used for rotating the vector was, I believe, first
described by Kernighan. See the vector R as consisting of two
sub-vectors AB, where A has length BY for BY >= 0. The result
after rotating is then BA. Reverse both sub-vectors to get ArBr
and reverse the result to get (ArBr)r which is BA. Similar for
rotating right. */
void
rotate_matrix (struct glyph_matrix *matrix, int first, int last, int by)
{
if (by < 0)
{
/* Up (rotate left, i.e. towards lower indices). */
by = -by;
reverse_rows (matrix, first, first + by);
reverse_rows (matrix, first + by, last);
reverse_rows (matrix, first, last);
}
else if (by > 0)
{
/* Down (rotate right, i.e. towards higher indices). */
reverse_rows (matrix, last - by, last);
reverse_rows (matrix, first, last - by);
reverse_rows (matrix, first, last);
}
}
/* Increment buffer positions in glyph rows of MATRIX. Do it for rows
with indices START <= index < END. Increment positions by DELTA/
DELTA_BYTES. */
void
increment_matrix_positions (struct glyph_matrix *matrix, int start, int end,
ptrdiff_t delta, ptrdiff_t delta_bytes)
{
/* Check that START and END are reasonable values. */
eassert (start >= 0 && start <= matrix->nrows);
eassert (end >= 0 && end <= matrix->nrows);
eassert (start <= end);
for (; start < end; ++start)
increment_row_positions (matrix->rows + start, delta, delta_bytes);
}
/* Clear the enable_p flags in a range of rows in glyph matrix MATRIX.
START and END are the row indices of the first and last + 1 row to clear. */
void
clear_glyph_matrix_rows (struct glyph_matrix *matrix, int start, int end)
{
eassert (start <= end);
eassert (start >= 0 && (start < matrix->nrows
/* matrix->nrows can be 0 for the initial frame. */
|| (matrix->nrows == 0)));
eassert (end >= 0 && end <= matrix->nrows);
for (; start < end; ++start)
matrix->rows[start].enabled_p = false;
}
/* Clear MATRIX.
Empty all rows in MATRIX by clearing their enabled_p flags.
The function prepare_desired_row will eventually really clear a row
when it sees one with a false enabled_p flag.
Reset update hints to default values. The only update hint
currently present is the flag MATRIX->no_scrolling_p. */
void
clear_glyph_matrix (struct glyph_matrix *matrix)
{
if (matrix)
{
clear_glyph_matrix_rows (matrix, 0, matrix->nrows);
matrix->no_scrolling_p = 0;
}
}
/* Shift part of the glyph matrix MATRIX of window W up or down.
Increment y-positions in glyph rows between START and END by DY,
and recompute their visible height. */
void
shift_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int start, int end, int dy)
{
int min_y, max_y;
eassert (start <= end);
eassert (start >= 0 && start < matrix->nrows);
eassert (end >= 0 && end <= matrix->nrows);
min_y = WINDOW_HEADER_LINE_HEIGHT (w);
max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
for (; start < end; ++start)
{
struct glyph_row *row = &matrix->rows[start];
row->y += dy;
row->visible_height = row->height;
if (row->y < min_y)
row->visible_height -= min_y - row->y;
if (row->y + row->height > max_y)
row->visible_height -= row->y + row->height - max_y;
if (row->fringe_bitmap_periodic_p)
row->redraw_fringe_bitmaps_p = 1;
}
}
/* Mark all rows in current matrices of frame F as invalid. Marking
invalid is done by setting enabled_p to zero for all rows in a
current matrix. */
void
clear_current_matrices (register struct frame *f)
{
/* Clear frame current matrix, if we have one. */
if (f->current_matrix)
clear_glyph_matrix (f->current_matrix);
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
/* Clear the matrix of the menu bar window, if such a window exists.
The menu bar window is currently used to display menus on X when
no toolkit support is compiled in. */
if (WINDOWP (f->menu_bar_window))
clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
#endif
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
/* Clear the matrix of the tool-bar window, if any. */
if (WINDOWP (f->tool_bar_window))
clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
#endif
/* Clear current window matrices. */
eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 0);
}
/* Clear out all display lines of F for a coming redisplay. */
void
clear_desired_matrices (register struct frame *f)
{
if (f->desired_matrix)
clear_glyph_matrix (f->desired_matrix);
#if defined (HAVE_X_WINDOWS) && ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
if (WINDOWP (f->menu_bar_window))
clear_glyph_matrix (XWINDOW (f->menu_bar_window)->desired_matrix);
#endif
#if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
if (WINDOWP (f->tool_bar_window))
clear_glyph_matrix (XWINDOW (f->tool_bar_window)->desired_matrix);
#endif
/* Do it for window matrices. */
eassert (WINDOWP (FRAME_ROOT_WINDOW (f)));
clear_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)), 1);
}
/* Clear matrices in window tree rooted in W. If DESIRED_P,
clear desired matrices, otherwise clear current matrices. */
static void
clear_window_matrices (struct window *w, bool desired_p)
{
while (w)
{
if (WINDOWP (w->contents))
clear_window_matrices (XWINDOW (w->contents), desired_p);
else
{
if (desired_p)
clear_glyph_matrix (w->desired_matrix);
else
{
clear_glyph_matrix (w->current_matrix);
w->window_end_valid = 0;
}
}
w = NILP (w->next) ? 0 : XWINDOW (w->next);
}
}
/***********************************************************************
Glyph Rows
See dispextern.h for an overall explanation of glyph rows.
***********************************************************************/
/* Clear glyph row ROW. NOTE: this code relies on the current
layout of `glyphs' and `used' fields of `struct glyph_row'. */
void
clear_glyph_row (struct glyph_row *row)
{
enum { off = offsetof (struct glyph_row, used) };
/* Zero everything except pointers in `glyphs'. */
memset (row->used, 0, sizeof *row - off);
}
/* Make ROW an empty, enabled row of canonical character height,
in window W starting at y-position Y. */
void
blank_row (struct window *w, struct glyph_row *row, int y)
{
int min_y, max_y;
min_y = WINDOW_HEADER_LINE_HEIGHT (w);
max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (w);
clear_glyph_row (row);
row->y = y;
row->ascent = row->phys_ascent = 0;
row->height = row->phys_height = FRAME_LINE_HEIGHT (XFRAME (w->frame));
row->visible_height = row->height;
if (row->y < min_y)
row->visible_height -= min_y - row->y;
if (row->y + row->height > max_y)
row->visible_height -= row->y + row->height - max_y;
row->enabled_p = true;
}
/* Increment buffer positions in glyph row ROW. DELTA and DELTA_BYTES
are the amounts by which to change positions. Note that the first
glyph of the text area of a row can have a buffer position even if
the used count of the text area is zero. Such rows display line
ends. */
static void
increment_row_positions (struct glyph_row *row,
ptrdiff_t delta, ptrdiff_t delta_bytes)
{
int area, i;
/* Increment start and end positions. */
MATRIX_ROW_START_CHARPOS (row) += delta;
MATRIX_ROW_START_BYTEPOS (row) += delta_bytes;
MATRIX_ROW_END_CHARPOS (row) += delta;
MATRIX_ROW_END_BYTEPOS (row) += delta_bytes;
CHARPOS (row->start.pos) += delta;
BYTEPOS (row->start.pos) += delta_bytes;
CHARPOS (row->end.pos) += delta;
BYTEPOS (row->end.pos) += delta_bytes;
if (!row->enabled_p)
return;
/* Increment positions in glyphs. */
for (area = 0; area < LAST_AREA; ++area)
for (i = 0; i < row->used[area]; ++i)
if (BUFFERP (row->glyphs[area][i].object)
&& row->glyphs[area][i].charpos > 0)
row->glyphs[area][i].charpos += delta;
/* Capture the case of rows displaying a line end. */
if (row->used[TEXT_AREA] == 0
&& MATRIX_ROW_DISPLAYS_TEXT_P (row))
row->glyphs[TEXT_AREA]->charpos += delta;
}
#if 0
/* Swap glyphs between two glyph rows A and B. This exchanges glyph
contents, i.e. glyph structure contents are exchanged between A and
B without changing glyph pointers in A and B. */
static void
swap_glyphs_in_rows (struct glyph_row *a, struct glyph_row *b)
{
int area;
for (area = 0; area < LAST_AREA; ++area)
{
/* Number of glyphs to swap. */
int max_used = max (a->used[area], b->used[area]);
/* Start of glyphs in area of row A. */
struct glyph *glyph_a = a->glyphs[area];
/* End + 1 of glyphs in area of row A. */
struct glyph *glyph_a_end = a->glyphs[max_used];
/* Start of glyphs in area of row B. */
struct glyph *glyph_b = b->glyphs[area];
while (glyph_a < glyph_a_end)
{
/* Non-ISO HP/UX compiler doesn't like auto struct
initialization. */
struct glyph temp;
temp = *glyph_a;
*glyph_a = *glyph_b;
*glyph_b = temp;
++glyph_a;
++glyph_b;
}
}
}
#endif /* 0 */
/* Exchange pointers to glyph memory between glyph rows A and B. Also
exchange the used[] array and the hash values of the rows, because
these should all go together for the row's hash value to be
correct. */
static void
swap_glyph_pointers (struct glyph_row *a, struct glyph_row *b)
{
int i;
unsigned hash_tem = a->hash;
for (i = 0; i < LAST_AREA + 1; ++i)
{
struct glyph *temp = a->glyphs[i];
a->glyphs[i] = b->glyphs[i];
b->glyphs[i] = temp;
if (i < LAST_AREA)
{
short used_tem = a->used[i];
a->used[i] = b->used[i];
b->used[i] = used_tem;
}
}
a->hash = b->hash;
b->hash = hash_tem;
}
/* Copy glyph row structure FROM to glyph row structure TO, except that
glyph pointers, the `used' counts, and the hash values in the structures
are left unchanged. NOTE: this code relies on the current layout of
`glyphs', `used', `hash' and `x' fields of `struct glyph_row'. */
static void
copy_row_except_pointers (struct glyph_row *to, struct glyph_row *from)
{
enum { off = offsetof (struct glyph_row, x) };
memcpy (&to->x, &from->x, sizeof *to - off);
}