Skip to content

Commit

Permalink
load gx.nvim lazy (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishrb authored Jan 16, 2024
1 parent 38d776a commit f48b921
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
require("lazy").setup({
{
"chrishrb/gx.nvim",
event = { "BufEnter" },
keys = { {"gx", mode = { "n", "x" }} },
cmd = { "Browse" },
init = function ()
vim.g.netrw_nogx = 1 -- disable netrw gx
end,
dependencies = { "nvim-lua/plenary.nvim" },
config = true, -- default settings

Expand All @@ -53,9 +57,10 @@ require("lazy").setup({
})
```

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

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

## 🚀 Usage

Expand Down
35 changes: 16 additions & 19 deletions lua/gx/init.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
local helper = require("gx.helper")

local keymap = vim.keymap.set
local sysname = vim.loop.os_uname().sysname

local M = {}

-- search for url with handler
local function search_for_url()
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)

function M.browse(mode, line)
-- search for url
local url =
require("gx.handler").get_url(mode, line, M.options.handlers, M.options.handler_options)
Expand All @@ -21,23 +13,22 @@ local function search_for_url()
return
end

return M.browse(url)
end

function M.browse(url)
return require("gx.shell").execute_with_error(
M.options.open_browser_app,
M.options.open_browser_args,
url
)
end

-- create keybindings
local function bind_keys()
vim.g.netrw_nogx = 1 -- disable netrw gx
-- search for url with handler
local function open()
local line = vim.api.nvim_get_current_line()
local mode = vim.api.nvim_get_mode().mode

local opts = { noremap = true, silent = true }
keymap({ "n", "x" }, "gx", search_for_url, opts)
-- 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
Expand Down Expand Up @@ -87,12 +78,18 @@ 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)
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(opts.fargs[1])
M.browse("v", opts.fargs[1])
end, { nargs = 1 })
end

Expand Down

0 comments on commit f48b921

Please sign in to comment.