From 482823fd245ce4ddb571b70934287a16ae371575 Mon Sep 17 00:00:00 2001 From: dricottone Date: Sun, 12 May 2019 14:47:07 -0400 Subject: [PATCH] Refactored everything to fit my use case. --- TODO.txt | 2 + plugin/applescript/activate-vmp.scpt | Bin 3014 -> 0 bytes plugin/applescript/search-for-vmp.scpt | Bin 1720 -> 0 bytes plugin/vim-markdown-preview.vim | 276 +++++++++++++------------ 4 files changed, 150 insertions(+), 128 deletions(-) create mode 100644 TODO.txt delete mode 100644 plugin/applescript/activate-vmp.scpt delete mode 100644 plugin/applescript/search-for-vmp.scpt diff --git a/TODO.txt b/TODO.txt new file mode 100644 index 0000000..d6e7c06 --- /dev/null +++ b/TODO.txt @@ -0,0 +1,2 @@ +Rewrite the README + diff --git a/plugin/applescript/activate-vmp.scpt b/plugin/applescript/activate-vmp.scpt deleted file mode 100644 index 511402b6a44e9b02d94d46c4019959154c95856f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3014 zcmb_eS#(rY7Txbv#`p4G62vADBBhOjfec^}k_I(~5a

NrECS*D8wYB&L$eszmwq z*Ixb6Kiz-51aVeW6me`+oN&e{gP;*aK@g{QKylc;?|Y^wTmgUjW!;lIoV)Kn=bm$J zEs71U>1=F|&Z?eOYnl%rroH!b1@vS8Io(Gd=zQ-!n`Ni%GZ+#WsqiSb|bdB49BIQ!ox^c@lPi zpjLtLINOs5Se2uK-#h$XteVmtt38|@vYLc)9(Q>h<7$x~3u&oyRlv`~Stq%F=kmKi zYbdrlAfB&I$huTy{(uRdL_j8rKcWm1JqfE%WTH5i-$wW?EDsY;B7WoY8|2o)>VW_z zs7>P6o-7PZpDIJk`Cm--P#&6Ihn1+XQ6a^1FvXL|xkl)9Xb(>KieGx_=r(gTSC8}f z1wZ$29_;2ot6r?8dJ=(y>M$4Q;{s2@nx*wQn9I)`ekKQXE^m!`xF952hdDgv@)!zg zkrR{2CT6CFcFn~+O!F}f)+S6Z#dOTZjp#uis?=ggqgNr6!dmoeWrq=Y|kW7*{$s{1P`ueEFqjE2gm;S{YbaQVTguFVetS0R^u)ssWrX_(Ftmm_0m z0KYaHe>hDN`5}Y{pYOYT|4apdC(JeFg_Pw4)EA@Po?Kvj{Qv#ZJ$=`}wh&V;8)R#% zJZ%*%jm3NVhf;&x(ZvgYbKs6uBgYrdkmkS7|5s^CD%F)tM4P(PslG&YY+&Fw%D*!d z#)jctT}7j6)CUwsN5{YZ&r8OJ5~`6UDz?~hZDW__OHF@x*Y3ZoZ(+P)qxMfMJN!4b zn^AZ~B@pe|hBTxGh8oZ%hU~dDOB-5!>Ujw=VTdLxLEe#0IY`_>M`pW!L zv?!-j=t5HY97U5dIjwP@M%Bvt0sR%Lh!tU}Msd}Her0+JgVKvC2^OkEH;PcrDST>1 z>C~LUrY0m)GX@ux<{B8US^UL_p8;QnqqqbSS7l8+l}^O_hZ4qV z@}9nuz<~~5k)C;HUA}x8XKf#KmLoplo8$1sGt_a1FG$Z+)Kb2U#Qt%JRUW8n9&Vd} ztGBH>_))S5xxL-xz`8?K&AZ!roA+p?{-vxHYyFX#M++)YwtgJ)E3Yqt+u63g@<4CR zv6+ijBGTQqz4x5vJ*(PQa;FhnI*WY;ke$Qlad||-_Hnt1uqG_@C9Lsi_R)--iSqGt z+{R}eK4<)+Ul9>*!)AxiqQLF_Yr1dA!f4c2u~ES zTNaq(|AUr@M6R#Q%Y{;C3G`y1)mK-DOMPXU5SQ^OKFKG1EDP};=VN@-;o}+n#|-|X zS@;g0(xSPi2%og}Iea2((bq9TZ}asI3jGRh;UnDajzJ3}By@U7gZrpszFNw`2i0ye)&i!CvX`)~v+{_aQGf7*DIZ K7o8Xx+4w*Cju{XD diff --git a/plugin/applescript/search-for-vmp.scpt b/plugin/applescript/search-for-vmp.scpt deleted file mode 100644 index a57ddedd62b9923711a0f8b396d081cebd190a55..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1720 zcmb_cOK)3M5dQA9Q#aQ>cASJHq;YCWUwPP0s=~r4h$sz>NLovjNVH+&xK`}?aUXE0s!|JmrFoPjZRz_Fnm&f_+*^xODq_R2&v<4$cg{E8ojLEP zt0yLhk46XMg9Ca80eC|qFkr%CP=TWEDbfUvz$?$?nSvK)injJNV2h$K3bed944(-f zgSVnlQFuOf+#kS_r|>ISZeSZm5ik*8ph8elm=y-L;dO-2q$rG%W4Fr_k34~)&;-M# zi%T~AvVn#H76<5<{n=Fw2gez&3O#3S*FV3<-H?$wPRp$qy%@3SBO&e#GQ~UmiHHYMQi8 zw}b=+5D6jz<0o{7(T#mLg*-~kR6-gR6mSAI=1`_*mUhIn)?cROo;)!PEES zZERQA?xx<4x8%M@?vn&r2Ml?n>1gAQ$~}ds%jpn{e*-%dg*n9>LwFZ&VyB{T2pz%! zx$Bp^bwU+(y6F$#9l7I?JDU6*|B!#%FSqNlrrZPk_(O zp|HoLW}L0JS7EP`*Sp2 zYi1=!Hy4t$XKP91h;+J=or`7C)j|>@loi`9B{AdF63aNXB;lT!#4Ab+vfaWs#1XJD zgCg5MiV-$`g?5QnoI^6pPly8*;5$wu!|^F|Fl*4zB{T^a zOyU(-T3{rgSu^Ch7SG^ZE(kTj!<+9KdeJ9q1{KoR%Vu4b6-!pp5l?4N)oi}9@~6cj z@ELjh3cXx@S#esK7gZ2_@HNRF=$EUq%wdnql1201A6C4foNHBIvk4Ok-A=M-%Ay8$ zRZq*ZMiQg4AXogdP#+t=Tya#6WD^-|M32nNWw|65<$|1--x0B7{>3O+a+zAe&J-=V zq%F2<&o zmWu&+3%PATVf1XO9lhVJZ1x)8ulBF`BJiCI9fK#A`lZl4dUh&xOjfk`HB|^rhCH%{ QhIBPkVSffT=I4L>8{nvbGynhq diff --git a/plugin/vim-markdown-preview.vim b/plugin/vim-markdown-preview.vim index 082c69f..b23af95 100644 --- a/plugin/vim-markdown-preview.vim +++ b/plugin/vim-markdown-preview.vim @@ -1,180 +1,200 @@ -"============================================================ -" Vim Markdown Preview -" git@github.com:JamshedVesuna/vim-markdown-preview.git -"============================================================ -let g:vmp_script_path = resolve(expand(':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='' 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='' +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 ' . g:vim_markdown_preview_hotkey . ' :call Vim_Markdown_Preview()' -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 ' . g:vim_markdown_preview_hotkey . ' :call Vim_Markdown_Preview_Local()' -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 ' . g:vmp_hotkey . ' :call Vim_Markdown_Preview()' endif + -- 2.45.2