Re: lines and regexp
Benno Schulenberg <[email protected]>
| Newsgroups | gmane.editors.nano.devel |
|---|---|
| Message-ID | <[email protected]> |
Op 07-09-2022 om 18:57 schreef Mike Scalora: > I noticed something curious about lines and regexp search and replace in nano. If > I open a file with two lines which nano reports the number of lines on open and > search and "replace all" on the regexp $ (or ^), I get 3 replacements. It doesn't > matter if the second line in the file has a newline or not, I get the same result > because nano adds the second newline automatically. This contradicts nano's > reporting of the number of lines read and adds yet another line to the file. This latter thing is why 'nonewlines' became the default in nano-4.0. (But it was quickly reverted in nano-4.1, because wanting to add something at the end of the file had become a nuisance: after M-/ one had to type <End> and <Enter> first.) The user can easily prevent the unexpected replacement and the addition of the extra new line by specifying 'set nonewlines' in their ~/.nanorc. But using this option results in the mentioned nuisance, so I don't think most users would want that. Alternatively, we could change nano's behavior as in the attached patch, that prevents any non-explicit replacement on the magic line. Opinions? > ==$ nano /tmp/test.txt > ^w^r$<return><space>pie<return>a^s^x Apparently you have 'set regexp' in your ~/.nanorc. I have not, and start a replacement session always with M-R (not ^W^R), followed by another M-R when I want to do a regex replacement. > Idea: Remove the trailing newline during the regexp operation. The patch prevents the replacement on the magic line and thus prevents the addition of a new magic line. > I didn't see a way to actually search for a newline There is none. Section 3.6 in the info manual (`info nano edit search`) mentions: "A regular expression in a search string always covers just one line". Suggestions on how to improve the wording in this section are welcome. Benno
prevent-replacement-on-the-magicline.patch
(text/x-patch, 473 B)
diff --git a/src/search.c b/src/search.c
index d759668b..09d68a38 100644
--- a/src/search.c
+++ b/src/search.c
@@ -606,7 +606,8 @@ ssize_t do_replace_loop(const char *needle, bool whole_word_only,
skipone = (choice == 0 || ISSET(BACKWARDS_SEARCH));
}
- if (choice == YES || replaceall) {
+ if (choice == YES || (replaceall && (openfile->current->next ||
+ openfile->current->data[0] || ISSET(NO_NEWLINES)))) {
size_t length_change;
char *altered;
OpenPGP_signature
(application/pgp-signature, 840 B) - not displayed