search.c 32.9 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
 *   any later version.                                                   *
 *                                                                        *
11
12
13
14
 *   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.                             *
Chris Allegretta's avatar
Chris Allegretta committed
15
16
17
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
18
19
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
Chris Allegretta's avatar
Chris Allegretta committed
20
21
22
 *                                                                        *
 **************************************************************************/

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

Chris Allegretta's avatar
Chris Allegretta committed
27
28
29
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
30
#include <unistd.h>
Chris Allegretta's avatar
Chris Allegretta committed
31
#include <ctype.h>
32
#include <errno.h>
Chris Allegretta's avatar
Chris Allegretta committed
33
#include <assert.h>
Chris Allegretta's avatar
Chris Allegretta committed
34
#include "proto.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_gotolinecolumn(1, 1, TRUE, TRUE, 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);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
557
558
		if (fileptr == current && fileptr_x == current_x &&
			!didfind)
559
560
561
562
563
564
565
566
		    statusbar(_("This is the only occurrence"));
	    } else {
#endif
		statusbar(_("This is the only occurrence"));
#ifdef HAVE_REGEX_H
	    }
#endif
	}
567
568
569
    } else
        statusbar(_("No current search pattern"));

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

Chris Allegretta's avatar
Chris Allegretta committed
576
577
void replace_abort(void)
{
578
579
580
    /* 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. */
581
    search_abort();
582
    placewewant = xplustabs();
583
584
}

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

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

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

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

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

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

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

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

    return new_size;
}
631
#endif
632

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

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

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

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

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

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

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

674
/* Step through each replace word and prompt user before replacing.
675
676
677
 * 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.
678
679
 *
 * needle is the string to seek.  We replace it with answer.  Return -1
680
681
 * if needle isn't found, else the number of replacements performed.  If
 * canceled isn't NULL, set it to TRUE if we canceled. */
682
683
684
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
685
{
686
687
    ssize_t numreplaced = -1;
    size_t match_len;
688
    size_t pww_save = placewewant;
689
    bool replaceall = FALSE;
690
#ifdef HAVE_REGEX_H
691
    /* The starting-line match and bol/eol regex flags. */
692
    bool begin_line = FALSE, bol_or_eol = FALSE;
693
#endif
694
#ifndef NANO_SMALL
695
    bool old_mark_set = ISSET(MARK_ISSET);
696
697
698
699
700
    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
701

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

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

718
    findnextstr_wrap_reset();
719
    while (findnextstr(TRUE, wholewords,
720
#ifdef HAVE_REGEX_H
721
722
723
724
725
	/* 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
726
#else
727
	FALSE
728
#endif
729
	, real_current, *real_current_x, needle, &match_len)) {
730
731

	int i = 0;
732

733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#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

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

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

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

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

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

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

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

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

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

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

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

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

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

803
#ifndef NANO_SMALL
804
805
806
807
	    /* 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
808
809
		if (current == mark_beginbuf &&
			mark_beginx > current_x) {
810
811
812
813
814
		    if (mark_beginx < current_x + match_len)
			mark_beginx = current_x;
		    else
			mark_beginx += length_change;
		}
815
	    }
Chris Allegretta's avatar
Chris Allegretta committed
816

817
818
819
	    /* 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) {
820
#endif
821
		/* Keep real_current_x in sync with the text changes. */
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
822
823
		if (current == real_current &&
			current_x <= *real_current_x) {
824
825
826
827
		    if (*real_current_x < current_x + match_len)
			*real_current_x = current_x + match_len;
		    *real_current_x += length_change;
		}
828
#ifndef NANO_SMALL
829
	    }
830
#endif
831

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

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

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

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

860
#ifndef NANO_SMALL
861
    if (old_mark_set) {
862
863
864
	/* 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. */
865
	unpartition_filestruct(&filepart);
866
	edittop = edittop_save;
867
	SET(MARK_ISSET);
868
869
	edit_refresh();
    }
870
871
#endif

872
873
874
875
    /* 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
876
877
878
    return numreplaced;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	free(ans);
Chris Allegretta's avatar
Chris Allegretta committed
984
985

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

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

996
	    do_search();
997
	    return;
Chris Allegretta's avatar
Chris Allegretta committed
998
	}
999

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

	if (column < 1)
	    column = 1;
Chris Allegretta's avatar
Chris Allegretta committed
1015
1016
    }

1017
1018
1019
1020
1021
1022
1023
1024
1025
    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
1026

1027
    current_x = actual_x(current->data, column - 1);
1028

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

1033
    placewewant = xplustabs();
Chris Allegretta's avatar
Chris Allegretta committed
1034
    display_main_list();
Chris Allegretta's avatar
Chris Allegretta committed
1035
1036
}

1037
void do_gotolinecolumn_void(void)
Chris Allegretta's avatar
Chris Allegretta committed
1038
{
1039
    do_gotolinecolumn(1, 1, FALSE, TRUE, FALSE);
Chris Allegretta's avatar
Chris Allegretta committed
1040
}
1041

1042
#if defined(ENABLE_MULTIBUFFER) || !defined(DISABLE_SPELLER)
1043
void do_gotopos(int line, size_t pos_x, int pos_y, size_t pos_pww)
1044
{
1045
    /* Since do_gotolinecolumn() resets the x-coordinate but not the
1046
     * y-coordinate, set the coordinates up this way. */
1047
    current_y = pos_y;
1048
    do_gotolinecolumn(line, pos_x, FALSE, FALSE, TRUE);
1049

1050
    /* Set the rest of the coordinates up. */
1051
    placewewant = pos_pww;
1052
1053
1054
    update_line(current, pos_x);
}
#endif
1055
1056

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

    ch_under_cursor = current->data[current_x];
1068

1069
1070
    pos = strchr(brackets, ch_under_cursor);
    if (ch_under_cursor == '\0' || pos == NULL) {
1071
	statusbar(_("Not a bracket"));
1072
	return;
1073
1074
    }

1075
    assert(strlen(brackets) % 2 == 0);
1076

1077
    wanted_ch = brackets[(strlen(brackets) - 1) - (pos - brackets)];
1078
1079

    current_save = current;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1080
    current_x_save = current_x;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1081
    pww_save = placewewant;
1082
    flags_save = flags;
1083
1084
    SET(USE_REGEXP);

1085
1086
    /* Apparent near redundancy with regexp_pat[] here is needed.
     * "[][]" works, "[[]]" doesn't. */
1087
1088
    if (pos < brackets + (strlen(brackets) / 2)) {
	/* On a left bracket. */
1089
1090
1091
	regexp_pat[1] = wanted_ch;
	regexp_pat[2] = ch_under_cursor;
	UNSET(REVERSE_SEARCH);
1092
1093
    } else {
	/* On a right bracket. */
1094
1095
1096
1097
1098
1099
	regexp_pat[1] = ch_under_cursor;
	regexp_pat[2] = wanted_ch;
	SET(REVERSE_SEARCH);
    }

    regexp_init(regexp_pat);
1100

1101
    /* We constructed regexp_pat to be a valid expression. */
1102
    assert(regexp_compiled);
1103

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

1127
    regexp_cleanup();
1128
    flags = flags_save;
1129
1130
}
#endif
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155

