From 5df41255b293d20ea5f1a54ba1950ff483c66187 Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Fri, 11 Jan 2019 18:54:54 +0530 Subject: Fix #187 It seems the behaviour of the default opener (xdg-open) varies across platforms and environments. Details are documented in the issue. To prevent an empty file from being accidentally opened leading to unexpected behaviour, we are adding a guard with the message that the empty file should be opened using edit or open option. --- src/nnn.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nnn.c b/src/nnn.c index 50c9b9d..1cdaa56 100644 --- a/src/nnn.c +++ b/src/nnn.c @@ -260,7 +260,7 @@ typedef struct { uint dircolor : 1; /* Current status of dir color */ uint metaviewer : 1; /* Index of metadata viewer in utils[] */ uint ctxactive : 1; /* Context active or not */ - uint reserved : 10; + uint reserved : 9; /* The following settings are global */ uint curctx : 2; /* Current context number */ uint picker : 1; /* Write selection to user-specified file */ @@ -269,6 +269,7 @@ typedef struct { uint useeditor : 1; /* Use VISUAL to open text files */ uint runscript : 1; /* Choose script to run mode */ uint runctx : 2; /* The context in which script is to be run */ + uint restrict0b : 1; /* Restrict 0-byte file opening */ } settings; /* Contexts or workspaces */ @@ -284,7 +285,7 @@ typedef struct { /* GLOBALS */ /* Configuration, contexts */ -static settings cfg = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}; +static settings cfg = {0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}; static context g_ctx[CTX_MAX] __attribute__ ((aligned)); static struct entry *dents; @@ -2770,6 +2771,11 @@ nochange: continue; } + if (!sb.st_size && cfg.restrict0b) { + printmsg("empty: use edit or open with"); + goto nochange; + } + /* Invoke desktop opener as last resort */ spawn(utils[OPENER], newpath, NULL, NULL, F_NOWAIT | F_NOTRACE); continue; @@ -3775,6 +3781,10 @@ int main(int argc, char *argv[]) if (getenv("DISABLE_FILE_OPEN_ON_NAV")) cfg.nonavopen = 1; + /* Restrict opening of 0-byte files */ + if (getenv("NNN_RESTRICT_0B")) + cfg.restrict0b = 1; + signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); -- cgit v1.2.3-70-g09d2