From e11d2e2017cb52a73511614ba6f8d7809437e80d Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sat, 13 Aug 2022 15:27:24 -0700 Subject: [PATCH 1/2] 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 @@ 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 @@ 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): From f347eaf1c87be711b783a913375185fcc80cdb28 Mon Sep 17 00:00:00 2001 From: Ilya Grigoriev Date: Sat, 13 Aug 2022 17:26:30 -0700 Subject: [PATCH 2/2] Slight stylistic edits to `ranger_zlua.py` --- ranger_zlua.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/ranger_zlua.py b/ranger_zlua.py index 1434933..67389fc 100644 --- a/ranger_zlua.py +++ b/ranger_zlua.py @@ -2,44 +2,45 @@ import ranger.api import subprocess -old_hook_init = ranger.api.hook_init - # $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') +ZLUA_LUAEXE = os.environ.get('RANGER_LUA') or os.environ.get('ZLUA_LUAEXE') +ZLUA_SCRIPT = os.environ.get('RANGER_ZLUA') or os.environ.get('ZLUA_SCRIPT') -if not PATH_LUA: +if not ZLUA_LUAEXE: for path in os.environ.get('PATH', '').split(os.path.pathsep): for name in ('lua', 'luajit', 'lua5.3', 'lua5.2', 'lua5.1'): test = os.path.join(path, name) test = test + (sys.platform[:3] == 'win' and ".exe" or "") if os.path.exists(test): - PATH_LUA = test + ZLUA_LUAEXE = test break def _report_error(msg): sys.stderr.write('ranger_zlua: ' + msg) raise RuntimeError(msg) -if not PATH_LUA: +if not ZLUA_LUAEXE: _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)): +if (not ZLUA_SCRIPT) or (not os.path.exists(ZLUA_SCRIPT)): _report_error('Could not find z.lua, please make sure $ZLUA_SCRIPT is set to absolute path of z.lua.\n') - + +# Inform z.lua about directories the user browses to inside ranger +old_hook_init = ranger.api.hook_init + def hook_init(fm): def update_zlua(signal): import os, random os.environ['_ZL_RANDOM'] = str(random.randint(0, 0x7fffffff)) - p = subprocess.Popen([PATH_LUA, PATH_ZLUA, "--add", signal.new.path]) + p = subprocess.Popen([ZLUA_LUAEXE, ZLUA_SCRIPT, "--add", signal.new.path]) p.wait() - if PATH_ZLUA and PATH_LUA and os.path.exists(PATH_ZLUA): + if ZLUA_SCRIPT and ZLUA_LUAEXE and os.path.exists(ZLUA_SCRIPT): fm.signal_bind('cd', update_zlua) return old_hook_init(fm) ranger.api.hook_init = hook_init + class z(ranger.api.commands.Command): def execute (self): import sys, os, time @@ -55,13 +56,13 @@ def execute (self): elif arg[:1] != '-': break if mode: - cmd = '"%s" "%s" '%(PATH_LUA, PATH_ZLUA) + cmd = '"%s" "%s" '%(ZLUA_LUAEXE, ZLUA_SCRIPT) if mode in ('-I', '-i', '--'): cmd += ' --cd' for arg in args: cmd += ' "%s"'%arg if mode in ('-e', '-x'): - path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args) + path = subprocess.check_output([ZLUA_LUAEXE, ZLUA_SCRIPT, '--cd'] + args) path = path.decode("utf-8", "ignore") path = path.rstrip('\n') self.fm.notify(path) @@ -79,7 +80,7 @@ def execute (self): if path and os.path.exists(path): self.fm.cd(path) else: - path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args) + path = subprocess.check_output([ZLUA_LUAEXE, ZLUA_SCRIPT, '--cd'] + args) path = path.decode("utf-8", "ignore") path = path.rstrip('\n') if path and os.path.exists(path):