src/dbg.h | 4 ++-- src/nnn.c | 41 +++++++++++++++++++++++++++++------------ diff --git a/src/dbg.h b/src/dbg.h index 4d5b54a4cefd333f778dded2e15fa0d74d585e64..7bd757a47a63d7910b4ee732223a64ed102dd68e 100644 --- a/src/dbg.h +++ b/src/dbg.h @@ -47,7 +47,7 @@ va_end(ap); return r; } -static int enabledbg() +static int enabledbg(void) { FILE *fp = fopen("/tmp/nnndbg", "w"); @@ -71,7 +71,7 @@ return 0; } -static void disabledbg() +static void disabledbg(void) { close(DEBUG_FD); } diff --git a/src/nnn.c b/src/nnn.c index c11aa237b0cd039b6f5698dfb6fe6c5d85b7f09c..72db2c5e4c3c6e042f58cc1b5b1b85508342184d 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -4045,8 +4045,19 @@ ++opt; } +#ifdef DBGMODE + enabledbg(); + atexit(disabledbg); +#endif + home = getenv("HOME"); DPRINTF_S(home); + + /* Get custom opener, if set */ + opener = xgetenv(env_cfg[NNN_OPENER], utils[OPENER]); + if (getenv(env_cfg[NNN_OPENER_DETACH])) + opener_flag |= F_NOWAIT; + DPRINTF_S(opener); /* Parse bookmarks string */ if (!parsebmstr()) { @@ -4072,10 +4083,28 @@ if (strlen(ipath) > 7 && ipath[0] == 'f' && ipath[1] == 'i' && ipath[2] == 'l' && ipath[3] == 'e' && ipath[4] == ':' && ipath[5] == '/' && ipath[6] == '/') ipath = ipath + 7; ipath = realpath(ipath, cwd); + DPRINTF_S(ipath); if (!ipath) { xerror(); return 1; } + + /* + * If nnn is set as the file manager, applications may try to open + * files by invoking nnn. In that case pass the file path to the + * desktop opener and exit. + */ + struct stat sb; + + if (stat(ipath, &sb) == -1) { + printwarn(); + return 1; + } + + if (S_ISREG(sb.st_mode)) { + spawn(opener, ipath, NULL, NULL, opener_flag); + return 0; + } } /* Edit text in EDITOR, if opted */ @@ -4112,12 +4141,6 @@ xerror(); return 1; } #endif - - /* Get custom opener, if set */ - opener = xgetenv(env_cfg[NNN_OPENER], utils[OPENER]); - if (getenv(env_cfg[NNN_OPENER_DETACH])) - opener_flag |= F_NOWAIT; - DPRINTF_S(opener); /* Set nnn nesting level, idletimeout used as tmp var */ idletimeout = xatoi(getenv(env_cfg[NNNLVL])); @@ -4200,9 +4223,6 @@ #endif read_history(NULL); #endif -#ifdef DBGMODE - enabledbg(); -#endif if (!initcurses()) return 1; @@ -4236,8 +4256,5 @@ close(event_fd); close(kq); #endif -#ifdef DBGMODE - disabledbg(); -#endif return 0; }