Skip to content

Commit a7cb094

Browse files
authored
Add search for word / selection under cursor (#22)
* Add search for word / selection under cursor * lint everything
1 parent 8d16f23 commit a7cb094

File tree

12 files changed

+80
-11
lines changed

12 files changed

+80
-11
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* open plugins in the browser with a single command (e.g. in lazy, packer you can hover over a plugin name, simply press `gx` and you get to the github page of the plugin)
1111
* open github issues directly in the browser (e.g. `Fixes #22` opens `https://github.com/chrishrb/gx.nvim/issues/22`)
1212
* dependencies from `package.json` (e.g. line `"express": "^4.18.2",` in the `package.json` opens `https://www.npmjs.com/package/express`)
13+
* if there is no url found under the cursor, the word/selection is automatically searched on the web
1314
* support for macOS, Linux and Windows
1415
* more to come (jira issues, ..)
1516

@@ -39,6 +40,10 @@ require("lazy").setup({
3940
plugin = true, -- open plugin links in lua (e.g. packer, lazy, ..)
4041
github = true, -- open github issues
4142
package_json = true, -- open dependencies from package.json
43+
search = true, -- search the web/selection on the web if nothing else is found
44+
},
45+
handler_options = {
46+
search_engine = "google", -- you can select between google, bing and duckduckgo
4247
},
4348
} end,
4449
},

lua/gx/handler.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local url_handler = require("gx.handlers.url")
55
local github_handler = require("gx.handlers.github")
66
local commit_handler = require("gx.handlers.commit")
77
local markdown_handler = require("gx.handlers.markdown")
8+
local search_handler = require("gx.handlers.search")
89

910
local M = {}
1011

@@ -20,7 +21,7 @@ local function add_handler(handlers, handler, active)
2021
end
2122

2223
-- handler function
23-
function M.get_url(mode, line, activated_handlers)
24+
function M.get_url(mode, line, activated_handlers, handler_options)
2425
local url
2526
local handlers = {}
2627
local tkeys = {}
@@ -32,6 +33,7 @@ function M.get_url(mode, line, activated_handlers)
3233
add_handler(handlers, commit_handler, activated_handlers.github)
3334
add_handler(handlers, markdown_handler, true)
3435
add_handler(handlers, url_handler, true)
36+
add_handler(handlers, search_handler, activated_handlers.search)
3537
-- ###
3638

3739
for k in pairs(handlers) do
@@ -40,7 +42,7 @@ function M.get_url(mode, line, activated_handlers)
4042
table.sort(tkeys)
4143

4244
for _, k in ipairs(tkeys) do
43-
url = handlers[k].handle(mode, line)
45+
url = handlers[k].handle(mode, line, handler_options)
4446

4547
if url then
4648
break

lua/gx/handlers/commit.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ M.filetype = nil
88
M.filename = nil
99

1010
-- navigate to github url for commit
11-
function M.handle(mode, line)
11+
function M.handle(mode, line, _)
1212
local pattern = "(%x%x%x%x%x%x%x+)"
1313
local commit_hash = helper.find(line, mode, pattern)
1414
if not commit_hash or #commit_hash > 40 then

lua/gx/handlers/github.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ M.filetype = nil
88
M.filename = nil
99

1010
-- navigate to neovim github plugin url
11-
function M.handle(mode, line)
11+
function M.handle(mode, line, _)
1212
local pattern = "%a*%s#(%d*)"
1313
local github_issue = helper.find(line, mode, pattern)
1414
if not github_issue then

lua/gx/handlers/markdown.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ M.filetype = { "markdown" }
88
M.filename = nil
99

1010
-- navigate to neovim github plugin url
11-
function M.handle(mode, line)
11+
function M.handle(mode, line, _)
1212
local pattern = "%[.*%]%((https?://[a-zA-Z0-9_/%-%.~@\\+#=?&]+)%)"
1313
local url = helper.find(line, mode, pattern)
1414
return url

lua/gx/handlers/package_json.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ M.filetype = { "json" }
77
M.filename = "package.json"
88

99
-- navigate to neovim github plugin url
10-
function M.handle(mode, line)
10+
function M.handle(mode, line, _)
1111
local pattern = '["]([^%s]*)["]:'
1212
local npm_package = helper.find(line, mode, pattern)
1313
if not npm_package then

lua/gx/handlers/plugin.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ M.filetype = { "lua", "vim" }
77
M.filename = nil
88

99
-- navigate to neovim github plugin url
10-
function M.handle(mode, line)
10+
function M.handle(mode, line, _)
1111
local pattern = "[\"']([^%s~/]*/[^%s~/]*)[\"']"
1212
local username_repo = helper.find(line, mode, pattern)
1313
if username_repo then

lua/gx/handlers/search.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
local helper = require("gx.helper")
2+
local notfier = require("gx.notfier")
3+
4+
local M = {}
5+
6+
-- every filetype and filename
7+
M.filetype = nil
8+
M.filename = nil
9+
10+
-- navigate to github url for commit
11+
function M.handle(mode, line, handler_options)
12+
local search_pattern
13+
14+
if mode == "v" then
15+
search_pattern = line
16+
else
17+
search_pattern = vim.fn.expand("<cword>")
18+
end
19+
20+
local search_engine_url = helper.get_search_url_from_engine(handler_options.search_engine)
21+
if search_engine_url == nil then
22+
notfier.error("search engine " .. handler_options.search_engine .. " not found!")
23+
return
24+
end
25+
26+
return search_engine_url .. helper.urlencode(search_pattern)
27+
end
28+
29+
return M

lua/gx/handlers/url.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ M.filetype = nil
77
M.filename = nil
88

99
-- get url from line (with http/s)
10-
function M.handle(mode, line)
11-
local pattern = "(https?://[a-zA-Z0-9_/%-%.~@\\+#=?&]+)"
10+
function M.handle(mode, line, _)
11+
local pattern = "(https?://[a-zA-Z0-9_/%-%.~@\\+#=?&:]+)"
1212
local url = helper.find(line, mode, pattern)
1313

1414
-- match url without http(s)
1515
if not url then
16-
pattern = "([a-zA-Z0-9_/%-%.~@\\+#]+%.[a-zA-Z0-9_/%-%.~@\\+#%=?&]+)"
16+
pattern = "([a-zA-Z0-9_/%-%.~@\\+#]+%.[a-zA-Z0-9_/%-%.~@\\+#%=?&:]+)"
1717
url = helper.find(line, mode, pattern)
1818
if url then
1919
return "https://" .. url

lua/gx/helper.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,28 @@ function M.check_filename(handler_filename)
107107
return false
108108
end
109109

110+
local char_to_hex = function(c)
111+
return string.format("%%%02X", string.byte(c))
112+
end
113+
114+
function M.urlencode(url)
115+
if url == nil then
116+
return
117+
end
118+
url = url:gsub("\n", "\r\n")
119+
url = string.gsub(url, "([^%w _%%%-%.~])", char_to_hex)
120+
url = url:gsub(" ", "+")
121+
return url
122+
end
123+
124+
function M.get_search_url_from_engine(engine)
125+
local search_url = {
126+
google = "https://www.google.com/search?q=",
127+
bing = "https://www.bing.com/search?q=",
128+
duckduckgo = "https://duckduckgo.com/?q=",
129+
}
130+
131+
return search_url[engine]
132+
end
133+
110134
return M

0 commit comments

Comments
 (0)