nano-devel
[Top][All Lists]
Advanced

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

[PATCH 1/2] options: require --magic or 'set magic' to enable the use of


From: Benno Schulenberg
Subject: [PATCH 1/2] options: require --magic or 'set magic' to enable the use of libmagic
Date: Mon, 21 Sep 2020 11:08:56 +0200

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        | 11 ++++++++++-
 src/rcfile.c      |  1 +
 4 files changed, 15 insertions(+), 4 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 e0b458e6..53bac147 100644
--- a/src/definitions.h
+++ b/src/definitions.h
@@ -541,7 +541,8 @@ enum
        JUMPY_SCROLLING,
        EMPTY_LINE,
        INDICATOR,
-       BOOKSTYLE
+       BOOKSTYLE,
+       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 75c0c9b5..e9cdfa5c 100644
--- a/src/nano.c
+++ b/src/nano.c
@@ -579,6 +579,9 @@ void usage(void)
 #endif
 #ifndef NANO_TINY
        print_opt("-Z", "--zap", N_("Let Bsp and Del erase a marked region"));
+#ifdef HAVE_LIBMAGIC
+       print_opt("-!", "--magic", N_("Also try magic to determine syntax"));
+#endif
        print_opt("-a", "--atblanks", N_("When soft-wrapping, do it at 
whitespace"));
 #endif
 #ifdef ENABLE_WRAPPING
@@ -1766,6 +1769,7 @@ int main(int argc, char **argv)
                {"wordbounds", 0, NULL, 'W'},
                {"wordchars", 1, NULL, 'X'},
                {"zap", 0, NULL, 'Z'},
+               {"magic", 0, NULL, '!'},
                {"atblanks", 0, NULL, 'a'},
                {"autoindent", 0, NULL, 'i'},
                {"cutfromcursor", 0, NULL, 'k'},
@@ -1828,7 +1832,7 @@ int main(int argc, char **argv)
                SET(RESTRICTED);
 
        while ((optchr = getopt_long(argc, argv, 
"ABC:DEFGHIJ:KLMNOPQ:RST:UVWX:Y:Z"
-                               "abcdef:ghijklmno:pqr:s:tuvwxyz$", 
long_options, NULL)) != -1) {
+                               "!abcdef:ghijklmno:pqr:s:tuvwxyz$", 
long_options, NULL)) != -1) {
                switch (optchr) {
 #ifndef NANO_TINY
                        case 'A':
@@ -1950,6 +1954,11 @@ int main(int argc, char **argv)
                        case 'Z':
                                SET(LET_THEM_ZAP);
                                break;
+#ifdef HAVE_LIBMAGIC
+                       case '!':
+                               SET(USE_MAGIC);
+                               break;
+#endif
                        case 'a':
                                SET(AT_BLANKS);
                                break;
diff --git a/src/rcfile.c b/src/rcfile.c
index b908f2ce..49523cf4 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -59,6 +59,7 @@ static const rcoption rcopts[] = {
 #ifdef ENABLE_LINENUMBERS
        {"linenumbers", LINE_NUMBERS},
 #endif
+       {"magic", USE_MAGIC},
        {"morespace", MORE_SPACE},  /* Deprecated; remove in 2021. */
 #ifdef ENABLE_MOUSE
        {"mouse", USE_MOUSE},
-- 
2.25.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]