search.c 32.7 KB
Newer Older
Chris Allegretta's avatar
Chris Allegretta committed
1
/* $Id$ */
Chris Allegretta's avatar
Chris Allegretta committed
2
3
4
/**************************************************************************
 *   search.c                                                             *
 *                                                                        *
5
 *   Copyright (C) 2000-2005 Chris Allegretta                             *
Chris Allegretta's avatar
Chris Allegretta committed
6
7
 *   This program is free software; you can redistribute it and/or modify *
 *   it under the terms of the GNU General Public License as published by *
8
 *   the Free Software Foundation; either version 2, or (at your option)  *
Chris Allegretta's avatar
Chris Allegretta committed
9
10
11
12
13
14
15
16
17
18
19
20
21
 *   any later version.                                                   *
 *                                                                        *
 *   This program is distributed in the hope that it will be useful,      *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 *   GNU General Public License for more details.                         *
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                        *
 **************************************************************************/

22
23
24
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
25

Chris Allegretta's avatar
Chris Allegretta committed
26
27
28
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
29
#include <unistd.h>
Chris Allegretta's avatar
Chris Allegretta committed
30
#include <ctype.h>
31
#include <errno.h>
Chris Allegretta's avatar
Chris Allegretta committed
32
#include <assert.h>
Chris Allegretta's avatar
Chris Allegretta committed
33
34
#include "proto.h"
#include "nano.h"
Chris Allegretta's avatar
Chris Allegretta committed
35

36
37
static bool search_last_line = FALSE;
	/* Have we gone past the last line while searching? */
38
#ifdef HAVE_REGEX_H
39
40
static bool regexp_compiled = FALSE;
	/* Have we compiled any regular expressions? */
41
42
43
44
45
46

/* Regular expression helper functions. */

/* Compile the given regular expression.  Return value 0 means the
 * expression was invalid, and we wrote an error message on the status
 * bar.  Return value 1 means success. */
47
int regexp_init(const char *regexp)
48
{
49
50
51
52
53
    int rc = regcomp(&search_regexp, regexp, REG_EXTENDED
#ifndef NANO_SMALL
	| (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE)
#endif
	);
54
55
56
57
58
59
60
61
62

    assert(!regexp_compiled);
    if (rc != 0) {
	size_t len = regerror(rc, &search_regexp, NULL, 0);
	char *str = charalloc(len);

	regerror(rc, &search_regexp, str, len);
	statusbar(_("Bad regex \"%s\": %s"), regexp, str);
	free(str);
63
	return 0;
64
    }
65

66
    regexp_compiled = TRUE;
67
    return 1;
68
69
}

70
void regexp_cleanup(void)
71
{
72
73
    if (regexp_compiled) {
	regexp_compiled = FALSE;
74
75
	regfree(&search_regexp);
    }
76
}
77
#endif
78

79
80
void not_found_msg(const char *str)
{
81
82
83
    char *disp;
    int numchars;
 
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
84
    assert(str != NULL);
85

86
    disp = display_string(str, 0, (COLS / 2) + 1, FALSE);
87
    numchars = actual_x(disp, mbstrnlen(disp, COLS / 2));
88
89

    statusbar(_("\"%.*s%s\" not found"), numchars, disp,
90
	(disp[numchars] == '\0') ? "" : "...");
91
92

    free(disp);
93
94
95
96
97
}

void search_abort(void)
{
    display_main_list();
98
#ifndef NANO_SMALL
99
    if (ISSET(MARK_ISSET))
100
101
	edit_refresh();
#endif
102
#ifdef HAVE_REGEX_H
103
    regexp_cleanup();
104
105
106
#endif
}

Chris Allegretta's avatar
Chris Allegretta committed
107
108
void search_init_globals(void)
{
109
110
111
112
    if (last_search == NULL)
	last_search = mallocstrcpy(NULL, "");
    if (last_replace == NULL)
	last_replace = mallocstrcpy(NULL, "");
Chris Allegretta's avatar
Chris Allegretta committed
113
114
}

115
116
117
118
119
120
/* Set up the system variables for a search or replace.  If use_answer
 * is TRUE, only set backupstring to answer.  Return -2 to run opposite
 * program (search -> replace, replace -> search), return -1 if the
 * search should be canceled (due to Cancel, Go to Line, or a failed
 * regcomp()), return 0 on success, and return 1 on rerun calling
 * program.
Chris Allegretta's avatar
Chris Allegretta committed
121
 *
122
123
124
 * replacing is TRUE if we call from do_replace(), and FALSE if called
 * from do_search(). */
