22import ranger .api
33import subprocess
44
5- old_hook_init = ranger .api .hook_init
6-
75# $RANGER_LUA and $RANGER_ZLUA variables are deprecated, do not use them.
8- PATH_LUA = os .environ .get ('RANGER_LUA' ) or os .environ .get ('ZLUA_LUAEXE' )
9- PATH_ZLUA = os .environ .get ('RANGER_ZLUA' ) or os .environ .get ('ZLUA_SCRIPT' )
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' )
108
11- if not PATH_LUA :
9+ if not ZLUA_LUAEXE :
1210 for path in os .environ .get ('PATH' , '' ).split (os .path .pathsep ):
1311 for name in ('lua' , 'luajit' , 'lua5.3' , 'lua5.2' , 'lua5.1' ):
1412 test = os .path .join (path , name )
1513 test = test + (sys .platform [:3 ] == 'win' and ".exe" or "" )
1614 if os .path .exists (test ):
17- PATH_LUA = test
15+ ZLUA_LUAEXE = test
1816 break
1917
2018def _report_error (msg ):
2119 sys .stderr .write ('ranger_zlua: ' + msg )
2220 raise RuntimeError (msg )
2321
24- if not PATH_LUA :
22+ if not ZLUA_LUAEXE :
2523 _report_error ('Please install lua in $PATH or make sure $ZLUA_LUAEXE points to a lua executable.\n ' )
26-
27- if (not PATH_ZLUA ) or (not os .path .exists (PATH_ZLUA )):
24+ if (not ZLUA_SCRIPT ) or (not os .path .exists (ZLUA_SCRIPT )):
2825 _report_error ('Could not find z.lua, please make sure $ZLUA_SCRIPT is set to absolute path of z.lua.\n ' )
2926
30-
27+
28+ # Inform z.lua about directories the user browses to inside ranger
29+ old_hook_init = ranger .api .hook_init
30+
3131def hook_init (fm ):
3232 def update_zlua (signal ):
3333 import os , random
3434 os .environ ['_ZL_RANDOM' ] = str (random .randint (0 , 0x7fffffff ))
35- p = subprocess .Popen ([PATH_LUA , PATH_ZLUA , "--add" , signal .new .path ])
35+ p = subprocess .Popen ([ZLUA_LUAEXE , ZLUA_SCRIPT , "--add" , signal .new .path ])
3636 p .wait ()
37- 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 ):
3838 fm .signal_bind ('cd' , update_zlua )
3939 return old_hook_init (fm )
4040
4141ranger .api .hook_init = hook_init
4242
43+
4344class z (ranger .api .commands .Command ):
4445 def execute (self ):
4546 import sys , os , time
@@ -55,13 +56,13 @@ def execute (self):
5556 elif arg [:1 ] != '-' :
5657 break
5758 if mode :
58- cmd = '"%s" "%s" ' % (PATH_LUA , PATH_ZLUA )
59+ cmd = '"%s" "%s" ' % (ZLUA_LUAEXE , ZLUA_SCRIPT )
5960 if mode in ('-I' , '-i' , '--' ):
6061 cmd += ' --cd'
6162 for arg in args :
6263 cmd += ' "%s"' % arg
6364 if mode in ('-e' , '-x' ):
64- path = subprocess .check_output ([PATH_LUA , PATH_ZLUA , '--cd' ] + args )
65+ path = subprocess .check_output ([ZLUA_LUAEXE , ZLUA_SCRIPT , '--cd' ] + args )
6566 path = path .decode ("utf-8" , "ignore" )
6667 path = path .rstrip ('\n ' )
6768 self .fm .notify (path )
@@ -79,7 +80,7 @@ def execute (self):
7980 if path and os .path .exists (path ):
8081 self .fm .cd (path )
8182 else :
82- path = subprocess .check_output ([PATH_LUA , PATH_ZLUA , '--cd' ] + args )
83+ path = subprocess .check_output ([ZLUA_LUAEXE , ZLUA_SCRIPT , '--cd' ] + args )
8384 path = path .decode ("utf-8" , "ignore" )
8485 path = path .rstrip ('\n ' )
8586 if path and os .path .exists (path ):
0 commit comments