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

Benjamin Valentin <[email protected]>
Newsgroups gmane.editors.nano.devel
Message-ID <20230226015822.50d8c8e3@rechenknecht2k11>
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.

v2: on files that start with a ':', only that first ':' is ignored.

Best,
Benjamin
0001-startup-add-support-for-file-line-format.patch (text/x-patch, 1.9 KB)
From 2cfc4c2c046dea89acb620e013f35f3b9644fa78 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  | 32 ++++++++++++++++++++++++++++++--
 src/utils.c |  2 +-
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/src/nano.c b/src/nano.c
index 4d5e7ea6..bbc27e26 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -2482,8 +2482,36 @@ 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 */
+			char *colon = filename;
+			while ((colon = strchr(colon, ':'))) {
+
+				/* if file starts with : skip that */
+				if (colon == filename) {
+					++colon;
+					continue;
+				}
+
+				/* 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.