int search_init(bool replacing, bool use_answer)
Chris Allegretta's avatar
Chris Allegretta committed
125
{
126
    int i = 0;
127
    char *buf;
128
    static char *backupstring = NULL;
129
130
131
132
133
134
135
136
137
138
139
	/* The search string we'll be using. */

    /* If backupstring doesn't exist, initialize it to "". */
    if (backupstring == NULL)
	backupstring = mallocstrcpy(NULL, "");

    /* If use_answer is TRUE, set backupstring to answer and get out. */
    if (use_answer) {
	backupstring = mallocstrcpy(backupstring, answer);
	return 0;
    }
140
141
142
143

    /* We display the search prompt below.  If the user types a partial
     * search string and then Replace or a toggle, we will return to
     * do_search() or do_replace() and be called again.  In that case,
144
     * we should put the same search string back up. */
145

Chris Allegretta's avatar
Chris Allegretta committed
146
    search_init_globals();
147

148
#ifndef NANO_SMALL
149
    search_history.current = (historytype *)&search_history.next;
150
#endif
151
152

    if (last_search[0] != '\0') {
153
	char *disp = display_string(last_search, 0, COLS / 3, FALSE);
154

155
	buf = charalloc(strlen(disp) + 7);
156
157
	/* We use COLS / 3 here because we need to see more on the
	 * line. */
158
159
160
	sprintf(buf, " [%s%s]", disp,
		strlenpt(last_search) > COLS / 3 ? "..." : "");
	free(disp);
161
162
    } else
	buf = mallocstrcpy(NULL, "");
Chris Allegretta's avatar
Chris Allegretta committed
163

164
    /* This is now one simple call.  It just does a lot. */
165
166
    i = statusq(FALSE, replacing ? replace_list : whereis_list,
	backupstring,
167
168
169
#ifndef NANO_SMALL
	&search_history,
#endif
170
	"%s%s%s%s%s%s", _("Search"),
171

172
#ifndef NANO_SMALL
173
174
	/* This string is just a modifier for the search prompt; no
	 * grammar is implied. */
175
176
177
	ISSET(CASE_SENSITIVE) ? _(" [Case Sensitive]") :
#endif
		"",
178

179
#ifdef HAVE_REGEX_H
180
181
	/* This string is just a modifier for the search prompt; no
	 * grammar is implied. */
182
183
184
	ISSET(USE_REGEXP) ? _(" [Regexp]") :
#endif
		"",
185

186
#ifndef NANO_SMALL
187
188
	/* This string is just a modifier for the search prompt; no
	 * grammar is implied. */
189
190
191
	ISSET(REVERSE_SEARCH) ? _(" [Backwards]") :
#endif
		"",
192

193
194
195
196
197
198
199
200
201
202
	replacing ?
#ifndef NANO_SMALL
		(ISSET(MARK_ISSET) ? _(" (to replace) in selection") :
#endif
		_(" (to replace)")
#ifndef NANO_SMALL
		)
#endif
		: "",

Chris Allegretta's avatar
Chris Allegretta committed
203
	buf);
Chris Allegretta's avatar
Chris Allegretta committed
204

205
    /* Release buf now that we don't need it anymore. */
Chris Allegretta's avatar
Chris Allegretta committed
206
207
    free(buf);

208
209
210
    free(backupstring);
    backupstring = NULL;

211
212
213
    /* Cancel any search, or just return with no previous search. */
    if (i == -1 || (i < 0 && last_search[0] == '\0') ||
	    (!replacing && i == 0 && answer[0] == '\0')) {
214
	statusbar(_("Cancelled"));
215
216
217
#ifndef NANO_SMALL
	search_history.current = search_history.next;
#endif
Chris Allegretta's avatar
Chris Allegretta committed
218
	return -1;
Chris Allegretta's avatar
Chris Allegretta committed
219
220
    } else {
	switch (i) {
221
	    case -2:		/* It's the same string. */
222
#ifdef HAVE_REGEX_H
223
224
225
		/* Since answer is "", use last_search! */
		if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0)
		    return -1;
226
#endif
227
228
229
		break;
	    case 0:		/* They entered something new. */
		last_replace[0] = '\0';
230
#ifdef HAVE_REGEX_H
231
232
		if (ISSET(USE_REGEXP) && regexp_init(answer) == 0)
		    return -1;
233
#endif
234
		break;
Chris Allegretta's avatar
Chris Allegretta committed
235
#ifndef NANO_SMALL
236
237
238
239
240
241
242
243
	    case TOGGLE_CASE_KEY:
		TOGGLE(CASE_SENSITIVE);
		backupstring = mallocstrcpy(backupstring, answer);
		return 1;
	    case TOGGLE_BACKWARDS_KEY:
		TOGGLE(REVERSE_SEARCH);
		backupstring = mallocstrcpy(backupstring, answer);
		return 1;
244
#ifdef HAVE_REGEX_H
245
246
247
248
	    case TOGGLE_REGEXP_KEY:
		TOGGLE(USE_REGEXP);
		backupstring = mallocstrcpy(backupstring, answer);
		return 1;
249
#endif
Chris Allegretta's avatar
Chris Allegretta committed
250
#endif /* !NANO_SMALL */
251
252
253
254
	    case NANO_TOOTHERSEARCH_KEY:
		backupstring = mallocstrcpy(backupstring, answer);
		return -2;	/* Call the opposite search function. */
	    case NANO_TOGOTOLINE_KEY:
255
#ifndef NANO_SMALL
256
		search_history.current = search_history.next;
257
#endif
258
259
260
		do_gotoline(-1, FALSE);	/* Put answer up on the
					 * statusbar and fall
					 * through. */
261
262
	    default:
		return -1;
Chris Allegretta's avatar
Chris Allegretta committed
263
	}
Chris Allegretta's avatar
Chris Allegretta committed
264
265
266
267
    }
    return 0;
}

