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
|
/* Copyright (c) 1996--1999 Geoff Pike. */
/* All rights reserved. */
/* Floater is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
/* This software is provided "as is" and comes with absolutely no */
/* warranties. Geoff Pike is not liable for damages under any */
/* circumstances. Support is not provided. Use at your own risk. */
/* Personal, non-commercial use is allowed. Attempting to make money */
/* from Floater or products or code derived from Floater is not allowed */
/* without prior written consent from Geoff Pike. Anything that remotely */
/* involves commercialism, including (but not limited to) systems that */
/* show advertisements while being used and systems that collect */
/* information on users that is later sold or traded require prior */
/* written consent from Geoff Pike. */
#include "floater.h"
#if GUI_ONLY||SILENT
bool needrefresh = FALSE;
#else
#include "comm.h"
#include "br.h"
#include "tickler.h"
#include "text.h"
/*
text.c: for both reading and writing to the screen when we don't have Tk
*/
extern bool initializing; /* from main.c */
/* actually, this variable is only a rough guide, but if it is true, we make
sure to refresh relatively soon */
bool needrefresh = FALSE;
static bool ccedit = FALSE;
static void Fmove(int y, int x);
static void Fmvaddstr(int y, int x, char *s);
static void Fmvaddch(int y, int x, char c);
static void inittextinput(void);
static void inittextoutput(void);
static void initeditor(void);
static void gotch_ccedit(int c);
/****************************************************************************
Code for multiplexing output to multiple "virtual screens"
****************************************************************************/
typedef struct scrnlist {
int n; /* identifying number */
int curx, cury; /* cursor position */
int anchorx, anchory; /* our "anchor" */
int drawx, drawy; /* where ch() or str() will draw */
int statusbary, cmdliney, talkliney;
int talktop, talkbottom; /* location of scrolling talk window */
char **scrn; /* what's on the scrn */
struct scrnlist *next;
} scrnlist;
static scrnlist *drawsc = NULL; /* the scrn we're drawing to */
static scrnlist *dispsc = NULL; /* the scrn we're displaying */
static scrnlist *scrns = NULL; /* the list of all scrns */
void makescrn(int n, int cmdliney, int talkliney, int statusbary,
int talktop, int talkbottom)
{
scrnlist *sc;
int y, x;
sc = alloc(scrnlist);
sc->n = n;
sc->cmdliney = cmdliney;
sc->talkliney = talkliney;
sc->statusbary = statusbary;
sc->talktop = talktop;
sc->talkbottom = talkbottom;
sc->curx = sc->cury = sc->anchorx = sc->anchory = sc->drawx = sc->drawy = 0;
sc->scrn = (char **) salloc(sizeof(char *) * LINES);
for (y = 0; y < LINES; y++) {
sc->scrn[y] = salloc(sizeof(char) * (COLS + 1));
for (x = 0; x < COLS; x++) sc->scrn[y][x] = ' ';
sc->scrn[y][x] = '\0';
}
sc->next = scrns;
scrns = sc;
}
/* Change which scrn we're drawing on to scrn. */
void drawscrn(int scrn)
{
scrnlist *sc;
if (drawsc != NULL && drawsc->n == scrn) return;
/* find screen we're switching to */
for (sc = scrns; sc->n != scrn; ) {
sc = sc->next;
assert(sc != NULL);
}
drawsc = sc;
}
/* Pad s out with hyphens until it is the appropriate width, then
display it at the given x position on the statusbar. */
static void draw_on_statusbar(int x, int width, char *s)
{
char t[200];
assert(width > 0);
sprintf(t, "%s-----------------------------------------------------------------------------------", s);
t[width] = '\0';
Fmvaddstr(drawsc->statusbary, x, t);
}
void update_statusbar(void)
{
char s[100], *t;
bool center;
bool want_auction = (dispsc->n == 1);
int mid, start, end, width;
int old = drawsc->n;
static char *previous_scroll = NULL;
static char *previous_myturn = NULL;
static char *previous_bridge = NULL;
static bool show_init = FALSE;
drawscrn(dispsc->n); /* draw on whatever screen we're displaying */
if (!ccedit && atob(TclGet("scrolllock")))
sprintf(s, "%d-%s/%s", 1 + atoi(TclGet("talklineattop")),
TclDo("uplevel #0 {expr $talklineattop + "
"$talkbottom - $talktop + 1}"),
TclGet("ntalklines"));
else s[0] = '\0';
if (previous_scroll == NULL || !streq(previous_scroll, s)) {
draw_on_statusbar(65, COLS - 65, s);
reset(previous_scroll);
previous_scroll = STRDUP(s);
}
if (silentmyturntoplay()) t = "<PLAY>";
else if (silentmyturntobid()) t = "<BID>";
else t = "";
if (previous_myturn == NULL || !streq(previous_myturn, t)) {
draw_on_statusbar(1, 6, t);
reset(previous_myturn);
previous_myturn = STRDUP(t);
}
start = 8;
end = 63;
mid = (1 + end + start) / 2;
width = end - start + 1;
t = NULL;
center = FALSE;
if (want_auction && bridge_statusbar(s, start, end, ¢er) && strexists(s))
t = s;
else if ((t = display_of_previous_trick()) != NULL)
center = TRUE;
if (t == NULL) t = "";
if (previous_bridge == NULL || !streq(previous_bridge, t)) {
if (center) draw_on_statusbar(mid - strlen(t) / 2, strlen(t), t);
else draw_on_statusbar(start, width, t);
reset(previous_bridge);
previous_bridge = STRDUP(t);
}
if (show_init != initializing)
if ((show_init = initializing))
draw_on_statusbar(1, 30, "Initializing... Please wait");
else
draw_on_statusbar(1, 30, "");
drawscrn(old); /* go back to drawing on whatever screen we were before */
}
/* Change which scrn we're actually displaying. */
void displayscrn(int scrn)
{
scrnlist *sc;
int y;
if (dispsc != NULL && dispsc->n == scrn) return;
/* find scrn we're switching to */
for (sc = scrns; sc->n != scrn; ) {
sc = sc->next;
assert(sc != NULL);
}
/* copy the new screen to curses */
for (y = 0; y < LINES; y++) mvaddstr(y, 0, sc->scrn[y]);
/* specially handle status bar, talk line, and command line */
if (dispsc != NULL) {
if (sc->statusbary >= 0 && dispsc->statusbary >= 0) {
mvaddstr(sc->statusbary, 0, dispsc->scrn[dispsc->statusbary]);
strcpy(sc->scrn[sc->statusbary], dispsc->scrn[dispsc->statusbary]);
}
if (sc->talkliney >= 0 && dispsc->talkliney >= 0) {
mvaddstr(sc->talkliney, 0, dispsc->scrn[dispsc->talkliney]);
strcpy(sc->scrn[sc->talkliney], dispsc->scrn[dispsc->talkliney]);
}
if (sc->cmdliney >= 0 && dispsc->cmdliney >= 0) {
mvaddstr(sc->cmdliney, 0, dispsc->scrn[dispsc->cmdliney]);
strcpy(sc->scrn[sc->cmdliney], dispsc->scrn[dispsc->cmdliney]);
}
}
/* update cursor position */
if (dispsc != NULL) {
if (dispsc->cury == dispsc->cmdliney) sc->cury = sc->cmdliney;
else if (dispsc->cury == dispsc->talkliney) sc->cury = sc->talkliney;
else if (dispsc->cury == dispsc->statusbary) sc->cury = sc->statusbary;
}
move(sc->cury, sc->curx);
dispsc = sc;
/* reconfigure and draw talk region */
TclDo5("uplevel #0 {set talktop ", itoa(sc->talktop), "; set talkbottom ",
itoa(sc->talkbottom), "}");
if (sc->talktop >= 0) {
TclDo5("catch {talkregion ", itoa(sc->talktop), " ",
itoa(sc->talkbottom), "}");
}
}
static void inittextoutput(void)
{
makescrn(MATRIX_SCREEN, 15, 16, 17, 18, LINES - 1);
makescrn(NO_MATRIX_SCREEN, LINES - 2, LINES - 1, LINES - 3, 0, LINES - 4);
makescrn(EDIT_SCREEN, -1, -1, LINES - 3, -1, -1);
displayscrn(0);
drawscrn(0);
Fmvaddstr(17, 0, "------------------------------ status bar ------------------------------");
}
static void Fmove(int y, int x)
{
/* assert(y >= 0 && y < LINES && x >= 0 && x < COLS); */
if (!(y >= 0 && y < LINES && x >= 0 && x < COLS)) {
char s[1000];
sprintf(s, "%%s:%%s Fmove y=%d x=%d COLS=%d LINES=%d}",
y, x, COLS, LINES);
mail_bug(s, __FILE__, __LINE__);
x = y = 0;
}
drawsc->curx = x;
drawsc->cury = y;
if (dispsc == drawsc) move(y, x);
}
static void Fmvaddstr(int y, int x, char *s)
{
int len = strlen(s);
if ((x + len == COLS + 1) && (s[len - 1] == '\n')) s[--len] = '\0';
assert(y >= 0 && y < LINES && x >= 0 && (x + len <= COLS));
strncpy(&(drawsc->scrn[y][x]), s, len);
if (dispsc == drawsc) mvaddstr(y, x, s);
}
static void Fmvaddch(int y, int x, char c)
{
static char s[2] = {'\0', '\0'};
s[0] = c;
Fmvaddstr(y, x, s);
}
/****************************************************************************/
/* argv[1] should be two integers separated by a space (x then y) */
int anchor(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[])
{
if (argc != 2) {
SET_INTERP_RESULT("wrong # of arguments to `anchor'");
return TCL_ERROR;
}
sscanf(argv[1], "%d %d", &drawsc->anchorx, &drawsc->anchory);
drawsc->drawx = drawsc->anchorx;
drawsc->drawy = drawsc->anchory;
return TCL_OK;
}
/* argv[1] is width of rect; argv[2] is height */
int clearrect(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
int width, height, j;
char s[100], format[10];
if (argc != 3) {
SET_INTERP_RESULT("wrong # of arguments to `clearrect'");
return TCL_ERROR;
}
width = atoi(argv[1]);
height = atoi(argv[2]);
assert(width < 100);
sprintf(format, "%%%ds", width); /* e.g. width=56 yields "%56s" */
sprintf(s, format, "");
assert(strlen(s) == width);
for (j=0; j<height; j++) {
Fmvaddstr(drawsc->drawy + j, drawsc->drawx, s);
}
return TCL_OK;
}
/* move down a line and to the anchor's x; doesn't change the anchor */
int down_and_anchor(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
if (argc != 2 && argc != 1) {
SET_INTERP_RESULT("wrong # of arguments to `down_and_anchor'");
return TCL_ERROR;
}
drawsc->drawy += (argc == 1) ? 1 : atoi(argv[1]);
drawsc->drawx = drawsc->anchorx;
return TCL_OK;
}
int right(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
if (argc != 2 && argc != 1) {
SET_INTERP_RESULT("wrong # of arguments to `right'");
return TCL_ERROR;
}
drawsc->drawx += (argc == 1) ? 1 : atoi(argv[1]);
return TCL_OK;
}
int str(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
if (argc != 2) {
SET_INTERP_RESULT("wrong # of arguments to `str'");
return TCL_ERROR;
}
Fmvaddstr(drawsc->drawy, drawsc->drawx, argv[1]);
drawsc->drawx += strlen(argv[1]);
return TCL_OK;
}
int ch(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
if (argc != 2) {
SET_INTERP_RESULT("wrong # of arguments to `ch'");
return TCL_ERROR;
}
Fmvaddch(drawsc->drawy, drawsc->drawx, argv[1][0]);
drawsc->drawx++;
return TCL_OK;
}
/* Before refreshing the screen, this function is called to make sure
the cursor is shown in the right place. NB: Different from
reset_cursor_position(). */
void move_to_curyx(void)
{
move(dispsc->cury, dispsc->curx);
}
/* no args; just move to (drawsc->curx, drawsc->cury) */
int reset_cursor_position(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
if (argc != 1) {
SET_INTERP_RESULT("wrong # of arguments to `reset_cursor_position'");
return TCL_ERROR;
}
Fmove(drawsc->cury, drawsc->curx);
return TCL_OK;
}
/* no args; just prepare to draw on screen currently being displayed */
int draw_on_current_display(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[])
{
static int old;
if (argc != 2) {
SET_INTERP_RESULT("wrong # of arguments to `draw_on_current_display'");
return TCL_ERROR;
}
if (argv[1][0] == '+') {
old = drawsc->n;
drawscrn(dispsc->n);
} else if (argv[1][0] == '-') {
drawscrn(old);
}
return TCL_OK;
}
/****************************************************************************/
/* Scroll the talk window by n lines (negative means back). */
void floaterscroll(int n)
{
TclDo2("talkscroll ", itoa(n));
}
#if MAKING_TUTORIAL
static void screen_shot(void)
{
static int n = 0;
char *s;
n++;
s = TEMPCAT("screen", itoa(n));
scr_dump(s);
log(TEMPCAT("include ", s));
beep();
}
#endif
/* Input */
static void drawinputlines(void);
static void drawinputline(char *s, int p, int y, int *x);
#define MAX_INPUT_STRING 1000
#define ctrl(x) ((x) - 'a' + 1)
#define FLOATER_TAB ctrl('i')
#define FLOATER_TOGGLE_DISPLAY ctrl('v')
#define FLOATER_REFRESH ctrl('w')
#define FLOATER_REFRESH2 ctrl('l')
#define FLOATER_BEGINNING_OF_LINE ctrl('a')
#define FLOATER_KILL_REST_OF_LINE ctrl('k')
#define FLOATER_KILL FLOATER_KILL_REST_OF_LINE
#define FLOATER_YANK ctrl('y')
#define FLOATER_END_OF_LINE ctrl('e')
#define FLOATER_DELETE_UNDER ctrl('d')
#define FLOATER_DOWN ctrl('n')
#define FLOATER_UP ctrl('p')
#define FLOATER_LEFT ctrl('b')
#define FLOATER_RIGHT ctrl('f')
#define FLOATER_RETURN ctrl('m')
#define FLOATER_DONE_EDITING ctrl('u')
#define FLOATER_ABORT ctrl('x')
#define FLOATER_ERASE_ALL ctrl('r')
#if MAKING_TUTORIAL
#define FLOATER_SCREEN_SHOT ctrl('t')
#endif
/* these ifndefs should do nothing; just in case, the values used are bogus */
#ifndef KEY_HOME
#define KEY_HOME -1
#endif
#ifndef KEY_BACKSPACE
#define KEY_BACKSPACE -2
#endif
#ifndef KEY_DC
#define KEY_DC -3
#endif
#ifndef KEY_LEFT
#define KEY_LEFT -4
#endif
#ifndef KEY_RIGHT
#define KEY_RIGHT -5
#endif
#ifndef KEY_ENTER
#define KEY_ENTER -6
#endif
#ifndef KEY_UP
#define KEY_UP -7
#endif
#ifndef KEY_DOWN
#define KEY_DOWN -8
#endif
/* If it doesn't appear above, should we ignore this character? */
#define IGNORE(ch) ((ch) < ' ' || (ch) > 127 || isin((ch), "[]{}"))
#define CMDLINE_Y (drawsc->cmdliney)
#define TALKLINE_Y (drawsc->talkliney)
#define INPUT_X_OFFSET 8
#define CMDLINE_PROMPT "command "
#define TALKLINE_PROMPT " talk "
static char cmdline[MAX_INPUT_STRING], talkline[MAX_INPUT_STRING];
static int cmdlineoffset, talklineoffset;
static int cmdlinetrunc = 0, talklinetrunc = 0;
static char *focus; /* points to either talkline or cmdline */
static bool text2fieldinputmode = FALSE;
/* Input two items; return them in result1 and result2 */
/* length of label1 and label2 should be same as CMDLINE_PROMPT */
void text2fieldinput(char *label1, char *init1,
char *label2, char *init2, char **result1, char **result2)
{
int c;
drawscrn(dispsc->n); /* draw on whatever screen we're displaying */
assert(strlen(CMDLINE_PROMPT) == strlen(label1));
assert(strlen(CMDLINE_PROMPT) == strlen(label2));
strcpy(cmdline, init1);
cmdlineoffset = strlen(cmdline);
strcpy(talkline, init2);
talklineoffset = strlen(talkline);
Fmvaddstr(CMDLINE_Y, 0, label1);
Fmvaddstr(TALKLINE_Y, 0, label2);
focus = cmdline;
drawinputlines();
text2fieldinputmode = TRUE;
textrefresh();
while (text2fieldinputmode) if ((c = getch()) != ERR) gotch(c);
*result1 = TEMPCOPY(cmdline);
*result2 = TEMPCOPY(talkline);
inittextinput();
drawinputlines();
textrefresh();
}
static void inittextinput(void)
{
drawscrn(dispsc->n); /* draw on whatever scrn we're displaying */
cmdline[0] = talkline[0] = '\0';
cmdlineoffset = talklineoffset = 0;
Fmvaddstr(CMDLINE_Y, 0, CMDLINE_PROMPT);
Fmvaddstr(TALKLINE_Y, 0, TALKLINE_PROMPT);
focus = cmdline;
TclSet("talkwidth", itoa(COLS));
drawsc->curx = INPUT_X_OFFSET;
drawsc->cury = (focus == cmdline) ? CMDLINE_Y : TALKLINE_Y;
}
void inittextio(void)
{
inittextoutput();
inittextinput();
initeditor();
}
/* s is the string to be displayed
p is the cursor position within s
y is the line to draw on
*x is how many characters at the start of s we are currently not showing */
static void drawinputline(char *s, int p, int y, int *x)
{
int width = COLS - INPUT_X_OFFSET;
int len = strlen(s);
char ch, lefttrunc, righttrunc; /* the last two are booleans */
/* compute what *x should be */
if (len < width || (len == width && p < len)) *x = 0;
else {
int hypothetical = p - *x;
if (hypothetical < 0) *x = p;
else while (hypothetical-- >= width) ++*x;
}
lefttrunc = (*x > 0);
righttrunc = (len > width + *x);
ch = lefttrunc ? (righttrunc ? '.' : '<') : (righttrunc ? '>' : ' ');
Fmvaddch(y, INPUT_X_OFFSET - 1, ch);
if (lefttrunc || righttrunc) {
char temp;
s += *x;
temp = s[width];
s[width] = '\0';
addstr(s);
if (strlen(s) < width) clrtoeol();
s[width] = temp;
} else {
addstr(s);
clrtoeol();
}
}
static void drawinputlines(void)
{
int old = drawsc->n;
assert(cmdlineoffset >= 0 && cmdlineoffset <= strlen(cmdline));
assert(talklineoffset >= 0 && talklineoffset <= strlen(talkline));
drawscrn(dispsc->n); /* draw on whatever screen we're displaying */
drawinputline(cmdline, cmdlineoffset, CMDLINE_Y, &cmdlinetrunc);
drawinputline(talkline, talklineoffset, TALKLINE_Y, &cmdlinetrunc);
drawsc->curx = INPUT_X_OFFSET + ((focus == cmdline) ?
(cmdlineoffset - cmdlinetrunc) :
(talklineoffset - talklinetrunc));
drawsc->cury = (focus == cmdline) ? CMDLINE_Y : TALKLINE_Y;
if (drawsc->curx >= COLS) drawsc->curx = COLS - 1;
drawscrn(old); /* go back to drawing on whatever screen we were before */
}
static void gotchs(char *s)
{
while (*s != '\0') gotch(*s++);
}
#define DECR(x) (((x) > 0) ? --(x) : 0)
#define INCR(x) (((x) < strlen(focus)) ? ++(x) : 0)
void gotch(int c)
{
extern void tasks(void);
if (ccedit) {
gotch_ccedit(c);
return;
}
switch (c) {
#if MAKING_TUTORIAL
case FLOATER_SCREEN_SHOT:
screen_shot();
break;
#endif
case FLOATER_TAB:
if (focus == cmdline) focus = talkline;
else focus = cmdline;
break;
case FLOATER_REFRESH:
case FLOATER_REFRESH2:
wrefresh(curscr);
return;
case FLOATER_BEGINNING_OF_LINE:
case KEY_HOME:
if (focus == cmdline) cmdlineoffset = 0;
else talklineoffset = 0;
break;
case FLOATER_KILL_REST_OF_LINE:
focus[(focus == cmdline) ? cmdlineoffset : talklineoffset] = '\0';
/* fall through */
case FLOATER_END_OF_LINE:
if (focus == cmdline) cmdlineoffset = strlen(cmdline);
else talklineoffset = strlen(talkline);
break;
case FLOATER_DELETE_UNDER:
{
int offset = (focus == cmdline) ? cmdlineoffset : talklineoffset;
if (focus[offset] == '\0') return;
while ((focus[offset] = focus[offset + 1]) != '\0') offset++;
}
break;
case KEY_BACKSPACE:
case KEY_DC:
#ifdef KEY_SDC
case KEY_SDC:
#endif
delete:
{
int offset = (focus == cmdline) ? cmdlineoffset : talklineoffset;
if (--offset < 0) return;
while ((focus[offset] = focus[offset + 1]) != '\0') offset++;
}
/* fall through */
case FLOATER_LEFT:
case KEY_LEFT:
if (focus == cmdline) DECR(cmdlineoffset);
else DECR(talklineoffset);
break;
case FLOATER_RIGHT:
case KEY_RIGHT:
if (focus == cmdline) INCR(cmdlineoffset);
else INCR(talklineoffset);
break;
case FLOATER_RETURN:
case KEY_ENTER:
if (text2fieldinputmode) {
if (focus == cmdline) focus = talkline;
else text2fieldinputmode = FALSE;
} else {
if (focus == cmdline) {
if (cmdline[0] == '\0' && insert_default_action(FALSE, gotchs)) return;
#if MAKING_TUTORIAL
log(TEMPCAT("# command ", cmdline));
#endif
TclDo3("command {", tclclean(cmdline), "}");
cmdlineoffset = 0;
} else {
if (talkline[0] == '\0' && insert_default_action(TRUE, gotchs)) return;
#if MAKING_TUTORIAL
log(TEMPCAT("# talk ", talkline));
#endif
if (talkline[0] != '/') TclDo3("talk {", tclclean(talkline), "}");
else TclDo3("command {", tclclean(talkline + 1), "}");
talklineoffset = 0;
}
focus[0] = '\0';
tasks();
}
break;
case FLOATER_TOGGLE_DISPLAY:
displayscrn(1 - dispsc->n);
break;
#ifdef KEY_PPAGE
case KEY_PPAGE:
floaterscroll(dispsc->talktop - dispsc->talkbottom);
break;
#endif
#ifdef KEY_NPAGE
case KEY_NPAGE:
floaterscroll(dispsc->talkbottom - dispsc->talktop);
break;
#endif
#ifdef KEY_LL
case KEY_LL:
TclDo("command bottom");
break;
#endif
case KEY_UP:
case FLOATER_UP:
floaterscroll(-1);
break;
case KEY_DOWN:
case FLOATER_DOWN:
floaterscroll(1);
break;
default:
if (IGNORE(c)) return;
if (strlen(unctrl(c)) != 1)
if (streq(unctrl(c), "^?")) goto delete;
else return;
{
int offset = (focus == cmdline) ? cmdlineoffset : talklineoffset;
char temp[MAX_INPUT_STRING];
if (strlen(focus) > MAX_INPUT_STRING - 2) { beep(); return; }
strcpy(temp, focus + offset);
strcpy(focus + offset + 1, temp);
focus[offset] = c;
}
if (focus == cmdline) ++cmdlineoffset;
else ++talklineoffset;
break;
}
if (ccedit) return;
drawinputlines();
textrefresh();
}
/****************************************************************************
text (convention card) editor
****************************************************************************/
/* allow at most 40 lines of text */
#define MAX_EDIT_Y 39
static int edityoffset = 0;
#define editywindow(y) ((y) + edityoffset)
#define inverse_editywindow(y) ((y) - edityoffset)
/* physical screen positions */
#define TOP_EDIT_Y 0
#define BOTTOM_EDIT_Y (LINES - 4)
#define EDIT_HELP_Y (LINES - 2)
#define CONFIRM_Y (LINES - 1)
static char *lines[MAX_EDIT_Y + 1];
static int edity, editx;
static char edit_readonly; /* whether this is a read-only editing session */
static void initeditor(void)
{
int i;
for (i = 0; i <= MAX_EDIT_Y; i++) lines[i] = NULL;
}
#define makenewline(n) allocnewline((n), TRUE)
#define makenewline_nofree(n) allocnewline((n), FALSE)
static void allocnewline(int n, bool shouldfree)
{
if (shouldfree) reset(lines[n]);
lines[n] = (char *) salloc(sizeof(char) * (5 + MAX_INPUT_STRING));
lines[n][0] = '\0';
}
static char *showeditline(char *s)
{
if (strlen(s) >= COLS) {
char *temp = STRNDUP(s, COLS - 1);
temp[COLS - 1] = '$';
temp[COLS] = '\0';
return temp;
} else return s;
}
static void showalleditlines(void)
{
int j, k;
for (j = TOP_EDIT_Y; j <= BOTTOM_EDIT_Y; j++) {
k = editywindow(j);
mvaddstr(j, 0, ((k <= MAX_EDIT_Y) ? showeditline(lines[k]) : ""));
clrtoeol();
}
}
static void seteditcursor(void)
{
static int old_y = -1;
char s[10];
dispsc->cury = inverse_editywindow(edity);
dispsc->curx = min(editx, COLS - 1);
if (old_y != edity) {
old_y = edity;
sprintf(s, "L%d", old_y + 1);
draw_on_statusbar(65, 4, s);
}
}
/* returns whether it deems scrolling to be necessary */
static bool adjustvisiblelines(void)
{
if (inverse_editywindow(edity) < 0) {
edityoffset = edity - 5;
return TRUE;
}
if (inverse_editywindow(edity) > BOTTOM_EDIT_Y) {
edityoffset = edity - (BOTTOM_EDIT_Y - 5);
return TRUE;
}
return FALSE;
}
static int olddispsc;
static char *ccedit_direction = NULL;
void beginedit(char *direction, char readonly)
{
int j;
char s[100];
char *help = readonly ?
"^U (or ^X) = exit; ^N, ^P, ^A, ^E, ^F, ^B to move" :
"^U = done; ^X = abort; ^R = erase all; ^N, ^P, ^A, ^E, ^F, ^B to move";
draw_on_statusbar(65, COLS - 65, "L1");
if (readonly) {
sprintf(s, "Viewing %s", direction);
draw_on_statusbar(69, strlen(s), s);
}
olddispsc = dispsc->n;
reset(ccedit_direction);
ccedit_direction = STRDUP(direction);
displayscrn(EDIT_SCREEN);
drawscrn(EDIT_SCREEN);
mvaddstr(EDIT_HELP_Y, 40 - strlen(help) / 2, help);
ccedit = TRUE;
edit_readonly = readonly;
for (j = 0; j <= MAX_EDIT_Y; j++) {
makenewline(j);
sprintf(s, "getccline %s %d", direction, j + 1);
strcpy(lines[j], TclDo(s));
}
edityoffset = editx = edity = 0;
adjustvisiblelines();
showalleditlines();
seteditcursor();
}
static bool empty_line(char *s)
{
while (*s != '\0') if (isspace(*s)) s++; else return FALSE;
return TRUE;
}
/* Find the last line of the cc (the editing buffer) that is not blank. */
static int lastccline(void)
{
int i = MAX_EDIT_Y;
while (i >= 0 && empty_line(lines[i])) i--;
return i;
}
static void endedit(bool save_changes)
{
int j, n;
ccedit = FALSE;
draw_on_statusbar(65, COLS - 65, "");
if (save_changes) {
TclDo2("beginnewcc ", ccedit_direction);
for (j = 0, n = lastccline(); n >= 0; j++, n--)
TclDo3("addnewcc {", braceclean(lines[j]), "}");
TclDo("set s [endnewcc]; if [string compare $s \"\"] {talkmsg $s}");
selfmessage(makemsg2(CC_TO_HOST, ccedit_direction,
TclDo2("ccstr ", ccedit_direction)));
}
displayscrn(olddispsc);
dispsc->curx = INPUT_X_OFFSET;
dispsc->cury = (focus == cmdline) ? CMDLINE_Y : TALKLINE_Y;
}
static void done_editing(void)
{
endedit(!edit_readonly);
}
static void discard_changes(void)
{
endedit(FALSE);
}
static void erase_everything(void)
{
int i;
editx = edity = edityoffset = 0;
for (i = 0; i <= MAX_EDIT_Y; i++) makenewline(i);
showalleditlines();
seteditcursor();
textrefresh();
}
typedef void (*vvfn)(void);
static bool confirming = FALSE;
static vvfn confirmfn;
static int oldx, oldy;
static void confirm(char *s, vvfn f)
{
char t[100];
oldx = dispsc->curx;
oldy = dispsc->cury;
sprintf(t, "%s (y/n)? ", s);
mvaddstr(CONFIRM_Y, 0, t);
dispsc->cury = CONFIRM_Y;
dispsc->curx = strlen(t);
clrtoeol();
textrefresh();
confirming = TRUE;
confirmfn = f;
}
static void gotch_confirming(int c)
{
if (c == 'y' || c == 'Y') (*confirmfn)();
else if (c == 'n' || c == 'N') {
dispsc->curx = oldx;
dispsc->cury = oldy;
} else { beep(); return; }
if (ccedit) {
mvaddstr(CONFIRM_Y, 0, " ");
clrtoeol();
}
confirming = FALSE;
}
static void insertstr(char *s)
{
char *out;
int len = strlen(lines[edity]) + strlen(s);
out = salloc(sizeof(char) * (5 + min(len, MAX_INPUT_STRING)));
strcpy(out, lines[edity]);
strcpy(out + editx, s);
if (strlen(lines[edity]) != editx)
strcpy(out + strlen(out), lines[edity] + editx);
reset(lines[edity]);
lines[edity] = out;
}
static char *strkilled(bool *deletedcr)
{
if (empty_line(lines[edity] + editx)) {
*deletedcr = TRUE;
return TEMPCAT(lines[edity] + editx, "\n");
}
return (lines[edity] + editx);
}
/* Try to move the cursor right (or to start of next line, if at end of line).
Returns whether it actually moved the cursor (i.e. true unless eof) */
static bool moveright(void)
{
if (editx < strlen(lines[edity])) editx++;
else if (edity < MAX_EDIT_Y) {
edity++;
editx = 0;
} else return FALSE;
return TRUE;
}
static void gotch_ccedit(int c)
{
extern void tasks(void);
static bool killing = FALSE;
static char *killbuffer = NULL;
bool kill = FALSE, deletedcr = FALSE, all_dirty = FALSE, dirty = FALSE;
bool yank = FALSE;
int i, j;
drawscrn(EDIT_SCREEN);
if (confirming) {
gotch_confirming(c);
return;
}
switch (c) {
#if MAKING_TUTORIAL
case FLOATER_SCREEN_SHOT:
screen_shot();
break;
#endif
case FLOATER_REFRESH:
case FLOATER_REFRESH2:
wrefresh(curscr);
return;
case FLOATER_BEGINNING_OF_LINE:
editx = 0;
break;
case KEY_HOME:
editx = edity = 0;
break;
case FLOATER_YANK:
if (edit_readonly) goto complain;
if (!strexists(killbuffer)) return;
insertstr(TEMPCAT(killbuffer, "\t")); /* tab marks end of yanked text */
yank = dirty = TRUE;
break;
case FLOATER_KILL:
if (edit_readonly) goto complain;
if (killing) {
char *s;
s = STRCAT(killbuffer, strkilled(&deletedcr));
reset(killbuffer);
killbuffer = s;
} else {
reset(killbuffer);
killbuffer = STRDUP(strkilled(&deletedcr));
}
lines[edity][editx] = '\0';
dirty = kill = TRUE;
break;
case FLOATER_END_OF_LINE:
editx = strlen(lines[edity]);
break;
case FLOATER_DELETE_UNDER:
delete_under:
if (edit_readonly) goto complain;
if (editx == strlen(lines[edity])) deletedcr = TRUE;
else {
i = editx;
while ((lines[edity][i] = lines[edity][i+1]) != '\0') i++;
}
dirty = TRUE;
break;
case KEY_BACKSPACE:
case KEY_DC:
#ifdef KEY_SDC
case KEY_SDC:
#endif
delete:
if (edit_readonly) goto complain;
/* fall through */
case FLOATER_LEFT:
case KEY_LEFT:
if (editx > 0) editx--;
else if (edity > 0) {
edity--;
editx = strlen(lines[edity]);
} else break;
if (!(c == FLOATER_LEFT || c == KEY_LEFT)) goto delete_under;
break;
case FLOATER_RIGHT:
case KEY_RIGHT:
moveright();
break;
case KEY_UP:
case FLOATER_UP:
if (edity > 0) edity--;
if (editx > strlen(lines[edity])) editx = strlen(lines[edity]);
break;
case KEY_DOWN:
case FLOATER_DOWN:
if (edity < MAX_EDIT_Y) edity++;
if (editx > strlen(lines[edity])) editx = strlen(lines[edity]);
break;
case FLOATER_DONE_EDITING:
if (edit_readonly) confirm("Done", discard_changes);
else confirm("Done editing", done_editing);
break;
case FLOATER_ABORT:
if (edit_readonly) confirm("Done", discard_changes);
else confirm("Discard changes", discard_changes);
break;
case FLOATER_ERASE_ALL:
if (edit_readonly) goto complain;
confirm("Erase everything", erase_everything);
break;
case FLOATER_RETURN:
case KEY_ENTER:
if (edit_readonly) goto complain;
c = '\n';
/* fall through */
default:
if (c != '\n' && IGNORE(c)) return;
if (strlen(unctrl(c)) != 1 && c != '\n')
if (streq(unctrl(c), "^?")) goto delete;
else return;
if (edit_readonly) goto complain;
{
int offset = editx;
char *focus = lines[edity];
char temp[MAX_INPUT_STRING];
if (strlen(focus) > MAX_INPUT_STRING - 2) { beep(); return; }
strcpy(temp, focus + offset);
strcpy(focus + offset + 1, temp);
focus[offset] = c;
}
editx++;
dirty = TRUE;
break;
complain:
beep();
return;
}
/* end of switch (c) */
if (confirming || !ccedit) goto done;
if (deletedcr) {
if (edity < MAX_EDIT_Y) {
insertstr(lines[edity + 1]);
reset(lines[edity + 1]);
for (i = edity; i <= MAX_EDIT_Y - 2; i++) lines[i + 1] = lines[i + 2];
makenewline_nofree(i + 1);
} else beep();
all_dirty = TRUE;
}
if (all_dirty || dirty) {
bool needbump = FALSE;
/* clean up occurences of \n in current line */
if (edity == MAX_EDIT_Y && isin('\n', lines[edity])) {
i = 0;
while (lines[edity][i] != '\n') i++;
lines[edity][i] = '\0';
if (editx > strlen(lines[edity])) editx = strlen(lines[edity]);
} else while (isin('\n', lines[edity])) {
i = strlen(lines[edity]);
while (lines[edity][--i] != '\n'); /* find last '\n' */
lines[edity][i] = '\0';
reset(lines[MAX_EDIT_Y]);
for (j = MAX_EDIT_Y; j >= edity + 2; j--) lines[j] = lines[j - 1];
makenewline_nofree(edity + 1);
strcpy(lines[edity + 1], lines[edity] + i + 1);
if (editx == i + 1) needbump = TRUE;
all_dirty = TRUE;
}
if (needbump) {
editx = 0;
edity++;
}
if (yank) {
/* find and remove bogus tab that marks the end of the yanked text */
while (lines[edity][editx] != '\t' && moveright());
if (lines[edity][editx] == '\t') {
yank = needbump = FALSE;
/* less efficient but equally valid would be gotch(ctrl('d')) (?) */
goto delete_under;
}
}
}
if (adjustvisiblelines() || all_dirty) showalleditlines();
else if (dirty) {
mvaddstr(inverse_editywindow(edity), 0, showeditline(lines[edity]));
clrtoeol();
}
seteditcursor();
textrefresh();
done:
killing = kill;
}
#endif /* !GUI_ONLY */
|