Skip to content

Commit 47468a8

Browse files
authored
Merge pull request #165 from ilyagr/ilya
Fixes and docs for the ranger plugin
2 parents 3ecc147 + f347eaf commit 47468a8

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,14 @@ At last, press `<enter>` to accept or `<ESC>` to give up.
420420
Remember to enable the [enhanced matching](#enhanced-matching) algorithm, the current working directory can be skipped with it.
421421

422422

423+
## Ranger integration
424+
To add a `:z` command to the [`ranger` file manager], copy the `ranger_zlua.py` file to `~/.config/ranger/plugins/`.
425+
You can then use `:z foo`, `:z -b foo`, etc. from ranger. Use `:z -h` for help.
426+
427+
[`ranger` file manager]: https://github.com/ranger/ranger
428+
429+
To define additional commands (`:zb` for example) in ranger, you can put `alias zb z -b` into `~/.config/ranger/rc.conf`.
430+
423431

424432
## Tips
425433

ranger_zlua.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,45 @@
22
import ranger.api
33
import subprocess
44

5-
old_hook_init = ranger.api.hook_init
6-
7-
PATH_LUA = os.environ.get('RANGER_LUA')
8-
PATH_ZLUA = os.environ.get('RANGER_ZLUA')
5+
# $RANGER_LUA and $RANGER_ZLUA variables are deprecated, do not use them.
6+
ZLUA_LUAEXE = os.environ.get('RANGER_LUA') or os.environ.get('ZLUA_LUAEXE')
7+
ZLUA_SCRIPT = os.environ.get('RANGER_ZLUA') or os.environ.get('ZLUA_SCRIPT')
98

10-
if not PATH_LUA:
9+
if not ZLUA_LUAEXE:
1110
for path in os.environ.get('PATH', '').split(os.path.pathsep):
1211
for name in ('lua', 'luajit', 'lua5.3', 'lua5.2', 'lua5.1'):
1312
test = os.path.join(path, name)
1413
test = test + (sys.platform[:3] == 'win' and ".exe" or "")
1514
if os.path.exists(test):
16-
PATH_LUA = test
15+
ZLUA_LUAEXE = test
1716
break
1817

19-
if not PATH_LUA:
20-
sys.stderr.write('Please install lua or set $RANGER_LUA.\n')
21-
sys.exit()
18+
def _report_error(msg):
19+
sys.stderr.write('ranger_zlua: ' + msg)
20+
raise RuntimeError(msg)
21+
22+
if not ZLUA_LUAEXE:
23+
_report_error('Please install lua in $PATH or make sure $ZLUA_LUAEXE points to a lua executable.\n')
24+
if (not ZLUA_SCRIPT) or (not os.path.exists(ZLUA_SCRIPT)):
25+
_report_error('Could not find z.lua, please make sure $ZLUA_SCRIPT is set to absolute path of z.lua.\n')
2226

23-
if (not PATH_ZLUA) or (not os.path.exists(PATH_ZLUA)):
24-
sys.stderr.write('Not find z.lua, please set $RANGER_ZLUA to absolute path of z.lua.\n')
25-
sys.exit()
2627

27-
28+
# Inform z.lua about directories the user browses to inside ranger
29+
old_hook_init = ranger.api.hook_init
30+
2831
def hook_init(fm):
2932
def update_zlua(signal):
3033
import os, random
3134
os.environ['_ZL_RANDOM'] = str(random.randint(0, 0x7fffffff))
32-
p = subprocess.Popen([PATH_LUA, PATH_ZLUA, "--add", signal.new.path])
35+
p = subprocess.Popen([ZLUA_LUAEXE, ZLUA_SCRIPT, "--add", signal.new.path])
3336
p.wait()
34-
if PATH_ZLUA and PATH_LUA and os.path.exists(PATH_ZLUA):
37+
if ZLUA_SCRIPT and ZLUA_LUAEXE and os.path.exists(ZLUA_SCRIPT):
3538
fm.signal_bind('cd', update_zlua)
3639
return old_hook_init(fm)
3740

3841
ranger.api.hook_init = hook_init
3942

43+
4044
class z(ranger.api.commands.Command):
4145
def execute (self):
4246
import sys, os, time
@@ -52,13 +56,13 @@ def execute (self):
5256
elif arg[:1] != '-':
5357
break
5458
if mode:
55-
cmd = '"%s" "%s" '%(PATH_LUA, PATH_ZLUA)
59+
cmd = '"%s" "%s" '%(ZLUA_LUAEXE, ZLUA_SCRIPT)
5660
if mode in ('-I', '-i', '--'):
5761
cmd += ' --cd'
5862
for arg in args:
5963
cmd += ' "%s"'%arg
6064
if mode in ('-e', '-x'):
61-
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
65+
path = subprocess.check_output([ZLUA_LUAEXE, ZLUA_SCRIPT, '--cd'] + args)
6266
path = path.decode("utf-8", "ignore")
6367
path = path.rstrip('\n')
6468
self.fm.notify(path)
@@ -76,7 +80,7 @@ def execute (self):
7680
if path and os.path.exists(path):
7781
self.fm.cd(path)
7882
else:
79-
path = subprocess.check_output([PATH_LUA, PATH_ZLUA, '--cd'] + args)
83+
path = subprocess.check_output([ZLUA_LUAEXE, ZLUA_SCRIPT, '--cd'] + args)
8084
path = path.decode("utf-8", "ignore")
8185
path = path.rstrip('\n')
8286
if path and os.path.exists(path):

0 commit comments

Comments
 (0)