|
| 1 | +local notfier = require "lua.gx.notfier" |
| 2 | +local helper = require "lua.gx.helper" |
| 3 | +local parser = require "lua.gx.parser" |
| 4 | + |
| 5 | +local keymap = vim.keymap.set |
| 6 | +local sysname = vim.loop.os_uname().sysname |
| 7 | +local opts = { noremap = true, silent = true } |
| 8 | + |
| 9 | +if not sysname == "Darwin" or not sysname == "Linux" then |
| 10 | + notfier.error "Windows is not supported at the moment" |
| 11 | + return |
| 12 | +end |
| 13 | + |
| 14 | +local M = {} |
| 15 | + |
| 16 | +-- execute command on macOs and linux |
| 17 | +local function executeCommand(uri) |
| 18 | + local app |
| 19 | + |
| 20 | + if sysname == "Darwin" then |
| 21 | + app = "open" |
| 22 | + elseif sysname == "Linux" then |
| 23 | + app = "xdg-open" |
| 24 | + end |
| 25 | + |
| 26 | + local command = app .. ' "' .. uri .. '"' |
| 27 | + os.execute(command) |
| 28 | +end |
| 29 | + |
| 30 | +local function searchForUrl() |
| 31 | + local line = vim.api.nvim_get_current_line() |
| 32 | + local mode = vim.api.nvim_get_mode().mode |
| 33 | + |
| 34 | + -- cut if in visual mode |
| 35 | + line = helper.cutWithVisualMode(mode, line) |
| 36 | + |
| 37 | + -- search for url |
| 38 | + local i, j, url = parser.getUrl(line) |
| 39 | + |
| 40 | + if not helper.checkIfCursorOnUri(mode, i, j) then |
| 41 | + return |
| 42 | + end |
| 43 | + |
| 44 | + executeCommand(url) |
| 45 | +end |
| 46 | + |
| 47 | +local function bindKeys() |
| 48 | + vim.g.netrw_nogx = 1 -- disable netrw gx |
| 49 | + |
| 50 | + keymap("n", "gx", searchForUrl, opts) |
| 51 | + keymap("v", "gx", searchForUrl, opts) |
| 52 | +end |
| 53 | + |
| 54 | +function M.setup() |
| 55 | + bindKeys() |
| 56 | +end |
| 57 | + |
| 58 | +M.setup() |
| 59 | + |
| 60 | +return M |
0 commit comments