Skip to content

Commit f48b921

Browse files
authored
load gx.nvim lazy (#37)
1 parent 38d776a commit f48b921

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
require("lazy").setup({
3030
{
3131
"chrishrb/gx.nvim",
32-
event = { "BufEnter" },
32+
keys = { {"gx", mode = { "n", "x" }} },
33+
cmd = { "Browse" },
34+
init = function ()
35+
vim.g.netrw_nogx = 1 -- disable netrw gx
36+
end,
3337
dependencies = { "nvim-lua/plenary.nvim" },
3438
config = true, -- default settings
3539

@@ -53,9 +57,10 @@ require("lazy").setup({
5357
})
5458
```
5559

56-
## ⌨️ Mappings
60+
## ⌨️ Mappings and Commands
5761

5862
* `gx` is overridden by default
63+
* `Browse <URL or WORDS>`, e.g. `Browse http://google.de`, `Browse example`
5964

6065
## 🚀 Usage
6166

lua/gx/init.lua

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
local helper = require("gx.helper")
22

3-
local keymap = vim.keymap.set
43
local sysname = vim.loop.os_uname().sysname
54

65
local M = {}
76

8-
-- search for url with handler
9-
local function search_for_url()
10-
local line = vim.api.nvim_get_current_line()
11-
local mode = vim.api.nvim_get_mode().mode
12-
13-
-- cut if in visual mode
14-
line = helper.cut_with_visual_mode(mode, line)
15-
7+
function M.browse(mode, line)
168
-- search for url
179
local url =
1810
require("gx.handler").get_url(mode, line, M.options.handlers, M.options.handler_options)
@@ -21,23 +13,22 @@ local function search_for_url()
2113
return
2214
end
2315

24-
return M.browse(url)
25-
end
26-
27-
function M.browse(url)
2816
return require("gx.shell").execute_with_error(
2917
M.options.open_browser_app,
3018
M.options.open_browser_args,
3119
url
3220
)
3321
end
3422

35-
-- create keybindings
36-
local function bind_keys()
37-
vim.g.netrw_nogx = 1 -- disable netrw gx
23+
-- search for url with handler
24+
local function open()
25+
local line = vim.api.nvim_get_current_line()
26+
local mode = vim.api.nvim_get_mode().mode
3827

39-
local opts = { noremap = true, silent = true }
40-
keymap({ "n", "x" }, "gx", search_for_url, opts)
28+
-- cut if in visual mode
29+
line = helper.cut_with_visual_mode(mode, line)
30+
31+
return M.browse(mode, line)
4132
end
4233

4334
-- get the app for opening the webbrowser
@@ -87,12 +78,18 @@ local function with_defaults(options)
8778
}
8879
end
8980

81+
local function bind_keys()
82+
vim.g.netrw_nogx = 1 -- disable netrw gx
83+
local opts = { noremap = true, silent = true }
84+
vim.keymap.set({ "n", "x" }, "gx", open, opts)
85+
end
86+
9087
-- setup function
9188
function M.setup(options)
9289
M.options = with_defaults(options)
9390
bind_keys()
9491
vim.api.nvim_create_user_command("Browse", function(opts)
95-
M.browse(opts.fargs[1])
92+
M.browse("v", opts.fargs[1])
9693
end, { nargs = 1 })
9794
end
9895

0 commit comments

Comments
 (0)