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 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
local vim = vim
-- ----------------------------------------------------------------------------
-- DEFAULT CONFIG
-- ----------------------------------------------------------------------------
@@ -13,21 +12,20 @@ local home = vim.fn.expand("~/zettelkasten")
local ZkCfg = {
home = home,
dailies = home .. '/' .. 'daily',
weeklies = home .. '/' .. 'weekly',
templates = home .. '/' .. 'templates',
dailies = home .. "/" .. "daily",
weeklies = home .. "/" .. "weekly",
templates = home .. "/" .. "templates",
extension = ".md",
-- following a link to a non-existing note will create it
follow_creates_nonexisting = true,
dailies_create_nonexisting = true,
weeklies_create_nonexisting = true,
-- templates for new notes
template_new_note = home .. '/' .. 'templates/new_note.md',
template_new_daily = home .. '/' .. 'templates/daily_tk.md',
template_new_weekly = home .. '/' .. 'templates/weekly_tk.md',
template_new_note = home .. "/" .. "templates/new_note.md",
template_new_daily = home .. "/" .. "templates/daily_tk.md",
template_new_weekly = home .. "/" .. "templates/weekly_tk.md",
-- integrate with calendar-vim
plug_into_calendar = true,
@@ -37,8 +35,8 @@ local ZkCfg = {
-- use monday as first day of week: 1 .. true, 0 .. false
calendar_monday = 1,
-- calendar mark: where to put mark for marked days: 'left', 'right', 'left-fit'
calendar_mark = 'left-fit',
}
calendar_mark = "left-fit",
},
}
-- ----------------------------------------------------------------------------
@@ -52,24 +50,55 @@ local note_type_templates = {
local function file_exists(fname)
local f = io.open(fname, "r")
-- 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
local function daysuffix(day)
if((day == '1') or (day == '21') or (day == '31')) then return 'st' end
if((day == '2') or (day == '22')) then return 'nd' end
if((day == '3') or (day == '33')) then return 'rd' end
return 'th'
if (day == "1") or (day == "21") or (day == "31") then
return "st"
end
if (day == "2") or (day == "22") then
return "nd"
end
if (day == "3") or (day == "33") then
return "rd"
end
return "th"
end
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 dinfo = os.date("*t")
local opts = {}
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.month = dinfo.month
opts.year = dinfo.year
@@ -101,9 +130,9 @@ local create_note_from_template = function (title, filepath, templatefn, calenda
end
-- 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
ofile:write(linesubst(line, title, calendar_info) .. '\n')
ofile:write(linesubst(line, title, calendar_info) .. "\n")
end
ofile:close()
@@ -117,7 +146,6 @@ local path_to_linkname = function(p)
return fn
end
--
-- FindDailyNotes:
-- ---------------
@@ -128,9 +156,11 @@ local FindDailyNotes = function(opts)
opts = opts or {}
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)
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)
end
@@ -141,7 +171,6 @@ local FindDailyNotes = function(opts)
})
end
--
-- FindWeeklyNotes:
-- ---------------
@@ -152,9 +181,11 @@ local FindWeeklyNotes = function(opts)
opts = opts or {}
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)
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)
end
@@ -165,7 +196,6 @@ local FindWeeklyNotes = function(opts)
})
end
--
-- InsertLink:
-- -----------
@@ -189,7 +219,6 @@ local InsertLink = function(_)
})
end
--
-- FollowLink:
-- -----------
@@ -198,16 +227,18 @@ end
--
local FollowLink = function(opts)
opts = opts or {}
vim.cmd('normal yi]')
vim.cmd("normal yi]")
local title = vim.fn.getreg('"0')
-- check if fname exists anywhere
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.home .. '/' .. 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.home .. "/" .. title .. ZkCfg.extension)
if ((fexists ~= true) and ((opts.follow_creates_nonexisting == true) or ZkCfg.follow_creates_nonexisting == true)) then
local fname = ZkCfg.home .. '/' .. title .. ZkCfg.extension
if
(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)
end
@@ -219,7 +250,6 @@ local FollowLink = function(opts)
})
end
--
-- YankLink:
-- -----------
@@ -227,12 +257,11 @@ end
-- Create and yank a [[link]] from the current note.
--
local YankLink = function()
local title = '[[' .. path_to_linkname(vim.fn.expand('%')) .. ']]'
local title = "[[" .. path_to_linkname(vim.fn.expand("%")) .. "]]"
vim.fn.setreg('"', title)
print('yanked ' .. title)
print("yanked " .. title)
end
--
-- GotoToday:
-- ----------
@@ -243,9 +272,11 @@ local GotoToday = function(opts)
opts = opts or calenderinfo_today()
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)
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)
end
@@ -259,17 +290,16 @@ local GotoToday = function(opts)
actions.close(prompt_bufnr)
-- open the new note
if (opts.calendar == true) then
vim.cmd('wincmd w')
if opts.calendar == true then
vim.cmd("wincmd w")
end
vim.cmd('e ' .. fname)
vim.cmd("e " .. fname)
end)
return true
end,
})
end
--
-- FindNotes:
-- ----------
@@ -284,7 +314,6 @@ local FindNotes = function(_)
})
end
--
-- SearchNotes:
-- ------------
@@ -301,7 +330,6 @@ local SearchNotes = function(_)
})
end
--
-- CreateNote:
-- ------------
@@ -309,11 +337,13 @@ end
-- Prompts for title and creates note with default template
--
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)
if (fexists ~= true) then
if fexists ~= true then
create_note_from_template(title, fname, note_type_templates.normal)
end
@@ -326,10 +356,9 @@ local function on_create(title)
end
local CreateNote = function(_)
vim.ui.input({prompt = 'Title: '}, on_create)
vim.ui.input({ prompt = "Title: " }, on_create)
end
--
-- CreateNoteSelectTemplate()
-- --------------------------
@@ -338,13 +367,15 @@ end
-- creates the new note by template and opens it
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)
if (fexists == true) then
if fexists == true then
-- open the new note
vim.cmd('e ' .. fname)
vim.cmd("e " .. fname)
return
end
@@ -355,10 +386,10 @@ local function on_create_with_template(title)
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
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)
-- open the new note
vim.cmd('e ' .. fname)
vim.cmd("e " .. fname)
end)
return true
end,
@@ -366,10 +397,9 @@ local function on_create_with_template(title)
end
local CreateNoteSelectTemplate = function(_)
vim.ui.input({prompt = 'Title: '}, on_create_with_template)
vim.ui.input({ prompt = "Title: " }, on_create_with_template)
end
--
-- GotoThisWeek:
-- -------------
@@ -380,9 +410,11 @@ local GotoThisWeek = function(opts)
opts = opts or {}
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)
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)
end
@@ -394,28 +426,27 @@ local GotoThisWeek = function(opts)
})
end
--
-- Calendar Stuff
-- --------------
-- return if a daily 'note exists' indicator (sign) should be displayed for a particular day
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
return 1
end
return 0
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 today = string.format('%04d-%02d-%02d', year, month, day)
local today = string.format("%04d-%02d-%02d", year, month, day)
local opts = {}
opts.date = today
opts.hdate = daymap[weekday] .. ', ' .. monthmap[tonumber(month)] .. ' ' .. day .. daysuffix(day) .. ', ' .. year
opts.week = 'n/a' -- TODO: calculate the week somehow
opts.hdate = daymap[weekday] .. ", " .. monthmap[tonumber(month)] .. " " .. day .. daysuffix(day) .. ", " .. year
opts.week = "n/a" -- TODO: calculate the week somehow
opts.month = month
opts.year = year
opts.day = day
@@ -425,13 +456,13 @@ end
local ShowCalendar = function(opts)
local defaults = {}
defaults.cmd = 'CalendarVR'
defaults.cmd = "CalendarVR"
defaults.vertical_resize = 1
opts = opts or defaults
vim.cmd(opts.cmd)
if (opts.vertical_resize) then
vim.cmd('vertical resize +' .. opts.vertical_resize)
if opts.vertical_resize then
vim.cmd("vertical resize +" .. opts.vertical_resize)
end
end
@@ -451,7 +482,8 @@ local SetupCalendar = function(opts)
" year year
" weekday : day of week (monday=1)
" 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
function! MyCalBegin()
@@ -469,12 +501,11 @@ local SetupCalendar = function(opts)
]]
for k, v in pairs(opts) do
cmd = cmd:gsub('{{' .. k .. '}}', v)
cmd = cmd:gsub("{{" .. k .. "}}", v)
end
vim.cmd(cmd)
end
-- Setup(cfg)
--
-- 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
-- merge everything but calendar opts
-- they will be merged later
if (k ~= 'calendar_opts') then
if k ~= "calendar_opts" then
ZkCfg[k] = v
end
end
if vim.fn.executable('rg') then
ZkCfg.find_command = { 'rg', '--files', '--sortr', 'created', }
if vim.fn.executable("rg") then
ZkCfg.find_command = { "rg", "--files", "--sortr", "created" }
else
ZkCfg.find_command = nil
end
-- this looks a little messy
if (ZkCfg.plug_into_calendar) then
if ZkCfg.plug_into_calendar then
cfg.calendar_opts = cfg.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.calendar_monday = cfg.calendar_opts.calendar_monday 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'
ZkCfg.calendar_opts.calendar_monday = cfg.calendar_opts.calendar_monday
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)
end
end