~dricottone/vim-markdown-preview

482823fd245ce4ddb571b70934287a16ae371575 — dricottone 5 years ago 9b3ec41
Refactored everything to fit my use case.
4 files changed, 150 insertions(+), 128 deletions(-)

A TODO.txt
D plugin/applescript/activate-vmp.scpt
D plugin/applescript/search-for-vmp.scpt
M plugin/vim-markdown-preview.vim
A TODO.txt => TODO.txt +2 -0
@@ 0,0 1,2 @@
Rewrite the README


D plugin/applescript/activate-vmp.scpt => plugin/applescript/activate-vmp.scpt +0 -0
D plugin/applescript/search-for-vmp.scpt => plugin/applescript/search-for-vmp.scpt +0 -0
M plugin/vim-markdown-preview.vim => plugin/vim-markdown-preview.vim +148 -128
@@ 1,180 1,200 @@
"============================================================
"                    Vim Markdown Preview
"   git@github.com:JamshedVesuna/vim-markdown-preview.git
"============================================================

let g:vmp_script_path = resolve(expand('<sfile>:p:h'))

let g:vmp_osname = 'Unidentified'
" 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 = 'win32'
  let g:vmp_osname = 'nt'
elseif has('unix')
  let s:uname = system("uname")
  let g:vmp_osname = 'posix'
else
  let g:vmp_osname = 'Unidentified'
endif

  if has('mac') || has('macunix') || has("gui_macvim") || s:uname == "Darwin\n"
    let g:vmp_osname = 'mac'
    let g:vmp_search_script = g:vmp_script_path . '/applescript/search-for-vmp.scpt'
    let g:vmp_activate_script = g:vmp_script_path . '/applescript/activate-vmp.scpt'
" 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_osname = 'unix'
    let g:vmp_browser = 'Firefox'
  endif
endif

if !exists("g:vim_markdown_preview_browser")
  if g:vmp_osname == 'mac'
    let g:vim_markdown_preview_browser = 'Safari'
" 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:vim_markdown_preview_browser = 'Google Chrome'
    let g:vmp_outdir = '/tmp/'
  endif
else
  " For local output files, don't specify a path
  let g:vmp_outdir = ''
endif

if !exists("g:vim_markdown_preview_temp_file")
  let g:vim_markdown_preview_temp_file = 0
" Auto-delete mode
if !exists("g:vmp_auto_delete")
  let g:vmp_auto_delete = 0
endif

if !exists("g:vim_markdown_preview_toggle")
  let g:vim_markdown_preview_toggle = 0
" Auto-preview mode
if !exists("g:vmp_auto_preview")
  let g:vmp_auto_preview = 0
endif

if !exists("g:vim_markdown_preview_github")
  let g:vim_markdown_preview_github = 0
" Preview hotkey
if !exists("g:vmp_hotkey")
    let g:vmp_hotkey='<C-p>'
endif

if !exists("g:vim_markdown_preview_perl")
  let g:vim_markdown_preview_perl = 0
" 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

if !exists("g:vim_markdown_preview_pandoc")
  let g:vim_markdown_preview_pandoc = 0
" Preview title
if !exists("g:vmp_preview_title")
  let g:vmp_preview_title = "PREVIEW"
endif

if !exists("g:vim_markdown_preview_use_xdg_open")
    let g:vim_markdown_preview_use_xdg_open = 0
" Renderers
if !exists("g:vmp_renderer_pandoc")
  let g:vmp_renderer_pandoc = 0
endif

if !exists("g:vim_markdown_preview_hotkey")
    let g:vim_markdown_preview_hotkey='<C-p>'
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

function! Vim_Markdown_Preview()
  let b:curr_file = expand('%:p')

  if g:vim_markdown_preview_github == 1
    call system('grip "' . b:curr_file . '" --export /tmp/vim-markdown-preview.html --title vim-markdown-preview.html')
  elseif g:vim_markdown_preview_perl == 1
    call system('Markdown.pl "' . b:curr_file . '" > /tmp/vim-markdown-preview.html')
  elseif g:vim_markdown_preview_pandoc == 1
    call system('pandoc --standalone "' . b:curr_file . '" > /tmp/vim-markdown-preview.html')
  else
    call system('markdown "' . b:curr_file . '" > /tmp/vim-markdown-preview.html')
  endif
  if v:shell_error
    echo 'Please install the necessary requirements: https://github.com/JamshedVesuna/vim-markdown-preview#requirements'
  endif
