add vim conf files
[arrow.git] / conf_slk120 / vim / _vim / plugin / textutil.vim
blob22f19eafffa7a5fc5fd1e4241cdd0dbffa87f5a9
1 " textutil.vim : Vim plugin for editing rtf,rtfd,doc,wordml files.
3 " Name Of File: textutil.vim
4 " Maintainer:   omi taku <advweb@jcom.home.ne.jp>
5 " URL:          http://members.jcom.home.ne.jp/advweb/
6 " Script URL:   http://www.vim.org/scripts/script.php?script_id=1432
7 " Last Change:  2005/12/16
8 " Version:      0.1.1
10 " Installation:
11 "    1. Copy the textutil.vim script to the $HOME/.vim/plugin directory.
12 "       Refer to ':help add-plugin', ':help add-global-plugin' and ':help runtimepath'
13 "       for more details about Vim plugins.
14 "    2. Restart Vim.
16 " Usage:
17 "    When you open rtf, rtfd, doc or wordml file with Vim,
18 "    editing file format is automatically converted to plain text.
19 "    And when you write file, file format is automatically converted to
20 "    rtf, rtfd, doc or wordml file format.
22 " Configuration:
23 "    When this script convert rtf, rtfd, doc or wordml file to plain text with textutil command,
24 "    this script use "g:textutil_txt_encoding" encoding.
25 "    Default value of "g:textutil_txt_encoding" is 'utf-8'.
26 "    You want to change text encoding, set "g:textutil_txt_encoding" in your $HOME/.vimrc file.
27 "    for example,
29 "        :let g:textutil_txt_encoding='Shift_JIS'
31 " Note:
32 "    This script is based on 'textutil' command.
33 "    So this script will only run on MacOS 10.4 or later.
35 " History:
36 "    0.1.1 o Add "inputencoding" textutil command option.
37 "    0.1   o Initial upload.
40 " if plugin is already loaded then, not load plugin.
41 if exists("loaded_textutil") || &cp || exists("#BufReadPre#*.rtf")
42         finish
43 endif
44 let loaded_textutil = 1
46 " configuration
47 if !exists('g:textutil_txt_encoding')
48         let g:textutil_txt_encoding = 'utf-8'
49 endif
51 " set autocmd
52 augroup textutil
53         " Remove all textutil autocommands
54         au!
56         " rtf
57         autocmd BufReadPre,FileReadPre          *.rtf    setlocal bin
58         autocmd BufReadPost,FileReadPost        *.rtf    call s:read(s:read_cmd("txt"))
59         autocmd BufWritePost,FileWritePost      *.rtf    call s:write(s:write_cmd("rtf"))
61         " rtfd
62         autocmd BufReadPre,FileReadPre          *.rtfd   setlocal bin
63         autocmd BufReadPost,FileReadPost        *.rtfd   call s:read(s:read_cmd("txt"))
64         autocmd BufWritePost,FileWritePost      *.rtfd   call s:write(s:write_cmd("rtfd"))
66         " doc
67         autocmd BufReadPre,FileReadPre          *.doc    setlocal bin
68         autocmd BufReadPost,FileReadPost        *.doc    call s:read(s:read_cmd("txt"))
69         autocmd BufWritePost,FileWritePost      *.doc    call s:write(s:write_cmd("doc"))
71         " wordml
72         autocmd BufReadPre,FileReadPre          *.wordml setlocal bin
73         autocmd BufReadPost,FileReadPost        *.wordml call s:read(s:read_cmd("txt"))
74         autocmd BufWritePost,FileWritePost      *.wordml call s:write(s:write_cmd("wordml"))
75 augroup END
77 " return read command and option
78 fun s:read_cmd(ft)
79         return "textutil -convert " . a:ft . " -encoding " . g:textutil_txt_encoding
80 endfun
81 " return write command and option
82 fun s:write_cmd(ft)
83         return "textutil -convert " . a:ft . " -inputencoding " . g:textutil_txt_encoding
84 endfun
86 " Function to check that executing "cmd [-f]" works.
87 " The result is cached in s:have_"cmd" for speed.
88 fun s:check(cmd)
89         let name = substitute(a:cmd, '\(\S*\).*', '\1', '')
90         if !exists("s:have_" . name)
91                 let e = executable(name)
92                 if e < 0
93                         let r = system(name);
94                         let e = (r !~ "not found" && r != "")
95                 endif
96                 exe "let s:have_" . name . "=" . e
97         endif
98         exe "return s:have_" . name
99 endfun
101 " after reading file, convert file format.
102 fun s:read(cmd)
103         " don't do anything if the cmd is not supported
104         if !s:check(a:cmd)
105                 return
106         endif
107         " make 'patchmode' empty, we don't want a copy of the written file
108         let pm_save = &pm
109         set pm=
110         " remove 'a' and 'A' from 'cpo' to avoid the alternate file changes
111         let cpo_save = &cpo
112         set cpo-=a cpo-=A
113         " set 'modifiable'
114         let ma_save = &ma
115         setlocal ma
117         " when filtering the whole buffer, it will become empty
118         let empty = line("'[") == 1 && line("']") == line("$")
120         let tmp = tempname()
121         let tmpe = tmp . "." . expand("<afile>:e")
123         " write the just read lines to a temp file
124         execute "silent '[,']w " . tmpe
126         " convert tmpe to text file
127         call system(a:cmd . " \"" . tmpe . "\" -output \"" . tmp . "\"")
129         " delete the compressed lines; remember the line number
130         let l = line("'[") - 1
131         if exists(":lockmarks")
132                 lockmarks '[,']d _
133         else
134                 '[,']d _
135         endif
137         " read in the uncompressed lines "'[-1r tmp"
138         setlocal bin
139         if exists(":lockmarks")
140                 execute "silent lockmarks " . l . "r " . tmp
141         else
142                 execute "silent " . l . "r " . tmp
143         endif
145         " if buffer became empty, delete trailing blank line
146         if empty
147                 silent $delete _
148                 1
149         endif
150         " delete the temp file and the used buffers
151         call delete(tmp)
152         call delete(tmpe)
153         silent! exe "bwipe " . tmp
154         silent! exe "bwipe " . tmpe
155         let &pm = pm_save
156         let &cpo = cpo_save
157         let &l:ma = ma_save
158         " When uncompressed the whole buffer, do autocommands
159         if empty
160                 if &verbose >= 8
161                         execute "doau BufReadPost " . expand("%:r")
162                 else
163                         execute "silent! doau BufReadPost " . expand("%:r")
164                 endif
165         endif
166 endfun
168 " after writing file, convert file format.
169 fun s:write(cmd)
170         " don't do anything if the cmd is not supported
171         if s:check(a:cmd)
172                 " Rename the file before compressing it.
173                 let nm = expand("<afile>")
174                 let nmt = tempname()
175                 if rename(nm, nmt) == 0
176                         call system(a:cmd . " \"" . nmt . "\"")
177                         call rename(nmt . "." . expand("<afile>:e"), nm)
178                 endif
179         endif
180 endfun
182 " vim: set sw=4 :