~dricottone/vim-markdown-preview

ref: f134df6fcb557f9fc64cd2a0254b69d40bbe78d3 vim-markdown-preview/plugin/vim-markdown-preview.vim -rw-r--r-- 6.6 KiB
f134df6f — Thomas Hodgson Added Pandoc to the local preview function 7 years ago
                                                                                
906c7a8f Jamshed Vesuna
b40666c8 Wade Simmons
c56e9191 JamshedVesuna
6372ce63 Torben
4f21c0a1 Torben
24b44d5c Torben
6372ce63 Torben
24b44d5c Torben
4f21c0a1 Torben
1cb74148 JamshedVesuna
6372ce63 Torben
11074115 Torben
1cb74148 JamshedVesuna
a9971e10 JamshedVesuna
2cee512d JamshedVesuna
a992ce6b Thomas Hodgson
956b1542 JamshedVesuna
9088e7d1 JamshedVesuna
b3894e0b JamshedVesuna
b40666c8 Wade Simmons
5ab8e9af Oleksandr Shybystyi
1cb74148 JamshedVesuna
b3894e0b JamshedVesuna
2cee512d JamshedVesuna
a992ce6b Thomas Hodgson
261d88f3 Thomas Hodgson
5ab8e9af Oleksandr Shybystyi
b3894e0b JamshedVesuna
5ab8e9af Oleksandr Shybystyi
74a43a81 Jamshed Vesuna
cf2cc781 JamshedVesuna
b40666c8 Wade Simmons
1cb74148 JamshedVesuna
cf2cc781 JamshedVesuna
956b1542 JamshedVesuna
fbb5df7f ikozlowski
956b1542 JamshedVesuna
fbb5df7f ikozlowski
956b1542 JamshedVesuna
cf2cc781 JamshedVesuna
b40666c8 Wade Simmons
5838d1d4 JamshedVesuna
46cd8769 Ned Konz
5838d1d4 JamshedVesuna
46cd8769 Ned Konz
5838d1d4 JamshedVesuna
c56e9191 JamshedVesuna
6372ce63 Torben
c56e9191 JamshedVesuna
cf2cc781 JamshedVesuna
1cb74148 JamshedVesuna
a9971e10 JamshedVesuna
cf2cc781 JamshedVesuna
2d5e5346 Jamshed Vesuna
906c7a8f Jamshed Vesuna
1f913965 JamshedVesuna
b40666c8 Wade Simmons
1f913965 JamshedVesuna
1cb74148 JamshedVesuna
b3894e0b JamshedVesuna
2cee512d JamshedVesuna
f134df6f Thomas Hodgson
54876840 JamshedVesuna
b3894e0b JamshedVesuna
54876840 JamshedVesuna
74a43a81 Jamshed Vesuna
1f913965 JamshedVesuna
b40666c8 Wade Simmons
68cacb10 JamshedVesuna
1f913965 JamshedVesuna
956b1542 JamshedVesuna
fbb5df7f ikozlowski
956b1542 JamshedVesuna
fbb5df7f ikozlowski
956b1542 JamshedVesuna
1f913965 JamshedVesuna
b40666c8 Wade Simmons
5838d1d4 JamshedVesuna
5b42c021 JamshedVesuna
5838d1d4 JamshedVesuna
5b42c021 JamshedVesuna
5838d1d4 JamshedVesuna
c56e9191 JamshedVesuna
6372ce63 Torben
c56e9191 JamshedVesuna
1f913965 JamshedVesuna
1cb74148 JamshedVesuna
a9971e10 JamshedVesuna
68cacb10 JamshedVesuna
1f913965 JamshedVesuna
1cb74148 JamshedVesuna
9088e7d1 JamshedVesuna
1cb74148 JamshedVesuna
9088e7d1 JamshedVesuna
1135b27a JamshedVesuna
9088e7d1 JamshedVesuna
1cb74148 JamshedVesuna
1135b27a JamshedVesuna
1cb74148 JamshedVesuna
1135b27a JamshedVesuna
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
"============================================================
"                    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'

if has('win32') || has('win64')
  " Not yet used
  let g:vmp_osname = 'win32'
elseif has('unix')
  let s:uname = system("uname")

  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'
  else
    let g:vmp_osname = 'unix'
  endif
endif

if !exists("g:vim_markdown_preview_browser")
  if g:vmp_osname == 'mac'
    let g:vim_markdown_preview_browser = 'Safari'
  else
    let g:vim_markdown_preview_browser = 'Google Chrome'
  endif
endif

if !exists("g:vim_markdown_preview_temp_file")
  let g:vim_markdown_preview_temp_file = 0
endif

if !exists("g:vim_markdown_preview_toggle")
  let g:vim_markdown_preview_toggle = 0
endif

if !exists("g:vim_markdown_preview_github")
  let g:vim_markdown_preview_github = 0
endif

if !exists("g:vim_markdown_preview_perl")
  let g:vim_markdown_preview_perl = 0
endif

if !exists("g:vim_markdown_preview_pandoc")
  let g:vim_markdown_preview_pandoc = 0
endif

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

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

  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

  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

  if g:vim_markdown_preview_temp_file == 1
    sleep 200m
    call system('rm /tmp/vim-markdown-preview.html')
  endif
endfunction


"Renders html locally and displays images
function! Vim_Markdown_Preview_Local()
  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 --smart --standalone "' . b:curr_file . '" > /tmp/vim-markdown-preview.html')
  else
    call system('markdown "' . b:curr_file . '" > vim-markdown-preview.html')
  endif
  if v:shell_error
    echo 'Please install the necessary requirements: https://github.com/JamshedVesuna/vim-markdown-preview#requirements'
  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 &')
      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

  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
  endif

  if g:vim_markdown_preview_temp_file == 1
    sleep 200m
    call system('rm vim-markdown-preview.html')
  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
  autocmd BufWritePost *.markdown,*.md :call Vim_Markdown_Preview()
endif