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
|
|
|
* *
|
2020-01-15 10:42:38 +00:00
|
|
|
* Copyright (C) 1999-2011, 2013-2020 Free Software Foundation, Inc. *
|
2020-11-30 11:01:47 +00:00
|
|
|
* Copyright (C) 2015-2020 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
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2020-06-20 10:09:31 +00:00
|
|
|
#include "prototypes.h"
|
2001-04-28 18:03:52 +00:00
|
|
|
|
2000-06-19 04:22:15 +00:00
|
|
|
#include <string.h>
|
2020-09-13 17:40:39 +00:00
|
|
|
#include <time.h>
|
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);
|
2020-08-30 02:57:45 +00:00
|
|
|
char *str = nmalloc(len);
|
2004-04-30 19:40:03 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
regerror(value, &search_regexp, str, len);
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(AHEM, _("Bad regex \"%s\": %s"), regexp, str);
|
2017-12-29 18:27:33 +00:00
|
|
|
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. */
|
2020-07-26 09:49:42 +00:00
|
|
|
void search_init(bool replacing, bool retain_answer)
|
2000-06-19 04:22:15 +00:00
|
|
|
{
|
2018-02-04 21:02:45 +00:00
|
|
|
char *thedefault;
|
2020-07-26 09:49:42 +00:00
|
|
|
/* What will be searched for when the user types just <Enter>. */
|
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
|
|
|
|
2020-08-30 02:57:45 +00:00
|
|
|
thedefault = nmalloc(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
|
2019-10-13 10:24:27 +00:00
|
|
|
thedefault = copy_of("");
|
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). */
|
2020-06-20 15:15:40 +00:00
|
|
|
int response = do_prompt(
|
2018-02-04 20:23:48 +00:00
|
|
|
inhelp ? MFINDINHELP : (replacing ? MREPLACE : MWHEREIS),
|
2020-07-26 09:49:42 +00:00
|
|
|
retain_answer ? answer : "", &search_history, edit_refresh,
|
2018-02-04 20:23:48 +00:00
|
|
|
/* TRANSLATORS: This is the main search prompt. */
|
2019-05-08 17:32:30 +00:00
|
|
|
"%s%s%s%s%s%s", _("Search"),
|
2019-09-29 13:08:57 +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]") : "",
|
2019-05-08 17:32:30 +00:00
|
|
|
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. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response == -1 || (response == -2 && *last_search == '\0')) {
|
2018-02-04 20:23:48 +00:00
|
|
|
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. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response == 0 || response == -2) {
|
2018-02-04 20:23:48 +00:00
|
|
|
/* 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
|
|
|
|
2020-04-07 17:19:26 +00:00
|
|
|
if (ISSET(USE_REGEXP) && !regexp_init(last_search))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (replacing)
|
|
|
|
ask_for_and_do_replacements();
|
|
|
|
else
|
|
|
|
go_looking();
|
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
|
|
|
|
2020-08-18 07:10:15 +00:00
|
|
|
retain_answer = TRUE;
|
|
|
|
|
2019-05-08 17:17:16 +00:00
|
|
|
func = func_from_key(&response);
|
2018-02-04 20:23:48 +00:00
|
|
|
|
|
|
|
/* If we're here, one of the five toggles was pressed, or
|
|
|
|
* a shortcut was executed. */
|
2019-05-06 09:20:31 +00:00
|
|
|
if (func == case_sens_void)
|
2018-02-04 20:23:48 +00:00
|
|
|
TOGGLE(CASE_SENSITIVE);
|
2019-05-06 09:20:31 +00:00
|
|
|
else if (func == backwards_void)
|
2018-02-04 20:23:48 +00:00
|
|
|
TOGGLE(BACKWARDS_SEARCH);
|
2019-05-06 09:20:31 +00:00
|
|
|
else if (func == regexp_void)
|
2018-02-04 20:23:48 +00:00
|
|
|
TOGGLE(USE_REGEXP);
|
2019-05-06 09:20:31 +00:00
|
|
|
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;
|
2019-05-06 09:20:31 +00:00
|
|
|
} else if (func == flip_goto) {
|
|
|
|
do_gotolinecolumn(openfile->current->lineno,
|
|
|
|
openfile->placewewant + 1, TRUE, TRUE);
|
|
|
|
break;
|
|
|
|
} else
|
2019-05-06 08:29:45 +00:00
|
|
|
break;
|
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. */
|
2020-08-20 07:17:48 +00:00
|
|
|
nodelay(edit, TRUE);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (begin == NULL)
|
|
|
|
came_full_circle = FALSE;
|
|
|
|
|
|
|
|
while (TRUE) {
|
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) {
|
2019-06-09 17:37:56 +00:00
|
|
|
from = line->data + step_left(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') {
|
2019-06-09 17:22:24 +00:00
|
|
|
from += char_length(from);
|
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)) {
|
2019-06-09 17:22:24 +00:00
|
|
|
from = found + char_length(found);
|
2017-12-29 18:27:33 +00:00
|
|
|
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
|
|
|
|
2020-08-20 08:07:14 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (the_window_resized) {
|
|
|
|
regenerate_screen();
|
|
|
|
nodelay(edit, TRUE);
|
|
|
|
statusbar(_("Searching..."));
|
|
|
|
feedback = 1;
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
/* If we're back at the beginning, then there is no needle. */
|
|
|
|
if (came_full_circle) {
|
2020-08-20 07:17:48 +00:00
|
|
|
nodelay(edit, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
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. */
|
2020-08-20 08:44:06 +00:00
|
|
|
line = (ISSET(BACKWARDS_SEARCH)) ? line->prev : line->next;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* 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) {
|
2020-08-20 07:17:48 +00:00
|
|
|
nodelay(edit, FALSE);
|
2017-12-29 18:27:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-20 08:44:06 +00:00
|
|
|
line = (ISSET(BACKWARDS_SEARCH)) ? openfile->filebot : openfile->filetop;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (modus == JUSTFIND) {
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(REMARK, _("Search Wrapped"));
|
2017-12-29 18:27:33 +00:00
|
|
|
/* 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);
|
2020-08-20 08:51:19 +00:00
|
|
|
|
|
|
|
/* Glance at the keyboard once every second, to check for a Cancel. */
|
|
|
|
if (time(NULL) - lastkbcheck > 0) {
|
|
|
|
int input = wgetch(edit);
|
|
|
|
|
|
|
|
lastkbcheck = time(NULL);
|
|
|
|
|
|
|
|
/* Consume any queued-up keystrokes, until a Cancel or nothing. */
|
|
|
|
while (input != ERR) {
|
|
|
|
if (input == ESC_CODE) {
|
|
|
|
napms(20);
|
|
|
|
input = wgetch(edit);
|
|
|
|
meta_key = TRUE;
|
|
|
|
} else
|
|
|
|
meta_key = FALSE;
|
|
|
|
|
|
|
|
if (func_from_key(&input) == do_cancel) {
|
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (the_window_resized)
|
|
|
|
regenerate_screen();
|
|
|
|
#endif
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
/* Clear out the key buffer (in case a macro is running). */
|
|
|
|
while (input != ERR)
|
|
|
|
input = parse_kbinput(NULL);
|
|
|
|
nodelay(edit, FALSE);
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
input = wgetch(edit);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++feedback > 0)
|
|
|
|
/* TRANSLATORS: This is shown when searching takes
|
|
|
|
* more than half a second. */
|
|
|
|
statusbar(_("Searching..."));
|
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
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
|
|
|
|
2020-08-20 07:17:48 +00:00
|
|
|
nodelay(edit, FALSE);
|
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
|
|
|
|
2020-12-12 13:48:59 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (modus == JUSTFIND && (!openfile->mark || openfile->softmark)) {
|
|
|
|
openfile->mark = line;
|
|
|
|
openfile->mark_x = found_x + found_len;
|
|
|
|
openfile->softmark = TRUE;
|
2020-12-12 13:50:20 +00:00
|
|
|
if (!ISSET(SHOW_CURSOR))
|
|
|
|
hide_cursor = TRUE;
|
2020-12-12 13:48:59 +00:00
|
|
|
shift_held = TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-03 07:32:25 +00:00
|
|
|
/* Wipe the "Searching..." message and unsuppress cursor-position display. */
|
2017-12-29 18:27:33 +00:00
|
|
|
if (feedback > 0) {
|
|
|
|
wipe_statusbar();
|
2020-07-03 07:32:25 +00:00
|
|
|
lastmessage = VACUUM;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-07-25 13:08:32 +00:00
|
|
|
/* Ask for a string and then search forward for it. */
|
2017-09-17 20:07:25 +00:00
|
|
|
void do_search_forward(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
UNSET(BACKWARDS_SEARCH);
|
2020-07-25 13:08:32 +00:00
|
|
|
search_init(FALSE, FALSE);
|
2017-09-17 20:07:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 13:08:32 +00:00
|
|
|
/* Ask for a string and then search backwards for it. */
|
2017-09-17 20:07:25 +00:00
|
|
|
void do_search_backward(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
SET(BACKWARDS_SEARCH);
|
2020-07-25 13:08:32 +00:00
|
|
|
search_init(FALSE, FALSE);
|
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') {
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(AHEM, _("No current search pattern"));
|
2017-12-29 18:27:33 +00:00
|
|
|
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
|
|
|
|
2020-09-25 17:45:15 +00:00
|
|
|
if (LINES > 1)
|
|
|
|
wipe_statusbar();
|
2019-08-10 11:07:04 +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
|
|
|
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(AHEM, _("\"%.*s%s\" not found"), numchars, disp,
|
2018-04-13 19:09:44 +00:00
|
|
|
(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-03-20 16:57:15 +00:00
|
|
|
|
2020-05-29 16:45:14 +00:00
|
|
|
//#define TIMEIT 12
|
2019-06-09 16:48:52 +00:00
|
|
|
#ifdef TIMEIT
|
|
|
|
#include <time.h>
|
|
|
|
clock_t start = clock();
|
|
|
|
#endif
|
|
|
|
|
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)
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(REMARK, _("This is the only occurrence"));
|
2020-09-27 09:23:23 +00:00
|
|
|
else if (didfind == 1 && LINES == 1)
|
|
|
|
refresh_needed = TRUE;
|
2017-12-29 18:27:33 +00:00
|
|
|
else if (didfind == 0)
|
|
|
|
not_found_msg(last_search);
|
2003-08-23 21:11:06 +00:00
|
|
|
|
2019-06-09 16:48:52 +00:00
|
|
|
#ifdef TIMEIT
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(INFO, "Took: %.2f", (double)(clock() - start) / CLOCKS_PER_SEC);
|
2019-06-09 16:48:52 +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
|
|
|
{
|
2019-05-08 13:31:15 +00:00
|
|
|
size_t new_size = strlen(openfile->current->data) + 1;
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t match_len;
|
2019-05-08 13:31:15 +00:00
|
|
|
char *copy;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* 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;
|
2019-05-08 13:31:15 +00:00
|
|
|
new_size += replace_regexp(NULL, FALSE) - match_len;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else {
|
|
|
|
match_len = strlen(needle);
|
2019-05-08 13:31:15 +00:00
|
|
|
new_size += strlen(answer) - match_len;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2000-10-26 01:44:42 +00:00
|
|
|
|
2020-08-30 02:57:45 +00:00
|
|
|
copy = nmalloc(new_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
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2020-06-02 17:14:36 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
/* Reset the multiline coloring info and then recalculate it. */
|
|
|
|
void wipe_and_recalculate_colorinfo(void)
|
|
|
|
{
|
|
|
|
for (linestruct *line = openfile->filetop; line != NULL; line = line->next)
|
2020-08-17 08:05:25 +00:00
|
|
|
if (line->multidata)
|
|
|
|
for (short index = 0; index < openfile->syntax->nmultis; index++)
|
|
|
|
line->multidata[index] = -1;
|
2020-06-02 17:14:36 +00:00
|
|
|
|
|
|
|
precalc_multicolorinfo();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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;
|
2020-03-29 11:14:17 +00:00
|
|
|
bool right_side_up = (openfile->mark && mark_is_before_cursor());
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If the mark is on, frame the region, and turn the mark off. */
|
|
|
|
if (openfile->mark) {
|
2020-03-29 18:17:32 +00:00
|
|
|
get_region(&top, &top_x, &bot, &bot_x);
|
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
|
2019-10-09 17:12:07 +00:00
|
|
|
add_undo(REPLACE, NULL);
|
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
|
|
|
|
2020-06-06 16:57:17 +00:00
|
|
|
#ifndef NANO_TINY
|
|
|
|
if (ISSET(SOFTWRAP))
|
|
|
|
openfile->current->extrarows = extra_chunks_in(openfile->current);
|
|
|
|
#endif
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2020-06-02 17:14:36 +00:00
|
|
|
/* Check whether the replacement requires a change in the coloring. */
|
2020-06-02 08:50:18 +00:00
|
|
|
check_the_multis(openfile->current);
|
|
|
|
|
2020-06-02 17:14:36 +00:00
|
|
|
if (refresh_needed && !replaceall)
|
|
|
|
wipe_and_recalculate_colorinfo();
|
2020-06-02 08:50:18 +00:00
|
|
|
#endif
|
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);
|
2020-06-02 17:14:36 +00:00
|
|
|
|
2017-11-01 18:45:33 +00:00
|
|
|
#ifdef ENABLE_COLOR
|
2020-06-02 17:14:36 +00:00
|
|
|
if (refresh_needed)
|
|
|
|
wipe_and_recalculate_colorinfo();
|
2017-02-13 18:11:04 +00:00
|
|
|
#endif
|
2020-06-02 17:14:36 +00:00
|
|
|
|
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
|
|
|
|
2020-04-07 17:19:26 +00:00
|
|
|
/* Ask the user what to replace the search string with, and do the replacements. */
|
|
|
|
void ask_for_and_do_replacements(void)
|
2018-02-04 14:27:03 +00:00
|
|
|
{
|
2020-04-07 17:19:26 +00:00
|
|
|
linestruct *was_edittop = openfile->edittop;
|
|
|
|
size_t was_firstcolumn = openfile->firstcolumn;
|
|
|
|
linestruct *beginline = openfile->current;
|
|
|
|
size_t begin_x = openfile->current_x;
|
2018-02-04 14:27:03 +00:00
|
|
|
ssize_t numreplaced;
|
2020-06-20 15:15:40 +00:00
|
|
|
int response = do_prompt(MREPLACEWITH, "", &replace_history,
|
2019-05-08 17:32:30 +00:00
|
|
|
/* TRANSLATORS: This is a prompt. */
|
2020-06-20 15:15:40 +00:00
|
|
|
edit_refresh, _("Replace with"));
|
2003-01-05 21:47:06 +00:00
|
|
|
|
2017-10-29 18:42:12 +00:00
|
|
|
#ifdef ENABLE_HISTORIES
|
2020-04-07 17:19:26 +00:00
|
|
|
/* When not "", add the replace string to the replace history list. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response == 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
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. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response == -1) {
|
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
return;
|
|
|
|
} else if (response > 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
return;
|
2004-02-24 20:41:39 +00:00
|
|
|
|
2020-04-07 17:19:26 +00:00
|
|
|
numreplaced = do_replace_loop(last_search, FALSE, beginline, &begin_x);
|
2000-11-05 16:52:21 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Restore where we were. */
|
2020-04-07 17:19:26 +00:00
|
|
|
openfile->edittop = was_edittop;
|
|
|
|
openfile->firstcolumn = was_firstcolumn;
|
|
|
|
openfile->current = beginline;
|
2017-12-29 18:27:33 +00:00
|
|
|
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)
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(REMARK, P_("Replaced %zd occurrence",
|
2017-12-29 18:27:33 +00:00
|
|
|
"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. */
|
2020-07-26 09:49:42 +00:00
|
|
|
void do_gotolinecolumn(ssize_t line, ssize_t column, bool retain_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. */
|
2020-07-26 09:49:42 +00:00
|
|
|
int response = do_prompt(MGOTOLINE, retain_answer ? answer : "", NULL,
|
2019-05-08 17:32:30 +00:00
|
|
|
/* TRANSLATORS: This is a prompt. */
|
2020-06-20 15:15:40 +00:00
|
|
|
edit_refresh, _("Enter line number, column number"));
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* If the user cancelled or gave a blank answer, get out. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response < 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
statusbar(_("Cancelled"));
|
|
|
|
return;
|
|
|
|
}
|
2005-07-16 07:06:36 +00:00
|
|
|
|
2019-05-08 17:17:16 +00:00
|
|
|
if (func_from_key(&response) == flip_goto) {
|
2018-04-24 11:11:41 +00:00
|
|
|
UNSET(BACKWARDS_SEARCH);
|
2020-07-26 09:49:42 +00:00
|
|
|
/* Switch to searching but retain what the user typed so far. */
|
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. */
|
2019-05-08 17:17:16 +00:00
|
|
|
if (response > 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Try to extract one or two numbers from the user's response. */
|
|
|
|
if (!parse_line_column(answer, &line, &column)) {
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(AHEM, _("Invalid line or column number"));
|
2017-12-29 18:27:33 +00:00
|
|
|
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,
|
2019-06-09 08:41:14 +00:00
|
|
|
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
|
|
|
if (reverse) {
|
2020-06-25 08:03:56 +00:00
|
|
|
/* First step away from the current bracket. */
|
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
|
2019-06-09 17:37:56 +00:00
|
|
|
pointer = line->data + step_left(line->data, openfile->current_x);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2020-06-25 08:03:56 +00:00
|
|
|
/* Now seek for any of the two brackets we are interested in. */
|
2020-06-25 07:53:39 +00:00
|
|
|
while (!(found = mbrevstrpbrk(line->data, bracket_pair, pointer))) {
|
2019-04-06 18:29:04 +00:00
|
|
|
line = line->prev;
|
2020-06-25 07:53:39 +00:00
|
|
|
if (line == NULL)
|
|
|
|
return FALSE;
|
|
|
|
pointer = line->data + strlen(line->data);
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-25 08:03:56 +00:00
|
|
|
pointer = line->data + step_right(line->data, openfile->current_x);
|
|
|
|
|
2020-06-25 07:53:39 +00:00
|
|
|
while (!(found = mbstrpbrk(pointer, bracket_pair))) {
|
2019-04-06 18:29:04 +00:00
|
|
|
line = line->next;
|
2020-06-25 07:53:39 +00:00
|
|
|
if (line == NULL)
|
|
|
|
return FALSE;
|
|
|
|
pointer = 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) {
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(AHEM, _("Not a bracket"));
|
2017-12-29 18:27:33 +00:00
|
|
|
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-06-09 15:36:29 +00:00
|
|
|
halfway += char_length(matchbrackets + halfway);
|
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)
|
2019-06-09 17:37:56 +00:00
|
|
|
wanted_ch = matchbrackets + step_left(matchbrackets,
|
2019-06-09 08:41:14 +00:00
|
|
|
wanted_ch - matchbrackets);
|
2017-12-29 18:27:33 +00:00
|
|
|
else
|
2019-06-09 17:22:24 +00:00
|
|
|
wanted_ch += char_length(wanted_ch);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 15:36:29 +00:00
|
|
|
ch_len = char_length(ch);
|
|
|
|
wanted_ch_len = char_length(wanted_ch);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
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
|
|
|
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(AHEM, _("No matching bracket"));
|
2019-04-27 15:17:06 +00:00
|
|
|
|
|
|
|
/* Restore the cursor position. */
|
|
|
|
openfile->current = was_current;
|
|
|
|
openfile->current_x = was_current_x;
|
2001-09-22 22:14:25 +00:00
|
|
|
}
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
|
2020-04-06 15:42:29 +00:00
|
|
|
/* Place an anchor at the current line when none exists, otherwise remove it. */
|
|
|
|
void put_or_lift_anchor(void)
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
{
|
2020-04-06 15:42:29 +00:00
|
|
|
openfile->current->has_anchor = !openfile->current->has_anchor;
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
|
2020-04-13 09:48:36 +00:00
|
|
|
update_line(openfile->current, openfile->current_x);
|
|
|
|
|
2020-04-06 15:42:29 +00:00
|
|
|
if (openfile->current->has_anchor)
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(REMARK, _("Placed anchor"));
|
2020-04-06 15:42:29 +00:00
|
|
|
else
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(REMARK, _("Removed anchor"));
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 09:34:19 +00:00
|
|
|
/* Make the given line the current line, or report the anchoredness. */
|
|
|
|
void go_to_and_confirm(linestruct *line)
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
{
|
|
|
|
linestruct *was_current = openfile->current;
|
|
|
|
|
2020-04-13 09:34:19 +00:00
|
|
|
if (line != openfile->current) {
|
|
|
|
openfile->current = line;
|
|
|
|
openfile->current_x = 0;
|
|
|
|
edit_redraw(was_current, CENTERING);
|
|
|
|
statusbar(_("Jumped to anchor"));
|
|
|
|
} else if (openfile->current->has_anchor)
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(REMARK, _("This is the only anchor"));
|
2020-04-13 09:34:19 +00:00
|
|
|
else
|
2020-12-08 16:09:07 +00:00
|
|
|
statusline(AHEM, _("There are no anchors"));
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 09:34:19 +00:00
|
|
|
/* Jump to the first anchor before the current line; wrap around at the top. */
|
2020-04-06 15:42:29 +00:00
|
|
|
void to_prev_anchor(void)
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
{
|
2020-04-13 09:34:19 +00:00
|
|
|
linestruct *line = openfile->current;
|
|
|
|
|
|
|
|
do { line = (line->prev) ? line->prev : openfile->filebot;
|
|
|
|
} while (!line->has_anchor && line != openfile->current);
|
|
|
|
|
|
|
|
go_to_and_confirm(line);
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 09:34:19 +00:00
|
|
|
/* Jump to the first anchor after the current line; wrap around at the bottom. */
|
2020-04-06 15:42:29 +00:00
|
|
|
void to_next_anchor(void)
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
{
|
2020-04-13 09:34:19 +00:00
|
|
|
linestruct *line = openfile->current;
|
|
|
|
|
|
|
|
do { line = (line->next) ? line->next : openfile->filetop;
|
|
|
|
} while (!line->has_anchor && line != openfile->current);
|
|
|
|
|
|
|
|
go_to_and_confirm(line);
|
new feature: bindable functions for toggling and jumping to "bookmarks"
With the 'bookmark' function, the user can place a bookmark on any
line in the buffer. Multiple lines can be bookmarked in this way.
With 'prevbookmark' and 'nextbookmark', the user can then easily
return to the bookmarked lines. The search for a bookmark wraps
around, as if start and end of buffer are connected.
[However, in this implementation, when a bookmarked line is deleted,
the bookmark is deleted too. This is undesirable. Also, when such
a deleted line is pasted elsewhere, the bookmark reappears with it,
and when pasted multiple times, the bookmark will be there as many
times. This is thoroughly undesirable. These behaviors will be
changed in a later commit.]
A bookmark is not yet visible in any way.
This fulfills https://savannah.gnu.org/bugs/?57577.
Requested-by: Ken Tyler <kent@werple.net.au>
Signed-off-by: Marco Diego Aurélio Mesquita <marcodiegomesquita@gmail.com>
Signed-off-by: Benno Schulenberg <bensberg@telfort.nl>
2018-11-12 20:30:47 +00:00
|
|
|
}
|
2014-06-19 20:05:24 +00:00
|
|
|
#endif /* !NANO_TINY */
|