fix(gx): visual selection, expand env vars

---
Rejected experiment: move vim.ui.open() to vim.env.open()

Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.

Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.

diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
   end,
 })

-vim.env = setmetatable({}, {
-  __index = function(_, k)
+vim.env = setmetatable({
+  open = setmetatable({}, {
+      __call = function(_, uri)
+        print('xxxxx'..uri)
+        return true
+      end,
+      __tostring = function()
+        local v = vim.fn.getenv('open')
+        if v == vim.NIL then
+          return nil
+        end
+        return v
+      end,
+    })
+  },
+  {
+  __index = function(t, k, ...)
+    if k == 'open' then
+      error()
+      -- vim.print({...})
+      -- return rawget(t, k)
+    end
     local v = vim.fn.getenv(k)
     if v == vim.NIL then
       return nil
This commit is contained in:
Justin M. Keyes
2023-07-02 16:51:30 +02:00
parent af6e6ccf3d
commit 67b2ed1004
7 changed files with 82 additions and 78 deletions

View File

@@ -19,10 +19,15 @@ vim.api.nvim_create_user_command('InspectTree', function(cmd)
end
end, { desc = 'Inspect treesitter language tree for buffer', count = true })
if vim.g.use_lua_gx == nil or vim.g.use_lua_gx == true then
vim.keymap.set({ 'n', 'x' }, 'gx', function()
local uri = vim.fn.expand('<cfile>')
vim.ui.open(uri)
end, { desc = 'Open URI under cursor with system app' })
-- TODO: use vim.region() when it lands... #13896 #16843
local function get_visual_selection()
local save_a = vim.fn.getreginfo('a')
vim.cmd[[norm! "ay]]
local selection = vim.fn.getreg('a', 1)
vim.fn.setreg('a', save_a)
return selection
end
local gx_desc = 'Opens filepath or URI under cursor with the system handler (file explorer, web browser, …)'
vim.keymap.set({ 'n' }, 'gx', function() vim.ui.open(vim.fn.expand('<cfile>')) end, { desc = gx_desc })
vim.keymap.set({ 'x' }, 'gx', function() vim.ui.open(get_visual_selection()) end, { desc = gx_desc })