22import ranger .api
33import 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+
2831def 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
3841ranger .api .hook_init = hook_init
3942
43+
4044class 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