@@ -46,6 +46,17 @@ _add_completion() {
4646 eval " cur=${_cur} "
4747 # Save original cur (to restore later)
4848 local orig_cur=" $cur "
49+ local orig_dirpart=" "
50+ local stripped_orig=" $orig_cur "
51+ if [[ " $stripped_orig " == " '" * || " $stripped_orig " == ' "' * ]]; then
52+ stripped_orig=" ${stripped_orig: 1} "
53+ fi
54+ if [[ " $stripped_orig " == * /* ]]; then
55+ orig_dirpart=" ${stripped_orig%/* } "
56+ fi
57+ if [[ " $orig_dirpart " == " ." && " ${stripped_orig: 0: 2} " != " ./" ]]; then
58+ orig_dirpart=" "
59+ fi
4960
5061 # Check if we have the necessary variables
5162 if [[ -z " ${_cur-} " ]] || [[ -z " ${_var-} " ]]; then
@@ -109,11 +120,15 @@ _add_completion() {
109120 cd " $savedPWD " || return
110121
111122 if [[ ${# pinyin_matched[@]} -gt 0 ]]; then
112- if [[ -n " $dirpart " ]]; then
123+ local display_dirpart=" $dirpart "
124+ if [[ -n " $orig_dirpart " ]]; then
125+ display_dirpart=" $orig_dirpart "
126+ fi
127+ if [[ -n " $display_dirpart " ]]; then
113128 local sep=" /"
114- [[ " $dirpart " == " /" ]] && sep=" "
129+ [[ " $display_dirpart " == " /" ]] && sep=" "
115130 for i in " ${! pinyin_matched[@]} " ; do
116- pinyin_matched[$i ]=" ${dirpart }${sep}${pinyin_matched[$i]} "
131+ pinyin_matched[$i ]=" ${display_dirpart }${sep}${pinyin_matched[$i]} "
117132 done
118133 fi
119134
@@ -122,13 +137,30 @@ _add_completion() {
122137 orig_check=" ${orig_check: 1} "
123138 fi
124139 if [[ " $orig_check " == ~ * ]]; then
125- for i in " ${! pinyin_matched[@]} " ; do
126- if [[ " ${pinyin_matched[$i]} " == " $HOME " ]]; then
127- pinyin_matched[$i ]=" ~"
128- elif [[ " ${pinyin_matched[$i]} " == $HOME /* ]]; then
129- pinyin_matched[$i ]=" ~/${pinyin_matched[$i]# " $HOME /" } "
140+ local tilde_prefix=" ${orig_check%%/* } "
141+ local expanded_prefix=" "
142+ if [[ " $tilde_prefix " == " ~" ]]; then
143+ expanded_prefix=" $HOME "
144+ elif [[ " $tilde_prefix " == ~ + ]]; then
145+ expanded_prefix=" $PWD "
146+ elif [[ " $tilde_prefix " == ~ - ]]; then
147+ expanded_prefix=" ${OLDPWD-} "
148+ else
149+ local tilde_user=" ${tilde_prefix: 1} "
150+ if [[ -n " $tilde_user " ]]; then
151+ expanded_prefix=" $( getent passwd " $tilde_user " 2> /dev/null | cut -d: -f6) "
130152 fi
131- done
153+ fi
154+ if [[ -n " $expanded_prefix " ]]; then
155+ for i in " ${! pinyin_matched[@]} " ; do
156+ if [[ " ${pinyin_matched[$i]} " == " $expanded_prefix " ]]; then
157+ pinyin_matched[$i ]=" $tilde_prefix "
158+ elif [[ " ${pinyin_matched[$i]} " == " $expanded_prefix " /* ]]; then
159+ local suffix=" ${pinyin_matched[$i]# " $expanded_prefix /" } "
160+ pinyin_matched[$i ]=" $tilde_prefix /$suffix "
161+ fi
162+ done
163+ fi
132164 fi
133165
134166 local current_results_var=" current_results"
0 commit comments