Skip to content

Commit dc25136

Browse files
committed
initial commit
0 parents  commit dc25136

File tree

9 files changed

+158
-0
lines changed

9 files changed

+158
-0
lines changed

.stylua.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
column_width = 100
2+
line_endings = "Unix"
3+
indent_type = "Spaces"
4+
indent_width = 2
5+
quote_style = "AutoPreferDouble"
6+
call_parentheses = "None"

Makefile

Whitespace-only changes.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gx.nvim

after/plugin/gx.lua

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if vim.g.loaded_gx_plugin then
2+
return
3+
end
4+
5+
vim.g.loaded_gx_plugin = true
6+
7+
require("gx").setup()

lua/gx/execute_command.lua

Whitespace-only changes.

lua/gx/helper.lua

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local M = {}
2+
3+
-- get visual selection
4+
local function visual_selection_range()
5+
local _, csrow, cscol, _ = unpack(vim.fn.getpos "'<")
6+
local _, cerow, cecol, _ = unpack(vim.fn.getpos "'>")
7+
8+
if csrow < cerow or (csrow == cerow and cscol <= cecol) then
9+
return cscol - 1, cecol
10+
else
11+
return cecol - 1, cscol
12+
end
13+
end
14+
15+
-- check that cursor on uri in normal mode
16+
function M.checkIfCursorOnUri(mode, i, j)
17+
if mode ~= "n" then
18+
return true
19+
end
20+
21+
local col = vim.api.nvim_win_get_cursor(0)[2]
22+
if i <= (col + 1) and j >= (col + 1) then
23+
print(i, col, j)
24+
return true
25+
end
26+
27+
return false
28+
end
29+
30+
-- cut line if in visual mode
31+
function M.cutWithVisualMode(mode, line)
32+
if mode ~= "v" then
33+
return line
34+
end
35+
36+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<esc>", true, false, true), "x", false)
37+
local i, j = visual_selection_range()
38+
return string.sub(line, i + 1, j)
39+
end
40+
41+
return M

lua/gx/init.lua

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

lua/gx/notfier.lua

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
local M = {}
2+
3+
function M.t(str)
4+
return vim.api.nvim_replace_termcodes(str, true, true, true)
5+
end
6+
7+
function M.log(msg, hl, name)
8+
name = name or "Neovim"
9+
hl = hl or "Todo"
10+
vim.api.nvim_echo({ { name .. ": ", hl }, { msg } }, true, {})
11+
end
12+
13+
function M.warn(msg, name)
14+
vim.notify(msg, vim.log.levels.WARN, { title = name or "Warning" })
15+
end
16+
17+
function M.error(msg, name)
18+
vim.notify(msg, vim.log.levels.ERROR, { title = name or "Error Message" })
19+
end
20+
21+
function M.info(msg, name)
22+
vim.notify(msg, vim.log.levels.INFO, { title = name or "Information" })
23+
end
24+
25+
return M

lua/gx/parser.lua

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local pattern = "[%a]*://.*%.[/?_%d%a]*"
2+
3+
local M = {}
4+
5+
-- get uri from line
6+
function M.getUrl(line)
7+
local i, j = string.find(line, pattern)
8+
9+
if not i then
10+
return i, j, nil
11+
end
12+
13+
return i, j, string.sub(line, i, j)
14+
end
15+
16+
-- TODO: add more parser
17+
18+
return M

0 commit comments

Comments
 (0)