~ubuntu-branches/debian/sid/nano/sid

« back to all changes in this revision

Viewing changes to src/search.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2015-07-06 14:05:33 UTC
  • mfrom: (1.3.5)
  • Revision ID: package-import@ubuntu.com-20150706140533-atf2ip59pqbspkjo
Tags: 2.4.2-1
* The "Acord del Botànic" release.
* New upstream release.
  - Includes many improvements to syntax hightlighting definitions
    (closes: #664456, #688892, #744005, #783763, #785508, #788318, #790017).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: search.c 5195 2015-04-13 10:59:12Z bens $ */
 
1
/* $Id: search.c 5258 2015-06-20 08:10:25Z bens $ */
2
2
/**************************************************************************
3
3
 *   search.c                                                             *
4
4
 *                                                                        *
116
116
#endif
117
117
}
118
118
 
119
 
/* Initialize the global search and replace strings. */
120
 
void search_init_globals(void)
121
 
{
122
 
    focusing = TRUE;
123
 
    if (last_search == NULL)
124
 
        last_search = mallocstrcpy(NULL, "");
125
 
    if (last_replace == NULL)
126
 
        last_replace = mallocstrcpy(NULL, "");
127
 
}
128
 
 
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. */
159
149
 
160
 
    search_init_globals();
 
150
    focusing = TRUE;
161
151
 
162
152
    if (last_search[0] != '\0') {
163
153
        char *disp = display_string(last_search, 0, COLS / 3, FALSE);
259
249
    return 0;
260
250
}
261
251
 
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
269
 
        bool whole_word,
 
258
        bool whole_word_only,
270
259
#endif
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)
273
262
{
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. */
329
 
            if (whole_word) {
 
318
            if (whole_word_only) {
330
319
                char *word = mallocstrncpy(NULL, found, found_len + 1);
331
320
                word[found_len] = '\0';
332
321
 
334
323
                        fileptr->data, word);
335
324
                free(word);
336
325
            }
337
 
#endif
338
326
 
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. */
343
 
            if (
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)
346
330
#endif
347
 
                (!no_sameline || fileptr != openfile->current))
348
331
                break;
349
332
        }
350
333
 
477
460
#ifndef DISABLE_SPELLER
478
461
        FALSE,
479
462
#endif
480
 
        FALSE, openfile->current, openfile->current_x, answer, NULL);
 
463
        openfile->current, openfile->current_x, answer, NULL);
481
464
 
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) {
486
 
#ifdef HAVE_REGEX_H
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,
493
 
                answer)) {
494
 
            didfind = findnextstr(
495
 
#ifndef DISABLE_SPELLER
496
 
                FALSE,
497
 
#endif
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"));
503
 
        } else {
504
 
#endif
505
469
            statusbar(_("This is the only occurrence"));
506
 
#ifdef HAVE_REGEX_H
507
 
        }
508
 
#endif
509
470
    }
510
471
 
511
472
    openfile->placewewant = xplustabs();
522
483
    size_t pww_save = openfile->placewewant;
523
484
    bool didfind;
524
485
 
525
 
    search_init_globals();
 
486
    focusing = TRUE;
 
487
 
 
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);
 
493
#endif
526
494
 
527
495
    if (last_search[0] != '\0') {
528
496
#ifdef HAVE_REGEX_H
536
504
#ifndef DISABLE_SPELLER
537
505
                FALSE,
538
506
#endif
539
 
                FALSE, openfile->current, openfile->current_x,
540
 
                last_search, NULL);
 
507
                openfile->current, openfile->current_x, last_search, NULL);
541
508
 
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) {
546
 
#ifdef HAVE_REGEX_H
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,
553
 
                last_search)) {
554
 
                didfind = findnextstr(
555
 
#ifndef DISABLE_SPELLER
556
 
                        FALSE,
557
 
#endif
558
 
                        TRUE, openfile->current, openfile->current_x,
559
 
                        last_search, NULL);
560
 
                if (fileptr == openfile->current && fileptr_x ==
561
 
                        openfile->current_x && !didfind)
562
 
                    statusbar(_("This is the only occurrence"));
563
 
            } else {
564
 
#endif /* HAVE_REGEX_H */
565
513
                statusbar(_("This is the only occurrence"));
566
 
#ifdef HAVE_REGEX_H
567
 
            }
568
 
#endif
569
514
        }
570
515
    } else
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
681
 
        bool whole_word,
 
626
        bool whole_word_only,
682
627
#endif
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;
689
 
#ifdef HAVE_REGEX_H
690
 
    /* The starting-line match and bol/eol regex flags. */
691
 
    bool begin_line = FALSE, bol_or_eol = FALSE;
692
 
#endif
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
724
 
        whole_word,
725
 
#endif
726
 
#ifdef HAVE_REGEX_H
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. */
731
 
        bol_or_eol
732
 
#else
733
 
        FALSE
734
 
#endif
735
 
        , real_current, *real_current_x, needle, &match_len)) {
 
665
        whole_word_only,
 
666
#endif
 
667
        real_current, *real_current_x, needle, &match_len)) {
736
668
        int i = 0;
737
669
 
738
670
#ifndef NANO_TINY
747
679
        }
748
680
#endif
749
681
 
750
 
#ifdef HAVE_REGEX_H
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
754
 
         * we're done. */
755
 
        if (bol_or_eol && begin_line && openfile->current == real_current)
756
 
            break;
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. */
759
 
        else {
760
 
            if (openfile->current == real_current)
761
 
                begin_line = TRUE;
762
 
            bol_or_eol = FALSE;
763
 
        }
764
 
#endif
765
 
 
766
682
        /* Indicate that we found the search string. */
767
683
        if (numreplaced == -1)
768
684
            numreplaced = 0;
795
711
            }
796
712
        }
797
713
 
798
 
#ifdef HAVE_REGEX_H
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))
802
 
            bol_or_eol = TRUE;
803
 
#endif
804
 
 
805
714
        if (i > 0 || replaceall) {      /* Yes, replace it!!!! */
806
715
            char *copy;
807
716
            size_t length_change;