Extend plugin mechanism to arbitrary commands

This commit is contained in:
Arun Prakash Jana 2019-10-24 12:39:31 +05:30
parent cb916661c8
commit 5bebd4ac67
No known key found for this signature in database
GPG key ID: A75979F35C080412
3 changed files with 20 additions and 8 deletions

View file

@ -50,7 +50,7 @@ Add to that an awesome [Wiki](https://github.com/jarun/nnn/wiki)!
- Detailed file information - Detailed file information
- Media information (using plugin) - Media information (using plugin)
- Convenience - Convenience
- Plugins with configurable keybinds - Run plugins (or commands) with configurable keybinds
- FreeDesktop compliant trash (needs trash-cli) - FreeDesktop compliant trash (needs trash-cli)
- SSHFS mounts (needs sshfs) - SSHFS mounts (needs sshfs)
- Cross-dir file/all/range selection - Cross-dir file/all/range selection
@ -147,7 +147,7 @@ There is no config file. Associated files are stored under `${XDG_CONFIG_HOME:-$
| Example `export` | Description | | Example `export` | Description |
| --- | --- | | --- | --- |
| `NNN_BMS='d:~/Documents;D:~/Docs archive/'` | key-bookmark pairs [max 10] | | `NNN_BMS='d:~/Documents;D:~/Docs archive/'` | key-bookmark pairs [max 10] |
| `NNN_PLUG='o:fzy-open;p:mocplay;m:nmount;t:thumb'` | key-plugin pairs (<kbd>:key</kbd> to run) [max 10] | | `NNN_PLUG='o:fzy-open;p:mocplay;m:nmount;t:thumb'` | key-plugin pairs (<kbd>:key</kbd> to run) [max 15] |
| `NNN_USE_EDITOR=1` | open text files in `$VISUAL` (else `$EDITOR`, fallback vi) | | `NNN_USE_EDITOR=1` | open text files in `$VISUAL` (else `$EDITOR`, fallback vi) |
| `NNN_CONTEXT_COLORS='1234'` | specify per context color [default: '4444' (all blue)] | | `NNN_CONTEXT_COLORS='1234'` | specify per context color [default: '4444' (all blue)] |
| `NNN_SSHFS_OPTS='sshfs -o reconnect,idmap=user'` | specify SSHFS options | | `NNN_SSHFS_OPTS='sshfs -o reconnect,idmap=user'` | specify SSHFS options |

View file

@ -66,6 +66,10 @@ Plugins are installed to `${XDG_CONFIG_HOME:-$HOME/.config}/nnn/plugins`. You ca
With this, plugin `fzy-open` can be run with the keybind <kbd>:o</kbd>, `mocplay` can be run with <kbd>:p</kbd> and so on... The key vs. plugin pairs are shown in the help and config screen. Up to 10 plugins can have such keybinds. With this, plugin `fzy-open` can be run with the keybind <kbd>:o</kbd>, `mocplay` can be run with <kbd>:p</kbd> and so on... The key vs. plugin pairs are shown in the help and config screen. Up to 10 plugins can have such keybinds.
To assign keys to arbitrary commands (non-shell interpreted) and invoke like plugins, add `_` (underscore) before the command. For example:
export NNN_PLUG='x:_chmod +x;o:fzy-open'
**Method 2:** Use the _pick plugin_ shortcut to visit the plugin directory and execute a plugin. Repeating the same shortcut cancels the operation and puts you back in the original directory. **Method 2:** Use the _pick plugin_ shortcut to visit the plugin directory and execute a plugin. Repeating the same shortcut cancels the operation and puts you back in the original directory.
## Create your own plugins ## Create your own plugins
@ -105,7 +109,7 @@ There are many plugins provided by `nnn` which can be used as examples. Here are
#!/usr/bin/env sh #!/usr/bin/env sh
git log -p -- "$1" git log -p -- "$1"
``` ```
- Change to directory in clipboard using helper script - Change to directory in clipboard using helper script
```sh ```sh
#!/usr/bin/env sh #!/usr/bin/env sh
@ -121,13 +125,13 @@ There are many plugins provided by `nnn` which can be used as examples. Here are
nnn_cd "$(dirname $(readlink -fn $1))" 0 nnn_cd "$(dirname $(readlink -fn $1))" 0
``` ```
- Change to arbitrary directory without helper script - Change to arbitrary directory without helper script
```sh ```sh
#!/usr/bin/env sh #!/usr/bin/env sh
echo -n "cd to: " echo -n "cd to: "
read dir read dir
echo -n "0$dir" > $NNN_PIPE echo -n "0$dir" > $NNN_PIPE
``` ```

View file

@ -128,7 +128,7 @@
#define MSGWAIT '$' #define MSGWAIT '$'
#define REGEX_MAX 48 #define REGEX_MAX 48
#define BM_MAX 10 #define BM_MAX 10
#define PLUGIN_MAX 10 #define PLUGIN_MAX 15
#define ENTRY_INCR 64 /* Number of dir 'entry' structures to allocate per shot */ #define ENTRY_INCR 64 /* Number of dir 'entry' structures to allocate per shot */
#define NAMEBUF_INCR 0x800 /* 64 dir entries at once, avg. 32 chars per filename = 64*32B = 2KB */ #define NAMEBUF_INCR 0x800 /* 64 dir entries at once, avg. 32 chars per filename = 64*32B = 2KB */
#define DESCRIPTOR_LEN 32 #define DESCRIPTOR_LEN 32
@ -4941,13 +4941,21 @@ nochange:
} }
if (sel == SEL_PLUGKEY) { if (sel == SEL_PLUGKEY) {
uchar flag = F_NORMAL;
r = get_input(""); r = get_input("");
tmp = get_kv_val(plug, NULL, r, PLUGIN_MAX, FALSE); tmp = get_kv_val(plug, NULL, r, PLUGIN_MAX, FALSE);
if (!tmp) if (!tmp)
goto nochange; goto nochange;
mkpath(plugindir, tmp, newpath); if (tmp[0] == '_' && tmp[1]) {
spawn(newpath, (ndents ? dents[cur].name : NULL), path, path, F_NORMAL); xstrlcpy(newpath, ++tmp, PATH_MAX);
flag = F_CLI | F_CONFIRM;
} else
mkpath(plugindir, tmp, newpath);
spawn(newpath, (ndents ? dents[cur].name : NULL),
path, path, flag);
if (cfg.filtermode) if (cfg.filtermode)
presel = FILTER; presel = FILTER;