Annotation of OpenXM_contrib2/asir2000/fep/fep_vi.c, Revision 1.1
1.1 ! noro 1: /* Copyright (c) 1987, 1988 by Software Research Associates, Inc. */
! 2:
! 3: #ifndef lint
! 4: static char rcsid[]=
! 5: "$Header: fep_vi.c,v 4.7 91/05/29 14:31:10 utashiro Exp $ (SRA)";
! 6: #endif /* lint */
! 7:
! 8: #include <stdio.h>
! 9: #include <sys/ioctl.h>
! 10: #ifdef TERMIOS
! 11: #include <termios.h>
! 12: #if defined(__linux__) || defined(__CYGWIN__)
! 13: #ifndef _POSIX_VDISABLE
! 14: #define _POSIX_VDISABLE '\0'
! 15: #endif
! 16: #endif
! 17: #else
! 18: #include <sgtty.h>
! 19: #endif
! 20: #include <ctype.h>
! 21: #include "fep_defs.h"
! 22: #include "fep_glob.h"
! 23: #include "fep_funcs.h"
! 24:
! 25: enum {INSERTMODE, COMMANDMODE} vi_mode = COMMANDMODE;
! 26: int vi_count;
! 27:
! 28: extern char *CommandLine;
! 29: extern int CurrentPosition;
! 30:
! 31: /*
! 32: * Default binding table
! 33: */
! 34: BINDENT viComBindings[] = {
! 35: /* 1 */ {"1", vi_num},
! 36: /* 2 */ {"2", vi_num},
! 37: /* 3 */ {"3", vi_num},
! 38: /* 4 */ {"4", vi_num},
! 39: /* 5 */ {"5", vi_num},
! 40: /* 6 */ {"6", vi_num},
! 41: /* 7 */ {"7", vi_num},
! 42: /* 8 */ {"8", vi_num},
! 43: /* 9 */ {"9", vi_num},
! 44:
! 45: /* ~ */ {"~", vi_edit},
! 46: /* A */ {"A", vi_edit},
! 47: /* C */ {"C", vi_edit},
! 48: /* D */ {"D", vi_edit},
! 49: /* P */ {"P", vi_edit},
! 50: /* S */ {"S", vi_edit},
! 51: /* I */ {"I", vi_edit},
! 52: /* a */ {"a", vi_edit},
! 53: /* i */ {"i", vi_edit},
! 54: /* p */ {"p", vi_edit},
! 55: /* r */ {"r", vi_edit},
! 56: /* s */ {"s", vi_edit},
! 57: /* x */ {"x", vi_edit},
! 58: /* X */ {"X", vi_edit},
! 59: /* ^U */ {"\\^U", vi_edit},
! 60:
! 61: /* */ {" ", vi_motion},
! 62: /* ^H */ {"\b", vi_motion},
! 63: /* l */ {"l", vi_motion},
! 64: /* h */ {"h", vi_motion},
! 65: /* k */ {"k", vi_motion},
! 66: /* j */ {"j", vi_motion},
! 67: /* b */ {"b", vi_motion},
! 68: /* B */ {"B", vi_motion},
! 69: /* e */ {"e", vi_motion},
! 70: /* E */ {"E", vi_motion},
! 71: /* w */ {"w", vi_motion},
! 72: /* W */ {"W", vi_motion},
! 73: /* n */ {"n", vi_motion},
! 74: /* N */ {"N", vi_motion},
! 75: /* 0 */ {"0", vi_motion},
! 76: /* ^ */ {"^", vi_motion},
! 77: /* $ */ {"$", vi_motion},
! 78: /* | */ {"|", vi_motion},
! 79: /* - */ {"-", vi_motion},
! 80: /* + */ {"+", vi_motion},
! 81:
! 82: /* cb */ {"cb", vi_c},
! 83: /* cB */ {"cb", vi_c},
! 84: /* cw */ {"cw", vi_c},
! 85: /* cW */ {"cW", vi_c},
! 86: /* c0 */ {"c0", vi_c},
! 87: /* c^ */ {"c^", vi_c},
! 88: /* c$ */ {"c$", vi_c},
! 89: /* cc */ {"cc", vi_c},
! 90:
! 91: /* db */ {"db", vi_d},
! 92: /* dB */ {"dB", vi_d},
! 93: /* dw */ {"dw", vi_d},
! 94: /* dW */ {"dW", vi_d},
! 95: /* d0 */ {"d0", vi_d},
! 96: /* d^ */ {"d^", vi_d},
! 97: /* d$ */ {"d$", vi_d},
! 98: /* dd */ {"dd", vi_d},
! 99:
! 100: /* ^E */ {"\\^E", expand_file_name},
! 101: /* ^J */ {"\\^J", vi_new_line},
! 102: /* ^L */ {"\\^L", list_file_name},
! 103: /* ^M */ {"\\^M", vi_new_line},
! 104: /* ^P */ {"\\^P", previous_history},
! 105: /* ^N */ {"\\^N", next_history},
! 106: /* ^R */ {"\\^R", reprint},
! 107: /* ^^ */ {"\\^^", toggle_through},
! 108: /* ^X-^B */ {"\\^X\\^B", show_bindings},
! 109: /* ^X-B */ {"\\^XB", show_bindings},
! 110: /* ^X-b */ {"\\^Xb", show_bindings},
! 111: /* ^X-^H */ {"\\^X\\^H", show_history},
! 112: /* ^X-h */ {"\\^Xh", show_history},
! 113: /* ^X-H */ {"\\^XH", show_history},
! 114: /* ^X-l */ {"\\^Xl", list_file_name},
! 115: /* ^X-L */ {"\\^XL", list_file_name},
! 116: /* ^X-^L */ {"\\^X\\^L", fep_repaint},
! 117: /* ^X-^V */ {"\\^X\\^V", view_buffer},
! 118: /* ^X-^X */ {"\\^X\\^X", expand_file_name},
! 119: /* ^X-? */ {"\\^X?", show_bindings},
! 120: /* ^X-^C */ {"\\^X\\^C", (FUNC)terminate},
! 121: /* ^X-^D */ {"\\^X\\^D", send_eof},
! 122: /* ^X-( */ {"\\^X(", fep_start_script},
! 123: /* ^X-) */ {"\\^X)", fep_end_script},
! 124: /* */ {NULL, NULL}
! 125: };
! 126:
! 127: BINDENT viInsertBindings[] = {
! 128: /* ^H */ {"\\^H", vi_ins_edit},
! 129: /* ^W */ {"\\^W", vi_ins_edit},
! 130: /* ^U */ {"\\^U", vi_ins_edit},
! 131: /* ^V */ {"\\^V", vi_ins_edit},
! 132: /* ^J */ {"\\^J", vi_new_line},
! 133: /* ^L */ {"\\^L", list_file_name},
! 134: /* ^M */ {"\\^M", vi_new_line},
! 135: /* ^P */ {"\\^P", previous_history},
! 136: /* ^N */ {"\\^N", next_history},
! 137: /* ESC */ {"\\^[", vi_ins_edit},
! 138: /* ^E */ {"\\^E", expand_file_name},
! 139: /* ^^ */ {"\\^^", toggle_through},
! 140: /* ^X-^B */ {"\\^X\\^B", show_bindings},
! 141: /* ^X-B */ {"\\^XB", show_bindings},
! 142: /* ^X-b */ {"\\^Xb", show_bindings},
! 143: /* ^X-^H */ {"\\^X\\^H", show_history},
! 144: /* ^X-h */ {"\\^Xh", show_history},
! 145: /* ^X-H */ {"\\^XH", show_history},
! 146: /* ^X-l */ {"\\^Xl", list_file_name},
! 147: /* ^X-L */ {"\\^XL", list_file_name},
! 148: /* ^X-^L */ {"\\^X\\^L", fep_repaint},
! 149: /* ^X-^X */ {"\\^X\\^X", expand_file_name},
! 150: /* ^X-? */ {"\\^X?", show_bindings},
! 151: /* ^X-^C */ {"\\^X\\^C", (FUNC)terminate},
! 152: /* ^X-^D */ {"\\^X\\^D", send_eof},
! 153: /* ^X-^V */ {"\\^X\\^V", view_buffer},
! 154: /* ^X-( */ {"\\^X(", fep_start_script},
! 155: /* ^X-) */ {"\\^X)", fep_end_script},
! 156: /* */ {NULL, NULL}
! 157: };
! 158:
! 159: FUNC *viComTable;
! 160: FUNC *viInsTable;
! 161:
! 162: initViBindings (cft, aft)
! 163: FUNC cft[], aft[];
! 164: {
! 165: register int i;
! 166: BINDENT *ftp;
! 167:
! 168: for (i = 0; i < 256; i++)
! 169: aft[i] = fep_abort;
! 170: for (i = 0; i < 256; i++)
! 171: cft[i] = self_insert;
! 172:
! 173: #define import(table,key,fn) if((int)key>0)table[(int)key]=fn
! 174: #ifdef TERMIOS
! 175: /* Now, using cbreak mode
! 176: import (cft, initial_ttymode.c_cc[VSTART], ignore);
! 177: import (cft, initial_ttymode.c_cc[VSTOP], ignore);
! 178: */
! 179: #if 0
! 180: import (cft, initial_ttymode.c_cc[VINTR], insert_and_flush);
! 181: import (aft, initial_ttymode.c_cc[VINTR], insert_and_flush);
! 182: #else
! 183: /* added by noro */
! 184: import (cft, initial_ttymode.c_cc[VINTR], send_int_sig);
! 185: /* XXX : this may not be necessary */
! 186: import (aft, initial_ttymode.c_cc[VINTR], send_int_sig);
! 187: #endif
! 188: import (cft, initial_ttymode.c_cc[VQUIT], insert_and_flush);
! 189: import (cft, initial_ttymode.c_cc[VEOF], send_eof);
! 190: #ifdef VSWTC
! 191: import (cft, initial_ttymode.c_cc[VSWTC], insert_and_flush);
! 192: #endif
! 193: import (cft, initial_ttymode.c_cc[VSUSP], insert_and_flush);
! 194: #ifdef VDSUSP
! 195: import (cft, initial_ttymode.c_cc[VDSUSP], self_insert);
! 196: #endif
! 197: import (cft, initial_ttymode.c_cc[VREPRINT], reprint);
! 198: import (cft, initial_ttymode.c_cc[VDISCARD], self_insert);
! 199: import (cft, initial_ttymode.c_cc[VWERASE], delete_previous_word);
! 200: import (cft, initial_ttymode.c_cc[VLNEXT], literal_next);
! 201: import (cft, initial_ttymode.c_cc[VERASE], delete_previous_character);
! 202: import (cft, initial_ttymode.c_cc[VKILL], delete_line);
! 203: #else
! 204: /* Now, using cbreak mode
! 205: import (cft, tchars_buf.t_startx, ignore);
! 206: import (cft, tchars_buf.t_stopc, ignore);
! 207: */
! 208: #if 0
! 209: import (cft, tchars_buf.t_intrc, insert_and_flush);
! 210: import (aft, tchars_buf.t_intrc, insert_and_flush);
! 211: #else
! 212: import (cft, tchars_buf.t_intrc, send_int_sig);
! 213: import (aft, tchars_buf.t_intrc, send_int_sig);
! 214: #endif
! 215: import (cft, tchars_buf.t_quitc, insert_and_flush);
! 216: import (cft, tchars_buf.t_eofc, send_eof);
! 217: import (cft, tchars_buf.t_brkc, insert_and_flush);
! 218: import (cft, ltchars_buf.t_suspc, insert_and_flush);
! 219: import (cft, ltchars_buf.t_dsuspc, self_insert);
! 220: import (cft, ltchars_buf.t_rprntc, reprint);
! 221: import (cft, ltchars_buf.t_flushc, self_insert);
! 222: import (cft, ltchars_buf.t_werasc, delete_previous_word);
! 223: import (cft, ltchars_buf.t_lnextc, literal_next);
! 224: import (cft, initial_ttymode.sg_erase, delete_previous_character);
! 225: import (cft, initial_ttymode.sg_kill, delete_line);
! 226: #endif
! 227:
! 228: #undef import
! 229:
! 230: for (ftp = viComBindings; ftp->bt_s; ftp++)
! 231: bind_key (aft, ftp->bt_func, ftp->bt_s, fep_abort);
! 232: for (ftp = viInsertBindings; ftp->bt_s; ftp++)
! 233: bind_key (cft, ftp->bt_func, ftp->bt_s, fep_abort);
! 234:
! 235: viInsTable = cft;
! 236: viComTable = aft;
! 237: vi_mode = INSERTMODE;
! 238: }
! 239:
! 240: vi_edit (c)
! 241: int c;
! 242: {
! 243: int count = vi_count ? vi_count : 1;
! 244:
! 245: switch (c) {
! 246: case '~':
! 247: {
! 248: char c = CommandLine [CurrentPosition];
! 249:
! 250: if (isalpha (c)) {
! 251: (void) delete_next_character (c);
! 252: c ^= 0040;
! 253: (void) self_insert (c);
! 254: }
! 255: else
! 256: (void) forward_character (c);
! 257: }
! 258: break;
! 259:
! 260: case 'a':
! 261: if (!is_eol())
! 262: (void) forward_character (c);
! 263: case 'i':
! 264: (void) altenateEditmode (c);
! 265: break;
! 266:
! 267: case 'A':
! 268: (void) end_of_line (c);
! 269: (void) altenateEditmode ();
! 270: break;
! 271:
! 272: case 'I':
! 273: (void) beginning_of_line (c);
! 274: (void) altenateEditmode ();
! 275: break;
! 276:
! 277: case 'C':
! 278: (void) kill_to_end_of_line (c);
! 279: (void) altenateEditmode (c);
! 280: break;
! 281:
! 282: case 'D':
! 283: (void) kill_to_end_of_line (c);
! 284: break;
! 285:
! 286: case 'S':
! 287: (void) delete_line (c);
! 288: (void) altenateEditmode ();
! 289: break;
! 290:
! 291: case 'r':
! 292: (void) delete_next_character (c);
! 293: (void) self_insert (getcharacter ());
! 294: break;
! 295:
! 296: case 's':
! 297: (void) delete_next_n_character (count);
! 298: (void) altenateEditmode ();
! 299: break;
! 300:
! 301: case 'x':
! 302: (void) delete_next_n_character (count);
! 303: break;
! 304:
! 305: case 'X':
! 306: (void) delete_previous_n_character (count);
! 307: break;
! 308:
! 309: case 'p':
! 310: (void) forward_character ();
! 311: case 'P':
! 312: (void) yank_from_kill_buffer ();
! 313: (void) backward_character ();
! 314: break;
! 315:
! 316: case ctrl('U'):
! 317: (void) delete_line (c);
! 318: (void) altenateEditmode ();
! 319: break;
! 320: }
! 321: vi_count = 0;
! 322: return (0);
! 323: }
! 324:
! 325: altenateEditmode ()
! 326: {
! 327: FUNC *tmp;
! 328:
! 329: tmp = curFuncTab;
! 330: curFuncTab = altFuncTab;
! 331: altFuncTab = tmp;
! 332: vi_mode = (vi_mode == INSERTMODE) ? COMMANDMODE : INSERTMODE;
! 333:
! 334: return (0);
! 335: }
! 336:
! 337: vi_num (c)
! 338: int c;
! 339: {
! 340: vi_count = vi_count * 10 + ((int)c - (int)'0');
! 341: return (0);
! 342: }
! 343:
! 344: vi_motion (c)
! 345: int c;
! 346: {
! 347: int count = vi_count ? vi_count : 1;
! 348:
! 349: switch (c) {
! 350:
! 351: case 'w':
! 352: (void) forward_n_word (count);
! 353: break;
! 354:
! 355: case 'W':
! 356: (void) forward_n_Word (count);
! 357: break;
! 358:
! 359: case 'e':
! 360: (void) forward_to_end_of_n_word (count);
! 361: break;
! 362:
! 363: case 'E':
! 364: (void) forward_to_end_of_n_Word (count);
! 365: break;
! 366:
! 367: case 'b':
! 368: (void) backward_n_word (count);
! 369: break;
! 370:
! 371: case 'B':
! 372: (void) backward_n_Word (count);
! 373: break;
! 374:
! 375: case 'l':
! 376: case ' ':
! 377: (void) forward_n_character (count);
! 378: break;
! 379:
! 380: case 'h':
! 381: case '\b':
! 382: (void) backward_n_character (count);
! 383: break;
! 384:
! 385: case 'k':
! 386: case '-':
! 387: (void) previous_history (c);
! 388: break;
! 389:
! 390: case 'j':
! 391: case '+':
! 392: (void) next_history (c);
! 393: break;
! 394:
! 395: case 'n':
! 396: (void) search_reverse (c);
! 397: break;
! 398:
! 399: case 'N':
! 400: (void) search_forward (c);
! 401: break;
! 402:
! 403: case '0':
! 404: if (vi_count)
! 405: return (vi_num (c));
! 406: /* falling down */
! 407:
! 408: case '^':
! 409: (void) beginning_of_line (c);
! 410: break;
! 411:
! 412: case '$':
! 413: (void) end_of_line (c);
! 414: break;
! 415:
! 416: case '|':
! 417: (void) moveto (count - 1);
! 418: /***
! 419: if (strlen (CommandLine) >= count) {
! 420: (void) beginning_of_line (c);
! 421: (void) forward_n_character (count - 1);
! 422: }
! 423: else
! 424: (void) end_of_line (c);
! 425: ***/
! 426: break;
! 427:
! 428: default:
! 429: (void) fep_abort (c);
! 430: break;
! 431: }
! 432: vi_count = 0;
! 433: return (0);
! 434: }
! 435:
! 436: vi_c (c)
! 437: int c;
! 438: {
! 439: int count = vi_count ? vi_count : 1;
! 440:
! 441: switch (c) {
! 442: case '0':
! 443: case '^':
! 444: (void) kill_to_top_of_line (c);
! 445: break;
! 446:
! 447: case 'c':
! 448: delete_line(c);
! 449: break;
! 450:
! 451: case 'w':
! 452: (void) delete_next_n_word (count);
! 453: break;
! 454:
! 455: case 'W':
! 456: (void) delete_next_n_Word (count);
! 457: break;
! 458:
! 459: case 'b':
! 460: (void) delete_previous_n_word (count);
! 461: break;
! 462:
! 463: case 'B':
! 464: (void) delete_previous_n_Word (count);
! 465: break;
! 466:
! 467: case '$':
! 468: (void) kill_to_end_of_line(c);
! 469: break;
! 470:
! 471: default:
! 472: return (fep_abort (c));
! 473: }
! 474: vi_count = 0;
! 475: altenateEditmode ();
! 476: return (0);
! 477: }
! 478:
! 479: vi_d (c)
! 480: int c;
! 481: {
! 482: int count = vi_count ? vi_count : 1;
! 483:
! 484: switch (c) {
! 485: case '0':
! 486: case '^':
! 487: (void) kill_to_top_of_line (c);
! 488: break;
! 489:
! 490: case 'd':
! 491: delete_line(c);
! 492: break;
! 493:
! 494: case 'w':
! 495: (void) delete_next_n_word (count);
! 496: break;
! 497:
! 498: case 'W':
! 499: (void) delete_next_n_Word (count);
! 500: break;
! 501:
! 502: case 'b':
! 503: (void) delete_previous_n_word (count);
! 504: break;
! 505:
! 506: case 'B':
! 507: (void) delete_previous_n_Word (count);
! 508: break;
! 509:
! 510: case '$':
! 511: (void) kill_to_end_of_line(c);
! 512: break;
! 513:
! 514: default:
! 515: return (fep_abort (c));
! 516: }
! 517: vi_count = 0;
! 518: return (0);
! 519: }
! 520:
! 521: vi_new_line (c)
! 522: int c;
! 523: {
! 524: int count = vi_count ? vi_count : 1;
! 525:
! 526: vi_count = 0;
! 527:
! 528: (void) new_line (c);
! 529: if (vi_mode == COMMANDMODE)
! 530: altenateEditmode ();
! 531:
! 532: return (1);
! 533: }
! 534:
! 535: vi_ins_edit (c)
! 536: int c;
! 537: {
! 538: switch (c) {
! 539:
! 540: case ctrl('H'):
! 541: (void) delete_previous_character (c);
! 542: break;
! 543:
! 544: case ctrl('W'):
! 545: (void) delete_previous_word (c);
! 546: break;
! 547:
! 548: case ctrl('U'):
! 549: (void) delete_line (c);
! 550: break;
! 551:
! 552: case ctrl('V'):
! 553: (void) literal_next (c);
! 554: break;
! 555:
! 556: case '\033':
! 557: (void) altenateEditmode ();
! 558: (void) backward_character ();
! 559: break;
! 560: }
! 561: return (0);
! 562: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>