netrwFileHandlers.vim 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. " netrwFileHandlers: contains various extension-based file handlers for
  2. " netrw's browsers' x command ("eXecute launcher")
  3. " Author: Charles E. Campbell
  4. " Date: Sep 18, 2020
  5. " Version: 11
  6. " Copyright: Copyright (C) 1999-2012 Charles E. Campbell {{{1
  7. " Permission is hereby granted to use and distribute this code,
  8. " with or without modifications, provided that this copyright
  9. " notice is copied with it. Like anything else that's free,
  10. " netrwFileHandlers.vim is provided *as is* and comes with no
  11. " warranty of any kind, either expressed or implied. In no
  12. " event will the copyright holder be liable for any damages
  13. " resulting from the use of this software.
  14. "
  15. " Rom 6:23 (WEB) For the wages of sin is death, but the free gift of God {{{1
  16. " is eternal life in Christ Jesus our Lord.
  17. " ---------------------------------------------------------------------
  18. " Load Once: {{{1
  19. if exists("g:loaded_netrwFileHandlers") || &cp
  20. finish
  21. endif
  22. let g:loaded_netrwFileHandlers= "v11"
  23. if v:version < 702
  24. echohl WarningMsg
  25. echo "***warning*** this version of netrwFileHandlers needs vim 7.2"
  26. echohl Normal
  27. finish
  28. endif
  29. let s:keepcpo= &cpo
  30. set cpo&vim
  31. " ---------------------------------------------------------------------
  32. " netrwFileHandlers#Invoke: {{{1
  33. fun! netrwFileHandlers#Invoke(exten,fname)
  34. " call Dfunc("netrwFileHandlers#Invoke(exten<".a:exten."> fname<".a:fname.">)")
  35. let exten= a:exten
  36. " list of supported special characters. Consider rcs,v --- that can be
  37. " supported with a NFH_rcsCOMMAv() handler
  38. if exten =~ '[@:,$!=\-+%?;~]'
  39. let specials= {
  40. \ '@' : 'AT',
  41. \ ':' : 'COLON',
  42. \ ',' : 'COMMA',
  43. \ '$' : 'DOLLAR',
  44. \ '!' : 'EXCLAMATION',
  45. \ '=' : 'EQUAL',
  46. \ '-' : 'MINUS',
  47. \ '+' : 'PLUS',
  48. \ '%' : 'PERCENT',
  49. \ '?' : 'QUESTION',
  50. \ ';' : 'SEMICOLON',
  51. \ '~' : 'TILDE'}
  52. let exten= substitute(a:exten,'[@:,$!=\-+%?;~]','\=specials[submatch(0)]','ge')
  53. " call Decho('fname<'.fname.'> done with dictionary')
  54. endif
  55. if a:exten != "" && exists("*NFH_".exten)
  56. " support user NFH_*() functions
  57. " call Decho("let ret= netrwFileHandlers#NFH_".a:exten.'("'.fname.'")')
  58. exe "let ret= NFH_".exten.'("'.a:fname.'")'
  59. elseif a:exten != "" && exists("*s:NFH_".exten)
  60. " use builtin-NFH_*() functions
  61. " call Decho("let ret= netrwFileHandlers#NFH_".a:exten.'("'.fname.'")')
  62. exe "let ret= s:NFH_".a:exten.'("'.a:fname.'")'
  63. endif
  64. " call Dret("netrwFileHandlers#Invoke 0 : ret=".ret)
  65. return 0
  66. endfun
  67. " ---------------------------------------------------------------------
  68. " s:NFH_html: handles html when the user hits "x" when the {{{1
  69. " cursor is atop a *.html file
  70. fun! s:NFH_html(pagefile)
  71. " call Dfunc("s:NFH_html(".a:pagefile.")")
  72. let page= substitute(a:pagefile,'^','file://','')
  73. if executable("mozilla")
  74. " call Decho("executing !mozilla ".page)
  75. exe "!mozilla ".shellescape(page,1)
  76. elseif executable("netscape")
  77. " call Decho("executing !netscape ".page)
  78. exe "!netscape ".shellescape(page,1)
  79. else
  80. " call Dret("s:NFH_html 0")
  81. return 0
  82. endif
  83. " call Dret("s:NFH_html 1")
  84. return 1
  85. endfun
  86. " ---------------------------------------------------------------------
  87. " s:NFH_htm: handles html when the user hits "x" when the {{{1
  88. " cursor is atop a *.htm file
  89. fun! s:NFH_htm(pagefile)
  90. " call Dfunc("s:NFH_htm(".a:pagefile.")")
  91. let page= substitute(a:pagefile,'^','file://','')
  92. if executable("mozilla")
  93. " call Decho("executing !mozilla ".page)
  94. exe "!mozilla ".shellescape(page,1)
  95. elseif executable("netscape")
  96. " call Decho("executing !netscape ".page)
  97. exe "!netscape ".shellescape(page,1)
  98. else
  99. " call Dret("s:NFH_htm 0")
  100. return 0
  101. endif
  102. " call Dret("s:NFH_htm 1")
  103. return 1
  104. endfun
  105. " ---------------------------------------------------------------------
  106. " s:NFH_jpg: {{{1
  107. fun! s:NFH_jpg(jpgfile)
  108. " call Dfunc("s:NFH_jpg(jpgfile<".a:jpgfile.">)")
  109. if executable("gimp")
  110. exe "silent! !gimp -s ".shellescape(a:jpgfile,1)
  111. elseif executable(expand("$SystemRoot")."/SYSTEM32/MSPAINT.EXE")
  112. " call Decho("silent! !".expand("$SystemRoot")."/SYSTEM32/MSPAINT ".escape(a:jpgfile," []|'"))
  113. exe "!".expand("$SystemRoot")."/SYSTEM32/MSPAINT ".shellescape(a:jpgfile,1)
  114. else
  115. " call Dret("s:NFH_jpg 0")
  116. return 0
  117. endif
  118. " call Dret("s:NFH_jpg 1")
  119. return 1
  120. endfun
  121. " ---------------------------------------------------------------------
  122. " s:NFH_gif: {{{1
  123. fun! s:NFH_gif(giffile)
  124. " call Dfunc("s:NFH_gif(giffile<".a:giffile.">)")
  125. if executable("gimp")
  126. exe "silent! !gimp -s ".shellescape(a:giffile,1)
  127. elseif executable(expand("$SystemRoot")."/SYSTEM32/MSPAINT.EXE")
  128. exe "silent! !".expand("$SystemRoot")."/SYSTEM32/MSPAINT ".shellescape(a:giffile,1)
  129. else
  130. " call Dret("s:NFH_gif 0")
  131. return 0
  132. endif
  133. " call Dret("s:NFH_gif 1")
  134. return 1
  135. endfun
  136. " ---------------------------------------------------------------------
  137. " s:NFH_png: {{{1
  138. fun! s:NFH_png(pngfile)
  139. " call Dfunc("s:NFH_png(pngfile<".a:pngfile.">)")
  140. if executable("gimp")
  141. exe "silent! !gimp -s ".shellescape(a:pngfile,1)
  142. elseif executable(expand("$SystemRoot")."/SYSTEM32/MSPAINT.EXE")
  143. exe "silent! !".expand("$SystemRoot")."/SYSTEM32/MSPAINT ".shellescape(a:pngfile,1)
  144. else
  145. " call Dret("s:NFH_png 0")
  146. return 0
  147. endif
  148. " call Dret("s:NFH_png 1")
  149. return 1
  150. endfun
  151. " ---------------------------------------------------------------------
  152. " s:NFH_pnm: {{{1
  153. fun! s:NFH_pnm(pnmfile)
  154. " call Dfunc("s:NFH_pnm(pnmfile<".a:pnmfile.">)")
  155. if executable("gimp")
  156. exe "silent! !gimp -s ".shellescape(a:pnmfile,1)
  157. elseif executable(expand("$SystemRoot")."/SYSTEM32/MSPAINT.EXE")
  158. exe "silent! !".expand("$SystemRoot")."/SYSTEM32/MSPAINT ".shellescape(a:pnmfile,1)
  159. else
  160. " call Dret("s:NFH_pnm 0")
  161. return 0
  162. endif
  163. " call Dret("s:NFH_pnm 1")
  164. return 1
  165. endfun
  166. " ---------------------------------------------------------------------
  167. " s:NFH_bmp: visualize bmp files {{{1
  168. fun! s:NFH_bmp(bmpfile)
  169. " call Dfunc("s:NFH_bmp(bmpfile<".a:bmpfile.">)")
  170. if executable("gimp")
  171. exe "silent! !gimp -s ".a:bmpfile
  172. elseif executable(expand("$SystemRoot")."/SYSTEM32/MSPAINT.EXE")
  173. exe "silent! !".expand("$SystemRoot")."/SYSTEM32/MSPAINT ".shellescape(a:bmpfile,1)
  174. else
  175. " call Dret("s:NFH_bmp 0")
  176. return 0
  177. endif
  178. " call Dret("s:NFH_bmp 1")
  179. return 1
  180. endfun
  181. " ---------------------------------------------------------------------
  182. " s:NFH_pdf: visualize pdf files {{{1
  183. fun! s:NFH_pdf(pdf)
  184. " call Dfunc("s:NFH_pdf(pdf<".a:pdf.">)")
  185. if executable("gs")
  186. exe 'silent! !gs '.shellescape(a:pdf,1)
  187. elseif executable("pdftotext")
  188. exe 'silent! pdftotext -nopgbrk '.shellescape(a:pdf,1)
  189. else
  190. " call Dret("s:NFH_pdf 0")
  191. return 0
  192. endif
  193. " call Dret("s:NFH_pdf 1")
  194. return 1
  195. endfun
  196. " ---------------------------------------------------------------------
  197. " s:NFH_doc: visualize doc files {{{1
  198. fun! s:NFH_doc(doc)
  199. " call Dfunc("s:NFH_doc(doc<".a:doc.">)")
  200. if executable("oowriter")
  201. exe 'silent! !oowriter '.shellescape(a:doc,1)
  202. redraw!
  203. else
  204. " call Dret("s:NFH_doc 0")
  205. return 0
  206. endif
  207. " call Dret("s:NFH_doc 1")
  208. return 1
  209. endfun
  210. " ---------------------------------------------------------------------
  211. " s:NFH_sxw: visualize sxw files {{{1
  212. fun! s:NFH_sxw(sxw)
  213. " call Dfunc("s:NFH_sxw(sxw<".a:sxw.">)")
  214. if executable("oowriter")
  215. exe 'silent! !oowriter '.shellescape(a:sxw,1)
  216. redraw!
  217. else
  218. " call Dret("s:NFH_sxw 0")
  219. return 0
  220. endif
  221. " call Dret("s:NFH_sxw 1")
  222. return 1
  223. endfun
  224. " ---------------------------------------------------------------------
  225. " s:NFH_xls: visualize xls files {{{1
  226. fun! s:NFH_xls(xls)
  227. " call Dfunc("s:NFH_xls(xls<".a:xls.">)")
  228. if executable("oocalc")
  229. exe 'silent! !oocalc '.shellescape(a:xls,1)
  230. redraw!
  231. else
  232. " call Dret("s:NFH_xls 0")
  233. return 0
  234. endif
  235. " call Dret("s:NFH_xls 1")
  236. return 1
  237. endfun
  238. " ---------------------------------------------------------------------
  239. " s:NFH_ps: handles PostScript files {{{1
  240. fun! s:NFH_ps(ps)
  241. " call Dfunc("s:NFH_ps(ps<".a:ps.">)")
  242. if executable("gs")
  243. " call Decho("exe silent! !gs ".a:ps)
  244. exe "silent! !gs ".shellescape(a:ps,1)
  245. redraw!
  246. elseif executable("ghostscript")
  247. " call Decho("exe silent! !ghostscript ".a:ps)
  248. exe "silent! !ghostscript ".shellescape(a:ps,1)
  249. redraw!
  250. elseif executable("gswin32")
  251. " call Decho("exe silent! !gswin32 ".shellescape(a:ps,1))
  252. exe "silent! !gswin32 ".shellescape(a:ps,1)
  253. redraw!
  254. else
  255. " call Dret("s:NFH_ps 0")
  256. return 0
  257. endif
  258. " call Dret("s:NFH_ps 1")
  259. return 1
  260. endfun
  261. " ---------------------------------------------------------------------
  262. " s:NFH_eps: handles encapsulated PostScript files {{{1
  263. fun! s:NFH_eps(eps)
  264. " call Dfunc("s:NFH_eps()")
  265. if executable("gs")
  266. exe "silent! !gs ".shellescape(a:eps,1)
  267. redraw!
  268. elseif executable("ghostscript")
  269. exe "silent! !ghostscript ".shellescape(a:eps,1)
  270. redraw!
  271. elseif executable("ghostscript")
  272. exe "silent! !ghostscript ".shellescape(a:eps,1)
  273. redraw!
  274. elseif executable("gswin32")
  275. exe "silent! !gswin32 ".shellescape(a:eps,1)
  276. redraw!
  277. else
  278. " call Dret("s:NFH_eps 0")
  279. return 0
  280. endif
  281. " call Dret("s:NFH_eps 0")
  282. return 1
  283. endfun
  284. " ---------------------------------------------------------------------
  285. " s:NFH_fig: handles xfig files {{{1
  286. fun! s:NFH_fig(fig)
  287. " call Dfunc("s:NFH_fig()")
  288. if executable("xfig")
  289. exe "silent! !xfig ".a:fig
  290. redraw!
  291. else
  292. " call Dret("s:NFH_fig 0")
  293. return 0
  294. endif
  295. " call Dret("s:NFH_fig 1")
  296. return 1
  297. endfun
  298. " ---------------------------------------------------------------------
  299. " s:NFH_obj: handles tgif's obj files {{{1
  300. fun! s:NFH_obj(obj)
  301. " call Dfunc("s:NFH_obj()")
  302. if has("unix") && executable("tgif")
  303. exe "silent! !tgif ".a:obj
  304. redraw!
  305. else
  306. " call Dret("s:NFH_obj 0")
  307. return 0
  308. endif
  309. " call Dret("s:NFH_obj 1")
  310. return 1
  311. endfun
  312. let &cpo= s:keepcpo
  313. unlet s:keepcpo
  314. " ---------------------------------------------------------------------
  315. " Modelines: {{{1
  316. " vim: fdm=marker