Patch 8.2.4173
Problem:    Cannot use an import in 'foldexpr'.
Solution:   Set the script context to where 'foldexpr' was set. (closes #9584)
            Fix that the script context was not set for all buffers.
Files:      src/eval.c, src/proto/eval.pro, src/fold.c, src/structs.h,
            src/option.c, src/testdir/test_vim9_import.vim


*** ../vim-8.2.4172/src/eval.c  2022-01-19 17:21:24.846755312 +0000
--- src/eval.c  2022-01-21 15:31:43.919783356 +0000
***************
*** 772,778 ****
      return rettv.vval.v_list;
  }
  
! #ifdef FEAT_FOLDING
  /*
   * Evaluate "arg", which is 'foldexpr'.
   * Note: caller must set "curwin" to match "arg".
--- 772,778 ----
      return rettv.vval.v_list;
  }
  
! #if defined(FEAT_FOLDING) || defined(PROTO)
  /*
   * Evaluate "arg", which is 'foldexpr'.
   * Note: caller must set "curwin" to match "arg".
***************
*** 780,793 ****
   * give error messages.
   */
      int
! eval_foldexpr(char_u *arg, int *cp)
  {
      typval_T  tv;
      varnumber_T       retval;
      char_u    *s;
      int               use_sandbox = was_set_insecurely((char_u *)"foldexpr",
                                                                   OPT_LOCAL);
  
      ++emsg_off;
      if (use_sandbox)
        ++sandbox;
--- 780,798 ----
   * give error messages.
   */
      int
! eval_foldexpr(win_T *wp, int *cp)
  {
+     char_u    *arg;
      typval_T  tv;
      varnumber_T       retval;
      char_u    *s;
+     sctx_T    saved_sctx = current_sctx;
      int               use_sandbox = was_set_insecurely((char_u *)"foldexpr",
                                                                   OPT_LOCAL);
  
+     arg = wp->w_p_fde;
+     current_sctx = wp->w_p_script_ctx[WV_FDE];
+ 
      ++emsg_off;
      if (use_sandbox)
        ++sandbox;
***************
*** 818,823 ****
--- 823,829 ----
        --sandbox;
      --textwinlock;
      clear_evalarg(&EVALARG_EVALUATE, NULL);
+     current_sctx = saved_sctx;
  
      return (int)retval;
  }
*** ../vim-8.2.4172/src/proto/eval.pro  2022-01-07 12:45:24.115723150 +0000
--- src/proto/eval.pro  2022-01-21 15:31:47.211720126 +0000
***************
*** 18,28 ****
  varnumber_T eval_to_number(char_u *expr);
  typval_T *eval_expr(char_u *arg, exarg_T *eap);
  int call_vim_function(char_u *func, int argc, typval_T *argv, typval_T 
*rettv);
- varnumber_T call_func_retnr(char_u *func, int argc, typval_T *argv);
- int call_func_noret(char_u *func, int argc, typval_T *argv);
  void *call_func_retstr(char_u *func, int argc, typval_T *argv);
  void *call_func_retlist(char_u *func, int argc, typval_T *argv);
! int eval_foldexpr(char_u *arg, int *cp);
  char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int 
skip, int flags, int fne_flags);
  void clear_lval(lval_T *lp);
  void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int 
flags, char_u *op, int var_idx);
--- 18,26 ----
  varnumber_T eval_to_number(char_u *expr);
  typval_T *eval_expr(char_u *arg, exarg_T *eap);
  int call_vim_function(char_u *func, int argc, typval_T *argv, typval_T 
*rettv);
  void *call_func_retstr(char_u *func, int argc, typval_T *argv);
  void *call_func_retlist(char_u *func, int argc, typval_T *argv);
! int eval_foldexpr(win_T *wp, int *cp);
  char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int 
skip, int flags, int fne_flags);
  void clear_lval(lval_T *lp);
  void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int 
flags, char_u *op, int var_idx);
*** ../vim-8.2.4172/src/fold.c  2022-01-08 16:19:18.505639885 +0000
--- src/fold.c  2022-01-21 15:28:45.995198809 +0000
***************
*** 3307,3313 ****
      // KeyTyped may be reset to 0 when calling a function which invokes
      // do_cmdline().  To make 'foldopen' work correctly restore KeyTyped.
      save_keytyped = KeyTyped;
!     n = eval_foldexpr(flp->wp->w_p_fde, &c);
      KeyTyped = save_keytyped;
  
      switch (c)
