119
/* Initialize the global search and replace strings. */
120
void search_init_globals(void)
123
if (last_search == NULL)
124
last_search = mallocstrcpy(NULL, "");
125
if (last_replace == NULL)
126
last_replace = mallocstrcpy(NULL, "");
129
119
/* Set up the system variables for a search or replace. If use_answer
130
120
* is TRUE, only set backupstring to answer. Return -2 to run the
131
121
* opposite program (search -> replace, replace -> search), return -1 if
157
147
* do_search() or do_replace() and be called again. In that case,
158
148
* we should put the same search string back up. */
160
search_init_globals();
162
152
if (last_search[0] != '\0') {
163
153
char *disp = display_string(last_search, 0, COLS / 3, FALSE);
262
/* Look for needle, starting at (current, current_x). If no_sameline is
263
* TRUE, skip over begin when looking for needle. begin is the line
252
/* Look for needle, starting at (current, current_x). begin is the line
264
253
* where we first started searching, at column begin_x. The return
265
254
* value specifies whether we found anything. If we did, set needle_len
266
255
* to the length of the string we found if it isn't NULL. */
267
256
bool findnextstr(
268
257
#ifndef DISABLE_SPELLER
258
bool whole_word_only,
271
bool no_sameline, const filestruct *begin, size_t begin_x, const
272
char *needle, size_t *needle_len)
260
const filestruct *begin, size_t begin_x,
261
const char *needle, size_t *needle_len)
274
263
size_t found_len;
275
264
/* The length of the match we find. */
326
315
#ifndef DISABLE_SPELLER
327
316
/* If we're searching for whole words, see if this potential
328
317
* match is a whole word. */
318
if (whole_word_only) {
330
319
char *word = mallocstrncpy(NULL, found, found_len + 1);
331
320
word[found_len] = '\0';
334
323
fileptr->data, word);
339
327
/* If we're searching for whole words and this potential
340
* match isn't a whole word, or if we're not allowed to find
341
* a match on the same line we started on and this potential
342
* match is on that line, continue searching. */
344
#ifndef DISABLE_SPELLER
345
(!whole_word || found_whole) &&
328
* match isn't a whole word, continue searching. */
329
if (!whole_word_only || found_whole)
347
(!no_sameline || fileptr != openfile->current))
477
460
#ifndef DISABLE_SPELLER
480
FALSE, openfile->current, openfile->current_x, answer, NULL);
463
openfile->current, openfile->current_x, answer, NULL);
482
/* Check to see if there's only one occurrence of the string and
483
* we're on it now. */
465
/* If we found something, and we're back at the exact same spot where
466
* we started searching, then this is the only occurrence. */
484
467
if (fileptr == openfile->current && fileptr_x ==
485
468
openfile->current_x && didfind) {
487
/* Do the search again, skipping over the current line, if we're
488
* doing a bol and/or eol regex search ("^", "$", or "^$"), so
489
* that we find one only once per line. We should only end up
490
* back at the same position if the string isn't found again, in
491
* which case it's the only occurrence. */
492
if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
494
didfind = findnextstr(
495
#ifndef DISABLE_SPELLER
498
TRUE, openfile->current,
499
openfile->current_x, answer, NULL);
500
if (fileptr == openfile->current && fileptr_x ==
501
openfile->current_x && !didfind)
502
statusbar(_("This is the only occurrence"));
505
469
statusbar(_("This is the only occurrence"));
511
472
openfile->placewewant = xplustabs();
522
483
size_t pww_save = openfile->placewewant;
525
search_init_globals();
488
#ifndef DISABLE_HISTORIES
489
/* If nothing was searched for yet during this run of nano, but
490
* there is a search history, take the most recent item. */
491
if (last_search[0] == '\0' && searchbot->prev != NULL)
492
last_search = mallocstrcpy(last_search, searchbot->prev->data);
527
495
if (last_search[0] != '\0') {
528
496
#ifdef HAVE_REGEX_H
536
504
#ifndef DISABLE_SPELLER
539
FALSE, openfile->current, openfile->current_x,
507
openfile->current, openfile->current_x, last_search, NULL);
542
/* Check to see if there's only one occurrence of the string and
543
* we're on it now. */
509
/* If we found something, and we're back at the exact same spot
510
* where we started searching, then this is the only occurrence. */
544
511
if (fileptr == openfile->current && fileptr_x ==
545
512
openfile->current_x && didfind) {
547
/* Do the search again, skipping over the current line, if
548
* we're doing a bol and/or eol regex search ("^", "$", or
549
* "^$"), so that we find one only once per line. We should
550
* only end up back at the same position if the string isn't
551
* found again, in which case it's the only occurrence. */
552
if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
554
didfind = findnextstr(
555
#ifndef DISABLE_SPELLER
558
TRUE, openfile->current, openfile->current_x,
560
if (fileptr == openfile->current && fileptr_x ==
561
openfile->current_x && !didfind)
562
statusbar(_("This is the only occurrence"));
564
#endif /* HAVE_REGEX_H */
565
513
statusbar(_("This is the only occurrence"));
571
516
statusbar(_("No current search pattern"));
678
623
* canceled isn't NULL, set it to TRUE if we canceled. */
679
624
ssize_t do_replace_loop(
680
625
#ifndef DISABLE_SPELLER
626
bool whole_word_only,
683
628
bool *canceled, const filestruct *real_current, size_t
684
629
*real_current_x, const char *needle)
686
631
ssize_t numreplaced = -1;
687
632
size_t match_len;
688
633
bool replaceall = FALSE;
690
/* The starting-line match and bol/eol regex flags. */
691
bool begin_line = FALSE, bol_or_eol = FALSE;
693
634
#ifndef NANO_TINY
694
635
bool old_mark_set = openfile->mark_set;
695
636
filestruct *top, *bot;
721
662
findnextstr_wrap_reset();
722
663
while (findnextstr(
723
664
#ifndef DISABLE_SPELLER
727
/* We should find a bol and/or eol regex only once per line. If
728
* the bol_or_eol flag is set, it means that the last search
729
* found one on the beginning line, so we should skip over the
730
* beginning line when doing this search. */
735
, real_current, *real_current_x, needle, &match_len)) {
667
real_current, *real_current_x, needle, &match_len)) {
738
670
#ifndef NANO_TINY
751
/* If the bol_or_eol flag is set, we've found a match on the
752
* beginning line already, and we're still on the beginning line
753
* after the search, it means that we've wrapped around, so
755
if (bol_or_eol && begin_line && openfile->current == real_current)
757
/* Otherwise, set the begin_line flag if we've found a match on
758
* the beginning line, reset the bol_or_eol flag, and continue. */
760
if (openfile->current == real_current)
766
682
/* Indicate that we found the search string. */
767
683
if (numreplaced == -1)
799
/* Set the bol_or_eol flag if we're doing a bol and/or eol regex
800
* replace ("^", "$", or "^$"). */
801
if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp, needle))
805
714
if (i > 0 || replaceall) { /* Yes, replace it!!!! */
807
716
size_t length_change;