Merge pull request #1 from Conni2461/no_more_globals

fix: no more globals
This commit is contained in:
Rene Schallner
2021-11-24 12:18:31 +01:00
committed by GitHub
2 changed files with 61 additions and 74 deletions

View File

@@ -70,7 +70,7 @@ Somewhere in your vim config, put a snippet like this:
lua << END lua << END
local home = vim.fn.expand("~/zettelkasten") local home = vim.fn.expand("~/zettelkasten")
require('telekasten').setup({ require('telekasten').setup({
home = home home = home,
dailies = home .. '/' .. 'daily', dailies = home .. '/' .. 'daily',
weeklies = home .. '/' .. 'weekly', weeklies = home .. '/' .. 'weekly',
templates = home .. '/' .. 'templates', templates = home .. '/' .. 'templates',
@@ -303,4 +303,3 @@ hi tkHighlight ctermbg=yellow ctermfg=darkred cterm=bold
Currently, the following things are hardcoded: Currently, the following things are hardcoded:
- the file naming format for daily note files: `YYYY-MM-DD.ext` (e.g. `2021-11-21.md`) - the file naming format for daily note files: `YYYY-MM-DD.ext` (e.g. `2021-11-21.md`)
- the file naming format for weekly note files: `YYYY-Www.ext` (e.g. `2021-W46.md`) - the file naming format for weekly note files: `YYYY-Www.ext` (e.g. `2021-W46.md`)

View File

@@ -1,5 +1,6 @@
local builtin = require "telescope.builtin" local builtin = require "telescope.builtin"
local actions = require("telescope.actions") local action_state = require "telescope.actions.state" local actions = require("telescope.actions")
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
@@ -10,7 +11,7 @@ local vim = vim
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
local home = vim.fn.expand("~/zettelkasten") local home = vim.fn.expand("~/zettelkasten")
ZkCfg = { local ZkCfg = {
home = home, home = home,
dailies = home .. '/' .. 'daily', dailies = home .. '/' .. 'daily',
weeklies = home .. '/' .. 'weekly', weeklies = home .. '/' .. 'weekly',
@@ -123,7 +124,7 @@ end
-- --
-- Select from daily notes -- Select from daily notes
-- --
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")
@@ -147,7 +148,7 @@ end
-- --
-- Select from daily notes -- Select from daily notes
-- --
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")
@@ -171,13 +172,11 @@ 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
-- --
InsertLink = function(opts) local InsertLink = function(_)
opts = opts or {}
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, map) attach_mappings = function(prompt_bufnr, _)
map = map -- get rid of lsp error
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()
@@ -197,7 +196,7 @@ end
-- --
-- find the file linked to by the word under the cursor -- find the file linked to by the word under the cursor
-- --
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')
@@ -227,7 +226,7 @@ end
-- --
-- Create and yank a [[link]] from the current note. -- Create and yank a [[link]] from the current note.
-- --
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)
@@ -240,9 +239,7 @@ 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)
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")
@@ -257,8 +254,7 @@ GotoToday = function(opts)
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, map) attach_mappings = function(prompt_bufnr, _)
map = map -- get rid of lsp error
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
@@ -280,8 +276,7 @@ end
-- --
-- Select from notes -- Select from notes
-- --
FindNotes = function(opts) local FindNotes = function(_)
opts = opts or {}
builtin.find_files({ builtin.find_files({
prompt_title = "Find notes by name", prompt_title = "Find notes by name",
cwd = ZkCfg.home, cwd = ZkCfg.home,
@@ -296,9 +291,7 @@ end
-- --
-- find the file linked to by the word under the cursor -- find the file linked to by the word under the cursor
-- --
SearchNotes = function(opts) local SearchNotes = function(_)
opts = opts or {}
builtin.live_grep({ builtin.live_grep({
prompt_title = "Search in notes", prompt_title = "Search in notes",
cwd = ZkCfg.home, cwd = ZkCfg.home,
@@ -332,8 +325,7 @@ local function on_create(title)
}) })
end end
CreateNote = function(opts) local CreateNote = function(_)
opts = opts or {}
vim.ui.input({prompt = 'Title: '}, on_create) vim.ui.input({prompt = 'Title: '}, on_create)
end end
@@ -360,8 +352,7 @@ local function on_create_with_template(title)
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, map) attach_mappings = function(prompt_bufnr, _)
map = map -- get rid of lsp error
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
@@ -374,8 +365,7 @@ local function on_create_with_template(title)
}) })
end end
CreateNoteSelectTemplate = function(opts) local CreateNoteSelectTemplate = function(_)
opts = opts or {}
vim.ui.input({prompt = 'Title: '}, on_create_with_template) vim.ui.input({prompt = 'Title: '}, on_create_with_template)
end end
@@ -386,7 +376,7 @@ end
-- --
-- find this week's weekly note and create it if necessary. -- find this week's weekly note and create it if necessary.
-- --
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")
@@ -411,7 +401,7 @@ end
-- -------------- -- --------------
-- 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
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
@@ -420,10 +410,7 @@ CalendarSignDay = function(day, month, year)
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
CalendarAction = function(day, month, year, weekday, dir) local CalendarAction = function(day, month, year, weekday, _)
-- lsp
dir = dir
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
@@ -436,7 +423,7 @@ CalendarAction = function(day, month, year, weekday, dir)
GotoToday(opts) GotoToday(opts)
end end
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
@@ -449,13 +436,13 @@ ShowCalendar = function(opts)
end end
-- set up calendar integration: forward to our lua functions -- set up calendar integration: forward to our lua functions
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('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
function! MyCalAction(day, month, year, weekday, dir) function! MyCalAction(day, month, year, weekday, dir)
@@ -464,7 +451,7 @@ 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('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()
@@ -492,7 +479,7 @@ end
-- --
-- Overrides config with elements from cfg. See top of file for defaults. -- Overrides config with elements from cfg. See top of file for defaults.
-- --
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
@@ -533,6 +520,7 @@ local M = {
yank_notelink = YankLink, yank_notelink = YankLink,
create_note_sel_template = CreateNoteSelectTemplate, create_note_sel_template = CreateNoteSelectTemplate,
show_calendar = ShowCalendar, show_calendar = ShowCalendar,
CalendarSignDay = CalendarSignDay,
CalendarAction = CalendarAction,
} }
return M return M