search.c 32.3 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(int curr_pos, const char *datastr, const char
269
	*searchword)
270
{
Chris Allegretta's avatar
Chris Allegretta committed
271
    size_t sln = curr_pos + strlen(searchword);
272

273
274
    /* Start of line or previous character is not a letter and end of
     * line or next character is not a letter. */
275
276
    return (curr_pos < 1 || !isalpha(datastr[curr_pos - 1])) &&
	(sln == strlen(datastr) || !isalpha(datastr[sln]));
277
278
}

279
/* Look for needle, starting at current, column current_x.  If
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
280
281
 * no_sameline is TRUE, skip over begin when looking for needle.  begin
 * is the line where we first started searching, at column beginx.  If
282
 * can_display_wrap is TRUE, we put messages on the statusbar, wrap
283
284
285
 * 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. */
286
287
bool findnextstr(bool can_display_wrap, bool wholeword, bool
	no_sameline, const filestruct *begin, size_t beginx, const char
288
	*needle, size_t *needle_len)
Chris Allegretta's avatar
Chris Allegretta committed
289
{
Chris Allegretta's avatar
Chris Allegretta committed
290
    filestruct *fileptr = current;
291
    const char *rev_start = NULL, *found = NULL;
292
293
    size_t found_len;
	/* The length of the match we found. */
294
    size_t current_x_find = 0;
295
	/* The location of the match we found. */
296
    int current_y_find = current_y;
297
298
299
300
301
302

    /* 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
303
    rev_start =
304
#ifndef NANO_SMALL
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
305
	ISSET(REVERSE_SEARCH) ? fileptr->data + (current_x - 1) :
306
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
307
	fileptr->data + (current_x + 1);
Chris Allegretta's avatar
Chris Allegretta committed
308

309
    /* Look for needle in searchstr. */
310
    while (TRUE) {
311
	found = strstrwrapper(fileptr->data, needle, rev_start);
Chris Allegretta's avatar
Chris Allegretta committed
312

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	/* 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) {
329
		char *word = mallocstrncpy(NULL, found, found_len + 1);
330
331
332
333
334
335
336
337
338
339
340
341
342
		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))
343
		break;
Chris Allegretta's avatar
Chris Allegretta committed
344
	}
345

346
	/* We've finished processing the file, so get out. */
347
348
	if (search_last_line) {
	    if (can_display_wrap)
Chris Allegretta's avatar
Chris Allegretta committed
349
		not_found_msg(needle);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
350
	    return FALSE;
351
	}
352
353
354
355
356
357
358
359
360

#ifndef NANO_SMALL
	if (ISSET(REVERSE_SEARCH)) {
	    fileptr = fileptr->prev;
	    current_y_find--;
	} else {
#endif
	    fileptr = fileptr->next;
	    current_y_find++;
361
#ifndef NANO_SMALL
362
	}
363
#endif
364

365
	/* Start or end of buffer reached, so wrap around. */
366
367
	if (fileptr == NULL) {
	    if (!can_display_wrap)
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
368
		return FALSE;
369

370
#ifndef NANO_SMALL
371
372
373
374
	    if (ISSET(REVERSE_SEARCH)) {
		fileptr = filebot;
		current_y_find = editwinrows - 1;
	    } else {
375
#endif
376
377
378
379
380
381
		fileptr = fileage;
		current_y_find = 0;
#ifndef NANO_SMALL
	    }
#endif

382
383
384
	    if (can_display_wrap)
		statusbar(_("Search Wrapped"));
	}
Chris Allegretta's avatar
Chris Allegretta committed
385

386
	/* Original start line reached. */
387
	if (fileptr == begin)
388
	    search_last_line = TRUE;
389

390
391
392
393
394
395
	rev_start = fileptr->data;
#ifndef NANO_SMALL
	if (ISSET(REVERSE_SEARCH))
	    rev_start += strlen(fileptr->data);
#endif
    }
396

397
398
    /* We found an instance. */
    current_x_find = found - fileptr->data;
Chris Allegretta's avatar
Chris Allegretta committed
399

400
401
402
403
404
405
406
407
408
    /* 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
409

410
411
	if (can_display_wrap)
	    not_found_msg(needle);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
412
	return FALSE;
Chris Allegretta's avatar
Chris Allegretta committed
413
414
    }

415
    /* Set globals now that we are sure we found something. */
416
417
    current = fileptr;
    current_x = current_x_find;
418
    current_y = current_y_find;
419
    placewewant = xplustabs();
420
421

    /* needle_len holds the length of needle. */
422
423
    if (needle_len != NULL)
	*needle_len = found_len;
424

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
425
    return TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
426
427
}

