vim-polyglot/ftplugin/mail_vifm.vim

69 lines
1.8 KiB
VimL
Raw Permalink Normal View History

if exists('g:polyglot_disabled') && index(g:polyglot_disabled, 'vifm') != -1
finish
endif
2017-09-27 14:23:42 -04:00
" Mail file type extension to pick files for attachments via vifm
" Maintainer: xaizek <xaizek@posteo.net>
2018-02-05 22:15:01 -05:00
" Last Change: January 02, 2018
2017-09-27 14:23:42 -04:00
" Insert attachment picked via vifm after 'Subject' header
2018-02-05 22:15:01 -05:00
function! s:AddMailAttachments()
call vifm#globals#Init()
" XXX: similar code is in plugins/vifm.vim, but it's different in details
2017-09-27 14:23:42 -04:00
let l:listf = tempname()
if !has('nvim')
if has('gui_running')
execute 'silent !' g:vifm_term g:vifm_exec
\ '--choose-files' shellescape(l:listf, 1)
\ g:vifm_exec_args
else
execute 'silent !' g:vifm_exec
\ '--choose-files' shellescape(l:listf, 1)
\ g:vifm_exec_args
endif
redraw!
call s:HandleRunResults(v:shell_error, l:listf)
else
" Work around handicapped neovim...
let callback = { 'listf': l:listf }
2018-01-10 17:50:02 -05:00
function! callback.on_exit(id, code, event)
2017-09-27 14:23:42 -04:00
buffer #
silent! bdelete! #
call s:HandleRunResults(a:code, self.listf)
endfunction
enew
call termopen(g:vifm_exec . ' --choose-files ' . shellescape(l:listf, 1)
\. ' ' . g:vifm_exec_args, callback)
startinsert
endif
endfunction
function! s:HandleRunResults(exitcode, listf)
if a:exitcode != 0
echohl WarningMsg
echo 'Got non-zero code from vifm: ' . a:exitcode
echohl None
call delete(a:listf)
return
endif
let l:insert_pos = search('^Subject:', 'nw')
if filereadable(a:listf) && l:insert_pos != 0
for line in readfile(a:listf)
call append(l:insert_pos, 'Attach: '.line)
let l:insert_pos += 1
endfor
endif
call delete(a:listf)
endfunction
2018-02-05 22:15:01 -05:00
nnoremap <buffer> <silent> <localleader>a :call <sid>AddMailAttachments()<cr>
2017-09-27 14:23:42 -04:00
" vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 :