From c7aa9ddf63b9ac911d9f471cc2311afe10fa1c5e Mon Sep 17 00:00:00 2001 From: Christoph Herb Date: Tue, 16 Jan 2024 21:48:35 +0100 Subject: [PATCH] dont provide custom keybinding anymore, allow user to define own keybinding --- README.md | 9 +++++-- lua/gx/handlers/search.lua | 2 +- lua/gx/init.lua | 48 +++++++++++++++++++++----------------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2d646c4..9f302a1 100644 --- a/README.md +++ b/README.md @@ -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` @@ -29,7 +31,7 @@ require("lazy").setup({ { "chrishrb/gx.nvim", - keys = { {"gx", mode = { "n", "x" }} }, + keys = { { "gx", "Browse", mode = { "n", "x" }} }, cmd = { "Browse" }, init = function () vim.g.netrw_nogx = 1 -- disable netrw gx @@ -57,9 +59,12 @@ require("lazy").setup({ }) ``` -## ⌨️ Mappings and Commands +## ⌨️ Mappings * `gx` is overridden by default + +## 📡 API + * `Browse `, e.g. `Browse http://google.de`, `Browse example` ## 🚀 Usage diff --git a/lua/gx/handlers/search.lua b/lua/gx/handlers/search.lua index 7ffb301..97b4459 100644 --- a/lua/gx/handlers/search.lua +++ b/lua/gx/handlers/search.lua @@ -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("") diff --git a/lua/gx/init.lua b/lua/gx/init.lua index 3cdb2ac..2146d98 100644 --- a/lua/gx/init.lua +++ b/lua/gx/init.lua @@ -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) @@ -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 @@ -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