" ^ environment variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" v functions

  if g:vmp_osname == 'unix'
    let chrome_wid = system("xdotool search --name 'vim-markdown-preview.html - " . g:vim_markdown_preview_browser . "'")
    if !chrome_wid
      if g:vim_markdown_preview_use_xdg_open == 1
        call system('xdg-open /tmp/vim-markdown-preview.html 1>/dev/null 2>/dev/null &')
      else
        call system('see /tmp/vim-markdown-preview.html 1>/dev/null 2>/dev/null &')
      endif
    else
      let curr_wid = system('xdotool getwindowfocus')
      call system('xdotool windowmap ' . chrome_wid)
      call system('xdotool windowactivate ' . chrome_wid)
      call system("xdotool key 'ctrl+r'")
      call system('xdotool windowactivate ' . curr_wid)
    endif
  endif
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

  if g:vmp_osname == 'mac'
    if g:vim_markdown_preview_browser == "Google Chrome"
      let b:vmp_preview_in_browser = system('osascript "' . g:vmp_search_script . '"')
      if b:vmp_preview_in_browser == 1
        call system('open -g /tmp/vim-markdown-preview.html')
      else
        call system('osascript "' . g:vmp_activate_script . '"')
      endif
    else
      call system('open -a "' . g:vim_markdown_preview_browser . '" -g /tmp/vim-markdown-preview.html')
    endif
  endif
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

  if g:vim_markdown_preview_temp_file == 1
    sleep 200m
    call system('rm /tmp/vim-markdown-preview.html')
  endif
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

"Renders html locally and displays images
function! Vim_Markdown_Preview_Local()
function! Vim_Markdown_Preview()
  let b:curr_file = expand('%:p')

  if g:vim_markdown_preview_github == 1
    call system('grip "' . b:curr_file . '" --export vim-markdown-preview.html --title vim-markdown-preview.html')
  elseif g:vim_markdown_preview_perl == 1
    call system('Markdown.pl "' . b:curr_file . '" > /tmp/vim-markdown-preview.html')
  elseif g:vim_markdown_preview_pandoc == 1
    call system('pandoc --standalone "' . b:curr_file . '" > /tmp/vim-markdown-preview.html')
  " 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
    call system('markdown "' . b:curr_file . '" > vim-markdown-preview.html')
    let call = Get_Call_pandoc()
  endif

  echo 'Rendering...'
  call system(call)
  if v:shell_error
    echo 'Please install the necessary requirements: https://github.com/JamshedVesuna/vim-markdown-preview#requirements'
    echom 'Error in calling Markdown renderer. Is pandoc installed?.'
  endif

  if g:vmp_osname == 'unix'
    let chrome_wid = system("xdotool search --name vim-markdown-preview.html - " . g:vim_markdown_preview_browser . "'")
    if !chrome_wid
      if g:vim_markdown_preview_use_xdg_open == 1
        call system('xdg-open vim-markdown-preview.html 1>/dev/null 2>/dev/null &')
      else
        call system('see vim-markdown-preview.html 1>/dev/null 2>/dev/null &')
  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
      let curr_wid = system('xdotool getwindowfocus')
      call system('xdotool windowmap ' . chrome_wid)
      call system('xdotool windowactivate ' . chrome_wid)
      " Refresh existing window
      echo 'Refreshing preview...'
      call system('xdotool windowmap ' . preview_id)
      call system('xdotool windowactivate ' . preview_id)
      call system("xdotool key 'ctrl+r'")
      call system('xdotool windowactivate ' . curr_wid)
    endif
  endif

  if g:vmp_osname == 'mac'
    if g:vim_markdown_preview_browser == "Google Chrome"
      let b:vmp_preview_in_browser = system('osascript "' . g:vmp_search_script . '"')
      if b:vmp_preview_in_browser == 1
        call system('open -g vim-markdown-preview.html')
      else
        call system('osascript "' . g:vmp_activate_script . '"')
      endif
    else
      call system('open -a "' . g:vim_markdown_preview_browser . '" -g vim-markdown-preview.html')
    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:vim_markdown_preview_temp_file == 1
    sleep 200m
    call system('rm vim-markdown-preview.html')
  if g:vmp_auto_delete == 1
    sleep 500m
    call system('rm ' . g:vmp_outfile)
  endif
endfunction

if g:vim_markdown_preview_toggle == 0
  "Maps vim_markdown_preview_hotkey to Vim_Markdown_Preview()
  :exec 'autocmd Filetype markdown,md map <buffer> ' . g:vim_markdown_preview_hotkey . ' :call Vim_Markdown_Preview()<CR>'
elseif g:vim_markdown_preview_toggle == 1
  "Display images - Maps vim_markdown_preview_hotkey to Vim_Markdown_Preview_Local() - saves the html file locally
  "and displays images in path
  :exec 'autocmd Filetype markdown,md map <buffer> ' . g:vim_markdown_preview_hotkey . ' :call Vim_Markdown_Preview_Local()<CR>'
elseif g:vim_markdown_preview_toggle == 2
  "Display images - Automatically call Vim_Markdown_Preview_Local() on buffer write
  autocmd BufWritePost *.markdown,*.md :call Vim_Markdown_Preview_Local()
elseif g:vim_markdown_preview_toggle == 3
  "Automatically call Vim_Markdown_Preview() on buffer write
" ^ 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