dot/config/nvim/lua/plugin_votion.lua

121 lines
4.0 KiB
Lua
Raw Normal View History

-- define a configuration variable votion_directory that specifies
-- the root location of the votion database all MD files in this directory
-- and it's subdirectories are part of the database.
-- Set votion_directory
local votion_directory = vim.fn.expand("~") .. "/owc"
local actions = require("telescope.actions")
function votion_new()
-- create a new file votion_directory/yyyy-mm-ddThh-mm.md
local current_time = os.date("%Y-%m-%dT%H-%M")
local new_filename = string.format("%s/%s.md", votion_directory, current_time)
vim.api.nvim_command(string.format("edit %s", new_filename))
end
function votion_open()
require("telescope.builtin").find_files({
search_dirs = {votion_directory},
})
end
2023-07-09 23:20:49 +02:00
function votion_insert()
require("telescope.builtin").find_files({
search_dirs = {votion_directory},
attach_mappings = function(_, map)
map('i', '<CR>', function(prompt_bufnr)
local entry = require("telescope.actions.state").get_selected_entry()
require("telescope.actions").close(prompt_bufnr)
vim.api.nvim_put({entry.value}, "b", true, true)
end)
return true
end,
})
end
function votion_grep()
require("telescope.builtin").live_grep({
search_dirs = {votion_directory}
})
end
function votion_rename()
local current_file = vim.api.nvim_buf_get_name(0)
if current_file:find(votion_directory) then
local new_name = vim.fn.input("Rename to: ")
if new_name ~= "" then
local current_file_dir = vim.fn.fnamemodify(current_file, ":h")
new_name = string.format("%s/%s", current_file_dir, new_name)
vim.api.nvim_command(string.format("saveas %s", new_name))
vim.api.nvim_command(string.format("call delete('%s')", current_file))
vim.api.nvim_command(string.format("edit %s)", current_file))
vim.api.nvim_command("bwipeout")
end
else
print("Current file is not part of the Votion database.")
end
end
local function file_exists(file)
local f = io.open(file, "r")
if f ~= nil then io.close(f) return true else return false end
end
function votion_follow()
local cursor_pos = vim.api.nvim_win_get_cursor(0)
local current_line = vim.api.nvim_buf_get_lines(0, cursor_pos[1] - 1, cursor_pos[1], false)[1]
local link_pattern = "%[%[(.-)%]%]"
local link = current_line:match(link_pattern)
if not link then
local url_pattern = "%]%((.-)%)"
link = current_line:match(url_pattern)
end
if link then
local target_file = string.format("%s/%s.md", votion_directory, link)
if file_exists(target_file) then
vim.api.nvim_command(string.format("edit %s", target_file))
else
print(string.format("File '%s' does not exist. Press 'y' to create it.", link))
local user_input = vim.fn.getchar()
if user_input == 121 then
local new_file = string.format("%s/%s.md", votion_directory, link)
vim.api.nvim_command(string.format("edit %s", new_file))
end
end
else
print("No valid link under the cursor.")
end
end
function votion_delete()
local current_file = vim.api.nvim_buf_get_name(0)
if current_file:find(votion_directory, 1, true) then
local confirm_delete = vim.fn.confirm("Are you sure you want to delete this file?", "&Yes\n&No")
if confirm_delete == 1 then
local file_deleted = os.remove(current_file)
if file_deleted then
print("File deleted successfully.")
vim.api.nvim_command("bwipeout")
else
print("File deletion failed.")
end
end
else
print("Current file is not part of the Votion database.")
end
end
vim.api.nvim_command("command! VotionNew lua votion_new()")
vim.api.nvim_command("command! VotionOpen lua votion_open()")
vim.api.nvim_command("command! VotionGrep lua votion_grep()")
vim.api.nvim_command("command! VotionRename lua votion_rename()")
vim.api.nvim_command("command! VotionFollow lua votion_follow()")
vim.api.nvim_command("command! VotionDelete lua votion_delete()")
2023-07-09 23:20:49 +02:00
vim.api.nvim_command("command! VotionInsert lua votion_insert()")