/
console.c
2253 lines (2068 loc) · 54.5 KB
/
console.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
/*
* R : A Computer Language for Statistical Data Analysis
* file console.c
* Copyright (C) 1998--2003 Guido Masarotto and Brian Ripley
* Copyright (C) 2004-8 The R Foundation
* Copyright (C) 2004-10 The R Core Team
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, a copy is available at
* http://www.r-project.org/Licenses/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "win-nls.h"
#include <R_ext/Boolean.h>
extern Rboolean mbcslocale;
#define USE_MDI 1
extern void R_ProcessEvents(void);
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <string.h>
#include <ctype.h>
#include <wchar.h>
#include <limits.h>
#include <R_ext/rlocale.h>
#include "graphapp/ga.h"
#ifdef USE_MDI
#include "graphapp/stdimg.h"
#endif
#include "console.h"
#include "consolestructs.h"
#include "rui.h"
#include "getline/wc_history.h"
#include "Startup.h" /* for UImode */
#include <Fileio.h>
#include <stdint.h>
/* Surrogate Pairs Macro */
#define SURROGATE_PAIRS_HI_MIN ((uint16_t)0xd800)
#define SURROGATE_PAIRS_HI_MAX ((uint16_t)0xdbff)
#define SURROGATE_PAIRS_LO_MIN ((uint16_t)0xdc00)
#define SURROGATE_PAIRS_LO_MAX ((uint16_t)0xdfff)
#define SURROGATE_PAIRS_BIT_SZ ((uint32_t)10)
#define SURROGATE_PAIRS_MASK (((uint16_t)1 << SURROGATE_PAIRS_BIT_SZ)-1)
#define IsSurrogatePairsHi(_h) (SURROGATE_PAIRS_HI_MIN == \
((uint16_t)(_h) &~ (uint16_t)SURROGATE_PAIRS_MASK ))
#define IsSurrogatePairsLo(_l) (SURROGATE_PAIRS_LO_MIN == \
((uint16_t)(_l) &~ (uint16_t)SURROGATE_PAIRS_MASK ))
extern UImode CharacterMode;
static void performCompletion(control c);
static inline int wcswidth(const wchar_t *s)
{
return mbcslocale ? Ri18n_wcswidth(s, wcslen(s)) : wcslen(s);
}
static inline int wcwidth(const wchar_t s)
{
return mbcslocale ? Ri18n_wcwidth(s) : 1;
}
static void setCURCOL(ConsoleData p)
{
wchar_t *P = LINE(NUMLINES - 1);
int w0 = 0;
for (; P < LINE(NUMLINES - 1) + prompt_len + cur_pos; P++)
if(*P == L'\r') w0 = 0; else w0 += wcwidth(*P);
CURCOL = w0;
}
/* xbuf */
xbuf newxbuf(xlong dim, xint ms, xint shift)
{
xbuf p;
p = (xbuf) malloc(sizeof(struct structXBUF));
if (!p)
return NULL;
p->b = (wchar_t *) malloc((dim + 1) * sizeof(wchar_t));
if (!p->b) {
free(p);
return NULL;
}
p->user = (int *) malloc(ms * sizeof(int));
if (!p->user) {
free(p->b);
free(p);
return NULL;
}
p->s = (wchar_t **) malloc(ms * sizeof(wchar_t *));
if (!p->s) {
free(p->b);
free(p->user);
free(p);
return NULL;
}
p->ns = 1;
p->ms = ms;
p->shift = shift;
p->dim = dim;
p->av = dim;
p->free = p->b;
p->s[0] = p->b;
p->user[0] = -1;
*p->b = L'\0';
return p;
}
/* reallocate increased buffer sizes and update pointers */
void xbufgrow(xbuf p, xlong dim, xint ms)
{
if(dim > p->dim) {
wchar_t *ret = (wchar_t *) realloc(p->b, (dim + 1)*sizeof(wchar_t));
if(ret) {
int i, change;
change = ret - p->b;
p->b = ret;
p->av += change;
p->free += change;
for (i = 0; i < p->ns; i++) p->s[i] += change;
p->dim = dim;
}
}
if(ms > p->ms) {
wchar_t **ret = (wchar_t **) realloc(p->s, ms * sizeof(wchar_t *));
if(ret) {
int *ret2 = (int *) realloc(p->user, ms * sizeof(int));
if(ret2) {
p->s = ret;
p->user = ret2;
p->ms = ms;
}
}
}
}
void xbufdel(xbuf p)
{
if (!p) return;
free(p->s);
free(p->b);
free(p->user);
free(p);
}
static void xbufshift(xbuf p)
{
xint i;
xlong mshift;
wchar_t *new0;
if (p->shift >= p->ns) {
p->ns = 1;
p->av = p->dim;
p->free = p->b;
p->s[0] = p->b;
*p->b = L'\0';
p->user[0] = -1;
return;
}
new0 = p->s[p->shift];
mshift = new0 - p->s[0];
memmove(p->b, p->s[p->shift], (p->dim - mshift) * sizeof(wchar_t));
memmove(p->user, &p->user[p->shift], (p->ms - p->shift) * sizeof(int));
for (i = p->shift; i < p->ns; i++)
p->s[i - p->shift] = p->s[i] - mshift;
p->ns = p->ns - p->shift;
p->free -= mshift;
p->av += mshift;
}
static int xbufmakeroom(xbuf p, xlong size)
{
if (size > p->dim) return 0;
while ((p->av < size) || (p->ns == p->ms)) {
xbufshift(p);
}
p->av -= size;
return 1;
}
#define XPUTC(c) {xbufmakeroom(p,1); *p->free++=c;}
void xbufaddxc(xbuf p, wchar_t c)
{
int i;
switch (c) {
case L'\a':
gabeep();
break;
case L'\b':
if ((p->s[p->ns - 1])[0]) {
p->free--;
p->av++;
}
break;
case L'\t':
XPUTC(L' ');
*p->free = '\0';
/* Changed to width in 2.7.0 */
for (i = wcswidth(p->s[p->ns - 1]); (i % TABSIZE); i++) XPUTC(L' ');
break;
case L'\n':
XPUTC(L'\0');
p->s[p->ns] = p->free;
p->user[p->ns++] = -1;
break;
default:
XPUTC(c);
}
*p->free = L'\0';
}
void xbufaddxs(xbuf p, const wchar_t *s, int user)
{
const wchar_t *ps;
int l;
l = user ? (p->s[p->ns - 1])[0] : -1;
for (ps = s; *ps; ps++) xbufaddxc(p, *ps);
p->user[p->ns - 1] = l;
}
#define IN_CONSOLE
#include "rgui_UTF8.h"
extern size_t Rf_utf8towcs(wchar_t *wc, const char *s, size_t n);
static size_t enctowcs(wchar_t *wc, char *s, int n)
{
size_t nc = 0;
char *pb, *pe;
if((pb = strchr(s, UTF8in[0])) && *(pb+1) == UTF8in[1] &&
*(pb+2) == UTF8in[2]) {
*pb = '\0';
nc += mbstowcs(wc, s, n);
pb += 3; pe = pb;
while(*pe &&
!((pe = strchr(pb, UTF8out[0])) && *(pe+1) == UTF8out[1] &&
*(pe+2) == UTF8out[2])) pe++;
if(!*pe) return nc; /* FIXME */;
*pe = '\0';
/* convert string starting at pb from UTF-8 */
nc += Rf_utf8towcs(wc+nc, pb, (pe-pb));
pe += 3;
nc += enctowcs(wc+nc, pe, n-nc);
} else nc = mbstowcs(wc, s, n);
return nc;
}
static void xbufadds(xbuf p, const char *s, int user)
{
int n = strlen(s) + 1; /* UCS-2 must be shorter */
wchar_t tmp[n];
enctowcs(tmp, (char *) s, n);
xbufaddxs(p, tmp, user);
}
static void xbuffixl(xbuf p)
{
wchar_t *ps, *old;
if (!p->ns) return;
ps = p->s[p->ns - 1];
old = p->free;
p->free = ps + wcslen(ps);
p->av = p->dim - (p->free - p->b);
}
/* console */
rgb guiColors[numGuiColors] = {
White, Black, gaRed, /* consolebg, consolefg, consoleuser, */
White, Black, gaRed, /* pagerbg, pagerfg, pagerhighlight, */
White, Black, gaRed, /* dataeditbg, dataeditfg, dataedituser */
White, Black /* editorbg, editorfg */
};
extern int R_HistorySize; /* from Defn.h */
ConsoleData
newconsoledata(font f, int rows, int cols, int bufbytes, int buflines,
rgb *guiColors, int kind, int buffered, int cursor_blink)
{
ConsoleData p;
initapp(0, 0);
p = (ConsoleData) malloc(sizeof(struct structConsoleData));
if (!p)
return NULL;
p->kind = kind;
if (kind == CONSOLE) {
p->lbuf = newxbuf(bufbytes, buflines, SLBUF);
if (!p->lbuf) {
free(p);
return NULL;
}
p->kbuf = malloc(NKEYS * sizeof(wchar_t));
if (!p->kbuf) {
xbufdel(p->lbuf);
free(p);
return NULL;
}
} else {
p->lbuf = NULL;
p->kbuf = NULL;
}
p->bm = NULL;
p->rows = rows;
p->cols = cols;
for (int i=0; i<numGuiColors; i++)
p->guiColors[i] = guiColors[i];
p->f = f;
FH = fontheight(f);
FW = fontwidth(f);
WIDTH = (COLS + 1) * FW;
HEIGHT = (ROWS + 1) * FH + 1; /* +1 avoids size problems in MDI */
FV = FC = 0;
p->newfv = p->newfc = 0;
p->firstkey = p->numkeys = 0;
p->clp = NULL;
p->r = -1;
p->overwrite = 0;
p->lazyupdate = 1;
p->needredraw = 0;
p->wipe_completion = 0;
p->my0 = p->my1 = -1;
p->mx0 = 5;
p->mx1 = 14;
p->sel = 0;
p->input = 0;
p->lazyupdate = buffered;
p->cursor_blink = cursor_blink;
return (p);
}
static int col_to_pos(ConsoleData p, int x)
{
if(mbcslocale) {
int w0 = 0, cnt = 0;
wchar_t *P = LINE(CURROW);
for (; w0 < x && *P && P - LINE(CURROW) <= max_pos + prompt_len; ) {
w0 += Ri18n_wcwidth(*P++);
cnt++;
}
return(cnt - prompt_len);
} else
return(min(x - prompt_len, max_pos));
}
static int within_input(ConsoleData p, int mx, int my)
{
return(my == CURROW && mx >= prompt_wid && col_to_pos(p, mx) < max_pos);
}
/* Intersect the mouse selection with the input region. If no overlap or !apply, do nothing*/
static int intersect_input(ConsoleData p, int apply)
{
int my0 = p->my0, my1 = p->my1, mx0 = p->mx0, mx1 = p->mx1, temp;
if (my0 > my1 || (my0 == my1 && mx0 > my1)) { /* put them in order */
temp = my0;
my0 = my1;
my1 = temp;
temp = mx0;
mx0 = mx1;
mx1 = temp;
}
if (my1 < CURROW || my0 > CURROW) return(0);
if (my0 < CURROW) mx0 = 0;
if (my1 > CURROW) mx1 = COLS;
if (mx1 < CURCOL || col_to_pos(p, mx0) >= max_pos) return(0);
mx0 = max(mx0, prompt_wid);
while (col_to_pos(p, mx1) >= max_pos) mx1--;
if (apply) {
p->mx0 = mx0;
p->mx1 = mx1;
p->my0 = my0;
p->my1 = my1;
}
return(1);
}
/* Here fch and lch are columns, and we have to cope with both MBCS
and double-width chars. */
static void writelineHelper(ConsoleData p, int fch, int lch,
rgb fgr, rgb bgr, int j, int len, wchar_t *s)
{
rect r;
/* This is right, since columns are of fixed size */
r = rect(BORDERX + fch * FW, BORDERY + j * FH, (lch - fch + 1) * FW, FH);
gfillrect(p->bm, bgr, r);
if (len > FC+fch) {
/* Some of the string is visible: */
if(mbcslocale) {
int i, w0, nc;
wchar_t *P = s, *q;
Rboolean leftedge;
nc = (wcslen(s) + 1) * sizeof(wchar_t); /* overkill */
wchar_t buff[nc];
q = buff;
leftedge = FC && (fch == 0);
if(leftedge) fch++;
for (w0 = -FC; w0 < fch && *P; P++) /* should have enough ... */
w0 += Ri18n_wcwidth(*P);
/* Now we have got to on or just after the left edge.
Possibly have a widechar hanging over.
If so, fill with blanks.
*/
if(w0 > fch) for(i = 0; i < w0 - fch; i++) *q++ = L' ';
if (leftedge) *q++ = L'$';
while (w0 < lch) {
if(!*P) break;
w0 += Ri18n_wcwidth(*P);
if(w0 > lch) break; /* char straddling the right edge
is not displayed */
*q++ = *P++;
}
if((len > FC+COLS) && (lch == COLS - 1)) *q++ = L'$';
else *q++ = *P++;
*q = L'\0';
gdrawwcs(p->bm, p->f, fgr, pt(r.x, r.y), buff);
} else {
int last;
wchar_t ch, chf, chl;
/* we don't know the string length, so modify it in place */
if (FC && (fch == 0)) {chf = s[FC]; s[FC] = '$';} else chf = L'\0';
if ((len > FC+COLS) && (lch == COLS - 1)) {
chl = s[FC+lch]; s[FC+lch] = '$';
} else chl = L'\0';
last = FC + lch + 1;
if (len > last) {ch = s[last]; s[last] = L'\0';} else ch = L'\0';
gdrawwcs(p->bm, p->f, fgr, pt(r.x, r.y), &s[FC+fch]);
/* restore the string */
if (ch) s[last] = ch;
if (chl) s[FC+lch] = chl;
if (chf) s[FC] = chf;
}
}
}
#define WLHELPER(a, b, c, d) writelineHelper(p, a, b, c, d, j, len, s)
/* write line i of the buffer at row j on bitmap */
static int writeline(control c, ConsoleData p, int i, int j)
{
wchar_t *s, *stmp, *p0;
int insel, len, col1, d;
int c1, c2, c3, x0, y0, x1, y1;
rect r;
int bg, fg, highlight, base;
if (p->kind == CONSOLE) base = consolebg;
else if (p->kind == PAGER) base = pagerbg;
else base = dataeditbg;
bg = p->guiColors[base];
fg = p->guiColors[base+1];
highlight = p->guiColors[base+2];
if ((i < 0) || (i >= NUMLINES)) return 0;
stmp = s = LINE(i);
len = wcswidth(stmp);
/* If there is a \r in the line, we need to preprocess it */
if((p0 = wcschr(s, L'\r'))) {
int l, l1;
stmp = LINE(i);
wchar_t s0[wcslen(stmp) +1];
s = s0;
l = p0 - stmp;
wcsncpy(s, stmp, l);
stmp = p0 + 1;
while((p0 = wcschr(stmp, L'\r'))) {
l1 = p0 - stmp;
wcsncpy(s, stmp, l1);
if(l1 > l) l = l1;
stmp = p0 + 1;
}
l1 = wcslen(stmp);
wcsncpy(s, stmp, l1);
if(l1 > l) l = l1;
s[l] = L'\0';
len = l; /* for redraw that uses len */
}
col1 = COLS - 1;
insel = p->sel ? ((i - p->my0) * (i - p->my1)) : 1;
if (insel < 0) {
WLHELPER(0, col1, bg, fg);
return len;
}
if ((USER(i) >= 0) && (USER(i) < FC + COLS)) {
if (USER(i) <= FC)
WLHELPER(0, col1, highlight, bg);
else {
d = USER(i) - FC;
WLHELPER(0, d - 1, fg, bg);
WLHELPER(d, col1, highlight, bg);
}
} else if (USER(i) == -2) {
WLHELPER(0, col1, highlight, bg);
} else
WLHELPER(0, col1, fg, bg);
/* This is the cursor, and it may need to be variable-width */
if ((p->r >= 0) && (CURCOL >= FC) && (CURCOL < FC + COLS) &&
(i == NUMLINES - 1) && (p->sel == 0 || !intersect_input(p, 0))) {
if (!p->overwrite) {
if (p->cursor_blink) {
setcaret(c, BORDERX + (CURCOL - FC) * FW, BORDERY + j * FH,
p->cursor_blink == 1 ? 1 : FW/4, FH);
showcaret(c, 1);
} else showcaret(c, 0);
if (p->cursor_blink < 2) {
r = rect(BORDERX + (CURCOL - FC) * FW, BORDERY + j * FH, FW/4, FH);
gfillrect(p->bm, highlight, r);
}
} else if(mbcslocale) { /* determine the width of the current char */
int w0;
wchar_t *P = s, wc = 0, nn[2] = L" ";
for (w0 = 0; w0 <= CURCOL; P++) {
wc = *P;
if(!*P) break;
w0 += Ri18n_wcwidth(wc);
}
/* term string '\0' box width = 1 fix */
w0 = wc ? Ri18n_wcwidth(wc) : 1;
nn[0] = wc;
if (p->cursor_blink) {
setcaret(c, BORDERX + (CURCOL - FC) * FW, BORDERY + j * FH,
p->cursor_blink == 1 ? 1 : FW/4, FH);
showcaret(c, 1);
} else showcaret(c, 0);
if (p->cursor_blink < 2) {
r = rect(BORDERX + (CURCOL - FC) * FW, BORDERY + j * FH,
w0 * FW, FH);
gfillrect(p->bm, highlight, r);
gdrawwcs(p->bm, p->f, bg, pt(r.x, r.y), nn);
}
} else {
if (p->cursor_blink) {
setcaret(c, BORDERX + (CURCOL - FC) * FW, BORDERY + j * FH,
p->cursor_blink == 1 ? 1 : FW, FH);
showcaret(c, 1);
} else showcaret(c, 0);
if (p->cursor_blink < 2)
WLHELPER(CURCOL - FC, CURCOL - FC, bg, highlight);
}
}
if (insel != 0) return len;
c1 = (p->my0 < p->my1);
c2 = (p->my0 == p->my1);
c3 = (p->mx0 < p->mx1);
if (c1 || (c2 && c3)) {
x0 = p->mx0; y0 = p->my0;
x1 = p->mx1; y1 = p->my1;
} else {
x0 = p->mx1; y0 = p->my1;
x1 = p->mx0; y1 = p->my0;
}
if (i == y0) {
if (FC + COLS < x0) return len;
if(mbcslocale) {
int w0, w1 = 1;
wchar_t *P = s;
for (w0 = 0; w0 < x0; P++) {
if(!*P) break;
w1 = Ri18n_wcwidth(*P);
w0 += w1;
}
if(w0 > x0) x0 = w0 - w1;
}
c1 = (x0 > FC) ? (x0 - FC) : 0;
} else
c1 = 0;
if (i == y1) {
if (FC > x1) return len;
if(mbcslocale) {
int w0;
wchar_t *P = s;
for (w0 = 0; w0 <= x1; P++) {
if(!*P) break;
w0 += Ri18n_wcwidth(*P);
}
x1 = w0 - 1;
}
c2 = (x1 > FC + COLS) ? (COLS - 1) : (x1 - FC);
} else
c2 = COLS - 1;
WLHELPER(c1, c2, bg, fg);
return len;
}
void drawconsole(control c, rect r) /* r is unused here */
{
ConsoleData p = getdata(c);
int i, ll, wd, maxwd = 0;
ll = min(NUMLINES, ROWS);
if(!BM) return;; /* This is a workaround for PR#1711.
BM should never be null here */
if (p->kind == PAGER)
gfillrect(BM, p->guiColors[pagerbg], getrect(BM));
else
gfillrect(BM, p->guiColors[consolebg], getrect(BM));
if(!ll) return;;
for (i = 0; i < ll; i++) {
wd = WRITELINE(NEWFV + i, i);
if(wd > maxwd) maxwd = wd;
}
RSHOW(getrect(c));
FV = NEWFV;
p->needredraw = 0;
/* always display scrollbar if FC > 0 */
if(maxwd < COLS - 1) maxwd = COLS - 1;
maxwd += FC;
gchangescrollbar(c, HWINSB, FC, maxwd-FC, COLS,
p->kind == CONSOLE || NUMLINES > ROWS);
gchangescrollbar(c, VWINSB, FV, NUMLINES - 1 , ROWS, p->kind == CONSOLE);
}
void setfirstvisible(control c, int fv)
{
ConsoleData p = getdata(c);
int ds, rw, ww;
if (NUMLINES <= ROWS) return;;
if (fv < 0) fv = 0;
else if (fv > NUMLINES - ROWS) fv = NUMLINES - ROWS;
if (fv < 0) fv = 0;
ds = fv - FV;
if ((ds == 0) && !p->needredraw) return;;
if (abs(ds) > 1) {
NEWFV = fv;
REDRAW;
return;;
}
if (p->needredraw) {
ww = min(NUMLINES, ROWS) - 1;
rw = FV + ww;
writeline(c, p, rw, ww);
if (ds == 0) {
RSHOW(RLINE(ww));
return;;
}
}
if (ds == 1) {
gscroll(BM, pt(0, -FH), RMLINES(0, ROWS - 1));
if (p->kind == PAGER)
gfillrect(BM, p->guiColors[pagerbg], RLINE(ROWS - 1));
else
gfillrect(BM, p->guiColors[consolebg], RLINE(ROWS - 1));
WRITELINE(fv + ROWS - 1, ROWS - 1);
}
else if (ds == -1) {
gscroll(BM, pt(0, FH), RMLINES(0, ROWS - 1));
if (p->kind == PAGER)
gfillrect(BM, p->guiColors[pagerbg], RLINE(0));
else
gfillrect(BM, p->guiColors[consolebg], RLINE(0));
WRITELINE(fv, 0);
}
RSHOW(getrect(c));
FV = fv;
NEWFV = fv;
p->needredraw = 0;
gchangescrollbar(c, VWINSB, fv, NUMLINES - 1 , ROWS, p->kind == CONSOLE);
}
void setfirstcol(control c, int newcol)
{
ConsoleData p = getdata(c);
int i, ml, li, ll;
ll = (NUMLINES < ROWS) ? NUMLINES : ROWS;
if (newcol > 0) {
for (i = 0, ml = 0; i < ll; i++) {
/* <FIXME> this should really take \r into account */
li = wcswidth(LINE(NEWFV + i));
ml = (ml < li) ? li : ml;
}
ml = ml - COLS;
ml = 5*(ml/5 + 1);
if (newcol > ml) newcol = ml;
}
if (newcol < 0) newcol = 0;
FC = newcol;
REDRAW;
}
void console_mousedrag(control c, int button, point pt)
{
ConsoleData p = getdata(c);
pt.x -= BORDERX;
pt.y -= BORDERY;
if (button & LeftButton) {
int r, s;
r=((pt.y > 32000) ? 0 : ((pt.y > HEIGHT) ? HEIGHT : pt.y))/FH;
s=((pt.x > 32000) ? 0 : ((pt.x > WIDTH) ? WIDTH : pt.x))/FW;
if ((r < 0) || (r > ROWS) || (s < 0) || (s > COLS))
return;;
p->my1 = FV + r;
p->mx1 = FC + s;
p->needredraw = 1;
p->sel = 1;
if (within_input(p, p->mx1, p->my1)) {
cur_pos = col_to_pos(p, p->mx1);
setCURCOL(p);
}
if (pt.y <= 0) setfirstvisible(c, FV - 3);
else if (pt.y >= ROWS*FH) setfirstvisible(c, FV+3);
if (pt.x <= 0) setfirstcol(c, FC - 3);
else if (pt.x >= COLS*FW) setfirstcol(c, FC+3);
else REDRAW;
}
}
void console_mouserep(control c, int button, point pt)
{
ConsoleData p = getdata(c);
if ((button & LeftButton) && (p->sel)) console_mousedrag(c, button,pt);
}
void console_mousedown(control c, int button, point pt)
{
ConsoleData p = getdata(c);
pt.x -= BORDERX;
pt.y -= BORDERY;
if (p->sel) {
p->sel = 0;
p->needredraw = 1;
}
if (button & LeftButton) {
p->my0 = FV + pt.y/FH;
p->mx0 = FC + pt.x/FW;
if (within_input(p, p->mx0, p->my0) ||
(p->my0 == CURROW && p->mx0 > prompt_wid)) {
cur_pos = col_to_pos(p, p->mx0);
setCURCOL(p);
p->needredraw = 1;
}
}
if (p->needredraw) REDRAW;
}
void consoletogglelazy(control c)
{
ConsoleData p = getdata(c);
if (p->kind == PAGER) return;
p->lazyupdate = (p->lazyupdate + 1) % 2;
}
int consolegetlazy(control c)
{
ConsoleData p = getdata(c);
return p->lazyupdate;
}
void consoleflush(control c)
{
REDRAW;
}
/* These are the getline keys ^A ^E ^B ^F ^N ^P ^K ^H ^D ^U ^T ^O,
plus ^Z for EOF.
We also use ^C ^V/^Y ^X (copy/paste/both) ^W ^L
*/
#define BEGINLINE 1
#define ENDLINE 5
#define CHARLEFT 2
#define CHARRIGHT 6
#define NEXTHISTORY 14
#define PREVHISTORY 16
#define KILLRESTOFLINE 11
#define BACKCHAR 8
#define DELETECHAR 4
#define KILLLINE 21
#define CHARTRANS 20
#define OVERWRITE 15
#define EOFKEY 26
/* ^I for completion */
#define TABKEY 9
/* free ^G ^Q ^R ^S, perhaps ^J */
static void checkpointpos(xbuf p, int save)
{
static int ns, av;
static wchar_t *free;
if(save) {
ns = p->ns;
av = p->av;
free = p->free;
} else {
p->ns = ns;
p->av = av;
p->free = free;
}
}
static void storekey(control c, int k)
{
ConsoleData p = getdata(c);
if (p->wipe_completion) {
p->wipe_completion = 0;
checkpointpos(p->lbuf, 0);
/* mark whole of current line as user input */
USER(NUMLINES-1) = 0;
p->needredraw = 1;
REDRAW;
}
if (p->kind == PAGER) return;
if (k == BKSP) k = BACKCHAR;
if (k == TABKEY) {
performCompletion(c);
return;
}
if (p->numkeys >= NKEYS) {
gabeep();
return;;
}
p->kbuf[(p->firstkey + p->numkeys) % NKEYS] = k;
p->numkeys++;
}
static void storetab(control c)
{
ConsoleData p = getdata(c);
p->kbuf[(p->firstkey + p->numkeys) % NKEYS] = L' ';
p->numkeys++;
}
#include <Rinternals.h>
#include <R_ext/Parse.h>
static int completion_available = -1;
void set_completion_available(int x)
{
completion_available = x;
}
static void performCompletion(control c)
{
ConsoleData p = getdata(c);
int i, alen, alen2, max_show = 10, cursor_position = p->c - prompt_wid;
wchar_t *partial_line = LINE(NUMLINES - 1) + prompt_wid;
const char *additional_text;
SEXP cmdSexp, cmdexpr, ans = R_NilValue;
ParseStatus status;
if(!completion_available) {
storetab(c);
return;
}
if(completion_available < 0) {
char *p = getenv("R_COMPLETION");
if(p && strcmp(p, "FALSE") == 0) {
completion_available = 0;
storetab(c);
return;
}
/* First check if namespace is loaded */
if(findVarInFrame(R_NamespaceRegistry, install("utils"))
!= R_UnboundValue) completion_available = 1;
else { /* Then try to load it */
char *p = "try(loadNamespace('utils'), silent=TRUE)";
PROTECT(cmdSexp = mkString(p));
cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue));
if(status == PARSE_OK) {
for(i = 0; i < length(cmdexpr); i++)
eval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv);
}
UNPROTECT(2);
if(findVarInFrame(R_NamespaceRegistry, install("utils"))
!= R_UnboundValue) completion_available = 1;
else {
completion_available = 0;
return;
}
}
}
/* FIXME: need to escape quotes properly */
wchar_t pline[wcslen(partial_line) + 1];
wcscpy(pline, partial_line);
/* poor attempt at escaping quotes that sort of works */
alen = wcslen(pline);
for (i = 0; i < alen; i++)
if (pline[i] == '"') pline[i] = L'\'';
char cmd[wcslen(pline) + 100];
sprintf(cmd, "utils:::.win32consoleCompletion(\"%ls\", %d)",
pline, cursor_position);
PROTECT(cmdSexp = mkString(cmd));
cmdexpr = PROTECT(R_ParseVector(cmdSexp, -1, &status, R_NilValue));
if (status != PARSE_OK) {
UNPROTECT(2);
/* Uncomment next line to debug */
/* Rprintf("failed: %s \n", cmd); */
/* otherwise pretend that nothing happened and return */
return;
}
/* Loop is needed here as EXPSEXP will be of length > 1 */
for(i = 0; i < length(cmdexpr); i++)
ans = eval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv);
UNPROTECT(2);
/* ans has the form list(addition, possible), where 'addition' is
unique additional text if any, and 'possible' is a character
vector holding possible completions if any (already formatted
for linewise printing in the current implementation). If
'possible' has any content, we want to print those (or show in
status bar or whatever). Otherwise add the 'additional' text
at the cursor */
#define ADDITION 0
#define POSSIBLE 1
alen = length(VECTOR_ELT(ans, POSSIBLE));
additional_text = CHAR(STRING_ELT( VECTOR_ELT(ans, ADDITION), 0 ));
alen2 = strlen(additional_text);
if (alen) {
/* make a copy of the current string first */
wchar_t *p1 = LINE(NUMLINES - 1);
checkpointpos(p->lbuf, 1);
char buf1[MB_CUR_MAX * wcslen(p1) + 1];
sprintf(buf1,"%ls\n", p1);
consolewrites(c, buf1);
for (i = 0; i < min(alen, max_show); i++) {
consolewrites(c, CHAR(STRING_ELT(VECTOR_ELT(ans, POSSIBLE), i)));
consolewrites(c, "\n");
}
if (alen > max_show)
consolewrites(c, "\n[...truncated]\n");
p->wipe_completion = 1;
}
if (alen2)
for (i = 0; i < alen2; i++) storekey(c, additional_text[i]);
return;
}
/* deletes that part of the selection which is on the input line */
static void deleteselected(ConsoleData p)
{
if (p->sel) {
int s0, s1;
wchar_t *cur_line;
if (intersect_input(p, 1)) {
/* convert to bytes after the prompt */
s0 = col_to_pos(p, p->mx0);
s1 = col_to_pos(p, p->mx1);