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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
|
/*
* map.c -- map and map! interface routines
* Original interface by Otto Lind, 6/3/93
* Additional map and map! support by Kevin Buettner, 9/17/94
*
* $Id: map.c,v 1.136 2025/01/26 17:07:24 tom Exp $
*/
#include "estruct.h"
#include "edef.h"
#if DISP_NTWIN
# define NTWIN_CURSOR_MINWAIT 550 /* msec */
#endif
/*
* Picture for struct maprec
* -------------------------
*
* Assume the following mappings...
*
* map za abc
* map zb def
* map q quit
*
* These may be represented by the following picture...
*
* |
* v
* +---+--------+---+---+ +---+--------+---+---+
* | z | NULL | o | o-+-->| q | "quit" | 0 | 0 |
* +---+--------+-|-+---+ +---+--------+---+---+
* |
* v
* +---+--------+---+---+ +---+--------+---+---+
* | a | "abc" | 0 | o-+-->| b | "def" | 0 | 0 |
* +---+--------+---+---+ +---+--------+---+---+
*
* where the pertinent fields are as follows:
*
* +----+-----+-------+-------+
* | ch | srv | dlink | flink |
* +----+-----+-------+-------+
*
* When matching a character sequence, we follow dlink when we've found a
* matching character. We change the character to be matched to the next
* character in the sequence. If the character doesn't match, we stay at
* the same level (with the same character) and follow flink.
*
*/
#define MAPREC struct maprec
MAPREC {
int ch;
/* character to match */
UINT flags;
/* flags word */
MAPREC *dlink;
/* Where to go to match the next character in a multi-character sequence.
*/
MAPREC *flink;
/* Where to try next if match against current character is unsuccessful.
*/
int irv;
/* system defined mapping: The (wide) character code to replace a matched
* sequence by.
*/
char *srv;
/* user defined mapping: This is the string to replace a matched sequence
* by.
*/
};
#define STRIP_REMAPFLAGS(c) ((c) & (int) ~REMAPFLAGS)
#define MAPF_SYSTIMER 0x01
#define MAPF_USERTIMER 0x02
#define MAPF_TIMERS 0x03
#define MAPF_NOREMAP 0x04
static int suppress_sysmap;
static struct maprec *map_command = NULL;
static struct maprec *map_insert = NULL;
static struct maprec *map_syskey = NULL;
static struct maprec *abbr_map = NULL;
typedef int (*AvailFunc) (void);
typedef int (*GetFunc) (void);
typedef int (*StartFunc) (void);
#if !OPT_SHOW_MAPS
#define relist_mappings(name)
#endif
#define NUMKEYSTR (KBLOCK / 2)
#ifdef DOKEYLOG
int do_keylog = 1;
#endif
#if OPT_SHOW_MAPS
#define MAPS_PREFIX 12
/*ARGSUSED*/
static void
makemaplist(int dummy GCC_UNUSED, void *mapp)
{
TBUFF *lhsstr = NULL;
struct maprec **lhsstack;
struct maprec *mp = (struct maprec *) mapp;
int footnote = 0;
size_t depth = 0;
size_t maxdepth;
size_t i;
char *the_lhs_string;
lhsstr = tb_init(&lhsstr, 0);
lhsstack = typeallocn(struct maprec *, maxdepth = NSTRING);
if (lhsstack == NULL) {
no_memory("makemaplist");
return;
}
for_ever {
if (mp) {
const char *remapnote;
char *mapstr;
char esc_seq[NSTRING];
tb_put(&lhsstr, depth, mp->ch);
if (depth + 1 >= maxdepth) {
maxdepth *= 2;
safe_typereallocn(struct maprec *, lhsstack, maxdepth);
if (lhsstack == NULL) {
no_memory("makemaplist");
return;
}
}
lhsstack[depth++] = mp->flink;
mapstr = (char *) 0;
if (mp->srv) {
mapstr = mp->srv;
} else if (mp->irv != -1) {
(void) kcod2escape_seq(mp->irv, esc_seq, sizeof(esc_seq));
mapstr = esc_seq;
}
if (mapstr && (the_lhs_string = tb_values(lhsstr)) != NULL) {
if (mapp && (struct maprec *) mapp == abbr_map) {
/* the abbr map is stored inverted */
if (the_lhs_string != NULL)
for (i = depth; i != 0;)
bputc(the_lhs_string[--i]);
} else {
if (mp->flags & MAPF_NOREMAP) {
remapnote = "(n)";
footnote++;
} else {
remapnote = " ";
}
bprintf("%s ", remapnote);
bputsn_xcolor(the_lhs_string, (int) depth, XCOLOR_STRING);
}
bputc('\t');
bputsn_xcolor(mapstr, -1, XCOLOR_STRING);
bputc('\n');
}
mp = mp->dlink;
} else if (depth != 0)
mp = lhsstack[--depth];
else
break;
}
if (footnote) {
bprintf("[(n) means never remap]\n");
}
tb_free(&lhsstr);
free((char *) lhsstack);
}
static int
show_mapped_chars(const char *bname)
{
struct maprec *mp;
if (strcmp(bname, MAP_BufName) == 0)
mp = map_command;
else if (strcmp(bname, MAPBANG_BufName) == 0)
mp = map_insert;
else if (strcmp(bname, ABBR_BufName) == 0)
mp = abbr_map;
else if (strcmp(bname, SYSMAP_BufName) == 0)
mp = map_syskey;
else
return FALSE;
return liststuff(bname, FALSE, makemaplist, 0, (void *) mp);
}
#if OPT_UPBUFF
static int
show_Mappings(BUFFER *bp)
{
b_clr_obsolete(bp);
return show_mapped_chars(bp->b_bname);
}
#undef relist_mappings
static void
relist_mappings(const char *bufname)
{
update_scratch(bufname, show_Mappings);
}
#endif /* OPT_UPBUFF */
#endif /* OPT_SHOW_MAPS */
static int
addtomap(struct maprec **mpp,
const char *ks,
int kslen,
UINT flags,
int irv,
char *srv)
{
int status = TRUE;
struct maprec *mp = NULL;
if (ks != NULL && kslen != 0) {
TRACE2(("addtomap %s\n", visible_buff(ks, kslen, FALSE)));
while (*mpp && kslen) {
mp = *mpp;
mp->flags |= flags;
if (CharOf(*ks) == mp->ch) {
mpp = &mp->dlink;
ks++;
kslen--;
} else
mpp = &mp->flink;
}
while (kslen) {
mp = typealloc(struct maprec);
if (mp == NULL) {
status = no_memory("addtomap");
break;
}
*mpp = mp;
mp->dlink = mp->flink = NULL;
TRACE2(("...adding 0x%X\n", CharOf(*ks)));
mp->ch = CharOf(*ks++);
mp->srv = NULL;
mp->flags = flags;
mp->irv = -1;
mpp = &mp->dlink;
kslen--;
}
if (mp != NULL) {
if (irv != -1)
mp->irv = irv;
if (srv) {
if (mp->srv)
free(mp->srv);
mp->srv = strmalloc(srv);
TRACE2(("...addtomap %s\n", visible_buff(srv, strlen(srv), FALSE)));
}
mp->flags = flags;
}
}
return status;
}
static int
delfrommap(struct maprec **mpp, const char *ks, int length)
{
struct maprec **save_m = mpp;
struct maprec ***mstk = NULL;
int depth = 0;
int pass;
int n;
if (mpp == NULL || ks == NULL || length == 0)
return FALSE;
for (pass = 0; pass < 2; pass++) {
mpp = save_m;
depth = 0;
n = 0;
while (*mpp && n < length) {
if (pass)
mstk[depth] = mpp;
if ((*mpp)->ch == CharOf(ks[n])) {
mpp = &(*mpp)->dlink;
n++;
depth++;
} else
mpp = &(*mpp)->flink;
}
if (n < length) {
free(mstk);
return FALSE; /* not in map */
}
if (!pass) {
mstk = typecallocn(struct maprec **, (size_t) depth + 1);
if (mstk == NULL)
return no_memory("delfrommap");
}
}
depth--;
if ((*mstk[depth])->srv) {
free((*mstk[depth])->srv);
(*mstk[depth])->srv = NULL;
} else if ((*mstk[depth])->irv != -1) {
(*mstk[depth])->irv = -1;
} else {
free((char *) mstk);
return FALSE;
}
for (; depth >= 0; depth--) {
struct maprec *mp = *mstk[depth];
if (mp->irv == -1 && mp->dlink == NULL && mp->srv == NULL) {
*mstk[depth] = mp->flink;
if (depth > 0 && (*mstk[depth - 1])->dlink == mp)
(*mstk[depth - 1])->dlink = NULL;
free((char *) mp);
} else
break;
}
free((char *) mstk);
return TRUE;
}
/* do map translations.
* C is first char to begin mapping on
* OUTP is an ITBUFF in which to put the result
* MP is the map in which to look
* GET is a routine to use to get the next character
* AVAIL is the routine that tells if GET can return something now
* returns number of characters matched
*/
static int
maplookup(int c,
ITBUFF ** out_ptr,
struct maprec *mp,
GetFunc get,
AvailFunc avail,
StartFunc start,
int suffix)
{
struct maprec *rmp = NULL;
ITBUFF *unmatched = NULL;
int matchedcnt;
int use_sys_timing;
int had_start = FALSE;
register int count = 0; /* index into 'unmatched[]' */
/*
* We don't want to delay for a user-specified :map! starting with
* poundc since it's likely that the mapping is happening on behalf of
* a function key. (It's so the user can ":map! #1 foobar" but still be
* able to insert a '#' character normally.) If they've changed poundc
* so it's not something one normally inserts, then it's okay to delay
* on it.
*/
use_sys_timing = (insertmode && c == poundc &&
(isPrint(poundc) || isSpace(poundc)));
unmatched = itb_init(&unmatched, 0);
itb_append(&unmatched, c);
count++;
TRACE2(("maplookup unmatched:%s\n", itb_visible(unmatched)));
matchedcnt = 0;
while (mp != NULL) {
TRACE2(("... c=0x%X, mp->ch=0x%X\n", c, mp->ch));
if (c == mp->ch) {
if (mp->irv != -1 || mp->srv != NULL) {
rmp = mp;
matchedcnt += count;
unmatched = itb_init(&unmatched, 0);
count = 0;
/* our code supports matching the longer of two maps one of
* which is a subset of the other. vi matches the shorter
* one.
*/
if ((*start) ()) {
had_start = TRUE;
if (!global_g_val(GMDMAPLONGER)) {
break;
}
}
}
mp = mp->dlink;
if (!mp)
break;
/* if there's no recorded input, and no user typeahead */
if (!(*avail) ()) {
/* give it a little extra time... */
int timer = 0;
#if DISP_NTWIN
int cursor_state = 0;
#endif
/* we want to use the longer of the two timers */
/* get the user timer. it may be zero */
if (!use_sys_timing && (mp->flags & MAPF_USERTIMER) != 0)
timer = global_g_val(GVAL_TIMEOUTUSERVAL);
/* if there was no user timer, or this is a system
sequence, use the system timer if it's bigger */
if (timer == 0 || (mp->flags & MAPF_SYSTIMER) != 0) {
if (timer < global_g_val(GVAL_TIMEOUTVAL))
timer = global_g_val(GVAL_TIMEOUTVAL);
}
#if DISP_NTWIN
if (timer > NTWIN_CURSOR_MINWAIT) {
/*
* editor waits for a potentially long time for the next
* character, during which the cursor will be invisible; so
* turn it back on. unfortunately, the cursor won't blink
* because there's no thread driving the message pump.
*/
cursor_state = winvile_cursor_state(TRUE, FALSE);
}
#endif
catnap(timer, TRUE);
#if DISP_NTWIN
if (timer > NTWIN_CURSOR_MINWAIT) /* restore cursor state */
(void) winvile_cursor_state(cursor_state, FALSE);
#endif
if (!(*avail) ())
break;
}
if ((c = (*get) ()) < 0)
break;
itb_append(&unmatched, STRIP_REMAPFLAGS(c));
count++;
} else
mp = mp->flink;
}
if (itb_values(unmatched) == NULL) {
matchedcnt = 0;
no_memory("maplookup");
} else if (had_start && (rmp != NULL)) {
/* unget the unmatched suffix */
while (suffix && (count > 0)) {
(void) itb_append(out_ptr, itb_values(unmatched)[--count]);
TRACE2(("...1 ungetting 0x%X\n", itb_values(unmatched)[count]));
}
/* unget the mapping and elide correct number of recorded chars */
if (rmp->srv) {
UINT remapflag;
char *cp;
/* cp = rmp->srv + cnt; */
for (cp = rmp->srv; *cp; cp++) ;
if (rmp->flags & MAPF_NOREMAP)
remapflag = NOREMAP;
else
remapflag = 0;
while (cp > rmp->srv) {
(void) itb_append(out_ptr, CharOf(*--cp) | (int) remapflag);
TRACE2(("...2 ungetting 0x%X|0x%X\n", CharOf(*cp), remapflag));
}
} else {
(void) itb_append(out_ptr, rmp->irv);
TRACE2(("...3 ungetting 0x%X\n", rmp->irv));
}
} else { /* didn't find a match */
while (count > 0) {
(void) itb_append(out_ptr, itb_values(unmatched)[--count]);
TRACE2(("...4 ungetting 0x%X\n", itb_values(unmatched)[count]));
}
matchedcnt = 0;
}
itb_free(&unmatched);
TRACE2(("...maplookup %d:%s\n", matchedcnt, itb_visible(*out_ptr)));
return matchedcnt;
}
static void
reverse_abbr(struct maprec **mpp, const char *bufname, TBUFF *kbuf)
{
if ((mpp && *mpp && *mpp == abbr_map) ||
(strcmp(bufname, ABBR_BufName) == 0)) {
/* reverse the lhs */
int i;
char t;
int len = tb_length0(kbuf);
char *s = tb_values(kbuf);
for (i = 0; i < len / 2; i++) {
t = s[len - i - 1];
s[len - i - 1] = s[i];
s[i] = t;
}
}
}
static int
map_common(struct maprec **mpp, const char *bufname, UINT remapflag)
{
int status;
static TBUFF *kbuf;
static TBUFF *val;
#if OPT_SHOW_MAPS
if (end_named_cmd()) {
status = show_mapped_chars(bufname);
} else
#endif
{
tb_scopy(&kbuf, "");
status = kbd_reply("change this string: ", &kbuf, eol_history,
' ', KBD_NOMAP | KBD_NOEVAL, no_completion);
if (status == TRUE) {
hst_glue(' ');
tb_scopy(&val, "");
if (!clexec) {
status = kbd_reply("to this new string: ", &val, eol_history,
'\n', KBD_NOMAP, no_completion);
} else {
(void) mac_literalarg(&val); /* consume to end of line */
status = tb_length(val) > 1;
}
if (status == TRUE) {
reverse_abbr(mpp, bufname, kbuf);
status = addtomap(mpp, tb_values(kbuf), tb_length0(kbuf),
MAPF_USERTIMER | remapflag, -1, tb_values(val));
relist_mappings(bufname);
}
}
}
return status;
}
static int
unmap_common(struct maprec **mpp, const char *bufname)
{
int status;
static TBUFF *kbuf;
/* otherwise it'll be mapped, and not found when looked up */
if (mpp && mpp == &map_syskey)
suppress_sysmap = TRUE;
tb_scopy(&kbuf, "");
status = kbd_reply("unmap string: ", &kbuf, eol_history,
' ', KBD_NOMAP, no_completion);
suppress_sysmap = FALSE;
if (status != TRUE)
return status;
reverse_abbr(mpp, bufname, kbuf);
if (delfrommap(mpp, tb_values(kbuf), tb_length0(kbuf)) != TRUE) {
mlforce("[Sequence not mapped]");
return FALSE;
}
relist_mappings(bufname);
return TRUE;
}
/*
* set a map for the character/string combo
*/
/* ARGSUSED */
int
map(int f GCC_UNUSED, int n GCC_UNUSED)
{
return map_common(&map_command, MAP_BufName, 0);
}
/* ARGSUSED */
int
map_bang(int f GCC_UNUSED, int n GCC_UNUSED)
{
return map_common(&map_insert, MAPBANG_BufName, 0);
}
/* ARGSUSED */
int
noremap(int f GCC_UNUSED, int n GCC_UNUSED)
{
return map_common(&map_command, MAP_BufName, MAPF_NOREMAP);
}
/* ARGSUSED */
int
noremap_bang(int f GCC_UNUSED, int n GCC_UNUSED)
{
return map_common(&map_insert, MAPBANG_BufName, MAPF_NOREMAP);
}
/* ARGSUSED */
int
abbrev(int f GCC_UNUSED, int n GCC_UNUSED)
{
return map_common(&abbr_map, ABBR_BufName, MAPF_NOREMAP);
}
#if OPT_SHOW_MAPS
/* ARGSUSED */
int
sysmap(int f GCC_UNUSED, int n GCC_UNUSED)
{
return show_mapped_chars(SYSMAP_BufName);
}
#endif
/*
* remove map entry, restore saved CMDFUNC for key
*/
/* ARGSUSED */
int
unmap(int f GCC_UNUSED, int n GCC_UNUSED)
{
return unmap_common(&map_command, MAP_BufName);
}
/* ARGSUSED */
int
unmap_bang(int f GCC_UNUSED, int n GCC_UNUSED)
{
return unmap_common(&map_insert, MAPBANG_BufName);
}
/* ARGSUSED */
int
unmap_system(int f GCC_UNUSED, int n GCC_UNUSED)
{
return unmap_common(&map_syskey, SYSMAP_BufName);
}
/* ARGSUSED */
int
unabbr(int f GCC_UNUSED, int n GCC_UNUSED)
{
return unmap_common(&abbr_map, ABBR_BufName);
}
/* addtosysmap is used to initialize the system default function key map
*/
void
addtosysmap(const char *seq, int seqlen, int code)
{
addtomap(&map_syskey, seq, seqlen, MAPF_SYSTIMER, code, (char *) 0);
relist_mappings(SYSMAP_BufName);
}
void
delfromsysmap(const char *seq, int seqlen)
{
delfrommap(&map_syskey, seq, seqlen);
relist_mappings(SYSMAP_BufName);
}
#define INPUT_FROM_TTGET 1
#define INPUT_FROM_MAPGETC 2
static ITBUFF *sysmappedchars = NULL;
static ITBUFF *mapgetc_ungottenchars = NULL;
static int mapgetc_ungotcnt = 0;
static void
save_keystroke(int c)
{
KILL *kp;
KILLREG *kr = &kbs[KEYST_KREG];
beginDisplay();
#ifdef DOKEYLOG
if (do_keylog) {
static int keyfd = -1;
static char *tfilenam;
if (!tfilenam)
tfilenam = tempnam("/tmp/vilekeylogs", "vilek");
if (tfilenam) {
if (keyfd < 0)
keyfd = open(tfilenam, O_CREAT | O_WRONLY, 0600);
if (keyfd >= 0)
write(keyfd, &c, 1);
}
}
#endif
if (kr->kbufh == NULL) {
kr->kbufh = typealloc(KILL);
kr->kused = 0;
}
if (kr->kbufh != NULL) {
kp = kr->kbufp = kr->kbufh;
kp->d_next = NULL;
kp->d_chunk[kr->kused++] = (UCHAR) c;
if (kr->kused >= NUMKEYSTR * 2) { /* time to dump the oldest half */
(void) memcpy((kp->d_chunk),
(&kp->d_chunk[NUMKEYSTR / 2]),
(size_t) (NUMKEYSTR / 2));
kr->kused = NUMKEYSTR / 2;
}
}
endofDisplay();
}
/* These two wrappers are provided because at least one pcc-based compiler
* balks at passing term.getch or term.typahead as a function pointer.
*/
static int
normal_getc(void)
{
int c = term.getch();
TRACE(("normal/getc:%c (0x%X)\n", c, c));
save_keystroke(c);
return c;
}
static int
normal_typeahead(void)
{
return (term.typahead());
}
static int
normal_start(void)
{
return TRUE;
}
int
sysmapped_c(void)
{
int c;
/* still some left? */
if (itb_more(sysmappedchars))
return itb_last(sysmappedchars);
if ((c = term.getch()) == -1)
return c; /* see comment in ttgetc */
TRACE(("mapped/getc:%c (0x%X)\n", c, c));
#if OPT_MULTIBYTE
if (global_g_val(GMDMINI_MAP) && !is8Bits(c)) {
TRACE(("reading_msg_line %d\n", reading_msg_line));
TRACE(("insertmode %d\n", insertmode));
if (!reading_msg_line && !insertmode) {
UCHAR temp[MAX_UTF8];
int j;
int rc;
rc = vl_conv_to_utf8(temp, (UINT) c, (B_COUNT) MAX_UTF8);
if (rc > 1) {
TRACE(("...push back UTF-8 encoding\n"));
for (j = 1; j < rc; ++j) {
TRACE(("...pushing back 0x%02x\n", temp[j]));
itb_append(&sysmappedchars, temp[j]);
}
c = temp[0];
TRACE(("...proceed with 0x%02x\n", temp[0]));
}
}
}
#endif
save_keystroke(c);
if (suppress_sysmap)
return c;
/* will push back on sysmappedchars successful, or not */
(void) maplookup(c, &sysmappedchars, map_syskey,
normal_getc, normal_typeahead, normal_start, TRUE);
return itb_last(sysmappedchars);
}
int
sysmapped_c_avail(void)
{
return itb_more(sysmappedchars) || (!mapgetc_ungotcnt && term.typahead());
}
/*
* Make the assumption that no input will magically appear (un)available to
* tgetc in between a mapungetc and the next mapgetc. Hence characters can't
* be ungotten onto the wrong buffer (exception is the last tgot char might be
* mapungot onto the map buffer. This is OK (if assumption holds) because the
* next character will be gotten from this buffer.
*/
void
mapungetc(int c)
{
TRACE2(("mapungetc 0x%X\n", c));
if (tgetc_avail()) {
tungetc(c);
} else {
(void) itb_append(&mapgetc_ungottenchars, c);
mapgetc_ungotcnt++;
}
}
static int infloopcount;
static int mapgetc_raw_flag;
static int
mapgetc(void)
{
int result;
UINT remapflag;
TRACE((T_CALLED "mapgetc\n"));
if (global_g_val(GMDREMAP))
remapflag = 0;
else
remapflag = NOREMAP;
if (!tgetc_avail() && mapgetc_ungotcnt > 0) {
++infloopcount;
if (infloopcount > global_g_val(GVAL_MAPLENGTH)
&& infloopcount > 25) {
(void) itb_init(&mapgetc_ungottenchars, esc_c);
mapgetc_ungotcnt = 0;
mlforce("[Infinite loop detected in %s sequence]",
(insertmode) ? "map!" : "map");
catnap(1000, FALSE); /* FIXME: be sure message gets out */
result = esc_c | (int) NOREMAP;
} else {
mapgetc_ungotcnt--;
result = itb_last(mapgetc_ungottenchars) | (int) remapflag;
}
} else {
infloopcount = 0;
result = tgetc(mapgetc_raw_flag);
}
returnCode(result);
}
int
mapped_c_avail(void)
{
return mapgetc_ungotcnt > 0 || tgetc_avail();
}
int
mapped_ungotc_avail(void)
{
return mapgetc_ungotcnt > 0;
}
void
map_drain(void)
{
int ch;
while (mapped_ungotc_avail()) {
ch = mapgetc();
TRACE(("map_drain...\n"));
(void) ch;
}
}
static int
mapped_c_start(void)
{
return TRUE;
}
int
mapped_c(int remap, int raw)
{
int c;
int matched;
struct maprec *mp;
int speckey = FALSE;
static ITBUFF *mappedchars = NULL;
TRACE((T_CALLED "mapped_c(remap:%d, raw:%d)\n", remap, raw));
/* still some pushback left? */
mapgetc_raw_flag = raw;
c = mapgetc();
if (((UINT) c & YESREMAP) == 0 && (!remap || ((UINT) c & NOREMAP)))
returnCode(STRIP_REMAPFLAGS(c));
c = STRIP_REMAPFLAGS(c);
if (reading_msg_line) {
mp = NULL;
TRACE(("...using no mapping\n"));
} else if (insertmode) {
mp = map_insert;
TRACE(("...using insert-mapping\n"));
} else {
mp = map_command;
TRACE(("...using command-mapping\n"));
}
/* if we got a function key from the lower layers, turn it into '#c'
and see if the user remapped that */
if (((UINT) c & SPEC)
#if OPT_KEY_MODIFY
&& !((UINT) c & mod_KEY) /* we don't do this special case */
#endif
) {
mapungetc(kcod2key(c));
c = poundc;
speckey = TRUE;
}
do {
/*
* This is a workaround for an ambiguity between the different return
* values from mapgetc(), on entry to this function:
* - If infloopcount is zero, mapgetc() has just called tgetc(), which
* in turn called sysmapped_c(), and then term.getc(). That pointer
* is initialized to vl_mb_getch(), which will decode UTF-8 into a
* Unicode value.
* - If infloopcount is nonzero, the function returns a byte (of UTF-8),
* which must be decoded later in this function.
*/
int full_c = (infloopcount == 0); /* tgetc returned full char? */
(void) itb_init(&mappedchars, esc_c);
matched = maplookup(c,
&mappedchars,
mp,
mapgetc,
mapped_c_avail,
mapped_c_start,
TRUE);
while (itb_more(mappedchars)) {
int cc = itb_next(mappedchars);
TRACE(("...ungetting %d\n", cc));
mapungetc(cc);
}
/* if the user has not mapped '#c', we return the wide code we got
in the first place. unless they wanted it quoted. then we
leave it as is */
if (!raw && speckey && !matched) {
c = STRIP_REMAPFLAGS(mapgetc());
if (c != poundc)
dbgwrite("BUG: # problem in mapped_c");
returnCode((STRIP_REMAPFLAGS(mapgetc()) | (int) SPEC));
}
c = mapgetc();
#if OPT_MULTIBYTE
if (remap && !full_c && (vl_encoding >= enc_UTF8 && b_is_utfXX(curbp))) {
char save[MAX_UTF8];
int have = 0;
int need = 9;
UINT utf8;
do {
save[have++] = (char) c;
need = vl_check_utf8(save, (B_COUNT) have);
if (need <= have)
break;
c = mapgetc();
TRACE(("...re-getting %d\n", c));
} while (c > 127);
if ((have > 1) &&
vl_conv_to_utf32(&utf8, save, (B_COUNT) have) == have) {
c = (int) utf8;
}
}
#endif
if (!global_g_val(GMDREMAPFIRST))
matched = FALSE;
speckey = FALSE;
} while (matched &&
((remap && !((UINT) c & NOREMAP)) || ((UINT) c & YESREMAP)));
returnCode(STRIP_REMAPFLAGS(c));
}
#if OPT_MULTIBYTE
static ITBUFF *u2b_input;
static int
u2b_mapgetc(void)
{
return itb_next(u2b_input);
}
static int
u2b_mapped_c_avail(void)
{
return itb_more(u2b_input);
}
static int
u2b_mapped_c_start(void)
{
return TRUE;
}
/*
* Check if the given character maps (using the command-mapping) to one byte.
* If so, return that (i.e., in the range 0..255). Otherwise, return -1.
*/
int
map_to_command_char(int c)
{
UCHAR temp[MAX_UTF8];
int used = vl_conv_to_utf8(temp, (UINT) c, sizeof(temp));
if (used > 1) {
int j;
int matched;
static ITBUFF *u2b_output;
(void) itb_init(&u2b_input, esc_c);
(void) itb_init(&u2b_output, esc_c);
for (j = 0; j < used; ++j) {
itb_append(&u2b_input, temp[j]);
}
matched = maplookup(u2b_mapgetc(),
&u2b_output,
map_command,
u2b_mapgetc,
u2b_mapped_c_avail,
u2b_mapped_c_start,
TRUE);
if (matched == used && itb_length(u2b_output) == 1) {
c = itb_values(u2b_output)[0];
}
}
return c;
}
#endif
static int abbr_curr_off;
static int abbr_search_lim;
static int
abbr_getc(void)
{
if (abbr_curr_off <= abbr_search_lim)
return -1; /* won't match anything in the tree */
return lgetc(DOT.l, --abbr_curr_off) & 0xff;
}
static int
abbr_c_avail(void)
{
return TRUE;
}
static int
abbr_c_start(void)
{
if (abbr_curr_off > abbr_search_lim) {
/*
* We need to check the char in front of the match. If it's a similar
* type to the first char of the match, i.e. both idents, or both
* non-idents, we do nothing. If whitespace precedes either ident or
* non-ident, the match is good.
*/
int first, prev;
first = lgetc(DOT.l, abbr_curr_off);
prev = lgetc(DOT.l, abbr_curr_off - 1);
if ((isident(first) && isident(prev)) ||
(!isident(first) && !(isident(prev) || isSpace(prev)))) {
return FALSE;
}
}
return TRUE;
}
void
abbr_check(int *backsp_limit_p)
{
int matched;
ITBUFF *abbr_chars = NULL;
int status = TRUE;
if (llength(DOT.l) < 1)
return;
abbr_curr_off = DOT.o;
abbr_search_lim = *backsp_limit_p;
(void) itb_init(&abbr_chars, esc_c);
matched = maplookup(abbr_getc(), &abbr_chars, abbr_map,
abbr_getc, abbr_c_avail, abbr_c_start, FALSE);
if (matched) {
/* there are still some conditions that have to be met by
the preceding chars, if any */
if (!abbr_c_start()) {
itb_free(&abbr_chars);
return;
}
DOT.o -= matched;
ldel_bytes((B_COUNT) matched, FALSE);
while (status && itb_more(abbr_chars)) {
status = inschar(itb_last(abbr_chars), backsp_limit_p);
}
}
itb_free(&abbr_chars);
return;
}
#if NO_LEAKS
static void
free_maprec(struct maprec **p)
{
struct maprec *q;
if ((q = *p) != NULL) {
free_maprec(&(q->flink));
free_maprec(&(q->dlink));
FreeAndNull(q->srv);
*p = NULL;
free((char *) q);
}
}
void
map_leaks(void)
{
free_maprec(&map_command);
free_maprec(&map_insert);
free_maprec(&map_syskey);
free_maprec(&abbr_map);
}
#endif /* NO_LEAKS */
|