@@ -10,6 +10,7 @@ local M = {
10
10
}
11
11
12
12
M .is_windows = vim .fn .has " win32" == 1 or vim .fn .has " win32unix" == 1
13
+ M .is_wsl = vim .fn .has " wsl" == 1
13
14
14
15
function M .path_to_matching_str (path )
15
16
return path :gsub (" (%-)" , " (%%-)" ):gsub (" (%.)" , " (%%.)" ):gsub (" (%_)" , " (%%_)" )
171
172
--- @return boolean
172
173
function M .is_windows_exe (ext )
173
174
if not M .pathexts then
174
- local PATHEXT = vim .env .PATHEXT or " "
175
- local wexe = vim .split (PATHEXT :gsub (" %." , " " ), " ;" )
175
+ if not vim .env .PATHEXT then
176
+ return false
177
+ end
178
+
179
+ local wexe = vim .split (vim .env .PATHEXT :gsub (" %." , " " ), " ;" )
176
180
M .pathexts = {}
177
181
for _ , v in pairs (wexe ) do
178
182
M .pathexts [v ] = true
@@ -182,6 +186,44 @@ function M.is_windows_exe(ext)
182
186
return M .pathexts [ext :upper ()]
183
187
end
184
188
189
+ --- Check whether path maps to Windows filesystem mounted by WSL
190
+ -- @param path string
191
+ -- @return boolean
192
+ function M .is_wsl_windows_fs_path (path )
193
+ -- Run 'wslpath' command to try translating WSL path to Windows path.
194
+ -- Consume stderr output as well because 'wslpath' can produce permission
195
+ -- errors on some files (e.g. temporary files in root of system drive).
196
+ local handle = io.popen (' wslpath -w "' .. path .. ' " 2>/dev/null' )
197
+ if handle then
198
+ local output = handle :read " *a"
199
+ handle :close ()
200
+
201
+ return string.find (output , " ^\\\\ wsl$\\ " ) == nil
202
+ end
203
+
204
+ return false
205
+ end
206
+
207
+ --- Check whether extension is Windows executable under WSL
208
+ -- @param ext string
209
+ -- @return boolean
210
+ function M .is_wsl_windows_fs_exe (ext )
211
+ if not vim .env .PATHEXT then
212
+ -- Extract executable extensions from within WSL.
213
+ -- Redirect stderr to null to silence warnings when
214
+ -- Windows command is executed from Linux filesystem:
215
+ -- > CMD.EXE was started with the above path as the current directory.
216
+ -- > UNC paths are not supported. Defaulting to Windows directory.
217
+ local handle = io.popen ' cmd.exe /c "echo %PATHEXT%" 2>/dev/null'
218
+ if handle then
219
+ vim .env .PATHEXT = handle :read " *a"
220
+ handle :close ()
221
+ end
222
+ end
223
+
224
+ return M .is_windows_exe (ext )
225
+ end
226
+
185
227
function M .rename_loaded_buffers (old_path , new_path )
186
228
for _ , buf in pairs (a .nvim_list_bufs ()) do
187
229
if a .nvim_buf_is_loaded (buf ) then
0 commit comments