the last search command in a fixed direction without prompting.
* src/global.c (shortcut_init): Tweak a string.
* src/search.c, src/move.c: Improve a few of the comments.
+ * src/search.c (replace_regexp, replace_line): Rename two variables,
+ and make the calculation of the new line size more symmetrical.
2015-07-25 Benno Schulenberg <bensberg@justemail.net>
* src/global.c (shortcut_init, strtosc), src/files.c (savefile),
#endif /* !NANO_TINY */
#ifdef HAVE_REGEX_H
-/* Calculate the size of the replacement line, taking possible
+/* Calculate the size of the replacement text, taking possible
* subexpressions \1 to \9 into account. Return the replacement
* text in the passed string only when create is TRUE. */
int replace_regexp(char *string, bool create)
{
const char *c = last_replace;
- size_t search_match_count = regmatches[0].rm_eo -
- regmatches[0].rm_so;
- size_t new_line_size = strlen(openfile->current->data) + 1 -
- search_match_count;
+ size_t replacement_size = 0;
/* Iterate through the replacement text to handle subexpression
* replacement using \1, \2, \3, etc. */
if (create)
*string++ = *c;
c++;
- new_line_size++;
+ replacement_size++;
} else {
size_t i = regmatches[num].rm_eo - regmatches[num].rm_so;
c += 2;
/* But add the length of the subexpression to new_size. */
- new_line_size += i;
+ replacement_size += i;
/* And if create is TRUE, append the result of the
* subexpression match to the new line. */
if (create)
*string = '\0';
- return new_line_size;
+ return replacement_size;
}
#endif /* HAVE_REGEX_H */
char *replace_line(const char *needle)
{
char *copy;
- size_t new_line_size, search_match_count;
+ size_t match_len;
+ size_t new_line_size = strlen(openfile->current->data) + 1;
- /* First calculate the size of the new line. */
+ /* First adjust the size of the new line for the change. */
#ifdef HAVE_REGEX_H
if (ISSET(USE_REGEXP)) {
- search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so;
- new_line_size = replace_regexp(NULL, FALSE);
+ match_len = regmatches[0].rm_eo - regmatches[0].rm_so;
+ new_line_size += replace_regexp(NULL, FALSE) - match_len;
} else {
#endif
- search_match_count = strlen(needle);
- new_line_size = strlen(openfile->current->data) -
- search_match_count + strlen(answer) + 1;
+ match_len = strlen(needle);
+ new_line_size += strlen(answer) - match_len;
#ifdef HAVE_REGEX_H
}
#endif
#endif
strcpy(copy + openfile->current_x, answer);
- assert(openfile->current_x + search_match_count <= strlen(openfile->current->data));
+ assert(openfile->current_x + match_len <= strlen(openfile->current->data));
/* Copy the tail of the original line. */
- strcat(copy, openfile->current->data + openfile->current_x +
- search_match_count);
+ strcat(copy, openfile->current->data + openfile->current_x + match_len);
return copy;
}