]> granicus.if.org Git - vim/commitdiff
patch 8.2.3079: Powershell core not supported by default v8.2.3079
authorMike Williams <mikew@globalgraphics.com>
Wed, 30 Jun 2021 18:56:00 +0000 (20:56 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 30 Jun 2021 18:56:00 +0000 (20:56 +0200)
Problem:    Powershell core not supported by default.
Solution:   Set option defaults for "pwsh". (Mike Williams, closes #8481)

runtime/doc/eval.txt
runtime/doc/options.txt
runtime/doc/os_dos.txt
runtime/doc/os_win32.txt
src/fileio.c
src/misc2.c
src/option.c
src/os_win32.c
src/testdir/test_shell.vim
src/version.c

index 7fb25345380091ef31daec106f5aedc90f328c9f..2eceece64b8ce60fddc2d2b3a649f6774f2ea014 100644 (file)
@@ -9878,11 +9878,13 @@ sha256({string})                                                *sha256()*
 
 shellescape({string} [, {special}])                    *shellescape()*
                Escape {string} for use as a shell command argument.
-               On MS-Windows, when the 'shell' contains powershell then it
-               will enclose {string} in single quotes and will double up all
-               internal single quotes.  With other values for 'shell' when
-               'shellslash' is not set, it will enclose {string} in double
-               quotes and double all double quotes within {string}.
+               When the 'shell' contains powershell (MS-Windows) or pwsh
+               (MS-Windows, Linux, and MacOS) then it will enclose {string}
+               in single quotes and will double up all internal single
+               quotes.
+               On MS-Windows, when 'shellslash' is not set, it will enclose
+               {string} in double quotes and double all double quotes within
+               {string}.
                Otherwise it will enclose {string} in single quotes and
                replace all "'" with "'\''".
 
@@ -11042,8 +11044,8 @@ tempname()                                      *tempname()* *temp-file-name*
                        :exe "redir > " . tmpfile
 <              For Unix, the file will be in a private directory |tempfile|.
                For MS-Windows forward slashes are used when the 'shellslash'
-               option is set or when 'shellcmdflag' starts with '-' except
-               when when 'shell' contains powershell.
+               option is set, or when 'shellcmdflag' starts with '-' and
+               'shell' does not contain powershell or pwsh.
 
 
 term_ functions are documented here: |terminal-function-details|
index b958cf63ed2d07f1b86f20e33d8f05bfcd371add..af2e23eca77740c9b283fc6bd2b4a8aae136886b 100644 (file)
@@ -6633,6 +6633,8 @@ A jump table for the options with a short description can be found at |Q_op|.
        "bash", "fish", "ash" or "dash" the default becomes "2>&1| tee".  This
        means that stderr is also included.  Before using the 'shell' option a
        path is removed, thus "/bin/sh" uses "sh".
+       For Unix and MS-Windows, when the 'shell' option is "pwsh" the default
+       becomes ">%s 2>&1" and the output is not echoed to the screen.
        The initialization of this option is done after reading the ".vimrc"
        and the other initializations, so that when the 'shell' option is set
        there, the 'shellpipe' option changes automatically, unless it was
@@ -6648,9 +6650,7 @@ A jump table for the options with a short description can be found at |Q_op|.
        security reasons.
 
                                                *'shellquote'* *'shq'*
-'shellquote' 'shq'     string  (default: ""; Win32, when 'shell' does not
-                                contain powershell but contains "sh"
-                                somewhere: "\"")
+'shellquote' 'shq'     string  (default: "")
                        global
        Quoting character(s), put around the command passed to the shell, for
        the "!" and ":!" commands.  The redirection is kept outside of the
@@ -6658,9 +6658,7 @@ A jump table for the options with a short description can be found at |Q_op|.
        probably not useful to set both options.
        This is an empty string by default.  Only known to be useful for
        third-party shells on MS-Windows-like systems, such as the MKS Korn
-       Shell or bash, where it should be "\"".  The default is adjusted
-       according the value of 'shell', to reduce the need to set this option
-       by the user.  See |dos-shell|.
+       Shell or bash, where it should be "\"".  See |dos-shell|.
        This option cannot be set from a |modeline| or in the |sandbox|, for
        security reasons.
 
@@ -6677,12 +6675,12 @@ A jump table for the options with a short description can be found at |Q_op|.
        The default is ">".  For Unix, if the 'shell' option is "csh" or
        "tcsh" during initializations, the default becomes ">&".  If the
        'shell' option is "sh", "ksh", "mksh", "pdksh", "zsh", "zsh-beta",
-       "bash" or "fish", the default becomes ">%s 2>&1".  This means that
-       stderr is also included.  For Win32, the Unix checks are done and
+       "bash", "fish", or "pwsh", the default becomes ">%s 2>&1".  This means
+       that stderr is also included.  For Win32, the Unix checks are done and
        additionally "cmd" is checked for, which makes the default ">%s 2>&1",
        and "powershell" is checked for which makes the default
-       "2>&1 | Out-File -Encoding default".  Also, the same names with ".exe"
-       appended are checked for.
+       "2>&1 | Out-File -Encoding default" (see |dos-powershell|).  Also, the
+       same names with ".exe" appended are checked for.
        The initialization of this option is done after reading the ".vimrc"
        and the other initializations, so that when the 'shell' option is set
        there, the 'shellredir' option changes automatically unless it was
@@ -6697,7 +6695,7 @@ A jump table for the options with a short description can be found at |Q_op|.
                        global
                        {only for MS-Windows}
        When set, a forward slash is used when expanding file names.  This is
-       useful when a Unix-like shell is used instead of cmd.exe or
+       useful when a Unix-like shell is used instead of cmd.exe, pwsh.exe, or
        powershell.exe.  Backward slashes can still be typed, but they are
        changed to forward slashes by Vim.
        Note that setting or resetting this option has no effect for some
@@ -6767,10 +6765,10 @@ A jump table for the options with a short description can be found at |Q_op|.
        then ')"' is appended.
        When the value is '(' then also see 'shellxescape'.
        This is an empty string by default on most systems, but is known to be
-       useful for on Win32 version, either for cmd.exe and powershell.exe
-       which automatically strips off the first and last quote on a command,
-       or 3rd-party shells such as the MKS Korn Shell or bash, where it
-       should be "\"".  The default is adjusted according the value of
+       useful for on Win32 version, either for cmd.exe, powershell.exe, or
+       pwsh.exe which automatically strips off the first and last quote on a
+       command, or 3rd-party shells such as the MKS Korn Shell or bash, where
+       it should be "\"".  The default is adjusted according the value of
        'shell', to reduce the need to set this option by the user.  See
        |dos-shell|.
        This option cannot be set from a |modeline| or in the |sandbox|, for
index b45cb088cb75bd03c58dfb94d25f0ce425975a89..b473134ce1d29462647d472353758d762e4d2f24 100644 (file)
@@ -304,40 +304,80 @@ Vim sets the 'shellcmdflag' and 'shellquote' or 'shellxquote' options will be
 set as described above.
 
 ==============================================================================
-10. PowerShell                                         *dos-powershell*
+10. PowerShell                                 *dos-powershell* *dos-pwsh*
 
-Vim also supports Windows PowerShell.  If 'shell' has been set to
-"powershell.exe" at startup then VIM sets 'shellcmdflag', 'shellxquote',
-'shellpipe', and 'shellredir' options to the following values:
+Vim supports PowerShell Desktop and PowerShell Core.  PowerShell Desktop is
+the version of PowerShell that is installed with Windows, while PowerShell
+Core is a separate downloadable version that works cross-platform.  To see
+which version you are using then enter the following in a PowerShell prompt -
+$PSVersionTable.PSEdition
+
+If 'shell' includes "powershell" in the filename at startup then VIM sets
+'shellcmdflag', 'shellxquote', 'shellpipe', and 'shellredir' options to the
+following values:
 
 'shellcmdflag' -Command
 'shellxquote'  "
 'shellpipe'    2>&1 | Out-File -Encoding default
 'shellredir'   2>&1 | Out-File -Encoding default
 
+If 'shell' includes "pwsh" in the filename at startup then VIM sets
+'shellcmdflag', 'shellxquote', 'shellpipe', and 'shellredir' options to the
+following values:
+
+'shellcmdflag' -c
+'shellxquote'  "
+'shellpipe'    >%s 2>&1
+'shellredir'   >%s 2>&1
+
 If you find that PowerShell commands are taking a long time to run then try
-setting 'shellcmdflag' to "-NoProfile -Command".  Note this will prevent any
-PowerShell environment setup by the profile from taking place.
+with "-NoProfile" at the beginning of the 'shellcmdflag'.  Note this will
+prevent any PowerShell environment setup by the profile from taking place.
 
 If you have problems running PowerShell scripts through the 'shell' then try
-setting 'shellcmdflag' to "-ExecutionPolicy RemoteSigned -Command".  See
-online Windows documentation for more information on PowerShell Execution
-Policy settings.
+with "-ExecutionPolicy RemoteSigned -Command" at the beginning of
+'shellcmdflag'.  See online Windows documentation for more information on
+PowerShell Execution Policy settings.
+
+See |option-backslash| about including spaces in 'shellcmdflag' when using
+multiple flags.
 
 The 'shellpipe' and 'shellredir' option values re-encode the UTF-16le output
-from Windows PowerShell to your currently configured console codepage.  The
+from PowerShell Desktop to your currently configured console codepage.  The
 output can be forced into a different encoding by changing "default" to one of
 the following:
 
        unicode          - UTF-16le (default output from PowerShell 5.1)
        bigendianunicode - UTF-16
        utf8             - UTF-8
-       utf7             - UTF-7 (no-BOM)
+       utf7             - UTF-7 (no BOM)
        utf32            - UTF-32
        ascii            - 7-bit ASCII character set
        default          - System's active code page (typically ANSI)
        oem              - System's current OEM code page
 
-Note Multi-byte Unicode encodings include a leading BOM.
+Note The abovce multi-byte Unicode encodings include a leading BOM unless
+otherwise indicated.
+
+By default PowerShell Core's output is UTF-8 encoded without a BOM.  If you
+want to force the output of PowerShell Core into a different encoding then set
+'shellredir' and 'shellpipe' to "2>&1 | Out-File -Encoding encoding" where
+encoding is one of the following:
+
+       ascii            - 7-bit ASCII character set
+       bigendianunicode - UTF-16be
+       bigendianutf32   - UTF-32be
+       oem              - System's current OEM code page
+       unicode          - UTF-16le
+       utf7             - UTF-7
+       utf8             - UTF-8
+       utf8BOM          - UTF-8, with BOM
+       utf8NoBOM        - UTF-8, no BOM (default output from PowerShell Core)
+       utf32            - UTF-32
+
+Since PowerShell Core 6.2, the Encoding parameter also supports specifying a
+numeric ID of a registered code page (-Encoding 1251) or string names of
+registered code pages (-Encoding "windows-1251").  The .NET documentation for
+Encoding.CodePage has more information
 
  vim:tw=78:ts=8:noet:ft=help:norl:
index 6c366083f62448d4b7bf20579283c1c46f3d0697..3767cb8a98779897637c5216796c69b15ef2d606 100644 (file)
@@ -30,6 +30,7 @@ File formats                  |dos-file-formats|
 Interrupting                   |dos-CTRL-Break|
 Temp files                     |dos-temp-files|
 Shell option default           |dos-shell|
+PowerShell defaults            |dos-powershell|
 
 Win32 GUI                      |gui-w32|
 
index 55012bfcbdf212ed2b523e5cc64b093bd8b8a038..bd4038c36afd39e29a474cf1eb8345fc990ce930 100644 (file)
@@ -5222,6 +5222,7 @@ vim_tempname(
     WCHAR      *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     char_u     *retval;
     char_u     *p;
+    char_u     *shname;
     long       i;
 
     wcscpy(itmp, L"");
@@ -5247,8 +5248,10 @@ vim_tempname(
     // "sh".  NOTE: This also checks 'shellcmdflag' to help those people who
     // didn't set 'shellslash' but only if not using PowerShell.
     retval = utf16_to_enc(itmp, NULL);
-    if ((strstr((char *)gettail(p_sh), "powershell") == NULL
-                                               && *p_shcf == '-') || p_ssl)
+    shname = gettail(p_sh);
+    if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
+                            || strstr((char *)shname, "pwsh") != NULL ))
+                                                                   || p_ssl)
        for (p = retval; *p; ++p)
            if (*p == '\\')
                *p = '/';
index 6bbfbd775c13d5367d27ca9cf24105ce258afd3a..ead1e635556c8089ab0f171fd92330c90a55add5 100644 (file)
@@ -1414,8 +1414,9 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
     char_u     *escaped_string;
     int                l;
     int                csh_like;
-# ifdef MSWIN
+    char_u     *shname;
     int                powershell;
+# ifdef MSWIN
     int                double_quotes;
 # endif
 
@@ -1425,9 +1426,12 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
     // Csh also needs to have "\n" escaped twice when do_special is set.
     csh_like = csh_like_shell();
 
+    // PowerShell uses it's own version for quoting single quotes
+    shname = gettail(p_sh);
+    powershell = strstr((char *)shname, "pwsh") != NULL;
 # ifdef MSWIN
-    // PowerShell only accepts single quotes so override p_ssl.
-    powershell = strstr((char *)gettail(p_sh), "powershell") != NULL;
+    powershell = powershell || strstr((char *)shname, "powershell") != NULL;
+    // PowerShell only accepts single quotes so override shellslash.
     double_quotes = !powershell && !p_ssl;
 # endif
 
@@ -1445,11 +1449,9 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
 # endif
        if (*p == '\'')
        {
-# ifdef MSWIN
            if (powershell)
                length +=2;             // ' => ''
            else
-# endif
                length += 3;            // ' => '\''
        }
        if ((*p == '\n' && (csh_like || do_newline))
@@ -1497,14 +1499,12 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
 # endif
            if (*p == '\'')
            {
-# ifdef MSWIN
                if (powershell)
                {
                    *d++ = '\'';
                    *d++ = '\'';
                }
                else
-# endif
                {
                    *d++ = '\'';
                    *d++ = '\\';
index 5fc059e29de182f5d88a0a410f7baec0f045cab7..ae303106ad3dd53ab6dcbf2e1cc5213ffc86c9e4 100644 (file)
@@ -933,8 +933,8 @@ set_init_3(void)
            }
        }
 # ifdef MSWIN
-       // PowerShell 5.1/.NET outputs UTF-16 with BOM so re-encode to the
-       // current codepage
+       // Windows PowerShell output is UTF-16 with BOM so re-encode to the
+       // current codepage.
        else if (   fnamecmp(p, "powershell") == 0
                    || fnamecmp(p, "powershell.exe") == 0
                )
@@ -965,6 +965,7 @@ set_init_3(void)
                    || fnamecmp(p, "fish") == 0
                    || fnamecmp(p, "ash") == 0
                    || fnamecmp(p, "dash") == 0
+                   || fnamecmp(p, "pwsh") == 0
 # ifdef MSWIN
                    || fnamecmp(p, "cmd") == 0
                    || fnamecmp(p, "sh.exe") == 0
@@ -976,6 +977,7 @@ set_init_3(void)
                    || fnamecmp(p, "bash.exe") == 0
                    || fnamecmp(p, "cmd.exe") == 0
                    || fnamecmp(p, "dash.exe") == 0
+                   || fnamecmp(p, "pwsh.exe") == 0
 # endif
                    )
            {
@@ -985,7 +987,10 @@ set_init_3(void)
 #  ifdef MSWIN
                    p_sp = (char_u *)">%s 2>&1";
 #  else
-                   p_sp = (char_u *)"2>&1| tee";
+                   if (fnamecmp(p, "pwsh") == 0)
+                       p_sp = (char_u *)">%s 2>&1";
+                   else
+                       p_sp = (char_u *)"2>&1| tee";
 #  endif
                    options[idx_sp].def_val[VI_DEFAULT] = p_sp;
                }
@@ -1011,6 +1016,7 @@ set_init_3(void)
      *                     p_shcf      p_sxq
      * cmd.exe          -   "/c"       "("
      * powershell.exe   -   "-Command" "\""
+     * pwsh.exe                -   "-c"        "\""
      * "sh" like shells -   "-c"       "\""
      *
      * For Win32 p_sxq is set instead of p_shq to include shell redirection.
index 1a005c9d531559c7f3826ceb50b15e40427e1b20..eff2269f8c4de86e85398250d0b47dabe5f0978f 100644 (file)
@@ -2135,6 +2135,7 @@ executable_exists(char *name, char_u **path, int use_path, int use_pathext)
     char_u     *pathbuf = NULL;
     char_u     *pathext = NULL;
     char_u     *pathextbuf = NULL;
+    char_u     *shname = NULL;
     int                noext = FALSE;
     int                retval = FALSE;
 
@@ -2142,8 +2143,10 @@ executable_exists(char *name, char_u **path, int use_path, int use_pathext)
        return FALSE;
 
     // Using the name directly when a Unix-shell like 'shell'.
-    if (strstr((char *)gettail(p_sh), "powershell") == NULL
-                               && strstr((char *)gettail(p_sh), "sh") != NULL)
+    shname = gettail(p_sh);
+    if (strstr((char *)shname, "sh") != NULL &&
+       !(strstr((char *)shname, "powershell") != NULL
+                                   || strstr((char *)shname, "pwsh") != NULL))
        noext = TRUE;
 
     if (use_pathext)
index 753a0d524900656e23ee951f6ca56917ff73169b..f5a3e98997592a743639a37feeaa376264e9e2c5 100644 (file)
@@ -19,7 +19,8 @@ func Test_shell_options()
           \ ['ash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
           \ ['dash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
           \ ['csh', '-c', '|& tee', '', '>&', '', ''],
-          \ ['tcsh', '-c', '|& tee', '', '>&', '', '']]
+          \ ['tcsh', '-c', '|& tee', '', '>&', '', ''],
+          \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '', '']]
   endif
   if has('win32')
     let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''],
