From ac3b0bfddecfffe5bf3b140ef1462109b4348c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sat, 8 May 2021 21:35:28 +0200 Subject: [PATCH] add copy/move/delete vim features --- vim/autoload/himalaya/mbox.vim | 61 +++++++++++------------ vim/autoload/himalaya/msg.vim | 49 ++++++++++++++++++ vim/autoload/himalaya/shared/bindings.vim | 2 +- vim/ftplugin/himalaya-msg-list.vim | 21 ++++---- vim/ftplugin/himalaya-msg-read.vim | 10 ++-- vim/lua/himalaya/mbox.lua | 4 +- 6 files changed, 97 insertions(+), 50 deletions(-) diff --git a/vim/autoload/himalaya/mbox.vim b/vim/autoload/himalaya/mbox.vim index 7692cdef..b7c9ae5f 100644 --- a/vim/autoload/himalaya/mbox.vim +++ b/vim/autoload/himalaya/mbox.vim @@ -18,6 +18,27 @@ function! himalaya#mbox#next_page() call himalaya#msg#list() endfunction +" Pickers + +function! s:telescope_picker(cb, mboxes) + call luaeval("require('himalaya.mbox').mbox_picker")(a:cb, a:mboxes) +endfunction + +function! s:fzf_picker(cb, mboxes) + call fzf#run({ + \"source": a:mboxes, + \"sink": a:cb, + \"down": "25%", + \}) +endfunction + +function! s:native_picker(cb, mboxes) + let choice = map(copy(a:mboxes), "printf('%s (%d)', v:val, v:key)") + let choice = input(join(choice, ", ") . ": ") + redraw | echo + call function(a:cb)(a:mboxes[choice]) +endfunction + " Mailbox let s:curr_mbox = "INBOX" @@ -25,49 +46,23 @@ 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() +function! himalaya#mbox#pick(cb) try let mboxes = map(s:cli("mailboxes", [], "Fetching mailboxes", 0), "v:val.name") - " Get user choice for picker, otherwise check runtimepath if exists("g:himalaya_mailbox_picker") - let mbox_picker = g:himalaya_mailbox_picker + let picker = g:himalaya_mailbox_picker else if &rtp =~ "telescope" - let mbox_picker = "telescope" + let picker = "telescope" elseif &rtp =~ "fzf" - let mbox_picker = "fzf" + let picker = "fzf" else - let mbox_picker = "native" + let picker = "native" endif endif - call s:pickers[mbox_picker](mboxes) + execute printf("call s:%s_picker(a:cb, mboxes)", picker) catch if !empty(v:exception) redraw | call himalaya#shared#log#err(v:exception) @@ -75,7 +70,7 @@ function! himalaya#mbox#input() endtry endfunction -function! himalaya#mbox#post_input(mbox) +function! himalaya#mbox#set(mbox) let s:curr_mbox = a:mbox let s:curr_page = 0 call himalaya#msg#list() diff --git a/vim/autoload/himalaya/msg.vim b/vim/autoload/himalaya/msg.vim index ef22cf1b..53610f89 100644 --- a/vim/autoload/himalaya/msg.vim +++ b/vim/autoload/himalaya/msg.vim @@ -147,6 +147,55 @@ function! himalaya#msg#forward() endtry endfunction +function! himalaya#msg#copy(target_mbox) + try + let pos = getpos(".") + let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id + let source_mbox = himalaya#mbox#curr_mbox() + let msg = s:cli("--mailbox %s copy %d %s", [shellescape(source_mbox), msg_id, shellescape(a:target_mbox)], "Copying message", 1) + call himalaya#msg#list_with(source_mbox, himalaya#mbox#curr_page(), 1) + call setpos('.', pos) + catch + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif + endtry +endfunction + +function! himalaya#msg#move(target_mbox) + try + let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id + let choice = input(printf("Are you sure you want to move the message %d? (y/N) ", msg_id)) + if tolower(choice) != "y" | redraw | echo | return | endif + let pos = getpos(".") + let source_mbox = himalaya#mbox#curr_mbox() + let msg = s:cli("--mailbox %s move %d %s", [shellescape(source_mbox), msg_id, shellescape(a:target_mbox)], "Moving message", 1) + call himalaya#msg#list_with(source_mbox, himalaya#mbox#curr_page(), 1) + call setpos('.', pos) + catch + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif + endtry +endfunction + +function! himalaya#msg#delete() + try + let msg_id = stridx(bufname("%"), "Himalaya messages") == 0 ? s:get_focused_msg_id() : s:msg_id + let choice = input(printf("Are you sure you want to delete the message %d? (y/N) ", msg_id)) + if tolower(choice) != "y" | redraw | echo | return | endif + let pos = getpos(".") + let mbox = himalaya#mbox#curr_mbox() + let msg = s:cli("--mailbox %s delete %d", [shellescape(mbox), msg_id], "Deleting message", 1) + call himalaya#msg#list_with(mbox, himalaya#mbox#curr_page(), 1) + call setpos('.', pos) + catch + if !empty(v:exception) + redraw | call himalaya#shared#log#err(v:exception) + endif + endtry +endfunction + function! himalaya#msg#draft_save() let s:draft = join(getline(1, "$"), "\n") redraw | call s:log("Save draft [OK]") diff --git a/vim/autoload/himalaya/shared/bindings.vim b/vim/autoload/himalaya/shared/bindings.vim index 0cac5658..90a0f4d0 100644 --- a/vim/autoload/himalaya/shared/bindings.vim +++ b/vim/autoload/himalaya/shared/bindings.vim @@ -2,7 +2,7 @@ function! himalaya#shared#bindings#define(bindings) for [mode, key, name] in a:bindings let plug = substitute(name, "[#_]", "-", "g") let plug = printf("(himalaya-%s)", plug) - execute printf("%snoremap %s :call himalaya#%s()", mode, plug, name) + execute printf("%snoremap %s :call himalaya#%s", mode, plug, name) if !hasmapto(plug, mode) execute printf("%smap %s %s", mode, key, plug) diff --git a/vim/ftplugin/himalaya-msg-list.vim b/vim/ftplugin/himalaya-msg-list.vim index 6a3419e0..1434dfed 100644 --- a/vim/ftplugin/himalaya-msg-list.vim +++ b/vim/ftplugin/himalaya-msg-list.vim @@ -5,13 +5,16 @@ setlocal nomodifiable setlocal nowrap call himalaya#shared#bindings#define([ - \["n", "gm" , "mbox#input" ], - \["n", "gp" , "mbox#prev_page" ], - \["n", "gn" , "mbox#next_page" ], - \["n", "", "msg#read" ], - \["n", "gw" , "msg#write" ], - \["n", "gr" , "msg#reply" ], - \["n", "gR" , "msg#reply_all" ], - \["n", "gf" , "msg#forward" ], - \["n", "ga" , "msg#attachments"], + \["n", "gm" , "mbox#pick('himalaya#mbox#set')"], + \["n", "gp" , "mbox#prev_page()" ], + \["n", "gn" , "mbox#next_page()" ], + \["n", "", "msg#read()" ], + \["n", "gw" , "msg#write()" ], + \["n", "gr" , "msg#reply()" ], + \["n", "gR" , "msg#reply_all()" ], + \["n", "gf" , "msg#forward()" ], + \["n", "ga" , "msg#attachments()" ], + \["n", "gC" , "mbox#pick('himalaya#msg#copy')"], + \["n", "gM" , "mbox#pick('himalaya#msg#move')"], + \["n", "gD" , "msg#delete()" ], \]) diff --git a/vim/ftplugin/himalaya-msg-read.vim b/vim/ftplugin/himalaya-msg-read.vim index 3447039f..5c5133a0 100644 --- a/vim/ftplugin/himalaya-msg-read.vim +++ b/vim/ftplugin/himalaya-msg-read.vim @@ -8,9 +8,9 @@ setlocal nomodifiable syntax on call himalaya#shared#bindings#define([ - \["n", "gw", "msg#write" ], - \["n", "gr", "msg#reply" ], - \["n", "gR", "msg#reply_all" ], - \["n", "gf", "msg#forward" ], - \["n", "ga", "msg#attachments"], + \["n", "gw", "msg#write()" ], + \["n", "gr", "msg#reply()" ], + \["n", "gR", "msg#reply_all()" ], + \["n", "gf", "msg#forward()" ], + \["n", "ga", "msg#attachments()"], \]) diff --git a/vim/lua/himalaya/mbox.lua b/vim/lua/himalaya/mbox.lua index 941eb823..9fff03a8 100644 --- a/vim/lua/himalaya/mbox.lua +++ b/vim/lua/himalaya/mbox.lua @@ -30,7 +30,7 @@ local function entry_maker(entry) } end -M.mbox_picker = function(mboxes) +M.mbox_picker = function(cb, mboxes) local finder_opts = {results = mboxes} local previewer = nil if vim.g.himalaya_telescope_preview_enabled then @@ -45,7 +45,7 @@ M.mbox_picker = function(mboxes) actions.select_default:replace(function() local selection = action_state.get_selected_entry() actions.close(prompt_bufnr) - vim.fn['himalaya#mbox#post_input'](selection.display) + vim.fn[cb](selection.display) end) return true