Skip to content

Commit

Permalink
dont provide custom keybinding anymore, allow user to define own keyb…
Browse files Browse the repository at this point in the history
…inding
  • Loading branch information
chrishrb committed Jan 16, 2024
1 parent f48b921 commit c7aa9dd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

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

* open links without `netrw`
Expand Down Expand Up @@ -29,7 +31,7 @@
require("lazy").setup({
{
"chrishrb/gx.nvim",
keys = { {"gx", mode = { "n", "x" }} },
keys = { { "gx", "<cmd>Browse<cr>", mode = { "n", "x" }} },
cmd = { "Browse" },
init = function ()
vim.g.netrw_nogx = 1 -- disable netrw gx
Expand Down Expand Up @@ -57,9 +59,12 @@ require("lazy").setup({
})
```

## ⌨️ Mappings and Commands
## ⌨️ Mappings

* `gx` is overridden by default

## 📡 API

* `Browse <URL or WORDS>`, e.g. `Browse http://google.de`, `Browse example`

## 🚀 Usage
Expand Down
2 changes: 1 addition & 1 deletion lua/gx/handlers/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local M = {
function M.handle(mode, line, handler_options)
local search_pattern

if mode == "v" then
if mode == "v" or mode == "c" then
search_pattern = line
else
search_pattern = vim.fn.expand("<cword>")
Expand Down
48 changes: 27 additions & 21 deletions lua/gx/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ local sysname = vim.loop.os_uname().sysname

local M = {}

function M.browse(mode, line)
-- search for url
-- search for url with handler
function M.open(mode, line)
if not line then
line = vim.api.nvim_get_current_line()
mode = vim.api.nvim_get_mode().mode
end

-- cut if in visual mode
line = helper.cut_with_visual_mode(mode, line)

local url =
require("gx.handler").get_url(mode, line, M.options.handlers, M.options.handler_options)

Expand All @@ -20,17 +28,6 @@ function M.browse(mode, line)
)
end

-- search for url with handler
local function open()
local line = vim.api.nvim_get_current_line()
local mode = vim.api.nvim_get_mode().mode

-- cut if in visual mode
line = helper.cut_with_visual_mode(mode, line)

return M.browse(mode, line)
end

-- get the app for opening the webbrowser
local function get_open_browser_app()
local app
Expand Down Expand Up @@ -78,19 +75,28 @@ local function with_defaults(options)
}
end

local function bind_keys()
vim.g.netrw_nogx = 1 -- disable netrw gx
local opts = { noremap = true, silent = true }
vim.keymap.set({ "n", "x" }, "gx", open, opts)
local function bind_command()
vim.api.nvim_create_user_command("Browse", function(opts)
local fargs = opts.fargs[1]
if fargs then
M.open("c", fargs)
return
end

if opts.range == 2 then
local range = vim.fn.getline(opts.line1)
M.open("v", range)
return
end

M.open()
end, { nargs = "?", range = 1 })
end

-- setup function
function M.setup(options)
M.options = with_defaults(options)
bind_keys()
vim.api.nvim_create_user_command("Browse", function(opts)
M.browse("v", opts.fargs[1])
end, { nargs = 1 })
bind_command()
end

M.options = nil
Expand Down

0 comments on commit c7aa9dd

Please sign in to comment.