~dricottone/vim-markdown-preview

ref: 54876840f68eb267e29aef96f535c45b0e8249ab vim-markdown-preview/plugin/vim-markdown-preview.vim -rw-r--r-- 3.7 KiB
54876840 — JamshedVesuna Added GitHub markdown to image preview mapping 9 years ago
                                                                                
906c7a8f Jamshed Vesuna
e6146743 Anton Kozhemyachenko
1135b27a JamshedVesuna
5ab8e9af Oleksandr Shybystyi
a9971e10 JamshedVesuna
906c7a8f Jamshed Vesuna
cf2cc781 JamshedVesuna
a55bbfa2 Eric Siebeneich
5ab8e9af Oleksandr Shybystyi
cf2cc781 JamshedVesuna
a9971e10 JamshedVesuna
cf2cc781 JamshedVesuna
a9971e10 JamshedVesuna
cf2cc781 JamshedVesuna
a9971e10 JamshedVesuna
cf2cc781 JamshedVesuna
1135b27a JamshedVesuna
a9971e10 JamshedVesuna
cf2cc781 JamshedVesuna
2d5e5346 Jamshed Vesuna
906c7a8f Jamshed Vesuna
1f913965 JamshedVesuna
54876840 JamshedVesuna
1f913965 JamshedVesuna
a9971e10 JamshedVesuna
1f913965 JamshedVesuna
a9971e10 JamshedVesuna
1f913965 JamshedVesuna
a9971e10 JamshedVesuna
1f913965 JamshedVesuna
1135b27a JamshedVesuna
a9971e10 JamshedVesuna
1f913965 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
"============================================================
"                    Vim Markdown Preview
"   git@github.com:JamshedVesuna/vim-markdown-preview.git
"============================================================

let b:vim_markdown_preview_browser = get(g:, 'vim_markdown_preview_browser', 'Google Chrome')
let b:vim_markdown_preview_temp_file = get(g:, 'vim_markdown_preview_temp_file', 0)
let b:vim_markdown_preview_toggle = get(g:, 'vim_markdown_preview_toggle', 0)
let b:vim_markdown_preview_github = get(g:, 'vim_markdown_preview_github', 0)

function! Vim_Markdown_Preview()

  let OSNAME = 'Unidentified'

  if has('win32')
    " Not yet used
    let OSNAME = 'win32'
  endif
  if has('unix')
    let OSNAME = 'unix'
  endif
  if has('mac')
    let OSNAME = 'mac'
  endif

  let curr_file = expand('%:p')

  if b:vim_markdown_preview_github == 1
    call system('grip ' . curr_file . ' --export /tmp/vim-markdown-preview.html')
  else
    call system('markdown ' . curr_file . ' > /tmp/vim-markdown-preview.html')
  endif

  if OSNAME == 'unix'
    let chrome_wid = system("xdotool search --name 'vim-markdown-preview.html - " . b:vim_markdown_preview_browser . "'")
    if !chrome_wid
      call system('see /tmp/vim-markdown-preview.html &> /dev/null &')
    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 OSNAME == 'mac'
    call system('open -g /tmp/vim-markdown-preview.html')
  endif

  if b: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 OSNAME = 'Unidentified'

  if has('win32')
    " Not yet used
    let OSNAME = 'win32'
  endif
  if has('unix')
    let OSNAME = 'unix'
  endif
  if has('mac')
    let OSNAME = 'mac'
  endif

  let curr_file = expand('%:p')

  if b:vim_markdown_preview_github == 1
    call system('grip ' . curr_file . ' --export /tmp/vim-markdown-preview.html')
  else
    call system('markdown ' . curr_file . ' > /tmp/vim-markdown-preview.html')
  endif

  if OSNAME == 'unix'
    let chrome_wid = system("xdotool search --name '". curr_file . ".html - " . b:vim_markdown_preview_browser . "'")
    if !chrome_wid
      call system('see ' . curr_file . '.html &> /dev/null &')
    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 OSNAME == 'mac'
    call system('open -g ' . curr_file . '.html')
  endif

  if b:vim_markdown_preview_temp_file == 1
    sleep 200m
    call system('rm ' . curr_file . '.html')
  endif
endfunction

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