--- 3307,3313 ----
      // KeyTyped may be reset to 0 when calling a function which invokes
      // do_cmdline().  To make 'foldopen' work correctly restore KeyTyped.
      save_keytyped = KeyTyped;
!     n = eval_foldexpr(flp->wp, &c);
      KeyTyped = save_keytyped;
  
      switch (c)
*** ../vim-8.2.4172/src/structs.h       2022-01-10 18:06:58.682381797 +0000
--- src/structs.h       2022-01-21 16:12:04.392619729 +0000
***************
*** 3695,3700 ****
--- 3695,3702 ----
       */
      winopt_T  w_onebuf_opt;
      winopt_T  w_allbuf_opt;
+     // transform a pointer to a "onebuf" option into a "allbuf" option
+ #define GLOBAL_WO(p)  ((char *)p + sizeof(winopt_T))
  
      // A few options have local flags for P_INSECURE.
  #ifdef FEAT_STL_OPT
***************
*** 3718,3726 ****
      int               w_briopt_list;      // additional indent for lists
  #endif
  
-     // transform a pointer to a "onebuf" option into a "allbuf" option
- #define GLOBAL_WO(p)  ((char *)p + sizeof(winopt_T))
- 
      long      w_scbind_pos;
  
  #ifdef FEAT_EVAL
--- 3720,3725 ----
*** ../vim-8.2.4172/src/option.c        2022-01-21 13:29:52.483888785 +0000
--- src/option.c        2022-01-21 16:28:49.201983071 +0000
***************
*** 2604,2610 ****
--- 2604,2616 ----
        if (indir & PV_BUF)
            curbuf->b_p_script_ctx[indir & PV_MASK] = new_script_ctx;
        else if (indir & PV_WIN)
+       {
            curwin->w_p_script_ctx[indir & PV_MASK] = new_script_ctx;
+           if (both)
+               // also setting the "all buffers" value
+               curwin->w_allbuf_opt.wo_script_ctx[indir & PV_MASK] =
+                                                               new_script_ctx;
+       }
      }
  }
  
*** ../vim-8.2.4172/src/testdir/test_vim9_import.vim    2022-01-21 
13:29:52.483888785 +0000
--- src/testdir/test_vim9_import.vim    2022-01-21 16:28:35.818264833 +0000
***************
*** 673,678 ****
--- 673,717 ----
    &rtp = save_rtp
  enddef
  
+ def Test_use_autoload_import_in_fold_expression()
+   mkdir('Xdir/autoload', 'p')
+   var save_rtp = &rtp
+   exe 'set rtp^=' .. getcwd() .. '/Xdir'
+ 
+   var lines =<< trim END
+       vim9script
+       export def Expr(): string
+         return getline(v:lnum) =~ '^#' ? '>1' : '1'
+       enddef
+       g:fold_loaded = 'yes'
+   END
+   writefile(lines, 'Xdir/autoload/fold.vim')
+ 
+   lines =<< trim END
+       vim9script
+       import autoload 'fold.vim'
+       &foldexpr = 'fold.Expr()'
+       &foldmethod = 'expr'
+       &debug = 'throw'
+   END
+   new
+   setline(1, ['# one', 'text', '# two', 'text'])
+   g:fold_loaded = 'no'
+   CheckScriptSuccess(lines)
+   assert_equal('no', g:fold_loaded)
+   redraw
+   assert_equal('yes', g:fold_loaded)
+ 
+   # Check that script context of 'foldexpr' is copied to another buffer.
+   edit! otherfile
+   redraw
+ 
+   set foldexpr= foldmethod&
+   bwipe!
+   delete('Xdir', 'rf')
+   &rtp = save_rtp
+ enddef
+ 
  def Test_export_fails()
    CheckScriptFailure(['export var some = 123'], 'E1042:')
    CheckScriptFailure(['vim9script', 'export var g:some'], 'E1022:')
*** ../vim-8.2.4172/src/version.c       2022-01-21 14:55:10.169295589 +0000
--- src/version.c       2022-01-21 15:27:04.733140285 +0000
***************
*** 752,753 ****
--- 752,755 ----
  {   /* Add new patch number below this line */
+ /**/
+     4173,
  /**/

-- 
If cars evolved at the same rate as computers have, they'd cost five euro, 
run for a year on a couple of liters of petrol, and explode once a day.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            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/20220121163143.B023D1C0202%40moolenaar.net.

Raspunde prin e-mail lui