mirror of
https://github.com/jarun/nnn.git
synced 2024-11-24 20:01:27 +00:00
Detect filetype using file output
This commit is contained in:
parent
60de561b07
commit
4df1ae6311
33
src/nnn.c
33
src/nnn.c
|
@ -987,26 +987,6 @@ static char xchartohex(char c)
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *getmime(const char *file)
|
|
||||||
{
|
|
||||||
static regex_t regex;
|
|
||||||
static uint i;
|
|
||||||
static const uint len = LEN(assocs);
|
|
||||||
|
|
||||||
for (i = 0; i < len; ++i) {
|
|
||||||
if (regcomp(®ex, assocs[i].regex, REG_NOSUB | REG_EXTENDED | REG_ICASE) != 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (regexec(®ex, file, 0, NULL, 0) == 0) {
|
|
||||||
regfree(®ex);
|
|
||||||
return assocs[i].mime;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
regfree(®ex);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int setfilter(regex_t *regex, char *filter)
|
static int setfilter(regex_t *regex, char *filter)
|
||||||
{
|
{
|
||||||
static size_t len;
|
static size_t len;
|
||||||
|
@ -2719,13 +2699,12 @@ nochange:
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* If NNN_USE_EDITOR is set, open text in EDITOR */
|
/* If NNN_USE_EDITOR is set, open text in EDITOR */
|
||||||
if (cfg.useeditor)
|
if (cfg.useeditor &&
|
||||||
if (getmime(dents[cur].name) ||
|
get_output(g_buf, CMD_LEN_MAX, "file", FILE_OPTS, newpath, FALSE) &&
|
||||||
(get_output(g_buf, CMD_LEN_MAX, "file", FILE_OPTS, newpath, FALSE) &&
|
strstr(g_buf, "text/") == g_buf) {
|
||||||
strstr(g_buf, "text/") == g_buf)) {
|
spawn(editor, newpath, editor_arg, path, F_NORMAL);
|
||||||
spawn(editor, newpath, editor_arg, path, F_NORMAL);
|
continue;
|
||||||
continue;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Invoke desktop opener as last resort */
|
/* Invoke desktop opener as last resort */
|
||||||
spawn(utils[OPENER], newpath, NULL, NULL, F_NOWAIT | F_NOTRACE);
|
spawn(utils[OPENER], newpath, NULL, NULL, F_NOWAIT | F_NOTRACE);
|
||||||
|
|
10
src/nnn.h
10
src/nnn.h
|
@ -96,16 +96,6 @@ struct key {
|
||||||
enum action act; /* Action */
|
enum action act; /* Action */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Extension pattern and mime combination */
|
|
||||||
struct assoc {
|
|
||||||
char *regex; /* Regex to match on filename */
|
|
||||||
char *mime; /* File type */
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct assoc assocs[] = {
|
|
||||||
{ "\\.(c|cpp|h|log|md|py|rb|sh|txt)$", "text" },
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct key bindings[] = {
|
static struct key bindings[] = {
|
||||||
/* Back */
|
/* Back */
|
||||||
{ KEY_BACKSPACE, SEL_BACK },
|
{ KEY_BACKSPACE, SEL_BACK },
|
||||||
|
|
Loading…
Reference in a new issue