gzip.vim 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. " Vim autoload file for editing compressed files.
  2. " Maintainer: Bram Moolenaar <Bram@vim.org>
  3. " Last Change: 2016 Sep 28
  4. " These functions are used by the gzip plugin.
  5. " Function to check that executing "cmd [-f]" works.
  6. " The result is cached in s:have_"cmd" for speed.
  7. fun s:check(cmd)
  8. let name = substitute(a:cmd, '\(\S*\).*', '\1', '')
  9. if !exists("s:have_" . name)
  10. let e = executable(name)
  11. if e < 0
  12. let r = system(name . " --version")
  13. let e = (r !~ "not found" && r != "")
  14. endif
  15. exe "let s:have_" . name . "=" . e
  16. endif
  17. exe "return s:have_" . name
  18. endfun
  19. " Set b:gzip_comp_arg to the gzip argument to be used for compression, based on
  20. " the flags in the compressed file.
  21. " The only compression methods that can be detected are max speed (-1) and max
  22. " compression (-9).
  23. fun s:set_compression(line)
  24. " get the Compression Method
  25. let l:cm = char2nr(a:line[2])
  26. " if it's 8 (DEFLATE), we can check for the compression level
  27. if l:cm == 8
  28. " get the eXtra FLags
  29. let l:xfl = char2nr(a:line[8])
  30. " max compression
  31. if l:xfl == 2
  32. let b:gzip_comp_arg = "-9"
  33. " min compression
  34. elseif l:xfl == 4
  35. let b:gzip_comp_arg = "-1"
  36. endif
  37. endif
  38. endfun
  39. " After reading compressed file: Uncompress text in buffer with "cmd"
  40. fun gzip#read(cmd)
  41. " don't do anything if the cmd is not supported
  42. if !s:check(a:cmd)
  43. return
  44. endif
  45. " for gzip check current compression level and set b:gzip_comp_arg.
  46. silent! unlet b:gzip_comp_arg
  47. if a:cmd[0] == 'g'
  48. call s:set_compression(getline(1))
  49. endif
  50. " make 'patchmode' empty, we don't want a copy of the written file
  51. let pm_save = &pm
  52. set pm=
  53. " remove 'a' and 'A' from 'cpo' to avoid the alternate file changes
  54. let cpo_save = &cpo
  55. set cpo-=a cpo-=A
  56. " set 'modifiable'
  57. let ma_save = &ma
  58. setlocal ma
  59. " set 'write'
  60. let write_save = &write
  61. set write
  62. " Reset 'foldenable', otherwise line numbers get adjusted.
  63. if has("folding")
  64. let fen_save = &fen
  65. setlocal nofen
  66. endif
  67. " when filtering the whole buffer, it will become empty
  68. let empty = line("'[") == 1 && line("']") == line("$")
  69. let tmp = tempname()
  70. let tmpe = tmp . "." . expand("<afile>:e")
  71. if exists('*fnameescape')
  72. let tmp_esc = fnameescape(tmp)
  73. let tmpe_esc = fnameescape(tmpe)
  74. else
  75. let tmp_esc = escape(tmp, ' ')
  76. let tmpe_esc = escape(tmpe, ' ')
  77. endif
  78. " write the just read lines to a temp file "'[,']w tmp.gz"
  79. execute "silent '[,']w " . tmpe_esc
  80. " uncompress the temp file: call system("gzip -dn tmp.gz")
  81. call system(a:cmd . " " . s:escape(tmpe))
  82. if !filereadable(tmp)
  83. " uncompress didn't work! Keep the compressed file then.
  84. echoerr "Error: Could not read uncompressed file"
  85. let ok = 0
  86. else
  87. let ok = 1
  88. " delete the compressed lines; remember the line number
  89. let l = line("'[") - 1
  90. if exists(":lockmarks")
  91. lockmarks '[,']d _
  92. else
  93. '[,']d _
  94. endif
  95. " read in the uncompressed lines "'[-1r tmp"
  96. " Use ++edit if the buffer was empty, keep the 'ff' and 'fenc' options.
  97. setlocal nobin
  98. if exists(":lockmarks")
  99. if empty
  100. execute "silent lockmarks " . l . "r ++edit " . tmp_esc
  101. else
  102. execute "silent lockmarks " . l . "r " . tmp_esc
  103. endif
  104. else
  105. execute "silent " . l . "r " . tmp_esc
  106. endif
  107. " if buffer became empty, delete trailing blank line
  108. if empty
  109. silent $delete _
  110. 1
  111. endif
  112. " delete the temp file and the used buffers
  113. call delete(tmp)
  114. silent! exe "bwipe " . tmp_esc
  115. silent! exe "bwipe " . tmpe_esc
  116. endif
  117. " Store the OK flag, so that we can use it when writing.
  118. let b:uncompressOk = ok
  119. " Restore saved option values.
  120. let &pm = pm_save
  121. let &cpo = cpo_save
  122. let &l:ma = ma_save
  123. let &write = write_save
  124. if has("folding")
  125. let &l:fen = fen_save
  126. endif
  127. " When uncompressed the whole buffer, do autocommands
  128. if ok && empty
  129. if exists('*fnameescape')
  130. let fname = fnameescape(expand("%:r"))
  131. else
  132. let fname = escape(expand("%:r"), " \t\n*?[{`$\\%#'\"|!<")
  133. endif
  134. if &verbose >= 8
  135. execute "doau BufReadPost " . fname
  136. else
  137. execute "silent! doau BufReadPost " . fname
  138. endif
  139. endif
  140. endfun
  141. " After writing compressed file: Compress written file with "cmd"
  142. fun gzip#write(cmd)
  143. if exists('b:uncompressOk') && !b:uncompressOk
  144. echomsg "Not compressing file because uncompress failed; reset b:uncompressOk to compress anyway"
  145. " don't do anything if the cmd is not supported
  146. elseif s:check(a:cmd)
  147. " Rename the file before compressing it.
  148. let nm = resolve(expand("<afile>"))
  149. let nmt = s:tempname(nm)
  150. if rename(nm, nmt) == 0
  151. if exists("b:gzip_comp_arg")
  152. call system(a:cmd . " " . b:gzip_comp_arg . " -- " . s:escape(nmt))
  153. else
  154. call system(a:cmd . " -- " . s:escape(nmt))
  155. endif
  156. call rename(nmt . "." . expand("<afile>:e"), nm)
  157. endif
  158. endif
  159. endfun
  160. " Before appending to compressed file: Uncompress file with "cmd"
  161. fun gzip#appre(cmd)
  162. " don't do anything if the cmd is not supported
  163. if s:check(a:cmd)
  164. let nm = expand("<afile>")
  165. " for gzip check current compression level and set b:gzip_comp_arg.
  166. silent! unlet b:gzip_comp_arg
  167. if a:cmd[0] == 'g'
  168. call s:set_compression(readfile(nm, "b", 1)[0])
  169. endif
  170. " Rename to a weird name to avoid the risk of overwriting another file
  171. let nmt = expand("<afile>:p:h") . "/X~=@l9q5"
  172. let nmte = nmt . "." . expand("<afile>:e")
  173. if rename(nm, nmte) == 0
  174. if &patchmode != "" && getfsize(nm . &patchmode) == -1
  175. " Create patchmode file by creating the decompressed file new
  176. call system(a:cmd . " -c -- " . s:escape(nmte) . " > " . s:escape(nmt))
  177. call rename(nmte, nm . &patchmode)
  178. else
  179. call system(a:cmd . " -- " . s:escape(nmte))
  180. endif
  181. call rename(nmt, nm)
  182. endif
  183. endif
  184. endfun
  185. " find a file name for the file to be compressed. Use "name" without an
  186. " extension if possible. Otherwise use a weird name to avoid overwriting an
  187. " existing file.
  188. fun s:tempname(name)
  189. let fn = fnamemodify(a:name, ":r")
  190. if !filereadable(fn) && !isdirectory(fn)
  191. return fn
  192. endif
  193. return fnamemodify(a:name, ":p:h") . "/X~=@l9q5"
  194. endfun
  195. fun s:escape(name)
  196. " shellescape() was added by patch 7.0.111
  197. if exists("*shellescape")
  198. return shellescape(a:name)
  199. endif
  200. return "'" . a:name . "'"
  201. endfun
  202. " vim: set sw=2 :