-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathvi.c
2753 lines (2489 loc) · 55.4 KB
/
vi.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 software is part of the ast package *
* Copyright (c) 1982-2012 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/* Adapted for ksh by David Korn */
/*+ VI.C P.D. Sullivan
*
* One line editor for the shell based on the vi editor.
*
* Questions to:
* P.D. Sullivan
* cbosgd!pds
-*/
#if KSHELL
# include "defs.h"
#else
# include <ast.h>
# include "FEATURE/options"
# include <ctype.h>
#endif /* KSHELL */
#include "io.h"
#include "history.h"
#include "edit.h"
#include "terminal.h"
#include "FEATURE/time"
#ifdef ECHOCTL
# define echoctl ECHOCTL
#else
# define echoctl 0
#endif /* ECHOCTL */
#ifndef FIORDCHK
# define NTICKS 5 /* number of ticks for typeahead */
#endif /* FIORDCHK */
#define MAXCHAR MAXLINE-2 /* max char per line */
#if SHOPT_MULTIBYTE
# include "lexstates.h"
# define gencpy(a,b) ed_gencpy(a,b)
# define genncpy(a,b,n) ed_genncpy(a,b,n)
# define genlen(str) ed_genlen(str)
# define digit(c) ((c&~STRIP)==0 && isdigit(c))
# define is_print(c) ((c&~STRIP) || isprint(c))
# if !_lib_iswprint && !defined(iswprint)
# define iswprint(c) ((c&~0177) || isprint(c))
# endif
static int _isalph(int);
static int _ismetach(int);
static int _isblank(int);
# undef isblank
# define isblank(v) _isblank(virtual[v])
# define isalph(v) _isalph(virtual[v])
# define ismetach(v) _ismetach(virtual[v])
#else
static genchar _c;
# define gencpy(a,b) strcpy((char*)(a),(char*)(b))
# define genncpy(a,b,n) strncpy((char*)(a),(char*)(b),n)
# define genlen(str) strlen(str)
# define isalph(v) ((_c=virtual[v])=='_'||isalnum(_c))
# undef isblank
# define isblank(v) isspace(virtual[v])
# define ismetach(v) ismeta(virtual[v])
# define digit(c) isdigit(c)
# define is_print(c) isprint(c)
#endif /* SHOPT_MULTIBYTE */
#if ( 'a' == 97) /* ASCII? */
# define fold(c) ((c)&~040) /* lower and uppercase equivalent */
#else
# define fold(c) ((c)|0100) /* lower and uppercase equivalent */
#endif
#ifndef iswascii
#define iswascii(c) (!((c)&(~0177)))
#endif
typedef struct _vi_
{
int direction;
int lastmacro;
char addnl; /* boolean - add newline flag */
char last_find; /* last find command */
char last_cmd; /* last command */
char repeat_set;
char nonewline;
int findchar; /* last find char */
genchar *lastline;
int first_wind; /* first column of window */
int last_wind; /* last column in window */
int lastmotion; /* last motion */
int long_char; /* line bigger than window */
int long_line; /* line bigger than window */
int ocur_phys; /* old current physical position */
int ocur_virt; /* old last virtual position */
int ofirst_wind; /* old window first col */
int o_v_char; /* prev virtual[ocur_virt] */
int repeat; /* repeat count for motion cmds */
int lastrepeat; /* last repeat count for motion cmds */
int u_column; /* undo current column */
int U_saved; /* original virtual saved */
genchar *U_space; /* used for U command */
genchar *u_space; /* used for u command */
#ifdef FIORDCHK
clock_t typeahead; /* typeahead occurred */
#else
int typeahead; /* typeahead occurred */
#endif /* FIORDCHK */
#if SHOPT_MULTIBYTE
int bigvi;
#endif
Edit_t *ed; /* pointer to edit data */
} Vi_t;
#define editb (*vp->ed)
#undef putchar
#define putchar(c) ed_putchar(vp->ed,c)
#define crallowed editb.e_crlf
#define cur_virt editb.e_cur /* current virtual column */
#define cur_phys editb.e_pcur /* current phys column cursor is at */
#define curhline editb.e_hline /* current history line */
#define first_virt editb.e_fcol /* first allowable column */
#define globals editb.e_globals /* local global variables */
#define histmin editb.e_hismin
#define histmax editb.e_hismax
#define last_phys editb.e_peol /* last column in physical */
#define last_virt editb.e_eol /* last column */
#define lsearch editb.e_search /* last search string */
#define lookahead editb.e_lookahead /* characters in buffer */
#define previous editb.e_lbuf /* lookahead buffer */
#define max_col editb.e_llimit /* maximum column */
#define Prompt editb.e_prompt /* pointer to prompt */
#define plen editb.e_plen /* length of prompt */
#define physical editb.e_physbuf /* physical image */
#define usreof editb.e_eof /* user defined eof char */
#define usrerase editb.e_erase /* user defined erase char */
#define usrlnext editb.e_lnext /* user defined next literal */
#define usrkill editb.e_kill /* user defined kill char */
#define virtual editb.e_inbuf /* pointer to virtual image buffer */
#define window editb.e_window /* window buffer */
#define w_size editb.e_wsize /* window size */
#define inmacro editb.e_inmacro /* true when in macro */
#define yankbuf editb.e_killbuf /* yank/delete buffer */
#define ABORT -2 /* user abort */
#define APPEND -10 /* append chars */
#define BAD -1 /* failure flag */
#define BIGVI -15 /* user wants real vi */
#define CONTROL -20 /* control mode */
#define ENTER -25 /* enter flag */
#define GOOD 0 /* success flag */
#define INPUT -30 /* input mode */
#define INSERT -35 /* insert mode */
#define REPLACE -40 /* replace chars */
#define SEARCH -45 /* search flag */
#define TRANSLATE -50 /* translate virt to phys only */
#define INVALID (-1) /* invalid column */
static const char paren_chars[] = "([{)]}"; /* for % command */
static void cursor(Vi_t*, int);
static void del_line(Vi_t*,int);
static int getcount(Vi_t*,int);
static void getline(Vi_t*,int);
static int getrchar(Vi_t*);
static int mvcursor(Vi_t*,int);
static void pr_string(Vi_t*,const char*);
static void putstring(Vi_t*,int, int);
static void refresh(Vi_t*,int);
static void replace(Vi_t*,int, int);
static void restore_v(Vi_t*);
static void save_last(Vi_t*);
static void save_v(Vi_t*);
static int search(Vi_t*,int);
static void sync_cursor(Vi_t*);
static int textmod(Vi_t*,int,int);
/*+ VI_READ( fd, shbuf, nchar )
*
* This routine implements a one line version of vi and is
* called by _filbuf.c
*
-*/
/*
* if reedit is non-zero, initialize edit buffer with reedit chars
*/
int ed_viread(void *context, int fd, register char *shbuf, int nchar, int reedit)
{
Edit_t *ed = (Edit_t*)context;
register int i; /* general variable */
register int term_char=0; /* read() termination character */
register Vi_t *vp = ed->e_vi;
char prompt[PRSIZE+2]; /* prompt */
genchar Physical[2*MAXLINE]; /* physical image */
genchar Ubuf[MAXLINE]; /* used for U command */
genchar ubuf[MAXLINE]; /* used for u command */
genchar Window[MAXLINE]; /* window image */
int Globals[9]; /* local global variables */
int esc_or_hang=0; /* <ESC> or hangup */
char cntl_char=0; /* TRUE if control character present */
#if SHOPT_RAWONLY
# define viraw 1
#else
int viraw = (sh_isoption(SH_VIRAW) || ed->sh->st.trap[SH_KEYTRAP]);
# ifndef FIORDCHK
clock_t oldtime, newtime;
struct tms dummy;
# endif /* FIORDCHK */
#endif /* SHOPT_RAWONLY */
if(!vp)
{
ed->e_vi = vp = newof(0,Vi_t,1,0);
vp->lastline = (genchar*)malloc(MAXLINE*CHARSIZE);
vp->direction = -1;
vp->ed = ed;
}
/*** setup prompt ***/
Prompt = prompt;
ed_setup(vp->ed,fd, reedit);
shbuf[reedit] = 0;
#if !SHOPT_RAWONLY
if(!viraw)
{
/*** Change the eol characters to '\r' and eof ***/
/* in addition to '\n' and make eof an ESC */
if(tty_alt(ERRIO) < 0)
return(reexit?reedit:ed_read(context, fd, shbuf, nchar,0));
#ifdef FIORDCHK
ioctl(fd,FIORDCHK,&vp->typeahead);
#else
/* time the current line to determine typeahead */
oldtime = times(&dummy);
#endif /* FIORDCHK */
#if KSHELL
/* abort of interrupt has occurred */
if(ed->sh->trapnote&SH_SIGSET)
i = -1;
else
#endif /* KSHELL */
/*** Read the line ***/
i = ed_read(context, fd, shbuf, nchar, 0);
#ifndef FIORDCHK
newtime = times(&dummy);
vp->typeahead = ((newtime-oldtime) < NTICKS);
#endif /* FIORDCHK */
if(echoctl)
{
if( i <= 0 )
{
/*** read error or eof typed ***/
tty_cooked(ERRIO);
return(i);
}
term_char = shbuf[--i];
if( term_char == '\r' )
term_char = '\n';
if( term_char=='\n' || term_char==ESC )
shbuf[i--] = '\0';
else
shbuf[i+1] = '\0';
}
else
{
register int c = shbuf[0];
/*** Save and remove the last character if its an eol, ***/
/* changing '\r' to '\n' */
if( i == 0 )
{
/*** ESC was typed as first char of line ***/
esc_or_hang = 1;
term_char = ESC;
shbuf[i--] = '\0'; /* null terminate line */
}
else if( i<0 || c==usreof )
{
/*** read error or eof typed ***/
tty_cooked(ERRIO);
if( c == usreof )
i = 0;
return(i);
}
else
{
term_char = shbuf[--i];
if( term_char == '\r' )
term_char = '\n';
#if !defined(VEOL2) && !defined(ECHOCTL)
if(term_char=='\n')
{
tty_cooked(ERRIO);
return(i+1);
}
#endif
if( term_char=='\n' || term_char==usreof )
{
/*** remove terminator & null terminate ***/
shbuf[i--] = '\0';
}
else
{
/** terminator was ESC, which is not xmitted **/
term_char = ESC;
shbuf[i+1] = '\0';
}
}
}
}
else
#endif /* SHOPT_RAWONLY */
{
/*** Set raw mode ***/
#if !SHOPT_RAWONLY
if( editb.e_ttyspeed == 0 )
{
/*** never did TCGETA, so do it ***/
/* avoids problem if user does 'sh -o viraw' */
tty_alt(ERRIO);
}
#endif /* SHOPT_RAWONLY */
if(tty_raw(ERRIO,0) < 0 )
return(reedit?reedit:ed_read(context, fd, shbuf, nchar,0));
i = last_virt-1;
}
/*** Initialize some things ***/
virtual = (genchar*)shbuf;
#if SHOPT_MULTIBYTE
virtual = (genchar*)roundof((char*)virtual-(char*)0,sizeof(genchar));
shbuf[i+1] = 0;
i = ed_internal(shbuf,virtual)-1;
#endif /* SHOPT_MULTIBYTE */
globals = Globals;
cur_phys = i + 1;
cur_virt = i;
first_virt = 0;
vp->first_wind = 0;
last_virt = i;
last_phys = i;
vp->last_wind = i;
vp->long_line = ' ';
vp->long_char = ' ';
vp->o_v_char = '\0';
vp->ocur_phys = 0;
vp->ocur_virt = MAXCHAR;
vp->ofirst_wind = 0;
physical = Physical;
vp->u_column = INVALID - 1;
vp->U_space = Ubuf;
vp->u_space = ubuf;
window = Window;
window[0] = '\0';
if(!yankbuf)
yankbuf = (genchar*)malloc(MAXLINE*CHARSIZE);
if( vp->last_cmd == '\0' )
{
/*** first time for this shell ***/
vp->last_cmd = 'i';
vp->findchar = INVALID;
vp->lastmotion = '\0';
vp->lastrepeat = 1;
vp->repeat = 1;
*yankbuf = 0;
}
/*** fiddle around with prompt length ***/
if( nchar+plen > MAXCHAR )
nchar = MAXCHAR - plen;
max_col = nchar - 2;
if( !viraw )
{
int kill_erase = 0;
for(i=(echoctl?last_virt:0); i<last_virt; ++i )
{
/*** change \r to \n, check for control characters, ***/
/* delete appropriate ^Vs, */
/* and estimate last physical column */
if( virtual[i] == '\r' )
virtual[i] = '\n';
if(!echoctl)
{
register int c = virtual[i];
if( c<=usrerase)
{
/*** user typed escaped erase or kill char ***/
cntl_char = 1;
if(is_print(c))
kill_erase++;
}
else if( !is_print(c) )
{
cntl_char = 1;
if( c == usrlnext )
{
if( i == last_virt )
{
/*** eol/eof was escaped ***/
/* so replace ^V with it */
virtual[i] = term_char;
break;
}
/*** delete ^V ***/
gencpy((&virtual[i]), (&virtual[i+1]));
--cur_virt;
--last_virt;
}
}
}
}
/*** copy virtual image to window ***/
if(last_virt > 0)
last_phys = ed_virt_to_phys(vp->ed,virtual,physical,last_virt,0,0);
if( last_phys >= w_size )
{
/*** line longer than window ***/
vp->last_wind = w_size - 1;
}
else
vp->last_wind = last_phys;
genncpy(window, virtual, vp->last_wind+1);
if( term_char!=ESC && (last_virt==INVALID
|| virtual[last_virt]!=term_char) )
{
/*** Line not terminated with ESC or escaped (^V) ***/
/* eol, so return after doing a total update */
/* if( (speed is greater or equal to 1200 */
/* and something was typed) and */
/* (control character present */
/* or typeahead occurred) ) */
tty_cooked(ERRIO);
if( editb.e_ttyspeed==FAST && last_virt!=INVALID
&& (vp->typeahead || cntl_char) )
{
refresh(vp,TRANSLATE);
pr_string(vp,Prompt);
putstring(vp,0, last_phys+1);
if(echoctl)
ed_crlf(vp->ed);
else
while(kill_erase-- > 0)
putchar(' ');
}
if( term_char=='\n' )
{
if(!echoctl)
ed_crlf(vp->ed);
virtual[++last_virt] = '\n';
}
vp->last_cmd = 'i';
save_last(vp);
#if SHOPT_MULTIBYTE
virtual[last_virt+1] = 0;
last_virt = ed_external(virtual,shbuf);
return(last_virt);
#else
return(++last_virt);
#endif /* SHOPT_MULTIBYTE */
}
/*** Line terminated with escape, or escaped eol/eof, ***/
/* so set raw mode */
if( tty_raw(ERRIO,0) < 0 )
{
tty_cooked(ERRIO);
/*
* The following prevents drivers that return 0 on
* causing an infinite loop
*/
if(esc_or_hang)
return(-1);
virtual[++last_virt] = '\n';
#if SHOPT_MULTIBYTE
virtual[last_virt+1] = 0;
last_virt = ed_external(virtual,shbuf);
return(last_virt);
#else
return(++last_virt);
#endif /* SHOPT_MULTIBYTE */
}
if(echoctl) /*** for cntl-echo erase the ^[ ***/
pr_string(vp,"\b\b\b\b \b\b");
if(crallowed)
{
/*** start over since there may be ***/
/*** a control char, or cursor might not ***/
/*** be at left margin (this lets us know ***/
/*** where we are ***/
cur_phys = 0;
window[0] = '\0';
pr_string(vp,Prompt);
if( term_char==ESC && (last_virt<0 || virtual[last_virt]!=ESC))
refresh(vp,CONTROL);
else
refresh(vp,INPUT);
}
else
{
/*** just update everything internally ***/
refresh(vp,TRANSLATE);
}
}
/*** Handle usrintr, usrquit, or EOF ***/
i = sigsetjmp(editb.e_env,0);
if( i != 0 )
{
if(vp->ed->e_multiline)
{
cur_virt = last_virt;
sync_cursor(vp);
}
virtual[0] = '\0';
tty_cooked(ERRIO);
switch(i)
{
case UEOF:
/*** EOF ***/
return(0);
case UINTR:
/** interrupt **/
return(-1);
}
return(-1);
}
/*** Get a line from the terminal ***/
vp->U_saved = 0;
if(reedit)
{
cur_phys = vp->first_wind;
vp->ofirst_wind = INVALID;
refresh(vp,INPUT);
}
if(viraw)
getline(vp,APPEND);
else if(last_virt>=0 && virtual[last_virt]==term_char)
getline(vp,APPEND);
else
getline(vp,ESC);
if(vp->ed->e_multiline)
cursor(vp, last_phys);
/*** add a new line if user typed unescaped \n ***/
/* to cause the shell to process the line */
tty_cooked(ERRIO);
if(ed->e_nlist)
{
ed->e_nlist = 0;
stakset(ed->e_stkptr,ed->e_stkoff);
}
if( vp->addnl )
{
virtual[++last_virt] = '\n';
ed_crlf(vp->ed);
}
if( ++last_virt >= 0 )
{
#if SHOPT_MULTIBYTE
if(vp->bigvi)
{
vp->bigvi = 0;
shbuf[last_virt-1] = '\n';
}
else
{
virtual[last_virt] = 0;
last_virt = ed_external(virtual,shbuf);
}
#endif /* SHOPT_MULTIBYTE */
#if SHOPT_EDPREDICT
if(vp->ed->nhlist)
ed_histlist(vp->ed,0);
#endif /* SHOPT_EDPREDICT */
return(last_virt);
}
else
return(-1);
}
/*{ APPEND( char, mode )
*
* This routine will append char after cur_virt in the virtual image.
* mode = APPEND, shift chars right before appending
* REPLACE, replace char if possible
*
}*/
static void append(Vi_t *vp,int c, int mode)
{
register int i,j;
if( last_virt<max_col && last_phys<max_col )
{
if( mode==APPEND || (cur_virt==last_virt && last_virt>=0))
{
j = (cur_virt>=0?cur_virt:0);
for(i = ++last_virt; i > j; --i)
virtual[i] = virtual[i-1];
}
virtual[++cur_virt] = c;
}
else
ed_ringbell();
return;
}
/*{ BACKWORD( nwords, cmd )
*
* This routine will position cur_virt at the nth previous word.
*
}*/
static void backword(Vi_t *vp,int nwords, register int cmd)
{
register int tcur_virt = cur_virt;
while( nwords-- && tcur_virt > first_virt )
{
if( !isblank(tcur_virt) && isblank(tcur_virt-1)
&& tcur_virt>first_virt )
--tcur_virt;
else if(cmd != 'B')
{
register int last = isalph(tcur_virt-1);
register int cur = isalph(tcur_virt);
if((!cur && last) || (cur && !last))
--tcur_virt;
}
while( isblank(tcur_virt) && tcur_virt>=first_virt )
--tcur_virt;
if( cmd == 'B' )
{
while( !isblank(tcur_virt) && tcur_virt>=first_virt )
--tcur_virt;
}
else
{
if(isalph(tcur_virt))
while( isalph(tcur_virt) && tcur_virt>=first_virt )
--tcur_virt;
else
while( !isalph(tcur_virt) && !isblank(tcur_virt)
&& tcur_virt>=first_virt )
--tcur_virt;
}
cur_virt = ++tcur_virt;
}
return;
}
/*{ CNTLMODE()
*
* This routine implements the vi command subset.
* The cursor will always be positioned at the char of interest.
*
}*/
static int cntlmode(Vi_t *vp)
{
register int c;
register int i;
genchar tmp_u_space[MAXLINE]; /* temporary u_space */
genchar *real_u_space; /* points to real u_space */
int tmp_u_column = INVALID; /* temporary u_column */
int was_inmacro;
if(!vp->U_saved)
{
/*** save virtual image if never done before ***/
virtual[last_virt+1] = '\0';
gencpy(vp->U_space, virtual);
vp->U_saved = 1;
}
save_last(vp);
real_u_space = vp->u_space;
curhline = histmax;
first_virt = 0;
vp->repeat = 1;
if( cur_virt > INVALID )
{
/*** make sure cursor is at the last char ***/
sync_cursor(vp);
}
else if(last_virt > INVALID )
cur_virt++;
/*** Read control char until something happens to cause a ***/
/* return to APPEND/REPLACE mode */
while( c=ed_getchar(vp->ed,-1) )
{
vp->repeat_set = 0;
was_inmacro = inmacro;
if( c == '0' )
{
/*** move to leftmost column ***/
cur_virt = 0;
sync_cursor(vp);
continue;
}
if( digit(c) )
{
c = getcount(vp,c);
if( c == '.' )
vp->lastrepeat = vp->repeat;
}
/*** see if it's a move cursor command ***/
if(mvcursor(vp,c))
{
sync_cursor(vp);
vp->repeat = 1;
continue;
}
/*** see if it's a repeat of the last command ***/
if( c == '.' )
{
c = vp->last_cmd;
vp->repeat = vp->lastrepeat;
i = textmod(vp,c, c);
}
else
{
i = textmod(vp,c, 0);
}
/*** see if it's a text modification command ***/
switch(i)
{
case BAD:
break;
default: /** input mode **/
if(!was_inmacro)
{
vp->last_cmd = c;
vp->lastrepeat = vp->repeat;
}
vp->repeat = 1;
if( i == GOOD )
continue;
return(i);
}
switch( c )
{
/***** Other stuff *****/
case cntl('L'): /** Redraw line **/
/*** print the prompt and ***/
/* force a total refresh */
if(vp->nonewline==0 && !vp->ed->e_nocrnl)
putchar('\n');
vp->nonewline = 0;
pr_string(vp,Prompt);
window[0] = '\0';
cur_phys = vp->first_wind;
vp->ofirst_wind = INVALID;
vp->long_line = ' ';
break;
case cntl('V'):
{
register const char *p = fmtident(e_version);
save_v(vp);
del_line(vp,BAD);
while(c = *p++)
append(vp,c,APPEND);
refresh(vp,CONTROL);
ed_getchar(vp->ed,-1);
restore_v(vp);
ed_ungetchar(vp->ed,'a');
break;
}
case '/': /** Search **/
case '?':
case 'N':
case 'n':
save_v(vp);
switch( search(vp,c) )
{
case GOOD:
/*** force a total refresh ***/
window[0] = '\0';
goto newhist;
case BAD:
/*** no match ***/
ed_ringbell();
default:
if( vp->u_column == INVALID )
del_line(vp,BAD);
else
restore_v(vp);
break;
}
break;
case 'j': /** get next command **/
case '+': /** get next command **/
#if SHOPT_EDPREDICT
if(vp->ed->hlist)
{
if(vp->ed->hoff >= vp->ed->hmax)
goto ringbell;
vp->ed->hoff++;
goto hupdate;
}
#endif /* SHOPT_EDPREDICT */
curhline += vp->repeat;
if( curhline > histmax )
{
curhline = histmax;
goto ringbell;
}
else if(curhline==histmax && tmp_u_column!=INVALID )
{
vp->u_space = tmp_u_space;
vp->u_column = tmp_u_column;
restore_v(vp);
vp->u_space = real_u_space;
break;
}
save_v(vp);
cur_virt = INVALID;
goto newhist;
case 'k': /** get previous command **/
case '-': /** get previous command **/
#if SHOPT_EDPREDICT
if(vp->ed->hlist)
{
if(vp->ed->hoff == 0)
goto ringbell;
vp->ed->hoff--;
hupdate:
ed_histlist(vp->ed,*vp->ed->hlist!=0);
vp->nonewline++;
ed_ungetchar(vp->ed,cntl('L'));
continue;
}
#endif /* SHOPT_EDPREDICT */
if( curhline == histmax )
{
vp->u_space = tmp_u_space;
i = vp->u_column;
save_v(vp);
vp->u_space = real_u_space;
tmp_u_column = vp->u_column;
vp->u_column = i;
}
curhline -= vp->repeat;
if( curhline <= histmin )
{
curhline += vp->repeat;
goto ringbell;
}
save_v(vp);
cur_virt = INVALID;
newhist:
if(curhline!=histmax || cur_virt==INVALID)
hist_copy((char*)virtual, MAXLINE, curhline,-1);
else
{
strcpy((char*)virtual,(char*)vp->u_space);
#if SHOPT_MULTIBYTE
ed_internal((char*)vp->u_space,vp->u_space);
#endif /* SHOPT_MULTIBYTE */
}
#if SHOPT_MULTIBYTE
ed_internal((char*)virtual,virtual);
#endif /* SHOPT_MULTIBYTE */
if((last_virt=genlen(virtual)-1) >= 0 && cur_virt == INVALID)
cur_virt = 0;
#if SHOPT_EDPREDICT
if(vp->ed->hlist)
{
ed_histlist(vp->ed,0);
if(c=='\n')
ed_ungetchar(vp->ed,c);
ed_ungetchar(vp->ed,cntl('L'));
vp->nonewline = 1;
cur_virt = 0;
}
#endif /*SHOPT_EDPREDICT */
break;
case 'u': /** undo the last thing done **/
restore_v(vp);
break;
case 'U': /** Undo everything **/
save_v(vp);
if( virtual[0] == '\0' )
goto ringbell;
else
{
gencpy(virtual, vp->U_space);
last_virt = genlen(vp->U_space) - 1;
cur_virt = 0;
}
break;
#if KSHELL
case 'v':
if(vp->repeat_set==0)
goto vcommand;
#endif /* KSHELL */
case 'G': /** goto command repeat **/
if(vp->repeat_set==0)
vp->repeat = histmin+1;
if( vp->repeat <= histmin || vp->repeat > histmax )
{
goto ringbell;
}
curhline = vp->repeat;
save_v(vp);
if(c == 'G')
{
cur_virt = INVALID;
goto newhist;
}
#if KSHELL
vcommand:
if(ed_fulledit(vp->ed)==GOOD)
return(BIGVI);
else
goto ringbell;
#endif /* KSHELL */
case '#': /** insert(delete) # to (no)comment command **/
if( cur_virt != INVALID )
{
register genchar *p = &virtual[last_virt+1];
*p = 0;
/*** see whether first char is comment char ***/
c = (virtual[0]=='#');
while(p-- >= virtual)