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.
This commit is contained in:
Benno Schulenberg 2020-09-21 09:23:37 +02:00
parent 0906181d75
commit 58a3293efc
4 changed files with 20 additions and 5 deletions

View File

@ -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;

View File

@ -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. */

View File

@ -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);

View File

@ -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