Skip to content

Instantly share code, notes, and snippets.

@ichizok
Created December 27, 2021 02:58
Show Gist options
  • Save ichizok/9e238bbc8bc36b0eb4bc394f48db257c to your computer and use it in GitHub Desktop.
Save ichizok/9e238bbc8bc36b0eb4bc394f48db257c to your computer and use it in GitHub Desktop.
" Microsoft Office viewer
if exists('g:loaded_office')
finish
endif
let g:loaded_office = 1
let s:cpo_save = &cpo
set cpo&vim
function! s:convert2text(filepath) abort
setl modifiable noswapfile
silent %d
let ext = fnamemodify(a:filepath, ':e')
if ext ==# 'docx'
" Microsoft Word
let cmd = ['docx2md', a:filepath]
else "if ext ==# 'pptx' || ext ==# 'xlsx'
" Microsoft Excel, PowerPoint
let cmd = ['tika', '--text', a:filepath]
endif
let job = #{completed: v:false}
function! job.close_cb(ch) abort
let self.completed = v:true
endfunction
let job.handle = job_start(cmd, #{out_io: 'buffer', out_buf: bufnr(), err_io: 'null', close_cb: job.close_cb})
if job_status(job.handle) !=# 'run'
throw 'Could not convert ' .. ext .. ' to text'
endif
while !job.completed
sleep 10m
endwhile
1
setl nomodified readonly
setf office
endfunction
augroup office-vim
autocmd!
autocmd BufReadCmd *.docx,*.pptx,*.xlsx call <SID>convert2text(expand('<amatch>:p'))
augroup END
if !exists('g:loaded_zipPlugin')
let s:let_zipPlugin_ext = system('grep "^\\s*let g:zipPlugin_ext\\s*=" ' .. $VIMRUNTIME .. '/plugin/zipPlugin.vim')
call execute(s:let_zipPlugin_ext)
unlet s:let_zipPlugin_ext
let g:zipPlugin_ext = substitute(g:zipPlugin_ext, ',\?\*\.\%(docx\|pptx\|xlsx\)', '', 'g')
endif
let &cpo = s:cpo_save
unlet s:cpo_save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment