emacs.c (c5d5393c) | emacs.c (0e7d3a01) |
---|---|
1/* $OpenBSD: emacs.c,v 1.32 2004/12/20 11:34:26 otto Exp $ */ | 1/* $OpenBSD: emacs.c,v 1.33 2004/12/22 17:14:34 millert Exp $ */ |
2 3/* 4 * Emacs-like command line editing and history 5 * 6 * created by Ron Natalie at BRL 7 * modified by Doug Kingston, Doug Gwyn, and Lou Salkind 8 * adapted to PD ksh by Eric Gisin 9 */ --- 302 unchanged lines hidden (view full) --- 312 int c; 313 const char *p; 314 int i; 315 Findex f; 316 317 xbp = xbuf = buf; xend = buf + len; 318 xlp = xcp = xep = buf; 319 *xcp = 0; | 2 3/* 4 * Emacs-like command line editing and history 5 * 6 * created by Ron Natalie at BRL 7 * modified by Doug Kingston, Doug Gwyn, and Lou Salkind 8 * adapted to PD ksh by Eric Gisin 9 */ --- 302 unchanged lines hidden (view full) --- 312 int c; 313 const char *p; 314 int i; 315 Findex f; 316 317 xbp = xbuf = buf; xend = buf + len; 318 xlp = xcp = xep = buf; 319 *xcp = 0; |
320 xlp_valid = TRUE; | 320 xlp_valid = true; |
321 xmp = NULL; 322 x_curprefix = 0; 323 macroptr = (char *) 0; 324 x_histp = histptr + 1; 325 x_last_command = XFUNC_error; 326 327 xx_cols = x_cols; 328 x_col = promptlen(prompt, &p); --- 37 unchanged lines hidden (view full) --- 366 if (!(x_ftab[f].xf_flags & XF_PREFIX)) 367 x_last_command = f; 368 break; 369 case KEOL: 370 i = xep - xbuf; 371 return i; 372 case KINTR: /* special case for interrupt */ 373 trapsig(SIGINT); | 321 xmp = NULL; 322 x_curprefix = 0; 323 macroptr = (char *) 0; 324 x_histp = histptr + 1; 325 x_last_command = XFUNC_error; 326 327 xx_cols = x_cols; 328 x_col = promptlen(prompt, &p); --- 37 unchanged lines hidden (view full) --- 366 if (!(x_ftab[f].xf_flags & XF_PREFIX)) 367 x_last_command = f; 368 break; 369 case KEOL: 370 i = xep - xbuf; 371 return i; 372 case KINTR: /* special case for interrupt */ 373 trapsig(SIGINT); |
374 x_mode(FALSE); | 374 x_mode(false); |
375 unwind(LSHELL); 376 } 377 } 378} 379 380static int 381x_insert(int c) 382{ --- 52 unchanged lines hidden (view full) --- 435 int adj = x_adj_done; 436 437 if (x_do_ins(s, strlen(s)) < 0) 438 return -1; 439 /* 440 * x_zots() may result in a call to x_adjust() 441 * we want xcp to reflect the new position. 442 */ | 375 unwind(LSHELL); 376 } 377 } 378} 379 380static int 381x_insert(int c) 382{ --- 52 unchanged lines hidden (view full) --- 435 int adj = x_adj_done; 436 437 if (x_do_ins(s, strlen(s)) < 0) 438 return -1; 439 /* 440 * x_zots() may result in a call to x_adjust() 441 * we want xcp to reflect the new position. 442 */ |
443 xlp_valid = FALSE; | 443 xlp_valid = false; |
444 x_lastcp(); 445 x_adj_ok = (xcp >= xlp); 446 x_zots(cp); 447 if (adj == x_adj_done) /* has x_adjust() been called? */ 448 { 449 /* no */ 450 for (cp = xlp; cp > xcp; ) 451 x_bs(*--cp); --- 23 unchanged lines hidden (view full) --- 475 476 if (col == 0) { 477 x_e_putc(BEL); 478 return KSTD; 479 } 480 if (x_arg > col) 481 x_arg = col; 482 x_goto(xcp - x_arg); | 444 x_lastcp(); 445 x_adj_ok = (xcp >= xlp); 446 x_zots(cp); 447 if (adj == x_adj_done) /* has x_adjust() been called? */ 448 { 449 /* no */ 450 for (cp = xlp; cp > xcp; ) 451 x_bs(*--cp); --- 23 unchanged lines hidden (view full) --- 475 476 if (col == 0) { 477 x_e_putc(BEL); 478 return KSTD; 479 } 480 if (x_arg > col) 481 x_arg = col; 482 x_goto(xcp - x_arg); |
483 x_delete(x_arg, FALSE); | 483 x_delete(x_arg, false); |
484 return KSTD; 485} 486 487static int 488x_del_char(int c) 489{ 490 int nleft = xep - xcp; 491 492 if (!nleft) { 493 x_e_putc(BEL); 494 return KSTD; 495 } 496 if (x_arg > nleft) 497 x_arg = nleft; | 484 return KSTD; 485} 486 487static int 488x_del_char(int c) 489{ 490 int nleft = xep - xcp; 491 492 if (!nleft) { 493 x_e_putc(BEL); 494 return KSTD; 495 } 496 if (x_arg > nleft) 497 x_arg = nleft; |
498 x_delete(x_arg, FALSE); | 498 x_delete(x_arg, false); |
499 return KSTD; 500} 501 502/* Delete nc chars to the right of the cursor (including cursor position) */ 503static void 504x_delete(int nc, int push) 505{ 506 int i,j; --- 36 unchanged lines hidden (view full) --- 543 while (i--) 544 x_e_putc(' '); 545 i = j; 546 while (i--) 547 x_e_putc('\b'); 548 } 549 /*x_goto(xcp);*/ 550 x_adj_ok = 1; | 499 return KSTD; 500} 501 502/* Delete nc chars to the right of the cursor (including cursor position) */ 503static void 504x_delete(int nc, int push) 505{ 506 int i,j; --- 36 unchanged lines hidden (view full) --- 543 while (i--) 544 x_e_putc(' '); 545 i = j; 546 while (i--) 547 x_e_putc('\b'); 548 } 549 /*x_goto(xcp);*/ 550 x_adj_ok = 1; |
551 xlp_valid = FALSE; | 551 xlp_valid = false; |
552 for (cp = x_lastcp(); cp > xcp; ) 553 x_bs(*--cp); 554 555 return; 556} 557 558static int 559x_del_bword(int c) 560{ | 552 for (cp = x_lastcp(); cp > xcp; ) 553 x_bs(*--cp); 554 555 return; 556} 557 558static int 559x_del_bword(int c) 560{ |
561 x_delete(x_bword(), TRUE); | 561 x_delete(x_bword(), true); |
562 return KSTD; 563} 564 565static int 566x_mv_bword(int c) 567{ 568 (void)x_bword(); 569 return KSTD; --- 4 unchanged lines hidden (view full) --- 574{ 575 x_goto(xcp + x_fword()); 576 return KSTD; 577} 578 579static int 580x_del_fword(int c) 581{ | 562 return KSTD; 563} 564 565static int 566x_mv_bword(int c) 567{ 568 (void)x_bword(); 569 return KSTD; --- 4 unchanged lines hidden (view full) --- 574{ 575 x_goto(xcp + x_fword()); 576 return KSTD; 577} 578 579static int 580x_del_fword(int c) 581{ |
582 x_delete(x_fword(), TRUE); | 582 x_delete(x_fword(), true); |
583 return KSTD; 584} 585 586static int 587x_bword(void) 588{ 589 int nc = 0; 590 char *cp = xcp; --- 241 unchanged lines hidden (view full) --- 832 x_e_putc(BEL); 833 return; 834 } 835 x_histp = hp; 836 oldsize = x_size_str(xbuf); 837 strlcpy(xbuf, *hp, xend - xbuf); 838 xbp = xbuf; 839 xep = xcp = xbuf + strlen(xbuf); | 583 return KSTD; 584} 585 586static int 587x_bword(void) 588{ 589 int nc = 0; 590 char *cp = xcp; --- 241 unchanged lines hidden (view full) --- 832 x_e_putc(BEL); 833 return; 834 } 835 x_histp = hp; 836 oldsize = x_size_str(xbuf); 837 strlcpy(xbuf, *hp, xend - xbuf); 838 xbp = xbuf; 839 xep = xcp = xbuf + strlen(xbuf); |
840 xlp_valid = FALSE; | 840 xlp_valid = false; |
841 if (xep > x_lastcp()) 842 x_goto(xep); 843 else 844 x_redraw(oldsize); 845} 846 847static int 848x_nl_next_com(int c) --- 113 unchanged lines hidden (view full) --- 962 int i, j; 963 964 *xep = 0; 965 i = xep - xbuf; 966 j = x_size_str(xbuf); 967 xcp = xbuf; 968 x_push(i); 969 xlp = xbp = xep = xbuf; | 841 if (xep > x_lastcp()) 842 x_goto(xep); 843 else 844 x_redraw(oldsize); 845} 846 847static int 848x_nl_next_com(int c) --- 113 unchanged lines hidden (view full) --- 962 int i, j; 963 964 *xep = 0; 965 i = xep - xbuf; 966 j = x_size_str(xbuf); 967 xcp = xbuf; 968 x_push(i); 969 xlp = xbp = xep = xbuf; |
970 xlp_valid = TRUE; | 970 xlp_valid = true; |
971 *xcp = 0; 972 xmp = NULL; 973 x_redraw(j); 974 return KSTD; 975} 976 977static int 978x_mv_end(int c) --- 34 unchanged lines hidden (view full) --- 1013 x_e_putc('\r'); 1014 x_flush(); 1015 if (xbp == xbuf) 1016 { 1017 pprompt(prompt + prompt_skip, 0); 1018 x_col = promptlen(prompt, (const char **) 0); 1019 } 1020 x_displen = xx_cols - 2 - x_col; | 971 *xcp = 0; 972 xmp = NULL; 973 x_redraw(j); 974 return KSTD; 975} 976 977static int 978x_mv_end(int c) --- 34 unchanged lines hidden (view full) --- 1013 x_e_putc('\r'); 1014 x_flush(); 1015 if (xbp == xbuf) 1016 { 1017 pprompt(prompt + prompt_skip, 0); 1018 x_col = promptlen(prompt, (const char **) 0); 1019 } 1020 x_displen = xx_cols - 2 - x_col; |
1021 xlp_valid = FALSE; | 1021 xlp_valid = false; |
1022 cp = x_lastcp(); 1023 x_zots(xbp); 1024 if (xbp != xbuf || xep > xlp) 1025 limit = xx_cols; 1026 if (limit >= 0) 1027 { 1028 if (xep > xlp) 1029 i = 0; /* we fill the line */ --- 109 unchanged lines hidden (view full) --- 1139 x_arg = lastcol; 1140 else if (x_arg > lastcol) 1141 x_arg = lastcol; 1142 ndel = x_arg - col; 1143 if (ndel < 0) { 1144 x_goto(xbuf + x_arg); 1145 ndel = -ndel; 1146 } | 1022 cp = x_lastcp(); 1023 x_zots(xbp); 1024 if (xbp != xbuf || xep > xlp) 1025 limit = xx_cols; 1026 if (limit >= 0) 1027 { 1028 if (xep > xlp) 1029 i = 0; /* we fill the line */ --- 109 unchanged lines hidden (view full) --- 1139 x_arg = lastcol; 1140 else if (x_arg > lastcol) 1141 x_arg = lastcol; 1142 ndel = x_arg - col; 1143 if (ndel < 0) { 1144 x_goto(xbuf + x_arg); 1145 ndel = -ndel; 1146 } |
1147 x_delete(ndel, TRUE); | 1147 x_delete(ndel, true); |
1148 return KSTD; 1149} 1150 1151static void 1152x_push(int nchars) 1153{ 1154 char *cp = str_nsave(xcp, nchars, AEDIT); 1155 if (killstack[killsp]) --- 28 unchanged lines hidden (view full) --- 1184 || killstack[killtp] == 0) { 1185 killtp = killsp; 1186 x_e_puts("\nyank something first"); 1187 x_redraw(-1); 1188 return KSTD; 1189 } 1190 len = strlen(killstack[killtp]); 1191 x_goto(xcp - len); | 1148 return KSTD; 1149} 1150 1151static void 1152x_push(int nchars) 1153{ 1154 char *cp = str_nsave(xcp, nchars, AEDIT); 1155 if (killstack[killsp]) --- 28 unchanged lines hidden (view full) --- 1184 || killstack[killtp] == 0) { 1185 killtp = killsp; 1186 x_e_puts("\nyank something first"); 1187 x_redraw(-1); 1188 return KSTD; 1189 } 1190 len = strlen(killstack[killtp]); 1191 x_goto(xcp - len); |
1192 x_delete(len, FALSE); | 1192 x_delete(len, false); |
1193 do { 1194 if (killtp == 0) 1195 killtp = KILLSIZE - 1; 1196 else 1197 killtp--; 1198 } while (killstack[killtp] == 0); 1199 x_ins(killstack[killtp]); 1200 return KSTD; 1201} 1202 1203static int 1204x_abort(int c) 1205{ 1206 /* x_zotc(c); */ 1207 xlp = xep = xcp = xbp = xbuf; | 1193 do { 1194 if (killtp == 0) 1195 killtp = KILLSIZE - 1; 1196 else 1197 killtp--; 1198 } while (killstack[killtp] == 0); 1199 x_ins(killstack[killtp]); 1200 return KSTD; 1201} 1202 1203static int 1204x_abort(int c) 1205{ 1206 /* x_zotc(c); */ 1207 xlp = xep = xcp = xbp = xbuf; |
1208 xlp_valid = TRUE; | 1208 xlp_valid = true; |
1209 *xcp = 0; 1210 return KINTR; 1211} 1212 1213static int 1214x_error(int c) 1215{ 1216 x_e_putc(BEL); --- 4 unchanged lines hidden (view full) --- 1221x_stuffreset(int c) 1222{ 1223#ifdef TIOCSTI 1224 (void)x_stuff(c); 1225 return KINTR; 1226#else 1227 x_zotc(c); 1228 xlp = xcp = xep = xbp = xbuf; | 1209 *xcp = 0; 1210 return KINTR; 1211} 1212 1213static int 1214x_error(int c) 1215{ 1216 x_e_putc(BEL); --- 4 unchanged lines hidden (view full) --- 1221x_stuffreset(int c) 1222{ 1223#ifdef TIOCSTI 1224 (void)x_stuff(c); 1225 return KINTR; 1226#else 1227 x_zotc(c); 1228 xlp = xcp = xep = xbp = xbuf; |
1229 xlp_valid = TRUE; | 1229 xlp_valid = true; |
1230 *xcp = 0; 1231 x_redraw(-1); 1232 return KSTD; 1233#endif 1234} 1235 1236static int 1237x_stuff(int c) 1238{ 1239#ifdef TIOCSTI 1240 char ch = c; | 1230 *xcp = 0; 1231 x_redraw(-1); 1232 return KSTD; 1233#endif 1234} 1235 1236static int 1237x_stuff(int c) 1238{ 1239#ifdef TIOCSTI 1240 char ch = c; |
1241 bool_t savmode = x_mode(FALSE); | 1241 bool savmode = x_mode(false); |
1242 1243 (void)ioctl(TTY, TIOCSTI, &ch); 1244 (void)x_mode(savmode); 1245 x_redraw(-1); 1246#endif 1247 return KSTD; 1248} 1249 --- 226 unchanged lines hidden (view full) --- 1476 if (xmp > xcp) { 1477 rsize = xmp - xcp; 1478 xr = xcp; 1479 } else { 1480 rsize = xcp - xmp; 1481 xr = xmp; 1482 } 1483 x_goto(xr); | 1242 1243 (void)ioctl(TTY, TIOCSTI, &ch); 1244 (void)x_mode(savmode); 1245 x_redraw(-1); 1246#endif 1247 return KSTD; 1248} 1249 --- 226 unchanged lines hidden (view full) --- 1476 if (xmp > xcp) { 1477 rsize = xmp - xcp; 1478 xr = xcp; 1479 } else { 1480 rsize = xcp - xmp; 1481 xr = xmp; 1482 } 1483 x_goto(xr); |
1484 x_delete(rsize, TRUE); | 1484 x_delete(rsize, true); |
1485 xmp = xr; 1486 return KSTD; 1487} 1488 1489static int 1490x_xchg_point_mark(int c) 1491{ 1492 char *tmp; --- 156 unchanged lines hidden (view full) --- 1649 &start, &end, &words, &is_command); 1650 1651 if (nwords == 0) { 1652 x_e_putc(BEL); 1653 return KSTD; 1654 } 1655 1656 x_goto(xbuf + start); | 1485 xmp = xr; 1486 return KSTD; 1487} 1488 1489static int 1490x_xchg_point_mark(int c) 1491{ 1492 char *tmp; --- 156 unchanged lines hidden (view full) --- 1649 &start, &end, &words, &is_command); 1650 1651 if (nwords == 0) { 1652 x_e_putc(BEL); 1653 return KSTD; 1654 } 1655 1656 x_goto(xbuf + start); |
1657 x_delete(end - start, FALSE); | 1657 x_delete(end - start, false); |
1658 for (i = 0; i < nwords;) { 1659 if (x_escape(words[i], strlen(words[i]), x_emacs_putbuf) < 0 || 1660 (++i < nwords && x_ins(space) < 0)) 1661 { 1662 x_e_putc(BEL); 1663 return KSTD; 1664 } 1665 } --- 28 unchanged lines hidden (view full) --- 1694 return; 1695 } 1696 1697 olen = end - start; 1698 nlen = x_longest_prefix(nwords, words); 1699 /* complete */ 1700 if (nwords == 1 || nlen > olen) { 1701 x_goto(xbuf + start); | 1658 for (i = 0; i < nwords;) { 1659 if (x_escape(words[i], strlen(words[i]), x_emacs_putbuf) < 0 || 1660 (++i < nwords && x_ins(space) < 0)) 1661 { 1662 x_e_putc(BEL); 1663 return KSTD; 1664 } 1665 } --- 28 unchanged lines hidden (view full) --- 1694 return; 1695 } 1696 1697 olen = end - start; 1698 nlen = x_longest_prefix(nwords, words); 1699 /* complete */ 1700 if (nwords == 1 || nlen > olen) { 1701 x_goto(xbuf + start); |
1702 x_delete(olen, FALSE); | 1702 x_delete(olen, false); |
1703 x_escape(words[0], nlen, x_emacs_putbuf); 1704 x_adjust(); 1705 completed = 1; 1706 } 1707 /* add space if single non-dir match */ 1708 if (nwords == 1 && words[0][nlen - 1] != '/') { 1709 x_ins(space); 1710 completed = 1; --- 28 unchanged lines hidden (view full) --- 1739x_adjust(void) 1740{ 1741 x_adj_done++; /* flag the fact that we were called. */ 1742 /* 1743 * we had a problem if the prompt length > xx_cols / 2 1744 */ 1745 if ((xbp = xcp - (x_displen / 2)) < xbuf) 1746 xbp = xbuf; | 1703 x_escape(words[0], nlen, x_emacs_putbuf); 1704 x_adjust(); 1705 completed = 1; 1706 } 1707 /* add space if single non-dir match */ 1708 if (nwords == 1 && words[0][nlen - 1] != '/') { 1709 x_ins(space); 1710 completed = 1; --- 28 unchanged lines hidden (view full) --- 1739x_adjust(void) 1740{ 1741 x_adj_done++; /* flag the fact that we were called. */ 1742 /* 1743 * we had a problem if the prompt length > xx_cols / 2 1744 */ 1745 if ((xbp = xcp - (x_displen / 2)) < xbuf) 1746 xbp = xbuf; |
1747 xlp_valid = FALSE; | 1747 xlp_valid = false; |
1748 x_redraw(xx_cols); 1749 x_flush(); 1750} 1751 1752static int unget_char = -1; 1753 1754static void 1755x_e_ungetc(int c) --- 298 unchanged lines hidden (view full) --- 2054 char *rcp; 2055 int i; 2056 2057 if (!xlp_valid) { 2058 for (i = 0, rcp = xbp; rcp < xep && i < x_displen; rcp++) 2059 i += x_size(*rcp); 2060 xlp = rcp; 2061 } | 1748 x_redraw(xx_cols); 1749 x_flush(); 1750} 1751 1752static int unget_char = -1; 1753 1754static void 1755x_e_ungetc(int c) --- 298 unchanged lines hidden (view full) --- 2054 char *rcp; 2055 int i; 2056 2057 if (!xlp_valid) { 2058 for (i = 0, rcp = xbp; rcp < xep && i < x_displen; rcp++) 2059 i += x_size(*rcp); 2060 xlp = rcp; 2061 } |
2062 xlp_valid = TRUE; | 2062 xlp_valid = true; |
2063 return (xlp); 2064} 2065 2066#endif /* EDIT */ | 2063 return (xlp); 2064} 2065 2066#endif /* EDIT */ |