36 lines
1.4 KiB
Lua
36 lines
1.4 KiB
Lua
|
|
function R(name)
|
|
require("plenary.reload").reload_module(name)
|
|
end
|
|
|
|
function websearch(url, o)
|
|
-- Get visual selection or prompt for search
|
|
local search_term
|
|
|
|
-- Check if there's a visual selection
|
|
if vim.fn.mode() == 'v' or vim.fn.mode() == 'V' then
|
|
-- Get the visual selection
|
|
local start_pos = vim.fn.getpos("'<")
|
|
local end_pos = vim.fn.getpos("'>")
|
|
local lines = vim.fn.getline(start_pos[2], end_pos[2])
|
|
local selected_text = table.concat(lines, " ")
|
|
search_term = selected_text
|
|
else
|
|
search_term = (o and o.args) or vim.fn.input("Search: ")
|
|
end
|
|
|
|
-- Encode the search term for URLs
|
|
local escaped = vim.uri_encode(search_term)
|
|
|
|
-- Construct and execute the search URL
|
|
local search_url = (url):format(escaped)
|
|
|
|
vim.ui.open(search_url)
|
|
end
|
|
-- Command to call the search function
|
|
vim.api.nvim_create_user_command('DuckDuckGo', function(o) websearch('https://duckduckgo.com/?q=%s', o) end, { nargs = 1 })
|
|
vim.api.nvim_create_user_command('Docsrs', function(o) websearch('https://docs.rs/%s', o) end, { nargs = 1})
|
|
vim.api.nvim_create_user_command('RustStd', function(o) websearch('https://doc.rust-lang.org/std/?search=%s', o) end, { nargs = 1})
|
|
|
|
vim.keymap.set({ 'n', 'v'}, '<leader>sW', function() websearch('https://duckduckgo.com/?q=%s', null) end, { desc = "Search on DuckDuckGo" })
|