From 58a3293efc3fab5cdbd819c0bb798dc38bdff25f Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Mon, 21 Sep 2020 09:23:37 +0200 Subject: [PATCH] options: require --magic or 'set magic' to enable the use of libmagic Trying to determine which syntax to apply with the help of libmagic can be a somewhat time-consuming and yet often still fruitless affair. Therefore using libmagic should not be the default; it should require an option to tell nano to try using libmagic when it can't determine the applicable syntax from the file's name or first line. This is better than having a --nomagic option (and using libmagic by default), because when the user has 'set nomagic' in their nanorc or --nomagic in their alias, then switching the use of libmagic back on is troublesome. But with --magic (and not using libmagic by default), when nano fails to recognize a file, it is a simple ^X, add -! to the command line, and restart. The short form of the option is -! because we have run out of letters. This addresses https://savannah.gnu.org/bugs/?59028. --- src/color.c | 4 ++-- src/definitions.h | 3 ++- src/nano.c | 15 +++++++++++++-- src/rcfile.c | 3 +++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/color.c b/src/color.c index cc0a5f2d..2a0c0134 100644 --- a/src/color.c +++ b/src/color.c @@ -189,8 +189,8 @@ void find_and_prime_applicable_syntax(void) } #ifdef HAVE_LIBMAGIC - /* If we still don't have an answer, try using magic. */ - if (sntx == NULL && !inhelp) { + /* If we still don't have an answer, try using magic (when requested). */ + if (sntx == NULL && !inhelp && ISSET(USE_MAGIC)) { struct stat fileinfo; magic_t cookie = NULL; const char *magicstring = NULL; diff --git a/src/definitions.h b/src/definitions.h index 4b593624..a01dcd51 100644 --- a/src/definitions.h +++ b/src/definitions.h @@ -542,7 +542,8 @@ enum EMPTY_LINE, INDICATOR, BOOKSTYLE, - STATEFLAGS + STATEFLAGS, + USE_MAGIC }; /* Flags for the menus in which a given function should be present. */ diff --git a/src/nano.c b/src/nano.c index 49ef3775..a9a48e12 100644 --- a/src/nano.c +++ b/src/nano.c @@ -494,9 +494,12 @@ void usage(void) #endif /* TRANSLATORS: The next three are column headers of the --help output. */ print_opt(_("Option"), _("Long option"), N_("Meaning")); -#ifndef NANO_TINY +#ifdef HAVE_LIBMAGIC /* TRANSLATORS: The next forty or so strings are option descriptions * for the --help output. Try to keep them at most 40 characters. */ + print_opt("-!", "--magic", N_("Also try magic to determine syntax")); +#endif +#ifndef NANO_TINY print_opt("-%", "--stateflags", N_("Show some states on the title bar")); print_opt("-A", "--smarthome", N_("Enable smart home key")); if (!ISSET(RESTRICTED)) { @@ -1701,6 +1704,9 @@ int main(int argc, char **argv) /* Whether the quoting regex was compiled successfully. */ #endif const struct option long_options[] = { +#ifdef HAVE_LIBMAGIC + {"magic", 0, NULL, '!'}, +#endif {"boldtext", 0, NULL, 'D'}, #ifdef ENABLE_MULTIBUFFER {"multibuffer", 0, NULL, 'F'}, @@ -1841,9 +1847,14 @@ int main(int argc, char **argv) if (*(tail(argv[0])) == 'r') SET(RESTRICTED); - while ((optchr = getopt_long(argc, argv, "%ABC:DEFGHIJ:KLMNOPQ:RST:UVWX:Y:Z" + while ((optchr = getopt_long(argc, argv, "!%ABC:DEFGHIJ:KLMNOPQ:RST:UVWX:Y:Z" "abcdef:ghijklmno:pqr:s:tuvwxyz$", long_options, NULL)) != -1) { switch (optchr) { +#ifdef HAVE_LIBMAGIC + case '!': + SET(USE_MAGIC); + break; +#endif #ifndef NANO_TINY case '%': SET(STATEFLAGS); diff --git a/src/rcfile.c b/src/rcfile.c index 5d69d374..9ba3badb 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -56,6 +56,9 @@ static const rcoption rcopts[] = { #endif #ifdef ENABLE_LINENUMBERS {"linenumbers", LINE_NUMBERS}, +#endif +#ifdef HAVE_LIBMAGIC + {"magic", USE_MAGIC}, #endif {"morespace", MORE_SPACE}, /* Deprecated; remove in 2021. */ #ifdef ENABLE_MOUSE