Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1 | /************************************************************************** |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 2 | * search.c -- This file is part of GNU nano. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 3 | * * |
Chris Allegretta | 8a07a96 | 2009-12-02 03:36:22 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * |
Benno Schulenberg | 7a9f4a4 | 2014-04-30 20:18:26 +0000 | [diff] [blame] | 5 | * 2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc. * |
Benno Schulenberg | c118397 | 2017-02-21 20:27:49 +0100 | [diff] [blame] | 6 | * Copyright (C) 2015, 2016, 2017 Benno Schulenberg * |
Benno Schulenberg | 406e524 | 2016-08-29 15:14:18 +0200 | [diff] [blame] | 7 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 8 | * GNU nano is free software: you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published * |
| 10 | * by the Free Software Foundation, either version 3 of the License, * |
| 11 | * or (at your option) any later version. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 12 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 13 | * GNU nano is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty * |
| 15 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 16 | * See the GNU General Public License for more details. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 19 | * along with this program. If not, see http://www.gnu.org/licenses/. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 20 | * * |
| 21 | **************************************************************************/ |
| 22 | |
David Lawrence Ramsey | 034b994 | 2005-12-08 02:47:10 +0000 | [diff] [blame] | 23 | #include "proto.h" |
Chris Allegretta | 6efda54 | 2001-04-28 18:03:52 +0000 | [diff] [blame] | 24 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <stdio.h> |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 27 | #include <unistd.h> |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 29 | #include <errno.h> |
Chris Allegretta | 0dc26dc | 2009-01-24 22:40:41 +0000 | [diff] [blame] | 30 | #include <time.h> |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 31 | |
Benno Schulenberg | ae8df00 | 2016-05-01 12:59:32 +0200 | [diff] [blame] | 32 | static bool came_full_circle = FALSE; |
| 33 | /* Have we reached the starting line again while searching? */ |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 34 | #ifndef DISABLE_HISTORIES |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 35 | static bool history_changed = FALSE; |
| 36 | /* Have any of the history lists changed? */ |
| 37 | #endif |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 38 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 27fbc69 | 2004-10-19 21:09:37 +0000 | [diff] [blame] | 39 | static bool regexp_compiled = FALSE; |
| 40 | /* Have we compiled any regular expressions? */ |
David Lawrence Ramsey | 4b741b9 | 2004-04-30 19:40:03 +0000 | [diff] [blame] | 41 | |
Benno Schulenberg | 0b0ddb1 | 2017-01-02 20:25:24 +0100 | [diff] [blame] | 42 | /* Compile the given regular expression and store it in search_regexp. |
| 43 | * Return TRUE if the expression is valid, and FALSE otherwise. */ |
David Lawrence Ramsey | 61848f1 | 2007-01-09 23:40:24 +0000 | [diff] [blame] | 44 | bool regexp_init(const char *regexp) |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 45 | { |
Benno Schulenberg | 0b0ddb1 | 2017-01-02 20:25:24 +0100 | [diff] [blame] | 46 | int value = regcomp(&search_regexp, fixbounds(regexp), |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 47 | NANO_REG_EXTENDED | (ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE)); |
Chris Allegretta | 5d71514 | 2003-01-29 04:18:37 +0000 | [diff] [blame] | 48 | |
Benno Schulenberg | 0b0ddb1 | 2017-01-02 20:25:24 +0100 | [diff] [blame] | 49 | /* If regex compilation failed, show the error message. */ |
| 50 | if (value != 0) { |
| 51 | size_t len = regerror(value, &search_regexp, NULL, 0); |
David Lawrence Ramsey | 4b741b9 | 2004-04-30 19:40:03 +0000 | [diff] [blame] | 52 | char *str = charalloc(len); |
| 53 | |
Benno Schulenberg | 0b0ddb1 | 2017-01-02 20:25:24 +0100 | [diff] [blame] | 54 | regerror(value, &search_regexp, str, len); |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 55 | statusline(ALERT, _("Bad regex \"%s\": %s"), regexp, str); |
David Lawrence Ramsey | 4b741b9 | 2004-04-30 19:40:03 +0000 | [diff] [blame] | 56 | free(str); |
David Lawrence Ramsey | 0ec34ac | 2007-01-09 23:35:02 +0000 | [diff] [blame] | 57 | |
| 58 | return FALSE; |
David Lawrence Ramsey | 4b741b9 | 2004-04-30 19:40:03 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | regexp_compiled = TRUE; |
David Lawrence Ramsey | 0ec34ac | 2007-01-09 23:35:02 +0000 | [diff] [blame] | 62 | |
| 63 | return TRUE; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 64 | } |
| 65 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 66 | /* Decompile the compiled regular expression we used in the last |
| 67 | * search, if any. */ |
Chris Allegretta | e316773 | 2001-03-18 16:59:34 +0000 | [diff] [blame] | 68 | void regexp_cleanup(void) |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 69 | { |
David Lawrence Ramsey | 4b741b9 | 2004-04-30 19:40:03 +0000 | [diff] [blame] | 70 | if (regexp_compiled) { |
| 71 | regexp_compiled = FALSE; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 72 | regfree(&search_regexp); |
| 73 | } |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 74 | } |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 75 | #endif |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 76 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 77 | /* Indicate on the statusbar that the string at str was not found by the |
| 78 | * last search. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 79 | void not_found_msg(const char *str) |
| 80 | { |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 81 | char *disp; |
| 82 | int numchars; |
David Lawrence Ramsey | b94d51a | 2007-04-19 03:15:04 +0000 | [diff] [blame] | 83 | |
David Lawrence Ramsey | a3370c4 | 2004-04-05 01:08:14 +0000 | [diff] [blame] | 84 | assert(str != NULL); |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 85 | |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 86 | disp = display_string(str, 0, (COLS / 2) + 1, FALSE); |
David Lawrence Ramsey | 65658ef | 2005-01-16 20:05:36 +0000 | [diff] [blame] | 87 | numchars = actual_x(disp, mbstrnlen(disp, COLS / 2)); |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 88 | |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 89 | statusline(HUSH, _("\"%.*s%s\" not found"), numchars, disp, |
Benno Schulenberg | e753cd1 | 2016-03-17 19:30:29 +0000 | [diff] [blame] | 90 | (disp[numchars] == '\0') ? "" : "..."); |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 91 | |
| 92 | free(disp); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 95 | /* Abort the current search or replace. Clean up by displaying the main |
| 96 | * shortcut list, updating the screen if the mark was on before, and |
| 97 | * decompiling the compiled regular expression we used in the last |
| 98 | * search, if any. */ |
| 99 | void search_replace_abort(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 100 | { |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 101 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 102 | if (openfile->mark_set) |
Benno Schulenberg | f920e0d | 2016-12-03 17:00:28 +0100 | [diff] [blame] | 103 | refresh_needed = TRUE; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 104 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 105 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 106 | regexp_cleanup(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 107 | #endif |
| 108 | } |
| 109 | |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 110 | /* Set up the system variables for a search or replace. If use_answer |
David Lawrence Ramsey | 20323a8 | 2006-03-23 22:14:40 +0000 | [diff] [blame] | 111 | * is TRUE, only set backupstring to answer. Return -2 to run the |
| 112 | * opposite program (search -> replace, replace -> search), return -1 if |
| 113 | * the search should be canceled (due to Cancel, a blank search string, |
| 114 | * Go to Line, or a failed regcomp()), return 0 on success, and return 1 |
| 115 | * on rerun calling program. |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 116 | * |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 117 | * replacing is TRUE if we call from do_replace(), and FALSE if called |
| 118 | * from do_search(). */ |
| 119 | int search_init(bool replacing, bool use_answer) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 120 | { |
Chris Allegretta | f372bd9 | 2001-07-15 22:24:24 +0000 | [diff] [blame] | 121 | int i = 0; |
Chris Allegretta | 71844ba | 2000-11-03 14:23:00 +0000 | [diff] [blame] | 122 | char *buf; |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 123 | static char *backupstring = NULL; |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 124 | /* The search string we'll be using. */ |
Benno Schulenberg | 9f93b33 | 2016-03-19 18:51:46 +0000 | [diff] [blame] | 125 | functionptrtype func; |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 126 | |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 127 | /* If use_answer is TRUE, set backupstring to answer and get out. */ |
| 128 | if (use_answer) { |
| 129 | backupstring = mallocstrcpy(backupstring, answer); |
| 130 | return 0; |
| 131 | } |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 132 | |
| 133 | /* We display the search prompt below. If the user types a partial |
| 134 | * search string and then Replace or a toggle, we will return to |
| 135 | * do_search() or do_replace() and be called again. In that case, |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 136 | * we should put the same search string back up. */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 137 | |
Benno Schulenberg | 1d3d307 | 2016-05-27 21:31:55 +0200 | [diff] [blame] | 138 | if (*last_search != '\0') { |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 139 | char *disp = display_string(last_search, 0, COLS / 3, FALSE); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 140 | |
David Lawrence Ramsey | 08cd7ef | 2005-01-02 20:30:15 +0000 | [diff] [blame] | 141 | buf = charalloc(strlen(disp) + 7); |
Benno Schulenberg | cb776fa | 2015-03-21 21:40:56 +0000 | [diff] [blame] | 142 | /* We use (COLS / 3) here because we need to see more on the line. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 143 | sprintf(buf, " [%s%s]", disp, |
David Lawrence Ramsey | 3d12f0f | 2005-10-26 23:14:59 +0000 | [diff] [blame] | 144 | (strlenpt(last_search) > COLS / 3) ? "..." : ""); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 145 | free(disp); |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 146 | } else |
| 147 | buf = mallocstrcpy(NULL, ""); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 148 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 149 | /* This is now one simple call. It just does a lot. */ |
Benno Schulenberg | fd0589d | 2017-01-02 21:12:44 +0100 | [diff] [blame] | 150 | i = do_prompt(FALSE, FALSE, |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 151 | replacing ? MREPLACE : MWHEREIS, backupstring, |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 152 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 153 | &search_history, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 154 | #endif |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 155 | /* TRANSLATORS: This is the main search prompt. */ |
| 156 | edit_refresh, "%s%s%s%s%s%s", _("Search"), |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 157 | /* TRANSLATORS: The next three modify the search prompt. */ |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 158 | ISSET(CASE_SENSITIVE) ? _(" [Case Sensitive]") : "", |
David Lawrence Ramsey | 4b741b9 | 2004-04-30 19:40:03 +0000 | [diff] [blame] | 159 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 160 | ISSET(USE_REGEXP) ? _(" [Regexp]") : |
David Lawrence Ramsey | 4b741b9 | 2004-04-30 19:40:03 +0000 | [diff] [blame] | 161 | #endif |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 162 | "", |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 163 | ISSET(BACKWARDS_SEARCH) ? _(" [Backwards]") : "", replacing ? |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 164 | #ifndef NANO_TINY |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 165 | /* TRANSLATORS: The next two modify the search prompt. */ |
| 166 | openfile->mark_set ? _(" (to replace) in selection") : |
David Lawrence Ramsey | e1ee22f | 2004-11-03 23:05:11 +0000 | [diff] [blame] | 167 | #endif |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 168 | _(" (to replace)") : "", buf); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 169 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 170 | /* Release buf now that we don't need it anymore. */ |
Chris Allegretta | 45329a1 | 2002-03-10 01:22:21 +0000 | [diff] [blame] | 171 | free(buf); |
| 172 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 173 | free(backupstring); |
| 174 | backupstring = NULL; |
| 175 | |
Benno Schulenberg | e291131 | 2016-03-19 18:23:37 +0000 | [diff] [blame] | 176 | /* If the search was cancelled, or we have a blank answer and |
| 177 | * nothing was searched for yet during this session, get out. */ |
| 178 | if (i == -1 || (i == -2 && *last_search == '\0')) { |
David Lawrence Ramsey | d132210 | 2004-08-26 04:22:54 +0000 | [diff] [blame] | 179 | statusbar(_("Cancelled")); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 180 | return -1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 181 | } |
David Lawrence Ramsey | e0b5513 | 2005-05-31 17:35:11 +0000 | [diff] [blame] | 182 | |
Benno Schulenberg | 9f93b33 | 2016-03-19 18:51:46 +0000 | [diff] [blame] | 183 | /* If Enter was pressed, see what we got. */ |
| 184 | if (i == 0 || i == -2) { |
| 185 | /* If an answer was given, remember it. */ |
| 186 | if (*answer != '\0') { |
| 187 | last_search = mallocstrcpy(last_search, answer); |
| 188 | #ifndef DISABLE_HISTORIES |
| 189 | update_history(&search_history, answer); |
| 190 | #endif |
| 191 | } |
| 192 | #ifdef HAVE_REGEX_H |
| 193 | if (ISSET(USE_REGEXP) && !regexp_init(last_search)) |
| 194 | return -1; |
| 195 | else |
| 196 | #endif |
| 197 | return 0; /* We have a valid string or regex. */ |
| 198 | } |
| 199 | |
| 200 | func = func_from_key(&i); |
| 201 | |
Benno Schulenberg | 9f93b33 | 2016-03-19 18:51:46 +0000 | [diff] [blame] | 202 | if (func == case_sens_void) { |
| 203 | TOGGLE(CASE_SENSITIVE); |
| 204 | backupstring = mallocstrcpy(backupstring, answer); |
| 205 | return 1; |
| 206 | } else if (func == backwards_void) { |
| 207 | TOGGLE(BACKWARDS_SEARCH); |
| 208 | backupstring = mallocstrcpy(backupstring, answer); |
| 209 | return 1; |
Benno Schulenberg | 2848771 | 2016-04-04 19:38:57 +0200 | [diff] [blame] | 210 | } else |
Benno Schulenberg | 9f93b33 | 2016-03-19 18:51:46 +0000 | [diff] [blame] | 211 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | 2848771 | 2016-04-04 19:38:57 +0200 | [diff] [blame] | 212 | if (func == regexp_void) { |
Benno Schulenberg | 9f93b33 | 2016-03-19 18:51:46 +0000 | [diff] [blame] | 213 | TOGGLE(USE_REGEXP); |
| 214 | backupstring = mallocstrcpy(backupstring, answer); |
| 215 | return 1; |
Benno Schulenberg | 2848771 | 2016-04-04 19:38:57 +0200 | [diff] [blame] | 216 | } else |
Benno Schulenberg | 9f93b33 | 2016-03-19 18:51:46 +0000 | [diff] [blame] | 217 | #endif |
Benno Schulenberg | 2848771 | 2016-04-04 19:38:57 +0200 | [diff] [blame] | 218 | if (func == do_replace || func == flip_replace_void) { |
Benno Schulenberg | 9f93b33 | 2016-03-19 18:51:46 +0000 | [diff] [blame] | 219 | backupstring = mallocstrcpy(backupstring, answer); |
| 220 | return -2; /* Call the opposite search function. */ |
| 221 | } else if (func == do_gotolinecolumn_void) { |
| 222 | do_gotolinecolumn(openfile->current->lineno, |
| 223 | openfile->placewewant + 1, TRUE, TRUE); |
| 224 | return 3; |
| 225 | } |
| 226 | |
| 227 | return -1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Benno Schulenberg | b454d9c | 2015-04-25 15:17:22 +0000 | [diff] [blame] | 230 | /* Look for needle, starting at (current, current_x). begin is the line |
Benno Schulenberg | 400e7ce | 2016-03-30 12:00:48 +0000 | [diff] [blame] | 231 | * where we first started searching, at column begin_x. Return 1 when we |
| 232 | * found something, 0 when nothing, and -2 on cancel. When match_len is |
| 233 | * not NULL, set it to the length of the found string, if any. */ |
Benno Schulenberg | 8f10e36 | 2017-02-10 13:51:51 +0100 | [diff] [blame] | 234 | int findnextstr(const char *needle, bool whole_word_only, bool have_region, |
| 235 | size_t *match_len, bool skipone, const filestruct *begin, size_t begin_x) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 236 | { |
Benno Schulenberg | e36e829 | 2017-01-05 21:25:30 +0100 | [diff] [blame] | 237 | size_t found_len = strlen(needle); |
| 238 | /* The length of a match -- will be recomputed for a regex. */ |
Benno Schulenberg | 108fe33 | 2016-03-28 19:00:19 +0000 | [diff] [blame] | 239 | int feedback = 0; |
| 240 | /* When bigger than zero, show and wipe the "Searching..." message. */ |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 241 | filestruct *line = openfile->current; |
Benno Schulenberg | c88d7ce | 2017-01-13 16:11:47 +0100 | [diff] [blame] | 242 | /* The line that we will search through now. */ |
| 243 | const char *from = line->data + openfile->current_x; |
| 244 | /* The point in the line from where we start searching. */ |
| 245 | const char *found = NULL; |
| 246 | /* A pointer to the location of the match, if any. */ |
Benno Schulenberg | 9f1a44d | 2016-05-01 12:35:47 +0200 | [diff] [blame] | 247 | size_t found_x; |
| 248 | /* The x coordinate of a found occurrence. */ |
Chris Allegretta | 0dc26dc | 2009-01-24 22:40:41 +0000 | [diff] [blame] | 249 | time_t lastkbcheck = time(NULL); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 250 | |
Chris Allegretta | 0dc26dc | 2009-01-24 22:40:41 +0000 | [diff] [blame] | 251 | enable_nodelay(); |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 252 | |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 253 | if (begin == NULL) |
| 254 | came_full_circle = FALSE; |
| 255 | |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 256 | /* Start searching through the lines, looking for the needle. */ |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 257 | while (TRUE) { |
Benno Schulenberg | 3660c62 | 2016-03-23 10:27:54 +0000 | [diff] [blame] | 258 | /* Glance at the keyboard once every second. */ |
| 259 | if (time(NULL) - lastkbcheck > 0) { |
Benno Schulenberg | 6c86ee1 | 2014-07-02 19:12:38 +0000 | [diff] [blame] | 260 | int input = parse_kbinput(edit); |
| 261 | |
Benno Schulenberg | 8f61511 | 2014-04-14 09:57:06 +0000 | [diff] [blame] | 262 | lastkbcheck = time(NULL); |
Benno Schulenberg | 6c86ee1 | 2014-07-02 19:12:38 +0000 | [diff] [blame] | 263 | |
Benno Schulenberg | 9cbe6a6 | 2016-05-05 22:12:24 +0200 | [diff] [blame] | 264 | /* Consume all waiting keystrokes until a Cancel. */ |
| 265 | while (input) { |
| 266 | if (func_from_key(&input) == do_cancel) { |
| 267 | statusbar(_("Cancelled")); |
| 268 | disable_nodelay(); |
| 269 | return -2; |
| 270 | } |
| 271 | input = parse_kbinput(NULL); |
Chris Allegretta | 0dc26dc | 2009-01-24 22:40:41 +0000 | [diff] [blame] | 272 | } |
Benno Schulenberg | 108fe33 | 2016-03-28 19:00:19 +0000 | [diff] [blame] | 273 | |
| 274 | if (++feedback > 0) |
Benno Schulenberg | 6bb3097 | 2016-06-24 09:40:31 +0200 | [diff] [blame] | 275 | /* TRANSLATORS: This is shown when searching takes |
| 276 | * more than half a second. */ |
Benno Schulenberg | 108fe33 | 2016-03-28 19:00:19 +0000 | [diff] [blame] | 277 | statusbar(_("Searching...")); |
Chris Allegretta | 0dc26dc | 2009-01-24 22:40:41 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 280 | /* Search for the needle in the current line. */ |
Benno Schulenberg | ef7a7c5 | 2017-01-24 22:37:37 +0100 | [diff] [blame] | 281 | if (!skipone) |
| 282 | found = strstrwrapper(line->data, needle, from); |
David Lawrence Ramsey | 45cfbec | 2003-11-28 16:04:24 +0000 | [diff] [blame] | 283 | |
Benno Schulenberg | c88d7ce | 2017-01-13 16:11:47 +0100 | [diff] [blame] | 284 | /* Ignore the initial match at the starting position: continue |
| 285 | * searching from the next character, or invalidate the match. */ |
Benno Schulenberg | 94e5632 | 2017-01-26 20:05:21 +0100 | [diff] [blame] | 286 | if (skipone || (!whole_word_only && !came_full_circle && |
| 287 | found == begin->data + begin_x)) { |
Benno Schulenberg | ef7a7c5 | 2017-01-24 22:37:37 +0100 | [diff] [blame] | 288 | skipone = FALSE; |
Benno Schulenberg | c88d7ce | 2017-01-13 16:11:47 +0100 | [diff] [blame] | 289 | if (ISSET(BACKWARDS_SEARCH) && from != line->data) { |
| 290 | from = line->data + move_mbleft(line->data, from - line->data); |
| 291 | continue; |
| 292 | } else if (!ISSET(BACKWARDS_SEARCH) && *from != '\0') { |
| 293 | from += move_mbright(from, 0); |
| 294 | continue; |
| 295 | } |
| 296 | found = NULL; |
| 297 | } |
| 298 | |
David Lawrence Ramsey | 53752e8 | 2004-10-18 22:19:22 +0000 | [diff] [blame] | 299 | if (found != NULL) { |
David Lawrence Ramsey | 53752e8 | 2004-10-18 22:19:22 +0000 | [diff] [blame] | 300 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | e36e829 | 2017-01-05 21:25:30 +0100 | [diff] [blame] | 301 | /* When doing a regex search, compute the length of the match. */ |
Benno Schulenberg | 64aa875 | 2017-01-26 16:24:18 +0100 | [diff] [blame] | 302 | if (ISSET(USE_REGEXP)) |
Benno Schulenberg | e36e829 | 2017-01-05 21:25:30 +0100 | [diff] [blame] | 303 | found_len = regmatches[0].rm_eo - regmatches[0].rm_so; |
David Lawrence Ramsey | 53752e8 | 2004-10-18 22:19:22 +0000 | [diff] [blame] | 304 | #endif |
David Lawrence Ramsey | cf9c34a | 2005-11-16 05:59:06 +0000 | [diff] [blame] | 305 | #ifndef DISABLE_SPELLER |
Benno Schulenberg | 4223b83 | 2017-01-06 10:56:39 +0100 | [diff] [blame] | 306 | /* When we're spell checking, a match should be a separate word; |
| 307 | * if it's not, continue looking in the rest of the line. */ |
| 308 | if (whole_word_only && !is_separate_word(found - line->data, |
| 309 | found_len, line->data)) { |
| 310 | from = found + move_mbright(found, 0); |
| 311 | continue; |
| 312 | } |
David Lawrence Ramsey | cf9c34a | 2005-11-16 05:59:06 +0000 | [diff] [blame] | 313 | #endif |
Benno Schulenberg | 4223b83 | 2017-01-06 10:56:39 +0100 | [diff] [blame] | 314 | /* The match is valid. */ |
| 315 | break; |
Chris Allegretta | e4933a3 | 2001-06-13 02:35:44 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 318 | /* If we're back at the beginning, then there is no needle. */ |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 319 | if (came_full_circle) { |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 320 | not_found_msg(needle); |
Benno Schulenberg | 8f61511 | 2014-04-14 09:57:06 +0000 | [diff] [blame] | 321 | disable_nodelay(); |
Benno Schulenberg | 400e7ce | 2016-03-30 12:00:48 +0000 | [diff] [blame] | 322 | return 0; |
Chris Allegretta | e4933a3 | 2001-06-13 02:35:44 +0000 | [diff] [blame] | 323 | } |
David Lawrence Ramsey | c510042 | 2004-08-27 20:28:34 +0000 | [diff] [blame] | 324 | |
David Lawrence Ramsey | e6958d5 | 2006-03-23 21:25:04 +0000 | [diff] [blame] | 325 | /* Move to the previous or next line in the file. */ |
Benno Schulenberg | 7ba356a | 2016-04-08 18:11:22 +0200 | [diff] [blame] | 326 | if (ISSET(BACKWARDS_SEARCH)) |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 327 | line = line->prev; |
Benno Schulenberg | 7ba356a | 2016-04-08 18:11:22 +0200 | [diff] [blame] | 328 | else |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 329 | line = line->next; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 330 | |
Benno Schulenberg | 8f10e36 | 2017-02-10 13:51:51 +0100 | [diff] [blame] | 331 | /* If we've reached the start or end of the buffer, wrap around; |
| 332 | * but stop when spell-checking or replacing in a region. */ |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 333 | if (line == NULL) { |
Benno Schulenberg | 8f10e36 | 2017-02-10 13:51:51 +0100 | [diff] [blame] | 334 | if (whole_word_only || have_region) { |
Benno Schulenberg | 3c8647e | 2016-05-01 11:18:20 +0200 | [diff] [blame] | 335 | disable_nodelay(); |
| 336 | return 0; |
| 337 | } |
Benno Schulenberg | 8f10e36 | 2017-02-10 13:51:51 +0100 | [diff] [blame] | 338 | |
Benno Schulenberg | 7ba356a | 2016-04-08 18:11:22 +0200 | [diff] [blame] | 339 | if (ISSET(BACKWARDS_SEARCH)) |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 340 | line = openfile->filebot; |
Benno Schulenberg | 7ba356a | 2016-04-08 18:11:22 +0200 | [diff] [blame] | 341 | else |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 342 | line = openfile->fileage; |
Benno Schulenberg | 3c8647e | 2016-05-01 11:18:20 +0200 | [diff] [blame] | 343 | |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 344 | statusbar(_("Search Wrapped")); |
Benno Schulenberg | 108fe33 | 2016-03-28 19:00:19 +0000 | [diff] [blame] | 345 | /* Delay the "Searching..." message for at least two seconds. */ |
| 346 | feedback = -2; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 349 | /* If we've reached the original starting line, take note. */ |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 350 | if (line == begin) |
Benno Schulenberg | ae8df00 | 2016-05-01 12:59:32 +0200 | [diff] [blame] | 351 | came_full_circle = TRUE; |
David Lawrence Ramsey | 77b284a | 2004-10-27 02:21:01 +0000 | [diff] [blame] | 352 | |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 353 | /* Set the starting x to the start or end of the line. */ |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 354 | from = line->data; |
David Lawrence Ramsey | f3ecffd | 2005-06-16 18:48:30 +0000 | [diff] [blame] | 355 | if (ISSET(BACKWARDS_SEARCH)) |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 356 | from += strlen(line->data); |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 359 | found_x = found - line->data; |
Benno Schulenberg | 9f1a44d | 2016-05-01 12:35:47 +0200 | [diff] [blame] | 360 | |
| 361 | /* Ensure that the found occurrence is not beyond the starting x. */ |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 362 | if (came_full_circle && ((!ISSET(BACKWARDS_SEARCH) && found_x > begin_x) || |
| 363 | (ISSET(BACKWARDS_SEARCH) && found_x < begin_x))) { |
Benno Schulenberg | 9f1a44d | 2016-05-01 12:35:47 +0200 | [diff] [blame] | 364 | not_found_msg(needle); |
| 365 | disable_nodelay(); |
| 366 | return 0; |
| 367 | } |
| 368 | |
Chris Allegretta | 0dc26dc | 2009-01-24 22:40:41 +0000 | [diff] [blame] | 369 | disable_nodelay(); |
Benno Schulenberg | 8704dde | 2016-03-23 09:52:34 +0000 | [diff] [blame] | 370 | |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 371 | /* Set the current position to point at what we found. */ |
Benno Schulenberg | 4a1302e | 2017-01-05 22:40:49 +0100 | [diff] [blame] | 372 | openfile->current = line; |
Benno Schulenberg | 9f1a44d | 2016-05-01 12:35:47 +0200 | [diff] [blame] | 373 | openfile->current_x = found_x; |
David Lawrence Ramsey | 9819ed0 | 2004-10-21 15:32:11 +0000 | [diff] [blame] | 374 | |
Benno Schulenberg | 4b2751c | 2016-03-23 10:19:01 +0000 | [diff] [blame] | 375 | /* When requested, pass back the length of the match. */ |
| 376 | if (match_len != NULL) |
| 377 | *match_len = found_len; |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 378 | |
Benno Schulenberg | 108fe33 | 2016-03-28 19:00:19 +0000 | [diff] [blame] | 379 | if (feedback > 0) |
| 380 | blank_statusbar(); |
| 381 | |
Benno Schulenberg | 400e7ce | 2016-03-30 12:00:48 +0000 | [diff] [blame] | 382 | return 1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Benno Schulenberg | f150894 | 2016-03-20 16:57:15 +0000 | [diff] [blame] | 385 | /* Ask what to search for and then go looking for it. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 386 | void do_search(void) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 387 | { |
Benno Schulenberg | f150894 | 2016-03-20 16:57:15 +0000 | [diff] [blame] | 388 | int i = search_init(FALSE, FALSE); |
David Lawrence Ramsey | ef0d5a7 | 2006-05-22 02:08:49 +0000 | [diff] [blame] | 389 | |
Benno Schulenberg | 988274b | 2016-03-17 10:06:15 +0000 | [diff] [blame] | 390 | if (i == -1) /* Cancelled, or some other exit reason. */ |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 391 | search_replace_abort(); |
Benno Schulenberg | 988274b | 2016-03-17 10:06:15 +0000 | [diff] [blame] | 392 | else if (i == -2) /* Do a replace instead. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 393 | do_replace(); |
Benno Schulenberg | 988274b | 2016-03-17 10:06:15 +0000 | [diff] [blame] | 394 | else if (i == 1) /* Toggled something. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 395 | do_search(); |
Chris Allegretta | 86f8b3a | 2000-10-31 05:28:33 +0000 | [diff] [blame] | 396 | |
Benno Schulenberg | f150894 | 2016-03-20 16:57:15 +0000 | [diff] [blame] | 397 | if (i == 0) |
| 398 | go_looking(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Benno Schulenberg | 4990f74 | 2015-07-26 09:23:24 +0000 | [diff] [blame] | 401 | #ifndef NANO_TINY |
| 402 | /* Search in the backward direction for the next occurrence. */ |
| 403 | void do_findprevious(void) |
| 404 | { |
| 405 | if ISSET(BACKWARDS_SEARCH) |
| 406 | do_research(); |
| 407 | else { |
| 408 | SET(BACKWARDS_SEARCH); |
| 409 | do_research(); |
| 410 | UNSET(BACKWARDS_SEARCH); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* Search in the forward direction for the next occurrence. */ |
| 415 | void do_findnext(void) |
| 416 | { |
| 417 | if ISSET(BACKWARDS_SEARCH) { |
| 418 | UNSET(BACKWARDS_SEARCH); |
| 419 | do_research(); |
| 420 | SET(BACKWARDS_SEARCH); |
| 421 | } else |
| 422 | do_research(); |
| 423 | } |
Benno Schulenberg | 7098dd0 | 2016-03-17 18:51:46 +0000 | [diff] [blame] | 424 | #endif /* !NANO_TINY */ |
Benno Schulenberg | 4990f74 | 2015-07-26 09:23:24 +0000 | [diff] [blame] | 425 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 426 | /* Search for the last string without prompting. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 427 | void do_research(void) |
David Lawrence Ramsey | e049706 | 2003-08-23 21:11:06 +0000 | [diff] [blame] | 428 | { |
Benno Schulenberg | a267699 | 2015-06-20 08:10:25 +0000 | [diff] [blame] | 429 | #ifndef DISABLE_HISTORIES |
| 430 | /* If nothing was searched for yet during this run of nano, but |
| 431 | * there is a search history, take the most recent item. */ |
Benno Schulenberg | 1d3d307 | 2016-05-27 21:31:55 +0200 | [diff] [blame] | 432 | if (*last_search == '\0' && searchbot->prev != NULL) |
Benno Schulenberg | a267699 | 2015-06-20 08:10:25 +0000 | [diff] [blame] | 433 | last_search = mallocstrcpy(last_search, searchbot->prev->data); |
| 434 | #endif |
| 435 | |
Benno Schulenberg | 1d3d307 | 2016-05-27 21:31:55 +0200 | [diff] [blame] | 436 | if (*last_search == '\0') { |
Benno Schulenberg | 65c7c81 | 2016-01-26 09:16:09 +0000 | [diff] [blame] | 437 | statusbar(_("No current search pattern")); |
Benno Schulenberg | b3b2fa8 | 2016-03-20 16:03:20 +0000 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | |
David Lawrence Ramsey | e049706 | 2003-08-23 21:11:06 +0000 | [diff] [blame] | 441 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | b3b2fa8 | 2016-03-20 16:03:20 +0000 | [diff] [blame] | 442 | if (ISSET(USE_REGEXP) && !regexp_init(last_search)) |
| 443 | return; |
David Lawrence Ramsey | e049706 | 2003-08-23 21:11:06 +0000 | [diff] [blame] | 444 | #endif |
| 445 | |
Benno Schulenberg | b3b2fa8 | 2016-03-20 16:03:20 +0000 | [diff] [blame] | 446 | /* Use the search-menu key bindings, to allow cancelling. */ |
| 447 | currmenu = MWHEREIS; |
Benno Schulenberg | 19bc63d | 2016-03-17 09:12:30 +0000 | [diff] [blame] | 448 | |
Benno Schulenberg | f150894 | 2016-03-20 16:57:15 +0000 | [diff] [blame] | 449 | go_looking(); |
| 450 | } |
Benno Schulenberg | f150894 | 2016-03-20 16:57:15 +0000 | [diff] [blame] | 451 | |
| 452 | /* Search for the global string 'last_search'. Inform the user when |
| 453 | * the string occurs only once. */ |
| 454 | void go_looking(void) |
| 455 | { |
| 456 | filestruct *was_current = openfile->current; |
| 457 | size_t was_current_x = openfile->current_x; |
Benno Schulenberg | 400e7ce | 2016-03-30 12:00:48 +0000 | [diff] [blame] | 458 | int didfind; |
Benno Schulenberg | ee57cbf | 2016-08-06 14:39:08 +0200 | [diff] [blame] | 459 | #ifdef DEBUG |
| 460 | clock_t start = clock(); |
| 461 | #endif |
Benno Schulenberg | f150894 | 2016-03-20 16:57:15 +0000 | [diff] [blame] | 462 | |
Benno Schulenberg | 05238f3 | 2016-05-01 13:38:54 +0200 | [diff] [blame] | 463 | came_full_circle = FALSE; |
Benno Schulenberg | ae8df00 | 2016-05-01 12:59:32 +0200 | [diff] [blame] | 464 | |
Benno Schulenberg | 8f10e36 | 2017-02-10 13:51:51 +0100 | [diff] [blame] | 465 | didfind = findnextstr(last_search, FALSE, FALSE, NULL, FALSE, |
Benno Schulenberg | 2cd8ca4 | 2016-10-23 17:59:26 +0200 | [diff] [blame] | 466 | openfile->current, openfile->current_x); |
David Lawrence Ramsey | e049706 | 2003-08-23 21:11:06 +0000 | [diff] [blame] | 467 | |
Benno Schulenberg | b3b2fa8 | 2016-03-20 16:03:20 +0000 | [diff] [blame] | 468 | /* If we found something, and we're back at the exact same spot |
| 469 | * where we started searching, then this is the only occurrence. */ |
Benno Schulenberg | 400e7ce | 2016-03-30 12:00:48 +0000 | [diff] [blame] | 470 | if (didfind == 1 && openfile->current == was_current && |
Benno Schulenberg | f150894 | 2016-03-20 16:57:15 +0000 | [diff] [blame] | 471 | openfile->current_x == was_current_x) |
Benno Schulenberg | b3b2fa8 | 2016-03-20 16:03:20 +0000 | [diff] [blame] | 472 | statusbar(_("This is the only occurrence")); |
David Lawrence Ramsey | e049706 | 2003-08-23 21:11:06 +0000 | [diff] [blame] | 473 | |
Benno Schulenberg | ee57cbf | 2016-08-06 14:39:08 +0200 | [diff] [blame] | 474 | #ifdef DEBUG |
| 475 | statusline(HUSH, "Took: %.2f", (double)(clock() - start) / CLOCKS_PER_SEC); |
| 476 | #endif |
| 477 | |
Benno Schulenberg | aa1ae0a | 2016-04-10 21:16:19 +0200 | [diff] [blame] | 478 | edit_redraw(was_current); |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 479 | search_replace_abort(); |
David Lawrence Ramsey | e049706 | 2003-08-23 21:11:06 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 482 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 483 | /* Calculate the size of the replacement text, taking possible |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 484 | * subexpressions \1 to \9 into account. Return the replacement |
| 485 | * text in the passed string only when create is TRUE. */ |
David Lawrence Ramsey | e3970f5 | 2005-05-26 03:47:24 +0000 | [diff] [blame] | 486 | int replace_regexp(char *string, bool create) |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 487 | { |
Benno Schulenberg | 5174323 | 2016-03-28 19:14:33 +0000 | [diff] [blame] | 488 | const char *c = answer; |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 489 | size_t replacement_size = 0; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 490 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 491 | /* Iterate through the replacement text to handle subexpression |
| 492 | * replacement using \1, \2, \3, etc. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 493 | while (*c != '\0') { |
David Lawrence Ramsey | f968a18 | 2006-07-13 13:27:16 +0000 | [diff] [blame] | 494 | int num = (*(c + 1) - '0'); |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 495 | |
Benno Schulenberg | e753cd1 | 2016-03-17 19:30:29 +0000 | [diff] [blame] | 496 | if (*c != '\\' || num < 1 || num > 9 || num > search_regexp.re_nsub) { |
David Lawrence Ramsey | e3970f5 | 2005-05-26 03:47:24 +0000 | [diff] [blame] | 497 | if (create) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 498 | *string++ = *c; |
| 499 | c++; |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 500 | replacement_size++; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 501 | } else { |
David Lawrence Ramsey | 16799ba | 2005-06-21 22:32:50 +0000 | [diff] [blame] | 502 | size_t i = regmatches[num].rm_eo - regmatches[num].rm_so; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 503 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 504 | /* Skip over the replacement expression. */ |
| 505 | c += 2; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 506 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 507 | /* But add the length of the subexpression to new_size. */ |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 508 | replacement_size += i; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 509 | |
David Lawrence Ramsey | e3970f5 | 2005-05-26 03:47:24 +0000 | [diff] [blame] | 510 | /* And if create is TRUE, append the result of the |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 511 | * subexpression match to the new line. */ |
David Lawrence Ramsey | e3970f5 | 2005-05-26 03:47:24 +0000 | [diff] [blame] | 512 | if (create) { |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 513 | strncpy(string, openfile->current->data + |
Benno Schulenberg | 64aa875 | 2017-01-26 16:24:18 +0100 | [diff] [blame] | 514 | regmatches[num].rm_so, i); |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 515 | string += i; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 518 | } |
| 519 | |
David Lawrence Ramsey | e3970f5 | 2005-05-26 03:47:24 +0000 | [diff] [blame] | 520 | if (create) |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 521 | *string = '\0'; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 522 | |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 523 | return replacement_size; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 524 | } |
Benno Schulenberg | e4c34c3 | 2014-03-17 14:15:57 +0000 | [diff] [blame] | 525 | #endif /* HAVE_REGEX_H */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 526 | |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 527 | /* Return a copy of the current line with one needle replaced. */ |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 528 | char *replace_line(const char *needle) |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 529 | { |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 530 | char *copy; |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 531 | size_t match_len; |
| 532 | size_t new_line_size = strlen(openfile->current->data) + 1; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 533 | |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 534 | /* First adjust the size of the new line for the change. */ |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 535 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 536 | if (ISSET(USE_REGEXP)) { |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 537 | match_len = regmatches[0].rm_eo - regmatches[0].rm_so; |
| 538 | new_line_size += replace_regexp(NULL, FALSE) - match_len; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 539 | } else { |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 540 | #endif |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 541 | match_len = strlen(needle); |
| 542 | new_line_size += strlen(answer) - match_len; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 543 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 544 | } |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 545 | #endif |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 546 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 547 | /* Create the buffer. */ |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 548 | copy = charalloc(new_line_size); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 549 | |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 550 | /* Copy the head of the original line. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 551 | strncpy(copy, openfile->current->data, openfile->current_x); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 552 | |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 553 | /* Add the replacement text. */ |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 554 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 555 | if (ISSET(USE_REGEXP)) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 556 | replace_regexp(copy + openfile->current_x, TRUE); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 557 | else |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 558 | #endif |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 559 | strcpy(copy + openfile->current_x, answer); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 560 | |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 561 | assert(openfile->current_x + match_len <= strlen(openfile->current->data)); |
David Lawrence Ramsey | 16f8813 | 2005-05-26 03:32:41 +0000 | [diff] [blame] | 562 | |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 563 | /* Copy the tail of the original line. */ |
Benno Schulenberg | c7f5691 | 2015-07-26 17:29:34 +0000 | [diff] [blame] | 564 | strcat(copy, openfile->current->data + openfile->current_x + match_len); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 565 | |
| 566 | return copy; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Benno Schulenberg | f311c0a | 2016-07-27 09:23:49 +0200 | [diff] [blame] | 569 | /* Step through each occurrence of the search string and prompt the user |
| 570 | * before replacing it. We seek for needle, and replace it with answer. |
| 571 | * The parameters real_current and real_current_x are needed in order to |
David Lawrence Ramsey | 491cad3 | 2004-10-08 23:06:01 +0000 | [diff] [blame] | 572 | * allow the cursor position to be updated when a word before the cursor |
Benno Schulenberg | f311c0a | 2016-07-27 09:23:49 +0200 | [diff] [blame] | 573 | * is replaced by a shorter word. Return -1 if needle isn't found, -2 if |
| 574 | * the seeking is aborted, else the number of replacements performed. */ |
Benno Schulenberg | 2cd8ca4 | 2016-10-23 17:59:26 +0200 | [diff] [blame] | 575 | ssize_t do_replace_loop(const char *needle, bool whole_word_only, |
| 576 | const filestruct *real_current, size_t *real_current_x) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 577 | { |
David Lawrence Ramsey | 53752e8 | 2004-10-18 22:19:22 +0000 | [diff] [blame] | 578 | ssize_t numreplaced = -1; |
| 579 | size_t match_len; |
David Lawrence Ramsey | c510042 | 2004-08-27 20:28:34 +0000 | [diff] [blame] | 580 | bool replaceall = FALSE; |
Benno Schulenberg | ef7a7c5 | 2017-01-24 22:37:37 +0100 | [diff] [blame] | 581 | bool skipone = FALSE; |
David Lawrence Ramsey | efdb543 | 2017-02-12 12:12:08 -0600 | [diff] [blame] | 582 | bool mark_was_set = FALSE; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 583 | #ifndef NANO_TINY |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 584 | filestruct *top, *bot; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 585 | size_t top_x, bot_x; |
| 586 | bool right_side_up = FALSE; |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 587 | /* TRUE if (mark_begin, mark_begin_x) is the top of the mark, |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 588 | * FALSE if (current, current_x) is. */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 589 | |
David Lawrence Ramsey | efdb543 | 2017-02-12 12:12:08 -0600 | [diff] [blame] | 590 | mark_was_set = openfile->mark_set; |
| 591 | |
Benno Schulenberg | f311c0a | 2016-07-27 09:23:49 +0200 | [diff] [blame] | 592 | /* If the mark is on, frame the region, and turn the mark off. */ |
Benno Schulenberg | 5c3d529 | 2017-02-10 14:07:42 +0100 | [diff] [blame] | 593 | if (mark_was_set) { |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 594 | mark_order((const filestruct **)&top, &top_x, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 595 | (const filestruct **)&bot, &bot_x, &right_side_up); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 596 | openfile->mark_set = FALSE; |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 597 | |
| 598 | /* Start either at the top or the bottom of the marked region. */ |
| 599 | if (!ISSET(BACKWARDS_SEARCH)) { |
| 600 | openfile->current = top; |
Benno Schulenberg | da56438 | 2017-01-26 21:36:13 +0100 | [diff] [blame] | 601 | openfile->current_x = top_x; |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 602 | } else { |
| 603 | openfile->current = bot; |
| 604 | openfile->current_x = bot_x; |
| 605 | } |
David Lawrence Ramsey | 491cad3 | 2004-10-08 23:06:01 +0000 | [diff] [blame] | 606 | } |
Benno Schulenberg | e4c34c3 | 2014-03-17 14:15:57 +0000 | [diff] [blame] | 607 | #endif /* !NANO_TINY */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 608 | |
Benno Schulenberg | 05238f3 | 2016-05-01 13:38:54 +0200 | [diff] [blame] | 609 | came_full_circle = FALSE; |
Benno Schulenberg | ae8df00 | 2016-05-01 12:59:32 +0200 | [diff] [blame] | 610 | |
Benno Schulenberg | 400e7ce | 2016-03-30 12:00:48 +0000 | [diff] [blame] | 611 | while (TRUE) { |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 612 | int i = 0; |
Benno Schulenberg | 5c3d529 | 2017-02-10 14:07:42 +0100 | [diff] [blame] | 613 | int result = findnextstr(needle, whole_word_only, mark_was_set, |
Benno Schulenberg | 8f10e36 | 2017-02-10 13:51:51 +0100 | [diff] [blame] | 614 | &match_len, skipone, real_current, *real_current_x); |
Benno Schulenberg | 400e7ce | 2016-03-30 12:00:48 +0000 | [diff] [blame] | 615 | |
| 616 | /* If nothing more was found, or the user aborted, stop looping. */ |
| 617 | if (result < 1) { |
| 618 | if (result < 0) |
| 619 | numreplaced = -2; /* It's a Cancel instead of Not found. */ |
| 620 | break; |
| 621 | } |
David Lawrence Ramsey | 3497666 | 2004-10-09 16:26:32 +0000 | [diff] [blame] | 622 | |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 623 | #ifndef NANO_TINY |
Benno Schulenberg | 5c3d529 | 2017-02-10 14:07:42 +0100 | [diff] [blame] | 624 | /* An occurrence outside of the marked region means we're done. */ |
| 625 | if (mark_was_set && (openfile->current->lineno > bot->lineno || |
| 626 | openfile->current->lineno < top->lineno || |
| 627 | (openfile->current == bot && |
| 628 | openfile->current_x + match_len > bot_x) || |
| 629 | (openfile->current == top && |
| 630 | openfile->current_x < top_x))) |
| 631 | break; |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 632 | #endif |
| 633 | |
David Lawrence Ramsey | 8b698f4 | 2005-06-13 05:16:55 +0000 | [diff] [blame] | 634 | /* Indicate that we found the search string. */ |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 635 | if (numreplaced == -1) |
| 636 | numreplaced = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 637 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 638 | if (!replaceall) { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 639 | size_t xpt = xplustabs(); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 640 | char *exp_word = display_string(openfile->current->data, |
Benno Schulenberg | e753cd1 | 2016-03-17 19:30:29 +0000 | [diff] [blame] | 641 | xpt, strnlenpt(openfile->current->data, |
| 642 | openfile->current_x + match_len) - xpt, FALSE); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 643 | |
Benno Schulenberg | 56cfab3 | 2016-02-18 16:31:02 +0000 | [diff] [blame] | 644 | /* Refresh the edit window, scrolling it if necessary. */ |
Benno Schulenberg | 0a79c78 | 2015-03-27 11:29:23 +0000 | [diff] [blame] | 645 | edit_refresh(); |
| 646 | |
Benno Schulenberg | 568d2a3 | 2016-02-13 19:41:12 +0000 | [diff] [blame] | 647 | /* Don't show cursor, to not distract from highlighted match. */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 648 | curs_set(0); |
David Lawrence Ramsey | da72453 | 2005-06-15 19:15:14 +0000 | [diff] [blame] | 649 | |
Benno Schulenberg | c98afde | 2016-03-30 12:09:39 +0000 | [diff] [blame] | 650 | spotlight(TRUE, exp_word); |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 651 | |
Benno Schulenberg | a2d74f7 | 2014-05-28 14:34:11 +0000 | [diff] [blame] | 652 | /* TRANSLATORS: This is a prompt. */ |
David Lawrence Ramsey | e19449e | 2005-11-07 21:45:44 +0000 | [diff] [blame] | 653 | i = do_yesno_prompt(TRUE, _("Replace this instance?")); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 654 | |
Benno Schulenberg | c98afde | 2016-03-30 12:09:39 +0000 | [diff] [blame] | 655 | spotlight(FALSE, exp_word); |
David Lawrence Ramsey | da72453 | 2005-06-15 19:15:14 +0000 | [diff] [blame] | 656 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 657 | free(exp_word); |
David Lawrence Ramsey | da72453 | 2005-06-15 19:15:14 +0000 | [diff] [blame] | 658 | |
Benno Schulenberg | b834ed3 | 2016-03-29 15:05:47 +0000 | [diff] [blame] | 659 | if (i == -1) /* The replacing was cancelled. */ |
David Lawrence Ramsey | afb75f2 | 2003-12-29 02:15:23 +0000 | [diff] [blame] | 660 | break; |
Benno Schulenberg | 4edc83c | 2016-12-22 17:08:10 +0100 | [diff] [blame] | 661 | else if (i == 2) |
| 662 | replaceall = TRUE; |
Benno Schulenberg | ef7a7c5 | 2017-01-24 22:37:37 +0100 | [diff] [blame] | 663 | |
| 664 | /* When "No" or moving backwards, the search routine should |
| 665 | * first move one character further before continuing. */ |
| 666 | skipone = (i == 0 || ISSET(BACKWARDS_SEARCH)); |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Benno Schulenberg | 4edc83c | 2016-12-22 17:08:10 +0100 | [diff] [blame] | 669 | if (i == 1 || replaceall) { /* Yes, replace it. */ |
David Lawrence Ramsey | 91d468d | 2005-07-18 19:29:27 +0000 | [diff] [blame] | 670 | char *copy; |
David Lawrence Ramsey | 687776b | 2004-10-30 01:16:08 +0000 | [diff] [blame] | 671 | size_t length_change; |
Chris Allegretta | 1939c35 | 2003-01-26 04:26:25 +0000 | [diff] [blame] | 672 | |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 673 | #ifndef NANO_TINY |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 674 | add_undo(REPLACE); |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 675 | #endif |
David Lawrence Ramsey | 91d468d | 2005-07-18 19:29:27 +0000 | [diff] [blame] | 676 | copy = replace_line(needle); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 677 | |
Benno Schulenberg | ad827a6 | 2015-03-27 10:49:19 +0000 | [diff] [blame] | 678 | length_change = strlen(copy) - strlen(openfile->current->data); |
Chris Allegretta | 1939c35 | 2003-01-26 04:26:25 +0000 | [diff] [blame] | 679 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 680 | #ifndef NANO_TINY |
Benno Schulenberg | a1c03ed | 2015-11-07 09:49:34 +0000 | [diff] [blame] | 681 | /* If the mark was on and it was located after the cursor, |
| 682 | * then adjust its x position for any text length changes. */ |
Benno Schulenberg | 5c3d529 | 2017-02-10 14:07:42 +0100 | [diff] [blame] | 683 | if (mark_was_set && !right_side_up) { |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 684 | if (openfile->current == openfile->mark_begin && |
| 685 | openfile->mark_begin_x > openfile->current_x) { |
Benno Schulenberg | a1c03ed | 2015-11-07 09:49:34 +0000 | [diff] [blame] | 686 | if (openfile->mark_begin_x < openfile->current_x + match_len) |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 687 | openfile->mark_begin_x = openfile->current_x; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 688 | else |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 689 | openfile->mark_begin_x += length_change; |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 690 | bot_x = openfile->mark_begin_x; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 691 | } |
Chris Allegretta | 1939c35 | 2003-01-26 04:26:25 +0000 | [diff] [blame] | 692 | } |
Chris Allegretta | 1939c35 | 2003-01-26 04:26:25 +0000 | [diff] [blame] | 693 | |
Benno Schulenberg | a1c03ed | 2015-11-07 09:49:34 +0000 | [diff] [blame] | 694 | /* If the mark was not on or it was before the cursor, then |
| 695 | * adjust the cursor's x position for any text length changes. */ |
Benno Schulenberg | 5c3d529 | 2017-02-10 14:07:42 +0100 | [diff] [blame] | 696 | if (!mark_was_set || right_side_up) { |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 697 | #endif |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 698 | if (openfile->current == real_current && |
Benno Schulenberg | 3534d8f | 2017-01-24 16:42:42 +0100 | [diff] [blame] | 699 | openfile->current_x < *real_current_x) { |
Benno Schulenberg | ad827a6 | 2015-03-27 10:49:19 +0000 | [diff] [blame] | 700 | if (*real_current_x < openfile->current_x + match_len) |
| 701 | *real_current_x = openfile->current_x + match_len; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 702 | *real_current_x += length_change; |
Benno Schulenberg | fd759d5 | 2015-04-13 10:59:12 +0000 | [diff] [blame] | 703 | #ifndef NANO_TINY |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 704 | bot_x = *real_current_x; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 705 | } |
David Lawrence Ramsey | dcc201b | 2004-11-05 14:37:18 +0000 | [diff] [blame] | 706 | #endif |
Benno Schulenberg | fd759d5 | 2015-04-13 10:59:12 +0000 | [diff] [blame] | 707 | } |
Chris Allegretta | 1939c35 | 2003-01-26 04:26:25 +0000 | [diff] [blame] | 708 | |
Benno Schulenberg | 8ffc33c | 2015-07-26 08:20:28 +0000 | [diff] [blame] | 709 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | c88fae3 | 2016-07-27 09:04:06 +0200 | [diff] [blame] | 710 | /* Don't find the same zero-length or BOL match again. */ |
| 711 | if (match_len == 0 || (*needle == '^' && ISSET(USE_REGEXP))) |
Benno Schulenberg | ef7a7c5 | 2017-01-24 22:37:37 +0100 | [diff] [blame] | 712 | skipone = TRUE; |
Benno Schulenberg | 8ffc33c | 2015-07-26 08:20:28 +0000 | [diff] [blame] | 713 | #endif |
Benno Schulenberg | ef7a7c5 | 2017-01-24 22:37:37 +0100 | [diff] [blame] | 714 | /* When moving forward, put the cursor just after the replacement |
| 715 | * text, so that searching will continue there. */ |
David Lawrence Ramsey | f3ecffd | 2005-06-16 18:48:30 +0000 | [diff] [blame] | 716 | if (!ISSET(BACKWARDS_SEARCH)) |
Benno Schulenberg | ef7a7c5 | 2017-01-24 22:37:37 +0100 | [diff] [blame] | 717 | openfile->current_x += match_len + length_change; |
Chris Allegretta | 1939c35 | 2003-01-26 04:26:25 +0000 | [diff] [blame] | 718 | |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 719 | /* Update the file size, and put the changed line into place. */ |
| 720 | openfile->totsize += mbstrlen(copy) - mbstrlen(openfile->current->data); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 721 | free(openfile->current->data); |
| 722 | openfile->current->data = copy; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 723 | |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 724 | if (!replaceall) { |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 725 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 0f3e303 | 2016-12-02 17:37:11 +0100 | [diff] [blame] | 726 | /* When doing syntax coloring, the replacement might require |
| 727 | * a change of colors, so refresh the whole edit window. */ |
Benno Schulenberg | 65c7c81 | 2016-01-26 09:16:09 +0000 | [diff] [blame] | 728 | if (openfile->colorstrings != NULL && !ISSET(NO_COLOR_SYNTAX)) |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 729 | edit_refresh(); |
| 730 | else |
| 731 | #endif |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 732 | update_line(openfile->current, openfile->current_x); |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 735 | set_modified(); |
Benno Schulenberg | eef7d10 | 2016-12-20 19:27:41 +0100 | [diff] [blame] | 736 | as_an_at = TRUE; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 737 | numreplaced++; |
David Lawrence Ramsey | afb75f2 | 2003-12-29 02:15:23 +0000 | [diff] [blame] | 738 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 741 | if (numreplaced == -1) |
| 742 | not_found_msg(needle); |
Benno Schulenberg | 9ec546d | 2017-02-13 19:11:04 +0100 | [diff] [blame] | 743 | #ifndef DISABLE_COLOR |
| 744 | else if (numreplaced > 0) |
| 745 | refresh_needed = TRUE; |
| 746 | #endif |
Benno Schulenberg | 08a52c1 | 2015-04-11 15:21:08 +0000 | [diff] [blame] | 747 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 748 | #ifndef NANO_TINY |
Benno Schulenberg | 5c3d529 | 2017-02-10 14:07:42 +0100 | [diff] [blame] | 749 | if (mark_was_set) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 750 | openfile->mark_set = TRUE; |
David Lawrence Ramsey | 40bdb68 | 2004-11-03 22:03:41 +0000 | [diff] [blame] | 751 | #endif |
| 752 | |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 753 | /* If the NO_NEWLINES flag isn't set, and text has been added to the |
| 754 | * magicline, make a new magicline. */ |
| 755 | if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 756 | new_magicline(); |
| 757 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 758 | return numreplaced; |
| 759 | } |
| 760 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 761 | /* Replace a string. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 762 | void do_replace(void) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 763 | { |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 764 | filestruct *edittop_save, *begin; |
Benno Schulenberg | ea4e9b3 | 2016-04-10 09:33:49 +0200 | [diff] [blame] | 765 | size_t begin_x; |
David Lawrence Ramsey | 53752e8 | 2004-10-18 22:19:22 +0000 | [diff] [blame] | 766 | ssize_t numreplaced; |
David Lawrence Ramsey | 8f4762a | 2005-07-16 22:35:11 +0000 | [diff] [blame] | 767 | int i; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 768 | |
Chris Allegretta | 5050aa6 | 2001-04-22 07:10:21 +0000 | [diff] [blame] | 769 | if (ISSET(VIEW_MODE)) { |
| 770 | print_view_warning(); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 771 | return; |
Chris Allegretta | 5050aa6 | 2001-04-22 07:10:21 +0000 | [diff] [blame] | 772 | } |
| 773 | |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 774 | i = search_init(TRUE, FALSE); |
Benno Schulenberg | 988274b | 2016-03-17 10:06:15 +0000 | [diff] [blame] | 775 | |
| 776 | if (i == -1) /* Cancelled, or some other exit reason. */ |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 777 | search_replace_abort(); |
Benno Schulenberg | 988274b | 2016-03-17 10:06:15 +0000 | [diff] [blame] | 778 | else if (i == -2) /* Do a search instead. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 779 | do_search(); |
Benno Schulenberg | 988274b | 2016-03-17 10:06:15 +0000 | [diff] [blame] | 780 | else if (i == 1) /* Toggled something. */ |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 781 | do_replace(); |
| 782 | |
| 783 | if (i != 0) |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 784 | return; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 785 | |
Benno Schulenberg | fd0589d | 2017-01-02 21:12:44 +0100 | [diff] [blame] | 786 | i = do_prompt(FALSE, FALSE, MREPLACEWITH, NULL, |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 787 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 788 | &replace_history, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 789 | #endif |
Benno Schulenberg | b77b139 | 2016-08-26 12:18:04 +0200 | [diff] [blame] | 790 | /* TRANSLATORS: This is a prompt. */ |
| 791 | edit_refresh, _("Replace with")); |
Chris Allegretta | 15c28f8 | 2003-01-05 21:47:06 +0000 | [diff] [blame] | 792 | |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 793 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | cb776fa | 2015-03-21 21:40:56 +0000 | [diff] [blame] | 794 | /* If the replace string is not "", add it to the replace history list. */ |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 795 | if (i == 0) |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 796 | update_history(&replace_history, answer); |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 797 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 798 | |
Benno Schulenberg | 311f0e8 | 2016-03-19 20:19:49 +0000 | [diff] [blame] | 799 | /* When cancelled, or when a function was run, get out. */ |
| 800 | if (i == -1 || i > 0) { |
| 801 | if (i == -1) |
David Lawrence Ramsey | d132210 | 2004-08-26 04:22:54 +0000 | [diff] [blame] | 802 | statusbar(_("Cancelled")); |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 803 | search_replace_abort(); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 804 | return; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 805 | } |
| 806 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 807 | /* Save where we are. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 808 | edittop_save = openfile->edittop; |
| 809 | begin = openfile->current; |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 810 | begin_x = openfile->current_x; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 811 | |
Benno Schulenberg | 2cd8ca4 | 2016-10-23 17:59:26 +0200 | [diff] [blame] | 812 | numreplaced = do_replace_loop(last_search, FALSE, begin, &begin_x); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 813 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 814 | /* Restore where we were. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 815 | openfile->edittop = edittop_save; |
| 816 | openfile->current = begin; |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 817 | openfile->current_x = begin_x; |
Benno Schulenberg | f920e0d | 2016-12-03 17:00:28 +0100 | [diff] [blame] | 818 | refresh_needed = TRUE; |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 819 | |
| 820 | if (numreplaced >= 0) |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 821 | statusline(HUSH, P_("Replaced %lu occurrence", |
David Lawrence Ramsey | bdfa927 | 2005-06-14 23:36:13 +0000 | [diff] [blame] | 822 | "Replaced %lu occurrences", (unsigned long)numreplaced), |
| 823 | (unsigned long)numreplaced); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 824 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 825 | search_replace_abort(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 828 | /* Go to the specified line and x position. */ |
| 829 | void goto_line_posx(ssize_t line, size_t pos_x) |
| 830 | { |
Benno Schulenberg | 5834638 | 2015-12-08 18:54:13 +0000 | [diff] [blame] | 831 | for (openfile->current = openfile->fileage; line > 1 && |
| 832 | openfile->current != openfile->filebot; line--) |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 833 | openfile->current = openfile->current->next; |
| 834 | |
| 835 | openfile->current_x = pos_x; |
| 836 | openfile->placewewant = xplustabs(); |
| 837 | |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 838 | refresh_needed = TRUE; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 839 | } |
| 840 | |
David Lawrence Ramsey | 0a8ec17 | 2005-05-25 18:44:37 +0000 | [diff] [blame] | 841 | /* Go to the specified line and column, or ask for them if interactive |
Benno Schulenberg | 46db638 | 2015-12-31 19:20:40 +0000 | [diff] [blame] | 842 | * is TRUE. In the latter case also update the screen afterwards. |
Benno Schulenberg | 1592ca0 | 2015-12-31 17:20:46 +0000 | [diff] [blame] | 843 | * Note that both the line and column number should be one-based. */ |
David Lawrence Ramsey | 5beae58 | 2005-06-29 00:17:18 +0000 | [diff] [blame] | 844 | void do_gotolinecolumn(ssize_t line, ssize_t column, bool use_answer, |
Benno Schulenberg | 46db638 | 2015-12-31 19:20:40 +0000 | [diff] [blame] | 845 | bool interactive) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 846 | { |
David Lawrence Ramsey | 8f1afee | 2005-07-16 07:06:36 +0000 | [diff] [blame] | 847 | if (interactive) { |
Benno Schulenberg | 6c86ee1 | 2014-07-02 19:12:38 +0000 | [diff] [blame] | 848 | functionptrtype func; |
David Lawrence Ramsey | 8f1afee | 2005-07-16 07:06:36 +0000 | [diff] [blame] | 849 | |
David Lawrence Ramsey | 6335fb5 | 2007-01-01 05:15:32 +0000 | [diff] [blame] | 850 | /* Ask for the line and column. */ |
Benno Schulenberg | fd0589d | 2017-01-02 21:12:44 +0100 | [diff] [blame] | 851 | int i = do_prompt(FALSE, FALSE, MGOTOLINE, |
| 852 | use_answer ? answer : NULL, |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 853 | #ifndef DISABLE_HISTORIES |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 854 | NULL, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 855 | #endif |
Benno Schulenberg | a2d74f7 | 2014-05-28 14:34:11 +0000 | [diff] [blame] | 856 | /* TRANSLATORS: This is a prompt. */ |
David Lawrence Ramsey | 6816007 | 2006-02-18 21:32:29 +0000 | [diff] [blame] | 857 | edit_refresh, _("Enter line number, column number")); |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 858 | |
Benno Schulenberg | 59b9b22 | 2016-06-21 16:47:11 +0200 | [diff] [blame] | 859 | /* If the user cancelled or gave a blank answer, get out. */ |
David Lawrence Ramsey | 4c9e8f4 | 2004-10-04 16:01:37 +0000 | [diff] [blame] | 860 | if (i < 0) { |
David Lawrence Ramsey | b77ec62 | 2004-09-28 15:06:15 +0000 | [diff] [blame] | 861 | statusbar(_("Cancelled")); |
David Lawrence Ramsey | e5d8f32 | 2004-09-30 22:07:21 +0000 | [diff] [blame] | 862 | return; |
| 863 | } |
| 864 | |
Benno Schulenberg | 6c86ee1 | 2014-07-02 19:12:38 +0000 | [diff] [blame] | 865 | func = func_from_key(&i); |
Chris Allegretta | 0018d8e | 2008-03-13 08:23:52 +0000 | [diff] [blame] | 866 | |
Benno Schulenberg | 6c86ee1 | 2014-07-02 19:12:38 +0000 | [diff] [blame] | 867 | if (func == gototext_void) { |
Benno Schulenberg | 59b9b22 | 2016-06-21 16:47:11 +0200 | [diff] [blame] | 868 | /* Retain what the user typed so far and switch to searching. */ |
David Lawrence Ramsey | d532f19 | 2004-10-04 22:37:56 +0000 | [diff] [blame] | 869 | search_init(TRUE, TRUE); |
David Lawrence Ramsey | 4c9e8f4 | 2004-10-04 16:01:37 +0000 | [diff] [blame] | 870 | do_search(); |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Benno Schulenberg | 59b9b22 | 2016-06-21 16:47:11 +0200 | [diff] [blame] | 873 | /* If a function was executed, we're done here. */ |
| 874 | if (i > 0) |
| 875 | return; |
| 876 | |
Benno Schulenberg | 1a4ec6c | 2016-06-20 22:13:14 +0200 | [diff] [blame] | 877 | /* Try to extract one or two numbers from the user's response. */ |
| 878 | if (!parse_line_column(answer, &line, &column)) { |
Benno Schulenberg | e8c7cf2 | 2017-01-17 14:17:42 +0100 | [diff] [blame] | 879 | statusline(ALERT, _("Invalid line or column number")); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 880 | return; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 881 | } |
David Lawrence Ramsey | 9245f97 | 2005-05-17 18:06:26 +0000 | [diff] [blame] | 882 | } else { |
| 883 | if (line < 1) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 884 | line = openfile->current->lineno; |
David Lawrence Ramsey | 9245f97 | 2005-05-17 18:06:26 +0000 | [diff] [blame] | 885 | |
David Lawrence Ramsey | 0a8ec17 | 2005-05-25 18:44:37 +0000 | [diff] [blame] | 886 | if (column < 1) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 887 | column = openfile->placewewant + 1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Benno Schulenberg | 1a4ec6c | 2016-06-20 22:13:14 +0200 | [diff] [blame] | 890 | /* Take a negative line number to mean: from the end of the file. */ |
| 891 | if (line < 0) |
| 892 | line = openfile->filebot->lineno + line + 1; |
| 893 | if (line < 1) |
| 894 | line = 1; |
| 895 | |
| 896 | /* Iterate to the requested line. */ |
Benno Schulenberg | 34fbb1f | 2016-01-13 20:32:40 +0000 | [diff] [blame] | 897 | for (openfile->current = openfile->fileage; line > 1 && |
| 898 | openfile->current != openfile->filebot; line--) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 899 | openfile->current = openfile->current->next; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 900 | |
Benno Schulenberg | 1a4ec6c | 2016-06-20 22:13:14 +0200 | [diff] [blame] | 901 | /* Take a negative column number to mean: from the end of the line. */ |
| 902 | if (column < 0) |
| 903 | column = strlenpt(openfile->current->data) + column + 2; |
| 904 | if (column < 1) |
| 905 | column = 1; |
| 906 | |
| 907 | /* Set the x position that corresponds to the requested column. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 908 | openfile->current_x = actual_x(openfile->current->data, column - 1); |
| 909 | openfile->placewewant = column - 1; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 910 | |
Benno Schulenberg | aaab6e5 | 2016-05-06 21:57:25 +0200 | [diff] [blame] | 911 | /* When the position was manually given, center the target line. */ |
Benno Schulenberg | d716809 | 2016-10-10 13:16:32 +0200 | [diff] [blame] | 912 | if (interactive || ISSET(SOFTWRAP)) { |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 913 | adjust_viewport(CENTERING); |
| 914 | refresh_needed = TRUE; |
Benno Schulenberg | aaab6e5 | 2016-05-06 21:57:25 +0200 | [diff] [blame] | 915 | } else { |
| 916 | /* If the target line is close to the tail of the file, put the last |
| 917 | * line of the file on the bottom line of the screen; otherwise, just |
| 918 | * center the target line. */ |
| 919 | if (openfile->filebot->lineno - openfile->current->lineno < |
| 920 | editwinrows / 2) { |
| 921 | openfile->current_y = editwinrows - openfile->filebot->lineno + |
| 922 | openfile->current->lineno - 1; |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 923 | adjust_viewport(STATIONARY); |
Benno Schulenberg | aaab6e5 | 2016-05-06 21:57:25 +0200 | [diff] [blame] | 924 | } else |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 925 | adjust_viewport(CENTERING); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 926 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 927 | } |
| 928 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 929 | /* Go to the specified line and column, asking for them beforehand. */ |
David Lawrence Ramsey | 9245f97 | 2005-05-17 18:06:26 +0000 | [diff] [blame] | 930 | void do_gotolinecolumn_void(void) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 931 | { |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 932 | do_gotolinecolumn(openfile->current->lineno, |
Benno Schulenberg | 46db638 | 2015-12-31 19:20:40 +0000 | [diff] [blame] | 933 | openfile->placewewant + 1, FALSE, TRUE); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 934 | } |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 935 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 936 | #ifndef NANO_TINY |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 937 | /* Search for a match to one of the two characters in bracket_set. If |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 938 | * reverse is TRUE, search backwards for the leftmost bracket. |
| 939 | * Otherwise, search forwards for the rightmost bracket. Return TRUE if |
| 940 | * we found a match, and FALSE otherwise. */ |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 941 | bool find_bracket_match(bool reverse, const char *bracket_set) |
| 942 | { |
| 943 | filestruct *fileptr = openfile->current; |
| 944 | const char *rev_start = NULL, *found = NULL; |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 945 | |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 946 | assert(mbstrlen(bracket_set) == 2); |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 947 | |
| 948 | /* rev_start might end up 1 character before the start or after the |
| 949 | * end of the line. This won't be a problem because we'll skip over |
| 950 | * it below in that case, and rev_start will be properly set when |
| 951 | * the search continues on the previous or next line. */ |
Benno Schulenberg | 04013fb | 2016-04-16 12:44:52 +0200 | [diff] [blame] | 952 | if (reverse) |
| 953 | rev_start = fileptr->data + (openfile->current_x - 1); |
| 954 | else |
| 955 | rev_start = fileptr->data + (openfile->current_x + 1); |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 956 | |
| 957 | /* Look for either of the two characters in bracket_set. rev_start |
| 958 | * can be 1 character before the start or after the end of the line. |
| 959 | * In either case, just act as though no match is found. */ |
| 960 | while (TRUE) { |
Benno Schulenberg | 04013fb | 2016-04-16 12:44:52 +0200 | [diff] [blame] | 961 | if ((rev_start > fileptr->data && *(rev_start - 1) == '\0') || |
| 962 | rev_start < fileptr->data) |
| 963 | found = NULL; |
| 964 | else if (reverse) |
| 965 | found = mbrevstrpbrk(fileptr->data, bracket_set, rev_start); |
| 966 | else |
| 967 | found = mbstrpbrk(rev_start, bracket_set); |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 968 | |
Benno Schulenberg | 04013fb | 2016-04-16 12:44:52 +0200 | [diff] [blame] | 969 | if (found) |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 970 | break; |
| 971 | |
Benno Schulenberg | 7ba356a | 2016-04-08 18:11:22 +0200 | [diff] [blame] | 972 | if (reverse) |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 973 | fileptr = fileptr->prev; |
Benno Schulenberg | 7ba356a | 2016-04-08 18:11:22 +0200 | [diff] [blame] | 974 | else |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 975 | fileptr = fileptr->next; |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 976 | |
Benno Schulenberg | 04013fb | 2016-04-16 12:44:52 +0200 | [diff] [blame] | 977 | /* If we've reached the start or end of the buffer, get out. */ |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 978 | if (fileptr == NULL) |
| 979 | return FALSE; |
| 980 | |
| 981 | rev_start = fileptr->data; |
| 982 | if (reverse) |
| 983 | rev_start += strlen(fileptr->data); |
| 984 | } |
| 985 | |
Benno Schulenberg | 04013fb | 2016-04-16 12:44:52 +0200 | [diff] [blame] | 986 | /* Set the current position to the found matching bracket. */ |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 987 | openfile->current = fileptr; |
David Lawrence Ramsey | ebe4b3b | 2005-11-16 03:54:22 +0000 | [diff] [blame] | 988 | openfile->current_x = found - fileptr->data; |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 989 | |
| 990 | return TRUE; |
| 991 | } |
| 992 | |
| 993 | /* Search for a match to the bracket at the current cursor position, if |
| 994 | * there is one. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 995 | void do_find_bracket(void) |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 996 | { |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 997 | filestruct *current_save; |
Benno Schulenberg | 023fccb | 2016-04-10 11:14:03 +0200 | [diff] [blame] | 998 | size_t current_x_save; |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 999 | const char *ch; |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1000 | /* The location in matchbrackets of the bracket at the current |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 1001 | * cursor position. */ |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1002 | int ch_len; |
| 1003 | /* The length of ch in bytes. */ |
| 1004 | const char *wanted_ch; |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1005 | /* The location in matchbrackets of the bracket complementing |
| 1006 | * the bracket at the current cursor position. */ |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1007 | int wanted_ch_len; |
| 1008 | /* The length of wanted_ch in bytes. */ |
| 1009 | char *bracket_set; |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 1010 | /* The pair of characters in ch and wanted_ch. */ |
David Lawrence Ramsey | db2dc81 | 2006-01-06 22:35:52 +0000 | [diff] [blame] | 1011 | size_t i; |
| 1012 | /* Generic loop variable. */ |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1013 | size_t matchhalf; |
David Lawrence Ramsey | db2dc81 | 2006-01-06 22:35:52 +0000 | [diff] [blame] | 1014 | /* The number of single-byte characters in one half of |
| 1015 | * matchbrackets. */ |
| 1016 | size_t mbmatchhalf; |
| 1017 | /* The number of multibyte characters in one half of |
| 1018 | * matchbrackets. */ |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 1019 | size_t count = 1; |
| 1020 | /* The initial bracket count. */ |
| 1021 | bool reverse; |
| 1022 | /* The direction we search. */ |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1023 | char *found_ch; |
| 1024 | /* The character we find. */ |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 1025 | |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1026 | assert(mbstrlen(matchbrackets) % 2 == 0); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1027 | |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1028 | ch = openfile->current->data + openfile->current_x; |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 1029 | |
Benno Schulenberg | 677e947 | 2015-08-13 17:46:19 +0000 | [diff] [blame] | 1030 | if ((ch = mbstrchr(matchbrackets, ch)) == NULL) { |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 1031 | statusbar(_("Not a bracket")); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1032 | return; |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 1035 | /* Save where we are. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1036 | current_save = openfile->current; |
| 1037 | current_x_save = openfile->current_x; |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 1038 | |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1039 | /* If we're on an opening bracket, which must be in the first half |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1040 | * of matchbrackets, we want to search forwards for a closing |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1041 | * bracket. If we're on a closing bracket, which must be in the |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1042 | * second half of matchbrackets, we want to search backwards for an |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1043 | * opening bracket. */ |
David Lawrence Ramsey | db2dc81 | 2006-01-06 22:35:52 +0000 | [diff] [blame] | 1044 | matchhalf = 0; |
| 1045 | mbmatchhalf = mbstrlen(matchbrackets) / 2; |
| 1046 | |
| 1047 | for (i = 0; i < mbmatchhalf; i++) |
Benno Schulenberg | e753cd1 | 2016-03-17 19:30:29 +0000 | [diff] [blame] | 1048 | matchhalf += parse_mbchar(matchbrackets + matchhalf, NULL, NULL); |
David Lawrence Ramsey | db2dc81 | 2006-01-06 22:35:52 +0000 | [diff] [blame] | 1049 | |
David Lawrence Ramsey | 4a2e000 | 2006-01-28 06:04:59 +0000 | [diff] [blame] | 1050 | reverse = ((ch - matchbrackets) >= matchhalf); |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1051 | |
| 1052 | /* If we're on an opening bracket, set wanted_ch to the character |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1053 | * that's matchhalf characters after ch. If we're on a closing |
| 1054 | * bracket, set wanted_ch to the character that's matchhalf |
| 1055 | * characters before ch. */ |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1056 | wanted_ch = ch; |
| 1057 | |
David Lawrence Ramsey | db2dc81 | 2006-01-06 22:35:52 +0000 | [diff] [blame] | 1058 | while (mbmatchhalf > 0) { |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1059 | if (reverse) |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 1060 | wanted_ch = matchbrackets + move_mbleft(matchbrackets, |
Benno Schulenberg | e753cd1 | 2016-03-17 19:30:29 +0000 | [diff] [blame] | 1061 | wanted_ch - matchbrackets); |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1062 | else |
| 1063 | wanted_ch += move_mbright(wanted_ch, 0); |
| 1064 | |
David Lawrence Ramsey | db2dc81 | 2006-01-06 22:35:52 +0000 | [diff] [blame] | 1065 | mbmatchhalf--; |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | ch_len = parse_mbchar(ch, NULL, NULL); |
| 1069 | wanted_ch_len = parse_mbchar(wanted_ch, NULL, NULL); |
| 1070 | |
| 1071 | /* Fill bracket_set in with the values of ch and wanted_ch. */ |
| 1072 | bracket_set = charalloc((mb_cur_max() * 2) + 1); |
| 1073 | strncpy(bracket_set, ch, ch_len); |
| 1074 | strncpy(bracket_set + ch_len, wanted_ch, wanted_ch_len); |
| 1075 | null_at(&bracket_set, ch_len + wanted_ch_len); |
| 1076 | |
| 1077 | found_ch = charalloc(mb_cur_max() + 1); |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 1078 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1079 | while (TRUE) { |
David Lawrence Ramsey | c5c5230 | 2005-11-15 23:45:29 +0000 | [diff] [blame] | 1080 | if (find_bracket_match(reverse, bracket_set)) { |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 1081 | /* If we found an identical bracket, increment count. If we |
| 1082 | * found a complementary bracket, decrement it. */ |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1083 | parse_mbchar(openfile->current->data + openfile->current_x, |
Benno Schulenberg | e753cd1 | 2016-03-17 19:30:29 +0000 | [diff] [blame] | 1084 | found_ch, NULL); |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1085 | count += (strncmp(found_ch, ch, ch_len) == 0) ? 1 : -1; |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 1086 | |
| 1087 | /* If count is zero, we've found a matching bracket. Update |
| 1088 | * the screen and get out. */ |
| 1089 | if (count == 0) { |
Benno Schulenberg | 318ed6b | 2016-04-12 10:24:57 +0200 | [diff] [blame] | 1090 | focusing = FALSE; |
Benno Schulenberg | aa1ae0a | 2016-04-10 21:16:19 +0200 | [diff] [blame] | 1091 | edit_redraw(current_save); |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1092 | break; |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 1093 | } |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1094 | } else { |
David Lawrence Ramsey | 3a9a329 | 2005-07-21 02:20:01 +0000 | [diff] [blame] | 1095 | /* We didn't find either an opening or closing bracket. |
| 1096 | * Indicate this, restore where we were, and get out. */ |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 1097 | statusbar(_("No matching bracket")); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1098 | openfile->current = current_save; |
| 1099 | openfile->current_x = current_x_save; |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 1100 | break; |
| 1101 | } |
| 1102 | } |
David Lawrence Ramsey | a248863 | 2006-01-06 07:10:30 +0000 | [diff] [blame] | 1103 | |
| 1104 | /* Clean up. */ |
| 1105 | free(bracket_set); |
| 1106 | free(found_ch); |
Chris Allegretta | 8d990b5 | 2001-09-22 22:14:25 +0000 | [diff] [blame] | 1107 | } |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 1108 | #endif /* !NANO_TINY */ |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1109 | |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 1110 | #ifndef DISABLE_HISTORIES |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1111 | /* Indicate whether any of the history lists have changed. */ |
| 1112 | bool history_has_changed(void) |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1113 | { |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1114 | return history_changed; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1117 | /* Initialize the search and replace history lists. */ |
| 1118 | void history_init(void) |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1119 | { |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1120 | search_history = make_new_node(NULL); |
| 1121 | search_history->data = mallocstrcpy(NULL, ""); |
| 1122 | searchage = search_history; |
| 1123 | searchbot = search_history; |
| 1124 | |
| 1125 | replace_history = make_new_node(NULL); |
| 1126 | replace_history->data = mallocstrcpy(NULL, ""); |
| 1127 | replaceage = replace_history; |
| 1128 | replacebot = replace_history; |
| 1129 | } |
| 1130 | |
David Lawrence Ramsey | 0e581b3 | 2005-07-18 07:48:50 +0000 | [diff] [blame] | 1131 | /* Set the current position in the history list h to the bottom. */ |
| 1132 | void history_reset(const filestruct *h) |
| 1133 | { |
| 1134 | if (h == search_history) |
| 1135 | search_history = searchbot; |
| 1136 | else if (h == replace_history) |
| 1137 | replace_history = replacebot; |
| 1138 | } |
| 1139 | |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1140 | /* Return the first node containing the first len characters of the |
| 1141 | * string s in the history list, starting at h_start and ending at |
| 1142 | * h_end, or NULL if there isn't one. */ |
David Lawrence Ramsey | 96e6d56 | 2005-07-19 04:53:45 +0000 | [diff] [blame] | 1143 | filestruct *find_history(const filestruct *h_start, const filestruct |
| 1144 | *h_end, const char *s, size_t len) |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1145 | { |
David Lawrence Ramsey | 96e6d56 | 2005-07-19 04:53:45 +0000 | [diff] [blame] | 1146 | const filestruct *p; |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1147 | |
Benno Schulenberg | 036c5f9 | 2016-03-20 13:38:09 +0000 | [diff] [blame] | 1148 | for (p = h_start; p != h_end->prev && p != NULL; p = p->prev) { |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1149 | if (strncmp(s, p->data, len) == 0) |
David Lawrence Ramsey | 96e6d56 | 2005-07-19 04:53:45 +0000 | [diff] [blame] | 1150 | return (filestruct *)p; |
David Lawrence Ramsey | 7f4dd35 | 2005-05-08 17:08:51 +0000 | [diff] [blame] | 1151 | } |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1152 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1153 | return NULL; |
| 1154 | } |
| 1155 | |
Benno Schulenberg | 0172cb0 | 2016-06-02 12:07:08 +0200 | [diff] [blame] | 1156 | /* Update a history list (the one in which h is the current position) |
| 1157 | * with a fresh string s. That is: add s, or move it to the end. */ |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1158 | void update_history(filestruct **h, const char *s) |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1159 | { |
Benno Schulenberg | e198c85 | 2016-06-01 17:58:16 +0200 | [diff] [blame] | 1160 | filestruct **hage = NULL, **hbot = NULL, *thesame; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1161 | |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1162 | assert(h != NULL && s != NULL); |
| 1163 | |
| 1164 | if (*h == search_history) { |
| 1165 | hage = &searchage; |
| 1166 | hbot = &searchbot; |
| 1167 | } else if (*h == replace_history) { |
| 1168 | hage = &replaceage; |
| 1169 | hbot = &replacebot; |
| 1170 | } |
| 1171 | |
| 1172 | assert(hage != NULL && hbot != NULL); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1173 | |
Benno Schulenberg | 0172cb0 | 2016-06-02 12:07:08 +0200 | [diff] [blame] | 1174 | /* See if the string is already in the history. */ |
Benno Schulenberg | 660584c | 2016-06-01 21:29:44 +0200 | [diff] [blame] | 1175 | thesame = find_history(*hbot, *hage, s, HIGHEST_POSITIVE); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1176 | |
Benno Schulenberg | e198c85 | 2016-06-01 17:58:16 +0200 | [diff] [blame] | 1177 | /* If an identical string was found, delete that item. */ |
| 1178 | if (thesame != NULL) { |
| 1179 | filestruct *after = thesame->next; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1180 | |
Benno Schulenberg | e198c85 | 2016-06-01 17:58:16 +0200 | [diff] [blame] | 1181 | /* If the string is at the head of the list, move the head. */ |
| 1182 | if (thesame == *hage) |
| 1183 | *hage = after; |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1184 | |
Benno Schulenberg | e198c85 | 2016-06-01 17:58:16 +0200 | [diff] [blame] | 1185 | unlink_node(thesame); |
| 1186 | renumber(after); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1187 | } |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1188 | |
Benno Schulenberg | 0172cb0 | 2016-06-02 12:07:08 +0200 | [diff] [blame] | 1189 | /* If the history is full, delete the oldest item (the one at the |
| 1190 | * head of the list), to make room for a new item at the end. */ |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1191 | if ((*hbot)->lineno == MAX_SEARCH_HISTORY + 1) { |
Benno Schulenberg | c92b9be | 2016-12-23 12:51:04 +0100 | [diff] [blame] | 1192 | filestruct *oldest = *hage; |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1193 | |
| 1194 | *hage = (*hage)->next; |
Benno Schulenberg | c92b9be | 2016-12-23 12:51:04 +0100 | [diff] [blame] | 1195 | unlink_node(oldest); |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1196 | renumber(*hage); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1197 | } |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1198 | |
Benno Schulenberg | 0172cb0 | 2016-06-02 12:07:08 +0200 | [diff] [blame] | 1199 | /* Store the fresh string in the last item, then create a new item. */ |
David Lawrence Ramsey | b513778 | 2006-12-13 00:27:45 +0000 | [diff] [blame] | 1200 | (*hbot)->data = mallocstrcpy((*hbot)->data, s); |
Benno Schulenberg | fbe4376 | 2015-11-24 13:24:01 +0000 | [diff] [blame] | 1201 | splice_node(*hbot, make_new_node(*hbot)); |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1202 | *hbot = (*hbot)->next; |
| 1203 | (*hbot)->data = mallocstrcpy(NULL, ""); |
| 1204 | |
Benno Schulenberg | 0172cb0 | 2016-06-02 12:07:08 +0200 | [diff] [blame] | 1205 | /* Indicate that the history needs to be saved on exit. */ |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1206 | history_changed = TRUE; |
| 1207 | |
| 1208 | /* Set the current position in the list to the bottom. */ |
| 1209 | *h = *hbot; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
David Lawrence Ramsey | 8b3266e | 2005-05-26 02:46:42 +0000 | [diff] [blame] | 1212 | /* Move h to the string in the history list just before it, and return |
| 1213 | * that string. If there isn't one, don't move h and return NULL. */ |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1214 | char *get_history_older(filestruct **h) |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1215 | { |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1216 | assert(h != NULL); |
| 1217 | |
| 1218 | if ((*h)->prev == NULL) |
Chris Allegretta | 1b6ed07 | 2008-06-03 08:09:05 +0000 | [diff] [blame] | 1219 | return NULL; |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1220 | |
| 1221 | *h = (*h)->prev; |
| 1222 | |
| 1223 | return (*h)->data; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
David Lawrence Ramsey | 8b3266e | 2005-05-26 02:46:42 +0000 | [diff] [blame] | 1226 | /* Move h to the string in the history list just after it, and return |
| 1227 | * that string. If there isn't one, don't move h and return NULL. */ |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1228 | char *get_history_newer(filestruct **h) |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1229 | { |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1230 | assert(h != NULL); |
| 1231 | |
| 1232 | if ((*h)->next == NULL) |
Chris Allegretta | 1b6ed07 | 2008-06-03 08:09:05 +0000 | [diff] [blame] | 1233 | return NULL; |
David Lawrence Ramsey | 934f968 | 2005-05-23 16:30:06 +0000 | [diff] [blame] | 1234 | |
| 1235 | *h = (*h)->next; |
| 1236 | |
| 1237 | return (*h)->data; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1238 | } |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1239 | |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 1240 | /* More placeholders. */ |
Chris Allegretta | 637daa8 | 2011-02-07 14:45:56 +0000 | [diff] [blame] | 1241 | void get_history_newer_void(void) |
| 1242 | { |
| 1243 | ; |
| 1244 | } |
| 1245 | void get_history_older_void(void) |
| 1246 | { |
| 1247 | ; |
| 1248 | } |
| 1249 | |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1250 | #ifndef DISABLE_TABCOMP |
| 1251 | /* Move h to the next string that's a tab completion of the string s, |
| 1252 | * looking at only the first len characters of s, and return that |
David Lawrence Ramsey | 47ae0d7 | 2005-06-03 14:30:52 +0000 | [diff] [blame] | 1253 | * string. If there isn't one, or if len is 0, don't move h and return |
| 1254 | * s. */ |
Benno Schulenberg | e86dc03 | 2016-02-20 12:16:43 +0000 | [diff] [blame] | 1255 | char *get_history_completion(filestruct **h, char *s, size_t len) |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1256 | { |
| 1257 | assert(s != NULL); |
| 1258 | |
| 1259 | if (len > 0) { |
| 1260 | filestruct *hage = NULL, *hbot = NULL, *p; |
| 1261 | |
| 1262 | assert(h != NULL); |
| 1263 | |
| 1264 | if (*h == search_history) { |
| 1265 | hage = searchage; |
| 1266 | hbot = searchbot; |
| 1267 | } else if (*h == replace_history) { |
| 1268 | hage = replaceage; |
| 1269 | hbot = replacebot; |
| 1270 | } |
| 1271 | |
| 1272 | assert(hage != NULL && hbot != NULL); |
| 1273 | |
Benno Schulenberg | 036c5f9 | 2016-03-20 13:38:09 +0000 | [diff] [blame] | 1274 | /* Search the history list from the current position to the top |
| 1275 | * for a match of len characters. Skip over an exact match. */ |
| 1276 | p = find_history((*h)->prev, hage, s, len); |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1277 | |
David Lawrence Ramsey | d2edb8a | 2005-06-13 03:52:33 +0000 | [diff] [blame] | 1278 | while (p != NULL && strcmp(p->data, s) == 0) |
Benno Schulenberg | 036c5f9 | 2016-03-20 13:38:09 +0000 | [diff] [blame] | 1279 | p = find_history(p->prev, hage, s, len); |
David Lawrence Ramsey | d2edb8a | 2005-06-13 03:52:33 +0000 | [diff] [blame] | 1280 | |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1281 | if (p != NULL) { |
| 1282 | *h = p; |
Benno Schulenberg | e86dc03 | 2016-02-20 12:16:43 +0000 | [diff] [blame] | 1283 | return mallocstrcpy(s, (*h)->data); |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Benno Schulenberg | 036c5f9 | 2016-03-20 13:38:09 +0000 | [diff] [blame] | 1286 | /* Search the history list from the bottom to the current position |
David Lawrence Ramsey | d2edb8a | 2005-06-13 03:52:33 +0000 | [diff] [blame] | 1287 | * for a match of len characters. Skip over an exact match. */ |
Benno Schulenberg | 036c5f9 | 2016-03-20 13:38:09 +0000 | [diff] [blame] | 1288 | p = find_history(hbot, *h, s, len); |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1289 | |
David Lawrence Ramsey | d2edb8a | 2005-06-13 03:52:33 +0000 | [diff] [blame] | 1290 | while (p != NULL && strcmp(p->data, s) == 0) |
Benno Schulenberg | 036c5f9 | 2016-03-20 13:38:09 +0000 | [diff] [blame] | 1291 | p = find_history(p->prev, *h, s, len); |
David Lawrence Ramsey | d2edb8a | 2005-06-13 03:52:33 +0000 | [diff] [blame] | 1292 | |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1293 | if (p != NULL) { |
| 1294 | *h = p; |
Benno Schulenberg | e86dc03 | 2016-02-20 12:16:43 +0000 | [diff] [blame] | 1295 | return mallocstrcpy(s, (*h)->data); |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
David Lawrence Ramsey | d2edb8a | 2005-06-13 03:52:33 +0000 | [diff] [blame] | 1299 | /* If we're here, we didn't find a match, we didn't find an inexact |
| 1300 | * match, or len is 0. Return s. */ |
David Lawrence Ramsey | 96e6d56 | 2005-07-19 04:53:45 +0000 | [diff] [blame] | 1301 | return (char *)s; |
David Lawrence Ramsey | 34bdc35 | 2005-06-02 18:41:31 +0000 | [diff] [blame] | 1302 | } |
David Lawrence Ramsey | 4cf2246 | 2005-08-11 18:30:47 +0000 | [diff] [blame] | 1303 | #endif /* !DISABLE_TABCOMP */ |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 1304 | #endif /* !DISABLE_HISTORIES */ |