Chris Allegretta | 11b0011 | 2000-08-06 21:13:45 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 2 | /************************************************************************** |
| 3 | * search.c * |
| 4 | * * |
| 5 | * Copyright (C) 2000 Chris Allegretta * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 1, or (at your option) * |
| 9 | * any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 19 | * * |
| 20 | **************************************************************************/ |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 26 | #include <ctype.h> |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 27 | #include "config.h" |
| 28 | #include "proto.h" |
| 29 | #include "nano.h" |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 30 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 31 | #ifndef NANO_SMALL |
| 32 | #include <libintl.h> |
| 33 | #define _(string) gettext(string) |
| 34 | #else |
| 35 | #define _(string) (string) |
| 36 | #endif |
| 37 | |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 38 | /* Regular expression helper functions */ |
| 39 | |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 40 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 41 | void regexp_init(const char *regexp) |
| 42 | { |
| 43 | regcomp(&search_regexp, regexp, ISSET(CASE_SENSITIVE) ? 0 : REG_ICASE); |
| 44 | SET(REGEXP_COMPILED); |
| 45 | } |
| 46 | |
Chris Allegretta | e316773 | 2001-03-18 16:59:34 +0000 | [diff] [blame] | 47 | void regexp_cleanup(void) |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 48 | { |
| 49 | UNSET(REGEXP_COMPILED); |
| 50 | regfree(&search_regexp); |
| 51 | } |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 52 | #endif |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 53 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 54 | void search_init_globals(void) |
| 55 | { |
| 56 | if (last_search == NULL) { |
| 57 | last_search = nmalloc(1); |
| 58 | last_search[0] = 0; |
| 59 | } |
| 60 | if (last_replace == NULL) { |
| 61 | last_replace = nmalloc(1); |
| 62 | last_replace[0] = 0; |
| 63 | } |
| 64 | } |
| 65 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 66 | /* Set up the system variables for a search or replace. Returns -1 on |
| 67 | abort, 0 on success, and 1 on rerun calling program |
| 68 | Return -2 to run opposite program (searchg -> replace, replace -> search) |
| 69 | |
| 70 | replacing = 1 if we call from do_replace, 0 if called from do_search func. |
| 71 | */ |
| 72 | int search_init(int replacing) |
| 73 | { |
Chris Allegretta | 71844ba | 2000-11-03 14:23:00 +0000 | [diff] [blame] | 74 | int i = 0; |
| 75 | char *buf; |
Chris Allegretta | a4d2162 | 2000-07-08 23:57:03 +0000 | [diff] [blame] | 76 | char *prompt, *reprompt = ""; |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 77 | static char *backupstring = NULL; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 78 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 79 | search_init_globals(); |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 80 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 81 | buf = nmalloc(strlen(last_search) + 5); |
| 82 | buf[0] = 0; |
Chris Allegretta | 71844ba | 2000-11-03 14:23:00 +0000 | [diff] [blame] | 83 | |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 84 | /* Okay, fun time. backupstring is our holder for what is being |
| 85 | returned from the statusq call. Using answer for this would be tricky. |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 86 | Here, if we're using PICO_MODE, we only want nano to put the |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 87 | old string back up as editable if it's not the same as last_search. |
| 88 | |
| 89 | Otherwise, if we don't already have a backupstring, set it to |
| 90 | last_search. */ |
| 91 | |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 92 | if (ISSET(PICO_MODE)) { |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 93 | if (backupstring == NULL || !strcmp(backupstring, last_search)) |
| 94 | backupstring = mallocstrcpy(backupstring, ""); |
| 95 | } |
| 96 | else if (backupstring == NULL) |
| 97 | backupstring = mallocstrcpy(backupstring, last_search); |
| 98 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 99 | /* If using Pico messages, we do things the old fashioned way... */ |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 100 | if (ISSET(PICO_MODE)) { |
Chris Allegretta | 71844ba | 2000-11-03 14:23:00 +0000 | [diff] [blame] | 101 | if (last_search[0]) { |
| 102 | |
| 103 | /* We use COLS / 3 here because we need to see more on the line */ |
| 104 | if (strlen(last_search) > COLS / 3) { |
| 105 | snprintf(buf, COLS / 3 + 3, " [%s", last_search); |
| 106 | sprintf(&buf[COLS / 3 + 2], "...]"); |
| 107 | } else |
| 108 | sprintf(buf, " [%s]", last_search); |
| 109 | } else { |
| 110 | buf[0] = '\0'; |
| 111 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 112 | } |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 113 | else |
| 114 | strcpy(buf, ""); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 115 | |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 116 | if (ISSET(USE_REGEXP) && ISSET(CASE_SENSITIVE)) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 117 | prompt = _("Case Sensitive Regexp Search%s%s"); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 118 | else if (ISSET(USE_REGEXP)) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 119 | prompt = _("Regexp Search%s%s"); |
| 120 | else if (ISSET(CASE_SENSITIVE)) |
| 121 | prompt = _("Case Sensitive Search%s%s"); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 122 | else |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 123 | prompt = _("Search%s%s"); |
Chris Allegretta | a4d2162 | 2000-07-08 23:57:03 +0000 | [diff] [blame] | 124 | |
| 125 | if (replacing) |
| 126 | reprompt = _(" (to replace)"); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 127 | |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 128 | /* This is now one simple call. It just does a lot */ |
| 129 | i = statusq(0, replacing ? replace_list : whereis_list, |
| 130 | replacing ? REPLACE_LIST_LEN : WHEREIS_LIST_LEN, backupstring, |
Chris Allegretta | ef601bb | 2000-11-19 19:15:03 +0000 | [diff] [blame] | 131 | prompt, reprompt, buf); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 132 | |
| 133 | /* Cancel any search, or just return with no previous search */ |
| 134 | if ((i == -1) || (i < 0 && !last_search[0])) { |
| 135 | statusbar(_("Search Cancelled")); |
| 136 | reset_cursor(); |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 137 | free(backupstring); |
| 138 | backupstring = NULL; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 139 | return -1; |
| 140 | } else if (i == -2) { /* Same string */ |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 141 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 74bb31b | 2001-03-14 09:08:14 +0000 | [diff] [blame] | 142 | if (ISSET(USE_REGEXP)) { |
| 143 | |
| 144 | /* If we're in pico mode, answer is "", use last_search! */ |
| 145 | if (ISSET(PICO_MODE)) |
| 146 | regexp_init(last_search); |
| 147 | else |
| 148 | regexp_init(answer); |
| 149 | } |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 150 | #else |
| 151 | ; |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 152 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 153 | } else if (i == 0) { /* They entered something new */ |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 154 | #ifdef HAVE_REGEX_H |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 155 | if (ISSET(USE_REGEXP)) |
| 156 | regexp_init(answer); |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 157 | #endif |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 158 | free(backupstring); |
| 159 | backupstring = NULL; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 160 | last_replace[0] = '\0'; |
| 161 | } else if (i == NANO_CASE_KEY) { /* They want it case sensitive */ |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 162 | free(backupstring); |
| 163 | backupstring = NULL; |
| 164 | backupstring = mallocstrcpy(backupstring, answer); |
| 165 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 166 | if (ISSET(CASE_SENSITIVE)) |
| 167 | UNSET(CASE_SENSITIVE); |
| 168 | else |
| 169 | SET(CASE_SENSITIVE); |
| 170 | |
| 171 | return 1; |
| 172 | } else if (i == NANO_OTHERSEARCH_KEY) { |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 173 | backupstring = mallocstrcpy(backupstring, answer); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 174 | return -2; /* Call the opposite search function */ |
Chris Allegretta | 8c2b40f | 2000-06-29 01:30:04 +0000 | [diff] [blame] | 175 | } else if (i == NANO_FROMSEARCHTOGOTO_KEY) { |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 176 | free(backupstring); |
| 177 | backupstring = NULL; |
Chris Allegretta | 8c2b40f | 2000-06-29 01:30:04 +0000 | [diff] [blame] | 178 | do_gotoline_void(); |
| 179 | return -3; |
Chris Allegretta | 105da33 | 2000-10-31 05:10:10 +0000 | [diff] [blame] | 180 | } else { /* First line key, etc. */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 181 | do_early_abort(); |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 182 | free(backupstring); |
| 183 | backupstring = NULL; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 184 | return -3; |
| 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 190 | void not_found_msg(char *str) |
| 191 | { |
Chris Allegretta | cbb0f8b | 2000-11-15 03:50:02 +0000 | [diff] [blame] | 192 | if (strlen(str) <= COLS / 2) |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 193 | statusbar(_("\"%s\" not found"), str); |
| 194 | else { |
Chris Allegretta | cbb0f8b | 2000-11-15 03:50:02 +0000 | [diff] [blame] | 195 | char *foo = NULL; |
| 196 | |
| 197 | foo = mallocstrcpy(foo, str); |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 198 | foo[COLS / 2] = 0; |
| 199 | statusbar(_("\"%s...\" not found"), foo); |
Chris Allegretta | cbb0f8b | 2000-11-15 03:50:02 +0000 | [diff] [blame] | 200 | |
| 201 | free(foo); |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 205 | filestruct *findnextstr(int quiet, filestruct * begin, int beginx, |
| 206 | char *needle) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 207 | { |
| 208 | filestruct *fileptr; |
| 209 | char *searchstr, *found = NULL, *tmp; |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 210 | int past_editbot = 0, current_x_find; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 211 | |
Chris Allegretta | b9bfc9b | 2000-09-10 05:06:09 +0000 | [diff] [blame] | 212 | fileptr = current; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 213 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 214 | current_x_find = current_x + 1; |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 215 | |
| 216 | /* Are we searching the last line? (i.e. the line where search started) */ |
Chris Allegretta | 1cc29f6 | 2000-11-20 01:29:37 +0000 | [diff] [blame] | 217 | if ((fileptr == begin) && (current_x_find < beginx)) |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 218 | search_last_line = 1; |
| 219 | |
| 220 | /* Make sure we haven't passed the end of the string */ |
Chris Allegretta | 1cc29f6 | 2000-11-20 01:29:37 +0000 | [diff] [blame] | 221 | if (strlen(fileptr->data) < current_x_find) |
| 222 | current_x_find--; |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 223 | |
Chris Allegretta | 1cc29f6 | 2000-11-20 01:29:37 +0000 | [diff] [blame] | 224 | searchstr = &fileptr->data[current_x_find]; |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 225 | |
| 226 | /* Look for needle in searchstr */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 227 | while ((found = strstrwrapper(searchstr, needle)) == NULL) { |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 228 | |
| 229 | /* finished processing file, get out */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 230 | if (search_last_line) { |
Chris Allegretta | 1cc29f6 | 2000-11-20 01:29:37 +0000 | [diff] [blame] | 231 | if (!quiet) |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 232 | not_found_msg(needle); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 233 | return NULL; |
| 234 | } |
Adam Rogoyski | 2a4ef92 | 2000-07-09 00:15:11 +0000 | [diff] [blame] | 235 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 236 | fileptr = fileptr->next; |
| 237 | |
Adam Rogoyski | a966d99 | 2000-07-09 18:42:53 +0000 | [diff] [blame] | 238 | if (!past_editbot && (fileptr == editbot)) |
| 239 | past_editbot = 1; |
Adam Rogoyski | 2a4ef92 | 2000-07-09 00:15:11 +0000 | [diff] [blame] | 240 | |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 241 | /* EOF reached, wrap around once */ |
| 242 | if (fileptr == NULL) { |
| 243 | fileptr = fileage; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 244 | |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 245 | past_editbot = 1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 246 | |
| 247 | if (!quiet) |
| 248 | statusbar(_("Search Wrapped")); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* Original start line reached */ |
| 252 | if (fileptr == begin) |
| 253 | search_last_line = 1; |
| 254 | |
| 255 | searchstr = fileptr->data; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 258 | /* We found an instance */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 259 | current_x_find = 0; |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 260 | for (tmp = fileptr->data; tmp != found; tmp++) |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 261 | current_x_find++; |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 262 | |
| 263 | /* Ensure we haven't wrap around again! */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 264 | if ((search_last_line) && (current_x_find >= beginx)) { |
Chris Allegretta | 1cc29f6 | 2000-11-20 01:29:37 +0000 | [diff] [blame] | 265 | if (!quiet) |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 266 | not_found_msg(needle); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 267 | return NULL; |
| 268 | } |
| 269 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 270 | /* Set globals now that we are sure we found something */ |
| 271 | current = fileptr; |
| 272 | current_x = current_x_find; |
| 273 | |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 274 | if (past_editbot) |
| 275 | edit_update(fileptr, CENTER); |
| 276 | else |
| 277 | update_line(current, current_x); |
| 278 | |
| 279 | placewewant = xplustabs(); |
| 280 | reset_cursor(); |
| 281 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 282 | return fileptr; |
| 283 | } |
| 284 | |
| 285 | void search_abort(void) |
| 286 | { |
| 287 | UNSET(KEEP_CUTBUFFER); |
| 288 | display_main_list(); |
| 289 | wrefresh(bottomwin); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 290 | if (ISSET(MARK_ISSET)) |
Chris Allegretta | f1d33d3 | 2000-08-19 03:53:39 +0000 | [diff] [blame] | 291 | edit_refresh_clearok(); |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 292 | |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 293 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 294 | if (ISSET(REGEXP_COMPILED)) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 295 | regexp_cleanup(); |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 296 | #endif |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Search for a string */ |
| 300 | int do_search(void) |
| 301 | { |
| 302 | int i; |
| 303 | filestruct *fileptr = current; |
| 304 | |
| 305 | wrap_reset(); |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 306 | i = search_init(0); |
| 307 | switch (i) { |
| 308 | case -1: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 309 | current = fileptr; |
| 310 | search_abort(); |
| 311 | return 0; |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 312 | case -3: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 313 | search_abort(); |
| 314 | return 0; |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 315 | case -2: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 316 | do_replace(); |
| 317 | return 0; |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 318 | case 1: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 319 | do_search(); |
| 320 | search_abort(); |
| 321 | return 1; |
| 322 | } |
Chris Allegretta | 86f8b3a | 2000-10-31 05:28:33 +0000 | [diff] [blame] | 323 | |
| 324 | /* The sneaky user deleted the previous search string */ |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 325 | if (!ISSET(PICO_MODE) && !strcmp(answer, "")) { |
Jordi Mallach | e7a647c | 2000-11-01 18:43:21 +0000 | [diff] [blame] | 326 | statusbar(_("Search Cancelled")); |
Chris Allegretta | 86f8b3a | 2000-10-31 05:28:33 +0000 | [diff] [blame] | 327 | search_abort(); |
| 328 | return 0; |
| 329 | } |
| 330 | |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 331 | /* If answer is now == "", then PICO_MODE is set. So, copy |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 332 | last_search into answer... */ |
| 333 | |
| 334 | if (!strcmp(answer, "")) |
| 335 | answer = mallocstrcpy(answer, last_search); |
| 336 | else |
| 337 | last_search = mallocstrcpy(last_search, answer); |
| 338 | |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 339 | search_last_line = 0; |
| 340 | findnextstr(0, current, current_x, answer); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 341 | search_abort(); |
| 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | void print_replaced(int num) |
| 346 | { |
| 347 | if (num > 1) |
Jordi Mallach | 5b387d7 | 2001-02-20 12:45:47 +0000 | [diff] [blame] | 348 | statusbar(_("Replaced %d occurrences"), num); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 349 | else if (num == 1) |
Jordi Mallach | 5b387d7 | 2001-02-20 12:45:47 +0000 | [diff] [blame] | 350 | statusbar(_("Replaced 1 occurrence")); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void replace_abort(void) |
| 354 | { |
Chris Allegretta | 18bd029 | 2000-07-28 01:18:10 +0000 | [diff] [blame] | 355 | /* Identicle to search_abort, so we'll call it here. If it |
| 356 | does something different later, we can change it back. For now |
| 357 | it's just a waste to duplicat code */ |
| 358 | search_abort(); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 359 | placewewant = xplustabs(); |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 362 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 363 | int replace_regexp(char *string, int create_flag) |
| 364 | { |
| 365 | /* split personality here - if create_flag is null, just calculate |
| 366 | * the size of the replacement line (necessary because of |
| 367 | * subexpressions like \1 \2 \3 in the replaced text) */ |
| 368 | |
| 369 | char *c; |
| 370 | int new_size = strlen(current->data) + 1; |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 371 | int search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 372 | |
| 373 | new_size -= search_match_count; |
| 374 | |
| 375 | /* Iterate through the replacement text to handle |
| 376 | * subexpression replacement using \1, \2, \3, etc */ |
| 377 | |
| 378 | c = last_replace; |
| 379 | while (*c) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 380 | if (*c != '\\') { |
| 381 | if (create_flag) |
| 382 | *string++ = *c; |
| 383 | c++; |
| 384 | new_size++; |
| 385 | } else { |
| 386 | int num = (int) *(c + 1) - (int) '0'; |
| 387 | if (num >= 1 && num <= 9) { |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 388 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 389 | int i = regmatches[num].rm_so; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 390 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 391 | if (num > search_regexp.re_nsub) { |
| 392 | /* Ugh, they specified a subexpression that doesn't |
| 393 | exist. */ |
| 394 | return -1; |
| 395 | } |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 396 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 397 | /* Skip over the replacement expression */ |
| 398 | c += 2; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 399 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 400 | /* But add the length of the subexpression to new_size */ |
| 401 | new_size += regmatches[num].rm_eo - regmatches[num].rm_so; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 402 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 403 | /* And if create_flag is set, append the result of the |
| 404 | * subexpression match to the new line */ |
| 405 | while (create_flag && i < regmatches[num].rm_eo) |
| 406 | *string++ = *(current->data + i++); |
| 407 | |
| 408 | } else { |
| 409 | if (create_flag) |
| 410 | *string++ = *c; |
| 411 | c++; |
| 412 | new_size++; |
| 413 | } |
| 414 | } |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (create_flag) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 418 | *string = 0; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 419 | |
| 420 | return new_size; |
| 421 | } |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 422 | #endif |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 423 | |
Chris Allegretta | b46df99 | 2000-11-01 02:12:13 +0000 | [diff] [blame] | 424 | char *replace_line(void) |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 425 | { |
| 426 | char *copy, *tmp; |
| 427 | int new_line_size; |
| 428 | int search_match_count; |
| 429 | |
| 430 | /* Calculate size of new line */ |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 431 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 432 | if (ISSET(USE_REGEXP)) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 433 | search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so; |
| 434 | new_line_size = replace_regexp(NULL, 0); |
| 435 | /* If they specified an invalid subexpression in the replace |
| 436 | * text, return NULL indicating an error */ |
| 437 | if (new_line_size < 0) |
| 438 | return NULL; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 439 | } else { |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 440 | #else |
| 441 | { |
| 442 | #endif |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 443 | search_match_count = strlen(last_search); |
| 444 | new_line_size = strlen(current->data) - strlen(last_search) + |
| 445 | strlen(last_replace) + 1; |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 446 | } |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 447 | |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 448 | /* Create buffer */ |
| 449 | copy = nmalloc(new_line_size); |
| 450 | |
| 451 | /* Head of Original Line */ |
| 452 | strncpy(copy, current->data, current_x); |
| 453 | copy[current_x] = 0; |
| 454 | |
| 455 | /* Replacement Text */ |
| 456 | if (!ISSET(USE_REGEXP)) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 457 | strcat(copy, last_replace); |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 458 | #ifdef HAVE_REGEX_H |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 459 | else |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 460 | (void) replace_regexp(copy + current_x, 1); |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 461 | #endif |
Chris Allegretta | 9fc8d43 | 2000-07-07 01:49:52 +0000 | [diff] [blame] | 462 | |
| 463 | /* The tail of the original line */ |
| 464 | /* This may expose other bugs, because it no longer |
| 465 | goes through each character on the string |
| 466 | and tests for string goodness. But because |
| 467 | we can assume the invariant that current->data |
| 468 | is less than current_x + strlen(last_search) long, |
| 469 | this should be safe. Or it will expose bugs ;-) */ |
| 470 | tmp = current->data + current_x + search_match_count; |
| 471 | strcat(copy, tmp); |
| 472 | |
| 473 | return copy; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 476 | /* step through each replace word and prompt user before replacing word */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 477 | int do_replace_loop(char *prevanswer, filestruct *begin, int *beginx, |
| 478 | int wholewords, int *i) |
| 479 | { |
| 480 | int replaceall = 0, numreplaced = 0; |
| 481 | filestruct *fileptr; |
| 482 | char *copy; |
| 483 | |
| 484 | switch (*i) { |
| 485 | case -1: /* Aborted enter */ |
| 486 | if (strcmp(last_replace, "")) |
| 487 | answer = mallocstrcpy(answer, last_replace); |
| 488 | statusbar(_("Replace Cancelled")); |
| 489 | replace_abort(); |
| 490 | return 0; |
| 491 | case 0: /* They actually entered something */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 492 | break; |
| 493 | default: |
| 494 | if (*i != -2) { /* First page, last page, for example |
| 495 | could get here */ |
| 496 | do_early_abort(); |
| 497 | replace_abort(); |
| 498 | return 0; |
| 499 | } |
| 500 | } |
| 501 | |
Chris Allegretta | c793c43 | 2000-11-21 12:52:55 +0000 | [diff] [blame] | 502 | if (ISSET(PICO_MODE) && !strcmp(answer, "")) |
| 503 | answer = mallocstrcpy(answer, last_replace); |
| 504 | |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 505 | last_replace = mallocstrcpy(last_replace, answer); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 506 | while (1) { |
| 507 | |
| 508 | /* Sweet optimization by Rocco here */ |
| 509 | fileptr = findnextstr(replaceall, begin, *beginx, prevanswer); |
| 510 | |
| 511 | /* No more matches. Done! */ |
Chris Allegretta | 022b96f | 2000-11-14 17:47:58 +0000 | [diff] [blame] | 512 | if (!fileptr) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 513 | break; |
| 514 | |
| 515 | /* Make sure only wholewords are found */ |
| 516 | if (wholewords) |
| 517 | { |
| 518 | /* start of line or previous character not a letter */ |
Chris Allegretta | 63a89d3 | 2000-11-18 03:05:50 +0000 | [diff] [blame] | 519 | if ((current_x == 0) || (!isalpha((int) fileptr->data[current_x-1]))) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 520 | { |
| 521 | /* end of line or next character not a letter */ |
| 522 | if (((current_x + strlen(prevanswer)) == strlen(fileptr->data)) |
Chris Allegretta | 63a89d3 | 2000-11-18 03:05:50 +0000 | [diff] [blame] | 523 | || (!isalpha((int) fileptr->data[current_x + strlen(prevanswer)]))) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 524 | ; |
| 525 | else |
| 526 | continue; |
| 527 | } |
| 528 | else |
| 529 | continue; |
| 530 | } |
| 531 | |
| 532 | /* If we're here, we've found the search string */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 533 | if (!replaceall) { |
| 534 | |
| 535 | curs_set(0); |
| 536 | do_replace_highlight(TRUE, prevanswer); |
| 537 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 538 | *i = do_yesno(1, 1, _("Replace this instance?")); |
| 539 | |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 540 | do_replace_highlight(FALSE, prevanswer); |
| 541 | curs_set(1); |
| 542 | } |
| 543 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 544 | if (*i > 0 || replaceall) { /* Yes, replace it!!!! */ |
| 545 | if (*i == 2) |
| 546 | replaceall = 1; |
| 547 | |
| 548 | copy = replace_line(); |
| 549 | if (!copy) { |
| 550 | statusbar(_("Replace failed: unknown subexpression!")); |
| 551 | replace_abort(); |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | /* Cleanup */ |
| 556 | free(current->data); |
| 557 | current->data = copy; |
| 558 | |
| 559 | /* Stop bug where we replace a substring of the replacement text */ |
| 560 | current_x += strlen(last_replace) - 1; |
| 561 | |
| 562 | /* Adjust the original cursor position - COULD BE IMPROVED */ |
| 563 | if (search_last_line) { |
| 564 | *beginx += strlen(last_replace) - strlen(last_search); |
| 565 | |
| 566 | /* For strings that cross the search start/end boundary */ |
| 567 | /* Don't go outside of allocated memory */ |
| 568 | if (*beginx < 1) |
| 569 | *beginx = 1; |
| 570 | } |
| 571 | |
| 572 | edit_refresh(); |
| 573 | set_modified(); |
| 574 | numreplaced++; |
| 575 | } else if (*i == -1) /* Abort, else do nothing and continue loop */ |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | return numreplaced; |
| 580 | } |
| 581 | |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 582 | /* Replace a string */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 583 | int do_replace(void) |
| 584 | { |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 585 | int i, numreplaced, beginx; |
| 586 | filestruct *begin; |
| 587 | char *prevanswer = NULL, *buf = NULL; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 588 | |
Chris Allegretta | 5050aa6 | 2001-04-22 07:10:21 +0000 | [diff] [blame^] | 589 | if (ISSET(VIEW_MODE)) { |
| 590 | print_view_warning(); |
| 591 | replace_abort(); |
| 592 | return 0; |
| 593 | } |
| 594 | |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 595 | i = search_init(1); |
| 596 | switch (i) { |
| 597 | case -1: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 598 | statusbar(_("Replace Cancelled")); |
| 599 | replace_abort(); |
| 600 | return 0; |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 601 | case 1: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 602 | do_replace(); |
| 603 | return 1; |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 604 | case -2: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 605 | do_search(); |
| 606 | return 0; |
Chris Allegretta | 9c371a2 | 2000-10-27 05:48:05 +0000 | [diff] [blame] | 607 | case -3: |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 608 | replace_abort(); |
| 609 | return 0; |
| 610 | } |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 611 | |
Chris Allegretta | 86f8b3a | 2000-10-31 05:28:33 +0000 | [diff] [blame] | 612 | /* Again, there was a previous string but they deleted it and hit enter */ |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 613 | if (!ISSET(PICO_MODE) && !strcmp(answer, "")) { |
Chris Allegretta | 86f8b3a | 2000-10-31 05:28:33 +0000 | [diff] [blame] | 614 | statusbar(_("Replace Cancelled")); |
| 615 | replace_abort(); |
| 616 | return 0; |
| 617 | } |
| 618 | |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 619 | /* If answer is now == "", then PICO_MODE is set. So, copy |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 620 | last_search into answer (and prevanswer)... */ |
| 621 | if (!strcmp(answer, "")) { |
| 622 | answer = mallocstrcpy(answer, last_search); |
| 623 | prevanswer = mallocstrcpy(prevanswer, last_search); |
| 624 | } else { |
| 625 | last_search = mallocstrcpy(last_search, answer); |
| 626 | prevanswer = mallocstrcpy(prevanswer, answer); |
| 627 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 628 | |
Chris Allegretta | bf9a8cc | 2000-11-17 01:37:39 +0000 | [diff] [blame] | 629 | if (ISSET(PICO_MODE)) { |
Chris Allegretta | 71844ba | 2000-11-03 14:23:00 +0000 | [diff] [blame] | 630 | buf = nmalloc(strlen(last_replace) + 5); |
| 631 | if (strcmp(last_replace, "")) { |
| 632 | if (strlen(last_replace) > (COLS / 3)) { |
| 633 | strncpy(buf, last_replace, COLS / 3); |
| 634 | sprintf(&buf[COLS / 3 - 1], "..."); |
| 635 | } else |
| 636 | sprintf(buf, "%s", last_replace); |
| 637 | |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 638 | i = statusq(0, replace_list_2, REPLACE_LIST_2_LEN, "", |
Chris Allegretta | 71844ba | 2000-11-03 14:23:00 +0000 | [diff] [blame] | 639 | _("Replace with [%s]"), buf); |
| 640 | } |
| 641 | else |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 642 | i = statusq(0, replace_list_2, REPLACE_LIST_2_LEN, "", |
Chris Allegretta | 71844ba | 2000-11-03 14:23:00 +0000 | [diff] [blame] | 643 | _("Replace with")); |
| 644 | } |
| 645 | else |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 646 | i = statusq(0, replace_list_2, REPLACE_LIST_2_LEN, last_replace, |
Chris Allegretta | 105da33 | 2000-10-31 05:10:10 +0000 | [diff] [blame] | 647 | _("Replace with")); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 648 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 649 | /* save where we are */ |
| 650 | begin = current; |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 651 | beginx = current_x + 1; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 652 | |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 653 | search_last_line = 0; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 654 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 655 | numreplaced = do_replace_loop(prevanswer, begin, &beginx, FALSE, &i); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 656 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 657 | /* restore where we were */ |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 658 | current = begin; |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 659 | current_x = beginx - 1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 660 | renumber_all(); |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 661 | edit_update(current, CENTER); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 662 | print_replaced(numreplaced); |
| 663 | replace_abort(); |
| 664 | return 1; |
| 665 | } |
| 666 | |
| 667 | void goto_abort(void) |
| 668 | { |
| 669 | UNSET(KEEP_CUTBUFFER); |
| 670 | display_main_list(); |
| 671 | } |
| 672 | |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 673 | int do_gotoline(long line) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 674 | { |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 675 | long i = 1; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 676 | |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 677 | if (line <= 0) { /* Ask for it */ |
| 678 | |
| 679 | long j = 0; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 680 | |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 681 | j = statusq(0, goto_list, GOTO_LIST_LEN, "", _("Enter line number")); |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 682 | if (j != 0) { |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 683 | statusbar(_("Aborted")); |
| 684 | goto_abort(); |
| 685 | return 0; |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | line = atoi(answer); |
| 689 | |
| 690 | /* Bounds check */ |
| 691 | if (line <= 0) { |
| 692 | statusbar(_("Come on, be reasonable")); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 693 | goto_abort(); |
| 694 | return 0; |
| 695 | } |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 698 | for (current = fileage; ((current->next != NULL) && (i < line)); i++) |
| 699 | current = current->next; |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 700 | |
Rocco Corsi | 4dfaf93 | 2001-04-20 01:59:55 +0000 | [diff] [blame] | 701 | current_x = 0; |
| 702 | edit_update(current, CENTER); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 703 | |
| 704 | goto_abort(); |
| 705 | return 1; |
| 706 | } |
| 707 | |
| 708 | int do_gotoline_void(void) |
| 709 | { |
| 710 | return do_gotoline(0); |
| 711 | } |