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 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
|
/*-
* tirc -- client for the Internet Relay Chat
*
* Copyright (c) 1996, 1997, 1998
* Matthias Buelow. All rights reserved.
*
* See the file ``COPYRIGHT'' for the usage and distribution
* license and warranty disclaimer.
*/
#ifndef lint
static char rcsid[] = "$Id: input.c,v 1.36 1998/02/20 21:01:44 token Exp $";
#endif
#include "tirc.h"
#ifdef HAVE_STRING_H
#include <string.h>
#elif defined(HAVE_MEMORY_H)
#include <memory.h>
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#elif defined(HAVE_SYS_SIGNAL_H)
#include <sys/signal.h>
#endif
#include "compat.h"
#include "tty.h"
#include "colour.h"
#define ESC 0x1b
static int forww __P((void));
static int backw __P((void));
static int endw __P((void));
static int forwW __P((void));
static int backW __P((void));
static int endW __P((void));
static void addtohist __P((unsigned char *));
static void delfromhist __P((void));
static void killtrailws __P((void));
static void idlecheck __P((void));
static char *lastnick __P((unsigned char **));
extern char lastmsg[MSGNAMEHIST][NICKSZ+2];
extern int lastmsgp;
extern char cmdch;
extern int pasting; /* cmd.c */
struct aline *ela, *elc, *ell;
int lsz, lptr, history;
static int del_from, del_to;
static int mode, del, chg, yank, mark, gotomark, find, dodel, literal,
replone;
static unsigned char *line;
static struct p_queue *pq_idle = NULL;
static time_t laststroke;
static int in_lastmsg; /* whether TAB can produce a reply */
static int lastmsgcomp;
static int freehist_registered;
static int cx_pending;
static int pgnum_pending;
static char *undobuf;
static char *cutbuf;
static int cutsize;
static char casetbl['z'-'A']; /* table for toggling case */
static int marktbl[1+'z'-'a'];
/*
* Reset all variables for the line input.
*/
void
reset_input()
{
extern sig_atomic_t atomic_idleexceed;
int i;
del_from = del_to = lsz = lptr = del = chg = find = history = 0;
dodel = literal = cx_pending = pgnum_pending = replone = 0;
ela = elc = ell = NULL;
if (NULL != line)
free(line);
line = chkmem(malloc(MAXINPUT+1));
mode = MODE_INSERT;
showmode(mode);
laststroke = time(NULL);
pq_del(pq_idle);
atomic_idleexceed = 0;
pq_idle = pq_add(idlecheck);
elhome();
if (!freehist_registered) {
Atexit(free_history);
freehist_registered = 1;
}
if (undobuf == NULL)
undobuf = chkmem(malloc(MAXINPUT+1));
if (cutbuf == NULL)
cutbuf = chkmem(malloc(MAXINPUT+1));
cutsize = 0;
/* Creating case toggle table */
for (i = 'A'; i <= 'z' ; i++) {
if (isupper(i))
casetbl[i-'A'] = tolower(i);
else if (islower(i))
casetbl[i-'A'] = toupper(i);
else
casetbl[i-'A'] = i;
}
/* clearing mark table */
for (i = 0; i <= 'z'-'a'; i++)
marktbl[i] = -1;
}
/*
* Process user input. This implements an editor line with commands
* and handling similar to the vi(1) text editor. For emacs users,
* some basic stuff is supported. Don't count the gotos, please.
*/
void
parsekey()
{
int t, c;
static int undolsz, undolptr;
static int omode;
char undotmp[MAXINPUT+1];
c = tty_getch();
laststroke = time(NULL);
if (mark) {
/* Pending set named mark */
mark = 0;
if (c >= 'a' && c <= 'z')
marktbl[c] = lptr;
else
dobell();
yank = chg = del = 0;
return;
}
if (gotomark) {
/* Pending go to named mark */
gotomark = 0;
if (c >= 'a' && c <= 'z' && marktbl[c] > 0
&& marktbl[c] < lsz) {
int lptrsv = lptr;
lptr = marktbl[c];
if (yank || chg || del) {
dodel = 1;
if (lptrsv <= lptr) {
del_from = lptrsv;
del_to = lptr-1;
}
else {
del_from = lptr;
del_to = lptrsv-1;
}
goto check4del;
}
else
update_eline(line, lsz, lptr, 0);
}
else {
yank = chg = del = 0;
dobell();
}
return;
}
if (cx_pending) {
/* Pending Ctrl-X */
cx_pending = 0;
switch (c) {
case 'o': /* switch to next window */
switchwin(1);
elrefr(1);
return;
case 'O': /* switch to previous window */
switchwin(-1);
elrefr(1);
return;
case '0': /* delete this window */
delircwin();
elrefr(1);
return;
case '2': /* create new window */
newircwin();
elrefr(1);
iwcmode(iwcmode(-1) | IMT_CTRL | IMT_PMSG);
return;
case 'b': /* switch to page */
pgnum_pending = 1;
ipg_pgline();
return;
case 'n': /* next page */
{
int pg = ipg_getpageno();
if (ipg_switchto(pg + 1, 1))
repinsel();
else if (pg > 0) {
ipg_switchto(0, 0);
repinsel();
}
return;
}
case 'p': /* previous page */
if (ipg_switchto(ipg_getpageno() - 1, 1))
repinsel();
else {
ipg_switchto(ipg_lastpageno(), 0);
repinsel();
}
return;
case K_CTRL('X'): /* other page */
if (ipg_switchto(ipg_setget_otherpg(-1), 1))
repinsel();
else {
ipg_switchto(0, 0);
repinsel();
}
return;
default:
dobell();
elrefr(1);
return;
}
}
if (pgnum_pending) {
/* Pending input for ^X-b */
pgnum_pending = 0;
if (!isdigit(c)) {
dobell();
elrefr(1);
return;
}
if (ipg_switchto(c - '0', 0))
repinsel();
return;
}
if (literal == MODE_OSTRIKE)
goto osliteral;
if (literal == MODE_INSERT)
goto inliteral;
if (c != '\t')
in_lastmsg = 0;
if (c == tck_reprint)
goto reprint;
if ((c == tck_kill) && (mode == MODE_OSTRIKE || mode == MODE_INSERT)) {
/* Kill to start of line. */
killtrailws();
memcpy(undobuf, line, (unsigned)lsz);
undolsz = lsz;
undolptr = lptr;
if (lptr < lsz-1) {
memmove(line, line + lptr, (unsigned)lsz-lptr);
lsz = lsz - lptr;
lptr = 0;
}
else
lsz = lptr = 0;
line[lsz] = 0;
update_eline(line, lsz, lptr, 0);
return;
}
switch (c) {
case K_CTRL('W'): /* next window if in command mode */
if (mode == MODE_COMMAND) {
switchwin(1);
return;
}
else {
del = 1;
chg = yank = 0;
goto word_back;
}
case K_CTRL('N'): /* turin */
switchwin(1);
elrefr(1);
return;
case K_CTRL('T'): /* prev window if in command mode */
if (mode == MODE_COMMAND)
switchwin(-1);
return;
case K_CTRL('X'): /* emacs ^X command */
tty_gotoxy(0, t_lines-1);
tty_printf(NULL, "%sWindow: ^X-o, ^X-O, ^X-2, ^X-0; Page: \
^X-b, ^X-n, ^X-p, ^X-^X%s%s", tc_bold, tc_noattr, tc_clreol);
tty_flush();
cx_pending = 1;
return;
case K_CTRL('F'): /* scroll down window by a screen */
case K_PGDN:
case K_S_PGDN:
case K_S2_PGDN:
case K_S3_PGDN:
iw_scroll(1000, 1);
return;
case K_CTRL('U'): /* unlike vi, this is equ to ctrl-b */
case K_CTRL('B'): /* scroll up window by a screen */
case K_PGUP:
case K_S_PGUP:
case K_S2_PGUP:
case K_S3_PGUP:
iw_scroll(1000, -1);
return;
case K_CTRL('G'): /* go to end of backscroll */
iw_scroll(0, 0);
return;
case K_CTRL('L'):
reprint: /* force screen redraw */
repinsel();
return;
case K_UP: /* cursor keys */
case K_S_UP:
case K_S2_UP:
goto histprev;
case K_DN:
case K_S_DN:
case K_S2_DN:
goto histnext;
case K_LEFT:
case K_S_LEFT:
case K_S2_LEFT:
goto cursleft;
case K_RIGHT:
case K_S_RIGHT:
case K_S2_RIGHT:
goto cursright;
case K_DEC_FIND:
search(BACKWARD);
return;
case K_CTRL('D'): /* unlike vi, this is emacish delete-char */
goto delcurrent;
case K_CTRL('E'): /* (emacsish) goto after end of line */
if (lsz)
lptr = lsz-1;
if (!isspace(line[lptr]) && lsz < MAXINPUT) {
line[lsz] = ' ';
lptr++;
lsz++;
}
mode = MODE_INSERT;
update_eline(line, lsz, lptr, 0);
goto skip_mode_checks;
case K_CTRL('A'): /* (emacsish) goto beginning of line */
lptr = 0;
mode = MODE_INSERT;
update_eline(line, lsz, lptr, 0);
goto skip_mode_checks;
case K_CTRL('K'): /* (emacsish) delete to end of line */
goto deltoeol;
case K_CTRL('P'): /* toggle paste mode */
del = chg = find = 0;
pastemode();
return;
case '\r':
case '\n': /* line done */
linedone(1);
return;
case '\t':
/*
* Do not interpret TAB if paste mode is active.
* TAB is inserted as a single space.
*/
if (pasting) {
c = '\040';
goto inliteral;
}
/*
* TAB faciliates responding to the last message you
* received. If pressed somewhere else, it tries to
* complete the current word as a nickname of the
* current channel.
*/
if (!in_lastmsg)
lastmsgcomp = lastmsgp;
if (lsz == 0 || in_lastmsg) {
if (--lastmsgcomp < 0)
lastmsgcomp = MSGNAMEHIST-1;
if (strlen(lastmsg[lastmsgcomp]) > 0)
sprintf(line, "%cmsg %s ", cmdch,
lastmsg[lastmsgcomp]);
else
sprintf(line, "%cmsg ", cmdch);
lptr = lsz = strlen(line);
mode = MODE_INSERT;
update_eline(line, lsz, lptr, 0);
in_lastmsg = 1;
}
else {
unsigned char *xpand, *w;
struct channel *ch;
unsigned char *lw;
if (mode == MODE_COMMAND) {
dobell();
return;
}
del = chg = find = 0;
ch = iw_getchan();
if (ch != NULL) {
register int xlen;
w = lastnick(&lw);
xpand = complete_from_ucache(ch, w,
(lw == skipws(line)));
if (xpand == NULL) {
dobell();
return;
}
if (lsz + strlen(xpand) >= MAXINPUT) {
dobell();
return;
}
xlen = strlen(xpand);
memmove(lw+xlen, line+lptr,
(unsigned)lsz-(lptr-xlen));
memmove(lw, xpand, (unsigned)xlen);
lsz += xlen - strlen(w);
lptr = (lw+xlen) - line;
update_eline(line, lsz, lptr, 0);
if (w != NULL)
free(w);
}
else
dobell();
}
return;
}
if (K_ISFKEY(c)) {
del = chg = find = 0;
dobell();
return;
}
/* Find character? */
if (find > 0) {
int oldptr = lptr;
del_from = lptr;
while (++lptr < lsz-1 && line[lptr] != c)
;
if (line[lptr] != c)
lptr = oldptr;
del_to = lptr;
find = 0;
if ((del || chg) && (del_from != del_to))
dodel = 1;
del = 0;
if (yank)
lptr = oldptr;
else
update_eline(line, lsz, lptr, 0);
goto check4del;
}
else if (find < 0) {
int oldptr = lptr;
if (!lptr)
goto check4del;
del_to = lptr-1;
while (--lptr > 0 && line[lptr] != c)
;
if (line[lptr] != c)
lptr = oldptr;
del_from = lptr;
find = 0;
if (del || chg)
dodel = 1;
if (yank)
lptr = oldptr;
else
update_eline(line, lsz, lptr, 0);
goto check4del;
}
if (mode == MODE_INSERT) {
if (c == tck_lnext) {
literal = MODE_INSERT;
return;
}
if (c == tck_erase || c == '\x7f' || c == '\x8')
goto delbackc;
switch (c) {
case ESC:
mode = omode = MODE_COMMAND;
showmode(mode);
if (lptr && lptr < lsz-1)
lptr--;
else
killtrailws();
update_eline(line, lsz, lptr, 0);
break;
default:
inskey:
if (!isprint(c)) {
dobell();
break;
}
inliteral:
literal = 0;
if (lsz >= MAXINPUT) {
dobell();
break;
}
memmove(line+lptr+1, line+lptr, (unsigned)lsz-lptr);
line[lptr++] = c;
lsz++;
update_eline(line, lsz, lptr, 0);
break;
}
}
else if (mode == MODE_OSTRIKE) {
if (c == tck_lnext) {
literal = MODE_OSTRIKE;
return;
}
switch (c) {
case ESC:
mode = omode = MODE_COMMAND;
showmode(mode);
if (lptr)
lptr--;
update_eline(line, lsz, lptr, 0);
break;
default:
if (!isprint(c)) {
dobell();
break;
}
osliteral:
literal = 0;
if (lptr < lsz) {
line[lptr] = c;
if (!replone)
lptr++;
replone = 0;
update_eline(line, lsz, lptr, 0);
break;
}
else {
mode = MODE_INSERT;
goto inskey;
}
}
}
else if (mode == MODE_COMMAND) {
if (c == tck_erase || c == '\x7f' || c == '\x8')
goto cursleft;
switch (c) {
case 'h': /* cursor left */
cursleft:
if (lptr) {
if (yank) {
*cutbuf = line[lptr-1];
cutsize = 1;
yank = del = 0;
return;
}
if (del)
goto delbackc;
else
lptr--;
}
else
dobell();
del = chg = yank = 0;
update_eline(line, lsz, lptr, 0);
break;
case 'l': /* cursor right */
case ' ':
cursright:
if (lptr < lsz-1) {
if (yank) {
*cutbuf = line[lptr+1];
cutsize = 1;
yank = del = 0;
return;
}
if (del)
goto delcurrent;
else
lptr++;
}
else
dobell();
del = chg = yank = 0;
update_eline(line, lsz, lptr, 0);
break;
case 'a': /* insert after current pos */
if (lsz) {
lptr++;
if (lptr > lsz)
lsz = lptr+1;
}
mode = MODE_INSERT;
del = chg = yank = 0;
update_eline(line, lsz, lptr, 0);
break;
case 'A': /* append at end of line */
if (lsz)
lptr = lsz++;
line[lptr] = ' ';
mode = MODE_INSERT;
del = chg = yank = 0;
update_eline(line, lsz, lptr, 0);
break;
case 'i': /* insert at current pos */
mode = MODE_INSERT;
del = chg = yank = 0;
update_eline(line, lsz, lptr, 0);
break;
case 'I': /* insert before line */
lptr = 0;
mode = MODE_INSERT;
del = chg = yank = 0;
update_eline(line, lsz, lptr, 0);
break;
case 'R': /* replace (overstrike) mode */
mode = MODE_OSTRIKE;
del = chg = yank = 0;
break;
case 'r': /* replace this character */
del = chg = yank = 0;
if (lptr > 0) {
literal = MODE_OSTRIKE;
replone = 1;
}
else {
replone = literal = 0;
dobell();
}
break;
case '0': /* move to position 0 */
del_from = 0;
del_to = lptr ? lptr-1 : 0;
if (del || chg)
dodel = 1;
if (!yank) {
lptr = 0;
update_eline(line, lsz, lptr, 0);
}
break;
case '$': /* move to last position */
del_from = lptr;
del_to = (lsz) ? lsz-1 : 0;
if (lsz && !yank)
lptr = lsz-1;
if (del || chg)
dodel = 1;
if (!yank)
update_eline(line, lsz, lptr, 0);
break;
case 'W': /* big word forward */
del_to = endW();
if (!yank)
lptr = forwW();
if (del || chg)
dodel = 1;
if (!yank)
update_eline(line, lsz, lptr, 0);
break;
case 'w': /* word forward */
del_to = endw();
if (!yank)
lptr = forww();
if (del || chg)
dodel = 1;
if (!yank)
update_eline(line, lsz, lptr, 0);
break;
case 'B': /* big word backward */
del_to = (lptr) ? lptr-1 : 0;
del_from = backW();
if (!yank)
lptr = del_from;
if (del || chg)
dodel = 1;
if (!yank)
update_eline(line, lsz, lptr, 0);
break;
case 'b': /* word backward */
word_back:
del_to = (lptr) ? lptr-1 : 0;
del_from = backw();
if (!yank)
lptr = del_from;
if (del || chg)
dodel = 1;
if (!yank)
update_eline(line, lsz, lptr, 0);
break;
case 'E': /* next big word end */
del_to = endW();
if (!yank)
lptr = del_to;
if (del || chg)
dodel = 1;
if (!yank)
update_eline(line, lsz, lptr, 0);
break;
case 'e': /* next word end */
del_to = endw();
if (!yank)
lptr = del_to;
if (del || chg)
dodel = 1;
if (!yank)
update_eline(line, lsz, lptr, 0);
break;
case 'x': /* delete current char */
delcurrent:
if (lsz && lptr < lsz) {
memcpy(undobuf, line, (unsigned)lsz);
undolsz = lsz;
undolptr = lptr;
*cutbuf = line[lptr];
cutsize = 1;
memmove(line+lptr, line+lptr+1,
(unsigned)--lsz-lptr);
if (lptr == lsz && lsz)
lptr = lsz-1;
update_eline(line, lsz, lptr, 0);
}
del = chg = dodel = yank = 0;
update_eline(line, lsz, lptr, 0);
return;
case 'X': /* delete previous char */
delbackc:
if (lptr && lsz) {
memcpy(undobuf, line, (unsigned)lsz);
undolsz = lsz;
undolptr = lptr;
*cutbuf = line[lptr-1];
cutsize = 1;
memmove(line+lptr-1, line+lptr,
(unsigned)lsz-lptr);
lsz--;
lptr--;
}
update_eline(line, lsz, lptr, 0);
del = chg = dodel = yank = 0;
return;
case 'Y': /* yank to end of line */
yank = 1;
goto deltoeol;
case 'C': /* change to end of line */
mode = MODE_INSERT;
/*FALLTHROUGH*/
case 'D': /* delete to end of line */
deltoeol:
memcpy(undobuf, line, (unsigned)lsz);
undolsz = lsz;
undolptr = lptr;
if (lsz) {
cutsize = lsz-lptr-1;
if (!yank) {
memcpy(cutbuf, line+lptr, cutsize);
if (lptr)
lsz = lptr--;
else
lsz = 0;
}
}
if (mode == MODE_INSERT && !yank && lsz > 0) {
line[lsz] = ' ';
lptr++;
lsz++;
}
if (!yank)
update_eline(line, lsz, lptr, 0);
del = chg = yank = 0;
break;
case 'y':
yank = 1;
goto delete_to_motion;
case 'c':
chg = 1;
/*FALLTHROUGH*/
case 'd': /* set delete to motion or delete line. */
delete_to_motion:
if (!del) {
del = 1;
del_from = del_to = lptr;
return;
}
else {
del_from = 0;
del_to = (lsz) ? lsz-1 : 0;
dodel = 1;
}
break;
case 'j': /* next line in history */
histnext:
if (elc) {
line[0] = 0;
if (elc->al_next) {
elc = elc->al_next;
if (elc->al_text)
strcpy(line, elc->al_text);
lsz = strlen(line);
lptr = min(lptr, lsz);
update_eline(line, lsz, lptr, 0);
}
}
del = chg = yank = 0;
break;
case 'k': /* previous line in history */
histprev:
if (elc) {
line[0] = 0;
if (elc->al_prev) {
elc = elc->al_prev;
if (elc->al_text)
strcpy(line, elc->al_text);
lsz = strlen(line);
lptr = min(lptr, lsz);
update_eline(line, lsz, lptr, 0);
}
}
del = chg = yank = 0;
break;
case 'U':
case 'u': /* Undo last major change */
memcpy(undotmp, line, (unsigned)lsz);
memcpy(line, undobuf, (unsigned)undolsz);
memcpy(undobuf, undotmp, (unsigned)lsz);
t = lsz;
lsz = undolsz;
undolsz = t;
t = lptr;
lptr = undolptr;
undolptr = t;
update_eline(line, lsz, lptr, 0);
del = chg = yank = 0;
break;
case '_':
case '^': /* go to first non-whitespace char */
{
int lptrsv = lptr;
t = lptr;
lptr = 0;
while (lptr < lsz && isspace(line[lptr]))
lptr++;
if (del || chg) {
dodel = 1;
del_from = min(t, lptr);
del_to = max(t, lptr);
if (del_to > 0)
del_to--;
}
if (yank)
lptr = lptrsv;
else
update_eline(line, lsz, lptr, 0);
}
break;
case 'f': /* find forward */
find = 1;
break;
case 'F': /* find backward */
find = -1;
break;
case '/': /* searching function (forward) */
search(FORWARD);
return;
case '?': /* searching function (backward) */
search(BACKWARD);
return;
case 'm': /* set mark a-z */
mark = 1;
yank = chg = del = 0;
return;
case '`':
/*FALLTHROUGH*/
case '\'': /* goto mark a-z */
gotomark = 1;
return;
case 'n': /* search next match */
findre(FORWARD);
return;
case 'N': /* search next match (reverse) */
findre(BACKWARD);
return;
case '~': /* toggle case, move one forward */
if (line[lptr] >= 'A' && line[lptr] <= 'z')
line[lptr] = casetbl[line[lptr]-'A'];
if (lptr < lsz)
lptr++;
update_eline(line, lsz, lptr, 0);
del = chg = yank = 0;
return;
case ':': /* insert mode & add cmdch to beginning */
del = chg = yank = 0;
mode = MODE_INSERT;
c = cmdch;
lptr = 0;
goto inliteral;
/*NOTREACHED*/
case 'o': /*FALLTHROUGH*/
case 'O': /* options editor */
options_dialog();
return;
case 'p': /* paste cut buffer after current char */
if (lsz == 0)
goto pastebefore;
if (lsz+cutsize < MAXINPUT && cutsize > 0) {
memcpy(undobuf, line, (unsigned)lsz);
undolsz = lsz;
undolptr = lptr;
if (lptr < lsz-1)
memmove(line+lptr+cutsize+1,
line+lptr+1,
(unsigned)lsz-lptr);
memcpy(line+lptr+1, cutbuf, cutsize);
lptr = lptr+cutsize;
lsz = lsz+cutsize;
update_eline(line, lsz, lptr, 0);
del = chg = yank = 0;
}
else
dobell();
return;
case 'P': /* paste cut buffer before current char */
pastebefore:
if (lsz+cutsize < MAXINPUT && cutsize > 0) {
memcpy(undobuf, line, (unsigned)lsz);
undolsz = lsz;
undolptr = lptr;
memmove(line+lptr+cutsize, line+lptr,
(unsigned)lsz-lptr);
memcpy(line+lptr, cutbuf, cutsize);
lptr = lptr+cutsize;
lsz = lsz+cutsize;
update_eline(line, lsz, lptr, 0);
del = chg = yank = 0;
}
else
dobell();
return;
case ESC:
showmode(mode);
elhome();
/*FALLTHROUGH*/
default:
dobell();
del = chg = yank = 0;
break;
}
check4del:
if (dodel) {
/*
* Delete or change to motion.
* We make a backup copy that can be restored with
* the next u (undo) command.
*/
int i, j = 0;
char string[MAXINPUT+1];
int need_lastpos_adjust;
if (!yank) {
need_lastpos_adjust = (del_to == lsz-1);
memcpy(undobuf, line, (unsigned)lsz);
undolsz = lsz;
undolptr = lptr;
}
cutsize = 0;
if (!yank) {
for (i = 0; i < lsz; i++)
if (i < del_from || i > del_to)
string[j++] = line[i];
else
cutbuf[cutsize++] = line[i];
}
else {
for (i = 0; i < lsz; i++)
if (i >= del_from && i <= del_to)
cutbuf[cutsize++] = line[i];
del_to = -1;
del = dodel = yank = 0;
return;
}
string[j] = 0;
lsz = j;
memmove(line, string, (unsigned)j);
lptr = (del_from) ? del_from : 0;
if (lptr && lptr >= lsz && mode == MODE_COMMAND)
lptr--;
if (chg) {
mode = MODE_INSERT;
chg = 0;
if (need_lastpos_adjust) {
line[lsz] = ' ';
lptr++;
lsz++;
}
}
update_eline(line, lsz, lptr, 0);
del_to = -1;
del = dodel = 0;
}
}
skip_mode_checks:
if (mode != omode) {
showmode(mode);
elhome();
}
omode = mode;
}
/* Forward one word. */
static int
forww()
{
register unsigned char *t = (unsigned char *) line + lptr;
if (isspace(*t)) {
while (((t-line) < lsz-1) && isspace(*t))
t++;
return t - line;
}
if (isalnum(*t))
while (((t-line) < lsz-1) && (isalnum(*t) || *t == '_'))
t++;
else if (ispunct(*t))
while (((t-line) < lsz-1) && ispunct(*t))
t++;
while (((t-line) < lsz-1) && isspace(*t))
t++;
return t - line;
}
/* Forward one big word. */
static int
forwW()
{
unsigned char *t = line+lptr;
char delim = 0;
while ((t-line) < lsz-1) {
if (isspace(*t)) {
delim = 1;
t++;
continue;
}
if (!delim) {
t++;
continue;
}
return t-line;
}
return lsz-1;
}
/* Back one word. */
static int
backw()
{
register unsigned char *t = line + lptr;
if (t > line && *t == 0)
t--;
if (isspace(*t))
while (t > line && isspace(*t))
t--;
else if (isalnum(*t))
while (t > line+1 && (isalnum(*t) || *t == '_') &&
!isalnum(*(t-1)) && *(t-1) != '_')
t--;
else if (ispunct(*t))
while (t > line+1 && ispunct(*t) && !ispunct(*(t-1)))
t--;
while (t > line && isspace(*t))
t--;
if (t == line+1)
return 0;
if (isalnum(*t))
while (t > line && (isalnum(*(t-1)) || *(t-1) == '_'))
t--;
else if (ispunct(*t))
while (t > line && ispunct(*(t-1)))
t--;
return t - line;
}
/* Back one big word. */
static int
backW()
{
unsigned char *t = line + lptr;
if (t > line+1 && ((isspace(*t) || isspace(*(t-1)))))
t--;
while (t > line && (isspace(*t)))
t--;
while (t > line && !isspace(*t))
t--;
if (isspace(*t))
return ++t-line;
return 0;
}
/* End of next word. */
static int
endw()
{
register unsigned char *t = line + lptr;
if (((t-line) < lsz-1))
t++;
if (isalnum(*t)) {
while (((t-line) < lsz-1) && (isalnum(*(t+1)) || *(t+1) == '_'))
t++;
return t - line;
}
else if (ispunct(*t)) {
while (((t-line) < lsz-1) && ispunct(*(t+1)))
t++;
return t - line;
}
while (((t-line) < lsz-1) && isspace(*t))
t++;
if (isalnum(*t))
while (((t-line) < lsz-1) && (isalnum(*(t+1)) || *(t+1) == '_'))
t++;
else if (ispunct(*t))
while (((t-line) < lsz-1) && ispunct(*(t+1)))
t++;
return t-line;
}
/* End of next big word. */
static int
endW()
{
unsigned char *t = line + lptr;
if ((t-line) < lsz-1 && ((isspace(*t) || isspace(*(t+1)))))
t = line+forww();
while (t < line+lsz-1 && !isspace(*t))
t++;
if (isspace(*t))
t--;
return t-line;
}
/*
* Re-paints the editor line.
*/
void
elrefr(force_redraw)
int force_redraw;
{
if (NULL != line) {
line[lsz] = 0;
update_eline(line, lsz, lptr, force_redraw);
} else
update_eline("", lsz, lptr, force_redraw);
showmode(mode);
elhome();
}
/*
* Move cursor to current position on editor line if the editor line
* isn't empty.
*/
void
elhome()
{
f_elhome();
tty_flush();
}
void
f_elhome()
{
move_eline(lptr);
}
static void
addtohist(ln)
unsigned char *ln;
{
struct aline *al;
int i;
if (ln == NULL)
return;
if (ela == NULL) {
ela = elc = ell = al =
chkmem(calloc(1, sizeof (struct aline)));
al->al_text = chkmem(Strdup(ln));
al->al_next = al->al_prev = NULL;
return;
}
if (++history > HISTORY) {
#ifdef DEBUGV
iw_printf(COLI_TEXT, "DEBUG: chopping editor line history, \
history = %d\n", history);
#endif
/* Delete 1/4 of the history buffer to get some space. */
for (i = 0; i < HISTORY/4; i++, history--) {
free(ela->al_text);
al = ela->al_next;
free(ela);
ela = al;
}
ela->al_prev = NULL;
}
al = chkmem(calloc(1, sizeof (struct aline)));
al->al_text = chkmem(Strdup(ln));
al->al_prev = ell;
ell->al_next = al;
ell = elc = al;
}
static void
delfromhist()
{
struct aline *l;
if (ell == NULL)
return;
l = ell;
ell = l->al_prev;
if (ell)
ell->al_next = NULL;
free(l->al_text);
free(l);
history--;
}
/*
* Store the line in the history buffer. If exec is true, the line
* is parsed and executed.
*/
void
linedone(exec)
int exec;
{
killtrailws();
line[lsz] = 0;
if (lsz > 0) {
delfromhist();
addtohist(line);
addtohist("");
}
lsz = lptr = 0;
update_eline(line, lsz, 0, 0);
mode = MODE_INSERT;
showmode(mode);
elc = ell;
del_to = -1;
if (exec)
cmdline(line);
}
/*
* Delete all trailing whitespace on line.
*/
static void
killtrailws()
{
while (lsz > 0 && line[lsz-1] == ' ')
lsz--;
if (lptr >= lsz)
lptr = lsz-1;
if (lptr < 0)
lptr = 0;
}
/*
* Check if client idle time exceeded and exit if yes.
*/
static void
idlecheck()
{
time_t now = time(NULL);
extern sig_atomic_t atomic_idleexceed;
if (now - laststroke > IDLELIMIT)
atomic_idleexceed = 1;
}
/*
* Set editor line mode to m.
* Use with caution and very rarely!
*/
void
el_setmode(m)
int m;
{
mode = m;
}
/*
* Deletes the history and frees all memory connected to the history
* list (for memory leak detection, at program exit).
*/
void
free_history()
{
struct aline *al;
int i;
for (i = 0; i < history; i++) {
free(ela->al_text);
al = ela->al_next;
free(ela);
ela = al;
}
}
/*
* Return a pointer to the last nick/word before or up to the cursor. The
* word is extracted and stored in a storage with dynamically allocated
* memory (caller must free memory on its own).
* The pointer linep is set to the beginning of the word in the editor
* line.
* Returns NULL if not successful.
*/
static char *
lastnick(linep)
unsigned char **linep;
{
unsigned char *word, *upto;
register unsigned char *p;
if (lptr < 1)
return NULL;
upto = p = line+lptr;
while (p > line && isnickch(*((char *)p-1)))
(char *)p--;
word = chkmem(malloc((unsigned)((char *)upto-(char *)p+1)));
memcpy(word, p, (unsigned)(upto-p));
word[upto-p] = 0;
*linep = p;
return word;
}
|