11let s: save_cpo = &cpo
22set cpo &vim
33
4+ let s: on_windows = has (' win32' ) || has (' win64' )
5+
46" helper functions {{{
57function ! s: has_vimproc ()
68 if ! exists (' s:exists_vimproc' )
@@ -92,7 +94,7 @@ function! clang_format#get_version()
9294 set shell = /bin/ bash
9395 endif
9496 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' )
9698 if stridx (version_output, ' NPM' ) != -1
9799 " Note:
98100 " When clang-format is installed with npm, version string is changed (#39).
@@ -109,7 +111,7 @@ endfunction
109111
110112function ! clang_format#is_invalid ()
111113 if ! exists (' s:command_available' )
112- if ! executable (g: clang_format #command )
114+ if ! executable (g: clang_format #command )
113115 return 1
114116 endif
115117 let s: command_available = 1
@@ -134,6 +136,17 @@ function! s:verify_command()
134136 echoerr ' clang-format 3.3 or earlier is not supported for the lack of aruguments'
135137 endif
136138endfunction
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+
137150" }}}
138151
139152" variable definitions {{{
@@ -176,9 +189,9 @@ function! clang_format#format(line1, line2)
176189 else
177190 let args .= " -style=file "
178191 endif
179- let args .= printf (" -assume-filename=%s " , shellescape (escape (expand (' %' ), " \t " )))
192+ let args .= printf (" -assume-filename=%s " , s: shellescape (escape (expand (' %' ), " \t " )))
180193 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 )
182195 return s: system (clang_format, join (getline (1 , ' $' ), " \n " ))
183196endfunction
184197" }}}
0 commit comments