forked from mbert/elvis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
2700 lines (2456 loc) · 69.6 KB
/
draw.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
/* draw.c */
/* Copyright 1995 by Steve Kirkendall */
#include "elvis.h"
#ifdef FEATURE_RCSID
char id_draw[] = "$Id: draw.c,v 2.133 2004/03/07 21:37:49 steve Exp $";
#endif
#if defined (GUI_WIN32)
# define SLOPPY_ITALICS
# define isitalic(f) (colorinfo[(f)&0x7f].da.bits & COLOR_ITALIC)
#else
# undef SLOPPY_ITALICS
#endif
#if defined(GUI_WIN32) || defined(GUI_X11)
# define SLOPPY_BOXED
# define isboxed(f) ((colorinfo[(f)&0x7f].da.bits & COLOR_BOXED) \
|| (((f) & 0x80) \
&& (colorinfo[COLOR_FONT_SELECTION].da.bits & COLOR_BOXED)))
#else
# undef SLOPPY_BOXED
#endif
#if USE_PROTOTYPES
static void insimage(CHAR *ch, DRAWATTR *attr, int qty, int extent);
static void delimage(CHAR *ch, char *font, DRAWATTR *attr, int qty, int extent);
static void fillcell(_CHAR_ ch, _char_ font, long offset);
static void drawchar(CHAR *p, long qty, _char_ font, long offset);
static void compareimage(WINDOW win);
static void drawcurrent(DRAWINFO *di, int base, int same, DRAWATTR *da);
static void updateimage(WINDOW win);
static void genlastrow(WINDOW win);
static void opentextline(WINDOW win, CHAR *text, int len);
static void openchar(CHAR *p, long qty, _char_ font, long offset);
static void openmove(WINDOW win, long oldcol, long newcol, CHAR *image, long len);
# ifdef FEATURE_MISC
static ELVBOOL drawquick(WINDOW win);
# endif
# ifdef FEATURE_LISTCHARS
static void lcsdrawchar(CHAR *p, long qty, _char_ font, long offset);
# endif
#endif
#ifdef FEATURE_LISTCHARS
/* widths of the arrows to indicate side-scrolled text */
static int lprecedes, lextends;
#endif
#ifdef FEATURE_HLOBJECT
/* This stores the fontcodes for "hlobject1" through "hlobject9" */
static int hlfont[9];
static int hlobjects P_((WINDOW win, int font, long offset));
#ifdef FEATURE_MISC
static ELVBOOL hlprep P_((WINDOW win, BUFFER buf));
/* Decide what needs to be highlighted for "hllayers" and "hlobject". Return
* ElvTrue if same as previous highlighting for this window, or ElvFalse
* otherwise.
*/
static ELVBOOL hlprep(win, buf)
WINDOW win;
BUFFER buf;
{
VIINFO vinf;
CHAR *obj, *cp;
MARKBUF from, to;
long prevcount;
long cursoff;
int hllimit;
RESULT result;
int i, j;
long hlfrom, hlto;
ELVBOOL wholeline;
/* initialize hlfrom and hlto to impossible values */
hlfrom = o_bufchars(buf);
hlto = -1L;
/* remember the cursor offset, so we can restore it later */
cursoff = markoffset(win->cursor);
/* for each layer... */
wholeline = ElvFalse;
memset(&vinf, 0, sizeof vinf);
for (hllimit = 0, obj = o_hlobject(buf);
obj && hllimit < o_hllayers(win);
hllimit++)
{
/* build the vinf textobject command */
prevcount = vinf.count;
vinf.count = 0L;
while (*obj == ',' || elvspace(*obj))
obj++;
if (elvdigit(*obj))
{
for (vinf.count = 0; elvdigit(*obj); obj++)
vinf.count = vinf.count * 10 + *obj - '0';
}
if (obj[0] == 'V' && obj[1] && obj[2])
{
wholeline = ElvTrue;
vinf.command = obj[1];
vinf.key2 = obj[2];
obj += 3;
}
else if (obj[0] && obj[1])
{
wholeline = ElvFalse;
vinf.command = obj[0];
vinf.key2 = obj[1];
obj += 2;
}
else if (hllimit == 0)
break;
else
{
if (obj[0])
obj++;
vinf.count = prevcount + 1;
}
/* compute the endpoints of the textobject */
marksetoffset(win->cursor, cursoff);
result = vitextobj(win, &vinf, &from, &to);
if (result == RESULT_ERROR)
{
if (!*obj)
break;
hllimit--;
continue;
}
else if (result == RESULT_MORE)
{
to.offset = cursoff;
}
/* if supposed to use whole line, then expand endpoints */
if (wholeline)
{
/* move "from" backward to char after newline */
for (scanalloc(&cp, &from); scanprev(&cp) && *cp != '\n'; )
{
}
if (cp)
from.offset = markoffset(scanmark(&cp)) + 1;
else
from.offset = 0L;
scanfree(&cp);
/* move "to" forward to newline */
for (scanalloc(&cp, &to); *cp != '\n' && scannext(&cp); )
{
}
if (cp)
to.offset = markoffset(scanmark(&cp)) + 1;
else
to.offset = o_bufchars(buf);
}
/* if innermost range is the same as before, and nothing else
* is forcing us to redraw from scratch, then assume all ranges
* are unchanged.
*/
if (hllimit == 0
&& win->di->logic == DRAW_NORMAL
&& o_optimize
&& win->hlinfo[hllimit].from == markoffset(&from)
&& win->hlinfo[hllimit].to == markoffset(&to))
{
marksetoffset(win->cursor, cursoff);
return ElvTrue;
}
/* store the new range */
win->hlinfo[hllimit].from = markoffset(&from);
win->hlinfo[hllimit].to = markoffset(&to);
/* adjust the affected range */
if (win->hlinfo[hllimit].from < hlfrom)
hlfrom = win->hlinfo[hllimit].from;
if (win->hlinfo[hllimit].to > hlto)
hlto = win->hlinfo[hllimit].to;
}
marksetoffset(win->cursor, cursoff);
/* store the limits */
win->hlfrom = hlfrom;
win->hlto = hlto;
/* Assign fonts in reverse order, so "hlobject1" is outermost. If not
* enough fonts have been set, then recycle the defined ones. We can
* be sure that the first font has been set because we give it a
* default definition in the drawalloc() function.
*/
for (i = hllimit - 1, j = 0; i >= 0; i--)
{
win->hlinfo[i].font = hlfont[j++];
if (j >= QTY(hlfont) || colorinfo[hlfont[j]].da.bits == 0)
j = 0;
}
/* if nothing was highlighted, then clobber the hlinfo */
if (hllimit == 0)
win->hlinfo[0].from = -1L;
return ElvFalse;
}
#endif /* FEATURE_MISC */
/* compute the highlighted version of a font at a given point */
static int hlobjects(win, font, offset)
WINDOW win;
int font;
long offset;
{
int i;
/* if totally unaffected, then just return the font unchanged */
if (offset < win->hlfrom || win->hlto <= offset)
return font;
/* find the innermost layer which includes this offset */
for (i = 0; offset < win->hlinfo[i].from || win->hlinfo[i].to <= offset; i++)
{
}
/* combine the fonts */
return colortmp(font, win->hlinfo[i].font);
}
#endif /* defined(FEATURE_HLOBJECT) */
/* allocate a drawinfo struct, including the related arrays */
DRAWINFO *drawalloc(rows, columns, top)
int rows; /* height of new window image */
int columns;/* width of new window image */
MARK top; /* top of screen */
{
DRAWINFO *newp;
int i;
#ifdef FEATURE_HLOBJECT
CHAR name[20];
#endif
/* allocate the stuff */
newp = (DRAWINFO *)safealloc(1, sizeof *newp);
newp->newrow = (DRAWROW *)safealloc(rows, sizeof(DRAWROW));
newp->newline = (DRAWLINE *)safealloc(rows + 1, sizeof(DRAWLINE));
newp->curline = (DRAWLINE *)safealloc(rows, sizeof(DRAWLINE));
newp->newchar = (CHAR *)safealloc(rows * columns, sizeof(CHAR));
newp->newfont = (char *)safealloc(rows * columns, sizeof(char));
newp->curchar = (CHAR *)safealloc(rows * columns, sizeof(CHAR));
newp->curattr = (DRAWATTR *)safealloc(rows * columns, sizeof(DRAWATTR));
newp->offsets = (long *)safealloc(rows * columns, sizeof(long));
newp->topmark = markdup(top);
newp->bottommark = markdup(top);
/* clear the current image and the "new" image */
for (i = rows * columns; --i >= 0; )
{
newp->newchar[i] = newp->curchar[i] = ' ';
newp->newfont[i] = 0;
newp->curattr[i].bits = ~0; /* anything so newfont != curfont */
}
/* initialize the other variables */
newp->rows = rows;
newp->columns = columns;
newp->logic = DRAW_SCRATCH;
/* while we're initializing things, we might as well locate the
* font codes, if we haven't already done so.
*/
#ifdef FEATURE_HLOBJECT
for (i = 0; i < QTY(hlfont); i++)
{
sprintf(tochar8(name), "hlobject%d", i + 1);
hlfont[i] = colorfind(name);
}
colorset(hlfont[0], toCHAR("boxed"), ElvFalse);
#endif
#ifdef FEATURE_HLSEARCH
if (!searchfont)
{
searchfont = colorfind(toCHAR("hlsearch"));
colorset(searchfont, toCHAR("bold"), ElvFalse);
}
#endif
return newp;
}
/* free a drawinfo struct, including the related arrays */
void drawfree(di)
DRAWINFO *di; /* window image to destroy */
{
/* free the stuff */
safefree(di->newrow);
safefree(di->newline);
safefree(di->curline);
safefree(di->newchar);
safefree(di->newfont);
safefree(di->curchar);
safefree(di->curattr);
safefree(di->offsets);
markfree(di->topmark);
markfree(di->bottommark);
if (di->openimage)
{
safefree(di->openimage);
}
if (di->openline)
{
markfree(di->openline);
}
safefree(di);
}
/* re-output a rectangular part of a window's current image. The top-left
* corner of the window is (0,0).
*
* The redrawn area includes the interior and edges of the window. This means,
* for example, that if top==bottom, a single row will be (partially) redrawn.
*/
void drawexpose(win, top, left, bottom, right)
WINDOW win; /* window that was partially exposed */
int top; /* top edge to be redrawn */
int left; /* left edge to be redrawn */
int bottom; /* bottom edge to be redrawn */
int right; /* right edge to be redrawn */
{
#ifndef FEATURE_MISC
win->di->logic = DRAW_SCRATCH;
#else
int row, column, same, base, nonblank;
long firstline, lastline;
assert(win != NULL && top >= 0 && left >= 0 && bottom < o_lines(win)
&& right < o_columns(win) && top <= bottom && left <= right);
/* If this GUI has no moveto() function, then do nothing. We really
* should never get here anyway in that case, since elvis can't be
* running in full-screen mode.
*/
if (!gui->moveto)
{
return;
}
/* make sure we use the right colors. Basically, this is responsible
* for choosing whether to use the "normal" or "idle" colors for any
* unspecified attributes such as background color.
*/
if (guicolorsync(win))
{
/* colors changed, so must redraw whole window */
top = 0;
left = 0;
bottom = o_lines(win) - 1;
right = o_columns(win) - 1;
}
colorsetup();
/* for each row in the rectangle... */
for (row = top; row <= bottom; row++)
{
/* find the width of this row, ignoring trailing blanks */
for (nonblank = o_columns(win), base = o_columns(win) * row;
nonblank > left
&& drawdeffont(win->di, base + nonblank - 1)
&& win->di->curchar[base + nonblank - 1] == ' ';
nonblank--)
{
}
/* move to the first character we'll be redrawing in this row */
guimoveto(win, left, row);
/* for each segment of the row which shares the same font... */
for (column = left, base += column;
column <= right && column < nonblank;
base += same, column += same)
{
/* find the width of the segment */
for (same = 1;
column + same <= right
&& column + same < nonblank
&& drawspan(win->di, base + same, base);
same++)
{
}
/* output the segment */
(void)guidraw(win,
colorexpose(win->di->newfont[base],
&win->di->curattr[base]),
&win->di->curchar[base], same, 0);
}
/* if necessary, do a clrtoeol */
if (nonblank < right)
{
guiclrtoeol(win);
}
}
/* leave the cursor in the right place */
guimoveto(win, win->di->curscol, win->di->cursrow);
/* update the scrollbar, too */
if (gui->scrollbar)
{
if (win->md->move == dmnormal.move)
{
/* find line numbers of first and last lines shown */
firstline = markline(win->di->topmark) - 1;
lastline = markline(win->di->bottommark) - 1;
/* Some scrolling commands temporarily set bottommark
* to the end of the buffer. Ordinarily this causes
* no problems, but if an expose event occurs before
* the screen is redrawn, the the scrollbar's "thumb"
* will briefly extend to the bottom of the scrollbar.
* If we seem to be in that situation, then make a more
* reasonable guess about the screen bottom line number.
*/
if (lastline - firstline >= o_lines(win))
{
lastline = firstline + o_lines(win) - 1;
}
/* update the scrollbar */
(*gui->scrollbar)(win->gw, firstline, lastline,
o_buflines(markbuffer(win->cursor)));
}
else
{
(*gui->scrollbar)(win->gw, markoffset(win->di->topmark),
markoffset(win->di->bottommark), win->di->curnbytes);
}
}
guiflush();
#endif /* FEATURE_MISC */
}
/*----------------------------------------------------------------------------*/
/* This marks the end of the easy stuff! The remainder of this file consists
* of the drawimage() function and its support functions and variables.
*/
static WINDOW thiswin; /* The window being drawn */
static long thisline; /* the line being drawn */
static char thislnumstr[16];/* line number, as a string */
static int linesshown; /* number of different lines visible */
static int thiscell; /* index of next cell to draw */
static int thiscol; /* logical column number */
static int leftcol; /* leftmost column to actually draw */
static int rightcol; /* rightmost column to actully draw, plus 1 */
static int maxrow; /* number of rows to be drawn */
static int wantcurs; /* largest acceptable cursor row */
static int thisscroll; /* scroll distance while drawing this line */
static int scrollrows; /* #rows (i.e., area) scrolled by gui->scroll */
static int maxcell; /* number of cells to be drawn */
static int seloffset; /* offset of character, during selection */
static char selfont; /* font of character, during selection */
#ifdef FEATURE_REGION
static region_t *thisregion;
#endif
#ifdef FEATURE_LISTCHARS
static int lcscell; /* cell where arrow will be drawn */
#endif
/* insert some blanks into an image */
static void insimage(ch, attr, qty, extent)
CHAR *ch; /* where character insertion should begin */
DRAWATTR *attr; /* parallel array of attributes for "ch" characters */
int qty; /* number of blanks to be inserted */
int extent; /* number of characters after the insertion point */
{
register int i;
/* shift the characters and font codes */
for (i = extent; --i >= qty; )
{
ch[i] = ch[i - qty];
attr[i] = attr[i - qty];
}
/* initialize the newly inserted cells */
if (gui->newblank)
{
/* the newly inserted characters are all normal spaces */
for (i = 0; i < qty; i++)
{
ch[i] = ' ';
attr[i] = colorinfo[0].da;
}
}
else
{
/* the new cells' contents are undefined */
for (i = 0; i < qty; i++)
{
ch[i] = 0;
attr[i].bits = ~0;
}
}
}
/* delete some characters from an image, and add blanks to the end. */
static void delimage(ch, font, attr, qty, extent)
CHAR *ch; /* where character deletion should begin */
char *font; /* parallel array of font codes for "ch" characters */
DRAWATTR *attr; /* parallel array of attributes for "ch" characters */
int qty; /* number of characters to delete */
int extent; /* number of characters after the deletion point */
{
register int i;
/* shift the characters */
for (i = 0; i < extent - qty; i++)
{
ch[i] = ch[i + qty];
if (font)
font[i] = font[i + qty];
if (attr)
attr[i] = attr[i + qty];
}
/* we've dragged some normal characters onto the edge of the extent */
if (gui->newblank)
{
/* the new cells are filled with normal blanks */
for ( ; i < extent; i++)
{
ch[i] = ' ';
if (font)
font[i] = 0;
if (attr)
attr[i] = colorinfo[0].da;
}
}
else
{
/* the new cells' contents are undefined */
for ( ; i < extent; i++)
{
ch[i] = 0;
if (font)
font[i] = ~0;
if (attr)
attr[i].bits = ~0;
}
}
}
/* Fill in a character cell. This function is called by drawchar(), which
* in turn is called by an edit mode's image() function.
*/
static void fillcell(ch, font, offset)
_CHAR_ ch; /* new character to place in the next cell */
_char_ font; /* font code of new character */
long offset; /* buffer offset, or -1 if not from buffer */
{
register int i;
register DRAWINFO *di = thiswin->di;
assert(thiscell <= maxcell);
assert(maxcell <= maxrow * di->columns);
/* if we've reached the end of the screen without finding the cursor,
* then we'll need to do a little slop scrolling.
*/
if (thiscell == maxcell && (di->cursrow < 0 || di->cursrow > wantcurs))
{
/* scroll up 1 row */
delimage(di->newchar, di->newfont, NULL, (int)o_columns(thiswin), (int)(maxcell + o_columns(thiswin)));
for (i = 0; i < maxcell - o_columns(thiswin); i++)
{
di->offsets[i] = di->offsets[i + o_columns(thiswin)];
}
thisscroll++;
/* scroll the statistics, too */
di->newrow[1].insrows += di->newrow[0].insrows;
for (i = 0; i < maxrow - 1; i++)
{
di->newrow[i] = di->newrow[i + 1];
}
di->newrow[i].insrows =
di->newrow[i].shiftright =
di->newrow[i].inschars = 0;
for (i = 0; i < linesshown; i++)
{
di->newline[i].startrow--;
}
assert(di->cursrow != 0);
if (di->cursrow > 0)
di->cursrow--;
/* if the top /line/ has scrolled off screen, then scroll
* the line statistics, too.
*/
if (linesshown > 1 && di->newline[1].startrow == 0)
{
linesshown--;
for (i = 0; i < linesshown; i++)
{
di->newline[i] = di->newline[i + 1];
}
}
/* adjust limits, taking o_sidescroll into consideration */
thiscell -= o_columns(thiswin);
if (o_wrap(thiswin))
{
rightcol += o_columns(thiswin);
}
}
assert(thiscell < maxcell || di->cursrow >= 0);
/* if this is the cursor character, then the cursor belongs here */
#if 0
if (offset == markoffset(thiswin->cursor))
#else
if (offset >= markoffset(thiswin->cursor) && di->cursrow < 0) /*!!!*/
#endif
{
di->cursrow = thiscell / o_columns(thiswin);
}
/* if this cell really should be drawn, then draw it */
if (thiscol >= leftcol && thiscol < rightcol && thiscell < maxcell)
{
di->newchar[thiscell] = ch;
di->newfont[thiscell] = font;
di->offsets[thiscell] = offset;
thiscell++;
}
/* increment the column number */
thiscol++;
}
/* Add consecutive characters to the new image. This function is called by
* an edit mode's image() function to generate a screen image. In addition
* to displayable characters, this function gives special processing to
* the following:
* '\013' (VTAB) is interpretted as two '\n' characters.
* '\f' is interpretted as two '\n' characters.
* '\n' moves to the start of the next row.
* Note that tabs and backspaces are not given special treatment here;
* that's the responsibility of the edit mode's image() function.
*/
static void drawchar(p, qty, font, offset)
CHAR *p; /* the characters to be added */
long qty; /* number of characters to add */
_char_ font; /* font code of characters */
long offset; /* buffer offset of character, or -1 if not from buffer */
{
register CHAR ch; /* a character from *p */
register DRAWINFO *di = thiswin->di; /* window drawing info */
long delta; /* value to add to "offset" */
CHAR hifont; /* possibly highlighted font */
CHAR tmpch;
int i;
#ifdef FEATURE_REGION
/* if in a region, then merge the region's font into font */
if (thisregion)
font = colortmp(font, thisregion->font);
#endif
/* A negative qty value indicates that all characters have the same
* offset. Otherwise the characters will have consecutive offsets.
*/
if (qty < 0)
{
qty = -qty;
delta = 0;
}
else
{
delta = 1;
}
/* If in visual mode and this chunk contains the cursor, then remember
* the font now, *before* we add any sort of highlighting
*/
if (!thiswin->state->pop
&& offset >= 0
&& offset <= markoffset(thiswin->cursor)
&& (delta == 0 || markoffset(thiswin->cursor) < offset + delta * qty))
{
thiswin->di->cursface = font & 0x7f;
}
/* for each character... */
for ( ; qty > 0; qty--, p += delta, offset += delta)
{
/* copy the next char into a register variable */
ch = *p;
/* Treat '\f' and '\013' (VTAB) as two '\n's in a row
* (one of them recursively)
*/
if (ch == '\f' || ch == '\013')
{
tmpch = ch = '\n';
drawchar(&tmpch, 1, font, offset);
}
/* Handle non-ascii characters */
else if (ch >= 0x80)
{
switch (o_nonascii)
{
case 's': /* strip */
ch &= 0x7f;
if (ch < ' ' || ch == 0x7f)
ch = '.';
break;
case 'n': /* none */
ch = '.';
break; /* !!it B4: to make cc happy */
case 'm': /* most */
if (ch <= 0x9f)
ch = '.';
break; /* !!it B4: to make cc happy */
default: /* all */
/* no conversion necessary */;
}
}
/* If we're showing line numbers, and this character is supposed
* to be displayed in column 0, then output the line number
* before this character.
*/
if (o_number(thiswin) && thiscol == leftcol)
{
for (i = 0; i < 8; i++)
{
fillcell((CHAR)thislnumstr[i], COLOR_FONT_LNUM, -1);
}
}
/* If the offset is in the selected region for this window,
* then make the font letter uppercase so that it will be
* drawn highlighted. Also, make wide characters be either
* totally highlighted or totally unhighlighted.
*/
hifont = font;
#ifdef FEATURE_HLOBJECT
/* maybe use the highlighted version of this font */
hifont = hlobjects(thiswin, hifont, offset);
#endif
if (thiswin->seltop)
{
if (offset == seloffset && seloffset != -1)
{
hifont = selfont;
}
else
{
i = thiscol;
if (o_number(thiswin))
i -= 8;
if (markoffset(thiswin->seltop) <= offset
&& offset <= markoffset(thiswin->selbottom)
&& thiswin->selleft <= i
&& i <= thiswin->selright
&& (ch != '\n' || thiswin->seltype != 'r'))
{
hifont = font | 0x80;
}
}
selfont = hifont;
seloffset = offset;
}
#ifdef FEATURE_TEXTOBJ
else if (thiswin->match <= offset && offset < thiswin->matchend)
#else
else if (thiswin->match == offset)
#endif
{
hifont = font | 0x80; /* showmatch */
}
/* remember where this line started */
if (linesshown == 0 || di->newline[linesshown - 1].start != thisline)
{
assert(linesshown <= maxrow);
assert((thiscell % o_columns(thiswin)) ==
(o_number(thiswin) && leftcol == 0 ? 8 : 0));
di->newline[linesshown].start = thisline;
di->newline[linesshown++].startrow = thiscell / o_columns(thiswin);
}
/* if this is a newline, then treat it as a bunch of spaces */
if (ch == '\n')
{
/* remember this line's width. If a single "line"
* involves multiple newlines, then the width of the
* last non-empty phsyical line is kept.
*/
if (thiscol != 0)
{
di->newline[linesshown].width = thiscol;
}
/* "leftcol" has no effect on newlines */
if (thiscol < leftcol)
{
thiscol = leftcol;
}
/* Output a bunch of spaces. Note that the looping
* condition is sensitive to both the column number and
* the cell number, so it'll correctly handle lines
* whose width==o_columns(win). It emulates the :xw:
* brain-damaged newline, like a VT-100.
*
* This gets a bit tricky when you're appending to the
* end of a line, and you've just extended the line to
* reach edge of the screen. In this case, an extra
* blank line *should* appear on the screen; the cursor
* will be there. This test must be made outside the
* loop.
*/
i = o_columns(thiswin);
if ((offset == markoffset(thiswin->state->cursor)
&& thiscol % i == 0) || thiscol == leftcol)
{
fillcell(' ', hifont, offset);
}
/* Note: thiscell may've changed since previous "if" */
if (thiscell % i != 0)
{
for (i -= thiscell % i; i > 0; i--)
{
di->newchar[thiscell] = ' ';
di->newfont[thiscell] = hifont;
di->offsets[thiscell] = offset;
thiscol++, thiscell++;
}
}
/* reset the virtual column number */
thiscol = 0;
}
else /* normal character */
{
assert(ch >= ' ');
/* draw the character */
fillcell(ch, hifont, offset);
}
}
}
#ifdef FEATURE_LISTCHARS
/* This is a simplified version of drawchar(), intended *ONLY* for drawing the
* little arrows for sidescrolling when ":set nowrap lcs=extends:>,precedes:>"
*/
static void lcsdrawchar(p, qty, font, offset)
CHAR *p; /* the characters to be added */
long qty; /* number of characters to add -- always 1 */
_char_ font; /* font code of characters */
long offset; /* offset of chars -- ignored */
{
register DRAWINFO *di = thiswin->di; /* window drawing info */
di->newchar[lcscell] = *p;
di->newfont[lcscell] = font;
lcscell++;
}
#endif
/* This function compares old lines to new lines, and determines how much
* insert/deleting we should do, and approximately where we should do it.
*/
static void compareimage(win)
WINDOW win; /* window to be processed */
{
typedef enum {NEW, BEFORE, AFTER} type_t;
int i, j, k, diff, totdiff;
struct prevmatch_s
{
DRAWLINE *line; /* ptr to new line's current info */
type_t type; /* line position, relative to any change */
} *prev;
/* if we're supposed to redraw from scratch, then we won't be doing
* any inserting/deleting. Also, the "guidewidth" option interferes
* with shifting, so we disable it if guidewidth is set.
*/
if (win->di->logic == DRAW_SCRATCH
|| o_guidewidth(markbuffer(win->cursor)))
{
for (i = 0; i < maxrow; i++)
{
win->di->newrow[i].shiftright = 0;
assert(win->di->newrow[i].insrows == 0 && win->di->newrow[i].inschars == 0);
}
/* also, ignore anything in current image */
if (win->di->logic == DRAW_SCRATCH)
{
memset(win->di->curchar, 0, maxcell * sizeof(CHAR));
memset(win->di->curattr, 0, maxcell * sizeof(DRAWATTR));
}
return;
}
/* allocate an array for matching new lines to old lines */
prev = (struct prevmatch_s *)safealloc(linesshown, sizeof *prev);
/* Try to match each new line against current lines */
/* try to match top lines as being BEFORE a change */
i = 0;
for (j = 0; j < win->di->nlines && win->di->newline[0].start != win->di->curline[j].start; j++)
{
}
if (j < win->di->nlines)
{
do
{
prev[i].line = &win->di->curline[j];
prev[i].type = BEFORE;
} while (++i < linesshown
&& ++j < win->di->nlines
&& win->di->newline[i].start == win->di->curline[j].start);
}
/* try to match bottom lines as being AFTER a change */
for (k = linesshown - 1; k >= i; k--)
{
for (j = win->di->nlines - 1;
j >= 0
&& o_bufchars(markbuffer(win->cursor)) - win->di->newline[k].start
!= win->di->curnbytes - win->di->curline[j].start;
j--)
{
}
if (j >= 0)
{
do
{
prev[k].line = &win->di->curline[j];
prev[k].type = AFTER;
} while (--k >= i
&& --j >= i
&& o_bufchars(markbuffer(win->cursor)) - win->di->newline[k].start
== win->di->curnbytes - win->di->curline[j].start);
break;
}
prev[k].type = NEW;
}
/* any intervening lines are NEW */
for (; i < k; i++)
{
prev[i].type = NEW;
}
/* Compute character shifts/inserts/deletes for each row... */
for (i = 0; i < maxrow && win->di->newrow[i].lineoffset < o_bufchars(markbuffer(win->cursor)); i++)
{
/* find the line shown there (actually its index into newline[] and prev[]) */
for (j = 0; win->di->newrow[i].lineoffset != win->di->newline[j].start; j++)
{
assert(j + 1 < linesshown);
}
/* if it is a new line, then don't shift it */
if (prev[j].type == NEW)
{
win->di->newrow[i].shiftright = 0;
assert(win->di->newrow[i].inschars == 0);
}
else /* recognized old line, maybe insert/delete chars or rows */
{
/* number of chars to insert/delete is computed by change in length */
win->di->newrow[i].inschars = win->di->newline[j].width - prev[j].line->width;