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
|
/* exedit.c */
/* Copyright 1995 by Steve Kirkendall */
char id_exedit[] = "$Id: exedit.c,v 2.46 1998/03/09 18:28:18 steve Exp $";
#include "elvis.h"
/* This command implements the :insert, :append, and :change commands */
RESULT ex_append(xinf)
EXINFO *xinf;
{
MARK where;
assert(xinf->command == EX_APPEND || xinf->command == EX_CHANGE
|| xinf->command == EX_INSERT);
/* different behavior, depending on the command... */
if (xinf->command == EX_CHANGE)
{
cutyank('\0', xinf->fromaddr, xinf->toaddr, 'L', True);
where = markdup(xinf->fromaddr);
}
else if (xinf->command == EX_APPEND)
{
where = markdup(xinf->toaddr);
}
else
{
where = markdup(xinf->fromaddr);
}
/* Was the new text given as an argument to the command? */
if (xinf->rhs)
{
/* yes, insert the new text at the appropriate place */
bufreplace(where, where, xinf->rhs, (long)CHARlen(xinf->rhs));
markaddoffset(where, CHARlen(xinf->rhs));
}
xinf->newcurs = where;
return RESULT_COMPLETE;
}
/* This function implements the :delete and :yank command */
RESULT ex_delete(xinf)
EXINFO *xinf;
{
/* check the cut buffer name */
if (xinf->cutbuf && !isalnum(xinf->cutbuf) &&
xinf->cutbuf != '^' && xinf->cutbuf != '<' && xinf->cutbuf != '>')
{
msg(MSG_ERROR, "bad cut buffer");
return RESULT_ERROR;
}
/* do the command */
cutyank(xinf->cutbuf, xinf->fromaddr, xinf->toaddr, (CHAR)'L', (BOOLEAN)(xinf->command == EX_DELETE));
return RESULT_COMPLETE;
}
RESULT ex_global(xinf)
EXINFO *xinf;
{
CHAR *cp;
long lenln, endln;
RESULT ret = RESULT_COMPLETE;
MARK cursor;
MARK orig;
MARKBUF thisln;
assert(xinf->command == EX_GLOBAL || xinf->command == EX_VGLOBAL);
/* Default command is p, which should NOT be required */
if (!xinf->rhs)
xinf->rhs = CHARdup(toCHAR("p"));
/* a command is required */
if (!xinf->rhs)
{
msg(MSG_ERROR, "[s]$1 requires a command", xinf->cmdname);
return RESULT_ERROR;
}
/* only works when applied to window's main buffer */
if (!xinf->window->state->acton || xinf->window->state->acton->acton)
{
msg(MSG_ERROR, "[s]$1 only works on main buffer", xinf->cmdname);
return RESULT_ERROR;
}
/* ":g!" is like ":v" */
if (xinf->bang)
xinf->command = EX_VGLOBAL;
/* remember the cursor's original position. Inside the following loop,
* we'll force the cursor onto each matching line; when we're done,
* we'll move the cursor back and return the final position so the
* experform() function can move the cursor there in the conventional
* way -- that will be important if we switch buffers.
*/
if (xinf->window->state->acton)
cursor = xinf->window->state->acton->cursor;
else
cursor = xinf->window->cursor;
if (markbuffer(cursor) != markbuffer(xinf->fromaddr))
{
marksetbuffer(cursor, markbuffer(xinf->fromaddr));
marksetoffset(cursor, markoffset(xinf->fromaddr));
}
orig = markdup(cursor);
/* for each line... */
(void)scanalloc(&cp, xinf->fromaddr);
ret = RESULT_COMPLETE;
while (cp && markoffset(xinf->fromaddr) < markoffset(xinf->toaddr))
{
/* find the end of this line */
for (lenln = 0; cp && *cp != '\n'; lenln++)
(void)scannext(&cp);
if (cp)
{
(void)scannext(&cp);
lenln++;
}
/* move "fromaddr" to end of this line (start of next line) */
thisln = *xinf->fromaddr;
endln = cp ? markoffset(scanmark(&cp)) : markoffset(xinf->toaddr);
marksetoffset(xinf->fromaddr, endln);
/* is this a selected line? */
if ((regexec(xinf->re, &thisln, True) ? EX_GLOBAL : EX_VGLOBAL)
== xinf->command)
{
/* move the cursor to the matching line */
marksetoffset(cursor, endln - lenln);
/* free the scan pointer -- can't make changes
* while scanning.
*/
scanfree(&cp);
/* execute the command */
ret = exstring(xinf->window, xinf->rhs);
/* reallocate the scan pointer */
(void)scanalloc(&cp, xinf->fromaddr);
/* if the ex command failed, then exit */
if (ret != RESULT_COMPLETE)
break;
}
/* if user wants to abort operation, then exit */
if (guipoll(False))
break;
}
scanfree(&cp);
/* move the cursor back to its original position, and then return
* the final position so the cursor will be moved there in a graceful
* way.
*/
xinf->newcurs = markdup(cursor);
if (markbuffer(cursor) != markbuffer(orig))
marksetbuffer(cursor, markbuffer(orig));
marksetoffset(cursor, markoffset(orig));
markfree(orig);
return ret;
}
RESULT ex_join(xinf)
EXINFO *xinf;
{
CHAR prevchar; /* character before newline */
CHAR *cp; /* used while scanning for newlines */
long njoined; /* number of lines joined */
long newlines; /* number of newlines to be clobbered */
long nspaces; /* number of spaces to insert */
MARK start, end; /* region around a newline */
long offset; /* position of last change */
CHAR *endlist; /* string of sentence ending punctuation */
static CHAR spaces[3] = {' ', ' ', ' '};
/* initialize "offset" just to silence a compiler warning */
offset = 0;
/* initialize endlist from options (if set) or literals */
endlist = (o_sentenceend ? o_sentenceend : toCHAR(".!?"));
/* We're going to be replacing newlines with blanks. The number of
* newlines we want to replace is equal to the number lines affected
* minus one... except that if the user requested a "join" of a single
* line then we should assume we're supposed to join two lines.
*/
newlines = xinf->to - xinf->from;
if (xinf->from + newlines > o_buflines(markbuffer(xinf->fromaddr)))
{
msg(MSG_ERROR, "nothing to join with this line");
return RESULT_ERROR;;
}
njoined = newlines + 1;
/* scan the text for newlines */
prevchar = ' ';
for (scanalloc(&cp, xinf->fromaddr); cp && newlines > 0; scannext(&cp))
{
/* if newline, then clobber it */
if (*cp == '\n')
{
start = markdup(scanmark(&cp));
offset = markoffset(start);
if (scannext(&cp))
{
/* figure out how many spaces to insert */
if (xinf->bang || *cp == ')' || isspace(prevchar))
nspaces = 0;
else if (CHARchr(toCHAR(endlist), prevchar))
nspaces = o_sentencegap;
else
nspaces = 1;
/* skip any leading whitespace in next line */
while (!xinf->bang && cp && (*cp == ' ' || *cp == '\t'))
{
scannext(&cp);
}
/* Find the end mark */
end = (cp ? markdup(scanmark(&cp))
: markalloc(markbuffer(start), o_bufchars(markbuffer(start))));
/* free the scan context during the change;
* can't mix scanning & updates.
*/
scanfree(&cp);
/* replace the newline (and trailing whitespace)
* with a given number of spaces.
*/
bufreplace(start, end, spaces, nspaces);
marksetoffset(end, offset + nspaces - 1);
/* NOTE: the "- 1" is to compensate for
* the scannext() at the top of the loop
*/
/* resume scanning */
scanalloc(&cp, end);
markfree(end);
if (nspaces > 0)
{
prevchar = ' ';
}
}
markfree(start);
/* All that to clobber one newline! */
newlines--;
}
else if ((*cp != '"' && *cp != ')') || isspace(prevchar))
{
/* remember the character. When we hit a newline,
* the previous character will be checked to determine
* how many spaces to insert. Note that we ignore
* quote and parenthesis characters except after a
* blank.
*/
prevchar = *cp;
}
}
scanfree(&cp);
/* Choose a new cursor position: at the start of last joint */
xinf->newcurs = markalloc(markbuffer(xinf->fromaddr), offset);
/* Report it */
if (o_report != 0 && njoined >= o_report)
{
msg(MSG_INFO, "[d]$1 lines joined", njoined);
}
return RESULT_COMPLETE;
}
RESULT ex_move(xinf)
EXINFO *xinf;
{
long oldfrom, oldto, olddest, len;
/* detect errors */
oldfrom = markoffset(xinf->fromaddr);
oldto = markoffset(xinf->toaddr);
olddest = markoffset(xinf->destaddr);
if (markbuffer(xinf->fromaddr) == markbuffer(xinf->destaddr)
&& oldfrom < olddest
&& olddest < oldto)
{
msg(MSG_ERROR, "destination can't be inside source");
return RESULT_ERROR;
}
/* copy the text. Adjust the offsets explicitly */
bufpaste(xinf->destaddr, xinf->fromaddr, xinf->toaddr);
len = oldto - oldfrom;
if (olddest <= oldfrom)
{
oldfrom += len;
oldto += len;
}
/* leave the cursor on the last line of the destination */
xinf->newcurs = markalloc(markbuffer(xinf->fromaddr), olddest+len-1);
marksetoffset(xinf->newcurs, markoffset(
(*dmnormal.move)(xinf->window, xinf->newcurs, 0L, 0L, False)));
/* If moving (not copying) then delete source */
if (xinf->command == EX_MOVE)
{
marksetoffset(xinf->fromaddr, oldfrom);
marksetoffset(xinf->toaddr, oldto);
bufreplace(xinf->fromaddr, xinf->toaddr, NULL, 0);
}
return RESULT_COMPLETE;
}
RESULT ex_print(xinf)
EXINFO *xinf;
{
long last; /* offset of start of last line */
PFLAG pflag; /* how to print */
/* generate a pflag from the command name and any supplied pflag */
switch (xinf->pflag)
{
case PF_NONE:
case PF_PRINT:
pflag = (xinf->command == EX_NUMBER || o_number(xinf->window))
? ((xinf->command == EX_LIST || o_list(xinf->window)) ? PF_NUMLIST : PF_NUMBER)
: ((xinf->command == EX_LIST || o_list(xinf->window)) ? PF_LIST : PF_PRINT);
break;
case PF_LIST:
pflag = (xinf->command == EX_NUMBER || o_number(xinf->window)) ? PF_NUMLIST : PF_LIST;
break;
case PF_NUMBER:
pflag = (xinf->command == EX_LIST || o_list(xinf->window))? PF_NUMLIST : PF_NUMBER;
break;
default:
pflag = PF_NUMLIST;
break;
}
/* print the lines */
last = exprintlines(xinf->window, xinf->fromaddr, xinf->to - xinf->from + 1, pflag);
/* disable autoprinting for this command */
xinf->pflag = PF_NONE;
/* leave the cursor at the start of the last line */
xinf->newcurs = markalloc(markbuffer(xinf->fromaddr), last);
return RESULT_COMPLETE;
}
RESULT ex_put(xinf)
EXINFO *xinf;
{
MARK newcurs;
newcurs = cutput(xinf->cutbuf, xinf->window, xinf->fromaddr, (BOOLEAN)(xinf->from > 0), False, False);
if (newcurs)
{
xinf->newcurs = markdup(newcurs);
return RESULT_COMPLETE;
}
return RESULT_ERROR;
}
RESULT ex_read(xinf)
EXINFO *xinf;
{
long offset;
if (!xinf->rhs && xinf->nfiles != 1)
{
msg(MSG_ERROR, "filename required");
return RESULT_ERROR;
}
/* remember where we started inserting */
offset = markoffset(xinf->toaddr);
xinf->newcurs = markdup(xinf->toaddr);
/* read in the text */
if (!bufread(xinf->toaddr, xinf->rhs ? tochar8(xinf->rhs) : xinf->file[0]))
{
return RESULT_ERROR;
}
/* Choose a place to leave the cursor. If reading due to visual <:>
* command, this should be the start of the first line read; else it
* should be the start of the last line read.
*/
if (xinf->window->state->flags & ELVIS_1LINE)
{
marksetoffset(xinf->newcurs, offset);
}
else
{
marksetoffset(xinf->newcurs, markoffset(
(*xinf->window->md->move)(xinf->window, xinf->newcurs, -1, 0, False)));
}
return RESULT_COMPLETE;
}
/* This function implements the :< and :> commands. It is also used to do
* the real work for the visual <<> and <>> operators.
*/
RESULT ex_shift(xinf)
EXINFO *xinf;
{
long shift; /* amount to shift by */
long ws; /* amount of whitespace currently */
CHAR *cp; /* used for scanning through a line's whitespace */
long line; /* used for counting through line numbers */
MARKBUF start; /* start of the line */
MARKBUF end; /* end of the line's whitespace */
CHAR str[50];/* buffer for holding whitespace */
long i;
/* compute the amount of shifting required */
shift = o_shiftwidth(markbuffer(&xinf->defaddr)) * xinf->multi;
if (xinf->command == EX_SHIFTL)
{
shift = -shift;
}
/* for each line... */
start = xinf->defaddr;
for (line = xinf->from; line <= xinf->to; line++)
{
/* count the current whitespace */
scanalloc(&cp, marksetline(&start, line));
for (ws = 0; cp && (*cp == ' ' || *cp == '\t'); scannext(&cp))
{
if (*cp == ' ')
{
ws++;
}
else
{
ws = ws + o_tabstop(markbuffer(&xinf->defaddr))
- ws % o_tabstop(markbuffer(&xinf->defaddr));
}
}
end = *scanmark(&cp);
/* if this is an empty line, and no ! was given on the command
* line, then do nothing to this line.
*/
if (ws == 0 && *cp == '\n' && !xinf->bang)
{
scanfree(&cp);
continue;
}
scanfree(&cp);
/* compute the amount of whitespace we want to have */
ws += shift;
if (ws < 0)
{
ws = 0;
}
/* Replace the old whitespace with new whitespace. Since our
* buffer for holding new whitespace is of limited size, we
* may need to make several bufreplace() calls to do this.
*/
while (markoffset(&start) != markoffset(&end) || ws > 0)
{
/* build new whitespace (as much of it as possible) */
i = 0;
if (o_autotab(markbuffer(&xinf->defaddr)))
{
for (;
ws >= o_tabstop(markbuffer(&xinf->defaddr))
&& i < QTY(str);
i++, ws -= o_tabstop(markbuffer(&xinf->defaddr)))
{
str[i] = '\t';
}
}
for (; ws > 0 && i < QTY(str); i++, ws--)
{
str[i] = ' ';
}
/* replace old whitespace with new */
bufreplace(&start, &end, str, i);
markaddoffset(&start, i);
marksetoffset(&end, markoffset(&start));
}
}
if (o_report != 0 && xinf->to - xinf->from + 1 >= o_report)
msg(MSG_INFO,"[ds]$1 lines $2ed",
xinf->to - xinf->from + 1, xinf->cmdname);
return RESULT_COMPLETE;
}
/* This function implements the :substitute command, and the :& and :~
* variations of that command. It is also used to perform the real work
* of visual <&> command.
*/
RESULT ex_substitute(xinf)
EXINFO *xinf;
{
CHAR *opt; /* substitution options */
long chline; /* # of lines changed */
long chsub; /* # of substitutions made */
long cursoff;/* offset where cursor should be moved to */
static PFLAG xpflag; /* printing flag */
static BOOLEAN xoptg; /* boolean option: substitute globally in line? */
static BOOLEAN xoptx; /* boolean option: execute instead of substitute? */
static long xcount; /* numeric option: which instance in each line to sub */
PFLAG pflag; /* printing flag */
BOOLEAN optg; /* boolean option: substitute globally in line? */
BOOLEAN optx; /* boolean option: execute instead of substitute? */
long count; /* numeric option: which instance in each line to sub */
MARKBUF posn; /* position within a line to be replaced */
long instance;/* number of instances matched so far within line */
CHAR *newp; /* replacement text */
CHAR *scan; /* used for scanning to find end of line */
int match;
assert(xinf->command == EX_SUBSTITUTE || xinf->command == EX_SUBAGAIN);
/* initialize "cursoff" just to silence a compiler warning */
cursoff = 0;
/* all options default to their stored values */
pflag = xpflag;
optg = xoptg;
optx = xoptx;
count = xcount;
/* parse arguments */
if (xinf->command == EX_SUBAGAIN)
{
/* same regular expression as last time */
xinf->re = regcomp(toCHAR(""), xinf->window->state->cursor);
if (!xinf->re)
{
/* error message already given by regcomp() */
return RESULT_ERROR;
}
/* same replacement text as last time */
newp = regtilde(toCHAR(o_magic ? "~" : "\\~"));
/* if visual "&", then turn off the "p", "x", and "c" options */
if (xinf->bang)
{
pflag= PF_NONE;
xoptx = PF_NONE;
}
}
else /* xinf->command == CMD_SUBSTITUTE */
{
/* generate the new text */
newp = regtilde(xinf->lhs ? xinf->lhs : toCHAR(""));
/* analyse the option string */
if (!o_edcompatible)
{
pflag = xinf->pflag;
optg = optx = False;
count = 0;
}
for (opt = xinf->rhs; opt && *opt; opt++)
{
switch (*opt)
{
case 'g':
optg = (BOOLEAN)!optg;
break;
case 'x':
optx = (BOOLEAN)!optx;
break;
case 'c':
msg(MSG_ERROR, "'c' not implemented - use autoselect option");
return RESULT_ERROR;
case 'p':
pflag = (pflag==PF_PRINT) ? PF_PRINT : PF_NONE;
break;
case 'l':
pflag = (pflag==PF_LIST) ? PF_LIST : PF_NONE;
break;
case '#':
pflag = (pflag==PF_NUMBER) ? PF_NUMBER : PF_NONE;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
count = 0;
do
{
count = count * 10 + *opt++ - '0';
} while (isdigit(*opt));
opt--;
break;
default:
if (!isspace(*opt))
{
msg(MSG_ERROR, "[C]unsupported flag '$1'", *opt);
return RESULT_ERROR;
}
}
}
/* sanity checks */
if (count != 0 && optg)
{
msg(MSG_ERROR, "can't mix number and 'g' flag");
return RESULT_ERROR;
}
/* default behavior is to either replace only first instance,
* or all instances, depending on the "gdefault" option.
*/
if (count == 0 && !optg)
{
if (o_gdefault && !o_edcompatible)
optg = True;
else
count = 1;
}
/* remember the flags, unless "saveregexp" is off */
if (o_saveregexp)
{
xpflag = pflag;
xoptg = optg;
xoptx = optx;
xcount = count;
}
}
/* if no replacement text, fail */
if (!newp)
{
return RESULT_ERROR;
}
/* this command does its own printing; disable auto printing */
xinf->pflag = PF_NONE;
/* reset the change counters */
chline = chsub = 0L;
/* for each line in the range... */
for (posn = *xinf->fromaddr; markoffset(&posn) < markoffset(xinf->toaddr); )
{
/* for each instance within the line... */
for (instance = 0, match = regexec(xinf->re, &posn, True);
match && (optg || instance < count);
match = regexec(xinf->re, &posn, False))
{
/* increment the substitution change counter */
chsub++;
instance++;
/* if this is an instance we care about... */
if (optg || instance == count)
{
/* Either execute the replacement, or perform
* the substitution
*/
opt = regsub(xinf->re, newp, (BOOLEAN)!optx);
if (!opt)
{
return RESULT_ERROR;
}
if (optx)
{
exstring(xinf->window, opt);
}
safefree(opt);
/* remember the offset of this change so we can
* move the cursor there later.
*/
cursoff = xinf->re->startp[0];
}
/* Move "posn" to the end of the matched region. If
* the regexp could conceivably match a zero-length
* string, then skip one character.
*/
marksetoffset(&posn, xinf->re->endp[0]);
if (xinf->re->minlen == 0)
{
/* markaddoffset(&posn, 1);*/
if (scanchar(&posn) == '\n')
break;
}
}
/* if any changes were made, then increment chline */
if (optg ? instance > 0 : instance == count)
{
chline++;
}
/* scan forward for the end of the line */
for (scanalloc(&scan, &posn); scan && *scan != '\n'; scannext(&scan))
{
}
if (scan)
scannext(&scan);
if (scan)
posn = *scanmark(&scan);
else
marksetoffset(&posn, o_bufchars(markbuffer(xinf->fromaddr)));
scanfree(&scan);
}
/* If done from within a ":g" command, or used with "x" flag,
* or if ":set report=0" then finish silently.
*/
if (xinf->global || optx || o_report == 0)
{
return RESULT_COMPLETE;
}
/* Reporting */
if (chsub == 0)
{
msg(MSG_WARNING, "substitution failed");
}
else if (chline >= o_report)
{
msg(MSG_INFO, "[dd]$1 substitutions on $2 lines", chsub, chline);
xinf->newcurs = markalloc(xinf->re->buffer, cursoff);
}
return RESULT_COMPLETE;
}
RESULT ex_undo(xinf)
EXINFO *xinf;
{
long l = 1;
assert(xinf->command == EX_UNDO || xinf->command == EX_REDO);
/* choose an undo/redo level to recover */
if (xinf->lhs)
{
if (!calcnumber(xinf->lhs) || (l = CHAR2long(xinf->lhs)) < 1)
{
msg(MSG_ERROR, "bad undo level");
return RESULT_ERROR;
}
}
/* if redo, then negate the undo value */
if (xinf->command == EX_REDO)
l = -l;
/* try to revert to the undo level */
l = bufundo(xinf->window->cursor, l);
/* if successful, adjust the cursor position */
if (l >= 0)
{
marksetoffset(xinf->window->cursor, l);
return RESULT_COMPLETE;
}
return RESULT_ERROR;
}
RESULT ex_write(xinf)
EXINFO *xinf;
{
char *name;
BOOLEAN success;
if (xinf->rhs)
{
name = tochar8(xinf->rhs);
}
else if (xinf->nfiles >= 1)
{
assert(xinf->nfiles == 1);
name = xinf->file[0];
}
else
{
name = tochar8(o_filename(markbuffer(xinf->fromaddr)));
if (!name)
{
msg(MSG_ERROR, "no file name");
return RESULT_ERROR; /* nishi */
}
}
/* if writing to a different filename, remember that name */
if (name[0] != '!'
&& o_filename(markbuffer(xinf->fromaddr))
&& CHARcmp(name, o_filename(markbuffer(xinf->fromaddr))))
{
if (name[0] == '>' && name[1] == '>')
optprevfile(toCHAR(name + 2), 1);
else
optprevfile(toCHAR(name), 1);
}
/* actually write the file */
success = bufwrite(xinf->fromaddr, xinf->toaddr, name, xinf->bang);
return success ? RESULT_COMPLETE : RESULT_ERROR;
}
RESULT ex_z(xinf)
EXINFO *xinf;
{
CHAR type = '+'; /* type of window to show */
long count = o_window; /* number of lines to show */
PFLAG pflag; /* how to show */
long line = xinf->from; /* first line to show */
long offset;
CHAR *scan;
/* choose a default printing style, from options */
if (o_number(xinf->window))
if (o_list(xinf->window))
pflag = PF_NUMLIST;
else
pflag = PF_NUMBER;
else
if (o_list(xinf->window))
pflag = PF_LIST;
else
pflag = PF_NUMBER;
/* If we were given arguments, then parse them */
for (scan = xinf->rhs; scan && *scan; scan++)
{
switch (*scan)
{
case '-':
case '+':
case '.':
case '^':
case '=':
type = *scan;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
for (count = 0; isdigit(*scan); scan++)
{
count = count * 10 + *scan - '0';
}
scan--; /* we went one character too far */
break;
case 'l':
if (pflag == PF_NUMBER || pflag == PF_NUMLIST)
pflag = PF_NUMLIST;
else
pflag = PF_LIST;
break;
case '#':
if (pflag == PF_LIST || pflag == PF_NUMLIST)
pflag = PF_NUMLIST;
else
pflag = PF_NUMBER;
break;
case ' ':
case '\t':
case 'p':
/* ignore */
break;
default:
msg(MSG_ERROR, "bad argument to :z");
return RESULT_ERROR;
}
}
/* choose the first line, based on the type */
switch (type)
{
case '-': /* show the given line at the bottom */
line = xinf->from - count + 1;
break;
case '+': /* show the given line at the top */
line = xinf->from;
break;
case '.': /* show the given line in the middle */
line = xinf->from - count/2;
break;
case '^': /* show the window before the current line */
line = xinf->from - count * 2 + 1;
break;
case '=': /* show it in the middle, surrounded by lines of hyphens */
count -= 2;
line = xinf->from - count / 2;
break;
}
/* protect against readed past top or bottom of buffer */
if (line < 1)
{
count -= 1 - line;
line = 1;
}
if (line + count > o_buflines(markbuffer(xinf->fromaddr)))
{
count -= line - o_buflines(markbuffer(xinf->fromaddr));
}
/* construct a mark for the first line */
xinf->newcurs = markdup(xinf->fromaddr);
marksetline(xinf->newcurs, line);
/* print the lines */
if (type == '=')
{
/* for '=', we need to add lines of hyphens around given line */
if (line < xinf->from)
{
exprintlines(xinf->window, xinf->newcurs, xinf->from - line, pflag);
}
drawextext(xinf->window, toCHAR("-------------------------------------------------------------------------------\n"), 80);
exprintlines(xinf->window, xinf->fromaddr, 1, pflag);
drawextext(xinf->window, toCHAR("-------------------------------------------------------------------------------\n"), 80);
count -= (xinf->from - line) + 1;
if (count > 0)
{
marksetline(xinf->newcurs, xinf->from + 1);
exprintlines(xinf->window, xinf->newcurs, count, pflag);
}
/* leave the cursor on the given line */
marksetoffset(xinf->newcurs, markoffset(xinf->fromaddr));
}
else
{
/* print the lines all at once */
offset = exprintlines(xinf->window, xinf->newcurs, count, pflag);
/* leave the cursor at the start of the last line */
marksetoffset(xinf->newcurs, offset);
}
return RESULT_COMPLETE;
}
|