mirror of
https://github.com/Ascyii/nvim.git
synced 2026-01-01 12:14:24 -05:00
Init from other configuration repository. This is just a backup and will be refactored soon
This commit is contained in:
247
lua/helpers/after.lua
Normal file
247
lua/helpers/after.lua
Normal file
@@ -0,0 +1,247 @@
|
||||
-- Disable lsp stuff
|
||||
-- xzl
|
||||
-- setup mason
|
||||
-- xzl
|
||||
--require("mason").setup()
|
||||
--require("mason-lspconfig").setup({
|
||||
-- ensure_installed = { "rust_analyzer", "pyright" }, -- Example LSPs
|
||||
--})
|
||||
|
||||
-- Example: Configure a server
|
||||
-- TODO: this has to be done better and automatically
|
||||
--local lspconfig = require("lspconfig")
|
||||
--lspconfig.rust_analyzer.setup({})
|
||||
--lspconfig.pyright.setup({})
|
||||
|
||||
|
||||
if vim.g.diffm then
|
||||
-- setup tree
|
||||
require("nvim-tree").setup({
|
||||
sort_by = "case_sensitive",
|
||||
view = {
|
||||
width = 35,
|
||||
},
|
||||
disable_netrw = false,
|
||||
})
|
||||
else
|
||||
-- stuff for only main
|
||||
require 'telescope'.setup {
|
||||
extensions = {
|
||||
},
|
||||
}
|
||||
|
||||
-- load custom todo plugin
|
||||
require("custom.todo").setup()
|
||||
|
||||
|
||||
-- configure a workflow that integrates image support in documents and a quick opening of them
|
||||
local base_zet = "~/synced/brainstore/zettelkasten"
|
||||
require('telekasten').setup({
|
||||
home = vim.fn.expand(base_zet), -- Put the name of your notes directory here
|
||||
dailies = vim.fn.expand(base_zet .. "/daily"),
|
||||
-- how to change the defualt media folder?
|
||||
image_subdir = vim.fn.expand(base_zet .. "/media"),
|
||||
weeklies = vim.fn.expand(base_zet .. "/weekly"),
|
||||
templates = vim.fn.expand(base_zet .. "/templates"),
|
||||
template_new_note = vim.fn.expand(base_zet .. "/templates/note.md"),
|
||||
template_new_daily = vim.fn.expand(base_zet .. "/templates/daily.md"),
|
||||
template_new_weekly = vim.fn.expand(base_zet .. "/templates/weekly.md"),
|
||||
|
||||
-- auto_set_filetype = false,
|
||||
-- Important part:
|
||||
filename_format = "%Y%m%d%H%M-%title%", -- This adds the timestamp + slugged title
|
||||
new_note_filename = "uuid-title", -- Set naming convention to use uuid first
|
||||
uuid_type = "%Y%m%d%H%M", -- Timestamp as UUID
|
||||
uuid_separator = "-",
|
||||
})
|
||||
|
||||
-- setup tree
|
||||
require("nvim-tree").setup({
|
||||
sort_by = "case_sensitive",
|
||||
-- open_on_tab = true,
|
||||
|
||||
view = {
|
||||
width = 35,
|
||||
side = "right",
|
||||
preserve_window_proportions = true,
|
||||
number = false,
|
||||
relativenumber = false,
|
||||
},
|
||||
update_focused_file = {
|
||||
enable = false,
|
||||
},
|
||||
|
||||
renderer = {
|
||||
group_empty = true,
|
||||
highlight_git = true,
|
||||
highlight_opened_files = "name",
|
||||
indent_markers = {
|
||||
enable = true,
|
||||
},
|
||||
icons = {
|
||||
show = {
|
||||
file = false,
|
||||
folder = false,
|
||||
folder_arrow = true,
|
||||
git = false,
|
||||
modified = false,
|
||||
hidden = false,
|
||||
diagnostics = false,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
filters = {
|
||||
dotfiles = false, -- << SHOW dotfiles by default
|
||||
git_clean = false,
|
||||
no_buffer = false,
|
||||
custom = { ".DS_Store", ".git" }, -- Mac stuff you probably don't need
|
||||
},
|
||||
|
||||
git = {
|
||||
enable = true,
|
||||
ignore = false, -- so it shows even ignored files
|
||||
},
|
||||
|
||||
actions = {
|
||||
open_file = {
|
||||
quit_on_open = false, -- keep tree open after opening a file
|
||||
resize_window = true,
|
||||
},
|
||||
},
|
||||
|
||||
})
|
||||
end
|
||||
|
||||
-- stuff for both
|
||||
|
||||
-- lualine mode for the current workingdir
|
||||
local function get_cwd()
|
||||
local cwd = vim.fn.getcwd()
|
||||
local home = os.getenv("HOME")
|
||||
|
||||
-- Check if the current directory starts with the home directory path
|
||||
if cwd:sub(1, #home) == home then
|
||||
return "~" .. cwd:sub(#home + 1)
|
||||
else
|
||||
return cwd
|
||||
end
|
||||
end
|
||||
|
||||
require('lualine').setup {
|
||||
options = {
|
||||
icons_enabled = false,
|
||||
theme = 'gruvbox',
|
||||
-- think those icons are okay
|
||||
component_separators = { left = '', right = '' },
|
||||
section_separators = { left = '', right = '' },
|
||||
always_divide_middle = true,
|
||||
always_show_tabline = true,
|
||||
globalstatus = false,
|
||||
refresh = {
|
||||
statusline = 300,
|
||||
tabline = 300,
|
||||
winbar = 300,
|
||||
}
|
||||
},
|
||||
sections = {
|
||||
lualine_a = { 'mode' },
|
||||
lualine_b = { 'branch', 'diff', 'diagnostics' },
|
||||
lualine_c = { get_cwd, 'filename' },
|
||||
lualine_x = { 'encoding', 'fileformat', 'filetype' },
|
||||
lualine_y = { 'progress' },
|
||||
lualine_z = { 'location' }
|
||||
},
|
||||
inactive_sections = {
|
||||
lualine_a = {},
|
||||
lualine_b = {},
|
||||
lualine_c = { 'filename' },
|
||||
lualine_x = { 'location' },
|
||||
lualine_y = {},
|
||||
lualine_z = {}
|
||||
},
|
||||
extensions = { "nvim-tree" }
|
||||
}
|
||||
|
||||
|
||||
|
||||
require('gitsigns').setup {
|
||||
on_attach = function(bufnr)
|
||||
local gitsigns = require('gitsigns')
|
||||
|
||||
local function map(mode, l, r, opts)
|
||||
opts = opts or {}
|
||||
opts.buffer = bufnr
|
||||
vim.keymap.set(mode, l, r, opts)
|
||||
end
|
||||
|
||||
|
||||
map('n', ']c', function()
|
||||
if vim.wo.diff then
|
||||
vim.cmd.normal({ ']c', bang = true })
|
||||
else
|
||||
gitsigns.nav_hunk('next')
|
||||
end
|
||||
vim.cmd("normal! zz") -- center cursor
|
||||
end, { silent = true })
|
||||
|
||||
map('n', '[c', function()
|
||||
if vim.wo.diff then
|
||||
vim.cmd.normal({ '[c', bang = true })
|
||||
else
|
||||
gitsigns.nav_hunk('prev')
|
||||
end
|
||||
vim.cmd("normal! zz") -- center cursor
|
||||
end, { silent = true })
|
||||
|
||||
-- Actions
|
||||
map('n', '<leader>hs', gitsigns.stage_hunk)
|
||||
map('n', '<leader>hr', gitsigns.reset_hunk)
|
||||
|
||||
map('v', '<leader>hs', function()
|
||||
gitsigns.stage_hunk({ vim.fn.line('.'), vim.fn.line('v') })
|
||||
end)
|
||||
|
||||
map('v', '<leader>hr', function()
|
||||
gitsigns.reset_hunk({ vim.fn.line('.'), vim.fn.line('v') })
|
||||
end)
|
||||
|
||||
map('n', '<leader>hS', gitsigns.stage_buffer)
|
||||
map('n', '<leader>hR', gitsigns.reset_buffer)
|
||||
map('n', '<leader>hp', gitsigns.preview_hunk)
|
||||
map('n', '<leader>hi', gitsigns.preview_hunk_inline)
|
||||
|
||||
|
||||
map('n', '<leader>hb', function()
|
||||
gitsigns.blame_line({ full = true })
|
||||
end)
|
||||
|
||||
map('n', '<leader>hd', gitsigns.diffthis)
|
||||
|
||||
map('n', '<leader>hD', function()
|
||||
gitsigns.diffthis('~')
|
||||
end)
|
||||
|
||||
map('n', '<leader>hQ', function() gitsigns.setqflist('all') end)
|
||||
map('n', '<leader>hq', gitsigns.setqflist)
|
||||
|
||||
-- Toggles
|
||||
map('n', '<leader>tb', gitsigns.toggle_current_line_blame)
|
||||
map('n', '<leader>tw', gitsigns.toggle_word_diff)
|
||||
|
||||
-- Text object
|
||||
map({ 'o', 'x' }, 'ih', gitsigns.select_hunk)
|
||||
end
|
||||
}
|
||||
|
||||
require("lspsaga").setup({
|
||||
lightbulb = {
|
||||
enable = false,
|
||||
enable_in_insert = false, -- don’t show in insert mode
|
||||
sign = false,
|
||||
|
||||
virtual_text = false,
|
||||
},
|
||||
ui = {
|
||||
}
|
||||
})
|
||||
214
lua/helpers/functions.lua
Normal file
214
lua/helpers/functions.lua
Normal file
@@ -0,0 +1,214 @@
|
||||
-- General helper functions
|
||||
|
||||
function sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
|
||||
|
||||
-- branching depeding on diff mode
|
||||
if vim.g.diffm then
|
||||
-- diffmode
|
||||
else
|
||||
-- Function to open NvimTree based on current file or Git root
|
||||
local api = require("nvim-tree.api")
|
||||
function open_tree_based_on_file()
|
||||
local file_path = vim.fn.expand("%:p")
|
||||
local git_root = vim.fn.system("git rev-parse --show-toplevel"):gsub("\n", "")
|
||||
local dir = vim.fn.filereadable(file_path) and vim.fn.fnamemodify(file_path, ":p:h") or ""
|
||||
|
||||
-- If inside a Git repo, use the Git root as the base directory
|
||||
local open_dir = git_root ~= "" and git_root or dir
|
||||
|
||||
-- Open NvimTree in that directory
|
||||
api.tree.open({ path = open_dir })
|
||||
end
|
||||
|
||||
function open_vorlesung()
|
||||
local mapss = require("config.keymaps")
|
||||
-- get globals
|
||||
set_obs()
|
||||
local uni_dir = vim.fn.expand("~/projects/university/" .. _G.season)
|
||||
|
||||
require('telescope.builtin').find_files {
|
||||
prompt_title = "Select Vorlesung in " .. _G.season,
|
||||
cwd = uni_dir,
|
||||
find_command = {
|
||||
"eza", "-1", "-D"
|
||||
},
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
-- TODO: fix not putting the pdf in another dir then the current workdir
|
||||
-- when in a uni folder
|
||||
-- Open and watch typst
|
||||
local watch_job_id = nil
|
||||
local watch_buf_id = nil
|
||||
function watch_and_open()
|
||||
local input = vim.fn.expand("%:p")
|
||||
if not input:match("%.typ$") then
|
||||
vim.notify("Not a Typst file", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
|
||||
local dir = vim.fn.fnamemodify(input, ":h") -- directory of the Typst file
|
||||
local filename = vim.fn.expand("%:t:r") .. ".pdf" -- filename without extension + .pdf
|
||||
|
||||
-- Get the underlying unicourse dir
|
||||
local one_up = vim.fn.fnamemodify(dir, ":h")
|
||||
|
||||
print(one_up)
|
||||
local pdf_dir = nil
|
||||
if vim.fn.filereadable(one_up .. "/.unicourse") == 1 then
|
||||
pdf_dir = one_up .. "/../../pdfs"
|
||||
else
|
||||
pdf_dir = dir
|
||||
end
|
||||
|
||||
vim.fn.mkdir(pdf_dir, "p")
|
||||
local output = pdf_dir .. "/" .. filename
|
||||
|
||||
-- Check if a watcher is already running for this file
|
||||
if watch_job_id then
|
||||
vim.notify("Typst watcher already running - please close zathura", vim.log.levels.INFO)
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- Start typst watch
|
||||
local cwd = vim.fn.getcwd() -- set the starting directory
|
||||
-- TODO: root setting does not work
|
||||
local watch_cmd = { "typst", "watch", "--root", cwd, input, output }
|
||||
watch_job_id = vim.fn.jobstart(watch_cmd, {
|
||||
stdout_buffered = false,
|
||||
stderr_buffered = false, -- Ensure stderr is unbuffered for real-time error output
|
||||
on_stderr = function(_, data)
|
||||
if data then
|
||||
if not watch_tab_id then
|
||||
pre_tab = vim.api.nvim_get_current_tabpage() -- Get the current tab ID
|
||||
vim.cmd('tabnew') -- Open a new tab
|
||||
watch_tab_id = vim.api.nvim_get_current_tabpage() -- Get the current tab ID
|
||||
watch_buf_id = vim.api.nvim_get_current_buf() -- Get the buffer ID of the new tab
|
||||
vim.api.nvim_buf_set_option(watch_buf_id, "swapfile", false)
|
||||
vim.api.nvim_buf_set_name(watch_buf_id, "/tmp/TypstLog")
|
||||
vim.api.nvim_buf_set_lines(watch_buf_id, 0, 0, false, { "Watching: " .. input }) -- Insert at the top
|
||||
vim.cmd('write!')
|
||||
vim.api.nvim_set_current_tabpage(pre_tab)
|
||||
end
|
||||
-- Write stdout data to the same buffer
|
||||
for _, line in ipairs(data) do
|
||||
if line ~= "" then
|
||||
vim.api.nvim_buf_set_lines(watch_buf_id, -1, -1, false, { "[LOG] " .. line })
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
on_exit = function(_, exit_code)
|
||||
-- Ensure to close the tab that holds the logs
|
||||
--if watch_tab_id then
|
||||
-- -- Switch to the tab holding the log buffer and close it
|
||||
-- vim.api.nvim_set_current_tabpage(watch_tab_id)
|
||||
-- vim.cmd('tabclose') -- Close the tab holding the log buffer
|
||||
--end
|
||||
if exit_code == 0 then
|
||||
vim.notify("Typst watch stopped successfully", vim.log.levels.INFO)
|
||||
else
|
||||
vim.notify("Typst watch stopped with errors", vim.log.levels.ERROR)
|
||||
end
|
||||
watch_job_id = nil
|
||||
end,
|
||||
})
|
||||
vim.notify("Started Typst watch", vim.log.levels.INFO)
|
||||
|
||||
-- Start sioyek with the --new-window flag and stop watch when it exits
|
||||
-- ensure that there is no sioyek
|
||||
vim.fn.system("killall .zathura-wrapped")
|
||||
sleep(0.5)
|
||||
vim.fn.jobstart({ "zathura", output }, {
|
||||
on_exit = function()
|
||||
if watch_job_id then
|
||||
vim.fn.jobstop(watch_job_id)
|
||||
end
|
||||
watch_job_id = nil
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
function find_eff()
|
||||
require('telescope.builtin').find_files({
|
||||
hidden = true, -- show hidden files (dotfiles)
|
||||
no_ignore = true, -- respect .gitignore (ignore files listed in .gitignore)
|
||||
follow = true, -- follow symlinks
|
||||
disable_devicons = true,
|
||||
|
||||
-- Additional filtering to exclude .git, .cache, .local, and large files
|
||||
prompt_title = "Find Files - custom",
|
||||
find_command = {
|
||||
"rg", "--files",
|
||||
"--glob", "!**/.git/*",
|
||||
"--glob", "!**/.cache/*",
|
||||
"--glob", "!**/.local/*",
|
||||
"--glob", "!**/bigfiles/*", -- exclude large files folder
|
||||
"--glob", "!**/*.{jpg,png,gif,mp4,mkv,tar,zip,iso}" -- exclude some large file types
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- TODO: implement this
|
||||
--function search_brain_links()
|
||||
-- local fzf = require('fzf')
|
||||
-- local cmd = "grep -oP '\\[\\[.*:' " .. vim.fn.expand('%') -- grep pattern to search for [[.*:
|
||||
-- fzf.fzf(cmd, {
|
||||
-- preview = "bat --style=numbers --color=always --line-range :500", -- preview with bat (optional)
|
||||
-- sink = function(selected)
|
||||
-- if selected and #selected > 0 then
|
||||
-- local line = vim.fn.search(selected[1], 'n') -- jump to the match
|
||||
-- if line > 0 then
|
||||
-- vim.api.nvim_win_set_cursor(0, {line, 0})
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
-- })
|
||||
--end
|
||||
|
||||
|
||||
function set_root()
|
||||
local current_file = vim.fn.expand('%:p:h') -- get directory of current file
|
||||
local cmd = 'git -C ' .. vim.fn.fnameescape(current_file) .. ' status'
|
||||
vim.fn.system(cmd)
|
||||
if vim.v.shell_error == 0 then
|
||||
local git_root = vim.fn.systemlist('git -C ' .. vim.fn.fnameescape(current_file) .. ' rev-parse --show-toplevel')[1]
|
||||
vim.cmd('cd ' .. vim.fn.fnameescape(git_root))
|
||||
else
|
||||
vim.cmd('cd ' .. vim.fn.fnameescape(current_file))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- Function to open the last file
|
||||
function open_last_file()
|
||||
local last_file_path = vim.fn.stdpath('data') .. "/lastfile.txt"
|
||||
local file = io.open(last_file_path, "r")
|
||||
if file then
|
||||
local last_file = file:read("*line")
|
||||
file:close()
|
||||
if last_file and vim.fn.filereadable(last_file) == 1 then
|
||||
vim.cmd("edit " .. last_file)
|
||||
local success, _ = pcall(function()
|
||||
vim.cmd('normal! `.') -- Go to the last edit position
|
||||
vim.cmd('normal! zz') -- Center the cursor on the screen
|
||||
end)
|
||||
else
|
||||
print("Last file does not exist or is not readable")
|
||||
end
|
||||
else
|
||||
print("No last file found")
|
||||
end
|
||||
end
|
||||
|
||||
256
lua/helpers/linker.lua
Normal file
256
lua/helpers/linker.lua
Normal file
@@ -0,0 +1,256 @@
|
||||
-- this is the linker logic for nvim
|
||||
|
||||
local M = {}
|
||||
|
||||
local brainstore_dir = "~/synced/brainstore"
|
||||
local projects_dir = "~/projects"
|
||||
local mail_dir = "~/mail/plain_emails"
|
||||
local contacts_file = "~/synced/vault/contacts/contacts.txt"
|
||||
local cal_dir = "~/synced/brainstore/calendar"
|
||||
|
||||
function fzf_select(options, prompt, callback)
|
||||
local fzf = require("fzf-lua")
|
||||
fzf.fzf_exec(options, {
|
||||
prompt = prompt .. "> ",
|
||||
actions = {
|
||||
["default"] = function(selected)
|
||||
if selected and selected[1] then
|
||||
callback(selected[1])
|
||||
end
|
||||
end,
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
function M.insert_brainstore_link()
|
||||
require('telescope.builtin').find_files({
|
||||
hidden = true,
|
||||
no_ignore = true,
|
||||
follow = true,
|
||||
prompt_title = "Things in Brain",
|
||||
cwd = vim.fn.expand(brainstore_dir),
|
||||
find_command = {
|
||||
"rg", "--files",
|
||||
"--hidden",
|
||||
"--glob", "!**/.git/*",
|
||||
"--glob", "!**/*.{jpg,png,gif,mp4,mkv,tar,zip,iso}",
|
||||
},
|
||||
attach_mappings = function(prompt_bufnr, map)
|
||||
local actions = require('telescope.actions')
|
||||
local action_state = require('telescope.actions.state')
|
||||
|
||||
local function insert_link()
|
||||
local selection = action_state.get_selected_entry()
|
||||
if not selection then
|
||||
return
|
||||
end
|
||||
local selected = selection.path or selection.filename or selection[1]
|
||||
if selected then
|
||||
actions.close(prompt_bufnr) -- CLOSE with prompt_bufnr
|
||||
local link = "[[brain:" .. selected:gsub(vim.fn.expand(brainstore_dir) .. "/", "") .. "]]"
|
||||
vim.cmd("normal! h")
|
||||
vim.api.nvim_put({link}, "c", true, true)
|
||||
end
|
||||
end
|
||||
|
||||
map('i', '<CR>', insert_link)
|
||||
map('n', '<CR>', insert_link)
|
||||
|
||||
return true
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
-- fetches new send mail and creates a link to a selected mail
|
||||
-- the link can the like any other link followed
|
||||
function M.insert_mail_link()
|
||||
-- TODO: real parsing of the mails when there are multiple in one file
|
||||
vim.fn.system("python " .. vim.fn.expand("~/projects/scripts/extract_mail.py"))
|
||||
vim.fn.system("find " .. mail_dir .. " -type f > /tmp/mail_files")
|
||||
local mails = vim.fn.readfile("/tmp/mail_files")
|
||||
|
||||
fzf_select(mails, "Mails", function(selected)
|
||||
local link = "[[mail:" .. selected:gsub(vim.fn.expand(mail_dir) .. "/", "") .. "]]"
|
||||
vim.api.nvim_put({link}, "c", true, true)
|
||||
end)
|
||||
end
|
||||
|
||||
function M.insert_contact_link()
|
||||
local contacts = vim.fn.readfile(vim.fn.expand(contacts_file))
|
||||
|
||||
fzf_select(contacts, "Contacts", function(selected)
|
||||
local name = selected:match("^(.-)%s") or selected -- get first word as contact name
|
||||
local link = "[[contact:" .. name .. "]]"
|
||||
vim.api.nvim_put({link}, "c", true, true)
|
||||
end)
|
||||
end
|
||||
|
||||
function M.insert_date_link()
|
||||
local year = os.date("%y") -- get current year (e.g., "25" for 2025)
|
||||
local text = string.format("[[date:.%s]]", year)
|
||||
|
||||
vim.api.nvim_put({ text }, "c", true, true)
|
||||
|
||||
-- Move cursor back inside the brackets before the year
|
||||
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
-- Move left by 2 + length of year (e.g., 2 + 2 = 4 for "25")
|
||||
vim.api.nvim_win_set_cursor(0, { row, col - 4 })
|
||||
vim.cmd("startinsert")
|
||||
end
|
||||
|
||||
function M.insert_project_link()
|
||||
require('telescope.builtin').find_files({
|
||||
hidden = true,
|
||||
no_ignore = true,
|
||||
disable_devicons = true,
|
||||
follow = true,
|
||||
prompt_title = "List of projects",
|
||||
cwd = vim.fn.expand(projects_dir),
|
||||
find_command = {
|
||||
"eza", "-1", "-D",
|
||||
},
|
||||
attach_mappings = function(prompt_bufnr, map)
|
||||
local actions = require('telescope.actions')
|
||||
local action_state = require('telescope.actions.state')
|
||||
|
||||
local function insert_link()
|
||||
local selection = action_state.get_selected_entry()
|
||||
if not selection then
|
||||
return
|
||||
end
|
||||
local selected = selection.path or selection.filename or selection[1]
|
||||
if selected then
|
||||
actions.close(prompt_bufnr)
|
||||
local project = selected:gsub(vim.fn.expand(projects_dir) .. "/", "")
|
||||
|
||||
|
||||
require('telescope.builtin').find_files({
|
||||
hidden = true,
|
||||
no_ignore = true,
|
||||
follow = true,
|
||||
disable_devicons = true,
|
||||
prompt_title = "Pick a file. Press <ESC> to link just " .. project .. ".",
|
||||
cwd = vim.fn.expand(selected),
|
||||
find_command = {
|
||||
"rg", "--files",
|
||||
"--hidden",
|
||||
"--glob", "!**/.git/*",
|
||||
},
|
||||
attach_mappings = function(prompt_bufnr, map)
|
||||
local actions = require('telescope.actions')
|
||||
local action_state = require('telescope.actions.state')
|
||||
|
||||
local function insert_link()
|
||||
local selection = action_state.get_selected_entry()
|
||||
if not selection then
|
||||
return
|
||||
end
|
||||
local selected = selection.path or selection.filename or selection[1]
|
||||
if selected then
|
||||
actions.close(prompt_bufnr)
|
||||
local link = "[[project:" .. selected:gsub(vim.fn.expand(projects_dir) .. "/", "") .. "]]"
|
||||
vim.api.nvim_put({link}, "c", true, true)
|
||||
end
|
||||
end
|
||||
|
||||
local function insert_link_top()
|
||||
actions.close(prompt_bufnr)
|
||||
local link = "[[project:" .. project .. "]]"
|
||||
vim.api.nvim_put({link}, "c", true, true)
|
||||
end
|
||||
|
||||
map('i', '<CR>', insert_link)
|
||||
map('n', '<CR>', insert_link)
|
||||
|
||||
map('i', '<ESC>', insert_link_top)
|
||||
map('n', '<ESC>', insert_link_top)
|
||||
|
||||
|
||||
return true
|
||||
end
|
||||
})
|
||||
|
||||
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
map('i', '<CR>', insert_link)
|
||||
map('n', '<CR>', insert_link)
|
||||
|
||||
return true
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
local function spliting(inputstr, sep)
|
||||
if sep == nil then
|
||||
sep = "%s"
|
||||
end
|
||||
local t = {}
|
||||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
|
||||
table.insert(t, str)
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function pad2(n)
|
||||
n = tonumber(n)
|
||||
if n < 10 then
|
||||
return "0" .. n
|
||||
else
|
||||
return tostring(n)
|
||||
end
|
||||
end
|
||||
|
||||
-- The heart of this project following
|
||||
-- Remember to go back with C-O
|
||||
function M.follow_link()
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local link = line:match("%[%[(.-)%]%]")
|
||||
if not link then
|
||||
print("No link found on this line.")
|
||||
return
|
||||
end
|
||||
|
||||
local kind, target = link:match("^(.-):(.*)$")
|
||||
if not kind or not target then
|
||||
print("Invalid link format.")
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
-- List of all kinds that are available
|
||||
-- Here brainstore and projects are kind of the same but I keep them separated
|
||||
if kind == "brain" then
|
||||
vim.cmd("edit " .. brainstore_dir .. "/" .. target)
|
||||
elseif kind == "mail" then
|
||||
vim.cmd("edit " .. mail_dir .. "/" .. target)
|
||||
elseif kind == "contact" then
|
||||
vim.cmd("vsplit " .. contacts_file)
|
||||
vim.cmd("/" .. target)
|
||||
elseif kind == "project" then
|
||||
vim.cmd("edit " .. projects_dir .. "/" .. target)
|
||||
elseif kind == "date" then
|
||||
-- target: "4.3.25" or "03.04.2034"
|
||||
local splits = spliting(target, '.')
|
||||
local day = pad2(splits[1])
|
||||
local month = pad2(splits[2])
|
||||
local year = splits[3]
|
||||
|
||||
-- Normalize year: if 4 digits, cut to last two
|
||||
if #year == 4 then
|
||||
year = year:sub(3, 4)
|
||||
end
|
||||
|
||||
vim.cmd("edit " .. cal_dir .. "/calendar_20" .. year .. ".txt")
|
||||
vim.cmd("/" .. "20" .. year .. "-" .. month .. "-" .. day)
|
||||
vim.cmd("normal! zz")
|
||||
else
|
||||
print("Unknown link type: " .. kind .. ". Must be one of: " .. "mail, contact, project, brain, date.")
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user