diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2023-07-05 00:30:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-05 00:30:05 -0700 |
commit | 5936a88f181e52e17484d4ae6dfaea7d50d43935 (patch) | |
tree | ef727393e94037e5a675f566b59b1014f8fe2a29 /runtime/lua/vim/ui.lua | |
parent | b2e8c0df2062f765a4cf7d96379c5f0f19393dfd (diff) | |
parent | e644e7ce0b36dd5e75770f3faa0a84f15e2561e8 (diff) | |
download | rneovim-5936a88f181e52e17484d4ae6dfaea7d50d43935.tar.gz rneovim-5936a88f181e52e17484d4ae6dfaea7d50d43935.tar.bz2 rneovim-5936a88f181e52e17484d4ae6dfaea7d50d43935.zip |
Merge #23401 vim.ui.open: "gx" without netrw
Diffstat (limited to 'runtime/lua/vim/ui.lua')
-rw-r--r-- | runtime/lua/vim/ui.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index aaee175f3a..fd06611da2 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -104,4 +104,54 @@ function M.input(opts, on_confirm) end end +--- Opens `path` with the system default handler (macOS `open`, Windows `explorer.exe`, Linux +--- `xdg-open`, …), or returns (but does not show) an error message on failure. +--- +--- Expands "~/" and environment variables in filesystem paths. +--- +--- Examples: +--- <pre>lua +--- vim.ui.open("https://neovim.io/") +--- vim.ui.open("~/path/to/file") +--- vim.ui.open("$VIMRUNTIME") +--- </pre> +--- +---@param path string Path or URL to open +--- +---@return SystemCompleted|nil # Command result, or nil if not found. +---@return string|nil # Error message on failure +--- +---@see |vim.system()| +function M.open(path) + vim.validate({ + path = { path, 'string' }, + }) + local is_uri = path:match('%w+:') + if not is_uri then + path = vim.fn.expand(path) + end + + local cmd + + if vim.fn.has('mac') == 1 then + cmd = { 'open', path } + elseif vim.fn.has('win32') == 1 then + cmd = { 'explorer', path } + elseif vim.fn.executable('wslview') == 1 then + cmd = { 'wslview', path } + elseif vim.fn.executable('xdg-open') == 1 then + cmd = { 'xdg-open', path } + else + return nil, 'vim.ui.open: no handler found (tried: wslview, xdg-open)' + end + + local rv = vim.system(cmd, { text = true, detach = true }):wait() + if rv.code ~= 0 then + local msg = ('vim.ui.open: command failed (%d): %s'):format(rv.code, vim.inspect(cmd)) + return rv, msg + end + + return rv, nil +end + return M |