Skip to content

Commit 0d8a2aa

Browse files
committed
Add support 'plistutil'
1 parent 67280fb commit 0d8a2aa

File tree

3 files changed

+67
-55
lines changed

3 files changed

+67
-55
lines changed

README.md

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
![Screenshot of vim-plist in action](http://i.imgur.com/ezBKTK7.png)
44

5-
This vim bundle adds complete support for [property lists](http://en.wikipedia.org/wiki/Property_list) (*plist*) files on OS X.
5+
This vim bundle adds complete support for [property lists](plist) (*plist*)
6+
files.
67

7-
The plugin uses the underlying **plutil** tool for manipulating property lists.
8-
It supports reading and writing in *binary*, *xml* and *json* formats.
8+
The plugin uses the underlying **plutil** (or **plistutil**) tool for
9+
manipulating property lists. It supports reading and writing in *binary*,
10+
*xml* and *json* formats.
911

1012
## Requirements
1113

12-
- Vim 7.2 or later
13-
- plutil (bundled with OS X)
14+
- Vim 7.4 or later
15+
- plutil (bundled with macOS) or [plistutil](libplist)
1416

1517
## Installation
1618

@@ -68,17 +70,6 @@ options availabe.
6870
opened. If you want to override this and always save property lists in a
6971
specific format, you can use *json*, *xml* or *binary* format.
7072
71-
- Change the filetype used for property lists in *json* format:
72-
73-
```vim
74-
let g:plist_json_filetype = 'javascript'
75-
```
76-
77-
Vim does not have inherent support for *json* files (it reverts to
78-
*JavaScript* syntax). If you do want a specific filetype for *json* property
79-
lists, when using a json plugin (such as [vim-json][vim-json]), you can
80-
specify *json* as the filetype instead.
81-
8273
## Notes
8374
8475
If you want syntax checking I highly recommend [Syntastic][syntastic] since it
@@ -90,7 +81,7 @@ uses the underlying Vim *write* function which bypasses the plugins
9081
`BufWriteCmd` and `FileWriteCmd` hooks.
9182
9283
This does not add *plist* as a new filetype, but merely conversion
93-
functionality between the different representible formats.
84+
functionality between the different representable formats.
9485
9586
## Todo
9687
@@ -109,3 +100,5 @@ MIT: [License][license]
109100
[vim-json]: https://github.com/elzr/vim-json
110101
[syntastic]: https://github.com/scrooloose/syntastic
111102
[license]: https://github.com/darfink/vim-plist/blob/master/LICENSE
103+
[libplist]: https://github.com/libimobiledevice/libplist
104+
[plist]: http://en.wikipedia.org/wiki/Property_list

autoload/plist.vim

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,53 @@
44

55
" Defines the mapping between the vim-plist options and plutil arguments
66
let s:mapping = { 'json': 'json', 'binary': 'binary1', 'xml': 'xml1' }
7+
let s:mapping_util = { 'binary': 'bin', 'xml': 'xml' }
8+
let s:has_plist = executable('plutil')
9+
let s:has_plistutil = executable('plistutil')
10+
11+
function! s:warn(message)
12+
echohl WarningMsg
13+
let blackhole = input(a:message . ' (Press ENTER)')
14+
echohl None
15+
endfunction
716

817
function! plist#Read(bufread)
918
" Get the filename of the current argument
1019
let filename = expand('<afile>')
1120

1221
" If the file does not exist, there is nothing to convert
1322
if !filereadable(filename)
14-
" We simply leave the buffer handling to Vim
15-
silent execute ':doautocmd BufNewFile ' . fnameescape(filename)
23+
silent execute 'doautocmd BufNewFile ' . fnameescape(filename)
24+
return
25+
endif
26+
27+
if !s:has_plist && !s:has_plistutil
28+
echoerr 'plutil is not found in $PATH'
29+
silent execute 'read ' . fnameescape(filename)
1630
return
1731
endif
1832

19-
" Convert the file's content directly, and read it into the current buffer
20-
execute 'silent read !plutil -convert ' . s:mapping[g:plist_display_format] .
21-
\ ' -r ' . shellescape(filename, 1) . ' -o -'
33+
" Determine which format should be used when saving
34+
let b:plist_original_format = plist#DetectFormat(filename)
35+
36+
" Convert the file's content and read it into the current buffer
37+
if s:has_plist
38+
execute 'silent read !plutil -convert ' . s:mapping[g:plist_display_format]
39+
\ . ' -r ' . shellescape(filename, 1) . ' -o -'
40+
else
41+
if g:plist_display_format == 'json'
42+
call s:warn('Plistutil does not support json display format')
43+
endif
44+
45+
execute 'silent read !plistutil -f xml -i ' . shellescape(filename, 1) . ' -o -'
46+
endif
47+
48+
let b:read_error = v:shell_error != 0
2249

2350
if (v:shell_error)
24-
echohl WarningMsg
25-
let blackhole = input('Plist could not be converted! (Press ENTER)')
26-
echohl None
51+
call s:warn('Plist could not be converted!')
2752

28-
" Only wipeout the buffer if we were creating one to start with.
53+
" Only wipeout the buffer if one was being created to start with.
2954
" FileReadCmd just reads the content into the existing buffer
3055
if a:bufread
3156
silent bwipeout!
@@ -34,33 +59,36 @@ function! plist#Read(bufread)
3459
return
3560
endif
3661

37-
" We need to know in which format we should save the file
38-
call plist#DetectFormat(filename)
39-
40-
" Tell the user about any information we've parsed
62+
" Tell the user about any information parsed
4163
call plist#DisplayInfo(filename, b:plist_original_format)
4264
endfunction
4365

4466
function! plist#Write()
4567
" Cache the argument filename destination
4668
let filename = resolve(expand('<afile>'))
4769

48-
" If the user has not specified his preferred format when saving, we use the
49-
" same format as the filed had originally. Otherwise the user option takes
70+
" If the user has not specified his preferred format when saving, use the
71+
" same format as the file had originally. Otherwise the user option takes
5072
" precedence.
5173
let save_format = !len(g:plist_save_format) ? b:plist_original_format : g:plist_save_format
5274

53-
" Use plutil even when the current format is the same as the target format,
54-
" since it will give the user additional error checking (he will be notified
55-
" if there is any error upon saving).
56-
execute "silent '[,']write !plutil -convert " . s:mapping[save_format] .
57-
\ ' - -o ' . shellescape(filename, 1)
75+
if s:has_plist
76+
" Use plutil even when the current format is the same as the target
77+
" format, since it will give the user additional error checking (they will
78+
" be notified if there is any error upon saving).
79+
execute "silent '[,']write !plutil -convert " . s:mapping[save_format] .
80+
\ ' - -o ' . shellescape(filename, 1)
81+
else
82+
if b:plist_original_format == 'json'
83+
call s:warn('Plistutil cannot process json, saving buffer contents directly')
84+
else
85+
execute "silent '[,']write !plistutil -f " . s:mapping_util[save_format] .
86+
\ ' -i - -o ' . shellescape(filename, 1)
87+
endif
88+
endif
5889

5990
if (v:shell_error)
60-
echohl WarningMsg
61-
let blackhole = input('Plist could not be saved! (Press ENTER)')
62-
echohl None
63-
91+
call s:warn('Plist could not be saved!')
6492
return
6593
else
6694
" Give the user visual feedback about the write
@@ -83,12 +111,9 @@ function! plist#ReadPost()
83111
endfunction
84112

85113
function! plist#SetFiletype()
86-
if g:plist_display_format == 'json'
87-
" There is no specific support for json bundled with Vim, so we let the
88-
" user decide the filetype (by default we assume 'JavaScript').
89-
execute 'set filetype=' . g:plist_json_filetype
114+
if g:plist_display_format == 'json' && s:has_plist || b:plist_original_format == 'json' && !s:has_plist
115+
execute 'set filetype=' . (len(getcompletion('json', 'filetype')) ? 'json' : 'javascript')
90116
else
91-
" We hardcode this to xml, since it maps one-to-one
92117
set filetype=xml
93118
endif
94119
endfunction
@@ -97,11 +122,11 @@ function! plist#DetectFormat(filename)
97122
let content = readfile(a:filename, 1, 2)
98123

99124
if len(content) > 0 && content[0] =~ "^bplist"
100-
let b:plist_original_format = 'binary'
125+
return 'binary'
101126
elseif len(content) > 1 && content[1] =~ '^<!DOCTYPE plist'
102-
let b:plist_original_format = 'xml'
127+
return 'xml'
103128
else
104-
let b:plist_original_format = 'json'
129+
return 'json'
105130
endif
106131
endfunction
107132

plugin/plist.vim

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,3 @@ if !exists('g:plist_save_format')
3232
" Available options are: xml, json & binary
3333
let g:plist_save_format = ''
3434
end
35-
36-
if !exists('g:plist_json_filetype')
37-
" Controls the filetype used for json plists (JavaScript is what Vim uses by
38-
" default for these filetypes).
39-
let g:plist_json_filetype = 'javascript'
40-
end

0 commit comments

Comments
 (0)