Skip to content

Commit e5523c4

Browse files
committed
System.Filepath: Introduce expand_home() function
Because sometimes you want to expand the path "~vim-jp/$EDITOR" to "/home/vim-jp/$EDITOR", but that file name doesn't exist, making fnamemodify(_, ':p') unpredictable, and the environment variable syntax in the path makes expand(_) unwieldy.
1 parent 2f39c1d commit e5523c4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

autoload/vital/__vital__/System/Filepath.vim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,23 @@ function! s:is_case_tolerant() abort
197197
endfunction
198198

199199

200+
function! s:expand_home(path) abort
201+
if a:path[:0] !=# '~'
202+
return a:path
203+
endif
204+
let post_home_idx = match(a:path, s:path_sep_pattern)
205+
return post_home_idx is# -1
206+
\ ? s:remove_last_separator(expand(a:path))
207+
\ : s:remove_last_separator(expand(a:path[0 : post_home_idx - 1]))
208+
\ . a:path[post_home_idx :]
209+
endfunction
210+
200211
function! s:abspath(path) abort
201212
if s:is_absolute(a:path)
202213
return a:path
203214
endif
204-
" Note:
205-
" the behavior of ':p' for non existing file path/directory is not defined
215+
" NOTE: The behavior of ':p' for a non existing file/directory path
216+
" is not defined.
206217
return (filereadable(a:path) || isdirectory(a:path))
207218
\ ? fnamemodify(a:path, ':p')
208219
\ : s:join(fnamemodify(getcwd(), ':p'), a:path)

doc/vital/System/Filepath.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ is_case_tolerant() *Vital.System.Filepath.is_case_tolerant()*
9797
Return non-zero if filesystem ignores alphabetic case of a filename,
9898
zero otherwise.
9999

100+
expand_home({path}) *Vital.System.Filepath.expand_home()*
101+
Return a home-expanded path of {path}. The home directory expansion
102+
will not have a trailing directory separator.
103+
104+
Like |expand()|, but only expanding a leading "~" home directory
105+
sequence, or like |fnamemodify()| with the ":p" modifier, but without
106+
full path expansion or unpredictable results if a file name doesn't
107+
exist and doesn't have an absolute path. See |filename-modifiers|.
108+
100109
abspath({path}) *Vital.System.Filepath.abspath()*
101110
Return an absolute path of {path}.
102111
If the {path} is already an absolute path, it returns the {path}.

0 commit comments

Comments
 (0)