add copy/move/delete vim features

This commit is contained in:
Clément DOUIN 2021-05-08 21:35:28 +02:00
parent 98173c49e6
commit ac3b0bfdde
No known key found for this signature in database
GPG key ID: 69C9B9CFFDEE2DEF
6 changed files with 97 additions and 50 deletions

View file

@ -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()

View file

@ -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]")

View file

@ -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("<plug>(himalaya-%s)", plug)
execute printf("%snoremap <silent>%s :call himalaya#%s()<cr>", mode, plug, name)
execute printf("%snoremap <silent>%s :call himalaya#%s<cr>", mode, plug, name)
if !hasmapto(plug, mode)
execute printf("%smap <nowait><buffer>%s %s", mode, key, plug)

View file

@ -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", "<cr>", "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", "<cr>", "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()" ],
\])

View file

@ -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()"],
\])

View file

@ -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