" Note: Abbreviation `vmp` in variables refers to vim-markdown-preview
" Check OS for default handling
if has('win32') || has('win64')
" Not yet used
let g:vmp_osname = 'nt'
elseif has('unix')
let g:vmp_osname = 'posix'
else
let g:vmp_osname = 'Unidentified'
endif
" Set default browser for preview window
if !exists("g:vmp_browser")
if g:vmp_osname == 'nt'
let g:vmp_browser = 'microsoft-edge'
else
let g:vmp_browser = 'Firefox'
endif
endif
" Local vs system temporary files
if !exists("g:vmp_temp_mode")
let g:vmp_temp_mode = 0
endif
if g:vmp_temp_mode == 1
" For system temporary files, specify `/tmp/` (POSIX) or `%TEMP%\` (NT)
if g:vmp_osname == 'nt'
let g:vmp_outdir = system('$env:temp')
if !g:vmp_outdir
echom 'Cannot resolve `$env:temp`. Using default local mode.'
let g:vmp_outdir = ''
else
let g:vmp_outdir .= '\\'
endif
else
let g:vmp_outdir = '/tmp/'
endif
else
" For local output files, don't specify a path
let g:vmp_outdir = ''
endif
" Auto-delete mode
if !exists("g:vmp_auto_delete")
let g:vmp_auto_delete = 0
endif
" Auto-preview mode
if !exists("g:vmp_auto_preview")
let g:vmp_auto_preview = 0
endif
" Preview hotkey
if !exists("g:vmp_hotkey")
let g:vmp_hotkey='<C-p>'
endif
" Output file name
if !exists("g:vmp_filename")
let g:vmp_filename = "vim_markdown_preview.html"
endif
let g:vmp_outfile = g:vmp_outdir . g:vmp_filename
" Preview title
if !exists("g:vmp_preview_title")
let g:vmp_preview_title = "PREVIEW"
endif
" Renderers
if !exists("g:vmp_renderer_pandoc")
let g:vmp_renderer_pandoc = 0
endif
if !exists("g:vmp_renderer_grip")
let g:vmp_renderer_grip = 0
endif
if !exists("g:vmp_renderer_perl")
let g:vmp_renderer_perl = 0
endif
if !exists("g:vmp_renderer_markdown")
let g:vmp_renderer_markdown = 0
endif
" ^ environment variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" v functions
function! Get_Call_pandoc()
let b:curr_file = expand('%:p')
return 'pandoc --standalone "' . b:curr_file . '"'
\ . ' --css=$HOME/src/markdown-css/github.css'
\ . ' --metadata title=' . g:vmp_preview_title
\ . ' > ' . g:vmp_outfile
endfunction
function! Get_Call_grip()
let b:curr_file = expand('%:p')
return 'grip "' . b:curr_file . '"'
\ . ' --export ' . g:vmp_outfile
\ . ' --title ' . g:vmp_preview_title
endfunction
function! Get_Call_perl()
let b:curr_file = expand('%:p')
return 'Markdown.pl "' . b:curr_file . '"'
\ . ' > ' . g:vmp_outfile
endfunction
function! Get_Call_markdown()
let b:curr_file = expand('%:p')
return 'markdown "' . b:curr_file . '"'
\ . ' > ' . g:vmp_outfile
endfunction
function! Vim_Markdown_Preview()
let b:curr_file = expand('%:p')
" Order of preferences: pandoc, grip, Markdown.pl, Markdown.pl installed as
" `Markdown`, then pandoc as a default. To toggle renderers, use...
" - g:vmp_renderer_pandoc
" - g:vmp_renderer_grip
" - g:vmp_renderer_perl (Markdown.pl)
" - g:vmp_renderer_markdown (Markdown.pl installed as `Markdown`)
if g:vmp_renderer_pandoc == 1
let call = Get_Call_pandoc()
elseif g:vmp_renderer_grip == 1
let call = Get_Call_grip()
elseif g:vmp_renderer_perl == 1
let call = Get_Call_perl()
elseif g:vmp_renderer_markdown == 1
let call = Get_Call_markdown()
else
let call = Get_Call_pandoc()
endif
echo 'Rendering...'
call system(call)
if v:shell_error
echom 'Error in calling Markdown renderer. Is pandoc installed?.'
endif
if g:vmp_osname == 'posix'
let preview_name = g:vmp_preview_title . ' - ' . g:vmp_browser
let preview_id = system('xdotool search --name "' . preview_name . '"')
let curr_id = system('xdotool getwindowfocus')
if !preview_id
" Open new window
echo 'Opening preview...'
call system('xdg-open ' . g:vmp_outfile . ' 1>/dev/null 2>/dev/null &')
if v:shell_error
echom 'Error in calling xdg-open. See ' . g:vmp_outfile . '.'
endif
else
" Refresh existing window
echo 'Refreshing preview...'
call system('xdotool windowmap ' . preview_id)
call system('xdotool windowactivate ' . preview_id)
call system("xdotool key 'ctrl+r'")
endif
" Return to vim window
sleep 100m
call system('xdotool windowactivate ' . curr_id)
redraw
elseif g:vmp_osname == 'nt'
echom 'Not yet implemented'
" $Browser=new-object -com internetexplorer.application
" $Browser.navigate2("https://www.yourURL.com")
" $Browser.visible=$true
" $wshell = New-Object -ComObject wscript.shell
" if($wshell.AppActivate('Chrome')) {
" Sleep 1
" $wshell.SendKeys('{F5}')
" } else { break; }
endif
if g:vmp_auto_delete == 1
sleep 500m
call system('rm ' . g:vmp_outfile)
endif
endfunction
" ^ functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" v main
" Switch behavior (preview on Cntl-p vs on buffer write)
if g:vmp_auto_preview == 1
autocmd BufWritePost *.markdown,*.md :call Vim_Markdown_Preview()
else
:exec 'autocmd Filetype markdown,md map <buffer> ' . g:vmp_hotkey . ' :call Vim_Markdown_Preview()<CR>'
endif