[PATCH] startup: add support for <file>:<line> format

Benjamin Valentin <[email protected]>
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
Many tools (e.g. GCC) will output files with line numbers in the format 
<file>:<line>:<column>.
Support this format in addition to the +<line>,<column> format when 
opening files.

This makes it much easier to copy the file name from a compile error or 
failed assertion into a

nano command line an open the file at the right place.

Best,

Benjamin
0001-startup-add-support-for-file-row-format.patch (text/x-patch, 1.9 KB)
From ec36727bc31eb630c19794817f274a6183117ea6 Mon Sep 17 00:00:00 2001
From: Benjamin Valentin <[email protected]>
Date: Sun, 29 Jan 2023 23:43:00 +0100
Subject: [PATCH] startup: add support for <file>:<line> format

Many tools will output files with line numbers in the format <file>:<line>:<column>.
Support this format in addition to the +<line>,<column> format when opening files.
---
 src/nano.c  | 26 ++++++++++++++++++++++++--
 src/utils.c |  2 +-
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/nano.c b/src/nano.c
index c6db6dd3..86bcc7cb 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2482,8 +2482,30 @@ int main(int argc, char **argv)
 				continue;
 		} else
 #endif
-		 if (!open_buffer(argv[optind++], TRUE))
-			continue;
+		{
+			char *filename = argv[optind++];
+
+			/* search for : to open file on a specific line , ignore if filename starts with : */
+			char *colon = filename;
+			while ((colon = strchr(colon, ':')) && colon != filename) {
+
+				/* If : is escaped with \, unescape it and search for the next occurrence */
+				if (*(colon - 1) == '\\') {
+					size_t len = strlen(colon);
+					memmove(colon - 1, colon, len);
+					colon[len - 1] = 0;
+					continue;
+				}
+
+				/* if parsing succeeds, cut of the line suffix */
+				if (parse_line_column(colon + 1, &givenline, &givencol))
+					*colon = 0;
+				break;
+			}
+
+			if (!open_buffer(filename, TRUE))
+				continue;
+		}
 
 		/* If a position was given on the command line, go there. */
 		if (givenline != 0 || givencol != 0)
diff --git a/src/utils.c b/src/utils.c
index 18c6e35c..3be197e2 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -137,7 +137,7 @@ bool parse_line_column(const char *str, ssize_t *line, ssize_t *column)
 	while (*str == ' ')
 		str++;
 
-	comma = strpbrk(str, "m,. /;");
+	comma = strpbrk(str, "m,. /;:");
 
 	if (comma == NULL)
 		return parse_num(str, line);
-- 
2.37.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.