-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvx_edit.c
1310 lines (1232 loc) · 31.5 KB
/
tvx_edit.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
/* ---------------------------------------------------------------------
This module contains the main edit routine, which is
different for each editor being emulated. The code that is unique
to each emulation is confined to the three .ic files, tvx_lex.c, and
this module. The remainder of the tvx files are the same for all versions.
This version of TVX Copyright (c) 1986 by Bruce E. Wampler
Permission is hereby granted for free, unrestricted nonprofit
use of this software. Please feel free to modify, distribute,
and share this software as long as you aren't making any money
from it. If you want to use this code in a profit making environment,
please contact the author for permission.
--------------------------------------------------------------------- */
#include "tvx_defs.ic" /* note tv_defs.ic will #include stdio.h */
#include "tvx_glbl.ic"
char clower(),cupper();
#ifdef TVX_CMDSET /* works for both tvx_em and tvx0m_em */
/* =============================>>> EDIT <<<============================= */
edit()
{ /* edit - main editing routine */
SLOW int lexval,lexcnt,succ, lastln, itmp;
SLOW int noteloc[10], ni, lex_def;
SLOW char lexchr;
static int ins_set[] = /* allowable commands for insert */
{
VINSERT, VOPENLINE, VQUIT, VABORT, VFBEGIN, VGET, VYANK, 0
};
static int jump_set[] = /* commands to not reset jump memory */
{
VJUMP, VMEMORY, VHELP, VNOTELOC, VPRINTS, 0
};
SLOW char fchr; /* temp char for prefixed commands */
startm();
remark("Reading file...");
rdpage(); /* read a page into the buffer */
tvclr(); /* clear the screen */
if (curlin >= 1)
tvtype(curlin,tvlins); /* type out lines */
tvxy(1,1); /* and rehome the cursor */
waserr = FALSE; /* no errors to erase yet */
if (curlin<1)
tverr("Buffer empty");
#ifdef SCR_BUF
ttflush();
#endif
lexval = UNKNOWN; /* so can remember 1st time through */
useprint = FALSE; /* not to printer */
succ=TRUE; /* assume success initially */
lastln = 1 ; /* remember where we were */
for (ni = 0 ; ni < 10 ; noteloc[ni++] = 1)
; /* init noteloc */
do
{
oldlex = lexval; /* remember last command */
if (! succ)
echof = TRUE; /* resume echo when error */
lex_def = lex(&lexval,&lexcnt,&lexchr,succ); /* get command input */
if (waserr)
fixend();
waserr=FALSE;
succ=TRUE;
if (lexval == UNKNOWN)
{
cmderr(lexchr);
succ = FALSE; /* announce failure to lex */
}
else
{
if (curlin < 1) /* make sure legal command for empty buffer */
{
if (!inset(lexval,ins_set))
{
tverrb("Can't, buffer empty. Insert 1st ");
succ=FALSE;
continue;
}
}
if (!inset(lexval,jump_set))
lastln=curlin; /* let user look at help w/o changing */
switch (lexval)
{
case 1: /* right */
right(lexcnt);
break;
case 2: /* left */
right(-lexcnt);
break;
case 3: /* down line */
dwnlin(lexcnt);
break;
case 4: /* up line */
dwnlin(-lexcnt);
break;
case 5: /* down in column */
dwncol(lexcnt);
break;
case 6: /* up in column */
dwncol(-lexcnt);
break;
case 7: /* delete last character */
succ = delnxt(-lexcnt);
break;
case 8: /* delete next character */
succ = delnxt(lexcnt);
break;
case 9: /* insert */
succ = insert(lexcnt,lex_def);
break;
case 10: /* kill a line */
killin(lexcnt);
break;
case 11: /* kill rest of line */
krest();
break;
case 12: /* kill previous part of line */
kprev();
break;
case 13: /* move to beginning of line */
beglin();
break;
case 14: /* move to end of the line */
endlin();
break;
case 15: /* search for a pattern */
succ = search(lexcnt,TRUE);
break;
case 16: /* search for next part of a pattern */
succ = snext(lexcnt,TRUE);
break;
case 17: /* flip screen */
dwnlin(min(lexcnt*tvlins,nxtlin-curlin+1));
break;
case 18: /* goto top of page */
toppag();
break;
case 19: /* goto to bottom of page */
botpag();
break;
case 20: /* goto real beginning of the file */
succ = fbeg();
break;
case 21: /* verify */
verify(lexcnt);
break;
case 22: /* open new line */
openln(lexcnt);
#ifdef TVX_EM /* don't need insert for modeless tvx */
succ = insert(1,TRUE); /* go into insert mode, insert mode */
#endif
break;
case 23: /* delete last thing manipulated */
succ = rmvlst();
break;
case 24: /* save lines in move buffer */
succ = save(lexcnt,FALSE);
break;
case 25: /* get move buffer */
succ = getsav();
break;
case 26: /* read in next page of file */
wtpage(lexcnt); /* write out the current page */
succ = rdpage(); /* read in the next */
tvclr();
if (succ || lexcnt < 0)
verify(1);
break;
case 27: /* append external file to save buffer */
succ = addfil(lexcnt);
break;
case 28: /* quit */
tvclr();
remark("Exit");
goto lquit;
case 29: /* search again */
succ = search(lexcnt,FALSE); /* FALSE => don't read search string */
break;
case 30: /* execute repeat buffer again */
if (lexcnt != 1)
echof=FALSE; /* turn off echo */
rptcnt[rptuse] = lexcnt > 0 ? lexcnt : (-lexcnt);
break;
case 31: /* print memory status, etc. */
memory();
break;
case 32: /* change a parameter */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
setpar(lexcnt,fchr);
break;
case 33: /* remove last and enter insert mode */
if ((succ = rmvlst()))
succ = insert(1,TRUE);
break;
case 34: /* unkill last line killed */
succ = unkill();
break;
case 35: /* jump over a word */
wordr(lexcnt);
break;
case 36: /* neg jump over word */
wordr(-lexcnt);
break;
case 37: /* append to save buffer */
succ = save(lexcnt,TRUE);
break;
case 38: /* print screen */
scrprint();
break;
case 39: /* show repeat buffer + help*/
shoset();
break;
case 40: /* flip screen half page */
dwnlin( min((lexcnt*tvlins)/2 , nxtlin-curlin+1) );
break;
case 41: /* abort */
abort();
break;
case 42: /* change characters */
if ((succ = delnxt(lexcnt)))
succ = insert(1,TRUE);
break;
case 43: /* jump back to last location */
itmp = curlin;
curlin = lastln;
curchr = *(lines+curlin)+1; /* point to the current character */
verify(1);
lastln = itmp;
break;
case 44: /* tidy up screen */
succ = neaten(lexcnt);
break;
case 45: /* save current location */
if (lexcnt < 1 || lexcnt > 9)
lexcnt = 0;
noteloc[lexcnt] = curlin;
break;
case 46: /* return to noted location */
itmp = curlin;
if (lexcnt < 1 || lexcnt > 9)
lexcnt = 0;
if (noteloc[lexcnt] >= nxtlin)
{
tverrb("Line no longer there ");
noteloc[lexcnt] = curlin;
}
else
{
curlin = noteloc[lexcnt];
curchr = *(lines+curlin)+1; /* point to the current character */
verify(1);
lastln = itmp;
}
break;
case 47:
opsystem(); /* call operating system */
break;
case 48:
if (lex_def) /* default 1 passed */
lexcnt = rptuse + 1; /* use current repeat loop */
succ = edit_rpt(lexcnt); /* edit repeat buffer */
break;
case 49:
succ = store_rpt(lexcnt); /* store repeat buffer */
break;
case 50:
succ = exec_rpt(lexcnt); /* execute repeat buffer */
break;
case 51:
succ = ins_pat(lexcnt);
break;
case 52:
succ = user_1(lexcnt); /* user function 1 */
break;
case 53:
succ = user_2(lexcnt); /* user function 2 */
break;
case 54: /* '~': change case */
foldcase(lexcnt);
break;
} /* end of switch */
continue; /* next iteration of do loop */
} /* end of else */
#ifdef SCR_BUF
ttflush();
#endif
} /* end of do loop */
while (1);
lquit:
for ( wtpage(1) ; rdpage() ; wtpage(1) ) /* write whole file */
;
tvclr();
}
#endif
#ifdef VI_EM /* vi emulation */
/* =============================>>> EDIT <<<============================= */
edit()
{ /* edit - main editing routine */
SLOW int lexval,lexcnt,succ, lastln, itmp;
SLOW int noteloc[10], ni, lex_def;
SLOW char lexchr;
static int ins_set[] = /* MUCH more limited than tvx */
{
VINSERT, VENDZ, VTVX, VNOOP, 0
};
static int jump_set[] = /* commands to not reset jump memory */
{
VTVX, VMEMORY, VHELP, VNOTELOC, VNOOP, 0
};
SLOW char fchr; /* temp char for prefixed commands */
static int dir_up = FALSE; /* search down by default */
startm();
remark("Reading file...");
rdpage(); /* read a page into the buffer */
tvclr(); /* clear the screen */
if (curlin >= 1)
tvtype(curlin,tvlins); /* type out lines */
tvxy(1,1); /* and rehome the cursor */
waserr = FALSE; /* no errors to erase yet */
if (curlin<1)
tverr("Buffer empty");
#ifdef SCR_BUF
ttflush();
#endif
lexval = UNKNOWN; /* so can remember 1st time through */
useprint = FALSE; /* not to printer */
succ=TRUE; /* assume success initially */
lastln = 1 ; /* remember where we were */
for (ni = 0 ; ni < 10 ; noteloc[ni++] = 1)
; /* init noteloc */
do
{
oldlex = lexval; /* remember last command */
if (! succ)
echof = TRUE; /* resume echo when error */
lex_def = lex(&lexval,&lexcnt,&lexchr,succ); /* get command input */
if (waserr)
fixend();
waserr=FALSE;
succ=TRUE;
if (lexval == UNKNOWN)
{
cmderr(lexchr);
succ = FALSE; /* announce failure to lex */
}
else
{
if (curlin < 1) /* make sure legal command for empty buffer */
{
if (!inset(lexval,ins_set))
{
tverrb("Can't, buffer empty. Insert 1st ");
succ=FALSE;
continue;
}
}
if (!inset(lexval,jump_set))
lastln=curlin; /* let user look at help w/o changing */
/* these all started out in order, but... */
switch (lexval)
{
case 1: /* ^B: screen up */
dwnlin(min(-lexcnt*tvlins,nxtlin-curlin+1));
break;
case 2: /* ^D: half screen */
dwnlin( min((lexcnt*tvlins)/2 , nxtlin-curlin+1) );
break;
case 3: /* ^F: screen down */
dwnlin(min(lexcnt*tvlins,nxtlin-curlin+1));
break;
case 4: /* ^G: memory status */
memory();
break;
case 5: /* ^H: left (must be here to avoid
conflict with normal char del */
right(-lexcnt);
break;
case 6: /* ^L: verify screen */
verify(lexcnt);
break;
case 7: /* '=': help */
shoset();
break;
case 8: /* '!' - tidy */
succ = neaten(lexcnt);
break;
case 9: /* '#': execute macro n times */
succ = exec_rpt(lexcnt); /* execute repeat buffer */
break;
case 10: /* '$': end of current line */
endlin();
break;
case 11: /* '*': insert last found pattern */
succ = ins_pat(lexcnt);
break;
case 12: /* '/': search down */
dir_up = FALSE;
succ = search(1,TRUE); /* searching down*/
break;
case 13: /* ':': set parameter, just like tvx */
tverrb("Use ZZ to exit ");
break;
case 14: /* 'J': join lines */
endlin();
delnxt(lexcnt);
succ = insert((int) ' ',FALSE);
break;
case 15: /* '?': find upwards */
dir_up = TRUE;
succ = search(-1,TRUE);
break;
case 16: /* '@': execute current repeat loop */
if (lexcnt != 1)
echof=FALSE; /* turn off echo */
rptcnt[rptuse] = lexcnt > 0 ? lexcnt : (-lexcnt);
break;
case 17: /* 'A': append to end of line {.i} */
endlin();
succ = insert(1,TRUE); /* go into insert mode, insert mode */
break;
case 18: /* 'C': changes rest of line {"i } */
krest(); /* kill rest of line */
succ = insert(1,TRUE); /* enter insert mode */
break;
case 19: /* 'D': delete rest of the line {"} */
krest(); /* kill rest of line */
break;
case 20: /* 'G': goes to line number n, or
end of buffer if no n */
if (lex_def) /* no n supplied */
botpag();
else
{
toppag(); /* go to top of file */
dwnlin(lexcnt - 1); /* go to that line */
}
break;
case 21: /* 'H': Beginning of buffer */
toppag(); /* go to top of buffer */
break;
case 22: /* 'I': inserts a beginning of line */
beglin();
succ = insert(1,TRUE); /* go into insert mode */
break;
case 23: /* 'J': like vi j - down in column */
dwncol(lexcnt);
break;
case 24: /* 'K': like vi k - up in column */
dwncol(-lexcnt);
break;
case 25: /* 'L': bottom line of file */
botpag();
beglin();
break;
case 26: /* 'M': return to marked location */
itmp = curlin;
if (lexcnt < 1 || lexcnt > 9)
lexcnt = 0;
if (noteloc[lexcnt] >= nxtlin)
{
tverrb("Line no longer there ");
noteloc[lexcnt] = curlin;
}
else
{
curlin = noteloc[lexcnt];
curchr = *(lines+curlin)+1; /* point to the current character */
verify(1);
lastln = itmp;
}
break;
case 27: /* 'N': reverse find again */
succ = search((dir_up ? 1 : -1),FALSE);
/* FALSE => don't read search string */
break;
case 28: /* 'O': open a line above current line */
beglin();
openln(lexcnt);
succ = insert(1,TRUE); /* go into insert mode, insert mode */
break;
case 29: /* 'P': put save buffer above current line */
beglin();
succ = getsav();
break;
case 30: /* 'T': tvx commands */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
fchr = clower(fchr); /* lower case */
if (curlin < 1) /* make sure legal command for empty buffer */
{
if ( fchr != 'b')
{
tverrb("Can't, buffer empty. Insert 1st ");
succ=FALSE;
break;
}
}
switch (fchr)
{
case ':': /* set parameter */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
setpar(lexcnt,fchr);
break;
case '!':
opsystem(); /* call operating system */
break;
case 'b': /* goto real beginning of the file */
succ = fbeg();
break;
case 'e': /* edit repeat buffer */
if (lex_def) /* default 1 passed */
lexcnt = rptuse + 1; /* use current repeat loop */
succ = edit_rpt(lexcnt); /* edit repeat buffer */
break;
case 'j': /* jump back to last location */
itmp = curlin;
curlin = lastln;
curchr = *(lines+curlin)+1; /* point to the current character */
verify(1);
lastln = itmp;
break;
case 'p': /* put external file from save buffer */
succ = addfil(-1);
break;
case 'r': /* restore repeat buffer */
succ = store_rpt(lexcnt); /* store repeat buffer */
break;
case 's': /* print screen */
scrprint();
break;
case 'u': /* "undo" */
succ = unkill();
break;
case 'w': /* read in next page of file */
wtpage(lexcnt); /* write out the current page */
succ = rdpage(); /* read in the next */
tvclr();
if (succ || lexcnt < 0)
verify(1);
break;
case 'y': /* yank external file to save buffer */
succ = addfil(1);
break;
case '/': /* search across buffers */
succ = snext(1,TRUE);
break;
case '(': /* user 1 */
succ = user_1(lexcnt); /* user function 1 */
break;
default:
tverrb("Use !,b,e,j,p,r,s,u,w,y, or / with q ");
break;
}
break;
case 31: /* 'X': delete character before cursor */
succ = delnxt(-lexcnt);
break;
case 32: /* 'Y': append to save buffer */
succ = save(lexcnt,TRUE);
break;
case 33: /* 'Z': exit (ZZ: normal, ZA: abort) */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
switch (clower(fchr))
{
case 'z': /* normal exit */
tvclr();
remark("Exit");
goto lquit;
case 'a': /* abort or terminate exit */
abort();
break;
case 27: /* escape is no op */
break;
default:
tverrb("Use Z or A with Z");
break;
}
break;
case 34: /* '^': beginning of line */
beglin();
break;
case 35: /* 'a': append text */
right(1);
succ = insert(lexcnt,lex_def);
break;
case 36: /* 'b': word left */
wordr(-lexcnt);
break;
case 37: /* 'c': change something */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
switch (clower(fchr))
{
case 'c': /* change line */
killin(lexcnt); /* remove the line */
openln(1);
succ = insert(1,TRUE); /* go into insert mode */
break;
case ' ': /* change one character */
delnxt(lexcnt);
succ = insert(1,TRUE); /* go into insert mode */
break;
case '^': /* to beginning of line */
kprev();
succ = insert(1,TRUE); /* go into insert mode */
break;
case '$': /* to beginning of line */
krest();
succ = insert(1,TRUE); /* go into insert mode */
break;
case '/': /* delete last thing, enter insert */
if ((succ = rmvlst()))
succ = insert(1,TRUE);
break;
case 27: /* escape is no op */
break;
default:
tverrb("Use c,<sp>,^,$, or / with c ");
break;
}
break;
case 38: /* 'd': delete d, <sp>, ^, or $ */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
switch (clower(fchr))
{
case 'd': /* delete line */
killin(lexcnt); /* remove the line */
break;
case ' ': /* delete character */
delnxt(lexcnt);
break;
case '0':
case '^': /* to beginning of line */
kprev();
break;
case '$': /* to beginning of line */
krest();
break;
case '/': /* delete last thing */
succ = rmvlst();
break;
case 27: /* escape is no op */
break;
default:
tverrb("Use d,<sp>,^,$, or / with d ");
break;
}
break;
case 39: /* ^U: half screen up */
dwnlin( min((-lexcnt*tvlins)/2 , nxtlin-curlin+1) );
break;
case 40: /* 'i': insert */
succ = insert(1,lex_def);
break;
case 41: /* 'j': down to line beginning */
dwnlin(lexcnt);
break;
case 42: /* 'k': up to line beginning */
dwnlin(-lexcnt);
break;
case 43: /* 'l': right */
right(lexcnt);
break;
case 44: /* 'm': mark location n */
if (lexcnt < 1 || lexcnt > 9)
lexcnt = 0;
noteloc[lexcnt] = curlin;
break;
case 45: /* 'n': find next (in last direction) */
succ = search((dir_up ? -1 : 1),FALSE);
break;
case 46: /* 'o': open following line {do} */
dwnlin(1);
openln(lexcnt);
succ = insert(1,TRUE); /* go into insert mode */
break;
case 47: /* 'p': put save buffer after cur line */
dwnlin(1);
succ = getsav();
break;
case 48: /* 'r': replace next char */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
delnxt(lexcnt); /* delete count character */
succ = insert((int)fchr,FALSE);
#ifndef VI_MODS
right(-1); /* and back over char replaced */
#endif
break;
case 49: /* 's': substitute */
if ((succ = delnxt(lexcnt)))
succ = insert(1,TRUE);
break;
case 50: /* ESC: no operation */
break;
case 51: /* 'w': advance word */
wordr(lexcnt);
break;
case 52: /* 'x': delete char */
succ = delnxt(lexcnt);
break;
case 53: /* 'y': yank to save buffer */
succ = save(lexcnt,FALSE);
break;
case 54: /* '~': change case */
foldcase(lexcnt);
break;
} /* end of switch */
continue; /* next iteration of do loop */
} /* end of else */
#ifdef SCR_BUF
ttflush();
#endif
} /* end of do loop */
while (1);
lquit:
for ( wtpage(1) ; rdpage() ; wtpage(1) ) /* write whole file */
;
tvclr();
}
#endif
#ifdef EMAX_EM /* emacs modeless editor emulation */
/* =============================>>> EDIT <<<============================= */
edit()
{ /* edit - main editing routine */
SLOW int lexval,lexcnt,succ, lastln, itmp;
SLOW int noteloc[10], ni, lex_def;
SLOW char lexchr;
static int ins_set[] = /* MUCH more limited than tvx */
{
VQUIT, VTVX, VNOOP, VEXTEND, VOPENLINE, VGET, 0
};
static int jump_set[] = /* commands to not reset jump memory */
{
VTVX, VHELP, VNOOP, 0
};
static int c_jump_set[] = /* jump set for ^C */
{
7, 10, 13, 'l', 0
};
SLOW char fchr; /* temp char for prefixed commands */
startm();
remark("Reading file...");
rdpage(); /* read a page into the buffer */
tvclr(); /* clear the screen */
if (curlin >= 1)
tvtype(curlin,tvlins); /* type out lines */
tvxy(1,1); /* and rehome the cursor */
waserr = FALSE; /* no errors to erase yet */
if (curlin<1)
tverr("Buffer empty");
#ifdef SCR_BUF
ttflush();
#endif
lexval = UNKNOWN; /* so can remember 1st time through */
useprint = FALSE; /* not to printer */
succ=TRUE; /* assume success initially */
lastln = 1 ; /* remember where we were */
for (ni = 0 ; ni < 10 ; noteloc[ni++] = 1)
; /* init noteloc */
do
{
oldlex = lexval; /* remember last command */
if (! succ)
echof = TRUE; /* resume echo when error */
lex_def = lex(&lexval,&lexcnt,&lexchr,succ); /* get command input */
if (waserr)
fixend();
waserr=FALSE;
succ=TRUE;
if (lexval == UNKNOWN)
{
cmderr(lexchr);
succ = FALSE; /* announce failure to lex */
}
else
{
if (curlin < 1) /* make sure legal command for empty buffer */
{
if (!inset(lexval,ins_set))
{
tverrb("Can't, buffer empty. Insert 1st ");
succ=FALSE;
continue;
}
}
if (!inset(lexval,jump_set))
lastln = curlin; /* let user look at help w/o changing */
switch (lexval)
{
case 1: /* ^A: Cursor to start of line */
beglin();
break;
case 2: /* ^B: left */
right(-lexcnt);
break;
case 3: /* ^C: Command (tvx) */
if (!grptch(&fchr))
{
succ = FALSE;
break;
}
fchr = clower(fchr); /* lower case */
if (curlin < 1) /* make sure legal command for empty buffer */
{
if ( fchr != 2 && fchr != 'r')
{
tverrb("Can't, buffer empty. Insert 1st ");
succ=FALSE;
break;
}
}
if (!inset((int)fchr,c_jump_set))
lastln = curlin; /* reset jump loc if not special */
switch (fchr)
{
case 1: /* ^A: append to save buffer */
lexval = VSAPPEND;
succ = save(lexcnt,TRUE);
break;
case 2: /* ^B: goto real begin of the file */
succ = fbeg();
break;
case 5: /* ^E: edit repeat buffer */
if (lex_def) /* default 1 passed */
lexcnt = rptuse + 1; /* use current repeat loop */
succ = edit_rpt(lexcnt); /* edit repeat buffer */
break;
case 6: /* ^F: fill (tidy) */
succ = neaten(lexcnt);
break;
case 7: /* ^G: no op */
break;
case 8: /* ^H: half screen down */
dwnlin( min((lexcnt*tvlins)/2 , nxtlin-curlin+1) );
break;
case 10: /* ^J: jump back to last location */
itmp = curlin;
curlin = lastln;
curchr = *(lines+curlin)+1; /* point to the current char */
verify(1);
lastln = itmp;
break;
case 11: /* ^K: kill last thing */
succ = rmvlst();
break;
case 13: /* ^M: Mark current loc */
if (lexcnt < 1 || lexcnt > 9)
lexcnt = 0;
noteloc[lexcnt] = curlin;
break;
case 14: /* ^N: move to next line */
dwnlin(lexcnt);
break;
case 16: /* ^P: beginning of prev line */
dwnlin(-lexcnt);
break;
case 18: /* ^R: restore repeat buffer */
succ = store_rpt(lexcnt); /* store repeat buffer */
break;
case 23: /* ^W: write buffer */
wtpage(lexcnt); /* write out the current page */
succ = rdpage(); /* read in the next */
tvclr();
if (succ || lexcnt < 0)
verify(1);
break;
case '(': /* user 1 */
succ = user_1(lexcnt); /* user function 1 */
break;
case '~': /* ~: change case */
foldcase(lexcnt);
break;
case '*': /* * insert found pattern */
succ = ins_pat(lexcnt);
break;
case ';': /* ;: search again */
lexval = VSAGAIN;
succ = search(1,FALSE);
break;
case 'g': /* goto line number n */
toppag(); /* go to top of file */
dwnlin(lexcnt - 1); /* go to that line */
break;
case 'h': /* H: half screen up */
dwnlin( min((-lexcnt*tvlins)/2 , nxtlin-curlin+1) );
break;
case 'i':
succ = insert(lexcnt,lex_def);
break;
case 'l': /* print screen */
scrprint();
break;
case 'm': /* m: return to marked loc */
itmp = curlin;
if (lexcnt < 1 || lexcnt > 9)
lexcnt = 0;
if (noteloc[lexcnt] >= nxtlin)
{
tverrb("Line no longer there ");
noteloc[lexcnt] = curlin;
}
else
{
curlin = noteloc[lexcnt];
curchr = *(lines+curlin)+1; /* point to the current char */
verify(1);
lastln = itmp;
}
break;
case 'p': /* p: put line ins save buffer */
lexval = VSAVE;
succ = save(lexcnt,FALSE);
break;
case 'r': /* read external file to save buffer */
succ = addfil(1);
break;
case 's': /* search across buffers */
lexval = VNEXT;
succ = snext(1,TRUE);