268
bool is_whole_word(size_t pos, const char *buf, const char *word)
269
{
270
271
272
    char *p = charalloc(mb_cur_max()), *r = charalloc(mb_cur_max());
    size_t word_end = pos + strlen(word);
    bool retval;
273

274
275
276
277
278
279
280
281
282
    assert(buf != NULL && pos <= strlen(buf) && word != NULL);

    parse_mbchar(buf + move_mbleft(buf, pos), p, NULL, NULL);
    parse_mbchar(buf + word_end, r, NULL, NULL);

    /* If we're at the beginning of the line or the character before the
     * word isn't an alphanumeric character, and if we're at the end of
     * the line or the character after the word isn't an alphanumeric
     * character, we have a whole word. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
283
    retval = (pos == 0 || !is_alnum_mbchar(p)) &&
284
285
286
287
288
289
	(word_end == strlen(buf) || !is_alnum_mbchar(r));

    free(p);
    free(r);

    return retval;
290
291
}

292
293
294
/* Look for needle, starting at (current, current_x).  If no_sameline is
 * TRUE, skip over begin when looking for needle.  begin is the line
 * where we first started searching, at column beginx.  If
295
 * can_display_wrap is TRUE, we put messages on the statusbar, wrap
296
297
298
 * around the file boundaries.  The return value specifies whether we
 * found anything.  If we did, set needle_len to the length of the
 * string we found if it isn't NULL. */
299
300
bool findnextstr(bool can_display_wrap, bool wholeword, bool
	no_sameline, const filestruct *begin, size_t beginx, const char
301
	*needle, size_t *needle_len)
Chris Allegretta's avatar
Chris Allegretta committed
302
{
Chris Allegretta's avatar
Chris Allegretta committed
303
    filestruct *fileptr = current;
304
    const char *rev_start = NULL, *found = NULL;
305
306
    size_t found_len;
	/* The length of the match we found. */
307
    size_t current_x_find = 0;
308
	/* The location of the match we found. */
309
    int current_y_find = current_y;
310
311
312
313
314
315

    /* rev_start might end up 1 character before the start or after the
     * end of the line.  This won't be a problem because strstrwrapper()
     * will return immediately and say that no match was found, and
     * rev_start will be properly set when the search continues on the
     * previous or next line. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
316
    rev_start =
317
#ifndef NANO_SMALL
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
318
	ISSET(REVERSE_SEARCH) ? fileptr->data + (current_x - 1) :
319
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
320
	fileptr->data + (current_x + 1);
Chris Allegretta's avatar
Chris Allegretta committed
321

322
    /* Look for needle in searchstr. */
323
    while (TRUE) {
324
	found = strstrwrapper(fileptr->data, needle, rev_start);
Chris Allegretta's avatar
Chris Allegretta committed
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
	/* We've found a potential match. */
	if (found != NULL) {
	    bool found_whole = FALSE;
		/* Is this potential match a whole word? */

	    /* Set found_len to the length of the potential match. */
	    found_len =
#ifdef HAVE_REGEX_H
		ISSET(USE_REGEXP) ?
		regmatches[0].rm_eo - regmatches[0].rm_so :
#endif
		strlen(needle);

	    /* If we're searching for whole words, see if this potential
	     * match is a whole word. */
	    if (wholeword) {
342
		char *word = mallocstrncpy(NULL, found, found_len + 1);
343
344
345
346
347
348
349
350
351
352
353
354
355
		word[found_len] = '\0';

		found_whole = is_whole_word(found - fileptr->data,
			fileptr->data, word);
		free(word);
	    }

	    /* If we're searching for whole words and this potential
	     * match isn't a whole word, or if we're not allowed to find
	     * a match on the same line we started on and this potential
	     * match is on that line, continue searching. */
	    if ((!wholeword || found_whole) && (!no_sameline ||
		fileptr != current))
356
		break;
Chris Allegretta's avatar
Chris Allegretta committed
357
	}
358

359
	/* We've finished processing the file, so get out. */
360
361
	if (search_last_line) {
	    if (can_display_wrap)
Chris Allegretta's avatar
Chris Allegretta committed
362
		not_found_msg(needle);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
363
	    return FALSE;
364
	}
365
366
367
368
369
370
371
372
373

#ifndef NANO_SMALL
	if (ISSET(REVERSE_SEARCH)) {
	    fileptr = fileptr->prev;
	    current_y_find--;
	} else {
#endif
	    fileptr = fileptr->next;
	    current_y_find++;
374
#ifndef NANO_SMALL
375
	}
376
#endif
377

378
	/* Start or end of buffer reached, so wrap around. */
379
380
	if (fileptr == NULL) {
	    if (!can_display_wrap)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
381
		return FALSE;
382

383
#ifndef NANO_SMALL
384
385
386
387
	    if (ISSET(REVERSE_SEARCH)) {
		fileptr = filebot;
		current_y_find = editwinrows - 1;
	    } else {
388
#endif
389
390
391
392
393
394
		fileptr = fileage;
		current_y_find = 0;
#ifndef NANO_SMALL
	    }
#endif

395
396
397
	    if (can_display_wrap)
		statusbar(_("Search Wrapped"));
	}
Chris Allegretta's avatar
Chris Allegretta committed
398

399
	/* Original start line reached. */
400
	if (fileptr == begin)
401
	    search_last_line = TRUE;
402

403
404
405
406
407
408
	rev_start = fileptr->data;
#ifndef NANO_SMALL
	if (ISSET(REVERSE_SEARCH))
	    rev_start += strlen(fileptr->data);
#endif
    }
409

410
411
    /* We found an instance. */
    current_x_find = found - fileptr->data;
Chris Allegretta's avatar
Chris Allegretta committed
412

413
414
415
416
417
418
419
420
421
    /* Ensure we haven't wrapped around again! */
    if (search_last_line &&
#ifndef NANO_SMALL
	((!ISSET(REVERSE_SEARCH) && current_x_find > beginx) ||
	(ISSET(REVERSE_SEARCH) && current_x_find < beginx))
#else
	current_x_find > beginx
#endif
	) {
Chris Allegretta's avatar
Chris Allegretta committed
422

423
424
	if (can_display_wrap)
	    not_found_msg(needle);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
425
	return FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
426
427
    }

428
    /* Set globals now that we are sure we found something. */
429
430
    current = fileptr;
    current_x = current_x_find;
431
    current_y = current_y_find;
432
    placewewant = xplustabs();
433
434

    /* needle_len holds the length of needle. */
435
436
    if (needle_len != NULL)
	*needle_len = found_len;
437

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
438
    return TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
439
440
}

441
442
443
444
445
void findnextstr_wrap_reset(void)
{
    search_last_line = FALSE;
}

Chris Allegretta's avatar
Chris Allegretta committed
446
/* Search for a string. */
447
void do_search(void)
Chris Allegretta's avatar
Chris Allegretta committed
448
{
449
    size_t old_pww = placewewant, fileptr_x = current_x;
450
451
    int i;
    bool didfind;
452
    filestruct *fileptr = current;
Chris Allegretta's avatar
Chris Allegretta committed
453

454
#ifndef DISABLE_WRAPPING
Chris Allegretta's avatar
Chris Allegretta committed
455
    wrap_reset();
456
#endif
457

458
    i = search_init(FALSE, FALSE);
459
460
    if (i == -1)	/* Cancel, Go to Line, blank search string, or
			 * regcomp() failed. */
Chris Allegretta's avatar
Chris Allegretta committed
461
	search_abort();
462
    else if (i == -2)	/* Replace. */
Chris Allegretta's avatar
Chris Allegretta committed
463
	do_replace();
464
465
466
#ifndef NANO_SMALL
    else if (i == 1)	/* Case Sensitive, Backwards, or Regexp search
			 * toggle. */
Chris Allegretta's avatar
Chris Allegretta committed
467
	do_search();
468
#endif
469

470
    if (i != 0)
471
	return;
472
473

    /* If answer is now "", copy last_search into answer. */
Chris Allegretta's avatar
Chris Allegretta committed
474
    if (answer[0] == '\0')
475
476
477
478
	answer = mallocstrcpy(answer, last_search);
    else
	last_search = mallocstrcpy(last_search, answer);

479
#ifndef NANO_SMALL
480
481
    /* If answer is not "", add this search string to the search history
     * list. */
Chris Allegretta's avatar
Chris Allegretta committed
482
    if (answer[0] != '\0')
483
	update_history(&search_history, answer);
484
#endif
485

486
    findnextstr_wrap_reset();
487
    didfind = findnextstr(TRUE, FALSE, FALSE, current, current_x,
488
	answer, NULL);
489

490
491
492
493
494
495
496
497
498
    /* Check to see if there's only one occurrence of the string and
     * we're on it now. */
    if (fileptr == current && fileptr_x == current_x && didfind) {
#ifdef HAVE_REGEX_H
	/* Do the search again, skipping over the current line, if we're
	 * doing a bol and/or eol regex search ("^", "$", or "^$"), so
	 * that we find one only once per line.  We should only end up
	 * back at the same position if the string isn't found again, in
	 * which case it's the only occurrence. */
499
500
	if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
		last_search)) {
501
	    didfind = findnextstr(TRUE, FALSE, TRUE, current, current_x,
502
		answer, NULL);
503
504
505
506
507
508
509
510
511
	    if (fileptr == current && fileptr_x == current_x && !didfind)
		statusbar(_("This is the only occurrence"));
	} else {
#endif
	    statusbar(_("This is the only occurrence"));
#ifdef HAVE_REGEX_H
	}
#endif
    }
