1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
" 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