-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.c
851 lines (679 loc) · 18.1 KB
/
bind.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
/* This file is for functions having to do with key bindings,
descriptions, help commands and startup file.
written 11-feb-86 by Daniel Lawrence
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
#include "epath.h"
static unsigned int stock PP((char *keyname));
static int chunbind PP((int c));
static unsigned int getckey PP((int mflag));
static CONSTA char *getfname PP((int (*func)(int f, int n)));
#if APROP
static int strinc PP((CONSTA char *source, CONSTA char *sub));
#endif
#define ostring(s) if (discmd || !clexec) outstring(s)
int me_help(f, n) /* give me some help!!!!
bring up a fake buffer and read the help file
into it with view mode */
int f, n;
{
register BUFFER *bp; /* buffer pointer to help */
CONSTA char *fname; /* ptr to file returned by flook() */
UNUSED_ARGS_FN;
fname = NULL;
/* first check if we are already here */
bp = bfind("emacs.hlp", FALSE, BFINVS);
if (bp == NULL) {
fname = flook(pathname[1], FALSE);
if (fname == NULL) {
mlwrite("[Help file is not online]");
return(FALSE);
}
}
/* split the current window to make room for the help stuff */
if (splitwind(FALSE, 1) == FALSE)
return(FALSE);
curwp = spltwp; curbp = curwp->w_bufp;
if (bp == NULL) {
/* and read the stuff in */
if (getfile(fname, FALSE) == FALSE)
return(FALSE);
} else
swbuffer(bp);
/* make this window in VIEW mode, update all mode lines */
curbp->b_mode |= MDVIEW;
curbp->b_flag |= BFINVS;
upmode();
return(TRUE);
}
int deskey(f, n) /* describe the command for a certain key */
int f, n;
{
register int c; /* key to describe */
CONSTA char *ptr; /* string pointer to scan output strings */
char outseq[NSTRING]; /* output buffer for command sequence */
#ifdef NO_PROTOTYPE
#if C86 | DECUSC | DARWIN
int *getbind(int c);
#else
int (*getbind(int c))(int f, int n);
#endif
#endif
UNUSED_ARGS_FN;
/* prompt the user to type us a key to describe */
mlwrite(": describe-key ");
/* get the command sequence to describe
change it to something we can print as well */
cmdstr(c = getckey(FALSE), &outseq[0]);
/* and dump it out */
ostring(outseq);
ostring(" ");
/* find the right ->function */
if ((ptr = getfname(getbind(c))) == NULL)
ptr = "Not Bound";
/* output the command sequence */
ostring(ptr);
return(TRUE);
}
/* bindtokey: add a new key to the key binding table */
int bindtokey(f, n)
int f, n; /* command arguments [IGNORED] */
{
register unsigned int c;/* command key to bind */
register int (*kfunc)(int f, int n); /* ptr to the requested function to bind to */
register KEYTAB *ktp; /* pointer into the command table */
register int found; /* matched command flag */
char outseq[80]; /* output buffer for keystroke sequence */
#ifdef NO_PROTOTYPE
int (*getname(CONSTA char *prompt))(int f, int n);
#endif
#if C86 | DECUSC
int (*fmeta)(), (*fcex)(), (*funarg)(), (*fctrlg)();
fmeta = meta; fcex = cex; funarg = unarg; fctrlg = ctrlg;
#else
#define fmeta meta
#define fcex cex
#define funarg unarg
#define fctrlg ctrlg
#endif
UNUSED_ARGS_FN;
/* get the function name to bind it to */
kfunc = getname(": bind-to-key ");
if (kfunc == NULL) {
mlwrite("[No such function]");
return(FALSE);
}
ostring(" ");
/* get the command sequence to bind */
TTflush();
c = getckey((kfunc == fmeta) || (kfunc == fcex) ||
(kfunc == funarg) || (kfunc == fctrlg));
/* change it to something we can print as well */
cmdstr((int) c, &outseq[0]);
/* and dump it out */
ostring(outseq);
/* if the function is a prefix key */
if (kfunc == fmeta || kfunc == fcex ||
kfunc == funarg || kfunc == fctrlg) {
/* search for an existing binding for the prefix key */
ktp = &keytab[0];
#if DECEDT
if (kfunc == funarg || kfunc == fctrlg)
#endif
while (ktp->k_fp != NULL) {
if (ktp->k_fp == kfunc)
chunbind(ktp->k_code);
++ktp;
}
}
/* search the table to see if it exists */
ktp = &keytab[0];
found = FALSE;
while (ktp->k_fp != NULL) {
if (((int)c) == (int) ktp->k_code) {
found = TRUE;
break;
}
++ktp;
}
if (found) { /* it exists, just change it then */
if (((int)c) == metac) metac = 0;
if (((int)c) == ctlxc) ctlxc = 0;
if (((int)c) == reptc) reptc = 0;
if (((int)c) == abortc) abortc = 0;
ktp->k_fp = kfunc;
if (c > 0 && c < NFBIND) fkeytab[c] = kfunc;
else if (c == (FUNC|'C')) ffuncc = kfunc;
fkeylast.k_code = (short) c;
fkeylast.k_fp = kfunc;
} else { /* otherwise we need to add it to the end */
/* if we run out of binding room, bitch */
if (ktp >= &keytab[NBINDS]) {
mlwrite("Binding table FULL!");
return(FALSE);
}
ktp->k_code = (short) c; /* add keycode */
ktp->k_fp = kfunc; /* and the function pointer */
++ktp; /* and make sure the next is null */
ktp->k_code = 0;
ktp->k_fp = NULL;
if (c > 0 && c < NFBIND) fkeytab[c] = kfunc;
else if (c == (FUNC|'C')) ffuncc = kfunc;
fkeylast.k_code = (short) c;
fkeylast.k_fp = kfunc;
}
/* reset the appropriate global prefix variable */
if (metac == 0 && kfunc == fmeta) metac = c;
if (ctlxc == 0 && kfunc == fcex) ctlxc = c;
if (reptc == 0 && kfunc == funarg) reptc = c;
if (abortc == 0 && kfunc == fctrlg) abortc = c;
return(TRUE);
}
/* unbindkey: delete a key from the key binding table */
int unbindkey(f, n)
int f, n; /* command arguments [IGNORED] */
{
register int c; /* command key to unbind */
char outseq[80]; /* output buffer for keystroke sequence */
UNUSED_ARGS_FN;
/* prompt the user to type in a key to unbind */
mlwrite(": unbind-key ");
/* get the command sequence to unbind */
c = getckey(FALSE); /* get a command sequence */
/* change it to something we can print as well */
cmdstr(c, &outseq[0]);
/* and dump it out */
ostring(outseq);
/* if it isn't bound, bitch */
if (chunbind(c) == FALSE) {
mlwrite("[Key not bound]");
return(FALSE);
}
return(TRUE);
}
static int chunbind(c)
int c; /* command key to unbind */
{
register KEYTAB *ktp; /* pointer into the command table */
register KEYTAB *sktp; /* saved pointer into the command table */
register int found; /* matched command flag */
/* search the table to see if the key exists */
ktp = &keytab[0];
found = FALSE;
while (ktp->k_fp != NULL) {
if (c == (int) ktp->k_code) {
found = TRUE;
break;
}
++ktp;
}
/* if it isn't bound, bitch */
if (!found)
return(FALSE);
/* save the pointer and scan to the end of the table */
sktp = ktp;
while (ktp->k_fp != NULL)
++ktp;
--ktp; /* backup to the last legit entry */
/* copy the last entry to the current one */
sktp->k_code = ktp->k_code;
sktp->k_fp = ktp->k_fp;
/* null out the last one */
ktp->k_code = 0;
ktp->k_fp = NULL;
/* clear in fast lookup area */
if (c > 0 && c < NFBIND) fkeytab[c] = NULL;
else if (c == (FUNC|'C')) ffuncc = NULL;
fkeylast.k_code = keytab[0].k_code;
fkeylast.k_fp = keytab[0].k_fp;
if (c == metac) metac = 0;
if (c == ctlxc) ctlxc = 0;
if (c == reptc) reptc = 0;
if (c == abortc) abortc = 0;
return(TRUE);
}
int desbind(f, n) /* describe bindings
bring up a fake buffer and list the key bindings
into it with view mode */
int f, n;
#if APROP
{
UNUSED_ARGS_FN;
return(buildlist(1, ""));
}
int apro(f, n) /* Apropos (List functions that match a substring) */
int f, n;
{
char mstring[NSTRING]; /* string to match cmd names to */
int status; /* status return */
UNUSED_ARGS_FN;
status = mlreply("Apropos string: ", mstring, NSTRING - 1);
if (status != TRUE)
return(status);
return(buildlist(0, mstring));
}
int buildlist(type, mstring) /* build a binding list (limited or full) */
int type; /* 2 = initial match list, 1 = full list, 0 = partial list */
CONSTA char *mstring; /* match string if a partial list */
#endif
{
#if ST520 & LATTICE
#define register
#endif
register KEYTAB *ktp; /* pointer into the command table */
register NBIND *nptr; /* pointer into the name binding table */
register BUFFER *listbuf; /* buffer to put binding list into */
int cpos; /* current position to use in outseq */
char outseq[80]; /* output buffer for keystroke sequence */
int mlen, len;
/* get a buffer for the binding list */
listbuf = bfind("Binding list", TRUE, 0);
if (listbuf == NULL || bclear(listbuf) == FALSE) {
mlwrite("Can not display binding list");
return(FALSE);
}
/* let us know this is in progress */
mlwrite("[Building binding list]");
mlen = (int) strlen(mstring);
/* build the contents of this window, inserting it line by line */
nptr = &names[0];
while (nptr->n_func != NULL) {
/* add in the command name */
strcpy(outseq, nptr->n_name);
cpos = len = (int) strlen(outseq);
#if APROP
/* if we are executing an apropos command..... */
if (type == 0 &&
/* and current string doesn't include the search string */
strinc(outseq, mstring) == FALSE)
goto fail;
#endif
if (type == 2 && mlen > 0) {
if (len > mlen) len = mlen;
if (strncmp(outseq, mstring, len))
goto fail;
}
/* search down any keys bound to this */
ktp = &keytab[0];
while (ktp->k_fp != NULL) {
if (ktp->k_fp == nptr->n_func) {
/* pad out some spaces */
while (cpos < 25)
outseq[cpos++] = ' ';
/* add in the command sequence */
cmdstr(ktp->k_code, &outseq[cpos]);
/* and add it as a line into the buffer */
if (addline(listbuf, outseq) != TRUE)
return(FALSE);
cpos = 0; /* and clear the line */
}
++ktp;
}
/* if no key was bound, we need to dump it anyway */
if (cpos > 0) {
outseq[cpos] = 0;
if (addline(listbuf, outseq) != TRUE)
return(FALSE);
}
fail: /* and on to the next name */
++nptr;
}
wpopup(listbuf);
mlerase(); /* clear the mode line */
return(TRUE);
}
#if APROP
static int strinc(source, sub) /* does source include sub? */
CONSTA char *source; /* string to search in */
CONSTA char *sub; /* substring to look for */
{
CONSTA char *sp; /* ptr into source */
CONSTA char *nxtsp; /* next ptr into source */
CONSTA char *tp; /* ptr into substring */
/* for each character in the source string */
sp = source;
while (*sp) {
tp = sub;
nxtsp = sp;
/* is the substring here? */
while (*tp) {
if (*nxtsp++ != *tp)
break;
else
tp++;
}
/* yes, return a success */
if (*tp == 0)
return(TRUE);
/* no, onward */
sp++;
}
return(FALSE);
}
#endif
/* get a command key sequence from the keyboard */
static unsigned int getckey(mflag)
int mflag; /* going for a meta sequence? */
{
register unsigned int c; /* character fetched */
char tok[NSTRING]; /* command incoming */
/* check to see if we are executing a command line */
if (clexec) {
macarg(tok); /* get the next token */
return(stock(tok));
}
/* or the normal way */
if (mflag)
c = get1key();
else
c = getcmd();
return(c);
}
/* execute the startup file */
int startup(sfname)
CONSTA char *sfname; /* name of startup file (null if default) */
{
CONSTA char *fname; /* resulting file name to execute */
/* look up the startup file */
if (*sfname != 0)
fname = flook(sfname, TRUE);
else
fname = flook(pathname[0], TRUE);
/* if it isn't around, don't sweat it */
if (fname == NULL)
return(TRUE);
/* otherwise, execute the sucker */
return(dofile(fname));
}
/* Look up the existance of a file along the normal or PATH
environment variable. Look first in the HOME directory if
asked and possible
*/
CONSTA char *flook(fname, hflag)
CONSTA char *fname; /* base file name to search for */
int hflag; /* Look in the HOME environment variable first? */
{
register char *home; /* path to home directory */
register char *path; /* environmental PATH variable */
register char *sp; /* pointer into path spec */
register int i; /* index */
static char FAR fspec[NSTRING]; /* full path spec to search */
if (fname == NULL || *fname == '\0' || strcmp(fname, "-") == 0) {
return(NULL);
}
/* always try the current directory first */
if (ffropen(fname, FALSE) == FIOSUC) {
ffrclose();
return(fname);
}
#if ENVFUNC
if (hflag) {
home = getenv("HOME");
if (home != NULL) {
/* build home dir file spec */
strcpy(fspec, home);
#if VMS
#else
#if MSDOS
strcat(fspec, "\\");
#else
strcat(fspec, "/");
#endif
#endif
strcat(fspec, fname);
/* and try it out */
if (ffropen(fspec, FALSE) == FIOSUC) {
ffrclose();
return(fspec);
}
}
}
#endif
#if ENVFUNC && (VMS == 0)
/* get the PATH variable */
path = getenv("PATH");
if (path != NULL)
while (*path) {
/* build next possible file spec */
sp = fspec;
while (*path && (*path != PATHCHR))
*sp++ = *path++;
#if VMS
#else
#if MSDOS
if (sp > fspec && *(sp-1) != '\\') *sp++ = '\\';
#else
if (sp > fspec && *(sp-1) != '/') *sp++ = '/';
#endif
#endif
*sp = 0;
strcat(fspec, fname);
/* and try it out */
if (ffropen(fspec, FALSE) == FIOSUC) {
ffrclose();
return(fspec);
}
if (*path == PATHCHR)
++path;
}
#endif
/* look it up via the old table method */
for (i=2; i < NPNAMES; i++) {
strcpy(fspec, pathname[i]);
strcat(fspec, fname);
/* and try it out */
if (ffropen(fspec, FALSE) == FIOSUC) {
ffrclose();
return(fspec);
}
}
return(NULL); /* no such luck */
}
VOID cmdstr(c, seq) /* change a key command to a string we can print out */
int c; /* sequence to translate */
char *seq; /* destination string for sequence */
{
char *ptr; /* pointer into current position in sequence */
ptr = seq;
/* apply meta sequence if needed */
if (c & META) {
*ptr++ = 'M';
*ptr++ = '-';
}
/* apply ^X sequence if needed */
if (c & CTLX) {
*ptr++ = '^';
*ptr++ = 'X';
}
/* apply SPEC sequence if needed */
if (c & SPEC) {
*ptr++ = 'F';
*ptr++ = 'N';
}
/* apply control sequence if needed */
if (c & CTRL) {
*ptr++ = '^';
}
c = c & 255; /* strip the prefixes */
/* and output the final sequence */
*ptr++ = (char) c;
*ptr = 0; /* terminate the string */
}
/* This function looks a key binding up in the binding table */
#if C86 | DECUSC | DARWIN
int *getbind(c)
#else
int (*getbind(c))(int f, int n)
#endif
int c; /* key to find what is bound to it */
{
register KEYTAB *ktp;
if (c > 0 && c < NFBIND) return(fkeytab[c]);
else if (c == (FUNC|'C')) return(ffuncc);
else if (c == fkeylast.k_code) return(fkeylast.k_fp);
ktp = &keytab[0]; /* Look in key table. */
while (ktp->k_fp != NULL) {
if (c == (int) ktp->k_code) {
fkeylast.k_code = (short) c;
fkeylast.k_fp = ktp->k_fp;
return(ktp->k_fp);
}
++ktp;
}
/* no such binding */
return(NULL);
}
/* getfname: This function takes a ptr to function and gets the name
associated with it
*/
static CONSTA char *getfname(func)
int (*func)(int f, int n); /* ptr to the requested function to bind to */
{
register NBIND *nptr; /* pointer into the name binding table */
/* skim through the table, looking for a match */
nptr = &names[0];
while (nptr->n_func != NULL) {
if (nptr->n_func == func)
return(nptr->n_name);
++nptr;
}
return(NULL);
}
/* match fname to a function in the names table
and return any match or NULL if none */
#if C86 | DECUSC | DARWIN
int *fncmatch(fname)
#else
int (*fncmatch(fname))(int f, int n)
#endif
char *fname; /* name to attempt to match */
{
register NBIND *ffp; /* pointer to entry in name binding table */
char firstch;
/* scan through the table, returning any match */
ffp = &names[0];
firstch = (char) ((fname[0]) & ~DIFCASE);
while (ffp->n_func != NULL) {
if (firstch == (char) (ffp->n_name[0] & ~DIFCASE) &&
strcompare(fname, ffp->n_name, NLINE) == 0)
return(ffp->n_func);
++ffp;
}
return(NULL);
}
/* stock: String key name TO Command Key */
static unsigned int stock(keyname)
char *keyname; /* name of key to translate to Command key form */
{
register unsigned int c; /* key sequence to return */
int i, code;
/* parse it up */
c = 0;
/* first, the META prefix */
if (*keyname == 'M' && *(keyname+1) == '-') {
c = META;
keyname += 2;
}
/* the control-x prefix */
if (*keyname == '^' && *(keyname+1) == 'X') {
c |= CTLX;
keyname += 2;
}
/* next the function prefix */
if (*keyname == 'F' && *(keyname+1) == 'N') {
c |= SPEC;
keyname += 2;
}
/* control-x as well... (but not with FN) */
if (*keyname == '^' && *(keyname+1) == 'X' && !(c & (SPEC|CTLX))) {
c |= CTLX;
keyname += 2;
}
/* a control char? */
if (*keyname == '^' && *(keyname+1) != 0) {
c |= CTRL;
++keyname;
}
/* simplify entry of VT200 escape + [ + number + ~ sequences */
if ((c & SPEC) && *keyname >= '0' && *keyname <= '9' && *(keyname+1)) {
code = 0;
for(i = 0; i < 4 && *keyname >= '0' && *keyname <= '9'; i++)
code = code*10 + *keyname++ - '0';
code &= 0xFF;
if (code>=0x00 && code<=0x1F) code = CTRL | (code+'@');
c |= code;
}
else {
if (*keyname < 32) {
c |= CTRL;
*keyname += '@';
}
/* make sure we are not lower case (not with function keys)*/
if (*keyname >= 'a' && *keyname <= 'z' && !(c & SPEC))
*keyname -= 32;
/* the final sequence... */
c |= *keyname;
}
return(c);
}
CONSTA char *transbind(skey) /* string key name to binding name.... */
char *skey; /* name of keey to get binding for */
{
CONSTA char *bindname;
#ifdef NO_PROTOTYPE
#if C86 | DECUSC
int *getbind(int c);
#else
int (*getbind(int c))(int f, int n);
#endif
#endif
bindname = getfname(getbind( (int) stock(skey)));
if (bindname == NULL)
bindname = "ERROR";
return(bindname);
}
VOID fkeyinit() /* init fast binding lookup table */
{
register int c;
register KEYTAB *ktp;
for(c = 0; c < NFBIND; c++) fkeytab[c] = NULL;
ffuncc = NULL;
ktp = &keytab[0];
while (ktp->k_fp != NULL) {
if (ktp->k_code > 0 && ktp->k_code < NFBIND)
fkeytab[ ktp->k_code ] = ktp->k_fp;
else if (ktp->k_code == (FUNC|'C'))
ffuncc = ktp->k_fp;
++ktp;
}
fkeylast.k_code = keytab[0].k_code;
fkeylast.k_fp = keytab[0].k_fp;
}
int chrcompare(a, b)
int a, b;
{
if (islower(a)) a ^= DIFCASE;
if (islower(b)) b ^= DIFCASE;
if (a > b) return(1);
if (a < b) return(-1);
return(0);
}
int strcompare(a, b, len)
CONSTA char *a;
CONSTA char *b;
int len;
{
char ua, ub;
while (len-- > 0) {
ua = *a++;
if (islower(ua)) ua ^= DIFCASE;
ub = *b++;
if (islower(ub)) ub ^= DIFCASE;
if (ua > ub) return(1);
if (ua < ub) return(-1);
if (!ua) return(0);
}
return(0);
}