512

513
    placewewant = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
514
    edit_redraw(fileptr, old_pww);
Chris Allegretta's avatar
Chris Allegretta committed
515
516
517
    search_abort();
}

518
#ifndef NANO_SMALL
519
/* Search for the next string without prompting. */
520
void do_research(void)
521
{
522
    size_t old_pww = placewewant, fileptr_x = current_x;
523
    bool didfind;
524
    filestruct *fileptr = current;
525

526
#ifndef DISABLE_WRAPPING
527
    wrap_reset();
528
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
529

530
531
532
533
534
    search_init_globals();

    if (last_search[0] != '\0') {

#ifdef HAVE_REGEX_H
535
	/* Since answer is "", use last_search! */
536
	if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0)
537
	    return;
538
539
#endif

540
	findnextstr_wrap_reset();
541
	didfind = findnextstr(TRUE, FALSE, FALSE, current, current_x,
542
		last_search, NULL);
543

544
545
546
547
548
549
550
551
552
	/* Check to see if there's only one occurrence of the string and
	 * we're on it now. */
	if (fileptr == current && fileptr_x == current_x && didfind) {
#ifdef HAVE_REGEX_H
	    /* Do the search again, skipping over the current line, if
	     * we're doing a bol and/or eol regex search ("^", "$", or
	     * "^$"), so that we find one only once per line.  We should
	     * only end up back at the same position if the string isn't
	     * found again, in which case it's the only occurrence. */
553
554
	    if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
		last_search)) {
555
		didfind = findnextstr(TRUE, FALSE, TRUE, current,
556
			current_x, answer, NULL);
557
558
559
560
561
562
563
564
565
		if (fileptr == current && fileptr_x == current_x && !didfind)
		    statusbar(_("This is the only occurrence"));
	    } else {
#endif
		statusbar(_("This is the only occurrence"));
#ifdef HAVE_REGEX_H
	    }
#endif
	}
566
567
568
    } else
        statusbar(_("No current search pattern"));

569
    placewewant = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
570
    edit_redraw(fileptr, old_pww);
571
572
    search_abort();
}
573
#endif
574

Chris Allegretta's avatar
Chris Allegretta committed
575
576
void replace_abort(void)
{
577
578
579
    /* Identical to search_abort(), so we'll call it here.  If it does
     * something different later, we can change it back.  For now, it's
     * just a waste to duplicate code. */
580
    search_abort();
581
    placewewant = xplustabs();
582
583
}

584
#ifdef HAVE_REGEX_H
585
int replace_regexp(char *string, bool create_flag)
586
{
587
    /* Split personality here - if create_flag is FALSE, just calculate
588
     * the size of the replacement line (necessary because of
589
     * subexpressions \1 to \9 in the replaced text). */
590

591
    const char *c = last_replace;
592
    int search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so;
593
    int new_size = strlen(current->data) + 1 - search_match_count;
594

Chris Allegretta's avatar
Chris Allegretta committed
595
596
    /* Iterate through the replacement text to handle subexpression
     * replacement using \1, \2, \3, etc. */
597
    while (*c != '\0') {
598
599
	int num = (int)(*(c + 1) - '0');

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
600
601
	if (*c != '\\' || num < 1 || num > 9 ||
		num > search_regexp.re_nsub) {
602
603
604
605
606
	    if (create_flag)
		*string++ = *c;
	    c++;
	    new_size++;
	} else {
607
	    int i = regmatches[num].rm_eo - regmatches[num].rm_so;
608

609
610
	    /* Skip over the replacement expression. */
	    c += 2;
611

612
613
	    /* But add the length of the subexpression to new_size. */
	    new_size += i;
614

615
	    /* And if create_flag is TRUE, append the result of the
616
617
618
619
620
	     * subexpression match to the new line. */
	    if (create_flag) {
		strncpy(string, current->data + current_x +
			regmatches[num].rm_so, i);
		string += i;
621
622
	    }
	}
623
624
625
    }

    if (create_flag)
Chris Allegretta's avatar
Chris Allegretta committed
626
	*string = '\0';
627
628
629

    return new_size;
}
630
#endif
631

