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
|
/* vicmd.c */
/* Copyright 1995 by Steve Kirkendall */
char id_vicmd[] = "$Id: vicmd.c,v 2.55 1997/10/17 18:43:10 steve Exp $";
#include "elvis.h"
/* This implements the visual @x command, which uses the contents of a
* cut buffer as simulated keystrokes.
*/
RESULT v_at(win, vinf)
WINDOW win;
VIINFO *vinf;
{
CHAR *keys;
/* if this buffer is in learn mode, then stop it */
if (isalpha(vinf->key2))
maplearn(vinf->key2, False);
/* copy the cut buffer contents into memory */
keys = cutmemory(vinf->key2);
if (!keys)
return RESULT_ERROR;
/* Treat the cut buffer contents like keystrokes. */
mapunget(keys, (int)CHARlen(keys), True);
safefree(keys);
return RESULT_COMPLETE;
}
/* This function implements the <Z><Z> and <Z><Q> commands */
RESULT v_quit(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
RESULT result = RESULT_COMPLETE;
switch (vinf->key2)
{
case 'Z':
result = exstring(win, toCHAR("x"));
break;
case 'Q':
result = exstring(win, toCHAR("q!"));
break;
}
return result;
}
/* This function implements the <Control-R> function. It does this simply by
* setting the window's "logic" flag to DRAW_SCRATCH.
*/
RESULT v_expose(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
/* reset the GUI, to bypass any optimizations */
guireset();
/* Force the screen to be redrawn from scratch next time */
win->di->logic = DRAW_SCRATCH;
return RESULT_COMPLETE;
}
/* This function implements the insert/replace commands. */
RESULT v_input(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
MARK tmp;
long offset;
long topoff;
CHAR newline[1];
char cmd;
BUFFER dotbuf;
RESULT result = RESULT_COMPLETE;
DEFAULT(1);
/* if given as a ^O command, this can only toggle input/replace mode */
if (win->state->flags & ELVIS_ONCE)
{
/* we aren't the original vi state -- modify the
* input/replace flag of the input state that we
* came from.
*/
inputtoggle(win, (char)(vinf->command == 'R' ? 'R' : 't'));
return RESULT_COMPLETE;
}
/* If this is a "more" invocation (used to come back to this function
* after the user inputs text, if count>1) then pretend that this is
* a <.> command with a count of one less than the actual count.
*/
cmd = vinf->command;
if (win->state->flags & ELVIS_MORE)
{
vinf->tweak |= TWEAK_DOTTING;
if (cmd == 'i')
{
cmd = 'a';
}
}
/* if the buffer is empty, then the only legal command is <Shift-O> */
if (o_bufchars(markbuffer(win->state->cursor)) == 0)
{
cmd = 'O';
}
/* some variations of input require preparation */
topoff = -1;
switch (cmd)
{
case 'a':
/* if not zero-length line, then move left 1 char */
tmp = dispmove(win, 0, 0);
if (scanchar(tmp) != '\n')
{
markaddoffset(win->state->cursor, 1);
}
break;
case 'A':
/* go to end of line, and start inserting there */
tmp = dispmove(win, 0, INFINITY);
marksetoffset(win->state->cursor, markoffset(tmp));
if (scanchar(tmp) != '\n')
{
markaddoffset(win->state->cursor, 1);
}
break;
case 'I':
/* go to the front of the line */
m_front(win, vinf);
break;
case 'O':
/* insert a new line before this one */
tmp = dispmove(win, 0, 0);
newline[0] = '\n';
bufreplace(tmp, tmp, newline, 1);
topoff = markoffset(tmp);
marksetoffset(win->state->cursor, topoff);
if ((vinf->tweak & TWEAK_DOTTING) == 0)
{
dispindent(win, win->state->cursor, 1);
}
break;
case 'o':
/* insert a new line after this one */
tmp = dispmove(win, 0, INFINITY);
if (scanchar(tmp) != '\n')
{
markaddoffset(tmp, 1);
}
newline[0] = '\n';
bufreplace(tmp, tmp, newline, 1);
topoff = markoffset(tmp) + 1;
marksetoffset(win->state->cursor, topoff);
if ((vinf->tweak & TWEAK_DOTTING) == 0)
{
dispindent(win, win->state->cursor, -1);
}
break;
/* 'i' and 'R' need no special preparation */
}
/* shrink the current segment around the cursor */
offset = markoffset(win->state->cursor);
marksetoffset(win->state->top, topoff >= 0 ? topoff : offset);
marksetoffset(win->state->bottom, offset);
/* if we're doing a <.> command, then don't do interactive stuff.
* Instead, just paste a copy of the previous input.
*/
if (vinf->tweak & TWEAK_DOTTING)
{
/* If R command, then delete old characters */
if (cmd == 'R')
{
dotbuf = cutbuffer('.', False);
if (dotbuf && o_bufchars(dotbuf) > CUT_TYPELEN)
{
offset = vinf->count * (o_bufchars(dotbuf) - CUT_TYPELEN)
+ markoffset(win->state->cursor);
tmp = dispmove(win, 0L, INFINITY);
if (offset > markoffset(tmp))
{
offset = markoffset(tmp);
if (scanchar(tmp) != '\n')
{
offset++;
}
}
marksetoffset(tmp, offset);
bufreplace(win->state->cursor, tmp, NULL, 0);
}
}
/* insert copies of the previous text */
tmp = win->state->cursor;
do
{
tmp = cutput('.', win, tmp, False, True, True);
if (!tmp) return RESULT_ERROR;
markaddoffset(tmp, 1);
} while (--vinf->count > 0);
markaddoffset(tmp, -1);
marksetoffset(win->state->cursor, markoffset(tmp));
}
else /* not doing <.> */
{
/* really go into input mode */
inputpush(win, 0, cmd);
/* if we have more copies to input, remember that */
result = (--vinf->count > 0) ? RESULT_MORE : RESULT_COMPLETE;
}
return result;
}
/* set a named mark */
RESULT v_setmark(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
/* check mark name */
if (vinf->key2 < 'a' || vinf->key2 > 'z')
{
return RESULT_ERROR;
}
/* if mark already set, then free its old value. */
if (namedmark[vinf->key2 - 'a'])
{
markfree(namedmark[vinf->key2 - 'a']);
}
/* set the mark */
namedmark[vinf->key2 - 'a'] = markdup(win->state->cursor);
return RESULT_COMPLETE;
}
/* undo a change */
RESULT v_undo(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
long offset;
/* choose an appropriate default */
DEFAULT(vinf->command == 'U' ? 0 : 1 );
/* if redo, then negate the undo level */
if (vinf->command == ELVCTRL('R'))
vinf->count = -vinf->count;
/* try to switch to the undo version */
offset = bufundo(win->state->cursor, vinf->count);
/* did we succeed? */
if (offset >= 0)
{
/* yes! move the cursor to the position of the undone change */
assert(offset <= o_bufchars(markbuffer(win->state->cursor)));
marksetoffset(win->state->cursor, offset);
return RESULT_COMPLETE;
}
else
{
/* no, failed */
return RESULT_ERROR;
}
}
/* Delete/replace characters from the current line. This implements the <x>,
* <Shift-X>, <r>c and <~> commands.
*/
RESULT v_delchar(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
long front, end;
MARKBUF tmp;
long curs;
CHAR *repstr;
long replen;
CHAR *cp;
long i;
long travel;
DEFAULT(1);
/* Find the endpoints of this line. Note that we need to be careful
* about the end of the line, because we don't want to allow the
* newline character to be deleted.
*/
front = markoffset(dispmove(win, 0, 0));
if (win->state->acton)
{
end = markoffset((*dmnormal.move)(win, win->state->cursor, 0, INFINITY, True));
}
else
{
end = markoffset((*win->md->move)(win, win->state->cursor, 0, INFINITY, True));
if (scanchar(win->state->cursor) != '\n')
end++;
}
/* if zero-length line, then fail */
if (front == end)
return RESULT_ERROR;
/* construct a replacement string */
switch (vinf->command)
{
case 'r':
/* would this extend past the end of the line? */
curs = markoffset(win->state->cursor);
if (curs + vinf->count > end)
return RESULT_ERROR;
/* the <Enter> key is handled specially... */
if (vinf->key2 == '\r')
{
vinf->key2 = '\n';
travel = replen = 1;
}
else
{
replen = vinf->count;
travel = replen - 1;
}
/* constuct the replacement string */
repstr = (CHAR *)safealloc((int)replen, sizeof(CHAR));
for (i = 0; i < replen; i++)
{
repstr[i] = vinf->key2;
}
break;
case '~':
/* would this extend past the end of the line? */
curs = markoffset(win->state->cursor);
if (curs + vinf->count > end)
vinf->count = end - curs;
/* construct the replacement string */
replen = vinf->count;
repstr = (CHAR *)safealloc((int)replen, sizeof(CHAR));
travel = replen;
for (i = 0, scanalloc(&cp, marktmp(tmp, markbuffer(win->state->cursor), curs));
i < replen; i++, scannext(&cp))
{
if (isupper(*cp))
repstr[i] = tolower(*cp);
else if (islower(*cp))
repstr[i] = toupper(*cp);
else
repstr[i] = *cp;
}
scanfree(&cp);
break;
case 'X':
curs = markoffset(win->state->cursor) - vinf->count;
repstr = NULL;
replen = 0;
travel = 0;
break;
case 'x':
curs = markoffset(win->state->cursor);
if (curs + vinf->count > end)
return RESULT_ERROR;
else if (curs + vinf->count == end)
curs = end - vinf->count; /* move bkwd */
repstr = NULL;
replen = 0;
travel = 0;
break;
}
/* if the starting offset is on a different line, fail */
if (curs < front)
{
if (repstr)
safefree(repstr);
return RESULT_ERROR;
}
/* if the "travel" amount would leave the cursor on the newline,
* then adjust it.
*/
if (curs + travel >= end - vinf->count + replen
&& curs + travel != front)
travel--;
/* move the cursor & replace/delete the characters */
marksetoffset(win->state->cursor, curs);
cutyank(vinf->cutbuf, win->state->cursor,
marktmp(tmp, markbuffer(win->state->cursor), curs + vinf->count),
'c', False);
bufreplace(win->state->cursor, &tmp, repstr, replen);
/* if a replacement string was allocated, free it now */
if (repstr)
{
safefree(repstr);
}
/* move to the right (maybe) */
markaddoffset(win->state->cursor, travel);
/* if <r><Enter>, then worry about autoindent */
if (vinf->command == 'r' && vinf->key2 == '\n')
{
dispindent(win, win->state->cursor, -replen);
}
return RESULT_COMPLETE;
}
/* This function calls the GUI's "tabcmd" function, if it has one. */
RESULT v_window(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
WINDOW next;
switch (vinf->key2)
{
case 'w':
case ELVCTRL('W'):
/* were we given a window number? */
if (vinf->count == 0)
{
/* go to window after this one */
next = winofbuf(win, NULL);
if (!next)
{
next = winofbuf(NULL, NULL);
}
}
else
{
/* go to window number 'n' */
for (next = winofbuf(NULL, NULL);
next && o_windowid(next) != vinf->count; /* nishi */
next = winofbuf(next, NULL))
{
}
}
break;
case 'k':
/* move up 1 window */
for (next = NULL; winofbuf(next, NULL) != win; )
{
next = winofbuf(next, NULL);
if (!next)
break;
}
break;
case 'j':
/* move down 1 window */
next = winofbuf(win, NULL);
break;
case 's':
return exstring(win, toCHAR("split"));
case 'n':
return exstring(win, toCHAR("snew"));
case 'q':
return exstring(win, toCHAR("xit"));
case 'c':
return exstring(win, toCHAR("close"));
case 'o':
return exstring(win, toCHAR("only"));
case ']':
case ELVCTRL(']'):
/* Perform a tag lookup. The v_tag function is clever enough
* to realize that this is the splitting style of tag lookup.
*/
return v_tag(win, vinf);
case 'd':
if (!CHARcmp(o_display(win), o_bufdisplay(markbuffer(win->cursor))))
{
if (!CHARcmp(o_display(win), toCHAR("normal")))
dispset(win, "hex");
#ifdef DISPLAY_SYNTAX
else if (o_filename(markbuffer(win->cursor))
&& dmsknown(tochar8(o_filename(markbuffer(win->cursor))))
&& CHARcmp(o_display(win), toCHAR("syntax")) )
dispset(win, "syntax");
#endif
else
dispset(win, "normal");
}
else
{
dispset(win, tochar8(o_bufdisplay(markbuffer(win->cursor))));
}
win->di->logic = DRAW_CHANGED;
return RESULT_COMPLETE;
case 'S':
o_wrap(win) = (BOOLEAN)!o_wrap(win);
win->di->logic = DRAW_CHANGED;
return RESULT_COMPLETE;
default:
/* run the GUI's tabcmd function. If it doesn't have one, or
* if it has one but it returns False, then fail.
*/
if (!gui->tabcmd || !(*gui->tabcmd)(win->gw, vinf->key2, vinf->count))
{
return RESULT_ERROR;
}
return RESULT_COMPLETE;
}
/* did we find a window? */
if (!next)
{
msg(MSG_ERROR, "no such window");
return RESULT_ERROR;
}
/* go to the requested window */
if (gui->focusgw)
{
(*gui->focusgw)(next->gw);
}
else
{
eventfocus(next->gw);
}
return RESULT_COMPLETE;
}
/* This function implements the visual commands which deal with tags */
RESULT v_tag(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
CHAR *cmd;
CHAR *tagname;
CHAR *scan;
RESULT result;
assert(vinf->command == ELVCTRL('T') || vinf->command == ELVCTRL(']')
|| vinf->command == ELVCTRL('W'));
/* These commands only work on the first stratum */
if (win->state->acton)
{
msg(MSG_ERROR, "only works on window's default buffer");
return RESULT_ERROR;
}
/* the ^T command is easy... */
if (vinf->command == ELVCTRL('T'))
return exstring(win, toCHAR("pop"));
/* get the tag name */
tagname = (*win->md->tagatcursor)(win, win->cursor);
if (!tagname) return RESULT_ERROR;
/* build the command. Use "stag" for ^W[ and "tag" for ^[ */
cmd = NULL;
if (vinf->command == ELVCTRL('W'))
buildCHAR(&cmd, 's');
for (scan = toCHAR("tag "); *scan; scan++)
buildCHAR(&cmd, *scan);
for (scan = tagname; *scan; scan++)
{
if (*scan == '|')
buildCHAR(&cmd, '\\');
buildCHAR(&cmd, *scan);
}
/* run the command */
result = exstring(win, cmd);
/* clean up & exit */
safefree(tagname);
safefree(cmd);
return result;
}
/* This function starts or cancels visible marking. It can also be called
* with vinf=NULL to adjust the other endpoint of an existing mark.
*/
RESULT v_visible(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
long col;
assert(win->seltop || vinf);
/*assert(!win->state->acton);*/
/* Whenever a visible selection is in progress, redrawing is implied.
* We only need to force the screen to be regenerated when we're
* cancelling a visible selection.
*/
if (vinf && win->seltop)
win->di->logic = DRAW_CHANGED;
/* are we supposed to be adjusting the visible marking? */
if (!vinf)
{
/* change "selattop" if appropriate */
if (markoffset(win->cursor) < markoffset(win->seltop))
{
if (!win->selattop)
{
if (win->seltype != 'c')
{
/* set the bottom mark to the end of
* the line which contains the former
* top mark.
*/
marksetoffset(win->selbottom, markoffset(win->md->move(win, win->seltop, 0, INFINITY, False)));
}
else
{
/* former top mark becomes new bottom */
marksetoffset(win->selbottom, markoffset(win->seltop));
}
win->selattop = True;
}
}
else if (markoffset(win->selbottom) < markoffset(win->cursor))
{
if (win->selattop)
{
if (win->seltype != 'c')
{
/* set the top mark to the start of
* the line which contains the former
* bottom mark.
*/
marksetoffset(win->seltop, markoffset(win->md->move(win, win->selbottom, 0, 0, False)));
}
else
{
/* former bottom mark becomes new top */
marksetoffset(win->seltop, markoffset(win->selbottom));
}
win->selattop = False;
}
}
/* adjust the appropriate endpoint */
if (win->selattop)
{
/* set the top limit */
if (win->seltype != 'c')
{
/* set top to start of cursor line */
marksetoffset(win->seltop, markoffset(win->md->move(win, win->cursor, 0, 0, False)));
}
else
{
/* set top to cursor position */
marksetoffset(win->seltop, markoffset(win->cursor));
}
}
else /* we're adjusting the bottom of the marked region */
{
/* set the bottom limit */
if (win->seltype != 'c')
{
/* set bottom to end of cursor line */
marksetoffset(win->selbottom, markoffset(win->md->move(win, win->cursor, 0, INFINITY, False)));
}
else
{
/* set bottom to cursor position */
marksetoffset(win->selbottom, markoffset(win->cursor));
}
}
/* if rectangular, then we also need to adjust column limits */
if (win->seltype == 'r')
{
col = win->md->mark2col(win, win->cursor, True);
if (win->selorigcol < col)
{
win->selleft = win->selorigcol;
win->selright = col;
}
else
{
win->selleft = col;
win->selright = win->selorigcol;
}
}
/* Whew! That was hard. */
return RESULT_COMPLETE;
}
/* if already visibly marking, then cancel the marking */
if (win->seltop)
{
markfree(win->seltop);
markfree(win->selbottom);
win->seltop = win->selbottom = NULL;
return RESULT_COMPLETE;
}
/* else we need to start marking characters, lines, or a rectangle,
* depending on what the command key was.
*/
switch (vinf->command)
{
case 'v':
win->seltop = markdup(win->cursor);
win->selbottom = markdup(win->cursor);
win->selleft = 0;
win->selright = INFINITY;
win->selattop = False;
win->selorigcol = 0;
win->seltype = 'c';
break;
case 'V':
win->seltop = markdup(win->md->move(win, win->cursor, 0, 0, False));
win->selbottom = markdup(win->md->move(win, win->cursor, 0, INFINITY, False));
win->selleft = 0;
win->selright = INFINITY;
win->selorigcol = 0;
win->selattop = False;
win->seltype = 'l';
break;
case ELVCTRL('V'):
win->seltop = markdup(win->md->move(win, win->cursor, 0, 0, False));
win->selbottom = markdup(win->md->move(win, win->cursor, 0, INFINITY, False));
win->selleft = win->md->mark2col(win, win->cursor, True);
win->selright = win->selleft;
win->selorigcol = win->selleft;
win->selattop = False;
win->seltype = 'r';
break;
case ELVCTRL('['):
/* <Esc> was supposed to cancel visible marking. If we get
* here instead, then no marking was in progress so <Esc>
* should beep.
*/
return RESULT_ERROR;
}
return RESULT_COMPLETE;
}
/* This function pastes text. It implements the <p> and <Shift-P> commands. */
RESULT v_paste(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
MARK dest;
/* If repeating a paste from a numbered cut buffer, then allow the
* cutput function to locate the next numbered cut buffer itself.
* Else use the same cut buffer as in original comand.
*/
if ((vinf->tweak & TWEAK_DOTTING) != 0 && isdigit(vinf->cutbuf))
{
dest = cutput('\0', win, win->state->cursor, (BOOLEAN)(vinf->command == 'p'), True, False);
}
else
{
dest = cutput(vinf->cutbuf, win, win->state->cursor, (BOOLEAN)(vinf->command == 'p'), True, False);
}
/* check for failure or success. */
if (!dest)
{
return RESULT_ERROR;
}
marksetoffset(win->state->cursor, markoffset(dest));
return RESULT_COMPLETE;
}
/* This function implements the <Shift-Q> and <:> commands. */
RESULT v_ex(win, vinf)
WINDOW win; /* window where command was typed */
VIINFO *vinf; /* information about the command */
{
/* push a stratum that does ex commands */
statestratum(win, toCHAR(EX_BUF), ':', exenter);
/* The statetratum() function pushes a state which exits after a
* single command. If the command was <Shift-Q>, then we want to
* stay in the state, so we need to tweak the flags.
*/
if (vinf->command == 'Q')
{
win->state->flags &= ~(ELVIS_POP|ELVIS_ONCE|ELVIS_1LINE);
}
return RESULT_COMPLETE;
}
/* This function implements commands which are shortcuts for operator commands:
* <s>, <Shift-C>, <Shift-D>, <Shift-S>, and <Shift-Y>.
*/
RESULT v_notop(win, vinf)
WINDOW win;
VIINFO *vinf;
{
switch (vinf->command)
{
case 's':
vinf->oper = 'c';
vinf->command = 'l';
break;
case 'C':
vinf->oper = 'c';
vinf->command = '$';
break;
case 'D':
vinf->oper = 'd';
vinf->command = '$';
break;
case 'S':
vinf->oper = 'c';
vinf->command = '_';
break;
case 'Y':
vinf->oper = 'y';
vinf->command = '_';
break;
}
return viperform(win, vinf);
}
/* This function implements visual commands which are short-cuts for certain
* common ex commands.
*/
RESULT v_notex(win, vinf)
WINDOW win;
VIINFO *vinf;
{
EXINFO xinfb;
MARK tmpmark;
CHAR *word[3];
BUFFER buf;
RESULT result;
int i;
DEFAULT(2);
assert(vinf->command == ELVCTRL('^') || vinf->command == '&'
|| vinf->command == '*' || vinf->command == 'J'
|| vinf->command == 'K' || vinf->command == ELVCTRL('Z'));
/* build & execute an ex command */
memset((char *)&xinfb, 0, sizeof xinfb);
xinfb.window = win;
xinfb.defaddr = *win->state->cursor;
switch (vinf->command)
{
case ELVCTRL('Z'):
result = exstring(win, toCHAR("stop"));
break;
case ELVCTRL('^'):
result = exstring(win, toCHAR("e #"));
break;
case '&':
result = exstring(win, toCHAR("&"));
break;
case '*':
result = exstring(win, toCHAR("errlist"));
break;
case 'J':
/* ":join" */
xinfb.fromaddr = win->state->cursor;
xinfb.from = markline(xinfb.fromaddr);
xinfb.to = xinfb.from + vinf->count - 1;
if (xinfb.to > o_buflines(markbuffer(xinfb.fromaddr)))
{
msg(MSG_ERROR, "not that many lines in buffer");
return RESULT_ERROR;
}
result = ex_join(&xinfb);
if (result == RESULT_COMPLETE && xinfb.newcurs)
{
marksetoffset(win->cursor, markoffset(xinfb.newcurs));
markfree(xinfb.newcurs);
}
break;
default: /* 'K' */
/* ":!ref word" */
buf = markbuffer(win->state->cursor);
if (!o_keywordprg(buf))
{
msg(MSG_ERROR, "keywordprg not set");
return RESULT_ERROR;
}
tmpmark = wordatcursor(&xinfb.defaddr);
if (!tmpmark)
return RESULT_ERROR;
word[0] = bufmemory(tmpmark, &xinfb.defaddr);
if (CHARchr(o_keywordprg(buf), '$'))
{
word[1] = o_filename(buf) ? o_filename(buf) : toCHAR("");
word[2] = NULL;
xinfb.rhs = calculate(o_keywordprg(buf), word, True);
if (!xinfb.rhs)
return RESULT_ERROR; /* message already given */
xinfb.rhs = CHARdup(xinfb.rhs);
}
else /* no $1 in expression */
{
/* append word to command name, with a blank between */
i = CHARlen(o_keywordprg(markbuffer(win->state->cursor)));
xinfb.rhs = (CHAR *)safealloc(sizeof(CHAR), i + CHARlen(word[0]) + 2);
CHARcpy(xinfb.rhs, o_keywordprg(markbuffer(win->state->cursor)));
xinfb.rhs[i] = ' ';
CHARcpy(&xinfb.rhs[i + 1], word[0]);
}
xinfb.command = EX_BANG;
result = ex_bang(&xinfb);
safefree(xinfb.rhs);
break;
}
return result;
}
/* This function implements the # command, which adds or subtracts a value
* to the number that the cursor is on (if it is on a cursor)
*/
RESULT v_number(win, vinf)
WINDOW win;
VIINFO *vinf;
{
CHAR *p; /* used for scanning text */
MARKBUF start, end; /* start of the number */
long number; /* the value of the number */
char asc[12]; /* the result value, as a string */
DEFAULT(1);
/* if not on a number, then fail */
if (!isdigit(scanchar(win->state->cursor)))
{
return RESULT_ERROR;
}
/* locate the start of the number */
for (scanalloc(&p, win->state->cursor); p && isdigit(*p); scanprev(&p))
{
}
if (p)
{
scannext(&p);
start = *scanmark(&p);
}
else
{
start = *win->state->cursor;
marksetoffset(&start, 0);
scanseek(&p, &start);
}
/* fetch the value of the number */
for (number = 0; p && isdigit(*p); scannext(&p))
{
number = number * 10 + *p - '0';
}
/* remember where the number ended */
if (p)
{
end = *scanmark(&p);
}
else
{
end = *win->state->cursor;
marksetoffset(win->state->cursor, o_bufchars(markbuffer(&end)));
}
scanfree(&p);
/* add the argument to the number */
switch (vinf->key2)
{
case '-': number -= vinf->count; break;
case '=': number = vinf->count; break;
default: number += vinf->count;
}
/* convert the result back to a character string */
sprintf(asc, "%ld", number);
/* replace the old value with the new value */
bufreplace(&start, &end, toCHAR(asc), (int)strlen(asc));
/* leave the cursor at the start of the number */
marksetoffset(win->state->cursor, markoffset(&start));
return RESULT_COMPLETE;
}
|