From d8aeda53005a7eeada93eb9e1b4e51ef2e69fafd Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Fri, 10 Dec 2021 04:25:37 +0100 Subject: [PATCH] Telekasten command with completion --- lua/telekasten.lua | 186 +++++++++++++++++++++++------------------- plugin/telekasten.vim | 11 +++ syntax/telekasten.vim | 9 +- 3 files changed, 119 insertions(+), 87 deletions(-) create mode 100644 plugin/telekasten.vim diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 276b5ef..b008b7e 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1536,10 +1536,6 @@ local function Setup(cfg) vim.cmd("autocmd filetype markdown set syntax=telekasten") end - vim.cmd( - "command! -nargs=? Telekasten lua require('telekasten').panel()" - ) - if debug then print("Resulting config:") print("-----------------") @@ -1547,87 +1543,6 @@ local function Setup(cfg) 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_daily_notes = FindDailyNotes M.search_notes = SearchNotes @@ -1651,6 +1566,105 @@ M.insert_img_link = InsertImgLink M.preview_img = PreviewImg M.browse_media = BrowseImg 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 diff --git a/plugin/telekasten.vim b/plugin/telekasten.vim new file mode 100644 index 0000000..d59435f --- /dev/null +++ b/plugin/telekasten.vim @@ -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() diff --git a/syntax/telekasten.vim b/syntax/telekasten.vim index 60f64b3..459aa65 100644 --- a/syntax/telekasten.vim +++ b/syntax/telekasten.vim @@ -1,6 +1,11 @@ +" not sure if we really want this: +if exists("b:current_syntax") + finish +endif runtime! syntax/markdown.vim -"unlet b:current_syntax +unlet b:current_syntax + syn region Comment matchgroup=Comment start="" contains=tkTag keepend 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 +let b:current_syntax = 'telekasten' + " " just blue " hi tklink ctermfg=Blue cterm=bold,underline " hi tkBrackets ctermfg=gray