Telekasten command with completion

This commit is contained in:
Rene Schallner
2021-12-10 04:25:37 +01:00
parent 2b4076015e
commit d8aeda5300
3 changed files with 119 additions and 87 deletions

View File

@@ -1536,10 +1536,6 @@ local function Setup(cfg)
vim.cmd("autocmd filetype markdown set syntax=telekasten") vim.cmd("autocmd filetype markdown set syntax=telekasten")
end end
vim.cmd(
"command! -nargs=? Telekasten lua require('telekasten').panel(<f-args>)<CR>"
)
if debug then if debug then
print("Resulting config:") print("Resulting config:")
print("-----------------") print("-----------------")
@@ -1547,87 +1543,6 @@ local function Setup(cfg)
end end
end end
local function TelekastenCmd(subcommand)
local commands = {
{ "find notes", "find_notes", M.find_notes },
{ "find daily notes", "find_daily_notes", M.find_daily_notes },
{ "search in notes", "search_notes", M.search_notes },
{ "insert link", "insert_link", M.insert_link },
{ "follow link", "follow_link", M.follow_link },
{ "goto today", "goto_today", M.goto_today },
{ "new note", "new_note", M.new_note },
{ "goto thisweek", "goto_thisweek", M.goto_thisweek },
{ "find weekly notes", "find_weekly_notes", M.find_weekly_notes },
{ "yank link to note", "yank_notelink", M.yank_notelink },
{ "new templated note", "new_templated_note", M.new_templated_note },
{ "show calendar", "show_calendar", M.show_calendar },
{
"paste image from clipboard",
"paste_img_and_link",
M.paste_img_and_link,
},
{ "toggle todo", "toggle_todo", M.toggle_todo },
{ "show backlinks", "show_backlinks", M.show_backlinks },
{ "find friend notes", "find_friends", M.find_friends },
{
"browse images, insert link",
"insert_img_link",
M.insert_img_link,
},
{ "preview image under cursor", "preview_img", M.preview_img },
{ "browse media", "browse_media", M.browse_media },
}
local show = function(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Command palette",
finder = finders.new_table({
results = commands,
entry_maker = function(entry)
return {
value = entry,
display = entry[1],
ordinal = entry[2],
}
end,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
-- important: actions.close(bufnr) is not enough
-- it resulted in: preview_img NOT receiving the prompt as default text
-- apparently it has sth to do with keeping insert mode
actions._close(prompt_bufnr, true)
local selection = action_state.get_selected_entry().value[3]
selection()
end)
return true
end,
}):find()
end
if subcommand then
for _, entry in pairs(commands) do
if entry[2] == subcommand then
local selection = entry[3]
selection()
return
end
end
print("No such subcommand: `" .. subcommand .. "`")
else
local theme
if M.Cfg.command_palette_theme == "ivy" then
theme = themes.get_ivy()
else
theme = themes.get_dropdown()
end
show(theme)
end
end
M.find_notes = FindNotes M.find_notes = FindNotes
M.find_daily_notes = FindDailyNotes M.find_daily_notes = FindDailyNotes
M.search_notes = SearchNotes M.search_notes = SearchNotes
@@ -1651,6 +1566,105 @@ M.insert_img_link = InsertImgLink
M.preview_img = PreviewImg M.preview_img = PreviewImg
M.browse_media = BrowseImg M.browse_media = BrowseImg
M.taglinks = taglinks M.taglinks = taglinks
M.panel = TelekastenCmd
-- Telekasten command, completion
local TelekastenCmd = {
commands = {
{ "find notes", "find_notes", M.find_notes },
{ "find daily notes", "find_daily_notes", M.find_daily_notes },
{ "search in notes", "search_notes", M.search_notes },
{ "insert link", "insert_link", M.insert_link },
{ "follow link", "follow_link", M.follow_link },
{ "goto today", "goto_today", M.goto_today },
{ "new note", "new_note", M.new_note },
{ "goto thisweek", "goto_thisweek", M.goto_thisweek },
{ "find weekly notes", "find_weekly_notes", M.find_weekly_notes },
{ "yank link to note", "yank_notelink", M.yank_notelink },
{
"new templated note",
"new_templated_note",
M.new_templated_note,
},
{ "show calendar", "show_calendar", M.show_calendar },
{
"paste image from clipboard",
"paste_img_and_link",
M.paste_img_and_link,
},
{ "toggle todo", "toggle_todo", M.toggle_todo },
{ "show backlinks", "show_backlinks", M.show_backlinks },
{ "find friend notes", "find_friends", M.find_friends },
{
"browse images, insert link",
"insert_img_link",
M.insert_img_link,
},
{ "preview image under cursor", "preview_img", M.preview_img },
{ "browse media", "browse_media", M.browse_media },
},
}
TelekastenCmd.command = function(subcommand)
local show = function(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Command palette",
finder = finders.new_table({
results = TelekastenCmd.commands,
entry_maker = function(entry)
return {
value = entry,
display = entry[1],
ordinal = entry[2],
}
end,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, _)
actions.select_default:replace(function()
-- important: actions.close(bufnr) is not enough
-- it resulted in: preview_img NOT receiving the prompt as default text
-- apparently it has sth to do with keeping insert mode
actions._close(prompt_bufnr, true)
local selection = action_state.get_selected_entry().value[3]
selection()
end)
return true
end,
}):find()
end
if subcommand then
print("trying subcommand " .. "`" .. subcommand .. "`")
for _, entry in pairs(TelekastenCmd.commands) do
if entry[2] == subcommand then
local selection = entry[3]
selection()
return
end
end
print("No such subcommand: `" .. subcommand .. "`")
else
local theme
if M.Cfg.command_palette_theme == "ivy" then
theme = themes.get_ivy()
else
theme = themes.get_dropdown()
end
show(theme)
end
end
TelekastenCmd.complete = function()
local candidates = {}
for k, v in pairs(TelekastenCmd.commands) do
candidates[k] = v[2]
end
return candidates
end
M.panel = TelekastenCmd.command
M.Command = TelekastenCmd
return M return M