632
char *replace_line(const char *needle)
633
{
634
    char *copy;
635
636
637
    int new_line_size;
    int search_match_count;

638
    /* Calculate the size of the new line. */
639
#ifdef HAVE_REGEX_H
640
    if (ISSET(USE_REGEXP)) {
641
	search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so;
642
	new_line_size = replace_regexp(NULL, 0);
643
    } else {
644
#endif
645
646
647
648
	search_match_count = strlen(needle);
	new_line_size = strlen(current->data) - search_match_count +
	    strlen(answer) + 1;
#ifdef HAVE_REGEX_H
649
    }
650
#endif
651

652
    /* Create the buffer. */
653
    copy = charalloc(new_line_size);
654

655
    /* The head of the original line. */
656
657
    strncpy(copy, current->data, current_x);

658
    /* The replacement text. */
659
#ifdef HAVE_REGEX_H
660
661
    if (ISSET(USE_REGEXP))
	replace_regexp(copy + current_x, TRUE);
662
    else
663
#endif
664
	strcpy(copy + current_x, answer);
665

666
667
668
    /* The tail of the original line. */
    assert(current_x + search_match_count <= strlen(current->data));
    strcat(copy, current->data + current_x + search_match_count);
669
670

    return copy;
Chris Allegretta's avatar
Chris Allegretta committed
671
672
}

673
/* Step through each replace word and prompt user before replacing.
674
675
676
 * Parameters real_current and real_current_x are needed in order to
 * allow the cursor position to be updated when a word before the cursor
 * is replaced by a shorter word.
677
678
 *
 * needle is the string to seek.  We replace it with answer.  Return -1
679
680
 * if needle isn't found, else the number of replacements performed.  If
 * canceled isn't NULL, set it to TRUE if we canceled. */
681
682
683
ssize_t do_replace_loop(const char *needle, const filestruct
	*real_current, size_t *real_current_x, bool wholewords, bool
	*canceled)
Chris Allegretta's avatar
Chris Allegretta committed
684
{
685
686
    ssize_t numreplaced = -1;
    size_t match_len;
687
    size_t pww_save = placewewant;
688
    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
#ifndef NANO_SMALL
694
    bool old_mark_set = ISSET(MARK_ISSET);
695
696
697
698
699
    filestruct *edittop_save = edittop, *top, *bot;
    size_t top_x, bot_x;
    bool right_side_up = FALSE;
	/* TRUE if (mark_beginbuf, mark_beginx) is the top of the mark,
	 * FALSE if (current, current_x) is. */
Chris Allegretta's avatar
Chris Allegretta committed
700

701
    if (old_mark_set) {
702
	/* If the mark is on, partition the filestruct so that it
703
704
	 * contains only the marked text, set edittop to the top of the
	 * partition, turn the mark off, and refresh the screen. */
705
	mark_order((const filestruct **)&top, &top_x,
706
	    (const filestruct **)&bot, &bot_x, &right_side_up);
707
708
	filepart = partition_filestruct(top, top_x, bot, bot_x);
	edittop = fileage;
709
710
711
	UNSET(MARK_ISSET);
	edit_refresh();
    }
712
#endif
713

714
715
716
    if (canceled != NULL)
	*canceled = FALSE;

717
    findnextstr_wrap_reset();
718
    while (findnextstr(TRUE, wholewords,
719
#ifdef HAVE_REGEX_H
720
721
722
723
724
	/* We should find a bol and/or eol regex only once per line.  If
	 * the bol_or_eol flag is set, it means that the last search
	 * found one on the beginning line, so we should skip over the
	 * beginning line when doing this search. */
	bol_or_eol
725
#else
726
	FALSE
727
#endif
728
	, real_current, *real_current_x, needle, &match_len)) {
729
730

	int i = 0;
731

732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#ifdef HAVE_REGEX_H
	/* If the bol_or_eol flag is set, we've found a match on the
	 * beginning line already, and we're still on the beginning line
	 * after the search, it means that we've wrapped around, so
	 * we're done. */
	if (bol_or_eol && begin_line && current == real_current)
	    break;
	/* Otherwise, set the begin_line flag if we've found a match on
	 * the beginning line, reset the bol_or_eol flag, and
	 * continue. */
	else {
	    if (current == real_current)
		begin_line = TRUE;
	    bol_or_eol = FALSE;
	}
#endif

749
	if (!replaceall) {
750
	    edit_redraw(real_current, pww_save);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
751
	    pww_save = placewewant;
752
	}
Chris Allegretta's avatar
Chris Allegretta committed
753

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
754
755
	/* Record for the return value that we found the search
	 * string. */
756
757
	if (numreplaced == -1)
	    numreplaced = 0;
758

759
	if (!replaceall) {
760
761
762
763
	    char *exp_word;
	    size_t xpt = xplustabs();

	    exp_word = display_string(current->data, xpt,
764
765
		strnlenpt(current->data, match_len + current_x) - xpt,
		FALSE);
766

767
	    curs_set(0);
768
	    do_replace_highlight(TRUE, exp_word);
769

770
	    i = do_yesno(TRUE, _("Replace this instance?"));
Chris Allegretta's avatar
Chris Allegretta committed
771

772
773
	    do_replace_highlight(FALSE, exp_word);
	    free(exp_word);
774
	    curs_set(1);
775

776
777
778
	    if (i == -1) {	/* We canceled the replace. */
		if (canceled != NULL)
		    *canceled = TRUE;
779
		break;
780
	    }
781
782
	}

783
#ifdef HAVE_REGEX_H
784
	/* Set the bol_or_eol flag if we're doing a bol and/or eol regex
785
	 * replace ("^", "$", or "^$"). */
786
787
	if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
		needle))
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
788
	    bol_or_eol = TRUE;
