From f48b921e6d607ae9898051c1bb05ca61e829e3d3 Mon Sep 17 00:00:00 2001 From: Christoph <52382992+chrishrb@users.noreply.github.com> Date: Tue, 16 Jan 2024 20:42:29 +0100 Subject: [PATCH] load gx.nvim lazy (#37) --- README.md | 9 +++++++-- lua/gx/init.lua | 35 ++++++++++++++++------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 53ed656..2d646c4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -53,9 +57,10 @@ require("lazy").setup({ }) ``` -## ⌨️ Mappings +## ⌨️ Mappings and Commands * `gx` is overridden by default +* `Browse `, e.g. `Browse http://google.de`, `Browse example` ## 🚀 Usage diff --git a/lua/gx/init.lua b/lua/gx/init.lua index 0cb73e9..3cdb2ac 100644 --- a/lua/gx/init.lua +++ b/lua/gx/init.lua @@ -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) @@ -21,10 +13,6 @@ 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, @@ -32,12 +20,15 @@ function M.browse(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 @@ -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