11
plugin/telekasten.vim Normal file
View File

@@ -0,0 +1,11 @@
if exists('g:loaded_telekasten')
finish
endif
let g:loaded_telekasten = 1
function! s:telekasten_complete(arg,line,pos)
let l:candidates = luaeval('require("telekasten").Command.complete()')
return join(l:candidates, "\n")
endfunction
command! -nargs=? -complete=custom,s:telekasten_complete Telekasten lua require('telekasten').panel(<f-args>)

View File

@@ -1,6 +1,11 @@
" not sure if we really want this:
if exists("b:current_syntax")
finish
endif
runtime! syntax/markdown.vim runtime! syntax/markdown.vim
"unlet b:current_syntax unlet b:current_syntax
syn region Comment matchgroup=Comment start="<!--" end="-->" contains=tkTag keepend syn region Comment matchgroup=Comment start="<!--" end="-->" contains=tkTag keepend
syntax region tkLink matchgroup=tkBrackets start=/\[\[/ end=/\]\]/ display oneline syntax region tkLink matchgroup=tkBrackets start=/\[\[/ end=/\]\]/ display oneline
@@ -13,6 +18,8 @@ syntax match tkTagSep "\v\s*,\s*" contained
syntax region tkTag matchgroup=tkBrackets start=/^tags\s*:\s*\[\s*/ end=/\s*\]\s*$/ contains=tkTagSep display oneline syntax region tkTag matchgroup=tkBrackets start=/^tags\s*:\s*\[\s*/ end=/\s*\]\s*$/ contains=tkTagSep display oneline
let b:current_syntax = 'telekasten'
" " just blue " " just blue
" hi tklink ctermfg=Blue cterm=bold,underline " hi tklink ctermfg=Blue cterm=bold,underline
" hi tkBrackets ctermfg=gray " hi tkBrackets ctermfg=gray