v_ex.c revision 256281
1160581Sobrien/*- 2160581Sobrien * Copyright (c) 1992, 1993, 1994 3160581Sobrien * The Regents of the University of California. All rights reserved. 4160581Sobrien * Copyright (c) 1992, 1993, 1994, 1995, 1996 5160581Sobrien * Keith Bostic. All rights reserved. 6160581Sobrien * 7160581Sobrien * See the LICENSE file for redistribution information. 8160581Sobrien */ 9160581Sobrien 10160581Sobrien#include "config.h" 11160581Sobrien 12160581Sobrien#ifndef lint 13160581Sobrienstatic const char sccsid[] = "$Id: v_ex.c,v 10.61 2011/12/22 18:41:53 zy Exp $"; 14160581Sobrien#endif /* not lint */ 15160581Sobrien 16160581Sobrien#include <sys/types.h> 17160581Sobrien#include <sys/queue.h> 18160581Sobrien#include <sys/time.h> 19160581Sobrien 20160581Sobrien#include <bitstring.h> 21160581Sobrien#include <limits.h> 22160581Sobrien#include <stdio.h> 23160581Sobrien#include <stdlib.h> 24160581Sobrien#include <string.h> 25160581Sobrien#include <unistd.h> 26160581Sobrien 27160581Sobrien#include "../common/common.h" 28160581Sobrien#include "vi.h" 29160581Sobrien 30160581Sobrienstatic int v_ecl __P((SCR *)); 31162303Simpstatic int v_ecl_init __P((SCR *)); 32160581Sobrienstatic int v_ecl_log __P((SCR *, TEXT *)); 33160581Sobrienstatic int v_ex_done __P((SCR *, VICMD *)); 34162303Simpstatic int v_exec_ex __P((SCR *, VICMD *, EXCMD *)); 35162303Simp 36160581Sobrien/* 37160581Sobrien * v_again -- & 38160581Sobrien * Repeat the previous substitution. 39160581Sobrien * 40160581Sobrien * PUBLIC: int v_again __P((SCR *, VICMD *)); 41160581Sobrien */ 42160581Sobrienint 43160581Sobrienv_again(SCR *sp, VICMD *vp) 44160581Sobrien{ 45160581Sobrien EXCMD cmd; 46160581Sobrien 47160581Sobrien ex_cinit(sp, &cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1); 48160581Sobrien argv_exp0(sp, &cmd, L(""), 1); 49160581Sobrien return (v_exec_ex(sp, vp, &cmd)); 50246893Smarcel} 51246893Smarcel 52246893Smarcel/* 53246893Smarcel * v_exmode -- Q 54246893Smarcel * Switch the editor into EX mode. 55246893Smarcel * 56160581Sobrien * PUBLIC: int v_exmode __P((SCR *, VICMD *)); 57160581Sobrien */ 58160581Sobrienint 59162303Simpv_exmode(SCR *sp, VICMD *vp) 60160581Sobrien{ 61160581Sobrien GS *gp; 62160581Sobrien 63163440Sjhb gp = sp->gp; 64178713Sjhb 65160581Sobrien /* Try and switch screens -- the screen may not permit it. */ 66160581Sobrien if (gp->scr_screen(sp, SC_EX)) { 67160581Sobrien msgq(sp, M_ERR, 68160581Sobrien "207|The Q command requires the ex terminal interface"); 69160581Sobrien return (1); 70160581Sobrien } 71160581Sobrien (void)gp->scr_attr(sp, SA_ALTERNATE, 0); 72163440Sjhb 73160581Sobrien /* Save the current cursor position. */ 74160581Sobrien sp->frp->lno = sp->lno; 75160581Sobrien sp->frp->cno = sp->cno; 76160581Sobrien F_SET(sp->frp, FR_CURSORSET); 77160581Sobrien 78160581Sobrien /* Switch to ex mode. */ 79160581Sobrien F_CLR(sp, SC_VI | SC_SCR_VI); 80160581Sobrien F_SET(sp, SC_EX); 81160581Sobrien 82160581Sobrien /* Move out of the vi screen. */ 83160581Sobrien (void)ex_puts(sp, "\n"); 84160581Sobrien 85160581Sobrien return (0); 86160581Sobrien} 87160581Sobrien 88160581Sobrien/* 89162303Simp * v_join -- [count]J 90160581Sobrien * Join lines together. 91160581Sobrien * 92160581Sobrien * PUBLIC: int v_join __P((SCR *, VICMD *)); 93160581Sobrien */ 94160581Sobrienint 95160581Sobrienv_join(SCR *sp, VICMD *vp) 96160581Sobrien{ 97160581Sobrien EXCMD cmd; 98178670Sjhb int lno; 99178670Sjhb 100178670Sjhb /* 101178670Sjhb * YASC. 102178670Sjhb * The general rule is that '#J' joins # lines, counting the current 103162303Simp * line. However, 'J' and '1J' are the same as '2J', i.e. join the 104160581Sobrien * current and next lines. This doesn't map well into the ex command 105160581Sobrien * (which takes two line numbers), so we handle it here. Note that 106160581Sobrien * we never test for EOF -- historically going past the end of file 107160581Sobrien * worked just fine. 108160581Sobrien */ 109160581Sobrien lno = vp->m_start.lno + 1; 110160581Sobrien if (F_ISSET(vp, VC_C1SET) && vp->count > 2) 111160581Sobrien lno = vp->m_start.lno + (vp->count - 1); 112160581Sobrien 113160581Sobrien ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0); 114160581Sobrien return (v_exec_ex(sp, vp, &cmd)); 115160581Sobrien} 116160581Sobrien 117160581Sobrien/* 118160581Sobrien * v_shiftl -- [count]<motion 119160581Sobrien * Shift lines left. 120160581Sobrien * 121160581Sobrien * PUBLIC: int v_shiftl __P((SCR *, VICMD *)); 122160581Sobrien */ 123160581Sobrienint 124160581Sobrienv_shiftl(SCR *sp, VICMD *vp) 125160581Sobrien{ 126160581Sobrien EXCMD cmd; 127160581Sobrien 128160581Sobrien ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0); 129160581Sobrien argv_exp0(sp, &cmd, L("<"), 2); 130160581Sobrien return (v_exec_ex(sp, vp, &cmd)); 131160581Sobrien} 132160581Sobrien 133160581Sobrien/* 134160581Sobrien * v_shiftr -- [count]>motion 135160581Sobrien * Shift lines right. 136160581Sobrien * 137160581Sobrien * PUBLIC: int v_shiftr __P((SCR *, VICMD *)); 138160581Sobrien */ 139160581Sobrienint 140160581Sobrienv_shiftr(SCR *sp, VICMD *vp) 141160581Sobrien{ 142160581Sobrien EXCMD cmd; 143160581Sobrien 144160581Sobrien ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0); 145160581Sobrien argv_exp0(sp, &cmd, L(">"), 2); 146160581Sobrien return (v_exec_ex(sp, vp, &cmd)); 147160581Sobrien} 148160581Sobrien 149160581Sobrien/* 150162303Simp * v_suspend -- ^Z 151162303Simp * Suspend vi. 152160581Sobrien * 153160581Sobrien * PUBLIC: int v_suspend __P((SCR *, VICMD *)); 154160581Sobrien */ 155160581Sobrienint 156160581Sobrienv_suspend(SCR *sp, VICMD *vp) 157160581Sobrien{ 158160581Sobrien EXCMD cmd; 159160581Sobrien 160160581Sobrien ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0); 161160581Sobrien argv_exp0(sp, &cmd, L("suspend"), SIZE(L("suspend"))); 162160581Sobrien return (v_exec_ex(sp, vp, &cmd)); 163160581Sobrien} 164160581Sobrien 165160581Sobrien/* 166160581Sobrien * v_switch -- ^^ 167160581Sobrien * Switch to the previous file. 168160581Sobrien * 169160581Sobrien * PUBLIC: int v_switch __P((SCR *, VICMD *)); 170162303Simp */ 171160581Sobrienint 172160581Sobrienv_switch(SCR *sp, VICMD *vp) 173160581Sobrien{ 174160581Sobrien EXCMD cmd; 175160581Sobrien char *name; 176160581Sobrien CHAR_T *wp; 177160581Sobrien size_t wlen; 178160581Sobrien 179160581Sobrien /* 180160581Sobrien * Try the alternate file name, then the previous file 181160581Sobrien * name. Use the real name, not the user's current name. 182160581Sobrien */ 183160581Sobrien if ((name = sp->alt_name) == NULL) { 184160581Sobrien msgq(sp, M_ERR, "180|No previous file to edit"); 185160581Sobrien return (1); 186160581Sobrien } 187160581Sobrien 188163440Sjhb /* If autowrite is set, write out the file. */ 189162303Simp if (file_m1(sp, 0, FS_ALL)) 190162303Simp return (1); 191162303Simp 192162303Simp ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0); 193162303Simp CHAR2INT(sp, name, strlen(name) + 1, wp, wlen); 194162303Simp argv_exp0(sp, &cmd, wp, wlen); 195162303Simp return (v_exec_ex(sp, vp, &cmd)); 196160581Sobrien} 197160581Sobrien 198160581Sobrien/* 199162303Simp * v_tagpush -- ^[ 200162303Simp * Do a tag search on the cursor keyword. 201162303Simp * 202162303Simp * PUBLIC: int v_tagpush __P((SCR *, VICMD *)); 203162303Simp */ 204162303Simpint 205162303Simpv_tagpush(SCR *sp, VICMD *vp) 206160581Sobrien{ 207160581Sobrien EXCMD cmd; 208160581Sobrien 209160581Sobrien ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0); 210160581Sobrien argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw) + 1); 211160581Sobrien return (v_exec_ex(sp, vp, &cmd)); 212160581Sobrien} 213162303Simp 214160581Sobrien/* 215160581Sobrien * v_tagpop -- ^T 216160581Sobrien * Pop the tags stack. 217160581Sobrien * 218162303Simp * PUBLIC: int v_tagpop __P((SCR *, VICMD *)); 219160581Sobrien */ 220160581Sobrienint 221160581Sobrienv_tagpop(SCR *sp, VICMD *vp) 222160581Sobrien{ 223160581Sobrien EXCMD cmd; 224160581Sobrien 225162303Simp ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0); 226162303Simp return (v_exec_ex(sp, vp, &cmd)); 227160581Sobrien} 228162303Simp 229162303Simp/* 230162303Simp * v_filter -- [count]!motion command(s) 231160581Sobrien * Run range through shell commands, replacing text. 232160581Sobrien * 233160581Sobrien * PUBLIC: int v_filter __P((SCR *, VICMD *)); 234162303Simp */ 235160581Sobrienint 236160581Sobrienv_filter(SCR *sp, VICMD *vp) 237162303Simp{ 238162303Simp EXCMD cmd; 239162303Simp TEXT *tp; 240162303Simp 241162303Simp /* 242160581Sobrien * !!! 243160581Sobrien * Historical vi permitted "!!" in an empty file, and it's handled 244 * as a special case in the ex_bang routine. Don't modify this setup 245 * without understanding that one. In particular, note that we're 246 * manipulating the ex argument structures behind ex's back. 247 * 248 * !!! 249 * Historical vi did not permit the '!' command to be associated with 250 * a non-line oriented motion command, in general, although it did 251 * with search commands. So, !f; and !w would fail, but !/;<CR> 252 * would succeed, even if they all moved to the same location in the 253 * current line. I don't see any reason to disallow '!' using any of 254 * the possible motion commands. 255 * 256 * !!! 257 * Historical vi ran the last bang command if N or n was used as the 258 * search motion. 259 */ 260 if (F_ISSET(vp, VC_ISDOT) || 261 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) { 262 ex_cinit(sp, 263 &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0); 264 EXP(sp)->argsoff = 0; /* XXX */ 265 266 if (argv_exp1(sp, &cmd, L("!"), 1, 1)) 267 return (1); 268 cmd.argc = EXP(sp)->argsoff; /* XXX */ 269 cmd.argv = EXP(sp)->args; /* XXX */ 270 return (v_exec_ex(sp, vp, &cmd)); 271 } 272 273 /* Get the command from the user. */ 274 if (v_tcmd(sp, vp, 275 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT)) 276 return (1); 277 278 /* 279 * Check to see if the user changed their mind. 280 * 281 * !!! 282 * Entering <escape> on an empty line was historically an error, 283 * this implementation doesn't bother. 284 */ 285 tp = TAILQ_FIRST(sp->tiq); 286 if (tp->term != TERM_OK) { 287 vp->m_final.lno = sp->lno; 288 vp->m_final.cno = sp->cno; 289 return (0); 290 } 291 292 /* Home the cursor. */ 293 vs_home(sp); 294 295 ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0); 296 EXP(sp)->argsoff = 0; /* XXX */ 297 298 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1)) 299 return (1); 300 cmd.argc = EXP(sp)->argsoff; /* XXX */ 301 cmd.argv = EXP(sp)->args; /* XXX */ 302 return (v_exec_ex(sp, vp, &cmd)); 303} 304 305/* 306 * v_exec_ex -- 307 * Execute an ex command. 308 */ 309static int 310v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp) 311{ 312 int rval; 313 314 rval = exp->cmd->fn(sp, exp); 315 return (v_ex_done(sp, vp) || rval); 316} 317 318/* 319 * v_ex -- : 320 * Execute a colon command line. 321 * 322 * PUBLIC: int v_ex __P((SCR *, VICMD *)); 323 */ 324int 325v_ex(SCR *sp, VICMD *vp) 326{ 327 GS *gp; 328 TEXT *tp; 329 int do_cedit, do_resolution, ifcontinue; 330 331 gp = sp->gp; 332 333 /* 334 * !!! 335 * If we put out more than a single line of messages, or ex trashes 336 * the screen, the user may continue entering ex commands. We find 337 * this out when we do the screen/message resolution. We can't enter 338 * completely into ex mode however, because the user can elect to 339 * return into vi mode by entering any key, i.e. we have to be in raw 340 * mode. 341 */ 342 for (do_cedit = do_resolution = 0;;) { 343 /* 344 * !!! 345 * There may already be an ex command waiting to run. If 346 * so, we continue with it. 347 */ 348 if (!EXCMD_RUNNING(gp)) { 349 /* Get a command. */ 350 if (v_tcmd(sp, vp, ':', 351 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT)) 352 return (1); 353 tp = TAILQ_FIRST(sp->tiq); 354 355 /* 356 * If the user entered a single <esc>, they want to 357 * edit their colon command history. If they already 358 * entered some text, move it into the edit history. 359 */ 360 if (tp->term == TERM_CEDIT) { 361 if (tp->len > 1 && v_ecl_log(sp, tp)) 362 return (1); 363 do_cedit = 1; 364 break; 365 } 366 367 /* If the user didn't enter anything, return. */ 368 if (tp->term == TERM_BS) 369 break; 370 371 /* If the user changed their mind, return. */ 372 if (tp->term != TERM_OK) 373 break; 374 375 /* Log the command. */ 376 if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp)) 377 return (1); 378 379 /* Push a command on the command stack. */ 380 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1)) 381 return (1); 382 } 383 384 /* Home the cursor. */ 385 vs_home(sp); 386 387 /* 388 * !!! 389 * If the editor wrote the screen behind curses back, put out 390 * a <newline> so that we don't overwrite the user's command 391 * with its output or the next want-to-continue? message. This 392 * doesn't belong here, but I can't find another place to put 393 * it. See, we resolved the output from the last ex command, 394 * and the user entered another one. This is the only place 395 * where we have control before the ex command writes output. 396 * We could get control in vs_msg(), but we have no way to know 397 * if command didn't put out any output when we try and resolve 398 * this command. This fixes a bug where combinations of ex 399 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right. 400 */ 401 if (F_ISSET(sp, SC_SCR_EXWROTE)) 402 (void)putchar('\n'); 403 404 /* Call the ex parser. */ 405 (void)ex_cmd(sp); 406 407 /* Flush ex messages. */ 408 (void)ex_fflush(sp); 409 410 /* Resolve any messages. */ 411 if (vs_ex_resolve(sp, &ifcontinue)) 412 return (1); 413 414 /* 415 * Continue or return. If continuing, make sure that we 416 * eventually do resolution. 417 */ 418 if (!ifcontinue) 419 break; 420 do_resolution = 1; 421 422 /* If we're continuing, it's a new command. */ 423 ++sp->ccnt; 424 } 425 426 /* 427 * If the user previously continued an ex command, we have to do 428 * resolution to clean up the screen. Don't wait, we already did 429 * that. 430 */ 431 if (do_resolution) { 432 F_SET(sp, SC_EX_WAIT_NO); 433 if (vs_ex_resolve(sp, &ifcontinue)) 434 return (1); 435 } 436 437 /* Cleanup from the ex command. */ 438 if (v_ex_done(sp, vp)) 439 return (1); 440 441 /* The user may want to edit their colon command history. */ 442 if (do_cedit) 443 return (v_ecl(sp)); 444 445 return (0); 446} 447 448/* 449 * v_ex_done -- 450 * Cleanup from an ex command. 451 */ 452static int 453v_ex_done(SCR *sp, VICMD *vp) 454{ 455 size_t len; 456 457 /* 458 * The only cursor modifications are real, however, the underlying 459 * line may have changed; don't trust anything. This code has been 460 * a remarkably fertile place for bugs. Do a reality check on a 461 * cursor value, and make sure it's okay. If necessary, change it. 462 * Ex keeps track of the line number, but it cares less about the 463 * column and it may have disappeared. 464 * 465 * Don't trust ANYTHING. 466 * 467 * XXX 468 * Ex will soon have to start handling the column correctly; see 469 * the POSIX 1003.2 standard. 470 */ 471 if (db_eget(sp, sp->lno, NULL, &len, NULL)) { 472 sp->lno = 1; 473 sp->cno = 0; 474 } else if (sp->cno >= len) 475 sp->cno = len ? len - 1 : 0; 476 477 vp->m_final.lno = sp->lno; 478 vp->m_final.cno = sp->cno; 479 480 /* 481 * Don't re-adjust the cursor after executing an ex command, 482 * and ex movements are permanent. 483 */ 484 F_CLR(vp, VM_RCM_MASK); 485 F_SET(vp, VM_RCM_SET); 486 487 return (0); 488} 489 490/* 491 * v_ecl -- 492 * Start an edit window on the colon command-line commands. 493 */ 494static int 495v_ecl(SCR *sp) 496{ 497 GS *gp; 498 SCR *new; 499 500 /* Initialize the screen, if necessary. */ 501 gp = sp->gp; 502 if (gp->ccl_sp == NULL && v_ecl_init(sp)) 503 return (1); 504 505 /* Get a new screen. */ 506 if (screen_init(gp, sp, &new)) 507 return (1); 508 if (vs_split(sp, new, 1)) { 509 (void)screen_end(new); 510 return (1); 511 } 512 513 /* Attach to the screen. */ 514 new->ep = gp->ccl_sp->ep; 515 ++new->ep->refcnt; 516 517 new->frp = gp->ccl_sp->frp; 518 new->frp->flags = sp->frp->flags; 519 520 /* Move the cursor to the end. */ 521 (void)db_last(new, &new->lno); 522 if (new->lno == 0) 523 new->lno = 1; 524 525 /* Remember the originating window. */ 526 sp->ccl_parent = sp; 527 528 /* It's a special window. */ 529 F_SET(new, SC_COMEDIT); 530 531#if defined(USE_WIDECHAR) && defined(USE_ICONV) 532 /* Bypass iconv on writing to DB. */ 533 o_set(new, O_FILEENCODING, OS_STRDUP, codeset(), 0); 534#endif 535 536 /* Set up the switch. */ 537 sp->nextdisp = new; 538 F_SET(sp, SC_SSWITCH); 539 return (0); 540} 541 542/* 543 * v_ecl_exec -- 544 * Execute a command from a colon command-line window. 545 * 546 * PUBLIC: int v_ecl_exec __P((SCR *)); 547 */ 548int 549v_ecl_exec(SCR *sp) 550{ 551 size_t len; 552 CHAR_T *p; 553 554 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) { 555 v_emsg(sp, NULL, VIM_EMPTY); 556 return (1); 557 } 558 if (len == 0) { 559 msgq(sp, M_BERR, "307|No ex command to execute"); 560 return (1); 561 } 562 563 /* Push the command on the command stack. */ 564 if (ex_run_str(sp, NULL, p, len, 0, 0)) 565 return (1); 566 567 /* Set up the switch. */ 568 sp->nextdisp = sp->ccl_parent; 569 F_SET(sp, SC_EXIT); 570 return (0); 571} 572 573/* 574 * v_ecl_log -- 575 * Log a command into the colon command-line log file. 576 */ 577static int 578v_ecl_log(SCR *sp, TEXT *tp) 579{ 580 recno_t lno; 581 int rval; 582 CHAR_T *p; 583 size_t len; 584 SCR *ccl_sp; 585 586 /* Initialize the screen, if necessary. */ 587 if (sp->gp->ccl_sp == NULL && v_ecl_init(sp)) 588 return (1); 589 590 ccl_sp = sp->gp->ccl_sp; 591 592 /* 593 * Don't log colon command window commands into the colon command 594 * window... 595 */ 596 if (sp->ep == ccl_sp->ep) 597 return (0); 598 599 if (db_last(ccl_sp, &lno)) { 600 return (1); 601 } 602 /* Don't log line that is identical to previous one */ 603 if (lno > 0 && 604 !db_get(ccl_sp, lno, 0, &p, &len) && 605 len == tp->len && 606 !MEMCMP(tp->lb, p, len)) 607 rval = 0; 608 else { 609 rval = db_append(ccl_sp, 0, lno, tp->lb, tp->len); 610 /* XXXX end "transaction" on ccl */ 611 /* Is this still necessary now that we no longer hijack sp ? */ 612 log_cursor(ccl_sp); 613 } 614 615 return (rval); 616} 617 618/* 619 * v_ecl_init -- 620 * Initialize the colon command-line log file. 621 */ 622static int 623v_ecl_init(SCR *sp) 624{ 625 FREF *frp; 626 GS *gp; 627 628 gp = sp->gp; 629 630 /* Get a temporary file. */ 631 if ((frp = file_add(sp, NULL)) == NULL) 632 return (1); 633 634 /* 635 * XXX 636 * Create a screen -- the file initialization code wants one. 637 */ 638 if (screen_init(gp, sp, &gp->ccl_sp)) 639 return (1); 640 if (file_init(gp->ccl_sp, frp, NULL, 0)) { 641 (void)screen_end(gp->ccl_sp); 642 gp->ccl_sp = NULL; 643 return (1); 644 } 645 646 /* The underlying file isn't recoverable. */ 647 F_CLR(gp->ccl_sp->ep, F_RCV_ON); 648 649 return (0); 650} 651