428
429
430
431
432
void findnextstr_wrap_reset(void)
{
    search_last_line = FALSE;
}

Chris Allegretta's avatar
Chris Allegretta committed
433
/* Search for a string. */
434
void do_search(void)
Chris Allegretta's avatar
Chris Allegretta committed
435
{
436
    size_t old_pww = placewewant, fileptr_x = current_x;
437
438
    int i;
    bool didfind;
439
    filestruct *fileptr = current;
Chris Allegretta's avatar
Chris Allegretta committed
440

441
#ifndef DISABLE_WRAPPING
Chris Allegretta's avatar
Chris Allegretta committed
442
    wrap_reset();
443
#endif
444

445
    i = search_init(FALSE, FALSE);
446
447
    if (i == -1)	/* Cancel, Go to Line, blank search string, or
			 * regcomp() failed. */
Chris Allegretta's avatar
Chris Allegretta committed
448
	search_abort();
449
    else if (i == -2)	/* Replace. */
Chris Allegretta's avatar
Chris Allegretta committed
450
	do_replace();
451
452
453
#ifndef NANO_SMALL
    else if (i == 1)	/* Case Sensitive, Backwards, or Regexp search
			 * toggle. */
Chris Allegretta's avatar
Chris Allegretta committed
454
	do_search();
455
#endif
456

457
    if (i != 0)
458
	return;
459
460

    /* If answer is now "", copy last_search into answer. */
Chris Allegretta's avatar
Chris Allegretta committed
461
    if (answer[0] == '\0')
462
463
464
465
	answer = mallocstrcpy(answer, last_search);
    else
	last_search = mallocstrcpy(last_search, answer);

466
#ifndef NANO_SMALL
467
468
    /* If answer is not "", add this search string to the search history
     * list. */
Chris Allegretta's avatar
Chris Allegretta committed
469
    if (answer[0] != '\0')
470
	update_history(&search_history, answer);
471
#endif
472

473
    findnextstr_wrap_reset();
474
    didfind = findnextstr(TRUE, FALSE, FALSE, current, current_x,
475
	answer, NULL);
476

477
478
479
480
481
482
483
484
485
    /* 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. */
486
487
	if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
		last_search)) {
488
	    didfind = findnextstr(TRUE, FALSE, TRUE, current, current_x,
489
		answer, NULL);
490
491
492
493
494
495
496
497
498
	    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
    }
499

500
    placewewant = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
501
    edit_redraw(fileptr, old_pww);
Chris Allegretta's avatar
Chris Allegretta committed
502
503
504
    search_abort();
}

505
#ifndef NANO_SMALL
506
/* Search for the next string without prompting. */
507
void do_research(void)
508
{
509
    size_t old_pww = placewewant, fileptr_x = current_x;
510
    bool didfind;
511
    filestruct *fileptr = current;
512

513
#ifndef DISABLE_WRAPPING
514
    wrap_reset();
515
#endif
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
516

517
518
519
520
521
    search_init_globals();

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

#ifdef HAVE_REGEX_H
522
	/* Since answer is "", use last_search! */
523
	if (ISSET(USE_REGEXP) && regexp_init(last_search) == 0)
524
	    return;
525
526
#endif

527
	findnextstr_wrap_reset();
528
	didfind = findnextstr(TRUE, FALSE, FALSE, current, current_x,
529
		last_search, NULL);
530

531
532
533
534
535
536
537
538
539
	/* 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. */
540
541
	    if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
		last_search)) {
542
		didfind = findnextstr(TRUE, FALSE, TRUE, current,
543
			current_x, answer, NULL);
544
545
546
547
548
549
550
551
552
		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
	}
