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 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
|
/*
* Highlighted block functions
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
int nowmarking;
/* Global options */
int square = 0; /* Set for rectangle mode */
int lightoff = 0; /* Set if highlighting should turn off
after block operations */
/* Global variables */
P *markb = NULL; /* Beginning and end of block */
P *markk = NULL;
/* Push markb & markk */
typedef struct marksav MARKSAV;
struct marksav {
LINK(MARKSAV) link;
P *markb, *markk;
} markstack = { { &markstack, &markstack} };
MARKSAV markfree = { {&markfree, &markfree} };
int nstack = 0;
int upsh(BW *bw)
{
MARKSAV *m = alitem(&markfree, sizeof(MARKSAV));
m->markb = 0;
m->markk = 0;
if (markk)
pdupown(markk, &m->markk, USTR "upsh");
if (markb)
pdupown(markb, &m->markb, USTR "upsh");
enqueb(MARKSAV, link, &markstack, m);
++nstack;
return 0;
}
int upop(BW *bw)
{
MARKSAV *m = markstack.link.prev;
if (m != &markstack) {
--nstack;
prm(markk);
prm(markb);
markk = m->markk;
if (markk)
markk->owner = &markk;
markb = m->markb;
if (markb)
markb->owner = &markb;
demote(MARKSAV, link, &markfree, m);
if (lightoff)
unmark(bw);
updall();
return 0;
} else
return -1;
}
/* Return true if markb/markk are valid */
/* If r is set, swap markb with markk if necessary */
int autoswap;
int markv(int r)
{
if (markb && markk && markb->b == markk->b && (r == 2 ? markk->byte >= markb->byte : markk->byte > markb->byte) &&
(!square || (r == 2 ? markk->xcol >= markb->xcol : markk->xcol > markb->xcol))) {
return 1;
} else if(autoswap && r && markb && markk && markb->b == markk->b && markb->byte > markk->byte && (!square || markk->xcol < markb->xcol)) {
P *p = pdup(markb, USTR "markv");
prm(markb); markb=0; pdupown(markk, &markb, USTR "markv");
prm(markk); markk=0; pdupown(p, &markk, USTR "markv");
prm(p);
return 1;
} else
return 0;
}
/* Rectangle-mode subroutines */
/* B *pextrect(P *org,long height,long left,long right);
* Copy a rectangle into a new buffer
*
* org points to top-left corner of rectangle.
* height is number of lines in rectangle.
* right is rightmost column of rectangle + 1
*/
B *pextrect(P *org, long int height, long int right)
{
P *p = pdup(org, USTR "pextrect"); /* Left part of text to extract */
P *q = pdup(p, USTR "pextrect"); /* After right part of text to extract */
B *tmp = bmk(NULL); /* Buffer to extract to */
P *z = pdup(tmp->eof, USTR "pextrect"); /* Buffer pointer */
while (height--) {
pcol(p, org->xcol);
pset(q, p);
pcolwse(q, right);
p_goto_eof(z);
binsb(z, bcpy(p, q));
p_goto_eof(z);
binsc(z, '\n');
pnextl(p);
}
prm(p);
prm(q);
prm(z);
return tmp;
}
/* void pdelrect(P *org,long height,long right);
* Delete a rectangle.
*/
void pdelrect(P *org, long int height, long int right)
{
P *p = pdup(org, USTR "pdelrect");
P *q = pdup(p, USTR "pdelrect");
while (height--) {
pcol(p, org->xcol);
pset(q, p);
pcol(q, right);
bdel(p, q);
pnextl(p);
}
prm(p);
prm(q);
}
/* void pclrrect(P *org,long height,long right,int usetabs);
* Blank-out a rectangle.
*/
void pclrrect(P *org, long int height, long int right, int usetabs)
{
P *p = pdup(org, USTR "pclrrect");
P *q = pdup(p, USTR "pclrrect");
while (height--) {
long pos;
pcol(p, org->xcol);
pset(q, p);
pcoli(q, right);
pos = q->col;
bdel(p, q);
pfill(p, pos, usetabs);
pnextl(p);
}
prm(p);
prm(q);
}
/* int ptabrect(P *org,long height,long right)
* Check if there are any TABs in a rectangle
*/
int ptabrect(P *org, long int height, long int right)
{
P *p = pdup(org, USTR "ptabrect");
while (height--) {
int c;
pcol(p, org->xcol);
while ((c = pgetc(p)) != NO_MORE_DATA && c != '\n') {
if (c == '\t') {
prm(p);
return '\t';
} else if (piscol(p) > right)
break;
}
if (c != '\n')
pnextl(p);
}
prm(p);
return ' ';
}
/* Insert rectangle */
void pinsrect(P *cur, B *tmp, long int width, int usetabs)
{
P *p = pdup(cur, USTR "pinsrect"); /* We insert at & move this pointer */
P *q = pdup(tmp->bof, USTR "pinsrect"); /* These are for scanning through 'tmp' */
P *r = pdup(q, USTR "pinsrect");
/* if (width) */
while (pset(r, q), p_goto_eol(q), (q->line != tmp->eof->line || piscol(q))) {
pcol(p, cur->xcol);
if (piscol(p) < cur->xcol)
pfill(p, cur->xcol, usetabs);
binsb(p, bcpy(r, q));
pfwrd(p, q->byte - r->byte);
if (piscol(p) < cur->xcol + width)
pfill(p, cur->xcol + width, usetabs);
if (piseol(p))
pbackws(p);
if (!pnextl(p)) {
binsc(p, '\n');
pgetc(p);
}
if (pgetc(q) == NO_MORE_DATA)
break;
}
prm(p);
prm(q);
prm(r);
}
/* Block functions */
/* Set beginning */
int umarkb(BW *bw)
{
pdupown(bw->cursor, &markb, USTR "umarkb");
markb->xcol = bw->cursor->xcol;
updall();
return 0;
}
int udrop(BW *bw)
{
prm(markk);
if (marking && markb)
prm(markb);
else
umarkb(bw);
return 0;
}
int ubegin_marking(BW *bw)
{
if (nowmarking) {
/* We're marking now... don't stop */
return 0;
} else if (markv(0) && bw->cursor->b==markb->b) {
/* Try to extend current block */
if (bw->cursor->byte==markb->byte) {
pset(markb,markk);
prm(markk); markk=0;
nowmarking = 1;
return 0;
} else if(bw->cursor->byte==markk->byte) {
prm(markk); markk=0;
nowmarking = 1;
return 0;
}
}
/* Start marking - no message */
prm(markb); markb=0;
prm(markk); markk=0;
updall();
nowmarking = 1;
return umarkb(bw);
}
int utoggle_marking(BW *bw)
{
if (markv(0) && bw->cursor->b==markb->b && bw->cursor->byte>=markb->byte && bw->cursor->byte<=markk->byte) {
/* Just clear selection */
prm(markb); markb=0;
prm(markk); markk=0;
updall();
nowmarking = 0;
msgnw(bw->parent, joe_gettext(_("Selection cleared.")));
return 0;
} else if (markk) {
/* Clear selection and start new one */
prm(markb); markb=0;
prm(markk); markk=0;
updall();
nowmarking = 1;
msgnw(bw->parent, joe_gettext(_("Selection started.")));
return umarkb(bw);
} else if (markb && markb->b==bw->cursor->b) {
nowmarking = 0;
if (bw->cursor->byte<markb->byte) {
pdupown(markb, &markk, USTR "utoggle_marking");
prm(markb); markb=0;
pdupown(bw->cursor, &markb, USTR "utoggle_marking");
markb->xcol = bw->cursor->xcol;
} else {
pdupown(bw->cursor, &markk, USTR "utoggle_marking");
markk->xcol = bw->cursor->xcol;
}
updall(); /* Because other windows could be changed */
return 0;
} else {
nowmarking = 1;
msgnw(bw->parent, joe_gettext(_("Selection started.")));
return umarkb(bw);
}
}
int uselect(BW *bw)
{
if (!markb)
umarkb(bw);
return 0;
}
/* Set end */
int umarkk(BW *bw)
{
pdupown(bw->cursor, &markk, USTR "umarkk");
markk->xcol = bw->cursor->xcol;
updall();
return 0;
}
/* Unset marks */
int unmark(BW *bw)
{
prm(markb);
prm(markk);
nowmarking = 0;
updall();
return 0;
}
/* Mark line */
int umarkl(BW *bw)
{
p_goto_bol(bw->cursor);
umarkb(bw);
pnextl(bw->cursor);
umarkk(bw);
utomarkb(bw);
pcol(bw->cursor, bw->cursor->xcol);
return 0;
}
int utomarkb(BW *bw)
{
if (markb && markb->b == bw->b) {
pset(bw->cursor, markb);
return 0;
} else
return -1;
}
int utomarkk(BW *bw)
{
if (markk && markk->b == bw->b) {
pset(bw->cursor, markk);
return 0;
} else
return -1;
}
int uswap(BW *bw)
{
if (markb && markb->b == bw->b) {
P *q = pdup(markb, USTR "uswap");
umarkb(bw);
pset(bw->cursor, q);
prm(q);
return 0;
} else
return -1;
}
int utomarkbk(BW *bw)
{
if (markb && markb->b == bw->b && bw->cursor->byte != markb->byte) {
pset(bw->cursor, markb);
return 0;
} else if (markk && markk->b == bw->b && bw->cursor->byte != markk->byte) {
pset(bw->cursor, markk);
return 0;
} else
return -1;
}
/* Delete block */
int ublkdel(BW *bw)
{
if (markv(1)) {
if (square)
if (bw->o.overtype) {
long ocol = markk->xcol;
pclrrect(markb, markk->line - markb->line + 1, markk->xcol, ptabrect(markb, markk->line - markb->line + 1, markk->xcol));
pcol(markk, ocol);
markk->xcol = ocol;
} else
pdelrect(markb, markk->line - markb->line + 1, markk->xcol);
else
bdel(markb, markk);
if (lightoff)
unmark(bw);
} else {
msgnw(bw->parent, joe_gettext(_("No block")));
return -1;
}
return 0;
}
/* Special delete block function for PICO */
int upicokill(BW *bw)
{
upsh(bw);
umarkk(bw);
if (markv(1)) {
if (square)
if (bw->o.overtype) {
long ocol = markk->xcol;
pclrrect(markb, markk->line - markb->line + 1, markk->xcol, ptabrect(markb, markk->line - markb->line + 1, markk->xcol));
pcol(markk, ocol);
markk->xcol = ocol;
} else
pdelrect(markb, markk->line - markb->line + 1, markk->xcol);
else
bdel(markb, markk);
if (lightoff)
unmark(bw);
} else
udelln(bw);
return 0;
}
/* Move highlighted block */
int ublkmove(BW *bw)
{
if (markv(1)) {
if (markb->b!=bw->b && !modify_logic(bw,markb->b))
return -1;
if (square) {
long height = markk->line - markb->line + 1;
long width = markk->xcol - markb->xcol;
int usetabs = ptabrect(markb, height, markk->xcol);
long ocol = piscol(bw->cursor);
B *tmp = pextrect(markb, height, markk->xcol);
int update_xcol = (bw->cursor->xcol >= markk->xcol && bw->cursor->line >= markb->line && bw->cursor->line <= markk->line);
ublkdel(bw);
/* now we can't use markb and markk until we set them again */
/* ublkdel() frees them */
if (bw->o.overtype) {
/* If cursor was in block, blkdel moves it to left edge of block, so fix it
* back to its original place here */
pcol(bw->cursor, ocol);
pfill(bw->cursor, ocol, ' ');
pdelrect(bw->cursor, height, piscol(bw->cursor) + width);
} else if (update_xcol)
/* If cursor was to right of block, xcol was not properly updated */
bw->cursor->xcol -= width;
pinsrect(bw->cursor, tmp, width, usetabs);
brm(tmp);
if (lightoff)
unmark(bw);
else {
umarkb(bw);
umarkk(bw);
pline(markk, markk->line + height - 1);
pcol(markk, markb->xcol + width);
markk->xcol = markb->xcol + width;
}
return 0;
} else if (bw->cursor->b != markk->b || bw->cursor->byte > markk->byte || bw->cursor->byte < markb->byte) {
long size = markk->byte - markb->byte;
binsb(bw->cursor, bcpy(markb, markk));
bdel(markb, markk);
if (lightoff)
unmark(bw);
else {
umarkb(bw);
umarkk(bw);
pfwrd(markk, size);
}
updall();
return 0;
}
}
msgnw(bw->parent, joe_gettext(_("No block")));
return -1;
}
/* Duplicate highlighted block */
int ublkcpy(BW *bw)
{
if (markv(1)) {
if (square) {
long height = markk->line - markb->line + 1;
long width = markk->xcol - markb->xcol;
int usetabs = ptabrect(markb, height, markk->xcol);
B *tmp = pextrect(markb, height, markk->xcol);
if (bw->o.overtype)
pdelrect(bw->cursor, height, piscol(bw->cursor) + width);
pinsrect(bw->cursor, tmp, width, usetabs);
brm(tmp);
if (lightoff)
unmark(bw);
else {
umarkb(bw);
umarkk(bw);
pline(markk, markk->line + height - 1);
pcol(markk, markb->xcol + width);
markk->xcol = markb->xcol + width;
}
return 0;
} else {
long size = markk->byte - markb->byte;
B *tmp = bcpy(markb, markk);
/* Simple overtype for hex mode */
if (bw->o.hex && bw->o.overtype) {
P *q = pdup(bw->cursor, USTR "ublkcpy");
if (q->byte + size >= q->b->eof->byte)
pset(q, q->b->eof);
else
pfwrd(q, size);
bdel(bw->cursor, q);
prm(q);
}
binsb(bw->cursor, tmp);
if (lightoff)
unmark(bw);
else {
umarkb(bw);
umarkk(bw);
pfwrd(markk, size);
}
updall();
return 0;
}
} else {
msgnw(bw->parent, joe_gettext(_("No block")));
return -1;
}
}
/* Write highlighted block to a file */
/* This is called by ublksave in ufile.c */
/*int dowrite(BW *bw, unsigned char *s, void *object, int *notify)
{
if (notify)
*notify = 1;
if (markv(1)) {
if (square) {
int fl;
int ret = 0;
B *tmp = pextrect(markb,
markk->line - markb->line + 1,
markk->xcol);
if ((fl = bsave(tmp->bof, s, tmp->eof->byte, 0)) != 0) {
msgnw(bw->parent, joe_gettext(msgs[-fl]));
ret = -1;
}
brm(tmp);
if (lightoff)
unmark(bw);
vsrm(s);
return ret;
} else {
int fl;
int ret = 0;
if ((fl = bsave(markb, s, markk->byte - markb->byte, 0)) != 0) {
msgnw(bw->parent, joe_gettext(msgs[-fl]));
ret = -1;
}
if (lightoff)
unmark(bw);
vsrm(s);
return ret;
}
} else {
vsrm(s);
msgnw(bw->parent, USTR _(_("No block")));
return -1;
}
}*/
/* Set highlighted block on a program block */
void setindent(BW *bw)
{
P *p, *q;
long indent;
if (pisblank(bw->cursor))
return;
p = pdup(bw->cursor, USTR "setindent");
q = pdup(p, USTR "setindent");
indent = pisindent(p);
do {
if (!pprevl(p))
goto done;
else
p_goto_bol(p);
} while (pisindent(p) >= indent || pisblank(p));
pnextl(p);
/* Maybe skip blank lines at beginning */
done:
p_goto_bol(p);
p->xcol = piscol(p);
if (markb)
prm(markb);
markb = p;
p->owner = &markb;
do {
if (!pnextl(q))
break;
} while (pisindent(q) >= indent || pisblank(q));
/* Maybe skip blank lines at end */
if (markk)
prm(markk);
q->xcol = piscol(q);
markk = q;
q->owner = &markk;
updall();
}
/* Purity check */
/* Verifies that at least n indentation characters (for non-blank lines) match c */
/* If n is 0 (for urindent), this fails if c is space but indentation begins with tab */
int purity_check(int c, int n)
{
P *p = pdup(markb, USTR "purity_check");
while (p->byte < markk->byte) {
int x;
p_goto_bol(p);
if (!n && c==' ' && brc(p)=='\t') {
prm(p);
return 0;
} else if (!piseol(p))
for (x=0; x!=n; ++x)
if (pgetc(p)!=c) {
prm(p);
return 0;
}
pnextl(p);
}
prm(p);
return 1;
}
/* Left indent check */
/* Verify that there is enough whitespace to do the left indent */
int lindent_check(int c, int n)
{
P *p = pdup(markb, USTR "lindent_check");
int indwid;
if (c=='\t')
indwid = n * p->b->o.tab;
else
indwid = n;
while (p->byte < markk->byte) {
p_goto_bol(p);
if (!piseol(p) && pisindent(p)<indwid) {
prm(p);
return 0;
}
pnextl(p);
}
prm(p);
return 1;
}
/* Indent more */
int urindent(BW *bw)
{
if (square) {
if (markb && markk && markb->b == markk->b && markb->byte <= markk->byte && markb->xcol <= markk->xcol) {
P *p = pdup(markb, USTR "urindent");
do {
pcol(p, markb->xcol);
pfill(p, markb->xcol + bw->o.istep, bw->o.indentc);
} while (pnextl(p) && p->line <= markk->line);
prm(p);
}
} else {
if (!markb || !markk || markb->b != markk->b || bw->cursor->byte < markb->byte || bw->cursor->byte > markk->byte || markb->byte == markk->byte) {
setindent(bw);
} else if ( 1 /* bw->o.purify */) {
P *p = pdup(markb, USTR "urindent");
P *q = pdup(markb, USTR "urindent");
int indwid;
if (bw->o.indentc=='\t')
indwid = bw->o.tab * bw->o.istep;
else
indwid = bw->o.istep;
while (p->byte < markk->byte) {
p_goto_bol(p);
if (!piseol(p)) {
int col;
pset(q, p);
p_goto_indent(q, bw->o.indentc);
col = piscol(q);
bdel(p,q);
pfill(p,col+indwid,bw->o.indentc);
}
pnextl(p);
}
prm(p);
prm(q);
} else if (purity_check(bw->o.indentc,0)) {
P *p = pdup(markb, USTR "urindent");
while (p->byte < markk->byte) {
p_goto_bol(p);
if (!piseol(p))
while (piscol(p) < bw->o.istep) {
binsc(p, bw->o.indentc);
pgetc(p);
}
pnextl(p);
}
prm(p);
} else {
/* Purity failure */
msgnw(bw->parent,joe_gettext(_("Selected lines not properly indented")));
return 1;
}
}
return 0;
}
/* Indent less */
int ulindent(BW *bw)
{
if (square) {
if (markb && markk && markb->b == markk->b && markb->byte <= markk->byte && markb->xcol <= markk->xcol) {
P *p = pdup(markb, USTR "ulindent");
P *q = pdup(p, USTR "ulindent");
do {
pcol(p, markb->xcol);
while (piscol(p) < markb->xcol + bw->o.istep) {
int c = pgetc(p);
if (c != ' ' && c != '\t' && c != bw->o.indentc) {
prm(p);
prm(q);
return -1;
}
}
} while (pnextl(p) && p->line <= markk->line);
pset(p, markb);
do {
pcol(p, markb->xcol);
pset(q, p);
pcol(q, markb->xcol + bw->o.istep);
bdel(p, q);
} while (pnextl(p) && p->line <= markk->line);
prm(p);
prm(q);
}
} else {
if (!markb || !markk || markb->b != markk->b || bw->cursor->byte < markb->byte || bw->cursor->byte > markk->byte || markb->byte == markk->byte) {
setindent(bw);
} else if (1 /* bw->o.purify */ && lindent_check(bw->o.indentc,bw->o.istep)) {
P *p = pdup(markb, USTR "ulindent");
P *q = pdup(markb, USTR "ulindent");
int indwid;
if (bw->o.indentc=='\t')
indwid = bw->o.tab * bw->o.istep;
else
indwid = bw->o.istep;
while (p->byte < markk->byte) {
p_goto_bol(p);
if (!piseol(p)) {
int col;
pset(q, p);
p_goto_indent(q, bw->o.indentc);
col = piscol(q);
bdel(p,q);
pfill(p,col-indwid,bw->o.indentc);
}
pnextl(p);
}
prm(p);
prm(q);
} else if (purity_check(bw->o.indentc,bw->o.istep)) {
P *p = pdup(markb, USTR "ulindent");
P *q = pdup(p, USTR "ulindent");
p_goto_bol(p);
while (p->byte < markk->byte) {
if (!piseol(p)) {
pset(q, p);
while (piscol(q) < bw->o.istep)
pgetc(q);
bdel(p, q);
}
pnextl(p);
}
prm(p);
prm(q);
} else {
/* Purity failure */
msgnw(bw->parent,joe_gettext(_("Selected lines not properly indented")));
return 1;
}
}
return 0;
}
/* Insert a file */
int doinsf(BW *bw, unsigned char *s, void *object, int *notify)
{
if (notify)
*notify = 1;
if (square)
if (markv(2)) {
B *tmp;
long width = markk->xcol - markb->xcol;
long height;
int usetabs = ptabrect(markb,
markk->line - markb->line + 1,
markk->xcol);
tmp = bload(s);
if (berror) {
msgnw(bw->parent, joe_gettext(msgs[-berror]));
brm(tmp);
return -1;
}
if (piscol(tmp->eof))
height = tmp->eof->line + 1;
else
height = tmp->eof->line;
if (bw->o.overtype) {
pclrrect(markb, long_max(markk->line - markb->line + 1, height), markk->xcol, usetabs);
pdelrect(markb, height, width + markb->xcol);
}
pinsrect(markb, tmp, width, usetabs);
pdupown(markb, &markk, USTR "doinsf");
markk->xcol = markb->xcol;
if (height) {
pline(markk, markk->line + height - 1);
pcol(markk, markb->xcol + width);
markk->xcol = markb->xcol + width;
}
brm(tmp);
updall();
return 0;
} else {
msgnw(bw->parent, joe_gettext(_("No block")));
return -1;
} else {
int ret = 0;
B *tmp = bload(s);
if (berror) {
msgnw(bw->parent, joe_gettext(msgs[-berror])), brm(tmp);
ret = -1;
} else
binsb(bw->cursor, tmp);
vsrm(s);
bw->cursor->xcol = piscol(bw->cursor);
return ret;
}
}
/* Filter highlighted block through a UNIX command */
static int filtflg = 0;
static int dofilt(BW *bw, unsigned char *s, void *object, int *notify)
{
int fr[2];
int fw[2];
int flg = 0;
if (notify)
*notify = 1;
if (markb && markk && !square && markb->b == bw->b && markk->b == bw->b && markb->byte == markk->byte) {
flg = 1;
goto ok;
} if (!markv(1)) {
msgnw(bw->parent, joe_gettext(_("No block")));
return -1;
}
ok:
pipe(fr);
pipe(fw);
npartial(bw->parent->t->t);
ttclsn();
#ifdef HAVE_FORK
if (!fork()) {
#else
if (!vfork()) { /* For AMIGA only */
#endif
#ifdef HAVE_PUTENV
unsigned char *fname, *name;
unsigned len;
#endif
signrm();
close(0);
close(1);
close(2);
dup(fw[0]);
dup(fr[1]);
dup(fr[1]);
close(fw[0]);
close(fr[1]);
close(fw[1]);
close(fr[0]);
#ifdef HAVE_PUTENV
fname = vsncpy(NULL, 0, sc("JOE_FILENAME="));
name = bw->b->name ? bw->b->name : (unsigned char *)"Unnamed";
if((len = slen(name)) >= 512) /* limit filename length */
len = 512;
fname = vsncpy(sv(fname), name, len);
putenv((char *)fname);
vsrm(fname);
#endif
execl("/bin/sh", "/bin/sh", "-c", s, NULL);
_exit(0);
}
close(fr[1]);
close(fw[0]);
#ifdef HAVE_FORK
if (fork()) {
#else
if (vfork()) { /* For AMIGA only */
#endif
close(fw[1]);
if (square) {
B *tmp;
long width = markk->xcol - markb->xcol;
long height;
int usetabs = ptabrect(markb,
markk->line - markb->line + 1,
markk->xcol);
tmp = bread(fr[0], MAXLONG);
if (piscol(tmp->eof))
height = tmp->eof->line + 1;
else
height = tmp->eof->line;
if (bw->o.overtype) {
pclrrect(markb, markk->line - markb->line + 1, markk->xcol, usetabs);
pdelrect(markb, long_max(height, markk->line - markb->line + 1), width + markb->xcol);
} else
pdelrect(markb, markk->line - markb->line + 1, markk->xcol);
pinsrect(markb, tmp, width, usetabs);
pdupown(markb, &markk, USTR "dofilt");
markk->xcol = markb->xcol;
if (height) {
pline(markk, markk->line + height - 1);
pcol(markk, markb->xcol + width);
markk->xcol = markb->xcol + width;
}
if (lightoff)
unmark(bw);
brm(tmp);
updall();
} else {
P *p = pdup(markk, USTR "dofilt");
if (!flg)
prgetc(p);
bdel(markb, p);
binsb(p, bread(fr[0], MAXLONG));
if (!flg) {
pset(p,markk);
prgetc(p);
bdel(p,markk);
}
prm(p);
if (lightoff)
unmark(bw);
}
close(fr[0]);
wait(NULL);
wait(NULL);
} else {
if (square) {
B *tmp = pextrect(markb,
markk->line - markb->line + 1,
markk->xcol);
bsavefd(tmp->bof, fw[1], tmp->eof->byte);
} else
bsavefd(markb, fw[1], markk->byte - markb->byte);
close(fw[1]);
_exit(0);
}
vsrm(s);
ttopnn();
if (filtflg)
unmark(bw);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
}
static B *filthist = NULL;
static void markall(BW *bw)
{
pdupown(bw->cursor->b->bof, &markb, USTR "markall");
markb->xcol = 0;
pdupown(bw->cursor->b->eof, &markk, USTR "markall");
markk->xcol = piscol(markk);
updall();
}
static int checkmark(BW *bw)
{
if (!markv(1))
if (square)
return 2;
else {
markall(bw);
filtflg = 1;
return 1;
} else {
filtflg = 0;
return 0;
}
}
int ufilt(BW *bw)
{
#ifdef __MSDOS__
msgnw(bw->parent, joe_gettext(_("Sorry, no sub-processes in DOS (yet)")));
return -1;
#else
switch (checkmark(bw)) {
case 0:
if (wmkpw(bw->parent, joe_gettext(_("Command to filter block through (^C to abort): ")), &filthist, dofilt, NULL, NULL, utypebw, NULL, NULL, locale_map, 1))
return 0;
else
return -1;
case 1:
if (wmkpw(bw->parent, joe_gettext(_("Command to filter file through (^C to abort): ")), &filthist, dofilt, NULL, NULL, utypebw, NULL, NULL, locale_map, 1))
return 0;
else
return -1;
case 2:
default:
msgnw(bw->parent, joe_gettext(_("No block")));
return -1;
}
#endif
}
/* Force region to lower case */
int ulower(BW *bw)
{
if (markv(1)) {
P *q;
P *p;
int c;
B *b = bcpy(markb,markk);
/* Leave one character in buffer to keep pointers set properly... */
q = pdup(markk, USTR "ulower");
prgetc(q);
bdel(markb,q);
b->o.charmap = markb->b->o.charmap;
p=pdup(b->bof, USTR "ulower");
while ((c=pgetc(p))!=NO_MORE_DATA) {
c = joe_tolower(b->o.charmap,c);
binsc(q,c);
pgetc(q);
}
prm(p);
bdel(q,markk);
prm(q);
brm(b);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
} else
return -1;
}
/* Force region to upper case */
int uupper(BW *bw)
{
if (markv(1)) {
P *q;
P *p;
int c;
B *b = bcpy(markb,markk);
q = pdup(markk, USTR "uupper");
prgetc(q);
bdel(markb,q);
b->o.charmap = markb->b->o.charmap;
p=pdup(b->bof, USTR "uupper");
while ((c=pgetc(p))!=NO_MORE_DATA) {
c = joe_toupper(b->o.charmap,c);
binsc(q,c);
pgetc(q);
}
prm(p);
bdel(q,markk);
prm(q);
brm(b);
bw->cursor->xcol = piscol(bw->cursor);
return 0;
} else
return -1;
}
/* Get sum, sum of squares, and return count of
* a block of numbers.
*
* avg = sum/count
*
* stddev = sqrt( (a-avg)^2 + (b-avg)^2 + (c-avg)^2 )
* = sqrt( a^2-2*a*avg+avg^2 + b^2-2*b*avg+avg^2 +c^2-2*c*avg+avg^2 )
* = sqrt( a^2+b^2+c^2 + 3*avg^2 - 2*avg*(a+b+c) )
* = sqrt( sumsq + count*avg^2 - 2*avg*sum )
*
*/
int blksum(double *sum, double *sumsq)
{
unsigned char buf[80];
if (markv(1)) {
P *q = pdup(markb, USTR "blksum");
int x;
int c;
double accu = 0.0;
double accusq = 0.0;
double v;
int count = 0;
long left = markb->xcol;
long right = markk->xcol;
while (q->byte < markk->byte) {
/* Skip until we're within columns */
while (q->byte < markk->byte && square && (piscol(q) < left || piscol(q) >= right))
pgetc(q);
/* Skip to first number */
while (q->byte < markk->byte && (!square || (piscol(q) >= left && piscol(q) < right))) {
c=pgetc(q);
if ((c >= '0' && c <= '9') || c == '.' || c == '-') {
/* Copy number into buffer */
buf[0]=c; x=1;
while (q->byte < markk->byte && (!square || (piscol(q) >= left && piscol(q) < right))) {
c=pgetc(q);
if ((c >= '0' && c <= '9') || c == 'e' || c == 'E' ||
c == 'p' || c == 'P' || c == 'x' || c == 'X' ||
c == '.' || c == '-' || c == '+' ||
(c >= 'a' && c <= 'f') || (c >= 'A' && c<='F')) {
if(x != 79)
buf[x++]=c;
} else
break;
}
/* Convert number to floating point, add it to total */
buf[x] = 0;
v = strtod((char *)buf,NULL);
++count;
accu += v;
accusq += v*v;
break;
}
}
}
prm(q);
*sum = accu;
*sumsq = accusq;
return count;
} else
return -1;
}
/* Get a (possibly square) block into a buffer */
unsigned char *blkget()
{
if (markv(1)) {
P *q;
unsigned char *buf=joe_malloc(markk->byte-markb->byte+1);
unsigned char *s=buf;
long left = markb->xcol;
long right = markk->xcol;
q = pdup(markb, USTR "blkget");
while (q->byte < markk->byte) {
/* Skip until we're within columns */
while (q->byte < markk->byte && square && (piscol(q) < left || piscol(q) >= right))
pgetc(q);
/* Copy text into buffer */
while (q->byte < markk->byte && (!square || (piscol(q) >= left && piscol(q) < right))) {
*s++ = pgetc(q);
}
/* Add a new line if we went past right edge of column */
if (square && q->byte<markk->byte && piscol(q) >= right)
*s++ = '\n';
}
prm(q);
*s = 0;
return buf;
} else
return 0;
}
|