nnn

mirror of https://github.com/jarun/nnn
Log | Files | Refs | README | LICENSE

mainline.diff (7908B)


      1 # Description: Adds preview pipe to enable closing and re-opening the preview
      2 #              pane when running an undetached editor. If you are using vim
      3 #              you might experience incorrectly resized window. Consider adding
      4 #              the following to your vimrc:
      5 #              autocmd VimEnter * :silent exec "!kill -s WINCH $PPID"
      6 #
      7 # Authors: Luuk van Baal
      8 
      9 diff --git a/src/nnn.c b/src/nnn.c
     10 index 9a78fb55..ca4ac0f7 100644
     11 --- a/src/nnn.c
     12 +++ b/src/nnn.c
     13 @@ -396,8 +396,9 @@ typedef struct {
     14  	uint_t usebsdtar  : 1;  /* Use bsdtar as default archive utility */
     15  	uint_t xprompt    : 1;  /* Use native prompt instead of readline prompt */
     16  	uint_t showlines  : 1;  /* Show line numbers */
     17 +	uint_t previewer  : 1;  /* Run state of previewer */
     18  	uint_t plugparsed : 1;  /* Plugin kv pairs parsed */
     19 -	uint_t reserved   : 4;  /* Adjust when adding/removing a field */
     20 +	uint_t reserved   : 3;  /* Adjust when adding/removing a field */
     21  } runstate;
     22  
     23  /* Contexts or workspaces */
     24 @@ -527,6 +528,9 @@ alignas(max_align_t) static char g_tmpfpath[TMP_LEN_MAX];
     25  /* Buffer to store plugins control pipe location */
     26  alignas(max_align_t) static char g_pipepath[TMP_LEN_MAX];
     27  
     28 +/* Buffer to store preview plugins control pipe location */
     29 +static char g_ppipepath[TMP_LEN_MAX] __attribute__ ((aligned));
     30 +
     31  /* Non-persistent runtime states */
     32  static runstate g_state;
     33  
     34 @@ -705,12 +709,13 @@ static const char * const messages[] = {
     35  #define NNN_FCOLORS 5
     36  #define NNNLVL      6
     37  #define NNN_PIPE    7
     38 -#define NNN_MCLICK  8
     39 -#define NNN_SEL     9
     40 -#define NNN_ARCHIVE 10
     41 -#define NNN_ORDER   11
     42 -#define NNN_HELP    12
     43 -#define NNN_TRASH   13
     44 +#define NNN_PPIPE   8
     45 +#define NNN_MCLICK  9
     46 +#define NNN_SEL     10
     47 +#define NNN_ARCHIVE 11
     48 +#define NNN_ORDER   12
     49 +#define NNN_HELP    13
     50 +#define NNN_TRASH   14
     51  
     52  static const char * const env_cfg[] = {
     53  	"NNN_OPTS",
     54 @@ -721,6 +726,7 @@ static const char * const env_cfg[] = {
     55  	"NNN_FCOLORS",
     56  	"NNNLVL",
     57  	"NNN_PIPE",
     58 +	"NNN_PPIPE",
     59  	"NNN_MCLICK",
     60  	"NNN_SEL",
     61  	"NNN_ARCHIVE",
     62 @@ -859,7 +865,7 @@ static int set_sort_flags(int r);
     63  static void statusbar(char *path);
     64  static bool get_output(char *file, char *arg1, char *arg2, int fdout, bool page);
     65  #ifndef NOFIFO
     66 -static void notify_fifo(bool force);
     67 +static void notify_fifo(bool force, bool closepreview);
     68  #endif
     69  
     70  /* Functions */
     71 @@ -3166,7 +3172,7 @@ try_quit:
     72  			} else {
     73  #ifndef NOFIFO
     74  				if (!g_state.fifomode)
     75 -					notify_fifo(TRUE); /* Send hovered path to NNN_FIFO */
     76 +					notify_fifo(TRUE, FALSE); /* Send hovered path to NNN_FIFO */
     77  #endif
     78  				escaped = TRUE;
     79  				settimeout();
     80 @@ -5372,15 +5378,20 @@ static void run_cmd_as_plugin(const char *file, uchar_t flags)
     81  
     82  static bool plctrl_init(void)
     83  {
     84 -	size_t len;
     85 +	size_t len, lenbuf;
     86 +	pid_t pid = getpid();
     87  
     88  	/* g_tmpfpath is used to generate tmp file names */
     89  	g_tmpfpath[tmpfplen - 1] = '\0';
     90 -	len = xstrsncpy(g_pipepath, g_tmpfpath, TMP_LEN_MAX);
     91 +	len = lenbuf = xstrsncpy(g_pipepath, g_tmpfpath, TMP_LEN_MAX);
     92  	g_pipepath[len - 1] = '/';
     93 -	len = xstrsncpy(g_pipepath + len, "nnn-pipe.", TMP_LEN_MAX - len) + len;
     94 -	xstrsncpy(g_pipepath + len - 1, xitoa(getpid()), TMP_LEN_MAX - len);
     95 +	xstrsncpy(g_ppipepath, g_pipepath, TMP_LEN_MAX);
     96 +	len += xstrsncpy(g_pipepath + len, "nnn-pipe.", TMP_LEN_MAX - len);
     97 +	xstrsncpy(g_pipepath + len - 1, xitoa(pid), TMP_LEN_MAX - len);
     98 +	len = xstrsncpy(g_ppipepath + lenbuf, "nnn-ppipe.", TMP_LEN_MAX - lenbuf) + lenbuf;
     99 +	xstrsncpy(g_ppipepath + len - 1, xitoa(pid), TMP_LEN_MAX - len);
    100  	setenv(env_cfg[NNN_PIPE], g_pipepath, TRUE);
    101 +	setenv(env_cfg[NNN_PPIPE], g_ppipepath, TRUE);
    102  
    103  	return EXIT_SUCCESS;
    104  }
    105 @@ -5409,6 +5420,21 @@ static ssize_t read_nointr(int fd, void *buf, size_t count)
    106  	return len;
    107  }
    108  
    109 +void *previewpipe(void *arg __attribute__ ((unused)))
    110 +{
    111 +	int fd, buf;
    112 +
    113 +	mkfifo(g_ppipepath, 0600);
    114 +	fd = open(g_ppipepath, O_RDONLY);
    115 +
    116 +	if (read(fd, &buf, 1) == 1)
    117 +		g_state.previewer = buf;
    118 +
    119 +	close(fd);
    120 +	unlink(g_ppipepath);
    121 +	return NULL;
    122 +}
    123 +
    124  static char *readpipe(int fd, char *ctxnum, char **path)
    125  {
    126  	char ctx, *nextpath = NULL;
    127 @@ -6097,7 +6123,7 @@ static void populate(char *path, char *lastname)
    128  }
    129  
    130  #ifndef NOFIFO
    131 -static void notify_fifo(bool force)
    132 +static void notify_fifo(bool force, bool closepreview)
    133  {
    134  	if (!fifopath)
    135  		return;
    136 @@ -6113,6 +6139,12 @@ static void notify_fifo(bool force)
    137  		}
    138  	}
    139  
    140 +	if (closepreview) {
    141 +		if (write(fifofd, "close\n", 6) != 6)
    142 +			xerror();
    143 +		return;
    144 +	}
    145 +
    146  	static struct entry lastentry;
    147  
    148  	if (!force && !memcmp(&lastentry, &pdents[cur], sizeof(struct entry))) // NOLINT
    149 @@ -6145,7 +6177,7 @@ static void send_to_explorer(int *presel)
    150  		if (fd > 1)
    151  			close(fd);
    152  	} else
    153 -		notify_fifo(TRUE); /* Send opened path to NNN_FIFO */
    154 +		notify_fifo(TRUE, FALSE); /* Send opened path to NNN_FIFO */
    155  }
    156  #endif
    157  
    158 @@ -6178,7 +6210,7 @@ static void move_cursor(int target, int ignore_scrolloff)
    159  
    160  #ifndef NOFIFO
    161  	if (!g_state.fifomode)
    162 -		notify_fifo(FALSE); /* Send hovered path to NNN_FIFO */
    163 +		notify_fifo(FALSE, FALSE); /* Send hovered path to NNN_FIFO */
    164  #endif
    165  }
    166  
    167 @@ -6851,7 +6883,7 @@ static bool browse(char *ipath, int pkey)
    168  	pEntry pent;
    169  	enum action sel;
    170  	struct stat sb;
    171 -	int r = -1, presel, selstartid = 0, selendid = 0;
    172 +	int r = -1, presel, selstartid = 0, selendid = 0, previewkey = 0;
    173  	const uchar_t opener_flags = (cfg.cliopener ? F_CLI : (F_NOTRACE | F_NOSTDIN | F_NOWAIT));
    174  	bool watch = FALSE, cd = TRUE;
    175  	ino_t inode = 0;
    176 @@ -7110,7 +7142,7 @@ nochange:
    177  					move_cursor(r, 1);
    178  #ifndef NOFIFO
    179  				else if ((event.bstate == BUTTON1_PRESSED) && !g_state.fifomode)
    180 -					notify_fifo(TRUE); /* Send clicked path to NNN_FIFO */
    181 +					notify_fifo(TRUE, FALSE); /* Send clicked path to NNN_FIFO */
    182  #endif
    183  				/* Handle right click selection */
    184  				if (event.bstate == BUTTON3_PRESSED) {
    185 @@ -7272,7 +7304,14 @@ nochange:
    186  			    && strstr(g_buf, "text")
    187  #endif
    188  			) {
    189 +				if (g_state.previewer)
    190 +					notify_fifo(FALSE, TRUE);
    191  				spawn(editor, newpath, NULL, NULL, F_CLI);
    192 +				if (g_state.previewer) {
    193 +					pkey = previewkey;
    194 +					goto run_plugin;
    195 +				}
    196 +
    197  				if (cfg.filtermode) {
    198  					presel = FILTER;
    199  					clearfilter();
    200 @@ -7597,8 +7636,14 @@ nochange:
    201  				copycurname();
    202  				goto nochange;
    203  			case SEL_EDIT:
    204 +				if (g_state.previewer)
    205 +					notify_fifo(FALSE, TRUE);
    206  				if (!(g_state.picker || g_state.fifomode))
    207  					spawn(editor, newpath, NULL, NULL, F_CLI);
    208 +				if (g_state.previewer) {
    209 +					pkey = previewkey;
    210 +					goto run_plugin;
    211 +				}
    212  				continue;
    213  			default: /* SEL_LOCK */
    214  				lock_terminal();
    215 @@ -7987,6 +8032,7 @@ nochange:
    216  			cd = FALSE;
    217  			goto begin;
    218  		}
    219 +run_plugin:
    220  		case SEL_PLUGIN:
    221  			/* Check if directory is accessible */
    222  			if (!xdiraccess(plgpath)) {
    223 @@ -8012,6 +8058,12 @@ nochange:
    224  					goto nochange;
    225  				}
    226  
    227 +				if (xstrcmp(tmp, "preview-tui") == 0) {
    228 +					previewkey = r;
    229 +					pthread_t tid;
    230 +					pthread_create(&tid, NULL, previewpipe, NULL);
    231 +				}
    232 +
    233  				if (tmp[0] == '-' && tmp[1]) {
    234  					++tmp;
    235  					r = FALSE; /* Do not refresh dir after completion */
    236 @@ -8070,7 +8122,13 @@ nochange:
    237  		case SEL_SHELL: // fallthrough
    238  		case SEL_LAUNCH: // fallthrough
    239  		case SEL_PROMPT:
    240 +			if (g_state.previewer)
    241 +				notify_fifo(FALSE, TRUE);
    242  			r = handle_cmd(sel, path, newpath);
    243 +			if (g_state.previewer) {
    244 +				pkey = previewkey;
    245 +				goto run_plugin;
    246 +			}
    247  
    248  			/* Continue in type-to-nav mode, if enabled */
    249  			if (cfg.filtermode)
    250 @@ -8623,8 +8681,10 @@ static void cleanup(void)
    251  	if (g_state.autofifo)
    252  		unlink(fifopath);
    253  #endif
    254 -	if (g_state.pluginit)
    255 +	if (g_state.pluginit){
    256  		unlink(g_pipepath);
    257 +		unlink(g_ppipepath);
    258 +	}
    259  #ifdef DEBUG
    260  	disabledbg();
    261  #endif
    262 @@ -9169,7 +9229,7 @@ int main(int argc, char *argv[])
    263  
    264  #ifndef NOFIFO
    265  	if (!g_state.fifomode)
    266 -		notify_fifo(FALSE);
    267 +		notify_fifo(FALSE, TRUE);
    268  	if (fifofd != -1)
    269  		close(fifofd);
    270  #endif