-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildexecute.cpp
1681 lines (1580 loc) · 70.5 KB
/
buildexecute.cpp
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
//
// Copyright 2015-2022 by Kevin L. Goodwin [[email protected]]; All rights reserved
//
// This file is part of K.
//
// K 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.
//
// K 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 K. If not, see <http://www.gnu.org/licenses/>.
//
#include "ed_main.h"
#include "stringlist.h"
#include <stdexcept>
//
// A single textArgBuf has a major disadvantage: macros which use any inline
// literal args overwrite the last USER arg. Define SINGLE_TextArgBuffer to 0
// to remove this annoying behavior.
//
// <041220> klg will do the same thing for SearchSpecifiers
//
// <041220> klg this has trouble with macros that prompt the user: what user
// types is not what ends up being supplied to the cmd executed.
// So the transition from Interpreting() to !Interpreting()
// needs to be replaced. Need more research into internals, esp
// GetTextargString() nuances.
//
#define SINGLE_TextArgBuffer 1
#if SINGLE_TextArgBuffer
STATIC_VAR std::string s_textArgBuffer;
STATIC_FXN std::string &TextArgBuffer() { return s_textArgBuffer; }
#else
STATIC_VAR std::string s_macroTextArgBuffer, s_userTextArgBuffer;
STATIC_FXN std::string &TextArgBuffer() { return Interpreter::Interpreting() ? s_macroTextArgBuffer : s_userTextArgBuffer; }
#endif
STATIC_VAR Point s_SelAnchor;
STATIC_VAR Point s_SelEnd;
STATIC_VAR int s_iArgCount; // write ONLY via Clr_g_ArgCount(), Inc_g_ArgCount(); read ONLY via Get_g_ArgCount()
int Get_g_ArgCount() { return s_iArgCount; }
STATIC_FXN int Inc_g_ArgCount() { return ++s_iArgCount; }
STATIC_FXN void Clr_g_ArgCount() { s_iArgCount = 0; }
void ClearArgAndSelection() { PCV;
pcv->FBuf()->BlankAnnoDispSrcEdge( BlankDispSrc_SEL, false );
pcv->FreeHiLiteRects();
s_SelEnd.lin = -1;
if( Get_g_ArgCount() > 0 ) { 0 && DBG( "%s+", __func__ );
// MoveCursor
pcv->MoveCursor_NoUpdtWUC( s_SelAnchor.lin, s_SelAnchor.col ); 0 && DBG( "%s-", __func__ );
Clr_g_ArgCount();
}
}
void ExtendSelectionHilite( const Point &pt ) { PCV;
pcv->FBuf()->BlankAnnoDispSrcEdge( BlankDispSrc_SEL, true );
pcv->FreeHiLiteRects(); // ###############################################################
if( g_fBoxMode ) {
Rect hilite;
hilite.flMin.lin = std::min( s_SelAnchor.lin, pt.lin );
hilite.flMax.lin = std::max( s_SelAnchor.lin, pt.lin );
auto fLinesel( false );
if( pt.col > s_SelAnchor.col ) {
hilite.flMin.col = s_SelAnchor.col;
hilite.flMax.col = pt .col - 1;
}
else {
if( pt.col == s_SelAnchor.col
&& pt.lin != s_SelAnchor.lin
) {
fLinesel = true;
hilite.flMin.col = 0;
hilite.flMax.col = COL_MAX;
}
else {
if( pt.col < s_SelAnchor.col ) {
hilite.flMin.col = pt .col;
hilite.flMax.col = s_SelAnchor.col - 1;
}
else {
hilite.flMin.col = s_SelAnchor.col;
hilite.flMax.col = pt .col;
}
}
}
// if( !Interpreter::Interpreting() ) // comment out so selword macro leaves updated dialog line
{
FixedCharArray<100> buf;
if( fLinesel ) {
buf.Sprintf( "Arg [%d] %d lines [%d..%d]"
, Get_g_ArgCount()
, hilite.height()
, hilite.flMin.lin + 1
, hilite.flMax.lin + 1
);
}
else {
buf.Sprintf( "Arg [%d] %dw x %dh box (%d,%d) (%d,%d)"
, Get_g_ArgCount()
, hilite.width()
, hilite.height()
, hilite.flMin.lin + 1
, hilite.flMin.col + 1
, hilite.flMax.lin + 1
, hilite.flMax.col + 1
);
}
DispRawDialogStr( buf.c_str() );
}
pcv->InsHiLiteBox( ColorTblIdx::SEL, hilite ); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
else { // STREAM mode
if( s_SelAnchor.lin == pt.lin ) { // 1-LINE STREAM
pcv->InsHiLite1Line( ColorTblIdx::SEL, s_SelAnchor.lin, s_SelAnchor.col, pt.col + ((s_SelAnchor.col < pt.col) ? -1 : 0) ); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
else { // MULTILINE STREAM
// redraw ANCHOR-line hilite
pcv->InsHiLite1Line( ColorTblIdx::SEL, s_SelAnchor.lin, s_SelAnchor.col, (s_SelAnchor.lin <= pt.lin) ? COL_MAX : 0 ); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// redraw middle line(s) hilite, if any
const auto yDelta( pt.lin - s_SelAnchor.lin );
if( Abs(yDelta) > 1 ) {
Rect hilite;
hilite.flMin.lin = std::min( s_SelAnchor.lin, pt.lin ) + 1;
hilite.flMax.lin = std::max( s_SelAnchor.lin, pt.lin ) - 1;
hilite.flMin.col = 0;
hilite.flMax.col = COL_MAX;
pcv->InsHiLiteBox( ColorTblIdx::SEL, hilite ); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
// redraw CURSOR-line hilite
COL xFirst, xLast;
if( s_SelAnchor.lin > pt.lin ) { xFirst = COL_MAX; xLast = pt.col ; }
else { xFirst = 0 ; xLast = pt.col - 1; }
pcv->InsHiLite1Line( ColorTblIdx::SEL, pt.lin, xFirst, xLast ); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
s_SelEnd = pt;
}
//--------------------------------------------------------------
bool ARG::bp() {
int *pcrash( nullptr );
*pcrash = 0; // intentional crash
SW_BP;
return true;
}
bool ARG::cancel() { 0 && DBG( "%s+", __func__ );
switch( d_argType ) {
break;case NOARG: MsgClr();
break;default: if( !Interpreter::Interpreting() ) {
fnMsg( "Argument cancelled" );
}
ClearArgAndSelection();
// following does NOT work to restore the cursor to its initial position prior to selword macro execution
// why? because the selword macro moves the cursor to one end of the word BEFORE it begins the selection
// g_CurView()->MoveCursor( s_SelAnchor );
}
if( !Interpreter::Interpreting() ) {
DispNeedsRedrawVerticalCursorHilite();
}
FlushKeyQueuePrimeScreenRedraw();
return true;
}
PCChar ARG::ArgTypeName() const { 0 && DBG( "%s: %X", __func__, ActualArgType() );
switch( ActualArgType() ) {
default : return "unknown";
case NOARG : return "NOARG";
case NULLARG : return "NULLARG";
case TEXTARG : return "TEXTARG";
case LINEARG : return "LINEARG";
case STREAMARG : return "STREAMARG";
case BOXARG : return "BOXARG";
}
}
std::string ArgTypeNames( int argval ) {
STATIC_CONST struct {
int mask;
PCChar name;
} tbl[] = {
{ NOARG , "NOARG" },
{ TEXTARG , "TEXTARG" },
{ NULLARG , "NULLARG" },
{ NULLEOL , "NULLEOL" },
{ NULLEOW , "NULLEOW" },
{ LINEARG , "LINEARG" },
{ BOXARG , "BOXARG" },
{ STREAMARG , "STREAMARG" },
{ NUMARG , "NUMARG" },
{ MARKARG , "MARKARG" },
{ BOXSTR , "BOXSTR" },
{ MODIFIES , "MODIFIES" },
{ KEEPMETA , "KEEPMETA" },
{ WINDOWFUNC , "WINDOWFUNC" },
{ CURSORFUNC , "CURSORFUNC" },
{ MACROFUNC , "MACROFUNC" },
};
std::string rv;
BoolOneShot first;
for( const auto &te : tbl ) {
if( te.mask & argval ) {
if( !first ) { rv.append( "+" ); }
rv.append( te.name );
}
}
return rv;
}
bool ARG::BadArg() const {
ClearArgAndSelection();
return ErrorDialogBeepf( "'%s' Invalid Argument (%s)", CmdName(), ArgTypeName() );
}
bool ARG::ErrPause( PCChar fmt, ... ) const {
SprintfBuf title( "'%s' %s", CmdName(), fmt );
va_list args;
va_start( args, fmt );
VErrorDialogBeepf( title, args );
va_end(args);
return false;
}
bool ARG::fnMsg( PCChar fmt, ... ) const { // like ::Msg(), but prefixes the name of the active EdFxn
SprintfBuf title( "%s: %s", CmdName(), fmt );
va_list args;
va_start( args, fmt );
VMsg( title, args );
va_end(args);
return false;
}
// Those ARG::Xxx that interpret a selection arg as LINEARG|BOXARG or
// STREAMARG _regardless of the g_fBoxMode setting_ use
// ConvertLineOrBoxArgToStreamArg() and ConvertStreamargToLineargOrBoxarg() to
// get ARG in their preferred arg type:
//
void ARG::ConvertLineOrBoxArgToStreamArg() {
if( d_argType == LINEARG ) {
d_streamarg.flMin.lin = d_linearg.yMin;
d_streamarg.flMax.lin = d_linearg.yMax;
d_streamarg.flMin.col =
d_streamarg.flMax.col = s_SelAnchor.col;
}
else {
d_streamarg.flMin.lin = d_boxarg.flMin.lin;
d_streamarg.flMax.lin = d_boxarg.flMax.lin;
if( ( s_SelAnchor.lin == d_boxarg.flMin.lin
&& s_SelAnchor.col == d_boxarg.flMin.col
) ||
( s_SelAnchor.lin == d_boxarg.flMax.lin
&& (d_boxarg.flMax.col - s_SelAnchor.col == -1)
)
) {
d_streamarg.flMin.col = d_boxarg.flMin.col;
d_streamarg.flMax.col = d_boxarg.flMax.col + 1;
}
else {
d_streamarg.flMin.col = d_boxarg.flMax.col + 1;
d_streamarg.flMax.col = d_boxarg.flMin.col;
}
}
d_argType = STREAMARG;
}
void ARG::ConvertStreamargToLineargOrBoxarg() {
if( d_streamarg.flMin.col == d_streamarg.flMax.col ) {
d_argType = LINEARG;
d_linearg.yMin = d_streamarg.flMin.lin;
d_linearg.yMax = d_streamarg.flMax.lin;
return;
}
d_argType = BOXARG;
d_boxarg.flMin.lin = d_streamarg.flMin.lin;
d_boxarg.flMax.lin = d_streamarg.flMax.lin;
if( d_streamarg.flMin.col > d_streamarg.flMax.col ) {
d_boxarg.flMin.col = d_streamarg.flMax.col;
d_boxarg.flMax.col = d_streamarg.flMin.col - 1;
}
else {
d_boxarg.flMin.col = d_streamarg.flMin.col;
d_boxarg.flMax.col = d_streamarg.flMax.col - 1;
}
}
void TermNulleow( std::string &st ) {
for( auto it=st.begin()+1 ; it < st.end(); ++it ) {
if( !isWordChar( *it ) ) {
const auto idx( std::distance( st.begin(), it ) );
st.erase( idx );
break;
}
}
}
// NB: CursorFuncPeekSelnS and CursorFuncPeekSeln can both be CALLED using
// structured binding syntax (which is low-ceremony to write and understand)
// to collect return values.
// ref: see various implementations of ARG::longline
//
// BUT (calling) CursorFuncPeekSelnS codegen is smaller than
// (calling) CursorFuncPeekSeln codegen
// which appears to mean that returning a struct (subject to copy elision), even
// when that struct is "unpacked" via structured binding, is more efficient than
// returning a same-data tuple which is identically "unpacked" via structured
// binding (at the cost of introducing a new type (the function return struct
// type) into the global namespace; OTOH the new function return struct type is a
// single identifier, vs the tuple definition which is a complex aggregate
// declaration; repeating the latter in the function prototype and function
// definition (in different files) is noisier (I can't say "more error prone"
// because any mismatch in the tuple defs will cause a compile-time error)).
//
// The struct-return approach leverages of C++20-new 'designated initializers'
// to achieve self-documentation by naming each returned field.
//
// When possible returning a struct (leveraging RVO) is IMO preferable.
// Returning tuples instead (only?) makes sense when templates are in play (i.e.
// when types are not fixed), yet defining new struct types via template
// expansions is normal. So for example MinMax (template function) might define
// a return-value struct type as part of its (template) expansion, and the
// current example demonstrates that such a change would not preclude receiving
// the returned values via structured binding.
//
// 20220102
//
TCursorFuncPeekSeln CursorFuncPeekSelnS() { // intended use: selection-smart CURSORFUNC's
const auto Cursor( g_CurView()->Cursor() ); 0 && DBG( "Get_g_ArgCount = %d", Get_g_ArgCount() );
if( Get_g_ArgCount() > 0 ) {
const auto [xmin,xmax] = MinMax( s_SelAnchor.col, Cursor.col );
const auto [ymin,ymax] = MinMax( s_SelAnchor.lin, Cursor.lin );
return { .selnActive=true , .yMin=ymin , .yMax=ymax , .xMin=xmin , .xMax=xmax };
}
else {
return { .selnActive=false, .yMin=Cursor.lin, .yMax=Cursor.lin, .xMin=Cursor.col, .xMax=Cursor.col };
}
}
std::tuple<bool,LINE,LINE,COL,COL> CursorFuncPeekSeln() { // intended use: selection-smart CURSORFUNC's
const auto Cursor( g_CurView()->Cursor() ); 0 && DBG( "Get_g_ArgCount = %d", Get_g_ArgCount() );
if( Get_g_ArgCount() > 0 ) {
const auto [xmin,xmax] = MinMax( s_SelAnchor.col, Cursor.col );
const auto [ymin,ymax] = MinMax( s_SelAnchor.lin, Cursor.lin );
return std::make_tuple(true,ymin,ymax,xmin,xmax);
}
else {
return std::make_tuple(false,Cursor.lin,Cursor.lin,Cursor.col,Cursor.col);
}
}
stref View::GetWucOfSelection() {
if( this == g_CurView() ) {
const auto cursor( Cursor() );
if( Get_g_ArgCount() > 0
/* && s_SelAnchor.col != cursor.col */
/* && s_SelAnchor.lin == cursor.lin */
) { 0 && DBG("cur=%d,%d anchor=%d,%d",s_SelAnchor.lin,s_SelAnchor.col,cursor.lin,cursor.col);
const auto [xMin,xMax] = MinMax( s_SelAnchor.col, cursor.col );
auto sr( FBuf()->PeekRawLineSeg( cursor.lin, xMin, xMax-1 ) ); 0 && DBG("x:%d,%d '%" PR_BSR "'", xMin, xMax-1, BSR(sr) );
trim( sr );
return sr;
}
}
return stref();
}
bool ARG::BOXSTR_to_TEXTARG( LINE yOnly, COL xMin, COL xMax ) {
d_pFBuf->DupLineSeg( TextArgBuffer(), yOnly, xMin, xMax-1 );
d_argType = TEXTARG;
d_textarg.ulc.col = xMin;
d_textarg.ulc.lin = yOnly;
d_textarg.pText = TextArgBuffer().c_str(); 0 && DBG( "BOXSTR='%s'", d_textarg.pText );
return false;
}
STATIC_VAR bool s_fHaveLiteralTextarg;
// consumes g_ArgCount, s_fHaveLiteralTextarg
bool ARG::IngestArgTextAndSelection() { enum {SD=0}; SD && DBG( "%s+", __func__ );
// capture some global values into locals:
const auto fHaveLiteralTextarg( s_fHaveLiteralTextarg ); s_fHaveLiteralTextarg = false;
d_cArg = Get_g_ArgCount(); Clr_g_ArgCount();
d_pFBuf = g_CurFBuf();
const auto Cursor( g_CurView()->Cursor() );
if( d_cArg == 0 ) {
if( d_pCmd->d_argType & NOARGWUC ) {
auto start( Cursor );
const auto wuc( GetWordUnderPoint( d_pFBuf, &start ) );
if( !wuc.empty() ) {
d_argType = TEXTARG;
d_textarg.ulc = Cursor;
TextArgBuffer().assign( wuc );
d_textarg.pText = TextArgBuffer().c_str(); SD && DBG( "NOARGWUC='%s'", d_textarg.pText );
return false; //==================================================================
}
}
if( d_pCmd->d_argType & NOARG ) {
d_argType = NOARG;
d_noarg.cursor = Cursor; SD && DBG( "%s NOARG", __func__ );
return false; //=====================================================================
} SD && DBG( "%s !NOARG", __func__ );
return true; //=========================================================================
}
g_CurView()->MoveCursor_NoUpdtWUC( s_SelAnchor.lin, s_SelAnchor.col );
auto NumArg_value(0);
if( fHaveLiteralTextarg ) {
if( (d_pCmd->d_argType & NUMARG) && StrSpnSignedInt( TextArgBuffer().c_str() ) ) {
if( (NumArg_value = atoi( TextArgBuffer().c_str() )) ) {
s_SelAnchor.lin = std::max( 0, s_SelAnchor.lin + NumArg_value + (NumArg_value > 0) ? (-1) : (+1) );
}
}
else {
FBufLocn locn;
if( (d_pCmd->d_argType & MARKARG) && d_pFBuf->FindMark( TextArgBuffer().c_str(), &locn ) ) {
s_SelAnchor = locn.Pt(); SD && DBG( "FillArgStruct MarkFound '%s'", TextArgBuffer().c_str() );
}
else { // enum { SD=1 };
if( d_pCmd->d_argType & TEXTARG ) {
d_argType = TEXTARG;
d_textarg.ulc = Cursor;
d_textarg.pText = TextArgBuffer().c_str(); SD && DBG( "TEXTARG='%s'", d_textarg.pText );
return false; //===============================================================
} SD && DBG( "%s !TEXTARG", __func__ );
return true; //===================================================================
}
}
}
if( s_SelAnchor == Cursor && NumArg_value == 0 ) {
if( d_pCmd->d_argType & (NULLEOL | NULLEOW) ) {
d_pFBuf->DupLineSeg( TextArgBuffer(), s_SelAnchor.lin, s_SelAnchor.col, COL_MAX );
if( d_pCmd->d_argType & NULLEOW ) { TermNulleow( TextArgBuffer() ); }
d_argType = TEXTARG;
d_textarg.ulc = Cursor;
d_textarg.pText = TextArgBuffer().c_str(); SD && DBG( "NULLEO%c='%s'", (d_pCmd->d_argType & NULLEOW)?'W':'C', d_textarg.pText );
return false; //=====================================================================
}
if( d_pCmd->d_argType & NULLARG ) {
d_argType = NULLARG;
d_nullarg.cursor = Cursor; SD && DBG( "NULLARG" );
return false; //=====================================================================
} SD && DBG( "%s !NULLARG", __func__ );
return true; //=========================================================================
}
const auto [xMin,xMax] = MinMax( s_SelAnchor.col, Cursor.col );
const auto [yMin,yMax] = MinMax( s_SelAnchor.lin, Cursor.lin );
if( (d_pCmd->d_argType & BOXSTR) && s_SelAnchor.lin == Cursor.lin ) { SD && DBG( "%s BOXSTR_to_TEXTARG", __func__ );
return BOXSTR_to_TEXTARG( Cursor.lin, xMin, xMax ); //==================================
}
if( g_fBoxMode ) {
if( (d_pCmd->d_argType & LINEARG) && s_SelAnchor.col == Cursor.col ) { // no movement in X (COL) direction
d_argType = LINEARG;
d_linearg.yMin = yMin;
d_linearg.yMax = yMax; SD && DBG( "LINEARG [%d..%d]", d_linearg.yMin, d_linearg.yMax );
return false; //=====================================================================
}
if( (d_pCmd->d_argType & BOXARG) && s_SelAnchor.col != Cursor.col ) {
d_argType = BOXARG;
d_boxarg.flMin.col = xMin;
d_boxarg.flMin.lin = yMin;
d_boxarg.flMax.col = xMax - 1; // subtract out the offset that's used to differentiate a BOXARG from a LINEARG
d_boxarg.flMax.lin = yMax; SD && DBG( "BOXARG ulc=(%d,%d) lrc=(%d,%d)", d_boxarg.flMin.col, d_boxarg.flMin.lin, d_boxarg.flMax.col, d_boxarg.flMax.lin );
return false; //=====================================================================
} SD && DBG( "%s !SELARG: argType=%08X", __func__, d_pCmd->d_argType );
return true; //=========================================================================
}
if( d_pCmd->d_argType & STREAMARG ) {
//
// STREAM definition:
//
// [(d_streamarg.flMin.col,d_streamarg.flMin.lin)..(d_streamarg.flMax.col,d_streamarg.flMax.lin))
//
// In English: a stream of text from start (inclusive) to end (NOT
// inclusive); THE LAST CHARACTER IS NOT INCLUDED
//
// This is the "stream" definition used by API's like DelStream and CopyStream
//
d_argType = STREAMARG;
const auto fFwdSel( s_SelAnchor < Cursor );
if( fFwdSel ) {
d_streamarg.flMin = s_SelAnchor;
d_streamarg.flMax = Cursor ;
}
else {
d_streamarg.flMin = Cursor ;
d_streamarg.flMax = s_SelAnchor;
} SD && DBG( "stream (%d,%d), (%d,%d)", d_streamarg.flMin.lin, d_streamarg.flMin.col, d_streamarg.flMax.lin, d_streamarg.flMax.col );
return false; //========================================================================
} SD && DBG( "%s !ARG match", __func__ );
return true; //============================================================================
}
// trims leading and trailing blanks of each contributing line, ensures lines' contrib sare joined by ONE blank
std::string StreamArgToString( PFBUF pfb, Rect stream ) {
std::string dest;
const auto yMax( std::min( pfb->LastLine(), stream.flMax.lin ) );
if( stream.flMin.lin > yMax ) {
return dest;
}
auto append_dest = [&dest]( stref src ) {
src.remove_prefix( FirstNonBlankOrEnd( src ) );
if( !src.empty() ) {
if( !dest.empty() ) { dest.append( " " ); }
dest.append( src ); // <-- could be replaced by multi-blank-eater
rmv_trail_blanks( dest );
}
};
if( stream.flMin.lin == yMax ) {
append_dest( pfb->PeekRawLineSeg( stream.flMin.lin, stream.flMin.col, stream.flMax.col-1 ) );
}
else {
auto yLine( stream.flMin.lin );
append_dest( pfb->PeekRawLineSeg( yLine, stream.flMin.col, COL_MAX ) );
for( ++yLine ; yLine < yMax ; ++yLine ) {
append_dest( pfb->PeekRawLine( yLine ) );
}
append_dest( pfb->PeekRawLineSeg( yLine, 0, stream.flMax.col-1 ) );
}
return dest;
}
#ifdef fn_stream
bool ARG::stream() { // test for StreamArgToString
auto cArg(0); // stream:alt+k
switch( d_argType ) {
break;default: return BadArg();
break;case STREAMARG: {
const auto ststr( StreamArgToString( g_CurFBuf(), d_streamarg ) );
DBG( "Stream:%s|", ststr.c_str() );
}
}
return true;
}
#endif
//-------------------------------
STATIC_FXN bool ConsumeMeta() {
const auto fMetaWas( g_fMeta );
g_fMeta = false;
if( fMetaWas != g_fMeta ) { DispNeedsRedrawStatLn(); }
return fMetaWas;
}
bool ARG::meta() {
g_fMeta = !g_fMeta;
DispNeedsRedrawStatLn();
return g_fMeta;
}
bool ARG::InitOk( PCCMD pCmd ) {
d_pCmd = pCmd;
d_fMeta = (d_pCmd->d_argType & KEEPMETA) ? false : ConsumeMeta();
d_cArg = 0;
d_argType = NOARG;
d_noarg.cursor = g_CurView()->Cursor();
if( d_pCmd->d_argType & TAKES_ARG ) { // arg, meta, CURSORFUNC's will FAIL this test
if( IngestArgTextAndSelection() ) {
ClearArgAndSelection();
return ErrorDialogBeepf( "Bad argument: '%s' requires %s", CmdName(), ArgTypeNames( d_pCmd->d_argType ).c_str() );
}
ClearArgAndSelection();
}
return true;
}
bool ARG::Invoke() { 0 && DBG( "%s %s", FUNC, CmdName() );
d_pCmd->IncrCallCount();
// most of what follows is monitoring activity, not functionality-related...
constexpr auto MONITOR_INVOCATION( 0 && DEBUG_LOGGING );
STATIC_VAR int s_nestLevel;
if( MONITOR_INVOCATION ) {
++s_nestLevel;
constexpr int NEST_CHARS = 6;
const auto ixEos( s_nestLevel * NEST_CHARS );
if( 0 && g_fLogcmds && !d_pCmd->isCursorOrWindowFunc() && ixEos < sizeof(linebuf)-1 ) {
linebuf lbuf;
for( auto ix=0 ; ix < ixEos ; ++ix ) { lbuf[ix] = '>'; }
lbuf[ ixEos ] = '\0'; DBG( "%s %-15s", lbuf, CmdName() );
}
}
g_CurFBuf()->UndoInsertCmdAnnotation( d_pCmd );
const auto rv( CALL_METHOD( *this, d_pCmd->d_func )() );
MONITOR_INVOCATION && --s_nestLevel;
return rv;
}
STATIC_VAR ARG s_RepeatArg;
void ARG::SaveForRepeat() const {
//-------- save new repeat-arg information --------
// (basically, this is the assignment operator for ARG)
if( s_RepeatArg.d_argType == TEXTARG ) {
Free0( s_RepeatArg.d_textarg.pText );
}
s_RepeatArg = *this;
if( d_argType == TEXTARG ) {
s_RepeatArg.d_textarg.pText = Strdup( d_textarg.pText );
}
}
bool ARG::repeat() {
return s_RepeatArg.d_pCmd ? s_RepeatArg.Invoke() : fnMsg( "no command to repeat" );
}
bool CMD::BuildExecute() const { 0 && DBG( "%s+ %s", __func__, Name() );
if( (d_argType & MODIFIES) && g_CurFBuf()->CantModify() ) {
ClearArgAndSelection();
return false;
}
ARG argStruct;
if( !argStruct.InitOk( this ) ) {
return false;
}
if( IsCmdXeqInhibitedByRecord() && d_func != fn_record ) {
return false;
}
if( d_func != fn_repeat && !Interpreter::Interpreting() ) {
argStruct.SaveForRepeat();
}
return argStruct.Invoke();
}
// local class that displays the prompt for GetTextargString, so this function can
// be embedded in the code that obtains the next CMD (a child of CMD_reader),
// but only if necessary (ie. if the USER actually needs to hit some keys).
//
class EditPrompt {
const PCChar d_pszPrompt;
const PCChar d_pszEditText;
const COL d_xCursor;
const int d_colorAttribute;
public:
EditPrompt( PCChar pszPrompt, PCChar pszEditText, int colorAttribute, COL xCursor )
: d_pszPrompt(pszPrompt)
, d_pszEditText(pszEditText)
, d_xCursor(xCursor)
, d_colorAttribute(colorAttribute)
{ 0 && DBG( "%p %s: '%s'", this, __func__, d_pszPrompt );
}
void Write() const;
void UnWrite() const { CursorLocnOutsideView_Unset(); }
};
void EditPrompt::Write() const { 0 && DBG( "%p %s: '%s'", this, __func__, d_pszPrompt );
const auto promptLen( Strlen( d_pszPrompt ) );
auto oEditText( std::max( 0, d_xCursor - EditScreenCols() + promptLen + 1 ) );
auto editTextLen( Strlen( d_pszEditText ) );
if( oEditText > 0 ) {
oEditText -= oEditText % g_iHscroll;
oEditText += g_iHscroll;
editTextLen -= oEditText;
}
{
const auto editTextShown( std::min( EditScreenCols() - promptLen, editTextLen ) );
VideoFlusher vf;
VidWrStrColor( DialogLine(), 0 , d_pszPrompt , promptLen , d_colorAttribute, ePad::noPad );
VidWrStrColor( DialogLine(), promptLen, d_pszEditText+oEditText, editTextShown, g_colorStatus , ePad::noPad );
if( promptLen + editTextShown < EditScreenCols() ) {
VidWrStrColor( DialogLine(), promptLen+editTextShown, " " , 1 , d_colorAttribute, ePad::padWSpcsToEol );
}
}
CursorLocnOutsideView_Set( DialogLine(), d_xCursor - oEditText + promptLen );
}
class GetTextargString_CMD_reader : public CMD_reader
{
const EditPrompt &d_ep;
protected:
void VWritePrompt() override { d_ep.Write (); }
void VUnWritePrompt() override { d_ep.UnWrite(); }
public:
GetTextargString_CMD_reader( const EditPrompt &ep ) : d_ep( ep ) {}
PCCMD GetNextCMD( bool fGetKbInput ); // OVERRIDE parent-class method
};
PCCMD GetTextargString_CMD_reader::GetNextCMD( bool fKbInputOnly ) {
if( fKbInputOnly ) {
VWritePrompt();
d_fAnyInputFromKbd = true;
const auto rv( CmdFromKbdForExec() );
VUnWritePrompt();
return rv;
}
else { // VWritePrompt() called internal to GetNextCMD_ExpandAnyMacros if needed
return GetNextCMD_ExpandAnyMacros( eOnMacHalt::Continue );
}
}
STATIC_FXN void Bell_FlushKeyQueue_WaitForKey() {
ConOut::Bell();
ConIn::FlushKeyQueueAnythingFlushed();
WaitForKey( 1 );
}
GTS::eRV GTS::begline() {
xCursor_ = 0;
return KEEP_GOING;
}
GTS::eRV GTS::home() { return begline(); }
GTS::eRV GTS::up() {
if( textargStackPos_ < 0 ) {
AddToTextargStack( stb_ );
textargStackPos_ = 0;
}
if( textargStackPos_ < g_pFBufTextargStack->LastLine() ) {
g_pFBufTextargStack->DupRawLine( stb_, ++textargStackPos_ );
xCursor_ = stb_.length();
}
return KEEP_GOING;
}
GTS::eRV GTS::down() {
if( textargStackPos_ > 0 ) {
g_pFBufTextargStack->DupRawLine( stb_, --textargStackPos_ );
xCursor_ = stb_.length();
}
return KEEP_GOING;
}
GTS::eRV GTS::emacscdel() { // dup of cdelete
if( xCursor_ > 0 ) {
--xCursor_;
if( xCursor_ < stb_.length() ) {
stb_.erase( xCursor_, 1 );
}
}
return KEEP_GOING;
}
GTS::eRV GTS::cdelete() { return emacscdel(); }
GTS::eRV GTS::emacsnewl() { // dup of newline
if( flags_ & gts_OnlyNewlAffirms ) {
return DONE;
}
ConOut::Bell();
return KEEP_GOING;
}
GTS::eRV GTS::newline() { return emacsnewl(); }
GTS::eRV GTS::endline() {
xCursor_ = stb_.length(); // past end
return KEEP_GOING;
}
GTS::eRV GTS::left() {
if( xCursor_ > 0 ) {
--xCursor_;
}
return KEEP_GOING;
}
GTS::eRV GTS::mword() {
const auto pb( stb_.c_str() ); const auto len( stb_.length() );
if( xCursor_ >= len ) {
xCursor_ = len - 1;
}
while( xCursor_ > 0 ) {
if( --xCursor_ == 0 ) {
break;
}
if( !isWordChar( pb[xCursor_-1] ) && isWordChar( pb[xCursor_] ) ) {
break;
}
}
return KEEP_GOING;
}
GTS::eRV GTS::pword() {
const auto pb( stb_.c_str() ); const auto len( stb_.length() );
while( xCursor_ < len ) {
++xCursor_;
if( !isWordChar( pb[xCursor_] ) && isWordChar( pb[xCursor_+1] ) ) {
++xCursor_;
break;
}
}
return KEEP_GOING;
}
GTS::eRV GTS::arg() {
if( xCursor_ < stb_.length() ) {
stb_.erase( xCursor_ ); // delete all chars at or following (under or to the right of) the cursor
}
return KEEP_GOING;
}
GTS::eRV GTS::restcur() {
// assumes restcur remains assigned to alt+center!!!
// alt+center=alg+arg: does the converse of arg:
if( xCursor_ < stb_.length() ) {
stb_.erase( 0, xCursor_ ); // delete all chars preceding (to the left of) the cursor
xCursor_ = 0;
}
return KEEP_GOING;
}
GTS::eRV GTS::right() { 0 && DBG( "%s: %d, %" PR_SIZET, __func__, xCursor_, stb_.length() );
if( g_CurFBuf() && stb_.length() == xCursor_ ) {
const auto xx( xColInFile_ + xCursor_ );
std::string stTmp;
g_CurFBuf()->DupLineSeg( stTmp, g_CursorLine(), xx, xx ); 0 && DBG( "%d='%" PR_BSR "'", xx, BSR(stTmp) );
if( !stTmp.empty() ) {
stb_.push_back( stTmp[0] );
}
}
++xCursor_;
return KEEP_GOING;
}
GTS::eRV GTS::delete_() {
if( xCursor_ < stb_.length() ) {
stb_.erase( xCursor_, 1 );
}
return KEEP_GOING;
}
GTS::eRV GTS::sdelete() { return delete_(); }
GTS::eRV GTS::insert() {
stb_.insert( xCursor_, 1, ' ' );
return KEEP_GOING;
}
GTS::eRV GTS::sinsert() { return insert(); }
GTS::eRV GTS::flipcase() {
if( xCursor_ < stb_.length() ) {
stb_[ xCursor_ ] = FlipCase( stb_[ xCursor_ ] );
}
return KEEP_GOING;
}
GTS::eRV GTS::meta() {
noargNoMeta.meta();
return KEEP_GOING;
}
GTS::eRV GTS::cancel() {
return DONE;
}
GTS::eRV GTS::graphic() { // !!! called by macro_graphic !!!
if( fInitialStringSelected_ ) {
xCursor_ = 0;
if( xCursor_ < stb_.length() ) {
stb_.erase( xCursor_ );
}
} 0 && DBG( "graphic @ x=%d (stlen=%" PR_SIZET ")", xCursor_, stb_.length() );
if( xCursor_ > stb_.length() ) { 0 && DBG( "append %" PR_SIZET " spaces", xCursor_ - stb_.length() );
stb_.append( xCursor_ - stb_.length(), ' ' );
}
const auto ch( pCmd_->d_argData.chAscii() );
stb_.insert( xCursor_++, 1, ch );
return KEEP_GOING;
}
#ifdef fn_dispmstk
GTS::eRV GTS::dispmstk() {
noargNoMeta.dispmstk();
return KEEP_GOING;
}
#endif
// BUGBUG
// GTS::eRV GTS::up() {
// else if( pCmd_->NameMatch( "swapchar" ) ) {
// if( xCursor_+1 < stb_.length() ) {
// std::swap( stb_[xCursor_+0], stb_[xCursor_+1] );
// }
// return KEEP_GOING;
// }
class TabCompletion_filesystem {
bool d_fBellAndFreezeKbInput = false;
std::unique_ptr<DirMatches> d_pDirContent;
std::string d_pbTabxBase;
public:
void Deactivate() {
delete d_pDirContent.release();
d_pbTabxBase.clear(); // forget prev used WC
}
void GetNext( std::string &stb, COL &xCursor ) {
if( !d_pDirContent ) {
if( d_pbTabxBase.empty() ) { // no prev'ly used WC?
d_pbTabxBase = stb; // create based on curr content
}
d_pDirContent = std::make_unique<DirMatches>( d_pbTabxBase.c_str(), HasWildcard( d_pbTabxBase ) ? nullptr : "*", FILES_AND_DIRS, false );
}
Path::str_t nxt;
do {
nxt = d_pDirContent->GetNext();
} while( Path::IsDotOrDotDot( nxt ) );
if( !nxt.empty() ) {
stb = nxt;
xCursor = stb.length(); // past end
}
else {
delete d_pDirContent.release();
stb = d_pbTabxBase;
xCursor = ixFirstWildcardOrEos( stb ); // show user seed in case he wants to edit or iterate again thru WC expansion loop
d_fBellAndFreezeKbInput = true;
}
}
void BellAndFreezeKbInputIfExhausted() {
// BUGBUG GetTextargString_CMD_reader may prevent the following
// d_fBellAndFreezeKbInput code from achieving it's intended task
//
if( d_fBellAndFreezeKbInput ) {
// goal is to freeze the KB input so if user holds tab down, he
// can easily get back to the display of d_pbTabxBase.
//
// d_fBellAndFreezeKbInput exists because where d_fBellAndFreezeKbInput is
// set is temporarily prior to RefreshPromptAndEditInput being called;
// freezing there shows the old dialog line content, which is not
// useful; instead defer the freeze to here, when the latest dialog
// line content has actually been displayed.
//
d_fBellAndFreezeKbInput = false;
Bell_FlushKeyQueue_WaitForKey();
}
}
};
STATIC_FXN PCCMD GetTextargString_( std::string &stb, PCChar pszPrompt, int xCursor, PCCMD pCmd, int flags, bool *pfGotAnyInputFromKbd ) {
// pCmd if valid (currently only when we're called by ArgMainLoop) will be ARG::graphic, the first char of a typed arg.
enum { DBG_GTA=1 }; DBG_GTA && DBG( "+%s CMD='%s' dest='%s' flags=%X prompt='%s'", __func__, pCmd?pCmd->Name():"(none)", stb.c_str(), flags, pszPrompt?pszPrompt:"" );
const auto fSavedMeta( g_fMeta ); 0 && DBG( "%s+ g_fMeta=%d, fSavedMeta=%d", __func__, g_fMeta, fSavedMeta );
const auto xColInFile( pCmd ? s_SelAnchor.col : g_CursorCol() ); 0 && DBG( "%s+ xColInFile=%d (%d : %d)", __func__, xColInFile, s_SelAnchor.col, g_CursorCol() );
*pfGotAnyInputFromKbd = false;
auto textargStackPos( -1 );
TabCompletion_filesystem tcf;
while(1) { //******************************************************************
tcf.BellAndFreezeKbInputIfExhausted();
const auto fInitialStringSelected( ToBOOL(flags & gts_DfltResponse) );
if( !pCmd ) {
EditPrompt ep( pszPrompt, stb.c_str(), fInitialStringSelected ? g_colorError : g_colorInfo, xCursor );
GetTextargString_CMD_reader gtas( ep );
pCmd = gtas.GetNextCMD( ToBOOL(flags & gts_fKbInputOnly) );
if( !pCmd ) {
break;
}
if( gtas.GotAnyInputFromKbd() ) {
*pfGotAnyInputFromKbd = true;
}
}
// process pCmd
if( pCmd->d_argData.eka.EdKcEnum == EdKC_tab ) { // 20100222 hack: look at EdKcEnum since new tab key assignment is to a Lua function
tcf.GetNext( stb, xCursor ); // tab-handling
}
else {
tcf.Deactivate();
if( pCmd->d_GTS_fxn ) {
GTS gts = { // yes, this statement relies on GCC-only features <sigh>
.xCursor_ = xCursor ,
.stb_ = stb ,
.textargStackPos_ = textargStackPos,
//-------------------------------- ref/const-value boundary
.pCmd_ = pCmd ,
.xColInFile_ = xColInFile ,
.flags_ = flags ,
.fInitialStringSelected_ = fInitialStringSelected,
};
const auto rv( CALL_METHOD( gts, pCmd->d_GTS_fxn )() );
if( rv == GTS::DONE ) {
break;
}
}
else {
if( !pCmd->isCursorFunc() && !(flags & gts_OnlyNewlAffirms) ) {
break;
}
ConOut::Bell();
}
}
// Some editing or cursor movement was done and we will be continuing to edit.
// Consume meta + pCmd
if( !(pCmd->d_argType & KEEPMETA) ) {
g_fMeta = false;
}
pCmd = nullptr;
flags &= ~gts_DfltResponse;
} /*** while ***/ DBG_GTA && DBG( "-%s CMD='%s' arg='%s'" , __func__ , pCmd?pCmd->Name():"" , stb.c_str() );
g_fMeta = fSavedMeta;
if( *pfGotAnyInputFromKbd ) {