From 8a617c314e75f630aff3b141de45cfdde5813a9e Mon Sep 17 00:00:00 2001 From: Axel Dahlberg Date: Tue, 13 Apr 2021 17:10:42 +0200 Subject: [PATCH] add flag to enable telescope preview and to choose picker (#97) * Flags to enable telescope preview and to choose picker * 4 spaces -> 2 space lua * changes to align with repo code-style --- vim/autoload/himalaya/mbox.lua | 46 --------------------------- vim/autoload/himalaya/mbox.vim | 47 +++++++++++++++++++--------- vim/lua/himalaya/mbox.lua | 57 ++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 61 deletions(-) delete mode 100644 vim/autoload/himalaya/mbox.lua create mode 100644 vim/lua/himalaya/mbox.lua diff --git a/vim/autoload/himalaya/mbox.lua b/vim/autoload/himalaya/mbox.lua deleted file mode 100644 index f32a56f..0000000 --- a/vim/autoload/himalaya/mbox.lua +++ /dev/null @@ -1,46 +0,0 @@ -local actions = require('telescope.actions') -local action_state = require('telescope.actions.state') -local finders = require('telescope.finders') -local pickers = require('telescope.pickers') -local sorters = require('telescope.sorters') -local previewers = require('telescope.previewers') - -function mbox_picker(mboxes) - pickers.new { - results_title = 'Mailboxes', - finder = finders.new_table { - results = mboxes, - entry_maker = function(entry) - return { - value = entry, - display = entry, - ordinal = entry, - preview_command = function(entry, bufnr) - vim.api.nvim_buf_call(bufnr, function() - local page = 0 -- page 0 for preview - local success, output = pcall(vim.fn['himalaya#msg#list_with'], entry.value, page, true) - if not (success) then - vim.cmd('redraw') - vim.bo.modifiable = true - local errors = vim.fn.split(output, '\n') - errors[1] = "Errors: "..errors[1] - vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, errors) - end - end) - end - } - end, - }, - sorter = sorters.fuzzy_with_index_bias(), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - actions.close(prompt_bufnr) - vim.fn['himalaya#mbox#post_input'](selection.display) - end) - - return true - end, - previewer = previewers.display_content.new({}) - }:find() -end diff --git a/vim/autoload/himalaya/mbox.vim b/vim/autoload/himalaya/mbox.vim index 7809487..e9f6d67 100644 --- a/vim/autoload/himalaya/mbox.vim +++ b/vim/autoload/himalaya/mbox.vim @@ -25,27 +25,44 @@ function! himalaya#mbox#curr_mbox() return s:curr_mbox endfunction +function! s:telescope_picker(mboxes) + call luaeval('require("himalaya.mbox").mbox_picker')(a:mboxes) +endfunction + +function! s:fzf_picker(mboxes) + call fzf#run({ + \"source": a:mboxes, + \"sink": function("himalaya#mbox#post_input"), + \"down": "25%", + \}) +endfunction + +function! s:native_picker(mboxes) + let choice = map(copy(a:mboxes), "printf('%s (%d)', v:val, v:key)") + let choice = input(join(choice, ", ") . ": ") + redraw | echo + call himalaya#mbox#post_input(a:mboxes[choice]) +endfunction + +let s:pickers = {"telescope": function("s:telescope_picker"), "fzf": function("s:fzf_picker"), "native": function("s:native_picker")} + function! himalaya#mbox#input() try let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes", 0), "v:val.name") - if &rtp =~ "telescope" - execute printf("luafile %s/mbox.lua", s:dir) - call luaeval(printf("mbox_picker({%s})", join(map(mboxes, "string(v:val)"), ", "))) - - elseif &rtp =~ "fzf" - call fzf#run({ - \"source": mboxes, - \"sink": function("himalaya#mbox#post_input"), - \"down": "25%", - \}) - + if exists("g:himalaya_mailbox_picker") " Get user choice for picker, otherwise check runtimepath + let mbox_picker = g:himalaya_mailbox_picker else - let choice = map(copy(mboxes), "printf('%s (%d)', v:val, v:key)") - let choice = input(join(choice, ", ") . ": ") - redraw | echo - call himalaya#mbox#post_input(mboxes[choice]) + if &rtp =~ "telescope" + let mbox_picker = "telescope" + elseif &rtp =~ "fzf" + let mbox_picker = "fzf" + else + let mbox_picker = "native" + endif endif + + call s:pickers[mbox_picker](mboxes) catch if !empty(v:exception) redraw | call himalaya#shared#log#err(v:exception) diff --git a/vim/lua/himalaya/mbox.lua b/vim/lua/himalaya/mbox.lua new file mode 100644 index 0000000..c1daaa8 --- /dev/null +++ b/vim/lua/himalaya/mbox.lua @@ -0,0 +1,57 @@ +local M = {} + +local actions = require('telescope.actions') +local action_state = require('telescope.actions.state') +local finders = require('telescope.finders') +local pickers = require('telescope.pickers') +local sorters = require('telescope.sorters') +local previewers = require('telescope.previewers') + +local function preview_command(entry, bufnr) + vim.api.nvim_buf_call(bufnr, function() + local page = 0 -- page 0 for preview + local success, output = pcall(vim.fn['himalaya#msg#list_with'], entry.value, page, true) + if not (success) then + vim.cmd('redraw') + vim.bo.modifiable = true + local errors = vim.fn.split(output, '\n') + errors[1] = "Errors: "..errors[1] + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, errors) + end + end) +end + +local function entry_maker(entry) + return { + value = entry, + display = entry, + ordinal = entry, + preview_command = preview_command, + } +end + +M.mbox_picker = function(mboxes) + local finder_opts = {results = mboxes} + local previewer = nil + if vim.g.himalaya_telescope_preview_enabled then + finder_opts.entry_maker = entry_maker + previewer = previewers.display_content.new({}) + end + pickers.new { + results_title = 'Mailboxes', + finder = finders.new_table(finder_opts), + sorter = sorters.fuzzy_with_index_bias(), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + actions.close(prompt_bufnr) + vim.fn['himalaya#mbox#post_input'](selection.display) + end) + + return true + end, + previewer = previewer, + }:find() +end + +return M