Skip to content

Commit 3fa7afa

Browse files
committed
Expand special variables in :Z command
Now if you do `:Z ~` it'll actually take you to the home directory, since that's a special variable. Same thing for paths like `%:p` or shell variables. Hopefully this doesn't break anything. Closes #2.
1 parent 4b80366 commit 3fa7afa

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

autoload/zcd.vim

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
" Invalid g:zcd#path config.
66
func! s:ErrorInvalidPathConfig() abort
77
echohl Error
8-
echon 'Error(z.vim): '
8+
echon 'Error(z.vim):'
99
echohl None
10-
echon 'Huh, the '
10+
echon ' Huh, the '
1111
echohl String
1212
echon 'g:zcd#path'
1313
echohl None
@@ -54,6 +54,21 @@ func! s:GetPathToZ() abort
5454
return v:null
5555
endfunc
5656

57+
" Expand special symbols like '~', `%:h`, or `$HOME`.
58+
func! s:ExpandSymbols(input) abort
59+
return map(copy(a:input), 'expand(v:val)')
60+
endfunc
61+
62+
" The user entered a single expandable variable, like '~'. Just go there.
63+
func! s:GetObviousDestination(input, expanded) abort
64+
" See if `expand(...)` thinks the input is special.
65+
if len(a:input) is# 1 && a:input[0] isnot# a:expanded[0]
66+
return a:expanded[0]
67+
endif
68+
69+
return v:null
70+
endfunc
71+
5772
" Execute a shell command to find best folder matches.
5873
func! s:GetSearchOutput(search) abort
5974
let l:z_path = s:GetPathToZ()
@@ -96,6 +111,10 @@ func! s:ParseOutput(output) abort
96111
return l:results
97112
endfunc
98113

114+
func! s:GoToDirectory(directory) abort
115+
execute 'edit ' . fnameescape(a:directory)
116+
endfunc
117+
99118
" Parse z.sh output into a list of possible matches.
100119
" Ordered descending by match probability (assumes z.sh output order).
101120
func! zcd#FindMatches(search) abort
@@ -105,7 +124,14 @@ endfunc
105124

106125
" Invoked by :Z ...
107126
func! zcd#(...) abort
108-
let l:search = join(a:000, ' ')
127+
let l:expanded = s:ExpandSymbols(a:000)
128+
let l:obvious_destination = s:GetObviousDestination(a:000, l:expanded)
129+
130+
if l:obvious_destination isnot# v:null
131+
return s:GoToDirectory(l:obvious_destination)
132+
endif
133+
134+
let l:search = join(l:expanded, ' ')
109135
let l:matches = zcd#FindMatches(l:search)
110136

111137
" Error state. Assume it was already echoed.
@@ -123,7 +149,7 @@ func! zcd#(...) abort
123149
endif
124150

125151
" Open the most probable match in the current pane.
126-
execute 'edit ' . fnameescape(l:matches[0].directory)
152+
call s:GoToDirectory(l:matches[0].directory)
127153
endfunc
128154

129155
" Command completion.

doc/z.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,10 @@ zcd#FindMatches({search_term}) *zcd#FindMatches()*
7777
0.1.0
7878
Initial release
7979

80+
0.1.1 - March 6, 2019
81+
Added:
82+
- Expansion of special variables (e.g. `~`, `$HOME`, or `%:h`). Affects both
83+
command and programmatic usage.
84+
8085
==============================================================================
8186
vim: ft=help tw=78:

0 commit comments

Comments
 (0)