1
1
let s: save_cpo = &cpo
2
2
set cpo &vim
3
3
4
+ let s: on_windows = has (' win32' ) || has (' win64' )
5
+
4
6
" helper functions {{{
5
7
function ! s: has_vimproc ()
6
8
if ! exists (' s:exists_vimproc' )
@@ -92,7 +94,7 @@ function! clang_format#get_version()
92
94
set shell = /bin/ bash
93
95
endif
94
96
try
95
- let version_output = s: system (g: clang_format #command .' --version 2>&1' )
97
+ let version_output = s: system (s: shellescape ( g: clang_format #command ) .' --version 2>&1' )
96
98
if stridx (version_output, ' NPM' ) != -1
97
99
" Note:
98
100
" When clang-format is installed with npm, version string is changed (#39).
@@ -109,7 +111,7 @@ endfunction
109
111
110
112
function ! clang_format#is_invalid ()
111
113
if ! exists (' s:command_available' )
112
- if ! executable (g: clang_format #command )
114
+ if ! executable (g: clang_format #command )
113
115
return 1
114
116
endif
115
117
let s: command_available = 1
@@ -134,6 +136,17 @@ function! s:verify_command()
134
136
echoerr ' clang-format 3.3 or earlier is not supported for the lack of aruguments'
135
137
endif
136
138
endfunction
139
+
140
+ function ! s: shellescape (str) abort
141
+ if s: on_windows && (&shell = ~? ' cmd\.exe' )
142
+ return ' ^"' . substitute (substitute (substitute (a: str ,
143
+ \ ' [&|<>()^"%]' , ' ^\0' , ' g' ),
144
+ \ ' \\\+\ze"' , ' \=repeat(submatch(0), 2)' , ' g' ),
145
+ \ ' \^"' , ' \\\0' , ' g' ) . ' ^"'
146
+ endif
147
+ return shellescape (a: str )
148
+ endfunction
149
+
137
150
" }}}
138
151
139
152
" variable definitions {{{
@@ -176,9 +189,9 @@ function! clang_format#format(line1, line2)
176
189
else
177
190
let args .= " -style=file "
178
191
endif
179
- let args .= printf (" -assume-filename=%s " , shellescape (escape (expand (' %' ), " \t " )))
192
+ let args .= printf (" -assume-filename=%s " , s: shellescape (escape (expand (' %' ), " \t " )))
180
193
let args .= g: clang_format #extra_args
181
- let clang_format = printf (" %s %s --" , g: clang_format #command , args )
194
+ let clang_format = printf (" %s %s --" , s: shellescape ( g: clang_format #command ) , args )
182
195
return s: system (clang_format, join (getline (1 , ' $' ), " \n " ))
183
196
endfunction
184
197
" }}}
0 commit comments