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
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.
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.
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.
29 " :let g:textutil_txt_encoding='Shift_JIS'
32 " This script is based on 'textutil' command.
33 " So this script will only run on MacOS 10.4 or later.
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")
44 let loaded_textutil = 1
47 if !exists('g:textutil_txt_encoding')
48 let g:textutil_txt_encoding = 'utf-8'
53 " Remove all textutil autocommands
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"))
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"))
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"))
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"))
77 " return read command and option
79 return "textutil -convert " . a:ft . " -encoding " . g:textutil_txt_encoding
81 " return write command and option
83 return "textutil -convert " . a:ft . " -inputencoding " . g:textutil_txt_encoding
86 " Function to check that executing "cmd [-f]" works.
87 " The result is cached in s:have_"cmd" for speed.
89 let name = substitute(a:cmd, '\(\S*\).*', '\1', '')
90 if !exists("s:have_" . name)
91 let e = executable(name)
94 let e = (r !~ "not found" && r != "")
96 exe "let s:have_" . name . "=" . e
98 exe "return s:have_" . name
101 " after reading file, convert file format.
103 " don't do anything if the cmd is not supported
107 " make 'patchmode' empty, we don't want a copy of the written file
110 " remove 'a' and 'A' from 'cpo' to avoid the alternate file changes
117 " when filtering the whole buffer, it will become empty
118 let empty = line("'[") == 1 && line("']") == line("$")
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")
137 " read in the uncompressed lines "'[-1r tmp"
139 if exists(":lockmarks")
140 execute "silent lockmarks " . l . "r " . tmp
142 execute "silent " . l . "r " . tmp
145 " if buffer became empty, delete trailing blank line
150 " delete the temp file and the used buffers
153 silent! exe "bwipe " . tmp
154 silent! exe "bwipe " . tmpe
158 " When uncompressed the whole buffer, do autocommands
161 execute "doau BufReadPost " . expand("%:r")
163 execute "silent! doau BufReadPost " . expand("%:r")
168 " after writing file, convert file format.
170 " don't do anything if the cmd is not supported
172 " Rename the file before compressing it.
173 let nm = expand("<afile>")
175 if rename(nm, nmt) == 0
176 call system(a:cmd . " \"" . nmt . "\"")
177 call rename(nmt . "." . expand("<afile>:e"), nm)