Patch 8.2.0960
Problem:    Cannot use :import in legacy Vim script.
Solution:   Support :import in any Vim script.
Files:      src/vim9script.c, src/evalvars.c, src/userfunc.c,
            src/testdir/test_vim9_script.vim


*** ../vim-8.2.0959/src/vim9script.c    2020-05-24 23:00:06.436196028 +0200
--- src/vim9script.c    2020-06-11 22:32:18.584991259 +0200
***************
*** 17,23 ****
  
  #include "vim9.h"
  
! static char e_needs_vim9[] = N_("E1042: import/export can only be used in 
vim9script");
  
      int
  in_vim9script(void)
--- 17,23 ----
  
  #include "vim9.h"
  
! static char e_needs_vim9[] = N_("E1042: export can only be used in 
vim9script");
  
      int
  in_vim9script(void)
***************
*** 141,156 ****
      void
  ex_import(exarg_T *eap)
  {
!     if (current_sctx.sc_version != SCRIPT_VERSION_VIM9)
!       emsg(_(e_needs_vim9));
!     else
!     {
!       char_u *cmd_end = handle_import(eap->arg, NULL,
!                                                   current_sctx.sc_sid, NULL);
  
!       if (cmd_end != NULL)
!           eap->nextcmd = check_nextcmd(cmd_end);
!     }
  }
  
  /*
--- 141,150 ----
      void
  ex_import(exarg_T *eap)
  {
!     char_u *cmd_end = handle_import(eap->arg, NULL, current_sctx.sc_sid, 
NULL);
  
!     if (cmd_end != NULL)
!       eap->nextcmd = check_nextcmd(cmd_end);
  }
  
  /*
*** ../vim-8.2.0959/src/evalvars.c      2020-06-07 14:50:47.271846855 +0200
--- src/evalvars.c      2020-06-11 22:30:54.097437246 +0200
***************
*** 2359,2367 ****
            *dip = v;
      }
  
!     if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
      {
!       imported_T *import = find_imported(name, 0, NULL);
  
        // imported variable from another script
        if (import != NULL)
--- 2359,2371 ----
            *dip = v;
      }
  
!     if (tv == NULL && (current_sctx.sc_version == SCRIPT_VERSION_VIM9
!                                              || STRNCMP(name, "s:", 2) == 0))
      {
!       imported_T  *import;
!       char_u      *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
! 
!       import = find_imported(p, 0, NULL);
  
        // imported variable from another script
        if (import != NULL)
*** ../vim-8.2.0959/src/userfunc.c      2020-06-05 21:06:06.607601386 +0200
--- src/userfunc.c      2020-06-11 23:05:25.756062358 +0200
***************
*** 719,738 ****
      ufunc_T   *func;
      imported_T        *imported;
  
!     if (in_vim9script() && !is_global)
      {
!       // Find script-local function before global one.
!       func = find_func_with_sid(name, current_sctx.sc_sid);
!       if (func != NULL)
!           return func;
! 
!       // Find imported funcion before global one.
!       imported = find_imported(name, 0, cctx);
!       if (imported != NULL && imported->imp_funcname != NULL)
        {
!           hi = hash_find(&func_hashtab, imported->imp_funcname);
!           if (!HASHITEM_EMPTY(hi))
!               return HI2UF(hi);
        }
      }
  
--- 719,763 ----
      ufunc_T   *func;
      imported_T        *imported;
  
!     if (!is_global)
      {
!       char_u *after_script = NULL;
! 
!       if (in_vim9script())
!       {
!           // Find script-local function before global one.
!           func = find_func_with_sid(name, current_sctx.sc_sid);
!           if (func != NULL)
!               return func;
!       }
! 
!       if (!in_vim9script()
!               && name[0] == K_SPECIAL
!               && name[1] == KS_EXTRA
!               && name[2] == KE_SNR)
!       {
!           long sid;
! 
!           // Caller changes s: to <SNR>99_name.
! 
!           after_script = name + 3;
!           sid = getdigits(&after_script);
!           if (sid == current_sctx.sc_sid && *after_script == '_')
!               ++after_script;
!           else
!               after_script = NULL;
!       }
!       if (in_vim9script() || after_script != NULL)
        {
!           // Find imported function before global one.
!           imported = find_imported(
!                         after_script == NULL ? name : after_script, 0, cctx);
!           if (imported != NULL && imported->imp_funcname != NULL)
!           {
!               hi = hash_find(&func_hashtab, imported->imp_funcname);
!               if (!HASHITEM_EMPTY(hi))
!                   return HI2UF(hi);
!           }
        }
      }
  
*** ../vim-8.2.0959/src/testdir/test_vim9_script.vim    2020-05-25 
22:36:46.629735032 +0200
--- src/testdir/test_vim9_script.vim    2020-06-11 23:10:21.374469560 +0200
***************
*** 736,742 ****
    CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
    CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
    CheckScriptFailure(['export let some = 123'], 'E1042:')
!   CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1042:')
    CheckScriptFailure(['vim9script', 'export let g:some'], 'E1044:')
    CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
  
--- 736,742 ----
    CheckScriptFailure(['scriptversion 2', 'vim9script'], 'E1039:')
    CheckScriptFailure(['vim9script', 'scriptversion 2'], 'E1040:')
    CheckScriptFailure(['export let some = 123'], 'E1042:')
!   CheckScriptFailure(['import some from "./Xexport.vim"'], 'E1048:')
    CheckScriptFailure(['vim9script', 'export let g:some'], 'E1044:')
    CheckScriptFailure(['vim9script', 'export echo 134'], 'E1043:')
  
***************
*** 1836,1841 ****
--- 1836,1882 ----
    delete('Xforward')
  enddef
  
+ def Test_source_vim9_from_legacy()
+   let legacy_lines =<< trim END
+     source Xvim9_script.vim
+ 
+     call assert_false(exists('local'))
+     call assert_false(exists('exported'))
+     call assert_false(exists('s:exported'))
+     call assert_equal('global', global)
+     call assert_equal('global', g:global)
+ 
+     " imported variable becomes script-local
+     import exported from './Xvim9_script.vim'
+     call assert_equal('exported', s:exported)
+     call assert_false(exists('exported'))
+ 
+     " imported function becomes script-local
+     import GetText from './Xvim9_script.vim'
+     call assert_equal('text', s:GetText())
+     call assert_false(exists('*GetText'))
+   END
+   writefile(legacy_lines, 'Xlegacy_script.vim')
+ 
+   let vim9_lines =<< trim END
+     vim9script
+     let local = 'local'
+     g:global = 'global'
+     export let exported = 'exported'
+     export def GetText(): string
+        return 'text'
+     enddef
+   END
+   writefile(vim9_lines, 'Xvim9_script.vim')
+ 
+   source Xlegacy_script.vim
+ 
+   assert_equal('global', g:global)
+ "  unlet g:global
+ 
+   delete('Xlegacy_script.vim')
+   delete('Xvim9_script.vim')
+ enddef
  
  " Keep this last, it messes up highlighting.
  def Test_substitute_cmd()
*** ../vim-8.2.0959/src/version.c       2020-06-11 19:35:48.745325203 +0200
--- src/version.c       2020-06-11 23:06:06.383832674 +0200
***************
*** 756,757 ****
--- 756,759 ----
  {   /* Add new patch number below this line */
+ /**/
+     960,
  /**/

-- 
ARTHUR:    Will you ask your master if he wants to join my court at Camelot?!
GUARD #1:  But then of course African swallows are not migratory.
GUARD #2:  Oh, yeah...
GUARD #1:  So they couldn't bring a coconut back anyway...
                                  The Quest for the Holy Grail (Monty Python)

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/202006112111.05BLBEr0158450%40masaka.moolenaar.net.

Raspunde prin e-mail lui