4
4
5
5
" Defines the mapping between the vim-plist options and plutil arguments
6
6
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
7
16
8
17
function ! plist#Read (bufread )
9
18
" Get the filename of the current argument
10
19
let filename = expand (' <afile>' )
11
20
12
21
" If the file does not exist, there is nothing to convert
13
22
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)
16
30
return
17
31
endif
18
32
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
22
49
23
50
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!' )
27
52
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.
29
54
" FileReadCmd just reads the content into the existing buffer
30
55
if a: bufread
31
56
silent bwipeout !
@@ -34,33 +59,36 @@ function! plist#Read(bufread)
34
59
return
35
60
endif
36
61
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
41
63
call plist#DisplayInfo (filename, b: plist_original_format )
42
64
endfunction
43
65
44
66
function ! plist#Write ()
45
67
" Cache the argument filename destination
46
68
let filename = resolve (expand (' <afile>' ))
47
69
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
50
72
" precedence.
51
73
let save_format = ! len (g: plist_save_format ) ? b: plist_original_format : g: plist_save_format
52
74
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
58
89
59
90
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!' )
64
92
return
65
93
else
66
94
" Give the user visual feedback about the write
@@ -83,12 +111,9 @@ function! plist#ReadPost()
83
111
endfunction
84
112
85
113
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' )
90
116
else
91
- " We hardcode this to xml, since it maps one-to-one
92
117
set filetype = xml
93
118
endif
94
119
endfunction
@@ -97,11 +122,11 @@ function! plist#DetectFormat(filename)
97
122
let content = readfile (a: filename , 1 , 2 )
98
123
99
124
if len (content) > 0 && content[0 ] = ~ " ^bplist"
100
- let b: plist_original_format = ' binary'
125
+ return ' binary'
101
126
elseif len (content) > 1 && content[1 ] = ~ ' ^<!DOCTYPE plist'
102
- let b: plist_original_format = ' xml'
127
+ return ' xml'
103
128
else
104
- let b: plist_original_format = ' json'
129
+ return ' json'
105
130
endif
106
131
endfunction
107
132
0 commit comments