annotate runtime/autoload/gzip.vim @ 446:7472c565592a v7.0117

updated for version 7.0117
author vimboss
date Wed, 27 Jul 2005 21:13:01 +0000
parents
children 23f82b5d2814
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
446
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
1 " Vim autoload file for editing compressed files.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
2 " Maintainer: Bram Moolenaar <Bram@vim.org>
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
3 " Last Change: 2005 Jul 26
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
4
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
5 " These functions are used by the gzip plugin.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
6
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
7 " Function to check that executing "cmd [-f]" works.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
8 " The result is cached in s:have_"cmd" for speed.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
9 fun s:check(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
10 let name = substitute(a:cmd, '\(\S*\).*', '\1', '')
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
11 if !exists("s:have_" . name)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
12 let e = executable(name)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
13 if e < 0
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
14 let r = system(name . " --version")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
15 let e = (r !~ "not found" && r != "")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
16 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
17 exe "let s:have_" . name . "=" . e
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
18 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
19 exe "return s:have_" . name
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
20 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
21
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
22 " Set b:gzip_comp_arg to the gzip argument to be used for compression, based on
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
23 " the flags in the compressed file.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
24 " The only compression methods that can be detected are max speed (-1) and max
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
25 " compression (-9).
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
26 fun s:set_compression(line)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
27 " get the Compression Method
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
28 let l:cm = char2nr(a:line[2])
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
29 " if it's 8 (DEFLATE), we can check for the compression level
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
30 if l:cm == 8
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
31 " get the eXtra FLags
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
32 let l:xfl = char2nr(a:line[8])
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
33 " max compression
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
34 if l:xfl == 2
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
35 let b:gzip_comp_arg = "-9"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
36 " min compression
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
37 elseif l:xfl == 4
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
38 let b:gzip_comp_arg = "-1"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
39 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
40 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
41 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
42
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
43
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
44 " After reading compressed file: Uncompress text in buffer with "cmd"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
45 fun gzip#read(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
46 " don't do anything if the cmd is not supported
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
47 if !s:check(a:cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
48 return
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
49 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
50
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
51 " for gzip check current compression level and set b:gzip_comp_arg.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
52 silent! unlet b:gzip_comp_arg
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
53 if a:cmd[0] == 'g'
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
54 call s:set_compression(getline(1))
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
55 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
56
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
57 " make 'patchmode' empty, we don't want a copy of the written file
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
58 let pm_save = &pm
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
59 set pm=
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
60 " remove 'a' and 'A' from 'cpo' to avoid the alternate file changes
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
61 let cpo_save = &cpo
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
62 set cpo-=a cpo-=A
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
63 " set 'modifiable'
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
64 let ma_save = &ma
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
65 setlocal ma
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
66 " when filtering the whole buffer, it will become empty
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
67 let empty = line("'[") == 1 && line("']") == line("$")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
68 let tmp = tempname()
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
69 let tmpe = tmp . "." . expand("<afile>:e")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
70 " write the just read lines to a temp file "'[,']w tmp.gz"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
71 execute "silent '[,']w " . tmpe
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
72 " uncompress the temp file: call system("gzip -dn tmp.gz")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
73 call system(a:cmd . " " . tmpe)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
74 if !filereadable(tmp)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
75 " uncompress didn't work! Keep the compressed file then.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
76 echoerr "Error: Could not read uncompressed file"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
77 return
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
78 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
79 " delete the compressed lines; remember the line number
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
80 let l = line("'[") - 1
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
81 if exists(":lockmarks")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
82 lockmarks '[,']d _
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
83 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
84 '[,']d _
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
85 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
86 " read in the uncompressed lines "'[-1r tmp"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
87 setlocal nobin
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
88 if exists(":lockmarks")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
89 execute "silent lockmarks " . l . "r " . tmp
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
90 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
91 execute "silent " . l . "r " . tmp
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
92 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
93
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
94 " if buffer became empty, delete trailing blank line
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
95 if empty
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
96 silent $delete _
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
97 1
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
98 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
99 " delete the temp file and the used buffers
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
100 call delete(tmp)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
101 silent! exe "bwipe " . tmp
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
102 silent! exe "bwipe " . tmpe
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
103 let &pm = pm_save
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
104 let &cpo = cpo_save
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
105 let &l:ma = ma_save
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
106 " When uncompressed the whole buffer, do autocommands
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
107 if empty
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
108 if &verbose >= 8
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
109 execute "doau BufReadPost " . expand("%:r")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
110 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
111 execute "silent! doau BufReadPost " . expand("%:r")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
112 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
113 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
114 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
115
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
116 " After writing compressed file: Compress written file with "cmd"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
117 fun gzip#write(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
118 " don't do anything if the cmd is not supported
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
119 if s:check(a:cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
120 " Rename the file before compressing it.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
121 let nm = resolve(expand("<afile>"))
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
122 let nmt = s:tempname(nm)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
123 if rename(nm, nmt) == 0
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
124 if exists("b:gzip_comp_arg")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
125 call system(a:cmd . " " . b:gzip_comp_arg . " " . nmt)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
126 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
127 call system(a:cmd . " " . nmt)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
128 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
129 call rename(nmt . "." . expand("<afile>:e"), nm)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
130 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
131 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
132 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
133
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
134 " Before appending to compressed file: Uncompress file with "cmd"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
135 fun gzip#appre(cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
136 " don't do anything if the cmd is not supported
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
137 if s:check(a:cmd)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
138 let nm = expand("<afile>")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
139
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
140 " for gzip check current compression level and set b:gzip_comp_arg.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
141 silent! unlet b:gzip_comp_arg
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
142 if a:cmd[0] == 'g'
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
143 call s:set_compression(readfile(nm, "b", 1)[0])
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
144 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
145
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
146 " Rename to a weird name to avoid the risk of overwriting another file
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
147 let nmt = expand("<afile>:p:h") . "/X~=@l9q5"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
148 let nmte = nmt . "." . expand("<afile>:e")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
149 if rename(nm, nmte) == 0
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
150 if &patchmode != "" && getfsize(nm . &patchmode) == -1
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
151 " Create patchmode file by creating the decompressed file new
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
152 call system(a:cmd . " -c " . nmte . " > " . nmt)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
153 call rename(nmte, nm . &patchmode)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
154 else
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
155 call system(a:cmd . " " . nmte)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
156 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
157 call rename(nmt, nm)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
158 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
159 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
160 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
161
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
162 " find a file name for the file to be compressed. Use "name" without an
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
163 " extension if possible. Otherwise use a weird name to avoid overwriting an
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
164 " existing file.
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
165 fun s:tempname(name)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
166 let fn = fnamemodify(a:name, ":r")
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
167 if !filereadable(fn) && !isdirectory(fn)
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
168 return fn
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
169 endif
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
170 return fnamemodify(a:name, ":p:h") . "/X~=@l9q5"
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
171 endfun
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
172
7472c565592a updated for version 7.0117
vimboss
parents:
diff changeset
173 " vim: set sw=2 :