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-2004 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
#include "config.h"

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

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

/* 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. */
45
int regexp_init(const char *regexp)
46
{
47
48
49
50
51
    int rc = regcomp(&search_regexp, regexp, REG_EXTENDED
#ifndef NANO_SMALL
	| (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE)
#endif
	);
52
53
54
55
56
57
58
59
60

    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);
61
	return 0;
62
    }
63

64
    regexp_compiled = TRUE;
65
    return 1;
66
67
}

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

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

    disp = display_string(str, 0, (COLS / 2) + 1);
    numchars = strnlen(disp, COLS / 2);

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

    free(disp);
91
92
93
94
95
}

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

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

113
114
115
116
117
118
/* 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
119
 *
120
121
122
 * 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
123
{
124
    int i = 0;
125
    char *buf;
126
    static char *backupstring = NULL;
127
128
129
130
131
132
133
134
135
136
137
	/* 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;
    }
138
139
140
141

    /* 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,
142
     * we should put the same search string back up. */
143

Chris Allegretta's avatar
Chris Allegretta committed
144
    search_init_globals();
145

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

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

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

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

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

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

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

191
192
193
194
195
196
197
198
199
200
	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
201
	buf);
Chris Allegretta's avatar
Chris Allegretta committed
202

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

206
207
208
    free(backupstring);
    backupstring = NULL;

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

266
bool is_whole_word(int curr_pos, const char *datastr, const char
267
	*searchword)
268
{
Chris Allegretta's avatar
Chris Allegretta committed
269
    size_t sln = curr_pos + strlen(searchword);
270

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
423
    return TRUE;
Chris Allegretta's avatar
Chris Allegretta committed
424
425
}

426
427
428
429
430
void findnextstr_wrap_reset(void)
{
    search_last_line = FALSE;
}

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

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

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

455
    if (i != 0)
456
	return;
457
458

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

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

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

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

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

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

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

515
516
517
518
519
    search_init_globals();

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

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

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

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

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

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

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

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

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

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

594
595
	    /* Skip over the replacement expression. */
	    c += 2;
596

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

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

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

    return new_size;
}
615
#endif
616

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

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

637
    /* Create the buffer. */
638
    copy = charalloc(new_line_size);
639

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

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

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

    return copy;
Chris Allegretta's avatar
Chris Allegretta committed
656
657
}

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

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

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

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

	int i = 0;
717

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

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

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

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

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

752
	    curs_set(0);
753
	    do_replace_highlight(TRUE, exp_word);
754

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

757
758
	    do_replace_highlight(FALSE, exp_word);
	    free(exp_word);
759
	    curs_set(1);
760

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

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

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

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

783
	    copy = replace_line(needle);
Chris Allegretta's avatar
Chris Allegretta committed
784

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

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

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

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

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

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

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

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

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

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

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

    if (i != 0)
890
	return;
Chris Allegretta's avatar
Chris Allegretta committed
891

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

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

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

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

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

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

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

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

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

Chris Allegretta's avatar
Chris Allegretta committed
946
    renumber_all();
947
    edit_refresh();
Chris Allegretta's avatar
Chris Allegretta committed
948
949

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

Chris Allegretta's avatar
Chris Allegretta committed
953
954
955
    replace_abort();
}

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

	free(ans);
Chris Allegretta's avatar
Chris Allegretta committed
967
968

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

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

979
	    do_search();
980
	    return;
Chris Allegretta's avatar
Chris Allegretta committed
981
	}
982

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

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

1003
    current_x = 0;
1004

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

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

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

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

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

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

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

    ch_under_cursor = current->data[current_x];
1049

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

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

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

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

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

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

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

1107
    regexp_cleanup();
1108
    flags = flags_save;
1109
1110
}
#endif
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135

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

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

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

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

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

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