files: when a filename with a colon and digits exists, open that file

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 <ralph@inputplus.co.uk>
  https://lists.gnu.org/archive/html/nano-devel/2024-05/msg00001.html
Mitigation-was-suggested-by: Mike Scalora <mike@scalora.org>
  https://lists.gnu.org/archive/html/nano-devel/2024-05/msg00008.html
This commit is contained in:
Benno Schulenberg 2024-05-10 10:44:28 +02:00
parent 803a16cbcd
commit 54c8cb8c81

View File

@ -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;