789
790
#endif

791
	if (i > 0 || replaceall) {	/* Yes, replace it!!!! */
792
	    char *copy;
793
	    size_t length_change;
794

795
	    if (i == 2)
796
		replaceall = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
797

798
	    copy = replace_line(needle);
Chris Allegretta's avatar
Chris Allegretta committed
799

800
	    length_change = strlen(copy) - strlen(current->data);
Chris Allegretta's avatar
Chris Allegretta committed
801

802
#ifndef NANO_SMALL
803
804
805
806
	    /* If the mark was on and (mark_beginbuf, mark_begin_x) was
	     * the top of it, don't change mark_beginx. */
	    if (!old_mark_set || !right_side_up) {
		/* Keep mark_beginx in sync with the text changes. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
807
808
		if (current == mark_beginbuf &&
			mark_beginx > current_x) {
809
810
811
812
813
		    if (mark_beginx < current_x + match_len)
			mark_beginx = current_x;
		    else
			mark_beginx += length_change;
		}
814
	    }
Chris Allegretta's avatar
Chris Allegretta committed
815

816
817
818
	    /* If the mark was on and (current, current_x) was the top
	     * of it, don't change real_current_x. */
	    if (!old_mark_set || right_side_up) {
819
#endif
820
		/* Keep real_current_x in sync with the text changes. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
821
822
		if (current == real_current &&
			current_x <= *real_current_x) {
823
824
825
826
		    if (*real_current_x < current_x + match_len)
			*real_current_x = current_x + match_len;
		    *real_current_x += length_change;
		}
827
#ifndef NANO_SMALL
828
	    }
829
#endif
830

831
	    /* Set the cursor at the last character of the replacement
832
	     * text, so searching will resume after the replacement
833
834
	     * text.  Note that current_x might be set to (size_t)-1
	     * here. */
835
#ifndef NANO_SMALL
836
	    if (!ISSET(REVERSE_SEARCH))
837
838
#endif
		current_x += match_len + length_change - 1;
839

840
	    /* Cleanup. */
841
842
843
	    totsize += length_change;
	    free(current->data);
	    current->data = copy;
844

845
846
847
848
849
850
851
852
853
	    if (!replaceall) {
#ifdef ENABLE_COLOR
		if (ISSET(COLOR_SYNTAX))
		    edit_refresh();
		else
#endif
		    update_line(current, current_x);
	    }

Chris Allegretta's avatar
Chris Allegretta committed
854
855
	    set_modified();
	    numreplaced++;
856
	}
Chris Allegretta's avatar
Chris Allegretta committed
857
858
    }

859
#ifndef NANO_SMALL
860
    if (old_mark_set) {
861
862
863
	/* If the mark was on, unpartition the filestruct so that it
	 * contains all the text again, set edittop back to what it was
	 * before, turn the mark back on, and refresh the screen. */
864
	unpartition_filestruct(&filepart);
865
	edittop = edittop_save;
866
	SET(MARK_ISSET);
867
868
	edit_refresh();
    }
869
870
#endif

871
872
873
874
    /* If text has been added to the magicline, make a new magicline. */
    if (filebot->data[0] != '\0')
	new_magicline();

Chris Allegretta's avatar
Chris Allegretta committed
875
876
877
    return numreplaced;
}

Chris Allegretta's avatar
Chris Allegretta committed
878
/* Replace a string. */
879
void do_replace(void)
Chris Allegretta's avatar
Chris Allegretta committed
880
{
881
    int i;
882
    filestruct *edittop_save, *begin;
883
    size_t beginx, pww_save;
884
    ssize_t numreplaced;
Chris Allegretta's avatar
Chris Allegretta committed
885

Chris Allegretta's avatar
Chris Allegretta committed
886
887
888
    if (ISSET(VIEW_MODE)) {
	print_view_warning();
	replace_abort();
889
	return;
Chris Allegretta's avatar
Chris Allegretta committed
890
891
    }

892
    i = search_init(TRUE, FALSE);
893
894
    if (i == -1) {		/* Cancel, Go to Line, blank search
				 * string, or regcomp() failed. */
Chris Allegretta's avatar
Chris Allegretta committed
895
	replace_abort();
896
	return;
897
    } else if (i == -2) {	/* No Replace. */
Chris Allegretta's avatar
Chris Allegretta committed
898
	do_search();
899
	return;
900
901
902
903
904
    } else if (i == 1)		/* Case Sensitive, Backwards, or Regexp
				 * search toggle. */
	do_replace();

    if (i != 0)
905
	return;
Chris Allegretta's avatar
Chris Allegretta committed
906

907
908
909
    /* If answer is not "", add answer to the search history list and
     * copy answer into last_search. */
    if (answer[0] != '\0') {
910
#ifndef NANO_SMALL
911
	update_history(&search_history, answer);
912
#endif
Chris Allegretta's avatar
Chris Allegretta committed
913
	last_search = mallocstrcpy(last_search, answer);
914
    }
Chris Allegretta's avatar
Chris Allegretta committed
915

916
#ifndef NANO_SMALL
917
918
    replace_history.current = (historytype *)&replace_history.next;
    last_replace = mallocstrcpy(last_replace, "");
919
#endif
920

921
    i = statusq(FALSE, replace_list_2, last_replace,
922
#ifndef NANO_SMALL
923
	&replace_history,
924
#endif
925
	_("Replace with"));
926

927
#ifndef NANO_SMALL
928
929
930
    /* Add this replace string to the replace history list.  i == 0
     * means that the string is not "". */
    if (i == 0)
931
	update_history(&replace_history, answer);
932
933
934
935
936
937
#endif

    if (i != 0 && i != -2) {
	if (i == -1) {		/* Cancel. */
	    if (last_replace[0] != '\0')
		answer = mallocstrcpy(answer, last_replace);
938
	    statusbar(_("Cancelled"));
939
940
	}
	replace_abort();
941
	return;
942
943
944
    }

    last_replace = mallocstrcpy(last_replace, answer);
Chris Allegretta's avatar
Chris Allegretta committed
945

946
    /* Save where we are. */
947
    edittop_save = edittop;
Chris Allegretta's avatar
Chris Allegretta committed
948
    begin = current;
949
    beginx = current_x;
950
    pww_save = placewewant;
Chris Allegretta's avatar
Chris Allegretta committed
951

952
953
    numreplaced = do_replace_loop(last_search, begin, &beginx, FALSE,
	NULL);
Chris Allegretta's avatar
Chris Allegretta committed
954

955
    /* Restore where we were. */
956
    edittop = edittop_save;
Chris Allegretta's avatar
Chris Allegretta committed
957
    current = begin;
958
    current_x = beginx;
959
    placewewant = pww_save;
960

Chris Allegretta's avatar
Chris Allegretta committed
961
    renumber_all();
962
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
963
964

    if (numreplaced >= 0)
965
966
	statusbar(P_("Replaced %ld occurrence", "Replaced %ld occurrences",
		(long)numreplaced), (long)numreplaced);
Chris Allegretta's avatar
Chris Allegretta committed
967

Chris Allegretta's avatar
Chris Allegretta committed
968
969
970
    replace_abort();
}