553
554
555
    } else
        statusbar(_("No current search pattern"));

556
    placewewant = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
557
    edit_redraw(fileptr, old_pww);
558
559
    search_abort();
}
560
#endif
561

Chris Allegretta's avatar
Chris Allegretta committed
562
563
void replace_abort(void)
{
564
565
566
    /* 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. */
567
    search_abort();
568
    placewewant = xplustabs();
569
570
}

571
#ifdef HAVE_REGEX_H
572
int replace_regexp(char *string, bool create_flag)
573
{
574
    /* Split personality here - if create_flag is FALSE, just calculate
575
     * the size of the replacement line (necessary because of
576
     * subexpressions \1 to \9 in the replaced text). */
577

578
    const char *c = last_replace;
579
    int search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so;
580
    int new_size = strlen(current->data) + 1 - search_match_count;
581

Chris Allegretta's avatar
Chris Allegretta committed
582
583
    /* Iterate through the replacement text to handle subexpression
     * replacement using \1, \2, \3, etc. */
584
    while (*c != '\0') {
585
586
	int num = (int)(*(c + 1) - '0');

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
587
588
	if (*c != '\\' || num < 1 || num > 9 ||
		num > search_regexp.re_nsub) {
589
590
591
592
593
	    if (create_flag)
		*string++ = *c;
	    c++;
	    new_size++;
	} else {
594
	    int i = regmatches[num].rm_eo - regmatches[num].rm_so;
595

596
597
	    /* Skip over the replacement expression. */
	    c += 2;
598

599
600
	    /* But add the length of the subexpression to new_size. */
	    new_size += i;
601

602
	    /* And if create_flag is TRUE, append the result of the
603
604
605
606
607
	     * subexpression match to the new line. */
	    if (create_flag) {
		strncpy(string, current->data + current_x +
			regmatches[num].rm_so, i);
		string += i;
608
609
	    }
	}
610
611
612
    }

    if (create_flag)
Chris Allegretta's avatar
Chris Allegretta committed
613
	*string = '\0';
614
615
616

    return new_size;
}
617
#endif
618

619
char *replace_line(const char *needle)
620
{
621
    char *copy;
622
623
624
    int new_line_size;
    int search_match_count;

625
    /* Calculate the size of the new line. */
626
#ifdef HAVE_REGEX_H
627
    if (ISSET(USE_REGEXP)) {
628
	search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so;
629
	new_line_size = replace_regexp(NULL, 0);
630
    } else {
631
#endif
632
633
634
635
	search_match_count = strlen(needle);
	new_line_size = strlen(current->data) - search_match_count +
	    strlen(answer) + 1;
#ifdef HAVE_REGEX_H
636
    }
637
#endif
638

639
    /* Create the buffer. */
640
    copy = charalloc(new_line_size);
641

642
    /* The head of the original line. */
643
644
    strncpy(copy, current->data, current_x);

645
    /* The replacement text. */
646
#ifdef HAVE_REGEX_H
647
648
    if (ISSET(USE_REGEXP))
	replace_regexp(copy + current_x, TRUE);
649
    else
650
#endif
651
	strcpy(copy + current_x, answer);
652

653
654
655
    /* The tail of the original line. */
    assert(current_x + search_match_count <= strlen(current->data));
    strcat(copy, current->data + current_x + search_match_count);
656
657

    return copy;
Chris Allegretta's avatar
Chris Allegretta committed
658
659
}

660
/* Step through each replace word and prompt user before replacing.
661
662
663
 * 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.
664
665
 *
 * needle is the string to seek.  We replace it with answer.  Return -1
666
667
 * if needle isn't found, else the number of replacements performed.  If
 * canceled isn't NULL, set it to TRUE if we canceled. */
