Formatted with stylua and checked with luacheck

This commit is contained in:
Rene Schallner
2021-11-24 12:49:03 +01:00
parent 7333b76d37
commit 29af4fdcc0
2 changed files with 365 additions and 304 deletions

26
.luacheckrc Normal file
View File

@@ -0,0 +1,26 @@
-- Rerun tests only if their modification time changed.
cache = true
std = luajit
codes = true
self = false
-- Glorious list of warnings: https://luacheck.readthedocs.io/en/stable/warnings.html
ignore = {
"212", -- Unused argument, In the case of callback function, _arg_name is easier to understand than _, so this option is set to off.
"122", -- Indirectly setting a readonly global
}
globals = {
"_",
"TelescopeGlobalState",
"_TelescopeConfigurationValues",
"_TelescopeConfigurationPickers",
"__TelescopeKeymapStore",
}
-- Global objects defined by the C code
read_globals = {
"vim",
}

View File

@@ -1,11 +1,10 @@
local builtin = require "telescope.builtin" local builtin = require("telescope.builtin")
local actions = require("telescope.actions") local actions = require("telescope.actions")
local action_state = require "telescope.actions.state" local action_state = require("telescope.actions.state")
-- declare locals for the nvim api stuff to avoid more lsp warnings -- declare locals for the nvim api stuff to avoid more lsp warnings
local vim = vim local vim = vim
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
-- DEFAULT CONFIG -- DEFAULT CONFIG
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
@@ -13,21 +12,20 @@ local home = vim.fn.expand("~/zettelkasten")
local ZkCfg = { local ZkCfg = {
home = home, home = home,
dailies = home .. '/' .. 'daily', dailies = home .. "/" .. "daily",
weeklies = home .. '/' .. 'weekly', weeklies = home .. "/" .. "weekly",
templates = home .. '/' .. 'templates', templates = home .. "/" .. "templates",
extension = ".md", extension = ".md",
-- following a link to a non-existing note will create it -- following a link to a non-existing note will create it
follow_creates_nonexisting = true, follow_creates_nonexisting = true,
dailies_create_nonexisting = true, dailies_create_nonexisting = true,
weeklies_create_nonexisting = true, weeklies_create_nonexisting = true,
-- templates for new notes -- templates for new notes
template_new_note = home .. '/' .. 'templates/new_note.md', template_new_note = home .. "/" .. "templates/new_note.md",
template_new_daily = home .. '/' .. 'templates/daily_tk.md', template_new_daily = home .. "/" .. "templates/daily_tk.md",
template_new_weekly = home .. '/' .. 'templates/weekly_tk.md', template_new_weekly = home .. "/" .. "templates/weekly_tk.md",
-- integrate with calendar-vim -- integrate with calendar-vim
plug_into_calendar = true, plug_into_calendar = true,
@@ -37,8 +35,8 @@ local ZkCfg = {
-- use monday as first day of week: 1 .. true, 0 .. false -- use monday as first day of week: 1 .. true, 0 .. false
calendar_monday = 1, calendar_monday = 1,
-- calendar mark: where to put mark for marked days: 'left', 'right', 'left-fit' -- calendar mark: where to put mark for marked days: 'left', 'right', 'left-fit'
calendar_mark = 'left-fit', calendar_mark = "left-fit",
} },
} }
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
@@ -50,26 +48,57 @@ local note_type_templates = {
} }
local function file_exists(fname) local function file_exists(fname)
local f=io.open(fname,"r") local f = io.open(fname, "r")
-- print("checking for " .. fname) -- print("checking for " .. fname)
if f~=nil then io.close(f) return true else return false end if f ~= nil then
io.close(f)
return true
else
return false
end
end end
local function daysuffix(day) local function daysuffix(day)
if((day == '1') or (day == '21') or (day == '31')) then return 'st' end if (day == "1") or (day == "21") or (day == "31") then
if((day == '2') or (day == '22')) then return 'nd' end return "st"
if((day == '3') or (day == '33')) then return 'rd' end end
return 'th' if (day == "2") or (day == "22") then
return "nd"
end
if (day == "3") or (day == "33") then
return "rd"
end
return "th"
end end
local daymap = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" } local daymap = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }
local monthmap = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } local monthmap = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
local calenderinfo_today = function() local calenderinfo_today = function()
local dinfo = os.date("*t") local dinfo = os.date("*t")
local opts = {} local opts = {}
opts.date = os.date("%Y-%m-%d") opts.date = os.date("%Y-%m-%d")
opts.hdate = daymap[dinfo.wday] .. ', ' .. monthmap[dinfo.month] .. ' ' .. dinfo.day .. daysuffix(dinfo.day) .. ', ' .. dinfo.year opts.hdate = daymap[dinfo.wday]
.. ", "
.. monthmap[dinfo.month]
.. " "
.. dinfo.day
.. daysuffix(dinfo.day)
.. ", "
.. dinfo.year
opts.week = os.date("%V") opts.week = os.date("%V")
opts.month = dinfo.month opts.month = dinfo.month
opts.year = dinfo.year opts.year = dinfo.year
@@ -87,23 +116,23 @@ local function linesubst(line, title, calendar_info)
title = title, title = title,
} }
for k, v in pairs(substs) do for k, v in pairs(substs) do
line = line:gsub("{{"..k.."}}", v) line = line:gsub("{{" .. k .. "}}", v)
end end
return line return line
end end
local create_note_from_template = function (title, filepath, templatefn, calendar_info) local create_note_from_template = function(title, filepath, templatefn, calendar_info)
-- first, read the template file -- first, read the template file
local lines = {} local lines = {}
for line in io.lines(templatefn) do for line in io.lines(templatefn) do
lines[#lines+1] = line lines[#lines + 1] = line
end end
-- now write the output file, substituting vars line by line -- now write the output file, substituting vars line by line
local ofile = io.open(filepath, 'a') local ofile = io.open(filepath, "a")
for _, line in pairs(lines) do for _, line in pairs(lines) do
ofile:write(linesubst(line, title, calendar_info) .. '\n') ofile:write(linesubst(line, title, calendar_info) .. "\n")
end end
ofile:close() ofile:close()
@@ -117,7 +146,6 @@ local path_to_linkname = function(p)
return fn return fn
end end
-- --
-- FindDailyNotes: -- FindDailyNotes:
-- --------------- -- ---------------
@@ -128,9 +156,11 @@ local FindDailyNotes = function(opts)
opts = opts or {} opts = opts or {}
local today = os.date("%Y-%m-%d") local today = os.date("%Y-%m-%d")
local fname = ZkCfg.dailies .. '/' .. today .. ZkCfg.extension local fname = ZkCfg.dailies .. "/" .. today .. ZkCfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if ((fexists ~= true) and ((opts.dailies_create_nonexisting == true) or ZkCfg.dailies_create_nonexisting == true)) then if
(fexists ~= true) and ((opts.dailies_create_nonexisting == true) or ZkCfg.dailies_create_nonexisting == true)
then
create_note_from_template(today, fname, note_type_templates.daily) create_note_from_template(today, fname, note_type_templates.daily)
end end
@@ -141,7 +171,6 @@ local FindDailyNotes = function(opts)
}) })
end end
-- --
-- FindWeeklyNotes: -- FindWeeklyNotes:
-- --------------- -- ---------------
@@ -152,9 +181,11 @@ local FindWeeklyNotes = function(opts)
opts = opts or {} opts = opts or {}
local title = os.date("%Y-W%V") local title = os.date("%Y-W%V")
local fname = ZkCfg.weeklies .. '/' .. title .. ZkCfg.extension local fname = ZkCfg.weeklies .. "/" .. title .. ZkCfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if ((fexists ~= true) and ((opts.weeklies_create_nonexisting == true) or ZkCfg.weeklies_create_nonexisting == true)) then if
(fexists ~= true) and ((opts.weeklies_create_nonexisting == true) or ZkCfg.weeklies_create_nonexisting == true)
then
create_note_from_template(title, fname, note_type_templates.weekly) create_note_from_template(title, fname, note_type_templates.weekly)
end end
@@ -165,7 +196,6 @@ local FindWeeklyNotes = function(opts)
}) })
end end
-- --
-- InsertLink: -- InsertLink:
-- ----------- -- -----------
@@ -181,7 +211,7 @@ local InsertLink = function(_)
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
local fn = path_to_linkname(selection.value) local fn = path_to_linkname(selection.value)
vim.api.nvim_put({ "[["..fn.."]]" }, "", false, true) vim.api.nvim_put({ "[[" .. fn .. "]]" }, "", false, true)
end) end)
return true return true
end, end,
@@ -189,7 +219,6 @@ local InsertLink = function(_)
}) })
end end
-- --
-- FollowLink: -- FollowLink:
-- ----------- -- -----------
@@ -198,16 +227,18 @@ end
-- --
local FollowLink = function(opts) local FollowLink = function(opts)
opts = opts or {} opts = opts or {}
vim.cmd('normal yi]') vim.cmd("normal yi]")
local title = vim.fn.getreg('"0') local title = vim.fn.getreg('"0')
-- check if fname exists anywhere -- check if fname exists anywhere
local fexists = file_exists(ZkCfg.weeklies .. '/' .. title .. ZkCfg.extension) local fexists = file_exists(ZkCfg.weeklies .. "/" .. title .. ZkCfg.extension)
fexists = fexists or file_exists(ZkCfg.dailies .. '/' .. title .. ZkCfg.extension) fexists = fexists or file_exists(ZkCfg.dailies .. "/" .. title .. ZkCfg.extension)
fexists = fexists or file_exists(ZkCfg.home .. '/' .. title .. ZkCfg.extension) fexists = fexists or file_exists(ZkCfg.home .. "/" .. title .. ZkCfg.extension)
if ((fexists ~= true) and ((opts.follow_creates_nonexisting == true) or ZkCfg.follow_creates_nonexisting == true)) then if
local fname = ZkCfg.home .. '/' .. title .. ZkCfg.extension (fexists ~= true) and ((opts.follow_creates_nonexisting == true) or ZkCfg.follow_creates_nonexisting == true)
then
local fname = ZkCfg.home .. "/" .. title .. ZkCfg.extension
create_note_from_template(title, fname, note_type_templates.normal) create_note_from_template(title, fname, note_type_templates.normal)
end end
@@ -219,7 +250,6 @@ local FollowLink = function(opts)
}) })
end end
-- --
-- YankLink: -- YankLink:
-- ----------- -- -----------
@@ -227,12 +257,11 @@ end
-- Create and yank a [[link]] from the current note. -- Create and yank a [[link]] from the current note.
-- --
local YankLink = function() local YankLink = function()
local title = '[[' .. path_to_linkname(vim.fn.expand('%')) .. ']]' local title = "[[" .. path_to_linkname(vim.fn.expand("%")) .. "]]"
vim.fn.setreg('"', title) vim.fn.setreg('"', title)
print('yanked ' .. title) print("yanked " .. title)
end end
-- --
-- GotoToday: -- GotoToday:
-- ---------- -- ----------
@@ -243,9 +272,11 @@ local GotoToday = function(opts)
opts = opts or calenderinfo_today() opts = opts or calenderinfo_today()
local word = opts.date or os.date("%Y-%m-%d") local word = opts.date or os.date("%Y-%m-%d")
local fname = ZkCfg.dailies .. '/' .. word .. ZkCfg.extension local fname = ZkCfg.dailies .. "/" .. word .. ZkCfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if ((fexists ~= true) and ((opts.follow_creates_nonexisting == true) or ZkCfg.follow_creates_nonexisting == true)) then if
(fexists ~= true) and ((opts.follow_creates_nonexisting == true) or ZkCfg.follow_creates_nonexisting == true)
then
create_note_from_template(word, fname, note_type_templates.daily, opts) create_note_from_template(word, fname, note_type_templates.daily, opts)
end end
@@ -259,17 +290,16 @@ local GotoToday = function(opts)
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
-- open the new note -- open the new note
if (opts.calendar == true) then if opts.calendar == true then
vim.cmd('wincmd w') vim.cmd("wincmd w")
end end
vim.cmd('e ' .. fname) vim.cmd("e " .. fname)
end) end)
return true return true
end, end,
}) })
end end
-- --
-- FindNotes: -- FindNotes:
-- ---------- -- ----------
@@ -284,7 +314,6 @@ local FindNotes = function(_)
}) })
end end
-- --
-- SearchNotes: -- SearchNotes:
-- ------------ -- ------------
@@ -301,7 +330,6 @@ local SearchNotes = function(_)
}) })
end end
-- --
-- CreateNote: -- CreateNote:
-- ------------ -- ------------
@@ -309,11 +337,13 @@ end
-- Prompts for title and creates note with default template -- Prompts for title and creates note with default template
-- --
local function on_create(title) local function on_create(title)
if (title == nil) then return end if title == nil then
return
end
local fname = ZkCfg.home .. '/' .. title .. ZkCfg.extension local fname = ZkCfg.home .. "/" .. title .. ZkCfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if (fexists ~= true) then if fexists ~= true then
create_note_from_template(title, fname, note_type_templates.normal) create_note_from_template(title, fname, note_type_templates.normal)
end end
@@ -326,10 +356,9 @@ local function on_create(title)
end end
local CreateNote = function(_) local CreateNote = function(_)
vim.ui.input({prompt = 'Title: '}, on_create) vim.ui.input({ prompt = "Title: " }, on_create)
end end
-- --
-- CreateNoteSelectTemplate() -- CreateNoteSelectTemplate()
-- -------------------------- -- --------------------------
@@ -338,13 +367,15 @@ end
-- creates the new note by template and opens it -- creates the new note by template and opens it
local function on_create_with_template(title) local function on_create_with_template(title)
if (title == nil) then return end if title == nil then
return
end
local fname = ZkCfg.home .. '/' .. title .. ZkCfg.extension local fname = ZkCfg.home .. "/" .. title .. ZkCfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if (fexists == true) then if fexists == true then
-- open the new note -- open the new note
vim.cmd('e ' .. fname) vim.cmd("e " .. fname)
return return
end end
@@ -355,10 +386,10 @@ local function on_create_with_template(title)
attach_mappings = function(prompt_bufnr, _) attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local template = ZkCfg.templates .. '/' .. action_state.get_selected_entry().value local template = ZkCfg.templates .. "/" .. action_state.get_selected_entry().value
create_note_from_template(title, fname, template) create_note_from_template(title, fname, template)
-- open the new note -- open the new note
vim.cmd('e ' .. fname) vim.cmd("e " .. fname)
end) end)
return true return true
end, end,
@@ -366,10 +397,9 @@ local function on_create_with_template(title)
end end
local CreateNoteSelectTemplate = function(_) local CreateNoteSelectTemplate = function(_)
vim.ui.input({prompt = 'Title: '}, on_create_with_template) vim.ui.input({ prompt = "Title: " }, on_create_with_template)
end end
-- --
-- GotoThisWeek: -- GotoThisWeek:
-- ------------- -- -------------
@@ -380,9 +410,11 @@ local GotoThisWeek = function(opts)
opts = opts or {} opts = opts or {}
local title = os.date("%Y-W%V") local title = os.date("%Y-W%V")
local fname = ZkCfg.weeklies .. '/' .. title .. ZkCfg.extension local fname = ZkCfg.weeklies .. "/" .. title .. ZkCfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
if ((fexists ~= true) and ((opts.weeklies_create_nonexisting == true) or ZkCfg.weeklies_create_nonexisting == true)) then if
(fexists ~= true) and ((opts.weeklies_create_nonexisting == true) or ZkCfg.weeklies_create_nonexisting == true)
then
create_note_from_template(title, fname, note_type_templates.weekly) create_note_from_template(title, fname, note_type_templates.weekly)
end end
@@ -394,28 +426,27 @@ local GotoThisWeek = function(opts)
}) })
end end
-- --
-- Calendar Stuff -- Calendar Stuff
-- -------------- -- --------------
-- return if a daily 'note exists' indicator (sign) should be displayed for a particular day -- return if a daily 'note exists' indicator (sign) should be displayed for a particular day
local CalendarSignDay = function(day, month, year) local CalendarSignDay = function(day, month, year)
local fn = ZkCfg.dailies .. '/' .. string.format('%04d-%02d-%02d', year, month, day) .. ZkCfg.extension local fn = ZkCfg.dailies .. "/" .. string.format("%04d-%02d-%02d", year, month, day) .. ZkCfg.extension
if file_exists(fn) then if file_exists(fn) then
return 1 return 1
end end
return 0 return 0
end end
-- action on enter on a specific day: preview in telescope, stay in calendar on cancel, open note in other window on accept -- action on enter on a specific day:
-- preview in telescope, stay in calendar on cancel, open note in other window on accept
local CalendarAction = function(day, month, year, weekday, _) local CalendarAction = function(day, month, year, weekday, _)
local today = string.format('%04d-%02d-%02d', year, month, day) local today = string.format("%04d-%02d-%02d", year, month, day)
local opts = {} local opts = {}
opts.date = today opts.date = today
opts.hdate = daymap[weekday] .. ', ' .. monthmap[tonumber(month)] .. ' ' .. day .. daysuffix(day) .. ', ' .. year opts.hdate = daymap[weekday] .. ", " .. monthmap[tonumber(month)] .. " " .. day .. daysuffix(day) .. ", " .. year
opts.week = 'n/a' -- TODO: calculate the week somehow opts.week = "n/a" -- TODO: calculate the week somehow
opts.month = month opts.month = month
opts.year = year opts.year = year
opts.day = day opts.day = day
@@ -425,13 +456,13 @@ end
local ShowCalendar = function(opts) local ShowCalendar = function(opts)
local defaults = {} local defaults = {}
defaults.cmd = 'CalendarVR' defaults.cmd = "CalendarVR"
defaults.vertical_resize = 1 defaults.vertical_resize = 1
opts = opts or defaults opts = opts or defaults
vim.cmd(opts.cmd) vim.cmd(opts.cmd)
if (opts.vertical_resize) then if opts.vertical_resize then
vim.cmd('vertical resize +' .. opts.vertical_resize) vim.cmd("vertical resize +" .. opts.vertical_resize)
end end
end end
@@ -451,7 +482,8 @@ local SetupCalendar = function(opts)
" year year " year year
" weekday : day of week (monday=1) " weekday : day of week (monday=1)
" dir : direction of calendar " dir : direction of calendar
return luaeval('require("telekasten").CalendarAction(_A[1], _A[2], _A[3], _A[4], _A[5])', [a:day, a:month, a:year, a:weekday, a:dir]) return luaeval('require("telekasten").CalendarAction(_A[1], _A[2], _A[3], _A[4], _A[5])',
[a:day, a:month, a:year, a:weekday, a:dir])
endfunction endfunction
function! MyCalBegin() function! MyCalBegin()
@@ -469,12 +501,11 @@ local SetupCalendar = function(opts)
]] ]]
for k, v in pairs(opts) do for k, v in pairs(opts) do
cmd = cmd:gsub('{{' .. k .. '}}', v) cmd = cmd:gsub("{{" .. k .. "}}", v)
end end
vim.cmd(cmd) vim.cmd(cmd)
end end
-- Setup(cfg) -- Setup(cfg)
-- --
-- Overrides config with elements from cfg. See top of file for defaults. -- Overrides config with elements from cfg. See top of file for defaults.
@@ -484,23 +515,27 @@ local Setup = function(cfg)
for k, v in pairs(cfg) do for k, v in pairs(cfg) do
-- merge everything but calendar opts -- merge everything but calendar opts
-- they will be merged later -- they will be merged later
if (k ~= 'calendar_opts') then if k ~= "calendar_opts" then
ZkCfg[k] = v ZkCfg[k] = v
end end
end end
if vim.fn.executable('rg') then if vim.fn.executable("rg") then
ZkCfg.find_command = { 'rg', '--files', '--sortr', 'created', } ZkCfg.find_command = { "rg", "--files", "--sortr", "created" }
else else
ZkCfg.find_command = nil ZkCfg.find_command = nil
end end
-- this looks a little messy -- this looks a little messy
if (ZkCfg.plug_into_calendar) then if ZkCfg.plug_into_calendar then
cfg.calendar_opts = cfg.calendar_opts or {} cfg.calendar_opts = cfg.calendar_opts or {}
ZkCfg.calendar_opts = ZkCfg.calendar_opts or {} ZkCfg.calendar_opts = ZkCfg.calendar_opts or {}
ZkCfg.calendar_opts.weeknm = cfg.calendar_opts.weeknm or ZkCfg.calendar_opts.weeknm or 1 ZkCfg.calendar_opts.weeknm = cfg.calendar_opts.weeknm or ZkCfg.calendar_opts.weeknm or 1
ZkCfg.calendar_opts.calendar_monday = cfg.calendar_opts.calendar_monday or ZkCfg.calendar_opts.calendar_monday or 1 ZkCfg.calendar_opts.calendar_monday = cfg.calendar_opts.calendar_monday
ZkCfg.calendar_opts.calendar_mark = cfg.calendar_opts.calendar_mark or ZkCfg.calendar_opts.calendar_mark or 'left-fit' or ZkCfg.calendar_opts.calendar_monday
or 1
ZkCfg.calendar_opts.calendar_mark = cfg.calendar_opts.calendar_mark
or ZkCfg.calendar_opts.calendar_mark
or "left-fit"
SetupCalendar(ZkCfg.calendar_opts) SetupCalendar(ZkCfg.calendar_opts)
end end
end end