971
void do_gotoline(int line, bool save_pos)
Chris Allegretta's avatar
Chris Allegretta committed
972
{
973
    if (line <= 0) {		/* Ask for it. */
974
	char *ans = mallocstrcpy(NULL, answer);
975
	int i = statusq(FALSE, gotoline_list, line < 0 ? ans : "",
Chris Allegretta's avatar
Chris Allegretta committed
976
#ifndef NANO_SMALL
977
		NULL,
Chris Allegretta's avatar
Chris Allegretta committed
978
#endif
979
980
981
		_("Enter line number"));

	free(ans);
Chris Allegretta's avatar
Chris Allegretta committed
982
983

	/* Cancel, or Enter with blank string. */
984
	if (i < 0) {
985
	    statusbar(_("Cancelled"));
986
	    display_main_list();
987
988
989
	    return;
	}

990
	if (i == NANO_TOOTHERWHEREIS_KEY) {
991
992
993
	    /* Keep answer up on the statusbar. */
	    search_init(TRUE, TRUE);

994
	    do_search();
995
	    return;
Chris Allegretta's avatar
Chris Allegretta committed
996
	}
997

998
999
	/* Do a bounds check.  Display a warning on an out-of-bounds
	 * line number only if we hit Enter at the statusbar prompt. */
1000
	if (!parse_num(answer, &line) || line < 0) {
1001
1002
	    if (i == 0)
		statusbar(_("Come on, be reasonable"));
Chris Allegretta's avatar
Chris Allegretta committed
1003
	    display_main_list();
1004
	    return;
Chris Allegretta's avatar
Chris Allegretta committed
1005
1006
1007
	}
    }

1008
1009
1010
1011
1012
1013
1014
1015
1016
    if (current->lineno > line) {
	for (; current->prev != NULL && current->lineno > line;
		current = current->prev)
	    ;
    } else {
	for (; current->next != NULL && current->lineno < line;
		current = current->next)
	    ;
    }
Chris Allegretta's avatar
Chris Allegretta committed
1017

1018
    current_x = 0;
1019

1020
    /* If save_pos is TRUE, don't change the cursor position when
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1021
     * updating the edit window. */
1022
    edit_update(save_pos ? NONE : CENTER);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1023

1024
    placewewant = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1025
    display_main_list();
Chris Allegretta's avatar
Chris Allegretta committed
1026
1027
}

1028
void do_gotoline_void(void)
Chris Allegretta's avatar
Chris Allegretta committed
1029
{
1030
    do_gotoline(0, FALSE);
Chris Allegretta's avatar
Chris Allegretta committed
1031
}
1032

1033
#if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER)
1034
void do_gotopos(int line, size_t pos_x, int pos_y, size_t pos_pww)
1035
{
1036
1037
    /* Since do_gotoline() resets the x-coordinate but not the
     * y-coordinate, set the coordinates up this way. */
1038
    current_y = pos_y;
1039
    do_gotoline(line, TRUE);
1040

1041
1042
1043
1044
    /* Make sure that the x-coordinate is sane here. */
    current_x = strlen(current->data);
    if (pos_x < current_x)
	current_x = pos_x;
1045

1046
    /* Set the rest of the coordinates up. */
1047
    placewewant = pos_pww;
1048
1049
1050
    update_line(current, pos_x);
}
#endif
1051
1052

