Skip to content

Commit eba0eef

Browse files
authored
Breaking change: Dont provide custom keybinding anymore, allow user to define own keybindings (#39)
1 parent f48b921 commit eba0eef

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
![ci](https://github.com/chrishrb/gx.nvim/actions/workflows/ci.yml/badge.svg)
44

5+
> ATTENTION: There was a breaking change in version v0.5.0. The keybinding `gx` must now be configured manually. See [Installation](#-installation)
6+
57
## ✨ Features
68

79
* open links without `netrw`
810
* normal and visual mode support
911
* links with/without an explicit protocol (e.g. `google.com` will open `https://google.com`)
10-
* open plugins in the browser with a single command (e.g. in lazy, packer you can hover over a plugin name, simply press `gx` and you get to the github page of the plugin)
12+
* open plugins in the browser with a single command (e.g. in lazy, packer you can hover over a plugin name, simply press `gx` or execute command `Browse` and you get to the github page of the plugin)
1113
* open github issues directly in the browser (e.g. `Fixes #22` opens `https://github.com/chrishrb/gx.nvim/issues/22`)
1214
* dependencies from `package.json` (e.g. line `"express": "^4.18.2",` in the `package.json` opens `https://www.npmjs.com/package/express`)
1315
* formulae and casks from `Brewfile` (e.g. line `brew "neovim"` in the `Brewfile` opens `https://formulae.brew.sh/formula/neovim`)
@@ -29,7 +31,7 @@
2931
require("lazy").setup({
3032
{
3133
"chrishrb/gx.nvim",
32-
keys = { {"gx", mode = { "n", "x" }} },
34+
keys = { { "gx", "<cmd>Browse<cr>", mode = { "n", "x" }} },
3335
cmd = { "Browse" },
3436
init = function ()
3537
vim.g.netrw_nogx = 1 -- disable netrw gx
@@ -57,10 +59,10 @@ require("lazy").setup({
5759
})
5860
```
5961

60-
## ⌨️ Mappings and Commands
62+
## 📡 Commands
6163

62-
* `gx` is overridden by default
6364
* `Browse <URL or WORDS>`, e.g. `Browse http://google.de`, `Browse example`
65+
* OR hover/select words, links and more and execute command `Browse`
6466

6567
## 🚀 Usage
6668

lua/gx/handlers/search.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local M = {
1010
function M.handle(mode, line, handler_options)
1111
local search_pattern
1212

13-
if mode == "v" then
13+
if mode == "v" or mode == "c" then
1414
search_pattern = line
1515
else
1616
search_pattern = vim.fn.expand("<cword>")

lua/gx/init.lua

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@ local sysname = vim.loop.os_uname().sysname
44

55
local M = {}
66

7-
function M.browse(mode, line)
8-
-- search for url
7+
-- search for url with handler
8+
function M.open(mode, line)
9+
if not line then
10+
line = vim.api.nvim_get_current_line()
11+
mode = vim.api.nvim_get_mode().mode
12+
end
13+
14+
-- cut if in visual mode
15+
line = helper.cut_with_visual_mode(mode, line)
16+
917
local url =
1018
require("gx.handler").get_url(mode, line, M.options.handlers, M.options.handler_options)
1119

@@ -20,17 +28,6 @@ function M.browse(mode, line)
2028
)
2129
end
2230

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
27-
28-
-- cut if in visual mode
29-
line = helper.cut_with_visual_mode(mode, line)
30-
31-
return M.browse(mode, line)
32-
end
33-
3431
-- get the app for opening the webbrowser
3532
local function get_open_browser_app()
3633
local app
@@ -78,19 +75,28 @@ local function with_defaults(options)
7875
}
7976
end
8077

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)
78+
local function bind_command()
79+
vim.api.nvim_create_user_command("Browse", function(opts)
80+
local fargs = opts.fargs[1]
81+
if fargs then
82+
M.open("c", fargs)
83+
return
84+
end
85+
86+
if opts.range == 2 then
87+
local range = vim.fn.getline(opts.line1)
88+
M.open("v", range)
89+
return
90+
end
91+
92+
M.open()
93+
end, { nargs = "?", range = 1 })
8594
end
8695

8796
-- setup function
8897
function M.setup(options)
8998
M.options = with_defaults(options)
90-
bind_keys()
91-
vim.api.nvim_create_user_command("Browse", function(opts)
92-
M.browse("v", opts.fargs[1])
93-
end, { nargs = 1 })
99+
bind_command()
94100
end
95101

96102
M.options = nil

0 commit comments

Comments
 (0)