@@ -28,6 +29,8 @@ func Test_shell_options()
           \           '', '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
           \ ['powershell', '-Command', '2>&1 | Out-File -Encoding default', '',
           \               '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
+          \ ['pwsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
+          \ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
           \ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
           \ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
           \ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
@@ -61,6 +64,7 @@ func Test_shell_options()
       let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'"
       let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'"
     elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$'
+          \ || e[0] =~# '.*pwsh$' || e[0] =~# '.*pwsh.exe$'
       let str1 = "'cmd \"arg1\" ''arg2'' !%#'"
       let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\#'"
     else
@@ -76,9 +80,14 @@ func Test_shell_options()
       let [&shellcmdflag, &shellpipe, &shellquote, &shellredir,
             \ &shellxescape, &shellxquote] = e[1:6]
       new
-      r !echo hello
-      call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0])
-      bwipe!
+      try
+        r !echo hello
+        call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0])
+      catch
+        call assert_report('Failed to run shell command, shell: ' .. e[0])
+      finally
+        bwipe!
+      endtry
     endif
   endfor
   set shell& shellcmdflag& shellpipe& shellquote&
@@ -149,6 +158,8 @@ func Test_shellslash()
   " ".*\\\\[^\\\\]*$"
   let shells = [['cmd', '/c', '\\', '/'],
         \ ['powershell', '-Command', '\\', '/'],
+        \ ['pwsh', '-Command', '\\', '/'],
+        \ ['pwsh', '-c', '\\', '/'],
         \ ['sh', '-c', '/', '/']]
   for e in shells
     exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1]
index 3c626bb6707ff9cf51bb8002b4cdb4cb702f203e..e9ad71b81614686d20b67492993b2c5ed1c59426 100644 (file)
@@ -755,6 +755,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    3079,
 /**/
     3078,
 /**/