2000-06-19 04:22:15 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* search.c -- This file is part of GNU nano. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2019-02-24 18:32:17 +00:00
|
|
|
* Copyright (C) 1999-2011, 2013-2019 Free Software Foundation, Inc. *
|
2018-06-01 08:17:42 +00:00
|
|
|
* Copyright (C) 2015-2018 Benno Schulenberg *
|
2016-08-29 13:14:18 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano 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. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2000-06-19 04:22:15 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
#include <string.h>
|
2017-07-11 16:04:30 +00:00
|
|
|
#ifdef DEBUG
|
2009-01-24 22:40:41 +00:00
|
|
|
#include <time.h>
|
2017-07-11 16:04:30 +00:00
|
|
|
#endif
|
2000-06-21 03:00:43 +00:00
|
|
|
|
2016-05-01 10:59:32 +00:00
|
|
|
static bool came_full_circle = FALSE;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Have we reached the starting line again while searching? */
|
2018-04-13 08:22:39 +00:00
|
|
|
static bool have_compiled_regexp = FALSE;
|
|
|
|
/* Whether we have compiled a regular expression for the search. */
|
2004-04-30 19:40:03 +00:00
|
|
|
|
2017-01-02 19:25:24 +00:00
|
|
|
/* Compile the given regular expression and store it in search_regexp.
|
|
|
|
* Return TRUE if the expression is valid, and FALSE otherwise. */
|
2007-01-09 23:40:24 +00:00
|
|
|
bool regexp_init(const char *regexp)
|
2000-07-07 01:49:52 +00:00
|
|
|
{
|
2018-12-24 19:28:46 +00:00
|
|
|
int value = regcomp(&search_regexp, regexp,
|
2017-12-29 18:27:33 +00:00
|
|
|
NANO_REG_EXTENDED | (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE));
|
2004-04-30 19:40:03 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If regex compilation failed, show the error message. */
|
|
|
|
if (value != 0) {
|
|
|
|
size_t len = regerror(value, &search_regexp, NULL, 0);
|
|
|
|
char *str = charalloc(len);
|
2004-04-30 19:40:03 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
regerror(value, &search_regexp, str, len);
|
|
|
|
statusline(ALERT, _("Bad regex \"%s\": %s"), regexp, str);
|
|
|
|
free(str);
|
2007-01-09 23:35:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2003-01-29 04:18:37 +00:00
|
|
|
|
2018-04-13 08:22:39 +00:00
|
|
|
have_compiled_regexp = TRUE;
|
2007-01-09 23:35:02 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return TRUE;
|
2000-07-07 01:49:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 08:17:08 +00:00
|
|
|
/* Free a compiled regular expression, if one was compiled; and schedule a
|
|
|
|
* full screen refresh when the mark is on, in case the cursor has moved. */
|
2018-04-13 08:31:18 +00:00
|
|
|
void tidy_up_after_search(void)
|
2002-09-06 20:35:28 +00:00
|
|
|
{
|
2018-04-13 08:22:39 +00:00
|
|
|
if (have_compiled_regexp) {
|
2018-04-13 08:17:08 +00:00
|
|
|
regfree(&search_regexp);
|
2018-04-13 08:22:39 +00:00
|
|
|
have_compiled_regexp = FALSE;
|
2018-04-13 08:17:08 +00:00
|
|
|
}
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->mark)
|
|
|
|
refresh_needed = TRUE;
|
2004-02-24 20:41:39 +00:00
|
|
|
#endif
|
2002-09-06 20:35:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-04 20:23:48 +00:00
|
|
|
/* Prepare the prompt and ask the user what to search for. Keep looping
|
|
|
|
* as long as the user presses a toggle, and only take action and exit
|
|
|
|
* when <Enter> is pressed or a non-toggle shortcut was executed. */
|
|
|
|
void search_init(bool replacing, bool keep_the_answer)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2018-02-04 21:02:45 +00:00
|
|
|
char *thedefault;
|
|
|
|
/* What will be searched for when the user typed nothing. */
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2018-02-07 18:42:19 +00:00
|
|
|
/* When starting a new search, clear the current answer. */
|
|
|
|
if (!keep_the_answer)
|
|
|
|
answer = mallocstrcpy(answer, NULL);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2018-02-04 20:23:48 +00:00
|
|
|
/* If something was searched for earlier, include it in the prompt. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (*last_search != '\0') {
|
2019-02-27 20:37:53 +00:00
|
|
|
char *disp = display_string(last_search, 0, COLS / 3, FALSE, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2018-02-04 21:02:45 +00:00
|
|
|
thedefault = charalloc(strlen(disp) + 7);
|
2017-12-29 18:27:33 +00:00
|
|
|
/* We use (COLS / 3) here because we need to see more on the line. */
|
2018-02-04 21:02:45 +00:00
|
|
|
sprintf(thedefault, " [%s%s]", disp,
|
2019-04-24 06:49:18 +00:00
|
|
|
(breadth(last_search) > COLS / 3) ? "..." : "");
|
2017-12-29 18:27:33 +00:00
|
|
|
free(disp);
|
|
|
|
} else
|
2018-02-04 21:02:45 +00:00
|
|
|
thedefault = mallocstrcpy(NULL, "");
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2018-02-04 14:27:03 +00:00
|
|
|
while (TRUE) {
|
2018-02-04 20:23:48 +00:00
|
|
|
functionptrtype func;
|
|
|
|
/* Ask the user what to search for (or replace). */
|
|
|
|
int i = do_prompt(FALSE, FALSE,
|
|
|
|
inhelp ? MFINDINHELP : (replacing ? MREPLACE : MWHEREIS),
|
2018-02-07 18:42:19 +00:00
|
|
|
answer, &search_history,
|
2018-02-04 20:23:48 +00:00
|
|
|
/* TRANSLATORS: This is the main search prompt. */
|
|
|
|
edit_refresh, "%s%s%s%s%s%s", _("Search"),
|
2018-08-29 18:05:37 +00:00
|
|
|
/* TRANSLATORS: The next four modify the search prompt. */
|
2018-02-04 20:23:48 +00:00
|
|
|
ISSET(CASE_SENSITIVE) ? _(" [Case Sensitive]") : "",
|
|
|
|
ISSET(USE_REGEXP) ? _(" [Regexp]") : "",
|
|
|
|
ISSET(BACKWARDS_SEARCH) ? _(" [Backwards]") : "", replacing ?
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2018-02-04 20:23:48 +00:00
|
|
|
openfile->mark ? _(" (to replace) in selection") :
|
2004-11-03 23:05:11 +00:00
|
|
|
#endif
|
2018-02-04 21:02:45 +00:00
|
|
|
_(" (to replace)") : "", thedefault);
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2018-02-04 20:23:48 +00:00
|
|
|
/* If the search was cancelled, or we have a blank answer and
|
|
|
|
* nothing was searched for yet during this session, get out. */
|
|
|
|
if (i == -1 || (i == -2 && *last_search == '\0')) {
|
|
|
|
statusbar(_("Cancelled"));
|
2019-05-06 08:29:45 +00:00
|
|
|
break;
|
2018-02-04 20:23:48 +00:00
|
|
|
}
|
2008-03-05 07:34:01 +00:00
|
|
|
|
2018-02-04 20:23:48 +00:00
|
|
|
/* If Enter was pressed, prepare to do a replace or a search. */
|
|
|
|
if (i == 0 || i == -2) {
|
|
|
|
/* If an actual answer was typed, remember it. */
|
|
|
|
if (*answer != '\0') {
|
|
|
|
last_search = mallocstrcpy(last_search, answer);
|
2017-10-29 18:42:12 +00:00
|
|
|
#ifdef ENABLE_HISTORIES
|
2018-02-04 20:23:48 +00:00
|
|
|
update_history(&search_history, answer);
|
2016-03-19 16:19:44 +00:00
|
|
|
#endif
|
2018-02-04 20:23:48 +00:00
|
|
|
}
|
2018-02-04 14:27:03 +00:00
|
|
|
|
2018-04-24 11:24:42 +00:00
|
|
|
/* When not doing a regular-expression search, just search;
|
|
|
|
* otherwise compile the search string, and only search when
|
|
|
|
* the expression is valid. */
|
|
|
|
if (!ISSET(USE_REGEXP) || regexp_init(last_search)) {
|
|
|
|
if (replacing)
|
|
|
|
ask_for_replacement();
|
|
|
|
else
|
|
|
|
go_looking();
|
2018-02-04 20:23:48 +00:00
|
|
|
}
|
2018-02-04 14:27:03 +00:00
|
|
|
|
2019-05-06 08:29:45 +00:00
|
|
|
break;
|
2018-02-04 20:23:48 +00:00
|
|
|
}
|
2018-02-04 14:27:03 +00:00
|
|
|
|
2018-02-04 20:23:48 +00:00
|
|
|
func = func_from_key(&i);
|
|
|
|
|
|
|
|
/* If we're here, one of the five toggles was pressed, or
|
|
|
|
* a shortcut was executed. */
|
|
|
|
if (func == case_sens_void) {
|
|
|
|
TOGGLE(CASE_SENSITIVE);
|
|
|
|
} else if (func == backwards_void) {
|
|
|
|
TOGGLE(BACKWARDS_SEARCH);
|
|
|
|
} else if (func == regexp_void) {
|
|
|
|
TOGGLE(USE_REGEXP);
|
|
|
|
} else if (func == flip_replace) {
|
2018-09-11 06:57:17 +00:00
|
|
|
if (ISSET(VIEW_MODE)) {
|
|
|
|
print_view_warning();
|
2019-05-06 09:08:22 +00:00
|
|
|
napms(600);
|
|
|
|
} else
|
|
|
|
replacing = !replacing;
|
2018-02-04 20:23:48 +00:00
|
|
|
} else {
|
|
|
|
if (func == flip_goto)
|
|
|
|
do_gotolinecolumn(openfile->current->lineno,
|
|
|
|
openfile->placewewant + 1, TRUE, TRUE);
|
2019-05-06 08:29:45 +00:00
|
|
|
break;
|
2018-02-04 20:23:48 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2019-05-06 08:29:45 +00:00
|
|
|
|
|
|
|
tidy_up_after_search();
|
|
|
|
free(thedefault);
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2015-04-25 15:17:22 +00:00
|
|
|
/* Look for needle, starting at (current, current_x). begin is the line
|
2016-03-30 12:00:48 +00:00
|
|
|
* where we first started searching, at column begin_x. Return 1 when we
|
|
|
|
* found something, 0 when nothing, and -2 on cancel. When match_len is
|
|
|
|
* not NULL, set it to the length of the found string, if any. */
|
2017-10-26 17:15:11 +00:00
|
|
|
int findnextstr(const char *needle, bool whole_word_only, int modus,
|
2019-03-21 16:08:52 +00:00
|
|
|
size_t *match_len, bool skipone, const linestruct *begin, size_t begin_x)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t found_len = strlen(needle);
|
|
|
|
/* The length of a match -- will be recomputed for a regex. */
|
|
|
|
int feedback = 0;
|
|
|
|
/* When bigger than zero, show and wipe the "Searching..." message. */
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *line = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The line that we will search through now. */
|
|
|
|
const char *from = line->data + openfile->current_x;
|
|
|
|
/* The point in the line from where we start searching. */
|
|
|
|
const char *found = NULL;
|
|
|
|
/* A pointer to the location of the match, if any. */
|
|
|
|
size_t found_x;
|
|
|
|
/* The x coordinate of a found occurrence. */
|
|
|
|
time_t lastkbcheck = time(NULL);
|
|
|
|
/* The time we last looked at the keyboard. */
|
|
|
|
|
|
|
|
/* Set non-blocking input so that we can just peek for a Cancel. */
|
|
|
|
disable_waiting();
|
|
|
|
|
|
|
|
if (begin == NULL)
|
|
|
|
came_full_circle = FALSE;
|
|
|
|
|
|
|
|
/* Start searching through the lines, looking for the needle. */
|
|
|
|
while (TRUE) {
|
|
|
|
/* Glance at the keyboard once every second. */
|
|
|
|
if (time(NULL) - lastkbcheck > 0) {
|
|
|
|
int input = parse_kbinput(edit);
|
|
|
|
|
|
|
|
lastkbcheck = time(NULL);
|
|
|
|
|
|
|
|
/* Consume all waiting keystrokes until a Cancel. */
|
2018-04-13 18:02:28 +00:00
|
|
|
while (input != ERR) {
|
2017-12-29 18:27:33 +00:00
|
|
|
if (func_from_key(&input) == do_cancel) {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
enable_waiting();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
input = parse_kbinput(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++feedback > 0)
|
|
|
|
/* TRANSLATORS: This is shown when searching takes
|
|
|
|
* more than half a second. */
|
|
|
|
statusbar(_("Searching..."));
|
2016-05-05 20:12:24 +00:00
|
|
|
}
|
2009-01-24 22:40:41 +00:00
|
|
|
|
2018-01-07 21:24:10 +00:00
|
|
|
/* When starting a new search, skip the first character, then
|
|
|
|
* (in either case) search for the needle in the current line. */
|
2018-01-02 20:20:32 +00:00
|
|
|
if (skipone) {
|
2017-12-29 18:27:33 +00:00
|
|
|
skipone = FALSE;
|
|
|
|
if (ISSET(BACKWARDS_SEARCH) && from != line->data) {
|
|
|
|
from = line->data + move_mbleft(line->data, from - line->data);
|
2018-01-07 21:24:10 +00:00
|
|
|
found = strstrwrapper(line->data, needle, from);
|
2017-12-29 18:27:33 +00:00
|
|
|
} else if (!ISSET(BACKWARDS_SEARCH) && *from != '\0') {
|
|
|
|
from += move_mbright(from, 0);
|
2018-01-07 21:24:10 +00:00
|
|
|
found = strstrwrapper(line->data, needle, from);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2018-01-07 21:24:10 +00:00
|
|
|
} else
|
|
|
|
found = strstrwrapper(line->data, needle, from);
|
2017-01-13 15:11:47 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (found != NULL) {
|
|
|
|
/* When doing a regex search, compute the length of the match. */
|
|
|
|
if (ISSET(USE_REGEXP))
|
|
|
|
found_len = regmatches[0].rm_eo - regmatches[0].rm_so;
|
2017-10-31 18:32:42 +00:00
|
|
|
#ifdef ENABLE_SPELLER
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When we're spell checking, a match should be a separate word;
|
|
|
|
* if it's not, continue looking in the rest of the line. */
|
|
|
|
if (whole_word_only && !is_separate_word(found - line->data,
|
|
|
|
found_len, line->data)) {
|
|
|
|
from = found + move_mbright(found, 0);
|
|
|
|
continue;
|
|
|
|
}
|
2005-11-16 05:59:06 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The match is valid. */
|
|
|
|
break;
|
|
|
|
}
|
2004-08-27 20:28:34 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're back at the beginning, then there is no needle. */
|
|
|
|
if (came_full_circle) {
|
|
|
|
enable_waiting();
|
|
|
|
return 0;
|
|
|
|
}
|
2002-06-21 03:20:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Move to the previous or next line in the file. */
|
|
|
|
if (ISSET(BACKWARDS_SEARCH))
|
|
|
|
line = line->prev;
|
|
|
|
else
|
|
|
|
line = line->next;
|
|
|
|
|
|
|
|
/* If we've reached the start or end of the buffer, wrap around;
|
|
|
|
* but stop when spell-checking or replacing in a region. */
|
|
|
|
if (line == NULL) {
|
|
|
|
if (whole_word_only || modus == INREGION) {
|
|
|
|
enable_waiting();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ISSET(BACKWARDS_SEARCH))
|
|
|
|
line = openfile->filebot;
|
|
|
|
else
|
2019-03-21 16:23:49 +00:00
|
|
|
line = openfile->filetop;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (modus == JUSTFIND) {
|
|
|
|
statusbar(_("Search Wrapped"));
|
|
|
|
/* Delay the "Searching..." message for at least two seconds. */
|
|
|
|
feedback = -2;
|
|
|
|
}
|
|
|
|
}
|
2001-06-13 02:35:44 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we've reached the original starting line, take note. */
|
|
|
|
if (line == begin)
|
|
|
|
came_full_circle = TRUE;
|
2004-10-27 02:21:01 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Set the starting x to the start or end of the line. */
|
|
|
|
from = line->data;
|
|
|
|
if (ISSET(BACKWARDS_SEARCH))
|
|
|
|
from += strlen(line->data);
|
|
|
|
}
|
2000-10-24 22:25:36 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
found_x = found - line->data;
|
2016-05-01 10:35:47 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
enable_waiting();
|
2017-06-04 10:32:27 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Ensure that the found occurrence is not beyond the starting x. */
|
2018-01-14 21:17:47 +00:00
|
|
|
if (came_full_circle && ((!ISSET(BACKWARDS_SEARCH) && (found_x > begin_x ||
|
|
|
|
(modus == REPLACING && found_x == begin_x))) ||
|
2017-12-29 18:27:33 +00:00
|
|
|
(ISSET(BACKWARDS_SEARCH) && found_x < begin_x)))
|
|
|
|
return 0;
|
2016-05-01 10:35:47 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Set the current position to point at what we found. */
|
|
|
|
openfile->current = line;
|
|
|
|
openfile->current_x = found_x;
|
2004-10-21 15:32:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When requested, pass back the length of the match. */
|
|
|
|
if (match_len != NULL)
|
|
|
|
*match_len = found_len;
|
2000-11-29 04:33:26 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Wipe the "Searching..." message and unset the suppression flag. */
|
|
|
|
if (feedback > 0) {
|
|
|
|
wipe_statusbar();
|
|
|
|
suppress_cursorpos = FALSE;
|
|
|
|
}
|
2016-03-28 19:00:19 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return 1;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2016-03-20 16:57:15 +00:00
|
|
|
/* Ask what to search for and then go looking for it. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_search(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2018-02-04 14:27:03 +00:00
|
|
|
search_init(FALSE, FALSE);
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 20:07:25 +00:00
|
|
|
/* Search forward for a string. */
|
|
|
|
void do_search_forward(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
UNSET(BACKWARDS_SEARCH);
|
|
|
|
do_search();
|
2017-09-17 20:07:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search backwards for a string. */
|
|
|
|
void do_search_backward(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
SET(BACKWARDS_SEARCH);
|
|
|
|
do_search();
|
2017-09-17 20:07:25 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Search for the last string without prompting. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_research(void)
|
2003-08-23 21:11:06 +00:00
|
|
|
{
|
2017-10-29 18:42:12 +00:00
|
|
|
#ifdef ENABLE_HISTORIES
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If nothing was searched for yet during this run of nano, but
|
|
|
|
* there is a search history, take the most recent item. */
|
|
|
|
if (*last_search == '\0' && searchbot->prev != NULL)
|
|
|
|
last_search = mallocstrcpy(last_search, searchbot->prev->data);
|
2015-06-20 08:10:25 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (*last_search == '\0') {
|
|
|
|
statusbar(_("No current search pattern"));
|
|
|
|
return;
|
|
|
|
}
|
2016-03-20 16:03:20 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(USE_REGEXP) && !regexp_init(last_search))
|
|
|
|
return;
|
2003-08-23 21:11:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Use the search-menu key bindings, to allow cancelling. */
|
|
|
|
currmenu = MWHEREIS;
|
2016-03-17 09:12:30 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
go_looking();
|
2018-02-04 20:31:04 +00:00
|
|
|
|
2018-04-13 08:31:18 +00:00
|
|
|
tidy_up_after_search();
|
2016-03-20 16:57:15 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 19:09:44 +00:00
|
|
|
/* Search in the backward direction for the next occurrence. */
|
|
|
|
void do_findprevious(void)
|
|
|
|
{
|
|
|
|
SET(BACKWARDS_SEARCH);
|
|
|
|
do_research();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search in the forward direction for the next occurrence. */
|
|
|
|
void do_findnext(void)
|
|
|
|
{
|
|
|
|
UNSET(BACKWARDS_SEARCH);
|
|
|
|
do_research();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Report on the status bar that the given string was not found. */
|
|
|
|
void not_found_msg(const char *str)
|
|
|
|
{
|
2019-02-27 20:37:53 +00:00
|
|
|
char *disp = display_string(str, 0, (COLS / 2) + 1, FALSE, FALSE);
|
2019-04-24 07:08:23 +00:00
|
|
|
size_t numchars = actual_x(disp, wideness(disp, COLS / 2));
|
2018-04-13 19:09:44 +00:00
|
|
|
|
|
|
|
statusline(HUSH, _("\"%.*s%s\" not found"), numchars, disp,
|
|
|
|
(disp[numchars] == '\0') ? "" : "...");
|
|
|
|
free(disp);
|
|
|
|
}
|
|
|
|
|
2016-03-20 16:57:15 +00:00
|
|
|
/* Search for the global string 'last_search'. Inform the user when
|
|
|
|
* the string occurs only once. */
|
|
|
|
void go_looking(void)
|
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t was_current_x = openfile->current_x;
|
2016-08-06 12:39:08 +00:00
|
|
|
#ifdef DEBUG
|
2017-12-29 18:27:33 +00:00
|
|
|
clock_t start = clock();
|
2016-08-06 12:39:08 +00:00
|
|
|
#endif
|
2016-03-20 16:57:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
came_full_circle = FALSE;
|
2016-05-01 10:59:32 +00:00
|
|
|
|
2018-01-02 20:20:32 +00:00
|
|
|
didfind = findnextstr(last_search, FALSE, JUSTFIND, NULL, TRUE,
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current, openfile->current_x);
|
2003-08-23 21:11:06 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we found something, and we're back at the exact same spot
|
|
|
|
* where we started searching, then this is the only occurrence. */
|
|
|
|
if (didfind == 1 && openfile->current == was_current &&
|
|
|
|
openfile->current_x == was_current_x)
|
|
|
|
statusbar(_("This is the only occurrence"));
|
|
|
|
else if (didfind == 0)
|
|
|
|
not_found_msg(last_search);
|
2003-08-23 21:11:06 +00:00
|
|
|
|
2016-08-06 12:39:08 +00:00
|
|
|
#ifdef DEBUG
|
2017-12-29 18:27:33 +00:00
|
|
|
statusline(HUSH, "Took: %.2f", (double)(clock() - start) / CLOCKS_PER_SEC);
|
2016-08-06 12:39:08 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
edit_redraw(was_current, CENTERING);
|
2003-08-23 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-26 17:29:34 +00:00
|
|
|
/* Calculate the size of the replacement text, taking possible
|
2015-07-26 17:04:29 +00:00
|
|
|
* subexpressions \1 to \9 into account. Return the replacement
|
|
|
|
* text in the passed string only when create is TRUE. */
|
2005-05-26 03:47:24 +00:00
|
|
|
int replace_regexp(char *string, bool create)
|
2000-07-07 01:49:52 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *c = answer;
|
|
|
|
size_t replacement_size = 0;
|
|
|
|
|
|
|
|
/* Iterate through the replacement text to handle subexpression
|
|
|
|
* replacement using \1, \2, \3, etc. */
|
|
|
|
while (*c != '\0') {
|
|
|
|
int num = (*(c + 1) - '0');
|
|
|
|
|
|
|
|
if (*c != '\\' || num < 1 || num > 9 || num > search_regexp.re_nsub) {
|
|
|
|
if (create)
|
|
|
|
*string++ = *c;
|
|
|
|
c++;
|
|
|
|
replacement_size++;
|
|
|
|
} else {
|
|
|
|
size_t i = regmatches[num].rm_eo - regmatches[num].rm_so;
|
|
|
|
|
|
|
|
/* Skip over the replacement expression. */
|
|
|
|
c += 2;
|
|
|
|
|
|
|
|
/* But add the length of the subexpression to new_size. */
|
|
|
|
replacement_size += i;
|
|
|
|
|
|
|
|
/* And if create is TRUE, append the result of the
|
|
|
|
* subexpression match to the new line. */
|
|
|
|
if (create) {
|
|
|
|
strncpy(string, openfile->current->data +
|
|
|
|
regmatches[num].rm_so, i);
|
|
|
|
string += i;
|
|
|
|
}
|
|
|
|
}
|
2000-10-26 01:44:42 +00:00
|
|
|
}
|
2000-07-07 01:49:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (create)
|
|
|
|
*string = '\0';
|
2000-07-07 01:49:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return replacement_size;
|
2000-07-07 01:49:52 +00:00
|
|
|
}
|
2000-10-26 01:44:42 +00:00
|
|
|
|
2015-07-26 17:04:29 +00:00
|
|
|
/* Return a copy of the current line with one needle replaced. */
|
2004-02-24 20:41:39 +00:00
|
|
|
char *replace_line(const char *needle)
|
2000-07-07 01:49:52 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
char *copy;
|
|
|
|
size_t match_len;
|
|
|
|
size_t new_line_size = strlen(openfile->current->data) + 1;
|
|
|
|
|
|
|
|
/* First adjust the size of the new line for the change. */
|
|
|
|
if (ISSET(USE_REGEXP)) {
|
|
|
|
match_len = regmatches[0].rm_eo - regmatches[0].rm_so;
|
|
|
|
new_line_size += replace_regexp(NULL, FALSE) - match_len;
|
|
|
|
} else {
|
|
|
|
match_len = strlen(needle);
|
|
|
|
new_line_size += strlen(answer) - match_len;
|
|
|
|
}
|
2000-10-26 01:44:42 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Create the buffer. */
|
|
|
|
copy = charalloc(new_line_size);
|
2000-07-07 01:49:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Copy the head of the original line. */
|
|
|
|
strncpy(copy, openfile->current->data, openfile->current_x);
|
2000-07-07 01:49:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Add the replacement text. */
|
|
|
|
if (ISSET(USE_REGEXP))
|
|
|
|
replace_regexp(copy + openfile->current_x, TRUE);
|
|
|
|
else
|
|
|
|
strcpy(copy + openfile->current_x, answer);
|
2000-07-07 01:49:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
assert(openfile->current_x + match_len <= strlen(openfile->current->data));
|
2005-05-26 03:32:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Copy the tail of the original line. */
|
|
|
|
strcat(copy, openfile->current->data + openfile->current_x + match_len);
|
2000-07-07 01:49:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return copy;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2016-07-27 07:23:49 +00:00
|
|
|
/* Step through each occurrence of the search string and prompt the user
|
|
|
|
* before replacing it. We seek for needle, and replace it with answer.
|
|
|
|
* The parameters real_current and real_current_x are needed in order to
|
2004-10-08 23:06:01 +00:00
|
|
|
* allow the cursor position to be updated when a word before the cursor
|
2016-07-27 07:23:49 +00:00
|
|
|
* is replaced by a shorter word. Return -1 if needle isn't found, -2 if
|
|
|
|
* the seeking is aborted, else the number of replacements performed. */
|
2016-10-23 15:59:26 +00:00
|
|
|
ssize_t do_replace_loop(const char *needle, bool whole_word_only,
|
2019-03-21 16:08:52 +00:00
|
|
|
const linestruct *real_current, size_t *real_current_x)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
ssize_t numreplaced = -1;
|
|
|
|
size_t match_len;
|
|
|
|
bool replaceall = FALSE;
|
2018-01-02 20:20:32 +00:00
|
|
|
bool skipone = ISSET(BACKWARDS_SEARCH);
|
2017-12-29 18:27:33 +00:00
|
|
|
int modus = REPLACING;
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *was_mark = openfile->mark;
|
|
|
|
linestruct *top, *bot;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t top_x, bot_x;
|
|
|
|
bool right_side_up = FALSE;
|
|
|
|
/* TRUE if (mark_begin, mark_begin_x) is the top of the mark,
|
|
|
|
* FALSE if (current, current_x) is. */
|
|
|
|
|
|
|
|
/* If the mark is on, frame the region, and turn the mark off. */
|
|
|
|
if (openfile->mark) {
|
2019-04-28 09:01:51 +00:00
|
|
|
get_region((const linestruct **)&top, &top_x,
|
|
|
|
(const linestruct **)&bot, &bot_x, &right_side_up);
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->mark = NULL;
|
|
|
|
modus = INREGION;
|
|
|
|
|
|
|
|
/* Start either at the top or the bottom of the marked region. */
|
|
|
|
if (!ISSET(BACKWARDS_SEARCH)) {
|
|
|
|
openfile->current = top;
|
|
|
|
openfile->current_x = top_x;
|
|
|
|
} else {
|
|
|
|
openfile->current = bot;
|
|
|
|
openfile->current_x = bot_x;
|
|
|
|
}
|
2015-04-11 15:21:08 +00:00
|
|
|
}
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif /* !NANO_TINY */
|
2003-09-16 01:16:49 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
came_full_circle = FALSE;
|
2016-05-01 10:59:32 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (TRUE) {
|
2019-02-10 14:40:08 +00:00
|
|
|
int choice = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
int result = findnextstr(needle, whole_word_only, modus,
|
|
|
|
&match_len, skipone, real_current, *real_current_x);
|
|
|
|
|
|
|
|
/* If nothing more was found, or the user aborted, stop looping. */
|
|
|
|
if (result < 1) {
|
|
|
|
if (result < 0)
|
|
|
|
numreplaced = -2; /* It's a Cancel instead of Not found. */
|
|
|
|
break;
|
|
|
|
}
|
2004-10-09 16:26:32 +00:00
|
|
|
|
2015-04-11 15:21:08 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* An occurrence outside of the marked region means we're done. */
|
|
|
|
if (was_mark && (openfile->current->lineno > bot->lineno ||
|
|
|
|
openfile->current->lineno < top->lineno ||
|
|
|
|
(openfile->current == bot &&
|
|
|
|
openfile->current_x + match_len > bot_x) ||
|
|
|
|
(openfile->current == top &&
|
|
|
|
openfile->current_x < top_x)))
|
|
|
|
break;
|
2015-04-11 15:21:08 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Indicate that we found the search string. */
|
|
|
|
if (numreplaced == -1)
|
|
|
|
numreplaced = 0;
|
2003-09-16 01:16:49 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (!replaceall) {
|
2018-09-29 16:14:43 +00:00
|
|
|
spotlighted = TRUE;
|
|
|
|
light_from_col = xplustabs();
|
2019-04-24 07:08:23 +00:00
|
|
|
light_to_col = wideness(openfile->current->data,
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x + match_len);
|
2003-09-16 01:16:49 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Refresh the edit window, scrolling it if necessary. */
|
|
|
|
edit_refresh();
|
2015-03-27 11:29:23 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* TRANSLATORS: This is a prompt. */
|
2019-02-10 14:40:08 +00:00
|
|
|
choice = do_yesno_prompt(TRUE, _("Replace this instance?"));
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2018-09-29 16:14:43 +00:00
|
|
|
spotlighted = FALSE;
|
2005-06-15 19:15:14 +00:00
|
|
|
|
2019-02-10 14:40:08 +00:00
|
|
|
if (choice == -1) /* The replacing was cancelled. */
|
2017-12-29 18:27:33 +00:00
|
|
|
break;
|
2019-02-10 14:40:08 +00:00
|
|
|
else if (choice == 2)
|
2017-12-29 18:27:33 +00:00
|
|
|
replaceall = TRUE;
|
2017-01-24 21:37:37 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When "No" or moving backwards, the search routine should
|
|
|
|
* first move one character further before continuing. */
|
2019-02-10 14:40:08 +00:00
|
|
|
skipone = (choice == 0 || ISSET(BACKWARDS_SEARCH));
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2000-11-29 04:33:26 +00:00
|
|
|
|
2019-02-10 14:40:08 +00:00
|
|
|
if (choice == 1 || replaceall) { /* Yes, replace it. */
|
2017-12-29 18:27:33 +00:00
|
|
|
char *copy;
|
|
|
|
size_t length_change;
|
2003-01-26 04:26:25 +00:00
|
|
|
|
2008-08-02 22:31:01 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
add_undo(REPLACE);
|
2008-08-02 22:31:01 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
copy = replace_line(needle);
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
length_change = strlen(copy) - strlen(openfile->current->data);
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the mark was on and it was located after the cursor,
|
|
|
|
* then adjust its x position for any text length changes. */
|
|
|
|
if (was_mark && !right_side_up) {
|
|
|
|
if (openfile->current == was_mark &&
|
|
|
|
openfile->mark_x > openfile->current_x) {
|
|
|
|
if (openfile->mark_x < openfile->current_x + match_len)
|
|
|
|
openfile->mark_x = openfile->current_x;
|
|
|
|
else
|
|
|
|
openfile->mark_x += length_change;
|
|
|
|
bot_x = openfile->mark_x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the mark was not on or it was before the cursor, then
|
|
|
|
* adjust the cursor's x position for any text length changes. */
|
|
|
|
if (!was_mark || right_side_up) {
|
2004-11-03 22:03:41 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
if (openfile->current == real_current &&
|
|
|
|
openfile->current_x < *real_current_x) {
|
|
|
|
if (*real_current_x < openfile->current_x + match_len)
|
|
|
|
*real_current_x = openfile->current_x + match_len;
|
|
|
|
*real_current_x += length_change;
|
2015-04-13 10:59:12 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
bot_x = *real_current_x;
|
|
|
|
}
|
2004-11-05 14:37:18 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2002-02-16 20:03:44 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Don't find the same zero-length or BOL match again. */
|
|
|
|
if (match_len == 0 || (*needle == '^' && ISSET(USE_REGEXP)))
|
|
|
|
skipone = TRUE;
|
2017-02-21 22:04:39 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When moving forward, put the cursor just after the replacement
|
|
|
|
* text, so that searching will continue there. */
|
|
|
|
if (!ISSET(BACKWARDS_SEARCH))
|
|
|
|
openfile->current_x += match_len + length_change;
|
2002-02-16 20:03:44 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Update the file size, and put the changed line into place. */
|
|
|
|
openfile->totsize += mbstrlen(copy) - mbstrlen(openfile->current->data);
|
|
|
|
free(openfile->current->data);
|
|
|
|
openfile->current->data = copy;
|
2000-10-24 22:25:36 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (!replaceall) {
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When doing syntax coloring, the replacement might require
|
|
|
|
* a change of colors, so refresh the whole edit window. */
|
|
|
|
if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX))
|
|
|
|
edit_refresh();
|
|
|
|
else
|
2004-05-28 20:44:09 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
update_line(openfile->current, openfile->current_x);
|
|
|
|
}
|
2004-05-28 20:44:09 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
set_modified();
|
|
|
|
as_an_at = TRUE;
|
|
|
|
numreplaced++;
|
|
|
|
}
|
2003-12-29 02:15:23 +00:00
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (numreplaced == -1)
|
|
|
|
not_found_msg(needle);
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2017-12-29 18:27:33 +00:00
|
|
|
else if (numreplaced > 0)
|
|
|
|
refresh_needed = TRUE;
|
2017-02-13 18:11:04 +00:00
|
|
|
#endif
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->mark = was_mark;
|
2004-02-24 20:41:39 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If "automatic newline" is enabled, and text has been added to the
|
2019-01-06 14:35:31 +00:00
|
|
|
* magic line, make a new magic line. */
|
2019-04-07 06:47:29 +00:00
|
|
|
if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0')
|
2017-12-29 18:27:33 +00:00
|
|
|
new_magicline();
|
2004-11-03 22:03:41 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return numreplaced;
|
2000-11-05 16:52:21 +00:00
|
|
|
}
|
|
|
|
|
2003-01-13 01:35:15 +00:00
|
|
|
/* Replace a string. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_replace(void)
|
2000-11-05 16:52:21 +00:00
|
|
|
{
|
2018-02-04 20:23:48 +00:00
|
|
|
if (ISSET(VIEW_MODE))
|
2017-12-29 18:27:33 +00:00
|
|
|
print_view_warning();
|
2018-04-12 11:33:41 +00:00
|
|
|
else {
|
|
|
|
UNSET(BACKWARDS_SEARCH);
|
2018-02-04 20:23:48 +00:00
|
|
|
search_init(TRUE, FALSE);
|
2018-04-12 11:33:41 +00:00
|
|
|
}
|
2018-02-04 14:27:03 +00:00
|
|
|
}
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2018-02-04 20:23:48 +00:00
|
|
|
/* Ask the user what the already given search string should be replaced with. */
|
2018-02-04 14:27:03 +00:00
|
|
|
void ask_for_replacement(void)
|
|
|
|
{
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *edittop_save, *begin;
|
2018-02-04 14:27:03 +00:00
|
|
|
size_t firstcolumn_save, begin_x;
|
|
|
|
ssize_t numreplaced;
|
|
|
|
int i = do_prompt(FALSE, FALSE, MREPLACEWITH, NULL, &replace_history,
|
2017-12-29 18:27:33 +00:00
|
|
|
/* TRANSLATORS: This is a prompt. */
|
|
|
|
edit_refresh, _("Replace with"));
|
2003-01-05 21:47:06 +00:00
|
|
|
|
2017-10-29 18:42:12 +00:00
|
|
|
#ifdef ENABLE_HISTORIES
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If the replace string is not "", add it to the replace history list. */
|
|
|
|
if (i == 0)
|
|
|
|
update_history(&replace_history, answer);
|
2004-02-24 20:41:39 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When cancelled, or when a function was run, get out. */
|
|
|
|
if (i == -1 || i > 0) {
|
|
|
|
if (i == -1)
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
return;
|
|
|
|
}
|
2004-02-24 20:41:39 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Save where we are. */
|
|
|
|
edittop_save = openfile->edittop;
|
|
|
|
firstcolumn_save = openfile->firstcolumn;
|
|
|
|
begin = openfile->current;
|
|
|
|
begin_x = openfile->current_x;
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
numreplaced = do_replace_loop(last_search, FALSE, begin, &begin_x);
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Restore where we were. */
|
|
|
|
openfile->edittop = edittop_save;
|
|
|
|
openfile->firstcolumn = firstcolumn_save;
|
|
|
|
openfile->current = begin;
|
|
|
|
openfile->current_x = begin_x;
|
|
|
|
refresh_needed = TRUE;
|
2003-01-13 01:35:15 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (numreplaced >= 0)
|
|
|
|
statusline(HUSH, P_("Replaced %zd occurrence",
|
|
|
|
"Replaced %zd occurrences", numreplaced), numreplaced);
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2014-05-15 20:00:46 +00:00
|
|
|
/* Go to the specified line and x position. */
|
|
|
|
void goto_line_posx(ssize_t line, size_t pos_x)
|
|
|
|
{
|
2019-03-21 16:23:49 +00:00
|
|
|
for (openfile->current = openfile->filetop; line > 1 &&
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current != openfile->filebot; line--)
|
|
|
|
openfile->current = openfile->current->next;
|
2014-05-15 20:00:46 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_x = pos_x;
|
|
|
|
openfile->placewewant = xplustabs();
|
2014-05-15 20:00:46 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
refresh_needed = TRUE;
|
2014-05-15 20:00:46 +00:00
|
|
|
}
|
|
|
|
|
2005-05-25 18:44:37 +00:00
|
|
|
/* Go to the specified line and column, or ask for them if interactive
|
2015-12-31 19:20:40 +00:00
|
|
|
* is TRUE. In the latter case also update the screen afterwards.
|
2015-12-31 17:20:46 +00:00
|
|
|
* Note that both the line and column number should be one-based. */
|
2005-06-29 00:17:18 +00:00
|
|
|
void do_gotolinecolumn(ssize_t line, ssize_t column, bool use_answer,
|
2017-12-29 18:27:33 +00:00
|
|
|
bool interactive)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
if (interactive) {
|
|
|
|
/* Ask for the line and column. */
|
|
|
|
int i = do_prompt(FALSE, FALSE, MGOTOLINE,
|
|
|
|
use_answer ? answer : NULL, NULL,
|
|
|
|
/* TRANSLATORS: This is a prompt. */
|
|
|
|
edit_refresh, _("Enter line number, column number"));
|
|
|
|
|
|
|
|
/* If the user cancelled or gave a blank answer, get out. */
|
|
|
|
if (i < 0) {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
return;
|
|
|
|
}
|
2005-07-16 07:06:36 +00:00
|
|
|
|
2018-02-04 09:34:39 +00:00
|
|
|
if (func_from_key(&i) == flip_goto) {
|
2018-04-24 11:11:41 +00:00
|
|
|
UNSET(BACKWARDS_SEARCH);
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Retain what the user typed so far and switch to searching. */
|
2018-02-04 14:27:03 +00:00
|
|
|
search_init(FALSE, TRUE);
|
|
|
|
return;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2004-09-30 22:07:21 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If a function was executed, we're done here. */
|
|
|
|
if (i > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Try to extract one or two numbers from the user's response. */
|
|
|
|
if (!parse_line_column(answer, &line, &column)) {
|
|
|
|
statusline(ALERT, _("Invalid line or column number"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (line == 0)
|
|
|
|
line = openfile->current->lineno;
|
2008-03-13 08:23:52 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (column == 0)
|
|
|
|
column = openfile->placewewant + 1;
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2001-04-20 01:59:55 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Take a negative line number to mean: from the end of the file. */
|
|
|
|
if (line < 0)
|
|
|
|
line = openfile->filebot->lineno + line + 1;
|
|
|
|
if (line < 1)
|
|
|
|
line = 1;
|
2016-06-21 14:47:11 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Iterate to the requested line. */
|
2019-03-21 16:23:49 +00:00
|
|
|
for (openfile->current = openfile->filetop; line > 1 &&
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current != openfile->filebot; line--)
|
|
|
|
openfile->current = openfile->current->next;
|
|
|
|
|
|
|
|
/* Take a negative column number to mean: from the end of the line. */
|
|
|
|
if (column < 0)
|
2019-04-24 06:49:18 +00:00
|
|
|
column = breadth(openfile->current->data) + column + 2;
|
2017-12-29 18:27:33 +00:00
|
|
|
if (column < 1)
|
|
|
|
column = 1;
|
|
|
|
|
|
|
|
/* Set the x position that corresponds to the requested column. */
|
|
|
|
openfile->current_x = actual_x(openfile->current->data, column - 1);
|
|
|
|
openfile->placewewant = column - 1;
|
2001-07-11 02:08:33 +00:00
|
|
|
|
2017-05-11 08:52:39 +00:00
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(SOFTWRAP) && openfile->placewewant / editwincols >
|
2019-04-24 06:49:18 +00:00
|
|
|
breadth(openfile->current->data) / editwincols)
|
|
|
|
openfile->placewewant = breadth(openfile->current->data);
|
2017-05-11 08:52:39 +00:00
|
|
|
#endif
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* When the position was manually given, center the target line. */
|
|
|
|
if (interactive) {
|
|
|
|
adjust_viewport(CENTERING);
|
|
|
|
refresh_needed = TRUE;
|
|
|
|
} else {
|
|
|
|
int rows_from_tail;
|
2017-01-15 20:38:35 +00:00
|
|
|
|
|
|
|
#ifndef NANO_TINY
|
2017-12-29 18:27:33 +00:00
|
|
|
if (ISSET(SOFTWRAP)) {
|
2019-03-21 16:08:52 +00:00
|
|
|
linestruct *currentline = openfile->current;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t leftedge = leftedge_for(xplustabs(), openfile->current);
|
2017-01-15 20:38:35 +00:00
|
|
|
|
2018-03-22 14:20:05 +00:00
|
|
|
rows_from_tail = (editwinrows / 2) - go_forward_chunks(
|
2018-03-23 10:31:08 +00:00
|
|
|
editwinrows / 2, ¤tline, &leftedge);
|
2017-12-29 18:27:33 +00:00
|
|
|
} else
|
2017-01-15 20:38:35 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
rows_from_tail = openfile->filebot->lineno -
|
|
|
|
openfile->current->lineno;
|
|
|
|
|
|
|
|
/* If the target line is close to the tail of the file, put the last
|
|
|
|
* line or chunk on the bottom line of the screen; otherwise, just
|
|
|
|
* center the target line. */
|
2019-01-29 19:23:13 +00:00
|
|
|
if (rows_from_tail < editwinrows / 2 && !ISSET(JUMPY_SCROLLING)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
openfile->current_y = editwinrows - 1 - rows_from_tail;
|
|
|
|
adjust_viewport(STATIONARY);
|
|
|
|
} else
|
|
|
|
adjust_viewport(CENTERING);
|
|
|
|
}
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
|
|
|
|
2005-12-08 07:09:08 +00:00
|
|
|
/* Go to the specified line and column, asking for them beforehand. */
|
2005-05-17 18:06:26 +00:00
|
|
|
void do_gotolinecolumn_void(void)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
do_gotolinecolumn(openfile->current->lineno,
|
|
|
|
openfile->placewewant + 1, FALSE, TRUE);
|
2000-06-19 04:22:15 +00:00
|
|
|
}
|
2001-09-19 03:19:43 +00:00
|
|
|
|
2005-11-15 03:17:35 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-04-06 19:05:02 +00:00
|
|
|
/* Search, starting from the current position, for any of the two characters
|
2019-04-27 15:01:02 +00:00
|
|
|
* in bracket_pair. If reverse is TRUE, search backwards, otherwise forwards.
|
2019-04-06 19:05:02 +00:00
|
|
|
* Return TRUE when one of the brackets was found, and FALSE otherwise. */
|
2019-04-27 15:01:02 +00:00
|
|
|
bool find_a_bracket(bool reverse, const char *bracket_pair)
|
2005-11-15 23:45:29 +00:00
|
|
|
{
|
2019-04-06 18:29:04 +00:00
|
|
|
linestruct *line = openfile->current;
|
2019-04-27 14:31:57 +00:00
|
|
|
const char *pointer, *found;
|
2005-11-15 23:45:29 +00:00
|
|
|
|
2019-04-06 19:05:02 +00:00
|
|
|
/* Step away from the current bracket, either backwards or forwards. */
|
|
|
|
if (reverse) {
|
2019-04-27 12:29:45 +00:00
|
|
|
if (openfile->current_x == 0) {
|
2019-04-06 19:05:02 +00:00
|
|
|
line = line->prev;
|
|
|
|
if (line == NULL)
|
|
|
|
return FALSE;
|
|
|
|
pointer = line->data + strlen(line->data);
|
2019-04-27 12:29:45 +00:00
|
|
|
} else
|
|
|
|
pointer = line->data + move_mbleft(line->data, openfile->current_x);
|
2019-04-06 19:05:02 +00:00
|
|
|
} else
|
2019-04-27 14:31:57 +00:00
|
|
|
pointer = line->data + move_mbright(line->data, openfile->current_x);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-04-06 19:05:02 +00:00
|
|
|
/* Now seek for any of the two brackets, either backwards or forwards. */
|
2017-12-29 18:27:33 +00:00
|
|
|
while (TRUE) {
|
2019-04-06 19:05:02 +00:00
|
|
|
if (reverse)
|
2019-04-27 15:01:02 +00:00
|
|
|
found = mbrevstrpbrk(line->data, bracket_pair, pointer);
|
2017-12-29 18:27:33 +00:00
|
|
|
else
|
2019-04-27 15:01:02 +00:00
|
|
|
found = mbstrpbrk(pointer, bracket_pair);
|
2005-11-15 23:45:29 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (found)
|
|
|
|
break;
|
2005-11-15 23:45:29 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (reverse)
|
2019-04-06 18:29:04 +00:00
|
|
|
line = line->prev;
|
2017-12-29 18:27:33 +00:00
|
|
|
else
|
2019-04-06 18:29:04 +00:00
|
|
|
line = line->next;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If we've reached the start or end of the buffer, get out. */
|
2019-04-06 18:29:04 +00:00
|
|
|
if (line == NULL)
|
2017-12-29 18:27:33 +00:00
|
|
|
return FALSE;
|
2005-11-15 23:45:29 +00:00
|
|
|
|
2019-04-06 18:29:04 +00:00
|
|
|
pointer = line->data;
|
2017-12-29 18:27:33 +00:00
|
|
|
if (reverse)
|
2019-04-06 18:29:04 +00:00
|
|
|
pointer += strlen(line->data);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 19:05:02 +00:00
|
|
|
/* Set the current position to the found bracket. */
|
2019-04-06 18:29:04 +00:00
|
|
|
openfile->current = line;
|
|
|
|
openfile->current_x = found - line->data;
|
2005-11-15 23:45:29 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return TRUE;
|
2005-11-15 23:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search for a match to the bracket at the current cursor position, if
|
|
|
|
* there is one. */
|
2004-07-02 14:31:03 +00:00
|
|
|
void do_find_bracket(void)
|
2001-09-22 22:14:25 +00:00
|
|
|
{
|
2019-04-06 09:54:32 +00:00
|
|
|
linestruct *was_current = openfile->current;
|
|
|
|
size_t was_current_x = openfile->current_x;
|
|
|
|
/* The current cursor position, in case we don't find a complement. */
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *ch;
|
2019-04-05 08:35:48 +00:00
|
|
|
/* The location in matchbrackets of the bracket under the cursor. */
|
2017-12-29 18:27:33 +00:00
|
|
|
int ch_len;
|
|
|
|
/* The length of ch in bytes. */
|
|
|
|
const char *wanted_ch;
|
2019-04-05 08:35:48 +00:00
|
|
|
/* The location in matchbrackets of the complementing bracket. */
|
2017-12-29 18:27:33 +00:00
|
|
|
int wanted_ch_len;
|
|
|
|
/* The length of wanted_ch in bytes. */
|
2019-04-27 15:01:02 +00:00
|
|
|
char bracket_pair[MAXCHARLEN * 2 + 1];
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The pair of characters in ch and wanted_ch. */
|
2019-04-05 08:45:22 +00:00
|
|
|
size_t halfway = 0;
|
|
|
|
/* The index in matchbrackets where the closing brackets start. */
|
2019-04-05 09:01:07 +00:00
|
|
|
size_t charcount = mbstrlen(matchbrackets) / 2;
|
2019-04-05 08:35:48 +00:00
|
|
|
/* Half the number of characters in matchbrackets. */
|
2019-04-05 08:50:15 +00:00
|
|
|
size_t balance = 1;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* The initial bracket count. */
|
|
|
|
bool reverse;
|
|
|
|
/* The direction we search. */
|
|
|
|
|
2019-04-05 08:35:48 +00:00
|
|
|
ch = mbstrchr(matchbrackets, openfile->current->data + openfile->current_x);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-04-05 08:35:48 +00:00
|
|
|
if (ch == NULL) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusbar(_("Not a bracket"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-05 08:35:48 +00:00
|
|
|
/* Find the halfway point in matchbrackets, where the closing ones start. */
|
2019-04-05 09:01:07 +00:00
|
|
|
for (size_t i = 0; i < charcount; i++)
|
2019-04-05 08:45:22 +00:00
|
|
|
halfway += parse_mbchar(matchbrackets + halfway, NULL, NULL);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-04-05 08:35:48 +00:00
|
|
|
/* When on a closing bracket, we have to search backwards for a matching
|
|
|
|
* opening bracket; otherwise, forward for a matching closing bracket. */
|
2019-04-05 08:45:22 +00:00
|
|
|
reverse = (ch >= (matchbrackets + halfway));
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-04-05 09:01:07 +00:00
|
|
|
/* Step half the number of total characters either backwards or forwards
|
|
|
|
* through matchbrackets to find the wanted complementary bracket. */
|
2017-12-29 18:27:33 +00:00
|
|
|
wanted_ch = ch;
|
2019-04-05 09:01:07 +00:00
|
|
|
while (charcount-- > 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
if (reverse)
|
|
|
|
wanted_ch = matchbrackets + move_mbleft(matchbrackets,
|
|
|
|
wanted_ch - matchbrackets);
|
|
|
|
else
|
|
|
|
wanted_ch += move_mbright(wanted_ch, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ch_len = parse_mbchar(ch, NULL, NULL);
|
|
|
|
wanted_ch_len = parse_mbchar(wanted_ch, NULL, NULL);
|
|
|
|
|
2019-04-27 15:01:02 +00:00
|
|
|
/* Copy the two complementary brackets into a single string. */
|
|
|
|
strncpy(bracket_pair, ch, ch_len);
|
|
|
|
strncpy(bracket_pair + ch_len, wanted_ch, wanted_ch_len);
|
|
|
|
bracket_pair[ch_len + wanted_ch_len] = '\0';
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-04-27 15:10:10 +00:00
|
|
|
while (find_a_bracket(reverse, bracket_pair)) {
|
2019-04-27 15:17:06 +00:00
|
|
|
/* Increment/decrement balance for an identical/other bracket. */
|
|
|
|
balance += (strncmp(openfile->current->data + openfile->current_x,
|
|
|
|
ch, ch_len) == 0) ? 1 : -1;
|
|
|
|
|
|
|
|
/* When balance reached zero, we've found the complementary bracket. */
|
|
|
|
if (balance == 0) {
|
|
|
|
edit_redraw(was_current, FLOWING);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-27 15:10:10 +00:00
|
|
|
}
|
2019-04-27 15:17:06 +00:00
|
|
|
|
|
|
|
statusbar(_("No matching bracket"));
|
|
|
|
|
|
|
|
/* Restore the cursor position. */
|
|
|
|
openfile->current = was_current;
|
|
|
|
openfile->current_x = was_current_x;
|
2001-09-22 22:14:25 +00:00
|
|
|
}
|
2014-06-19 20:05:24 +00:00
|
|
|
#endif /* !NANO_TINY */
|