Patch 8.2.3503
Problem:    Vim9: using g:pat:cmd is confusing.
Solution:   Do not recognize g: as the :global command.  Also for s:pat:repl.
            (closes #8982)
Files:      runtime/doc/vim9.txt, src/ex_docmd.c, src/ex_cmds.c, src/errors.h,
            src/vim9compile.c, src/proto/vim9compile.pro,
            src/testdir/test_vim9_cmd.vim


*** ../vim-8.2.3502/runtime/doc/vim9.txt        2021-07-18 17:21:34.176266950 
+0100
--- runtime/doc/vim9.txt        2021-10-13 14:52:31.540492320 +0100
***************
*** 866,874 ****
  
  Some Ex commands can be confused with assignments in Vim9 script: >
        g:name = value    # assignment
-       g:pattern:cmd     # invalid command - ERROR
        :g:pattern:cmd    # :global command
  
  Functions defined with `:def` compile the whole function.  Legacy functions
  can bail out, and the following lines are not parsed: >
        func Maybe()
--- 942,963 ----
  
  Some Ex commands can be confused with assignments in Vim9 script: >
        g:name = value    # assignment
        :g:pattern:cmd    # :global command
  
+ To avoid confusion between a `:global` or `:substitute` command and an
+ expression or assignment, a few separators cannot be used when these commands
+ are abbreviated to a single character: ':', '-' and '.'. >
+       g:pattern:cmd     # invalid command - ERROR
+       s:pattern:repl    # invalid command - ERROR
+       g-pattern-cmd     # invalid command - ERROR
+       s-pattern-repl    # invalid command - ERROR
+       g.pattern.cmd     # invalid command - ERROR
+       s.pattern.repl    # invalid command - ERROR
+ 
+ Also, there cannot be a space between the command and the separator: >
+       g /pattern/cmd    # invalid command - ERROR
+       s /pattern/repl   # invalid command - ERROR
+ 
  Functions defined with `:def` compile the whole function.  Legacy functions
  can bail out, and the following lines are not parsed: >
        func Maybe()
*** ../vim-8.2.3502/src/ex_docmd.c      2021-10-09 13:58:49.005990220 +0100
--- src/ex_docmd.c      2021-10-13 14:52:31.544492386 +0100
***************
*** 3600,3605 ****
--- 3600,3614 ----
            }
        }
  
