diff --git a/autoload/vital/__vital__/System/Filepath.vim b/autoload/vital/__vital__/System/Filepath.vim index dc2b5df17..e3fe88151 100644 --- a/autoload/vital/__vital__/System/Filepath.vim +++ b/autoload/vital/__vital__/System/Filepath.vim @@ -14,6 +14,27 @@ let s:is_mac = !s:is_windows && !s:is_cygwin \ (!isdirectory('/proc') && executable('sw_vers'))) let s:is_case_tolerant = filereadable(expand(':r') . '.VIM') +if s:is_windows + function! s:to_slash(path) abort + return tr(a:path, '\', '/') + endfunction +else + function! s:to_slash(path) abort + return a:path + endfunction +endif + +if s:is_windows + function! s:from_slash(path) abort + return tr(a:path, '/', '\') + endfunction +else + function! s:from_slash(path) abort + return a:path + endfunction +endif + + " Get the directory separator. function! s:separator() abort return fnamemodify('.', ':p')[-1 :] diff --git a/doc/vital/System/Filepath.txt b/doc/vital/System/Filepath.txt index b69eb2ea1..b72080ffa 100644 --- a/doc/vital/System/Filepath.txt +++ b/doc/vital/System/Filepath.txt @@ -21,6 +21,29 @@ INTERFACE *Vital.System.Filepath-interface* ------------------------------------------------------------------------------ FUNCTIONS *Vital.System.Filepath-functions* +to_slash({path}) *Vital.System.Filepath.to_slash()* + Use when you quote path string for scripting languages, without + worrying about the runtime operating system is if windows or not. + + e.g. +> + let code = printf( + \ '(neoclojure-init "%s")', + \ shellescape(s:F.to_slash(path))) + .. + let path = s:F.from_slash( + \ s:get_path_like_string_from_something()) +< + + This function intentionally ignores 'shellslash' option, so that you + can simply use this function for path string not only from Vim but + also from others, such as environment vars, problem output, or really + anything. + See also: |Vital.System.Filepath.from_slash()| + +from_slash({path}) *Vital.System.Filepath.from_slash()* + This is opposite to |Vital.System.Filepath.to_slash()|. See its doc. + separator() *Vital.System.Filepath.separator()* Return OS related directory separator as string. This returns "/" on non-Windows, or Windows and |'shellslash'| is on. diff --git a/test/System/Filepath.vimspec b/test/System/Filepath.vimspec index c477eb683..e4462b0d1 100644 --- a/test/System/Filepath.vimspec +++ b/test/System/Filepath.vimspec @@ -8,6 +8,27 @@ Describe System.Filepath let is_case_sensitive_file_system = !(isdirectory(tolower(g:root)) && isdirectory(toupper(g:root))) End + Describe .to_slash() + It converts / to \ only when it's necessary + Assert Equals('a/b', FP.to_slash('a/b')) + if has('win32') || has('win64') + Assert Equals('a/b', FP.to_slash('a\b')) + else + Assert Equals('a\b', FP.to_slash('a\b')) + endif + End + End + + Describe .from_slash() + It converts \ to / only when it's necessary + if has('win32') || has('win64') + Assert Equals('a\b', FP.from_slash('a/b')) + else + Assert Equals('a/b', FP.from_slash('a/b')) + endif + End + End + Describe .is_absolute() if !s:is_windows It returns TRUE if {path} is an absolute path