/
viewinhtml.vim
161 lines (144 loc) · 4.61 KB
/
viewinhtml.vim
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
" vim plugin file
" Filename: viewinhtml.vim
" Maintainer: janus_wel <janus.wel.3@gmail.com>
" Last Change: 2009/12/28 03:13:20.
" Version: 0.11
" Dependency:
" This plugin needs following files
"
" * autoload/shell.vim
" http://github.com/januswel/dotfiles/blob/master/vimfiles/autoload/shell.vim
"
" Remark: {{{1
" This plugin provides the command and mappings to show contents of the
" current buffer in your favorite http user agent (web browser).
"
" *:ViewInHtml*
" :{range}ViewInHtml
" Show contents of the current buffer in your favorite
" http user agent (web browser). {range} is line number
" to show. When {range} is not specified, target is all
" of contents.
"
" *<Plug>ViewInHtml*
" <Plug>ViewInHtml
" An internal mapping in normal mode. Same as
" |:ViewInHtml| but target is the all of contents.
"
" <Leader>vh
" When there is no mappings with |<Plug>ViewInHtml|, this
" will be mapped to <Plug>ViewInHtml, .
" preparations {{{1
" check if this plugin is already loaded or not
if exists('loaded_viewinhtml')
finish
endif
let loaded_viewinhtml = 1
" check vim has required features
if !(has('win32') && has('multi_byte'))
finish
endif
" reset the value of 'cpoptions' for portability
let s:save_cpoptions = &cpoptions
set cpoptions&vim
" main {{{1
" commands {{{2
if exists(':ViewInHtml') != 2
command -nargs=0 -range=% ViewInHtml
\ <line1>,<line2>call s:ViewInHtmlSS()
endif
" mappings {{{2
if !(exists('no_plugin_maps') && no_plugin_maps)
\ && !(exists('no_viewinhtml_maps') && no_viewinhtml_maps)
if !hasmapto('<Plug>ViewInHtml', 'n')
nmap <unique><Leader>vh <Plug>ViewInHtml
endif
endif
nnoremap <silent><Plug>ViewInHtml
\ :%call <SID>ViewInHtml()<CR>
" constants {{{2
" g:html_font
" set your favorite font
" g:html_ignore_folding
" expand fold and convert
" g:html_no_pre
" use br elements instead of pre elements
" g:html_number_lines
" if this variable is undefined, the value of 'number' is used.
" force to display line number
" g:html_use_encoding
" set the same encoding as 'fileencoding'
let s:defaults = {
\ 'g:html_font': '"VL Gothic"',
\ 'g:html_ignore_folding': 1,
\ 'g:html_no_pre': 1,
\ 'g:html_number_lines': 1,
\ 'g:html_use_css': 1,
\ 'g:html_use_encoding': '&encoding',
\ 'g:use_xhtml': 1,
\ }
lockvar s:defaults
" functions {{{2
" main function
" wrapper function
function! s:ViewInHtml() range
if exists(':TOhtml') != 2
throw 'command ":TOhtml" is required'
endif
let save_shellslash = &shellslash
if shell#GetType() ==# 'cmd'
set noshellslash
else
set shellslash
endif
try
" setup
let unletvars = s:SetupTOhtmlVariables(s:defaults)
" generate html
execute printf('%d,%dTOhtml', a:firstline, a:lastline)
" clean up
call s:CleanupTOhtmlVariables(unletvars)
" if generating html is succeeded, current window will be the one that
" has generated html.
if &filetype =~? 'html'
" change file path and write it
let tempfile = tempname() . '.html'
silent execute 'saveas!' tempfile
" browse html with default UA
let filename = iconv(tempfile, &encoding, shell#GetEncoding())
let filename = shellescape(filename)
silent call system(filename)
" delete the buffer and comb out it from buffer list
bdelete
endif
catch
echoerr v:exception
finally
let &shellslash = save_shellslash
endtry
endfunction
" settings for :TOhtml
function! s:SetupTOhtmlVariables(defaults)
let unletvars = []
for name in keys(a:defaults)
if !exists(name)
call add(unletvars, name)
execute 'let' name '=' a:defaults[name]
endif
endfor
return unletvars
endfunction
" unlet variables that are defined by this script
function! s:CleanupTOhtmlVariables(vars)
for var in a:vars
if exists(var)
execute 'unlet' var
endif
endfor
endfunction
" post-processings {{{1
" restore the value of 'cpoptions'
let &cpoptions = s:save_cpoptions
unlet s:save_cpoptions
" }}}1
" vim: ts=4 sw=4 sts=0 et fdm=marker fdc=3