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

Benjamin Valentin via "Development discussions." <[email protected]>
Newsgroups gmane.editors.nano.devel
Message-ID <[email protected]>
Many development tools (e.g. GCC, addr2line, grep -n, assert/log messages)
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 as it can be done with e.g. Sublime Text, VSCode or Geany.

v4: Squashed two bugs - thank you for spotting those!

Best,
Benjamin
0001-startup-add-support-for-file-line-format.patch (text/x-patch, 2 KB)
From 8d11aa424a58731b331f3af0d25a8556617b9595 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.

Signed-off-by: Benjamin Valentin <[email protected]>
---
 src/nano.c  | 29 +++++++++++++++++++++++++++--
 src/utils.c |  2 +-
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/nano.c b/src/nano.c
index 4d5e7ea6..a4c4a1d9 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2482,8 +2482,33 @@ int main(int argc, char **argv)
 				continue;
 		} else
 #endif
-		 if (!open_buffer(argv[optind++], TRUE))
-			continue;
+		{
+			char *filename = argv[optind++];
+			char *colon = filename + (*filename ? 1 : 0);
+
+			/* Search for : to open file on a specific line. */
+			while ((colon = strchr(colon, ':'))) {
+
+				/* 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 + 1);
+					continue;
+				}
+
+				/* If parsing succeeds, cut of the line suffix. */
+				if (parse_line_column(colon + 1, &givenline, &givencol)) {
+					*colon = 0;
+					break;
+				}
+
+				/* Parsing failed, try to find another : */
+				++colon;
+			}
+
+			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.