668
669
670
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
671
{
672
673
    ssize_t numreplaced = -1;
    size_t match_len;
674
    size_t pww_save = placewewant;
675
    bool replaceall = FALSE;
676
#ifdef HAVE_REGEX_H
677
    /* The starting-line match and bol/eol regex flags. */
678
    bool begin_line = FALSE, bol_or_eol = FALSE;
679
#endif
680
#ifndef NANO_SMALL
681
    bool old_mark_set = ISSET(MARK_ISSET);
682
683
684
685
686
    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
687

688
    if (old_mark_set) {
689
	/* If the mark is on, partition the filestruct so that it
690
691
	 * contains only the marked text, set edittop to the top of the
	 * partition, turn the mark off, and refresh the screen. */
692
	mark_order((const filestruct **)&top, &top_x,
693
	    (const filestruct **)&bot, &bot_x, &right_side_up);
694
695
	filepart = partition_filestruct(top, top_x, bot, bot_x);
	edittop = fileage;
696
697
698
	UNSET(MARK_ISSET);
	edit_refresh();
    }
699
#endif
700

701
702
703
    if (canceled != NULL)
	*canceled = FALSE;

704
    findnextstr_wrap_reset();
705
    while (findnextstr(TRUE, wholewords,
706
#ifdef HAVE_REGEX_H
707
708
709
710
711
	/* 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
712
#else
713
	FALSE
714
#endif
715
	, real_current, *real_current_x, needle, &match_len)) {
716
717

	int i = 0;
718

719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#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

736
	if (!replaceall) {
737
	    edit_redraw(real_current, pww_save);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
738
	    pww_save = placewewant;
739
	}
Chris Allegretta's avatar
Chris Allegretta committed
740

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
741
742
	/* Record for the return value that we found the search
	 * string. */
743
744
	if (numreplaced == -1)
	    numreplaced = 0;
745

746
	if (!replaceall) {
747
748
749
750
	    char *exp_word;
	    size_t xpt = xplustabs();

	    exp_word = display_string(current->data, xpt,
751
752
		strnlenpt(current->data, match_len + current_x) - xpt,
		FALSE);
753

754
	    curs_set(0);
755
	    do_replace_highlight(TRUE, exp_word);
756

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

759
760
	    do_replace_highlight(FALSE, exp_word);
	    free(exp_word);
761
	    curs_set(1);
762

763
764
765
	    if (i == -1) {	/* We canceled the replace. */
		if (canceled != NULL)
		    *canceled = TRUE;
766
		break;
767
	    }
768
769
	}

770
#ifdef HAVE_REGEX_H
771
	/* Set the bol_or_eol flag if we're doing a bol and/or eol regex
772
	 * replace ("^", "$", or "^$"). */
773
774
	if (ISSET(USE_REGEXP) && regexp_bol_or_eol(&search_regexp,
		needle))
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
775
	    bol_or_eol = TRUE;
776
777
#endif

778
	if (i > 0 || replaceall) {	/* Yes, replace it!!!! */
779
	    char *copy;
780
	    size_t length_change;
781

782
	    if (i == 2)
783
		replaceall = TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
784

785
	    copy = replace_line(needle);
Chris Allegretta's avatar
Chris Allegretta committed
786

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

789
#ifndef NANO_SMALL
790
791
792
793
	    /* 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
794
795
		if (current == mark_beginbuf &&
			mark_beginx > current_x) {
796
797
798
799
800
		    if (mark_beginx < current_x + match_len)
			mark_beginx = current_x;
		    else
			mark_beginx += length_change;
		}
801
	    }
Chris Allegretta's avatar
Chris Allegretta committed
802

803
804
805
	    /* 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) {
806
#endif
807
		/* Keep real_current_x in sync with the text changes. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
808
809
		if (current == real_current &&
			current_x <= *real_current_x) {
810
811
812
813
		    if (*real_current_x < current_x + match_len)
			*real_current_x = current_x + match_len;
		    *real_current_x += length_change;
		}
814
#ifndef NANO_SMALL
815
	    }
816
#endif
817

818
	    /* Set the cursor at the last character of the replacement
819
	     * text, so searching will resume after the replacement
820
821
	     * text.  Note that current_x might be set to (size_t)-1
	     * here. */
822
#ifndef NANO_SMALL
823
	    if (!ISSET(REVERSE_SEARCH))
824
825
#endif
		current_x += match_len + length_change - 1;
826

827
	    /* Cleanup. */
828
829
830
	    totsize += length_change;
	    free(current->data);
	    current->data = copy;
831

832
833
834
835
836
837
838
839
840
	    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
841
842
	    set_modified();
	    numreplaced++;
843
	}
Chris Allegretta's avatar
Chris Allegretta committed
844
845
    }

846
#ifndef NANO_SMALL
847
    if (old_mark_set) {
848
849
850
	/* 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. */
851
	unpartition_filestruct(&filepart);
852
	edittop = edittop_save;
853
	SET(MARK_ISSET);
854
855
	edit_refresh();
    }
856
857
#endif

858
859
860
861
    /* 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
862
863
864
    return numreplaced;
}

Chris Allegretta's avatar
Chris Allegretta committed
865
/* Replace a string. */
866
void do_replace(void)
Chris Allegretta's avatar
Chris Allegretta committed
867
{
868
    int i;
869
    filestruct *edittop_save, *begin;
870
    size_t beginx, pww_save;
871
    ssize_t numreplaced;
Chris Allegretta's avatar
Chris Allegretta committed
872

Chris Allegretta's avatar
Chris Allegretta committed
873
874
875
    if (ISSET(VIEW_MODE)) {
	print_view_warning();
	replace_abort();
876
	return;
Chris Allegretta's avatar
Chris Allegretta committed
877
878
    }

879
    i = search_init(TRUE, FALSE);
880
881
    if (i == -1) {		/* Cancel, Go to Line, blank search
				 * string, or regcomp() failed. */
Chris Allegretta's avatar
Chris Allegretta committed
882
	replace_abort();
883
	return;
884
    } else if (i == -2) {	/* No Replace. */
Chris Allegretta's avatar
Chris Allegretta committed
885
	do_search();
886
	return;
887
888
889
890
891
    } else if (i == 1)		/* Case Sensitive, Backwards, or Regexp
				 * search toggle. */
	do_replace();

    if (i != 0)
892
	return;
Chris Allegretta's avatar
Chris Allegretta committed
893

894
895
896
    /* If answer is not "", add answer to the search history list and
     * copy answer into last_search. */
    if (answer[0] != '\0') {
897
#ifndef NANO_SMALL
898
	update_history(&search_history, answer);
899
#endif
Chris Allegretta's avatar
Chris Allegretta committed
900
	last_search = mallocstrcpy(last_search, answer);
901
    }
Chris Allegretta's avatar
Chris Allegretta committed
902

903
#ifndef NANO_SMALL
904
905
    replace_history.current = (historytype *)&replace_history.next;
    last_replace = mallocstrcpy(last_replace, "");
906
#endif
907

908
    i = statusq(FALSE, replace_list_2, last_replace,
909
#ifndef NANO_SMALL
910
	&replace_history,
911
#endif
912
	_("Replace with"));
913

914
#ifndef NANO_SMALL
915
916
917
    /* Add this replace string to the replace history list.  i == 0
     * means that the string is not "". */
    if (i == 0)
918
	update_history(&replace_history, answer);
919
920
921
922
923
924
#endif

    if (i != 0 && i != -2) {
	if (i == -1) {		/* Cancel. */
	    if (last_replace[0] != '\0')
		answer = mallocstrcpy(answer, last_replace);
925
	    statusbar(_("Cancelled"));
926
927
	}
	replace_abort();
928
	return;
929
930
931
    }

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

933
    /* Save where we are. */
934
    edittop_save = edittop;
Chris Allegretta's avatar
Chris Allegretta committed
935
    begin = current;
936
    beginx = current_x;
937
    pww_save = placewewant;
Chris Allegretta's avatar
Chris Allegretta committed
938

939
940
    numreplaced = do_replace_loop(last_search, begin, &beginx, FALSE,
	NULL);
Chris Allegretta's avatar
Chris Allegretta committed
941

942
    /* Restore where we were. */
943
    edittop = edittop_save;
Chris Allegretta's avatar
Chris Allegretta committed
944
    current = begin;
945
    current_x = beginx;
946
    placewewant = pww_save;
947

Chris Allegretta's avatar
Chris Allegretta committed
948
    renumber_all();
949
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
950
951

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

Chris Allegretta's avatar
Chris Allegretta committed
955
956
957
    replace_abort();
}

958
void do_gotoline(int line, bool save_pos)
Chris Allegretta's avatar
Chris Allegretta committed
959
{
960
    if (line <= 0) {		/* Ask for it. */
961
	char *ans = mallocstrcpy(NULL, answer);
962
	int i = statusq(FALSE, gotoline_list, line < 0 ? ans : "",
Chris Allegretta's avatar
Chris Allegretta committed
963
#ifndef NANO_SMALL
964
		NULL,
Chris Allegretta's avatar
Chris Allegretta committed
965
#endif
966
967
968
		_("Enter line number"));

	free(ans);
Chris Allegretta's avatar
Chris Allegretta committed
969
970

	/* Cancel, or Enter with blank string. */
971
	if (i < 0) {
972
	    statusbar(_("Cancelled"));
973
	    display_main_list();
974
975
976
	    return;
	}

977
	if (i == NANO_TOOTHERWHEREIS_KEY) {
978
979
980
	    /* Keep answer up on the statusbar. */
	    search_init(TRUE, TRUE);

981
	    do_search();
982
	    return;
Chris Allegretta's avatar
Chris Allegretta committed
983
	}
984

985
986
	/* Do a bounds check.  Display a warning on an out-of-bounds
	 * line number only if we hit Enter at the statusbar prompt. */
987
	if (!parse_num(answer, &line) || line < 0) {
988
989
	    if (i == 0)
		statusbar(_("Come on, be reasonable"));
Chris Allegretta's avatar
Chris Allegretta committed
990
	    display_main_list();
991
	    return;
Chris Allegretta's avatar
Chris Allegretta committed
992
993
994
	}
    }

995
996
997
998
999
1000
1001
1002
1003
    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
1004

1005
    current_x = 0;
1006

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

1011
    placewewant = 0;
Chris Allegretta's avatar
Chris Allegretta committed
1012
    display_main_list();
Chris Allegretta's avatar
Chris Allegretta committed
1013
1014
}

1015
void do_gotoline_void(void)
Chris Allegretta's avatar
Chris Allegretta committed
1016
{
1017
    do_gotoline(0, FALSE);
Chris Allegretta's avatar
Chris Allegretta committed
1018
}
1019

1020
#if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER)
1021
void do_gotopos(int line, size_t pos_x, int pos_y, size_t pos_pww)
1022
{
1023
1024
    /* Since do_gotoline() resets the x-coordinate but not the
     * y-coordinate, set the coordinates up this way. */
1025
    current_y = pos_y;
1026
    do_gotoline(line, TRUE);
1027

1028
1029
1030
1031
    /* Make sure that the x-coordinate is sane here. */
    current_x = strlen(current->data);
    if (pos_x < current_x)
	current_x = pos_x;
1032

1033
    /* Set the rest of the coordinates up. */
1034
    placewewant = pos_pww;
1035
1036
1037
    update_line(current, pos_x);
}
#endif
1038
1039

#if !defined(NANO_SMALL) && defined(HAVE_REGEX_H)
1040
void do_find_bracket(void)
1041
1042
{
    char ch_under_cursor, wanted_ch;
1043
    const char *pos, *brackets = "([{<>}])";
1044
    char regexp_pat[] = "[  ]";
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1045
    size_t current_x_save, pww_save;
1046
    int count = 1;
1047
    long flags_save;
1048
1049
1050
    filestruct *current_save;

    ch_under_cursor = current->data[current_x];
1051

1052
1053
    pos = strchr(brackets, ch_under_cursor);
    if (ch_under_cursor == '\0' || pos == NULL) {
1054
	statusbar(_("Not a bracket"));
1055
	return;
1056
1057
    }

1058
1059
    assert(strlen(brackets) % 2 == 0);
    wanted_ch = brackets[(strlen(brackets) - 1) - (pos - brackets)];
1060
1061

    current_save = current;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1062
    current_x_save = current_x;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1063
    pww_save = placewewant;
1064
    flags_save = flags;
1065
1066
    SET(USE_REGEXP);

1067
1068
    /* Apparent near redundancy with regexp_pat[] here is needed.
     * "[][]" works, "[[]]" doesn't. */
1069

1070
1071
    if (pos < brackets + (strlen(brackets) / 2)) {
	/* On a left bracket. */
1072
1073
1074
	regexp_pat[1] = wanted_ch;
	regexp_pat[2] = ch_under_cursor;
	UNSET(REVERSE_SEARCH);
1075
1076
    } else {
	/* On a right bracket. */
1077
1078
1079
1080
1081
1082
	regexp_pat[1] = ch_under_cursor;
	regexp_pat[2] = wanted_ch;
	SET(REVERSE_SEARCH);
    }

    regexp_init(regexp_pat);
1083
    /* We constructed regexp_pat to be a valid expression. */
1084
    assert(regexp_compiled);
1085

1086
    findnextstr_wrap_reset();
1087
    while (TRUE) {
1088
	if (findnextstr(FALSE, FALSE, FALSE, current, current_x,
1089
		regexp_pat, NULL)) {
1090
	    /* Found identical bracket. */
Chris Allegretta's avatar
Chris Allegretta committed
1091
	    if (current->data[current_x] == ch_under_cursor)
1092
		count++;
1093
1094
1095
	    /* Found complementary bracket. */
	    else if (--count == 0) {
		placewewant = xplustabs();
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1096
		edit_redraw(current_save, pww_save);
1097
		break;
1098
	    }
Chris Allegretta's avatar
Chris Allegretta committed
1099
	} else {
1100
	    /* Didn't find either a left or right bracket. */
1101
1102
	    statusbar(_("No matching bracket"));
	    current = current_save;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1103
	    current_x = current_x_save;
Chris Allegretta's avatar
Chris Allegretta committed
1104
	    update_line(current, current_x);
1105
1106
1107
1108
	    break;
	}
    }

1109
    regexp_cleanup();
1110
    flags = flags_save;
1111
1112
}
#endif
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

#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 */
1138
historytype *find_node(historytype *h, const char *s)
1139
{
Chris Allegretta's avatar
Chris Allegretta committed
1140
    for (; h->next != NULL; h = h->next)
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
	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
1160
    a = (historytype *)nmalloc(sizeof(historytype));
1161
    a->next = h->next;
1162
    a->prev = h;
1163
1164
1165
1166
1167
1168
    h->next->prev = a;
    h->next = a;
    a->data = mallocstrcpy(NULL, s);
}

/* update history list */
1169
void update_history(historyheadtype *h, const char *s)
1170
1171
1172
{
    historytype *p;

Chris Allegretta's avatar
Chris Allegretta committed
1173
1174
1175
    if ((p = find_node(h->next, s)) != NULL) {
	if (p == h->next)		/* catch delete and re-insert of
					   same string in 1st node */
1176
	    goto up_hs;
Chris Allegretta's avatar
Chris Allegretta committed
1177
	remove_node(p);			/* delete identical older string */
1178
1179
1180
1181
1182
1183
1184
1185
	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++;
1186
    SET(HISTORY_CHANGED);
Chris Allegretta's avatar
Chris Allegretta committed
1187
  up_hs:
1188
1189
1190
1191
1192
1193
    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
1194
    if (h->current->next != NULL) {	/* any older entries? */
1195
1196
1197
1198
1199
1200
1201
1202
	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
1203
    if (h->current->prev != NULL) {
1204
	h->current = h->current->prev;
Chris Allegretta's avatar
Chris Allegretta committed
1205
	if (h->current->prev != NULL)
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
	    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
1216
1217
    for (p = h->current->next; p->next != NULL; p = p->next) {
	if (strncmp(s, p->data, h->len) == 0 && strlen(p->data) != h->len) {
1218
1219
1220
1221
1222
1223
1224
1225
1226
	    h->current = p;
	    return p->data;
	}
    }
    h->current = (historytype*)h;
    null_at(&s, h->len);
    return s;
}

1227
#ifdef DEBUG
1228
1229
1230
/* free a history list */
void free_history(historyheadtype *h)
{
1231
    historytype *p;
1232

1233
    for (p = h->next; p->next != NULL; p = p->next)
1234
1235
	remove_node(p);
}
1236
#endif
1237
1238
1239

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