44
55" Defines the mapping between the vim-plist options and plutil arguments
66let 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
817function ! 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 )
4264endfunction
4365
4466function ! 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()
83111endfunction
84112
85113function ! 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
94119endfunction
@@ -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
106131endfunction
107132
0 commit comments