From e2eff00a9c0d89a196bb297b4958473a681ddfee Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 30 Dec 2024 05:00:44 +0300 Subject: support +line[,column] and filename:line:column syntaxes when opening files * This is done via the new opener.tes in the standard library. * Some programs that use $EDITOR expect the +line syntax to work. * You can copy filename:line:column directly from GCC error messages and filename:line from grep output. * Since there may be safe file names beginning with "+" or containing colons, there needs to be a way to turn this off, especially for scripts that don't know anything about the filenames to open. This is done with "--". Unfortunately, the first "--", that stops parameter processing, is always removed from the command line and not passed down into TECO land. This is not a problem for stand-alone scripts, since the script filename is already stopping option processing, so "--" would get passed down. But when calling the profile via `sciteco -- ...`, you could prevent leading minus signs to cause problems but since the `--` is removed, opener.tes cannot use it as a hint. Therefore, we introduced `-S` as a new alternative to `--`, that's always passed down as `--` (i.e. it is equivalent to "-- --"). In other words, `sciteco -S *` will always open exactly the specified files without any danger of misinterpreting certain file names. Should we ever switch to a custom option parsing algorithm, we might preserve "--" (unless after --mung) and thus get rid of "-S". * This advanced behavior can be tweaked by the user relatively easily. In the easiest case, we could replace M[opener] with <:L;R 0X.f [* @EB/^EN.f/ ]* L> in ~/.teco_ini to completely disable the special syntax. --- src/main.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index e93a3de..fb542e7 100644 --- a/src/main.c +++ b/src/main.c @@ -141,7 +141,7 @@ teco_process_options(gchar ***argv) g_autoptr(GError) error = NULL; - g_autoptr(GOptionContext) options = g_option_context_new("[--] [SCRIPT] [ARGUMENT...]"); + g_autoptr(GOptionContext) options = g_option_context_new("[--|-S] [SCRIPT] [ARGUMENT...]"); g_option_context_set_summary( options, @@ -168,27 +168,48 @@ teco_process_options(gchar ***argv) * in many situations. * It is also strictly required to make hash-bang lines like * #!/usr/bin/sciteco -m - * work. + * work (without additional --). */ g_option_context_set_strict_posix(options, TRUE); + /* + * The first unknown parameter will be left in argv and + * terminates option parsing (see above). + * This means we can use "-S" as an alternative to "--", + * that is always preserved and passed down to the macro. + */ + g_option_context_set_ignore_unknown_options(options, TRUE); + if (!g_option_context_parse_strv(options, argv, &error)) { g_fprintf(stderr, "Option parsing failed: %s\n", error->message); exit(EXIT_FAILURE); } - /* - * GOption will NOT remove "--" if followed by an - * option-argument, which may interfer with scripts - * doing their own option handling and interpreting "--". - * - * NOTE: This is still true if we're parsing in GNU-mode - * and "--" is not the first non-option argument as in - * sciteco foo -- -C bar. - */ - if ((*argv)[0] && !g_strcmp0((*argv)[1], "--")) + if ((*argv)[0] && !g_strcmp0((*argv)[1], "-S")) { + /* translate -S to --, this is always passed down */ + (*argv)[1][1] = '-'; + } else if ((*argv)[0] && !g_strcmp0((*argv)[1], "--")) { + /* + * GOption will NOT remove "--" if followed by an + * option-argument, which may interfer with scripts + * doing their own option handling and interpreting "--". + * Otherwise, GOption will always remove "--". + * + * NOTE: This is still true if we're parsing in GNU-mode + * and "--" is not the first non-option argument as in + * sciteco foo -- -C bar. + */ g_free(teco_strv_remove(*argv, 1)); + } else if ((*argv)[0] && (*argv)[1] && *(*argv)[1] == '-') { + /* + * GOption does not remove "--" if it is followed by "-", + * so if the first parameter starts with "-", we know it's + * not a known built-in parameter. + */ + g_fprintf(stderr, "Unknown option \"%s\"\n", (*argv)[1]); + exit(EXIT_FAILURE); + } gchar *mung_filename = NULL; -- cgit v1.2.3