RFC [PATCH 1/3] files: when a filename with a colon and digits exists, open that file
Benno Schulenberg <[email protected]>
| Newsgroups | gmane.editors.nano.devel |
|---|---|
| Message-ID | <[email protected]> |
When the user specifies, on the command line, a filename that ends with a colon plus digits, and that filename exists in the file system, then open that file, instead of interpreting the digits as a line number. Also, if the filename stripped of the colon plus digits does not exist, then do not interpret the digits as a line number either but treat them as part of the file name. Before this change, the user would have to escape the colon whenever they wanted to open a file whose name ended with a colon plus digits. Now the user needs to escape the colon only when 'foo' exists and they want to create, say, 'foo:24'. Problem-was-reported-by: Ralph Corderoy <[email protected]> https://lists.gnu.org/archive/html/nano-devel/2024-05/msg00001.html Mitigation-was-suggested-by: Mike Scalora <[email protected]> https://lists.gnu.org/archive/html/nano-devel/2024-05/msg00008.html --- src/nano.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/nano.c b/src/nano.c index 973054f0..0467504f 100644 --- a/src/nano.c +++ b/src/nano.c @@ -2495,19 +2495,24 @@ int main(int argc, char **argv) { char *filename = argv[optind++]; char *colon = filename + (*filename ? 1 : 0); + struct stat fileinfo; /* Search the filename for a colon. If the colon is preceded by * a backslash, elide the backslash and skip the colon. If there * is a valid number after the colon, chop colon and number off. * The number is later used to place the cursor on that line. */ + if (strchr(filename, ':') && stat(filename, &fileinfo) < 0) { while ((colon = strchr(colon, ':'))) { if (*(colon - 1) == '\\') memmove(colon - 1, colon, strlen(colon) + 1); - else if (parse_line_column(colon + 1, &givenline, &givencol)) + else if (parse_line_column(colon + 1, &givenline, &givencol)) { *colon = '\0'; - else + if (stat(filename, &fileinfo) < 0) + *colon++ = ':'; + } else ++colon; } + } if (!open_buffer(filename, TRUE)) continue; -- 2.42.1