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