Created
April 22, 2025 20:13
-
-
Save hwayne/a1d51598b7b09fdc37a6adf9657c8e0a to your computer and use it in GitHub Desktop.
Neovim Filelinks Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Drop this in /after or /after/plugin/ | |
-- explanation at commands (at end of file) | |
vim.g.exo = "where you store your data files" | |
local filelinks_file = vim.g.exo .. 'filelinks.json' | |
local function get_filelinks() | |
return vim.fn.json_decode(vim.fn.readfile(filelinks_file)) | |
end | |
local function get_links_for_file(file) | |
local filelinks = get_filelinks() | |
return filelinks[string.lower(file)] | |
end | |
local function add_link_for_file(linkpath) | |
vim.opt.shellslash = true -- to make windows play nice | |
linkpath = string.lower(vim.fs.joinpath(linkpath)) -- normalizes path | |
local filelinks = get_filelinks() | |
local file = string.lower(vim.fn.expand('%:p')) | |
-- todo:: normalize the | |
if filelinks[file] then | |
table.insert(filelinks[file], linkpath) | |
else | |
filelinks[file] = { linkpath } | |
end | |
-- Avoid clobbering the file if invalid | |
local s = vim.fn.json_encode(filelinks) | |
if s then | |
vim.fn.writefile({s}, filelinks_file) | |
end | |
vim.opt.shellslash = false | |
end | |
local function delete_link(file) | |
vim.opt.shellslash = true -- to make windows play nice | |
file = string.lower(vim.fs.joinpath(file)) | |
local filelinks = get_filelinks() | |
local links = get_links_for_file(file) | |
local to_del | |
vim.ui.select( | |
links, {prompt="Delete a link" }, | |
function(_, choice) to_del = choice | |
end | |
) | |
if to_del then | |
table.remove(filelinks[file], to_del) | |
-- Avoid clobbering the file if invalid | |
local s = vim.fn.json_encode(filelinks) | |
if s then | |
vim.fn.writefile({s}, filelinks_file) | |
end | |
end | |
vim.opt.shellslash = false | |
end | |
local function goto_link(file) | |
file = string.lower(vim.fs.joinpath(file)) | |
local links = get_links_for_file(file) | |
local target | |
if vim.tbl_count(links) == 1 then --only one link | |
target = links[1] | |
else | |
vim.ui.select( | |
links, {prompt="", | |
}, function(choice) target = choice end | |
) | |
end | |
if target then | |
vim.cmd.edit(vim.fs.joinpath(target)) -- joinpath normalizes | |
end | |
end | |
-- save another buffer as a link for this file | |
vim.api.nvim_create_user_command('Fladd', function(opts) add_link_for_file(opts.fargs[1]) end, {nargs=1, complete='buffer'}) | |
-- jump to any linked file | |
vim.api.nvim_create_user_command('Fl', function(opts) goto_link(vim.fn.expand('%:p')) end, {}) | |
-- remove a link you hate | |
vim.api.nvim_create_user_command('Fldel', function(opts) delete_link(vim.fn.expand('%:p')) end, {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment