From e11d2e2017cb52a73511614ba6f8d7809437e80d Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sat, 13 Aug 2022 15:27:24 -0700 Subject: [PATCH] Fixes and docs for the ranger plugin - Makes Ranger plugin use the standard `$ZLUA_LUAEXE` and `$ZLUA_SCRIPT` environment variables that `z.lua` itself uses. - Better error handling; errors loading plugin no longer cause `ranger` to quit entirely. - Briefly document the plugin --- README.md | 8 ++++++++ ranger_zlua.py | 15 +++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 000ac68..3e8a35a 100644 --- a/README.md +++ b/README.md @@ -420,6 +420,14 @@ At last, press `` to accept or `` to give up. Remember to enable the [enhanced matching](#enhanced-matching) algorithm, the current working directory can be skipped with it. +## Ranger integration +To add a `:z` command to the [`ranger` file manager], copy the `ranger_zlua.py` file to `~/.config/ranger/plugins/`. +You can then use `:z foo`, `:z -b foo`, etc. from ranger. Use `:z -h` for help. + +[`ranger` file manager]: https://github.com/ranger/ranger + +To define additional commands (`:zb` for example) in ranger, you can put `alias zb z -b` into `~/.config/ranger/rc.conf`. + ## Tips diff --git a/ranger_zlua.py b/ranger_zlua.py index 321a5eb..1434933 100644 --- a/ranger_zlua.py +++ b/ranger_zlua.py @@ -4,8 +4,9 @@ import subprocess old_hook_init = ranger.api.hook_init -PATH_LUA = os.environ.get('RANGER_LUA') -PATH_ZLUA = os.environ.get('RANGER_ZLUA') +# $RANGER_LUA and $RANGER_ZLUA variables are deprecated, do not use them. +PATH_LUA = os.environ.get('RANGER_LUA') or os.environ.get('ZLUA_LUAEXE') +PATH_ZLUA = os.environ.get('RANGER_ZLUA') or os.environ.get('ZLUA_SCRIPT') if not PATH_LUA: for path in os.environ.get('PATH', '').split(os.path.pathsep): @@ -16,13 +17,15 @@ if not PATH_LUA: PATH_LUA = test break +def _report_error(msg): + sys.stderr.write('ranger_zlua: ' + msg) + raise RuntimeError(msg) + if not PATH_LUA: - sys.stderr.write('Please install lua or set $RANGER_LUA.\n') - sys.exit() + _report_error('Please install lua in $PATH or make sure $ZLUA_LUAEXE points to a lua executable.\n') if (not PATH_ZLUA) or (not os.path.exists(PATH_ZLUA)): - sys.stderr.write('Not find z.lua, please set $RANGER_ZLUA to absolute path of z.lua.\n') - sys.exit() + _report_error('Could not find z.lua, please make sure $ZLUA_SCRIPT is set to absolute path of z.lua.\n') def hook_init(fm):