#if !defined(NANO_SMALL) && defined(HAVE_REGEX_H)
1053
void do_find_bracket(void)
1054
1055
{
    char ch_under_cursor, wanted_ch;
1056
    const char *pos, *brackets = "([{<>}])";
1057
    char regexp_pat[] = "[  ]";
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1058
    size_t current_x_save, pww_save;
1059
    int count = 1;
1060
    unsigned long flags_save;
1061
1062
1063
    filestruct *current_save;

    ch_under_cursor = current->data[current_x];
1064

1065
1066
    pos = strchr(brackets, ch_under_cursor);
    if (ch_under_cursor == '\0' || pos == NULL) {
1067
	statusbar(_("Not a bracket"));
1068
	return;
1069
1070
    }

1071
1072
    assert(strlen(brackets) % 2 == 0);
    wanted_ch = brackets[(strlen(brackets) - 1) - (pos - brackets)];
1073
1074

    current_save = current;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1075
    current_x_save = current_x;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1076
    pww_save = placewewant;
1077
    flags_save = flags;
1078
1079
    SET(USE_REGEXP);

1080
1081
    /* Apparent near redundancy with regexp_pat[] here is needed.
     * "[][]" works, "[[]]" doesn't. */
1082

1083
1084
    if (pos < brackets + (strlen(brackets) / 2)) {
	/* On a left bracket. */
1085
1086
1087
	regexp_pat[1] = wanted_ch;
	regexp_pat[2] = ch_under_cursor;
	UNSET(REVERSE_SEARCH);
1088
1089
    } else {
	/* On a right bracket. */
1090
1091
1092
1093
1094
1095
	regexp_pat[1] = ch_under_cursor;
	regexp_pat[2] = wanted_ch;
	SET(REVERSE_SEARCH);
    }

    regexp_init(regexp_pat);
1096
    /* We constructed regexp_pat to be a valid expression. */
1097
    assert(regexp_compiled);
1098

1099
    findnextstr_wrap_reset();
1100
    while (TRUE) {
1101
	if (findnextstr(FALSE, FALSE, FALSE, current, current_x,
1102
		regexp_pat, NULL)) {
1103
	    /* Found identical bracket. */
Chris Allegretta's avatar
Chris Allegretta committed
1104
	    if (current->data[current_x] == ch_under_cursor)
1105
		count++;
1106
1107
1108
	    /* Found complementary bracket. */
	    else if (--count == 0) {
		placewewant = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1109
		edit_redraw(current_save, pww_save);
1110
		break;
1111
	    }
Chris Allegretta's avatar
Chris Allegretta committed
1112
	} else {
1113
	    /* Didn't find either a left or right bracket. */
1114
1115
	    statusbar(_("No matching bracket"));
	    current = current_save;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1116
	    current_x = current_x_save;
Chris Allegretta's avatar
Chris Allegretta committed
1117
	    update_line(current, current_x);
1118
1119
1120
1121
	    break;
	}
    }

1122
    regexp_cleanup();
1123
    flags = flags_save;
1124
1125
}
#endif
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

#ifndef NANO_SMALL
/*
 * search and replace history list support functions
 */

/* initialize search and replace history lists */
void history_init(void)
{
    search_history.next = (historytype *)&search_history.prev;
    search_history.prev = NULL;
    search_history.tail = (historytype *)&search_history.next;
    search_history.current = search_history.next;
    search_history.count = 0;
    search_history.len = 0;

    replace_history.next = (historytype *)&replace_history.prev;
    replace_history.prev = NULL;
    replace_history.tail = (historytype *)&replace_history.next;
    replace_history.current = replace_history.next;
    replace_history.count = 0;
    replace_history.len = 0;
}

/* find first node containing string *s in history list *h */
1151
historytype *find_node(historytype *h, const char *s)
1152
{
Chris Allegretta's avatar
Chris Allegretta committed
1153
    for (; h->next != NULL; h = h->next)
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
	if (strcmp(s, h->data) == 0)
	    return h;
    return NULL;
}

/* remove node *r */
void remove_node(historytype *r)
{
    r->prev->next = r->next;
    r->next->prev = r->prev;
    free(r->data);
    free(r);
}

/* add a node after node *h */
void insert_node(historytype *h, const char *s)
{
    historytype *a;

David Lawrence Ramsey's avatar
   
David Lawrence Ramsey committed
1173
    a = (historytype *)nmalloc(sizeof(historytype));
1174
    a->next = h->next;
1175
    a->prev = h;
1176
1177
1178
1179
1180
1181
    h->next->prev = a;
    h->next = a;
    a->data = mallocstrcpy(NULL, s);
}

/* update history list */
1182
void update_history(historyheadtype *h, const char *s)
1183
1184
1185
{
    historytype *p;

Chris Allegretta's avatar
Chris Allegretta committed
1186
1187
1188
    if ((p = find_node(h->next, s)) != NULL) {
	if (p == h->next)		/* catch delete and re-insert of
					   same string in 1st node */
1189
	    goto up_hs;
Chris Allegretta's avatar
Chris Allegretta committed
1190
	remove_node(p);			/* delete identical older string */
1191
1192
1193
1194
1195
1196
1197
1198
	h->count--;
    }
    if (h->count == MAX_SEARCH_HISTORY) {	/* list 'full', delete oldest */
	remove_node(h->tail);
	h->count--;
    }
    insert_node((historytype *)h, s);
    h->count++;
1199
    SET(HISTORY_CHANGED);
Chris Allegretta's avatar
Chris Allegretta committed
1200
  up_hs:
1201
1202
1203
1204
1205
1206
    h->current = h->next;
}

/* return a pointer to either the next older history or NULL if no more */
char *get_history_older(historyheadtype *h)
{
Chris Allegretta's avatar
Chris Allegretta committed
1207
    if (h->current->next != NULL) {	/* any older entries? */
1208
1209
1210
1211
1212
1213
1214
1215
	h->current = h->current->next;	/* yes */
	return h->current->data;	/* return it */
    }
    return NULL;			/* end of list */
}

char *get_history_newer(historyheadtype *h)
{
Chris Allegretta's avatar
Chris Allegretta committed
1216
    if (h->current->prev != NULL) {
1217
	h->current = h->current->prev;
Chris Allegretta's avatar
Chris Allegretta committed
1218
	if (h->current->prev != NULL)
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
	    return h->current->data;
    }
    return NULL;
}

/* get a completion */
char *get_history_completion(historyheadtype *h, char *s)
{
    historytype *p;

Chris Allegretta's avatar
Chris Allegretta committed
1229
1230
    for (p = h->current->next; p->next != NULL; p = p->next) {
	if (strncmp(s, p->data, h->len) == 0 && strlen(p->data) != h->len) {
1231
1232
1233
1234
1235
1236
1237
1238
1239
	    h->current = p;
	    return p->data;
	}
    }
    h->current = (historytype*)h;
    null_at(&s, h->len);
    return s;
}

1240
#ifdef DEBUG
1241
1242
1243
/* free a history list */
void free_history(historyheadtype *h)
{
1244
    historytype *p;
1245

1246
    for (p = h->next; p->next != NULL; p = p->next)
1247
1248
	remove_node(p);
}
1249
#endif
1250
1251
1252

/* end of history support functions */
#endif /* !NANO_SMALL */