5
5
" Invalid g:zcd#path config.
6
6
func ! s: ErrorInvalidPathConfig () abort
7
7
echohl Error
8
- echon ' Error(z.vim): '
8
+ echon ' Error(z.vim):'
9
9
echohl None
10
- echon ' Huh, the '
10
+ echon ' Huh, the '
11
11
echohl String
12
12
echon ' g:zcd#path'
13
13
echohl None
@@ -54,6 +54,21 @@ func! s:GetPathToZ() abort
54
54
return v: null
55
55
endfunc
56
56
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
+
57
72
" Execute a shell command to find best folder matches.
58
73
func ! s: GetSearchOutput (search ) abort
59
74
let l: z_path = s: GetPathToZ ()
@@ -96,6 +111,10 @@ func! s:ParseOutput(output) abort
96
111
return l: results
97
112
endfunc
98
113
114
+ func ! s: GoToDirectory (directory ) abort
115
+ execute ' edit ' . fnameescape (a: directory )
116
+ endfunc
117
+
99
118
" Parse z.sh output into a list of possible matches.
100
119
" Ordered descending by match probability (assumes z.sh output order).
101
120
func ! zcd#FindMatches (search ) abort
@@ -105,7 +124,14 @@ endfunc
105
124
106
125
" Invoked by :Z ...
107
126
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 , ' ' )
109
135
let l: matches = zcd#FindMatches (l: search )
110
136
111
137
" Error state. Assume it was already echoed.
@@ -123,7 +149,7 @@ func! zcd#(...) abort
123
149
endif
124
150
125
151
" 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 )
127
153
endfunc
128
154
129
155
" Command completion.
0 commit comments