[patch] Invoking mcedit with filename:lineno



Hi,

many programs output warnings and errors in the format
filename:lineno[:]. The attached patch allows users to just copy and
paste these locations and invoke mcedit with them. For example:

$ mcedit main.c:2000

The patch maintains the usual behavior as much as possible. That is, the
new format is only used when the file "main.c:2000" doesn't exist, but
"main.c" exists.

Opinions?

Roland
Index: doc/mcedit.1.in
===================================================================
RCS file: /cvsroot/mc/mc/doc/mcedit.1.in,v
retrieving revision 1.22
diff -u -p -r1.22 mcedit.1.in
--- doc/mcedit.1.in	24 Sep 2007 12:50:37 -0000	1.22
+++ doc/mcedit.1.in	9 Dec 2008 12:44:12 -0000
@@ -3,7 +3,10 @@
 mcedit \- Internal file editor of GNU Midnight Commander.
 .SH USAGE
 .B mcedit
-[\-bcCdfhstVx?] [+number] file
+[\-bcCdfhstVx?] [+lineno] file
+.PP
+.B mcedit
+[\-bcCdfhstVx?] file:lineno[:]
 .SH DESCRIPTION
 .LP
 mcedit is a link to
@@ -18,8 +21,8 @@ version of
 \- standalone editor for X Window System.
 .SH OPTIONS
 .TP
-.I "+number"
-Go  to the line specified by number (do not put a space between the
+.I "+lineno"
+Go to the line specified by number (do not put a space between the
 .I "+"
 sign and the number).
 .TP
Index: src/main.c
===================================================================
RCS file: /cvsroot/mc/mc/src/main.c,v
retrieving revision 1.370
diff -u -p -r1.370 main.c
--- src/main.c	25 Sep 2007 15:33:37 -0000	1.370
+++ src/main.c	9 Dec 2008 12:44:12 -0000
@@ -2055,17 +2055,41 @@ handle_args (int argc, char *argv[])
     if (!STRNCOMP (base, "mce", 3) || !STRCOMP (base, "vi")) {
 	edit_one_file = "";
 	if (tmp) {
-	    if (*tmp == '+' && isdigit ((unsigned char) tmp[1])) {
-		int start_line = atoi (tmp);
-		if (start_line > 0) {
-		    char *file = poptGetArg (ctx);
-		    if (file) {
-			tmp = file;
-			edit_one_file_start_line = start_line;
+	    /*
+	     * Check for filename:lineno, followed by an optional colon.
+	     * This format is used by many programs (especially compilers)
+	     * in error messages and warnings. It is supported so that
+	     * users can quickly copy and paste file locations.
+	     */
+	    char *end = tmp + strlen (tmp), *p = end;
+	    if (p > tmp && p[-1] == ':')
+		p--;
+	    while (p > tmp && isdigit ((unsigned char) p[-1]))
+		p--;
+	    if (tmp < p && p < end && p[-1] == ':') {
+	        struct stat st;
+		gchar *fname = g_strndup (tmp, p - 1 - tmp);
+		if (mc_stat (tmp, &st) == -1 && mc_stat (fname, &st) != -1) {
+		    edit_one_file = fname;
+		    edit_one_file_start_line = atoi (p);
+		} else {
+		    g_free (fname);
+		    goto try_plus_filename;
+		}
+	    } else {
+	    try_plus_filename:
+		if (*tmp == '+' && isdigit ((unsigned char) tmp[1])) {
+		    int start_line = atoi (tmp);
+		    if (start_line > 0) {
+			char *file = poptGetArg (ctx);
+			if (file) {
+			    tmp = file;
+			    edit_one_file_start_line = start_line;
+			}
 		    }
 		}
+		edit_one_file = g_strdup (tmp);
 	    }
-	    edit_one_file = g_strdup (tmp);
 	}
     } else if (!STRNCOMP (base, "mcv", 3) || !STRCOMP (base, "view")) {
 	if (tmp)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]