Makefile | 2 +- config.def.h | 1 + noice.c | 41 +++++++++++++++++++++++------------------ diff --git a/Makefile b/Makefile index de973b9bd57e4a23e2ee85aa18c779ef5e1bc886..b293e4f73260cbab925c08a63fb03541103fb485 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ #CPPFLAGS = -DDEBUG #CFLAGS = -g CFLAGS = -O3 -march=native -LDLIBS = -lcurses -lmagic +LDLIBS = -lcurses DISTFILES = noice.c strlcat.c strlcpy.c util.h config.def.h\ noice.1 Makefile README LICENSE diff --git a/config.def.h b/config.def.h index b9f6741b4c7c165358ca3579899e9d58645557a7..663351a9090a0a7aab7a18e47cc097a57e080e87 100644 --- a/config.def.h +++ b/config.def.h @@ -9,6 +9,7 @@ char *idlecmd = "rain"; /* The screensaver program */ struct assoc assocs[] = { //{ "\\.(avi|mp4|mkv|mp3|ogg|flac|mov)$", "mpv" }, + { "\\.(c|cpp|h|txt|log)$", "vim" }, { "\\.(wma|mp3|ogg|flac)$", "fmedia" }, //{ "\\.(png|jpg|gif)$", "feh" }, //{ "\\.(html|svg)$", "firefox" }, diff --git a/noice.c b/noice.c index 9e679a8bca5d55f8893fc263abcda233596efc2d..30ccf4df006b2fc48410db9df1c3c230cd5d42d9 100644 --- a/noice.c +++ b/noice.c @@ -17,7 +17,6 @@ #include #include #include #include -#include #include "util.h" @@ -39,6 +38,7 @@ #undef MIN #define MIN(x, y) ((x) < (y) ? (x) : (y)) #define ISODD(x) ((x) & 1) #define CONTROL(c) ((c) ^ 0x40) +#define MAX_PATH_LEN 1024 struct assoc { char *regex; /* Regex to match on filename */ @@ -207,19 +207,6 @@ { regex_t regex; char *bin = NULL; int i; - - const char *mime; - magic_t magic; - - magic = magic_open(MAGIC_MIME_TYPE); - magic_load(magic, NULL); - magic_compile(magic, NULL); - mime = magic_file(magic, file); - DPRINTF_S(mime); - - if (strcmp(mime, "text/plain") == 0) - return "vim"; - magic_close(magic); for (i = 0; i < LEN(assocs); i++) { if (regcomp(®ex, assocs[i].regex, @@ -666,12 +653,30 @@ strlcpy(fltr, ifilter, sizeof(fltr)); goto begin; case S_IFREG: bin = openwith(newpath); + char *execvim = "vim"; + if (bin == NULL) { - char cmd[512]; + FILE *fp; + char cmd[MAX_PATH_LEN]; int status; - sprintf(cmd, "xdg-open \"%s\" > /dev/null 2>&1", newpath); - status = system(cmd); - continue; + + snprintf(cmd, MAX_PATH_LEN, "file \"%s\"", newpath); + fp = popen(cmd, "r"); + if (fp == NULL) + goto nochange; + if (fgets(cmd, MAX_PATH_LEN, fp) == NULL) { + pclose(fp); + goto nochange; + } + pclose(fp); + + if (strstr(cmd, "ASCII text") != NULL) + bin = execvim; + else { + snprintf(cmd, MAX_PATH_LEN, "xdg-open \"%s\" > /dev/null 2>&1", newpath); + status = system(cmd); + continue; + } } exitcurses(); spawn(bin, newpath, NULL);