@@ -120,11 +120,6 @@ def __init__(self, stdin=None, stdout=None):
120120 if os .getenv ("ZTI_AUTOSIZE" ):
121121 self .autosize = True
122122
123- self .macros_dir = os .getenv ("ZTI_MACROS_DIR" )
124- if self .macros_dir is None or not os .path .isdir (
125- self .macros_dir ):
126- self .macros_dir = os .path .expanduser ("~/.zti-macros.d" )
127-
128123 self .pend_intro = None
129124 self .__dirty_ranges = []
130125 self .single_session = False
@@ -1861,45 +1856,6 @@ def help_plugin(entry=entry):
18611856
18621857 self .plugins = " " .join (plugins )
18631858
1864- def __register_macros (self ):
1865- import importlib .util
1866- import sys
1867- import types
1868-
1869- if not os .path .isdir (self .macros_dir ):
1870- return
1871-
1872- for macro_file in os .listdir (self .macros_dir ):
1873- if not macro_file .endswith (".py" ):
1874- continue
1875-
1876- if len (macro_file .split ('.' )) > 1 :
1877- continue
1878-
1879- macro_name = macro_file .split ('.' )[0 ]
1880-
1881- # Ignore macros which already exist
1882- if f"do_{ macro_name } " in self .get_names ():
1883- continue
1884-
1885- macro_path = os .path .join (self .macros_dir , macro_file )
1886-
1887- # Import the user macro as a module
1888- macro_spec = importlib .util .spec_from_file_location (
1889- f"module.{ macro_name } " , macro_path )
1890- macro_module = importlib .util .module_from_spec (macro_spec )
1891- sys .modules [f"module.{ macro_name } " ] = macro_module
1892- macro_spec .loader .exec_module (macro_module )
1893-
1894- # Find the function
1895- if hasattr (macro_module , macro_name ):
1896- # Create a new bound method for the `Zti` class for this
1897- # function
1898- setattr (Zti , f"do_{ macro_name } " ,
1899- types .MethodType (
1900- getattr (macro_module , macro_name ),
1901- self ))
1902-
19031859 def __key_data (self , tns , data ):
19041860 try :
19051861 tns .key_data (data , zti = self )
@@ -2751,6 +2707,71 @@ def __refresh(self):
27512707
27522708 self .stdscr .refresh (_win_callback = self .__set_event_fn ())
27532709
2710+ def __register_macros (self ):
2711+ import importlib .util
2712+ import sys
2713+ import types
2714+
2715+ macros_dir = os .getenv ("ZTI_MACROS_DIR" )
2716+ if macros_dir is None :
2717+ macros_dir = os .path .expanduser ("~/.zti-mac" )
2718+
2719+ if not os .path .isdir (macros_dir ):
2720+ _logger .exception (f"{ macros_dir } is not a directory" )
2721+ return
2722+
2723+ for macro_file in os .listdir (macros_dir ):
2724+ if not macro_file .endswith (".py" ):
2725+ continue
2726+
2727+ if len (macro_file .split ('.' )) > 1 :
2728+ continue
2729+
2730+ macro_name = macro_file .split ('.' )[0 ]
2731+
2732+ # Ignore macros with uppercase letters or
2733+ # spaces
2734+ uppercase = [l for l in macro_name if l .isupper ()]
2735+ if ' ' in macro_name or len (uppercase ) != 0 :
2736+ continue
2737+
2738+ # Ignore macros which already exist
2739+ if f"do_{ macro_name } " in self .get_names ():
2740+ _logger .warning (f"Function with name do_{ macro_name } already exists, macro registration failed" )
2741+ continue
2742+
2743+ macro_path = os .path .join (macros_dir , macro_file )
2744+
2745+ # Import the user macro as a module
2746+ macro_spec = importlib .util .spec_from_file_location (
2747+ f"module.{ macro_name } " , macro_path )
2748+ macro_module = importlib .util .module_from_spec (macro_spec )
2749+ sys .modules [f"module.{ macro_name } " ] = macro_module
2750+ macro_spec .loader .exec_module (macro_module )
2751+
2752+ # Find the function
2753+ if hasattr (macro_module , f"do_{ macro_name } " ):
2754+ def do_macro (zti , arg ):
2755+ self .__bg_wait_end ()
2756+ macro_func = getattr (macro_module , f"do_{ macro_name } " )
2757+ macro_func (zti , arg )
2758+
2759+ # Create a new bound method for the `Zti` class for this
2760+ # function
2761+ setattr (Zti , f"do_{ macro_name } " ,
2762+ types .MethodType (
2763+ do_macro ,
2764+ self ))
2765+
2766+ # Check if a corresponding help function exists
2767+ if hasattr (macro_module , f"help_{ macro_name } " ):
2768+ # Create a new bound method for the `Zti` class for this
2769+ # function
2770+ setattr (Zti , f"help_{ macro_name } " ,
2771+ types .MethodType (
2772+ getattr (macro_module , f"help_{ macro_name } " ),
2773+ self ))
2774+
27542775 def __scale_size (self , maxrow , maxcol ):
27552776 arows , acols = self .autosize
27562777 aspect1 = arows / acols
0 commit comments