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,123 +1,151 @@
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
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
local home = vim.fn.expand("~/zettelkasten") 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
follow_creates_nonexisting = true,
dailies_create_nonexisting = true,
weeklies_create_nonexisting = true,
-- following a link to a non-existing note will create it -- templates for new notes
follow_creates_nonexisting = true, template_new_note = home .. "/" .. "templates/new_note.md",
dailies_create_nonexisting = true, template_new_daily = home .. "/" .. "templates/daily_tk.md",
weeklies_create_nonexisting = true, template_new_weekly = home .. "/" .. "templates/weekly_tk.md",
-- templates for new notes -- integrate with calendar-vim
template_new_note = home .. '/' .. 'templates/new_note.md', plug_into_calendar = true,
template_new_daily = home .. '/' .. 'templates/daily_tk.md', calendar_opts = {
template_new_weekly = home .. '/' .. 'templates/weekly_tk.md', -- calendar week display mode: 1 .. 'WK01', 2 .. 'WK 1', 3 .. 'KW01', 4 .. 'KW 1', 5 .. '1'
weeknm = 4,
-- integrate with calendar-vim -- use monday as first day of week: 1 .. true, 0 .. false
plug_into_calendar = true, calendar_monday = 1,
calendar_opts = { -- calendar mark: where to put mark for marked days: 'left', 'right', 'left-fit'
-- calendar week display mode: 1 .. 'WK01', 2 .. 'WK 1', 3 .. 'KW01', 4 .. 'KW 1', 5 .. '1' calendar_mark = "left-fit",
weeknm = 4, },
-- 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',
}
} }
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
local note_type_templates = { local note_type_templates = {
normal = ZkCfg.template_new_note, normal = ZkCfg.template_new_note,
daily = ZkCfg.template_new_daily, daily = ZkCfg.template_new_daily,
weekly = ZkCfg.template_new_weekly, weekly = ZkCfg.template_new_weekly,
} }
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]
opts.week = os.date("%V") .. ", "
opts.month = dinfo.month .. monthmap[dinfo.month]
opts.year = dinfo.year .. " "
opts.day = dinfo.day .. dinfo.day
return opts .. daysuffix(dinfo.day)
.. ", "
.. dinfo.year
opts.week = os.date("%V")
opts.month = dinfo.month
opts.year = dinfo.year
opts.day = dinfo.day
return opts
end end
local function linesubst(line, title, calendar_info) local function linesubst(line, title, calendar_info)
local cinfo = calendar_info or calenderinfo_today() local cinfo = calendar_info or calenderinfo_today()
local substs = { local substs = {
date = cinfo.date, date = cinfo.date,
hdate = cinfo.hdate, hdate = cinfo.hdate,
week = cinfo.week, week = cinfo.week,
year = cinfo.year, year = cinfo.year,
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()
end end
local path_to_linkname = function(p) local path_to_linkname = function(p)
local fn = vim.split(p, "/") local fn = vim.split(p, "/")
fn = fn[#fn] fn = fn[#fn]
fn = vim.split(fn, ZkCfg.extension) fn = vim.split(fn, ZkCfg.extension)
fn = fn[1] fn = fn[1]
return fn return fn
end end
-- --
-- FindDailyNotes: -- FindDailyNotes:
-- --------------- -- ---------------
@@ -125,23 +153,24 @@ end
-- Select from daily notes -- Select from daily notes
-- --
local FindDailyNotes = function(opts) 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
create_note_from_template(today, fname, note_type_templates.daily) (fexists ~= true) and ((opts.dailies_create_nonexisting == true) or ZkCfg.dailies_create_nonexisting == true)
end then
create_note_from_template(today, fname, note_type_templates.daily)
end
builtin.find_files({ builtin.find_files({
prompt_title = "Find daily note", prompt_title = "Find daily note",
cwd = ZkCfg.dailies, cwd = ZkCfg.dailies,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
-- --
-- FindWeeklyNotes: -- FindWeeklyNotes:
-- --------------- -- ---------------
@@ -149,23 +178,24 @@ end
-- Select from daily notes -- Select from daily notes
-- --
local FindWeeklyNotes = function(opts) 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
create_note_from_template(title, fname, note_type_templates.weekly) (fexists ~= true) and ((opts.weeklies_create_nonexisting == true) or ZkCfg.weeklies_create_nonexisting == true)
end then
create_note_from_template(title, fname, note_type_templates.weekly)
end
builtin.find_files({ builtin.find_files({
prompt_title = "Find weekly note", prompt_title = "Find weekly note",
cwd = ZkCfg.weeklies, cwd = ZkCfg.weeklies,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
-- --
-- InsertLink: -- InsertLink:
-- ----------- -- -----------
@@ -173,23 +203,22 @@ end
-- Select from all notes and put a link in the current buffer -- Select from all notes and put a link in the current buffer
-- --
local InsertLink = function(_) local InsertLink = function(_)
builtin.find_files({ builtin.find_files({
prompt_title = "Insert link to note", prompt_title = "Insert link to note",
cwd = ZkCfg.home, cwd = ZkCfg.home,
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 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,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
-- --
-- FollowLink: -- FollowLink:
-- ----------- -- -----------
@@ -197,29 +226,30 @@ end
-- find the file linked to by the word under the cursor -- find the file linked to by the word under the cursor
-- --
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)
create_note_from_template(title, fname, note_type_templates.normal) then
end local fname = ZkCfg.home .. "/" .. title .. ZkCfg.extension
create_note_from_template(title, fname, note_type_templates.normal)
end
builtin.find_files({ builtin.find_files({
prompt_title = "Follow link to note...", prompt_title = "Follow link to note...",
cwd = ZkCfg.home, cwd = ZkCfg.home,
default_text = title, default_text = title,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
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:
-- ---------- -- ----------
@@ -240,36 +269,37 @@ end
-- find today's daily note and create it if necessary. -- find today's daily note and create it if necessary.
-- --
local GotoToday = function(opts) 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
create_note_from_template(word, fname, note_type_templates.daily, opts) (fexists ~= true) and ((opts.follow_creates_nonexisting == true) or ZkCfg.follow_creates_nonexisting == true)
end then
create_note_from_template(word, fname, note_type_templates.daily, opts)
end
builtin.find_files({ builtin.find_files({
prompt_title = "Goto day", prompt_title = "Goto day",
cwd = ZkCfg.home, cwd = ZkCfg.home,
default_text = word, default_text = word,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
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)
-- 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:
-- ---------- -- ----------
@@ -277,14 +307,13 @@ end
-- Select from notes -- Select from notes
-- --
local FindNotes = function(_) local FindNotes = function(_)
builtin.find_files({ builtin.find_files({
prompt_title = "Find notes by name", prompt_title = "Find notes by name",
cwd = ZkCfg.home, cwd = ZkCfg.home,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
-- --
-- SearchNotes: -- SearchNotes:
-- ------------ -- ------------
@@ -292,16 +321,15 @@ end
-- find the file linked to by the word under the cursor -- find the file linked to by the word under the cursor
-- --
local SearchNotes = function(_) local SearchNotes = function(_)
builtin.live_grep({ builtin.live_grep({
prompt_title = "Search in notes", prompt_title = "Search in notes",
cwd = ZkCfg.home, cwd = ZkCfg.home,
search_dirs = { ZkCfg.home }, search_dirs = { ZkCfg.home },
default_text = vim.fn.expand("<cword>"), default_text = vim.fn.expand("<cword>"),
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
-- --
-- CreateNote: -- CreateNote:
-- ------------ -- ------------
@@ -309,27 +337,28 @@ 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
builtin.find_files({ builtin.find_files({
prompt_title = "Created note...", prompt_title = "Created note...",
cwd = ZkCfg.home, cwd = ZkCfg.home,
default_text = title, default_text = title,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
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,38 +367,39 @@ 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
builtin.find_files({ builtin.find_files({
prompt_title = "Select template...", prompt_title = "Select template...",
cwd = ZkCfg.templates, cwd = ZkCfg.templates,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
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,
}) })
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:
-- ------------- -- -------------
@@ -377,70 +407,71 @@ end
-- find this week's weekly note and create it if necessary. -- find this week's weekly note and create it if necessary.
-- --
local GotoThisWeek = function(opts) 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
create_note_from_template(title, fname, note_type_templates.weekly) (fexists ~= true) and ((opts.weeklies_create_nonexisting == true) or ZkCfg.weeklies_create_nonexisting == true)
end then
create_note_from_template(title, fname, note_type_templates.weekly)
end
builtin.find_files({ builtin.find_files({
prompt_title = "Goto this week:", prompt_title = "Goto this week:",
cwd = ZkCfg.weeklies, cwd = ZkCfg.weeklies,
default_text = title, default_text = title,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
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
opts.calendar = true opts.calendar = true
GotoToday(opts) GotoToday(opts)
end 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
-- set up calendar integration: forward to our lua functions -- set up calendar integration: forward to our lua functions
local SetupCalendar = function(opts) local SetupCalendar = function(opts)
local defaults = ZkCfg.calendar_opts local defaults = ZkCfg.calendar_opts
opts = opts or defaults opts = opts or defaults
local cmd = [[ local cmd = [[
function! MyCalSign(day, month, year) function! MyCalSign(day, month, year)
return luaeval('require("telekasten").CalendarSignDay(_A[1], _A[2], _A[3])', [a:day, a:month, a:year]) return luaeval('require("telekasten").CalendarSignDay(_A[1], _A[2], _A[3])', [a:day, a:month, a:year])
endfunction endfunction
@@ -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()
@@ -468,59 +500,62 @@ local SetupCalendar = function(opts)
let g:calendar_weeknm = {{weeknm}} let g:calendar_weeknm = {{weeknm}}
]] ]]
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.
-- --
local Setup = function(cfg) local Setup = function(cfg)
cfg = cfg or {} cfg = cfg or {}
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
SetupCalendar(ZkCfg.calendar_opts) or 1
end 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 end
local M = { local M = {
ZkCfg = ZkCfg, ZkCfg = ZkCfg,
find_notes = FindNotes, find_notes = FindNotes,
find_daily_notes = FindDailyNotes, find_daily_notes = FindDailyNotes,
search_notes = SearchNotes, search_notes = SearchNotes,
insert_link = InsertLink, insert_link = InsertLink,
follow_link = FollowLink, follow_link = FollowLink,
setup = Setup, setup = Setup,
goto_today = GotoToday, goto_today = GotoToday,
new_note = CreateNote, new_note = CreateNote,
goto_thisweek = GotoThisWeek, goto_thisweek = GotoThisWeek,
find_weekly_notes = FindWeeklyNotes, find_weekly_notes = FindWeeklyNotes,
yank_notelink = YankLink, yank_notelink = YankLink,
create_note_sel_template = CreateNoteSelectTemplate, create_note_sel_template = CreateNoteSelectTemplate,
show_calendar = ShowCalendar, show_calendar = ShowCalendar,
CalendarSignDay = CalendarSignDay, CalendarSignDay = CalendarSignDay,
CalendarAction = CalendarAction, CalendarAction = CalendarAction,
} }
return M return M