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
|
/*
| srch_rep.c
|
| $Header: /home/hugh/sources/aee/RCS/srch_rep.c,v 1.8 2002/02/04 05:37:57 hugh Exp $
*/
/*
|
| Copyright (c) 1986 - 1988, 1991 - 1996 Hugh Mahon.
|
*/
#include "aee.h"
int
search(move_cursor, start_line, offset, pointer, s_str_off, srch_short, disp) /* search for srch_str in file after cursor */
int move_cursor;/* should the cursor be moved by search to the found string */
struct text *start_line;/* these parameters allow for better meta handling */
int offset;
char *pointer;
int s_str_off;
int srch_short;
int disp; /* boolean for whether or not to display message */
{
int end_of_line;
int iter; /* position in line of start of suspect string */
int found;
int found2;
int iter2; /* position in line of end of suspect string */
int increment;
int moved_lines;
char x;
char x1, x2;
char *direction;
char *srch_1; /* pointer to start of suspect string */
char *srch_2; /* pointer to next character of string */
char *srch_3;
repl_length = 0;
if (forward)
direction = "d";
else
direction = "u";
end_of_line = FALSE;
if ((srch_str == NULL) || (*srch_str == (char) NULL))
return(FALSE);
if ((move_cursor) && (disp))
{
wmove(com_win,0,0);
wclrtoeol(com_win);
waddstr(com_win, searching_msg);
if (forward)
waddstr(com_win, fwd_srch_msg);
else
waddstr(com_win, rev_srch_msg);
wrefresh(com_win);
clr_cmd_line = TRUE;
}
moved_lines = 0;
found = FALSE;
srch_line = start_line;
srch_1 = pointer;
iter = offset;
if (!srch_short)
{
if ((offset < start_line->line_length) && (forward))
{
srch_1++;
iter = offset + 1;
}
else if ((offset > 1) && (!forward))
{
srch_1--;
iter = offset - 1;
}
else
end_of_line = TRUE;
}
while ((!found) && (srch_line != NULL))
{
while ((!end_of_line) && (!found))
{
srch_2 = srch_1;
iter2 = iter;
if (literal)
{
if (case_sen) /* if case sensitive */
{
srch_3 = srch_str;
while ((*srch_2 == *srch_3) && (*srch_3 != (char) NULL) && (iter2 < srch_line->line_length))
{
found = TRUE;
srch_2++;
srch_3++;
iter2++;
} /* end while */
}
else /* if not case sensitive */
{
srch_3 = u_srch_str;
while ((toupper(*srch_2) == *srch_3) && (*srch_3 != (char) NULL) && (iter2 < srch_line->line_length))
{
found = TRUE;
srch_2++;
srch_3++;
iter2++;
}
} /* end else */
}
else
{
if (case_sen)
srch_3 = srch_str;
else
srch_3 = u_srch_str;
do
{
found2 = FALSE;
if (srch_3[s_str_off] == '$')
{
if (iter2 == srch_line->line_length)
{
s_str_off++;
found2 = TRUE;
}
}
else if (srch_3[s_str_off] == '.')
{
if (iter2 < srch_line->line_length)
{
s_str_off++;
iter2++;
if (iter2 <= srch_line->line_length)
srch_2++;
found2 = TRUE;
}
}
else if (srch_3[s_str_off] == '*')
{
if (iter2 < srch_line->line_length)
{
increment = 1;
if ((srch_3[s_str_off+increment] != '$') && (srch_3[s_str_off+increment] != '['))
{
if (search(FALSE, srch_line, iter2, srch_2, (s_str_off+increment), TRUE, disp))
{
while (srch_3[s_str_off] != (char) NULL)
s_str_off++;
found2 = TRUE;
}
}
found2 = TRUE;
iter2++;
if (iter2 <= srch_line->line_length)
srch_2++;
else /* if at end of line, increment srch_3 to exit do...while loop */
s_str_off++;
}
else if ((iter2 == srch_line->line_length) && ((srch_3[s_str_off+1] == '$') || (srch_3[s_str_off+1] == (char) NULL)))
{
s_str_off++;
found2 = TRUE;
}
}
else if (srch_3[s_str_off] == '^')
{
if (iter == 1)
{
found2 = TRUE;
s_str_off++;
}
}
else if (srch_3[s_str_off] == '[')
{
s_str_off++;
x = (char) NULL;
if (srch_3[s_str_off] == '^')
{
x = '^';
s_str_off++;
}
else if (srch_3[s_str_off] == '\\')
s_str_off++;
found2 = FALSE;
while ((srch_3[s_str_off] != (char) NULL) && (srch_3[s_str_off] != ']') && (!found2))
{
if (srch_3[s_str_off] == '\\')
s_str_off++;
x1 = srch_3[s_str_off];
s_str_off++;
if (srch_3[s_str_off] == '-')
{
s_str_off++;
if (srch_3[s_str_off] == '\\')
s_str_off++;
x2 = srch_3[s_str_off];
s_str_off++;
if (case_sen)
{
if ((*srch_2 >= x1) && (*srch_2 <= x2))
found2 = TRUE;
}
else
{
if ((toupper(*srch_2) >= x1) && (toupper(*srch_2) <= x2))
found2 = TRUE;
}
}
else if (case_sen)
{
if (x1 == *srch_2)
found2 = TRUE;
}
else
{
if (x1 == toupper(*srch_2))
found2 = TRUE;
}
}
if (x == '^')
found2 = !found2;
if (found2)
{
iter2++;
if (iter2 <= srch_line->line_length)
srch_2++;
}
while ((srch_3[s_str_off] != ']') && (srch_3[s_str_off] != (char) NULL))
{
if (srch_3[s_str_off] == '\\')
s_str_off++;
s_str_off++;
}
if (srch_3[s_str_off] != (char) NULL)
s_str_off++;
}
else
{
if (srch_3[s_str_off] == '\\')
s_str_off++;
if (case_sen)
{
if (srch_3[s_str_off] == *srch_2)
found2 = TRUE;
}
else
{
if (srch_3[s_str_off] == toupper(*srch_2))
found2 = TRUE;
}
s_str_off++;
iter2++;
if (iter2 <= srch_line->line_length)
srch_2++;
}
} while ((srch_3[s_str_off] != (char) NULL) && (found2));
found = found2;
}
if (srch_short)
{
if (found)
repl_length = (iter2 - iter) - 1;
return(found);
}
if (!((srch_3[s_str_off] == (char) NULL) && (found)))
{
s_str_off = 0;
found = FALSE;
if (forward)
{
if (iter < srch_line->line_length)
srch_1++;
else
end_of_line = TRUE;
iter++;
}
else
{
if (iter > 1)
srch_1--;
else
end_of_line = TRUE;
iter--;
}
}
}
if (!found)
{
if (forward)
{
srch_line = srch_line->next_line;
if (srch_line != NULL)
srch_1 = srch_line->line;
iter = 1;
}
else
{
srch_line = srch_line->prev_line;
if (srch_line != NULL)
{
srch_1 = srch_line->line;
iter = 1;
while (iter < srch_line->line_length)
{
iter++;
srch_1++;
}
}
}
end_of_line = FALSE;
moved_lines++;
}
}
start_of_string = srch_1;
lines_moved = moved_lines;
if (found)
{
repl_length += 1 + (iter2 - iter);
if (move_cursor)
{
if (disp)
{
wmove(com_win, 0, 0);
wclrtoeol(com_win);
clr_cmd_line = FALSE;
status_display();
wrefresh(com_win);
}
if (lines_moved != 0)
move_rel(direction, lines_moved);
if ((lines_moved != 0) || (forward))
{
while (curr_buff->position < iter)
right(TRUE);
}
else if ((lines_moved == 0) && (!forward))
{
while (curr_buff->position > iter)
left(TRUE);
}
}
else
tmp_pos = iter;
}
else
{
if (disp)
{
wmove(com_win,0,0);
wclrtoeol(com_win);
wprintw(com_win, str_str);
iter2 = curr_buff->scr_horz;
for (srch_3 = srch_str; *srch_3 != (char) NULL; srch_3++)
{
if ((*srch_3 >= 32) && (*srch_3 < 127))
{
iter2++;
waddch(com_win, *srch_3);
}
else
iter2 += out_char(com_win, *srch_3, iter2, 0, 0);
}
wprintw(com_win, not_fnd_str);
wrefresh(com_win);
wmove(curr_buff->win, curr_buff->scr_vert,curr_buff->scr_horz);
}
}
return(found);
}
int
upper(value) /* convert character to upper case */
char value;
{
char value2;
if ((value >= 'a') && (value <= 'z'))
value2 = value - ('a' - 'A');
else
value2 = value;
return(value2);
}
int
search_prompt(flag) /* prompt and read search string (srch_str) */
int flag;
{
char *tmp_srch;
char *srch_1;
char *srch_3;
#ifndef XAE
tmp_srch = get_string(search_prompt_str, FALSE);
#else
if (flag)
tmp_srch = get_string(search_prompt_str, FALSE);
else
{
tmp_srch = xalloc(strlen(xsrch_string) + 1);
copy_str(xsrch_string, tmp_srch);
}
#endif /* ifndef XAE */
if (*tmp_srch != (char) NULL)
{
if (srch_str != NULL)
free(srch_str);
srch_3 = srch_str = tmp_srch;
if (u_srch_str != NULL)
free(u_srch_str);
srch_1 = u_srch_str = xalloc(strlen(srch_str) + 1);
while (*srch_3 != (char) NULL) /* make upper case version of string */
{
*srch_1 = toupper(*srch_3);
srch_1++;
srch_3++;
}
*srch_1 = (char) NULL;
if (strlen(srch_str) >= 1)
value = search(TRUE, curr_buff->curr_line, curr_buff->position, curr_buff->pointer, 0, FALSE, TRUE);
}
return(0);
}
void
replace() /* replace the given one string with another */
{
int counter;
int i, j;
int chng_all; /* change all occurrences */
int go_on; /* continue to replace */
int t_vert; /* temporary vertical position */
int t_horz; /* temporary horizontal position */
int s_flag; /* temporary select flag */
int t_posit; /* temporary position */
int t_pos; /* temporary abs_pos */
int temp_flag; /* temp storage for value of overstrike */
int temp_abs_line; /* keep track of original absolute line */
char *pnt; /* temporary pointer */
char *t_srch_str; /* temp search string */
char *ut_srch_str; /* upper case search string */
char *response; /* response from user */
char *del_strg; /* string deleted from line */
char res; /* value to hold response */
struct text *t_line; /* temp pointer to hold curr_buff->curr_line */
/* continue to look for occurrences while go_on is true,
if chng_all is true, do not prompt user whether or not to change
*/
temp_abs_line = curr_buff->absolute_lin;
temp_flag = overstrike;
overstrike = FALSE;
s_flag = mark_text;
mark_text = FALSE;
t_pos = curr_buff->scr_pos;
t_srch_str = srch_str;
ut_srch_str = u_srch_str;
t_posit = curr_buff->position;
t_line = curr_buff->curr_line;
t_vert = curr_buff->scr_vert;
t_horz = curr_buff->scr_horz;
go_on = TRUE;
u_srch_str = u_old_string;
srch_str = old_string;
chng_all = FALSE;
while (go_on)
{
if (search(TRUE, curr_buff->curr_line, curr_buff->position, curr_buff->pointer, 0, FALSE, TRUE))
{
wstandout(curr_buff->win);
if (curr_buff->position == curr_buff->curr_line->line_length)
waddch(curr_buff->win, ' ');
else if ((*curr_buff->pointer >= ' ') && (*curr_buff->pointer < 127))
waddch(curr_buff->win, *curr_buff->pointer);
else
value = out_char(curr_buff->win, *curr_buff->pointer, curr_buff->scr_pos, curr_buff->scr_vert, 0);
wstandend(curr_buff->win);
wrefresh(curr_buff->win);
if (!chng_all)
{
response = get_string(replace_action_prompt, TRUE);
if (toupper(*response) == toupper(*replace_all_char))
chng_all = TRUE;
else if (toupper(*response) == toupper(*quit_char))
go_on = FALSE;
res = *response;
free(response);
}
wmove(curr_buff->win, curr_buff->scr_vert, curr_buff->scr_horz);
if ((*curr_buff->pointer >= ' ') && (*curr_buff->pointer < 127))
waddch(curr_buff->win, *curr_buff->pointer);
else
value = out_char(curr_buff->win, *curr_buff->pointer, curr_buff->scr_pos, curr_buff->scr_vert, 0);
if (((toupper(res) == toupper(*replace_r_char)) ||
(go_on) || (res == (char) NULL)) &&
(toupper(res) != toupper(*replace_skip_char)))
{
del_strg = del_string(repl_length - 1);
counter = strlen(new_string) + 1;
pnt = new_string;
for (i = 1; i < counter; i++)
{
if ((!literal) && (*pnt == '&'))
{
undel_string(del_strg, (repl_length - 1));
for (j = 1; j < repl_length; j++)
right(TRUE);
}
else if ((!literal) && ((*pnt == '\\') && (pnt[1] == '&')))
{
i++;
pnt++;
insert(*pnt);
}
else
insert(*pnt);
pnt++;
}
if (!forward)
{
for (i = 1; i < counter; i++)
left(TRUE);
}
free(del_strg);
}
}
else
go_on = FALSE;
}
u_srch_str = ut_srch_str;
srch_str = t_srch_str;
curr_buff->curr_line = t_line;
curr_buff->pointer = curr_buff->curr_line->line;
for (value = 1; value < t_posit; value++)
curr_buff->pointer++;
curr_buff->position = t_posit;
curr_buff->abs_pos = curr_buff->scr_pos = t_pos;
curr_buff->scr_vert = t_vert;
curr_buff->scr_horz = t_horz;
mark_text = s_flag;
midscreen(curr_buff->scr_vert, curr_buff->position);
werase(com_win);
wrefresh(com_win);
overstrike = temp_flag;
curr_buff->absolute_lin = temp_abs_line;
}
int
repl_prompt(flag) /* prompt for replace parameters */
int flag;
{
char *rparam;
char *rp;
char *start;
char *upt;
char delimiter;
int last_char;
int counter;
#ifndef XAE
rp = rparam = get_string(replace_prompt_str, TRUE);
#else
if (flag)
rp = rparam = get_string(replace_prompt_str, TRUE);
else
{
counter = strlen(xnew_string) + strlen(xold_string) + 2;
if (counter > 2)
{
rp = rparam = xalloc(counter + 3);
*rp = '\33';
rp++;
copy_str(xold_string, rp);
while (*rp != (char) NULL)
rp++;
*rp = '\33';
rp++;
copy_str(xnew_string, rp);
while (*rp != (char) NULL)
rp++;
*rp = '\33';
rp++;
*rp = (char) NULL;
rp = rparam;
}
else
rp = "\000";
}
#endif /* ifndef XAE */
delimiter = *rp;
if (*rp != (char) NULL)
{
++rp;
start = rp;
counter = 1;
while ((*rp != delimiter) && (*rp != (char) NULL))
{
counter++;
rp++;
}
if (*rp == (char) NULL)
last_char = TRUE;
else
{
last_char = FALSE;
*rp = (char) NULL;
}
if (old_string != NULL)
{
free(old_string);
free(new_string);
free(u_old_string);
}
old_string = xalloc(counter);
u_old_string = xalloc(counter);
copy_str(start, old_string);
upt = u_old_string;
while (*start != (char) NULL)
{
*upt = toupper(*start);
upt++;
start++;
}
*upt = (char) NULL;
if (!last_char)
start = ++rp;
counter = 1;
while ((*rp != delimiter) && (*rp != (char) NULL))
{
counter++;
rp++;
}
*rp = (char) NULL;
new_string = xalloc(counter);
copy_str(start, new_string);
free(rparam);
if (strlen(old_string) >= 1)
replace();
}
return(0);
}
void
match() /* find the matching (), {}, or [] */
{
int level; /* level of nesting */
int t_vert; /* temporary vertical position */
int t_horz; /* temporary horizontal position */
int s_flag; /* temporary select flag */
int t_posit; /* temporary position */
int t_pos; /* temporary abs_pos */
int mforward; /* temporary storage of true forward */
int mliteral; /* temp storage of true literal value */
int total_lines; /* sum of lines moved */
int temp_abs_line; /* keep track of original line */
char curr_char; /* the current character */
char *t_srch_str; /* temp search string */
char *ut_srch_str; /* upper case search string */
char *direction; /* direction in which to move */
struct text *t_line; /* temp pointer to hold curr_buff->curr_line */
if ((*curr_buff->pointer == '{') || (*curr_buff->pointer == '}') || (*curr_buff->pointer == '(') || (*curr_buff->pointer == ')') || (*curr_buff->pointer == '[') || (*curr_buff->pointer == ']') || (*curr_buff->pointer == '<') || (*curr_buff->pointer == '>'))
{
temp_abs_line = curr_buff->absolute_lin;
curr_char = *curr_buff->pointer;
mliteral = literal;
mforward = forward;
literal = FALSE;
if ((*curr_buff->pointer == '{') || (*curr_buff->pointer == '(') || (*curr_buff->pointer == '[') || (*curr_buff->pointer == '<')) /* provide matches for quoted characters - > ] ) } */
{
direction = "d";
forward = TRUE;
}
else
{
direction = "u";
forward = FALSE;
}
s_flag = mark_text;
mark_text = FALSE;
t_pos = curr_buff->scr_pos;
t_srch_str = srch_str;
ut_srch_str = u_srch_str;
t_posit = curr_buff->position;
t_line = curr_buff->curr_line;
t_vert = curr_buff->scr_vert;
t_horz = curr_buff->scr_horz;
if ((curr_char == '{') || (curr_char == '}'))
srch_str = "[{}]";
if ((curr_char == '(') || (curr_char == ')'))
srch_str = "[()]";
if ((curr_char == '<') || (curr_char == '>'))
srch_str = "[<>]";
if ((curr_char == '[') || (curr_char == ']'))
srch_str = "[\\[\\]]";
u_srch_str = srch_str;
level = 1;
total_lines = 0;
while ((level > 0) && (search(FALSE, curr_buff->curr_line, curr_buff->position, curr_buff->pointer, 0, FALSE, TRUE)))
{
total_lines += lines_moved;
curr_buff->curr_line = srch_line;
curr_buff->pointer = start_of_string;
curr_buff->position = tmp_pos;
if (*start_of_string == curr_char)
level++;
else
level--;
}
lines_moved = total_lines;
curr_buff->curr_line = t_line;
curr_buff->pointer = curr_buff->curr_line->line;
for (value = 1; value < t_posit; value++)
curr_buff->pointer++;
curr_buff->position = t_posit;
curr_buff->abs_pos = curr_buff->scr_pos = t_pos;
curr_buff->scr_vert = t_vert;
curr_buff->scr_horz = t_horz;
mark_text = s_flag;
if (level == 0)
{
if (lines_moved != 0)
move_rel(direction, lines_moved);
if ((lines_moved != 0) || (forward))
{
while (curr_buff->position < tmp_pos)
right(TRUE);
}
else if ((lines_moved == 0) && (!forward))
{
while (curr_buff->position > tmp_pos)
left(TRUE);
}
}
else
{
clr_cmd_line = TRUE;
wmove(com_win, 0, 0);
wclrtoeol(com_win);
wprintw(com_win, cant_find_match_str);
wrefresh(com_win);
curr_buff->absolute_lin = temp_abs_line;
}
u_srch_str = ut_srch_str;
srch_str = t_srch_str;
literal = mliteral;
forward = mforward;
}
}
|