#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 */
1156
historytype *find_node(historytype *h, const char *s)
1157
{
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1158
    for (; h->next != NULL; h = h->next) {
1159
1160
	if (strcmp(s, h->data) == 0)
	    return h;
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1161
    }
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
    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
1179
    a = (historytype *)nmalloc(sizeof(historytype));
1180
    a->next = h->next;
1181
    a->prev = h;
1182
1183
1184
1185
1186
1187
    h->next->prev = a;
    h->next = a;
    a->data = mallocstrcpy(NULL, s);
}

/* update history list */
1188
void update_history(historyheadtype *h, const char *s)
1189
1190
1191
{
    historytype *p;

Chris Allegretta's avatar
Chris Allegretta committed
1192
1193
1194
    if ((p = find_node(h->next, s)) != NULL) {
	if (p == h->next)		/* catch delete and re-insert of
					   same string in 1st node */
1195
	    goto up_hs;
Chris Allegretta's avatar
Chris Allegretta committed
1196
	remove_node(p);			/* delete identical older string */
1197
1198
1199
1200
1201
1202
1203
1204
	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++;
1205
    SET(HISTORY_CHANGED);
Chris Allegretta's avatar
Chris Allegretta committed
1206
  up_hs:
1207
1208
1209
1210
1211
1212
    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
1213
    if (h->current->next != NULL) {	/* any older entries? */
1214
1215
1216
1217
1218
1219
1220
1221
	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
1222
    if (h->current->prev != NULL) {
1223
	h->current = h->current->prev;
1224
1225
	if (h->current->prev != NULL)
	    return h->current->data;
1226
1227
1228
1229
1230
1231
1232
1233
1234
    }
    return NULL;
}

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

Chris Allegretta's avatar
Chris Allegretta committed
1235
1236
    for (p = h->current->next; p->next != NULL; p = p->next) {
	if (strncmp(s, p->data, h->len) == 0 && strlen(p->data) != h->len) {
1237
1238
1239
1240
	    h->current = p;
	    return p->data;
	}
    }
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
1241
    h->current = (historytype *)h;
1242
1243
1244
1245
    null_at(&s, h->len);
    return s;
}

1246
#ifdef DEBUG
1247
1248
1249
/* free a history list */
void free_history(historyheadtype *h)
{
1250
    historytype *p;
1251

1252
    for (p = h->next; p->next != NULL; p = p->next)
1253
1254
	remove_node(p);
}
1255
#endif
1256
1257
1258

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