re-fix #63 : don't check on setup but on (first) use

This commit is contained in:
Rene Schallner
2022-01-16 14:03:58 +01:00
parent 95a0e0b59b
commit c27eab6007

View File

@@ -146,27 +146,32 @@ local function print_error(s)
end end
local function check_dir_and_ask(dir, purpose) local function check_dir_and_ask(dir, purpose)
local ret = true local ret = false
if dir ~= nil and Path:new(dir):exists() == false then if dir ~= nil and Path:new(dir):exists() == false then
print_error( vim.cmd("echohl ErrorMsg")
local answer = vim.fn.input(
"Telekasten.nvim: " "Telekasten.nvim: "
.. purpose .. purpose
.. " folder " .. " folder "
.. dir .. dir
.. " does not exist!" .. " does not exist!"
.. " Shall I create it? [y/N] "
) )
local answer = vim.fn.input("Shall I create it? [y/N] ") vim.cmd("echohl None")
answer = vim.fn.trim(answer) answer = vim.fn.trim(answer)
if answer == "y" or answer == "Y" then if answer == "y" or answer == "Y" then
if Path:new(dir):mkdir({ exists_ok = false }) then if Path:new(dir):mkdir({ exists_ok = false }) then
vim.cmd('echomsg " "') vim.cmd('echomsg " "')
vim.cmd('echomsg "' .. dir .. ' created"') vim.cmd('echomsg "' .. dir .. ' created"')
ret = true
else else
-- unreachable: plenary.Path:mkdir() will error out -- unreachable: plenary.Path:mkdir() will error out
print_error("Could not create directory " .. dir) print_error("Could not create directory " .. dir)
ret = false ret = false
end end
end end
else
ret = true
end end
return ret return ret
end end
@@ -196,6 +201,10 @@ end
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
-- image stuff -- image stuff
local function imgFromClipboard() local function imgFromClipboard()
if not global_dir_check() then
return
end
if vim.fn.executable("xclip") == 0 then if vim.fn.executable("xclip") == 0 then
vim.api.nvim_err_write("No xclip installed!\n") vim.api.nvim_err_write("No xclip installed!\n")
return return
@@ -1064,6 +1073,10 @@ local function FindDailyNotes(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
local today = os.date(dateformats.date) local today = os.date(dateformats.date)
local fname = M.Cfg.dailies .. "/" .. today .. M.Cfg.extension local fname = M.Cfg.dailies .. "/" .. today .. M.Cfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
@@ -1109,6 +1122,10 @@ local function FindWeeklyNotes(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
local title = os.date(dateformats.isoweek) local title = os.date(dateformats.isoweek)
local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension
local fexists = file_exists(fname) local fexists = file_exists(fname)
@@ -1155,6 +1172,10 @@ local function InsertLink(opts)
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
opts.subdirs_in_links = opts.subdirs_in_links or M.Cfg.subdirs_in_links opts.subdirs_in_links = opts.subdirs_in_links or M.Cfg.subdirs_in_links
if not global_dir_check() then
return
end
find_files_sorted({ find_files_sorted({
prompt_title = "Insert link to note", prompt_title = "Insert link to note",
cwd = M.Cfg.home, cwd = M.Cfg.home,
@@ -1230,6 +1251,10 @@ local function PreviewImg(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
vim.cmd("normal yi)") vim.cmd("normal yi)")
local fname = vim.fn.getreg('"0') local fname = vim.fn.getreg('"0')
@@ -1284,6 +1309,10 @@ local function BrowseImg(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
find_files_sorted({ find_files_sorted({
prompt_title = "Preview image/media", prompt_title = "Preview image/media",
cwd = M.Cfg.home, cwd = M.Cfg.home,
@@ -1327,6 +1356,10 @@ local function FindFriends(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
vim.cmd("normal yi]") vim.cmd("normal yi]")
local title = vim.fn.getreg('"0') local title = vim.fn.getreg('"0')
title = title:gsub("^(%[)(.+)(%])$", "%2") title = title:gsub("^(%[)(.+)(%])$", "%2")
@@ -1432,6 +1465,11 @@ end
-- --
local function GotoToday(opts) local function GotoToday(opts)
opts = opts or {} opts = opts or {}
if not global_dir_check() then
return
end
local today = os.date(dateformats.date) local today = os.date(dateformats.date)
opts.date_table = os.date("*t") opts.date_table = os.date("*t")
opts.date = today opts.date = today
@@ -1451,6 +1489,10 @@ local function FindNotes(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
find_files_sorted({ find_files_sorted({
prompt_title = "Find notes by name", prompt_title = "Find notes by name",
cwd = M.Cfg.home, cwd = M.Cfg.home,
@@ -1476,6 +1518,11 @@ end
-- --
local function InsertImgLink(opts) local function InsertImgLink(opts)
opts = opts or {} opts = opts or {}
if not global_dir_check() then
return
end
find_files_sorted({ find_files_sorted({
prompt_title = "Find image/media", prompt_title = "Find image/media",
cwd = M.Cfg.home, cwd = M.Cfg.home,
@@ -1526,6 +1573,10 @@ local function SearchNotes(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
builtin.live_grep({ builtin.live_grep({
prompt_title = "Search in notes", prompt_title = "Search in notes",
cwd = M.Cfg.home, cwd = M.Cfg.home,
@@ -1558,6 +1609,10 @@ local function ShowBacklinks(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
local title = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }).title local title = Pinfo:new({ filepath = vim.fn.expand("%:p"), M.Cfg }).title
-- or vim.api.nvim_buf_get_name(0) -- or vim.api.nvim_buf_get_name(0)
builtin.live_grep({ builtin.live_grep({
@@ -1640,6 +1695,11 @@ end
local function CreateNoteSelectTemplate(opts) local function CreateNoteSelectTemplate(opts)
opts = opts or {} opts = opts or {}
if not global_dir_check() then
return
end
-- vim.ui.input causes ppl problems - see issue #4 -- vim.ui.input causes ppl problems - see issue #4
-- vim.ui.input({ prompt = "Title: " }, on_create_with_template) -- vim.ui.input({ prompt = "Title: " }, on_create_with_template)
local title = vim.fn.input("Title: ") local title = vim.fn.input("Title: ")
@@ -1705,6 +1765,11 @@ local function CreateNote(opts)
opts = opts or {} opts = opts or {}
-- vim.ui.input causes ppl problems - see issue #4 -- vim.ui.input causes ppl problems - see issue #4
-- vim.ui.input({ prompt = "Title: " }, on_create) -- vim.ui.input({ prompt = "Title: " }, on_create)
if not global_dir_check() then
return
end
if M.Cfg.template_handling == "always_ask" then if M.Cfg.template_handling == "always_ask" then
return CreateNoteSelectTemplate(opts) return CreateNoteSelectTemplate(opts)
end end
@@ -1732,6 +1797,10 @@ local function FollowLink(opts)
opts.template_handling = opts.template_handling or M.Cfg.template_handling opts.template_handling = opts.template_handling or M.Cfg.template_handling
opts.new_note_location = opts.new_note_location or M.Cfg.new_note_location opts.new_note_location = opts.new_note_location or M.Cfg.new_note_location
if not global_dir_check() then
return
end
local search_mode = "files" local search_mode = "files"
local title local title
local filename_part = "" local filename_part = ""
@@ -2159,6 +2228,10 @@ local function GotoThisWeek(opts)
opts.close_after_yanking = opts.close_after_yanking opts.close_after_yanking = opts.close_after_yanking
or M.Cfg.close_after_yanking or M.Cfg.close_after_yanking
if not global_dir_check() then
return
end
local dinfo = calculate_dates() local dinfo = calculate_dates()
local title = dinfo.isoweek local title = dinfo.isoweek
local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension local fname = M.Cfg.weeklies .. "/" .. title .. M.Cfg.extension
@@ -2311,6 +2384,10 @@ local function FindAllTags(opts)
opts.cwd = M.Cfg.home opts.cwd = M.Cfg.home
opts.tag_notation = M.Cfg.tag_notation opts.tag_notation = M.Cfg.tag_notation
if not global_dir_check() then
return
end
local tag_map = tagutils.do_find_all_tags(opts) local tag_map = tagutils.do_find_all_tags(opts)
local taglist = {} local taglist = {}
@@ -2474,8 +2551,6 @@ local function Setup(cfg)
print("-----------------") print("-----------------")
print(vim.inspect(M.Cfg)) print(vim.inspect(M.Cfg))
end end
global_dir_check()
end end
M.find_notes = FindNotes M.find_notes = FindNotes