@@ -210,6 +210,19 @@ def viml_has_key(obj, key):
210210def viml_stridx (a , b ):
211211 return a .find (b )
212212
213+ def viml_type (obj ):
214+ if obj is None : return 0
215+ if isinstance (obj , (int , float )): return 0
216+ if isinstance (obj , str ): return 1
217+ if isinstance (obj , list ): return 3
218+ if isinstance (obj , dict ): return 4
219+ if callable (obj ): return 2
220+ return 0
221+
222+ def viml_function (name ):
223+ # Return a dummy function for type comparison
224+ return lambda : None
225+
213226
214227NIL = []
215228TRUE = 1
@@ -605,6 +618,10 @@ def __init__(self, *a000):
605618 self .neovim = a000 [0 ]
606619 else :
607620 self .neovim = 0
621+ if viml_len (a000 ) > 1 and viml_type (a000 [1 ]) == viml_type (AttributeDict ({})):
622+ self .callbacks = a000 [1 ]
623+ else :
624+ self .callbacks = AttributeDict ({})
608625 self .find_command_cache = AttributeDict ({})
609626
610627 def push_context (self , node ):
@@ -624,6 +641,10 @@ def find_context(self, type):
624641 def add_node (self , node ):
625642 viml_add (self .context [0 ].body , node )
626643
644+ def invoke_callback (self , name , * a000 ):
645+ if viml_has_key (self .callbacks , name ) and viml_type (self .callbacks [name ]) == viml_type (viml_function ("tr" )):
646+ viml_call (self .callbacks [name ], a000 )
647+
627648 def check_missing_endfunction (self , ends , pos ):
628649 if self .context [0 ].type == NODE_FUNCTION :
629650 raise VimLParserException (Err (viml_printf ("E126: Missing :endfunction: %s" , ends ), pos ))
@@ -1028,6 +1049,8 @@ def find_command(self):
10281049 name = c
10291050 elif self .reader .peekn (2 ) == "py" :
10301051 name = self .reader .read_alnum ()
1052+ elif self .reader .peekn (4 ) == "vim9" :
1053+ name = self .reader .read_alnum ()
10311054 else :
10321055 pos = self .reader .tell ()
10331056 name = self .reader .read_alpha ()
@@ -1039,11 +1062,19 @@ def find_command(self):
10391062 if viml_has_key (self .find_command_cache , name ):
10401063 return self .find_command_cache [name ]
10411064 cmd = NIL
1042- for x in self .builtin_commands :
1043- if viml_stridx (x .name , name ) == 0 and viml_len (name ) >= x .minlen :
1044- del cmd
1045- cmd = x
1046- break
1065+ # Special case for vim9script and vim9cmd to avoid matching vimgrep
1066+ if name != "vim9script" and name != "vim9cmd" :
1067+ for x in self .builtin_commands :
1068+ if viml_stridx (x .name , name ) == 0 and viml_len (name ) >= x .minlen :
1069+ del cmd
1070+ cmd = x
1071+ break
1072+ elif name == "vim9script" :
1073+ del cmd
1074+ cmd = AttributeDict ({"name" : "vim9script" , "minlen" : 5 , "flags" : "WORD1|CMDWIN|LOCK_OK" , "parser" : "parse_cmd_common" })
1075+ elif name == "vim9cmd" :
1076+ del cmd
1077+ cmd = AttributeDict ({"name" : "vim9cmd" , "minlen" : 4 , "flags" : "NEEDARG|EXTRA|NOTRLCOM|CMDWIN|LOCK_OK" , "parser" : "parse_cmd_common" })
10471078 if self .neovim :
10481079 for x in self .neovim_additional_commands :
10491080 if viml_stridx (x .name , name ) == 0 and viml_len (name ) >= x .minlen :
@@ -1182,6 +1213,13 @@ def parse_cmd_common(self):
11821213 node .pos = self .ea .cmdpos
11831214 node .ea = self .ea
11841215 node .str = self .reader .getstr (self .ea .linepos , end )
1216+ # Invoke callback for vim9 script commands
1217+ if self .ea .cmd .name == "vim9script" :
1218+ self .invoke_callback ("vim9script_callback" , node , node .str )
1219+ elif self .ea .cmd .name == "vim9cmd" :
1220+ self .invoke_callback ("vim9cmd_callback" , node , node .str )
1221+ elif self .ea .cmd .name == "def" :
1222+ self .invoke_callback ("def_callback" , node , node .str )
11851223 self .add_node (node )
11861224
11871225 def separate_nextcmd (self ):
0 commit comments