Add votion and gpt commands and keybindings

main
felixm 2023-06-18 09:06:49 -04:00
parent fd069d4a9d
commit 8bbe6c5269
6 changed files with 129 additions and 11 deletions

View File

@ -1,7 +1,8 @@
require('plugins')
require('mappings')
require('telescope_config')
require('gpt')
require('mappings')
require('plugins')
require('telescope_config')
require('votion')
-- Misc commands similar to my vimrc
vim.g.mapleader = ","
@ -12,4 +13,5 @@ vim.opt.smarttab = true
vim.opt.relativenumber = true
vim.api.nvim_set_keymap('i', 'jk', '<Esc>', {noremap = true})
vim.cmd.colorscheme "catppuccin-frappe"
vim.g.markdown_folding = 1

View File

@ -19,5 +19,4 @@ function gpt_complete_file()
print(message)
end
vim.api.nvim_command('command! GptCompleteFile lua gpt_complete_file()')

View File

@ -24,7 +24,7 @@ wk.register({
a = { "<cmd>unhide<cr>", "Show All" },
o = { "<cmd>Telescope buffers<cr>", "Open" },
e = { "<cmd>Oil<cr>", "Explorer" },
g = { "<cmd>w!<cr>|GptCompleteFile<cr>", "GPT Complete" },
g = { "<cmd>GptCompleteFile<cr>", "GPT Complete" },
},
}, { prefix = "<leader>" })
@ -49,3 +49,16 @@ wk.register({
}
},
}, { prefix = "<leader>" })
wk.register({
v = {
name = "Votion",
n = { "<cmd>VotionNew<cr>", "New" },
o = { "<cmd>VotionOpen<cr>", "Open" },
g = { "<cmd>VotionGrep<cr>", "Grep" },
r = { "<cmd>VotionRename<cr>", "Rename" },
f = { "<cmd>VotionFollow<cr>", "Follow" },
d = { "<cmd>VotionDelete<cr>", "Delete" },
},
}, { prefix = "<leader>" })

View File

@ -1,5 +1,5 @@
-- Declare the function in the _G table to make it globally available
_G.SplitOrMove = function(direction)
-- Function to either create or move to splits
function split_or_move(direction)
if vim.fn.winnr() ~= vim.fn.winnr(direction) then
vim.cmd(":wincmd " .. direction)
else
@ -16,5 +16,4 @@ _G.SplitOrMove = function(direction)
end
-- Create a command that you can reference from your which-key register
vim.cmd("command! -nargs=1 SplitOrMove lua _G.SplitOrMove(<q-args>)")
vim.api.nvim_command("command! -nargs=1 SplitOrMove lua split_or_move(<q-args>)")

105
config/nvim/lua/votion.lua Normal file
View File

@ -0,0 +1,105 @@
-- 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/vault"
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
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()")

View File

@ -15,8 +15,8 @@ else
ZSH_THEME="norm"
fi
alias v="vim"
alias vi="vim"
alias v="nvim"
alias vi="nvim"
alias felixio="ssh felixio@felixm.de"
alias failx="ssh felixio@felixm.de"
alias feh="feh -F -."