+       // "g:", "s:" and "l:" are always assumed to be a variable, thus start
+       // an expression.  A global/substitute/list command needs to use a
+       // longer name.
+       if (vim_strchr((char_u *)"gsl", *p) != NULL && p[1] == ':')
+       {
+           eap->cmdidx = CMD_eval;
+           return eap->cmd;
+       }
+ 
        // If it is an ID it might be a variable with an operator on the next
        // line, if the variable exists it can't be an Ex command.
        if (p > eap->cmd && ends_excmd(*skipwhite(p))
*** ../vim-8.2.3502/src/ex_cmds.c       2021-09-04 12:43:57.928929327 +0100
--- src/ex_cmds.c       2021-10-13 14:52:31.544492386 +0100
***************
*** 3724,3729 ****
--- 3724,3732 ----
                                // don't accept alphanumeric for separator
        if (check_regexp_delim(*cmd) == FAIL)
            return;
+       if (in_vim9script() && check_global_and_subst(eap->cmd, eap->arg)
+                                                                     == FAIL)
+           return;
  
        /*
         * undocumented vi feature:
***************
*** 4899,4904 ****
--- 4902,4910 ----
      cmd = eap->arg;
      which_pat = RE_LAST;          // default: use last used regexp
  
+     if (in_vim9script() && check_global_and_subst(eap->cmd, eap->arg) == FAIL)
+       return;
+ 
      /*
       * undocumented vi feature:
       *        "\/" and "\?": use previous search pattern.
*** ../vim-8.2.3502/src/errors.h        2021-10-10 12:35:13.331259343 +0100
--- src/errors.h        2021-10-13 14:52:31.540492320 +0100
***************
*** 666,668 ****
--- 666,672 ----
        INIT(= N_("E1239: Invalid value for blob: %d"));
  EXTERN char e_resulting_text_too_long[]
        INIT(= N_("E1240: Resulting text too long"));
+ EXTERN char e_separator_not_supported_str[]
+       INIT(= N_("E1241: Separator not supported: %s"));
+ EXTERN char e_no_white_space_allowed_before_separator_str[]
+       INIT(= N_("E1242: No white space allowed before separator: %s"));
*** ../vim-8.2.3502/src/vim9compile.c   2021-09-16 19:14:46.807034740 +0100
--- src/vim9compile.c   2021-10-13 15:03:56.895154184 +0100
***************
*** 9473,9478 ****
--- 9473,9498 ----
  #endif
  
  /*
+  * Check if the separator for a :global or :substitute command is OK.
+  */
+     int
+ check_global_and_subst(char_u *cmd, char_u *arg)
+ {
+     if (arg == cmd + 1 && vim_strchr(":-.", *arg) != NULL)
+     {
+       semsg(_(e_separator_not_supported_str), arg);
+       return FAIL;
+     }
+     if (VIM_ISWHITE(cmd[1]))
+     {
+       semsg(_(e_no_white_space_allowed_before_separator_str), cmd);
+       return FAIL;
+     }
+     return OK;
+ }
+ 
+ 
+ /*
   * Add a function to the list of :def functions.
   * This sets "ufunc->uf_dfunc_idx" but the function isn't compiled yet.
   */
***************
*** 10066,10071 ****
--- 10086,10093 ----
                    break;
  
            case CMD_substitute:
+                   if (check_global_and_subst(ea.cmd, p) == FAIL)
+                       goto erret;
                    if (cctx.ctx_skip == SKIP_YES)
                        line = (char_u *)"";
                    else
***************
*** 10132,10137 ****
--- 10154,10163 ----
                        line = compile_script(line, &cctx);
                    break;
  
+           case CMD_global:
+                   if (check_global_and_subst(ea.cmd, p) == FAIL)
+                       goto erret;
+                   // FALLTHROUGH
            default:
                    // Not recognized, execute with do_cmdline_cmd().
                    ea.arg = p;
*** ../vim-8.2.3502/src/proto/vim9compile.pro   2021-08-21 19:50:31.206336775 
+0100
--- src/proto/vim9compile.pro   2021-10-13 14:58:53.078549628 +0100
***************
*** 17,22 ****
--- 17,23 ----
  int assignment_len(char_u *p, int *heredoc);
  void vim9_declare_error(char_u *name);
  int check_vim9_unlet(char_u *name);
+ int check_global_and_subst(char_u *cmd, char_u *arg);
  int compile_def_function(ufunc_T *ufunc, int check_return_type, compiletype_T 
compile_type, cctx_T *outer_cctx);
  void set_function_type(ufunc_T *ufunc);
  void delete_instr(isn_T *isn);
*** ../vim-8.2.3502/src/testdir/test_vim9_cmd.vim       2021-08-25 
21:37:33.103295301 +0100
--- src/testdir/test_vim9_cmd.vim       2021-10-13 14:52:31.544492386 +0100
***************
*** 1489,1493 ****
--- 1489,1542 ----
    au! justTesting
  enddef
  
+ def Test_var_not_cmd()
+   var lines =<< trim END
+       g:notexist:cmd
+   END
+   CheckDefAndScriptFailure2(lines, 'E488: Trailing characters: :cmd', 'E121: 
Undefined variable: g:notexist', 1)
+ 
+   lines =<< trim END
+       g-pat-cmd
+   END
+   CheckDefAndScriptFailure(lines, 'E1241:', 1)
+ 
+   lines =<< trim END
+       s:notexist:repl
+   END
+   CheckDefAndScriptFailure2(lines, 'E488: Trailing characters: :repl', 'E121: 
Undefined variable: s:notexist', 1)
+ 
+   lines =<< trim END
+       s-pat-repl
+   END
+   CheckDefAndScriptFailure(lines, 'E1241:', 1)
+ 
+   lines =<< trim END
+       w:notexist->len()
+   END
+   CheckDefExecAndScriptFailure(lines, 'E121: Undefined variable: w:notexist', 
1)
+ 
+   lines =<< trim END
+       b:notexist->len()
+   END
+   CheckDefExecAndScriptFailure(lines, 'E121: Undefined variable: b:notexist', 
1)
+ 
+   lines =<< trim END
+       t:notexist->len()
+   END
+   CheckDefExecAndScriptFailure(lines, 'E121: Undefined variable: t:notexist', 
1)
+ enddef
+ 
+ def Test_no_space_after_command()
+   var lines =<< trim END
+       g /pat/cmd
+   END
+   CheckDefAndScriptFailure(lines, 'E1242:', 1)
+ 
+   lines =<< trim END
+       s /pat/repl
+   END
+   CheckDefAndScriptFailure(lines, 'E1242:', 1)
+ enddef
+ 
  
  " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
*** ../vim-8.2.3502/src/version.c       2021-10-13 10:05:26.863362862 +0100
--- src/version.c       2021-10-13 14:55:30.707388398 +0100
***************
*** 759,760 ****
--- 759,762 ----
  {   /* Add new patch number below this line */
+ /**/
+     3503,
  /**/

-- 
hundred-and-one symptoms of being an internet addict:
142. You dream about creating the world's greatest web site.

 /// 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/20211013140520.18E66C80053%40moolenaar.net.

Raspunde prin e-mail lui