1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

fix zsh tab completion

This commit is contained in:
William Ting 2013-12-30 17:44:39 -06:00
parent bec6314eab
commit 6293af5b7a
4 changed files with 71 additions and 70 deletions

View File

@ -29,6 +29,8 @@
- autojump now uses ~/Library/autojump for storing data on OS X instead of - autojump now uses ~/Library/autojump for storing data on OS X instead of
incorrectly using Linux's $XDG_DATA_HOME. Existing data should automatically incorrectly using Linux's $XDG_DATA_HOME. Existing data should automatically
be migrated to the new location. be migrated to the new location.
- Past behavior jumped to the highest weight database entry when not passed any
arguments. The new behavior is to stay in the current directory.
### Release v21.6.8: ### Release v21.6.8:

View File

@ -182,7 +182,10 @@ def find_matches(entries, needles):
[Entry('.', 0)])) [Entry('.', 0)]))
def handle_no_arguments(needle, needles, entries): def handle_tab_completion(needle, entries):
if not needle:
sys.exit(0)
tab_needle, path = get_tab_needle_and_path(needle, TAB_SEPARATOR) tab_needle, path = get_tab_needle_and_path(needle, TAB_SEPARATOR)
if path: if path:
@ -195,8 +198,11 @@ def handle_no_arguments(needle, needles, entries):
take(TAB_ENTRIES_COUNT, find_matches(entries, tab_needle)), take(TAB_ENTRIES_COUNT, find_matches(entries, tab_needle)),
TAB_SEPARATOR) TAB_SEPARATOR)
else: else:
print(encode_local(surround_quotes( print_tab_menu(
first(find_matches(entries, needles)).path))) needle,
take(TAB_ENTRIES_COUNT, find_matches(entries, needle)),
TAB_SEPARATOR)
def match_anywhere(needles, haystack, ignore_case=False): def match_anywhere(needles, haystack, ignore_case=False):
@ -333,11 +339,9 @@ def main(args):
if args.add: if args.add:
save(config, first(add_path(load(config), args.add))) save(config, first(add_path(load(config), args.add)))
elif args.complete: elif args.complete:
needle = first(sanitize(args.directory)) handle_tab_completion(
tab_entries = take( needle=first(sanitize(args.directory)),
TAB_ENTRIES_COUNT, entries=entriefy(load(config)))
find_matches(entriefy(load(config)), needle))
print_tab_menu(needle, tab_entries, TAB_SEPARATOR)
elif args.decrease: elif args.decrease:
data, entry = decrease_path(load(config), get_pwd(), args.decrease) data, entry = decrease_path(load(config), get_pwd(), args.decrease)
save(config, data) save(config, data)
@ -360,8 +364,14 @@ def main(args):
else: else:
entries = entriefy(load(config)) entries = entriefy(load(config))
needles = sanitize(args.directory) needles = sanitize(args.directory)
needle = first(needles) _, path = get_tab_needle_and_path(first(needles), TAB_SEPARATOR)
handle_no_arguments(needle, needles, entries)
if path:
# found complete tab completion entry
print(encode_local(surround_quotes(path)))
else:
print(encode_local(surround_quotes(
first(find_matches(entries, needles))).path))
return 0 return 0

View File

@ -1,15 +1,4 @@
# determine the data directory according to the XDG Base Directory Specification # set paths for user installations
if [[ -n ${XDG_DATA_HOME} ]] && [[ ${XDG_DATA_HOME} == *${USER}* ]]; then
export AUTOJUMP_DATA_DIR="${XDG_DATA_HOME}/autojump"
else
export AUTOJUMP_DATA_DIR=${HOME}/.local/share/autojump
fi
if [[ ! -e ${AUTOJUMP_DATA_DIR} ]]; then
mkdir -p "${AUTOJUMP_DATA_DIR}"
fi
# set paths if necessary for local installations
if [[ -d ${HOME}/.autojump ]]; then if [[ -d ${HOME}/.autojump ]]; then
path=(${HOME}/.autojump/bin ${path}) path=(${HOME}/.autojump/bin ${path})
fpath=(${HOME}/.autojump/functions/ ${fpath}) fpath=(${HOME}/.autojump/functions/ ${fpath})
@ -20,20 +9,16 @@ command -v brew &>/dev/null \
&& [[ -d "`brew --prefix`/share/zsh/site-functions" ]] \ && [[ -d "`brew --prefix`/share/zsh/site-functions" ]] \
&& fpath=(`brew --prefix`/share/zsh/site-functions ${fpath}) && fpath=(`brew --prefix`/share/zsh/site-functions ${fpath})
# add change pwd hook
autojump_chpwd() { autojump_chpwd() {
if [[ "${AUTOJUMP_KEEP_SYMLINKS}" == "1" ]]; then (autojump -a "$(pwd)" &) &>/dev/null
_PWD_ARGS=""
else
_PWD_ARGS="-P"
fi
{ (autojump -a "$(pwd ${_PWD_ARGS})"&)>/dev/null 2>>|${AUTOJUMP_DATA_DIR}/autojump_errors ; } 2>/dev/null
} }
typeset -gaU chpwd_functions typeset -gaU chpwd_functions
chpwd_functions+=autojump_chpwd chpwd_functions+=autojump_chpwd
# default autojump command
j() { j() {
# Cannot use =~ due to MacPorts zsh v4.2, see issue #125.
if [[ ${@} == -* ]]; then if [[ ${@} == -* ]]; then
autojump ${@} autojump ${@}
return return
@ -50,46 +35,45 @@ j() {
fi fi
} }
# jump to child directory (subdirectory of current path)
jc() { jc() {
if [[ ${@} == -* ]]; then if [[ ${@} == -* ]]; then
j ${@} autojump ${@}
else
j $(pwd)/ ${@}
fi
}
jo() {
if [[ ${@} == -* ]]; then
j ${@}
return return
fi fi
if [ -z $(autojump $@) ]; then j $(pwd)/ ${@}
echo "autojump: directory '${@}' not found"
echo "Try \`autojump --help\` for more information."
false
else
case ${OSTYPE} in
linux-gnu)
xdg-open "$(autojump $@)"
;;
darwin*)
open "$(autojump $@)"
;;
cygwin)
cygstart "" $(cygpath -w -a $(pwd))
;;
*)
echo "Unknown operating system." 1>&2
;;
esac
fi
} }
# open autojump results in file browser
jo() {
if [[ ${@} == -* ]]; then
autojump ${@}
return
fi
case ${OSTYPE} in
linux-gnu)
xdg-open "$(autojump $@)"
;;
darwin*)
open "$(autojump $@)"
;;
cygwin)
cygstart "" $(cygpath -w -a $(pwd))
;;
*)
echo "Unknown operating system." 1>&2
;;
esac
}
# open autojump results (child directory) in file browser
jco() { jco() {
if [[ ${@} == -* ]]; then if [[ ${@} == -* ]]; then
j ${@} autojump ${@}
else return
jo $(pwd) ${@}
fi fi
jo $(pwd)/ ${@}
} }

View File

@ -65,17 +65,22 @@ def get_tab_needle_and_path(tab_entry, separator):
[needle]__[index]__[path] [needle]__[index]__[path]
""" """
matches = re.search( match_needle = re.search(r'(.*?)' + separator, tab_entry)
r'(.*?)' + match_path = re.search(
separator + separator + r'[0-9]{1}' + separator + r'(.*)',
r'[0-9]{1}' +
separator +
r'(.*)',
tab_entry) tab_entry)
if matches: if match_needle:
return matches.groups() tab_needle = match_needle.group(1)
return None, None else:
tab_needle = None
if match_path:
path = match_path.group(1)
else:
path = None
return tab_needle, path
def get_pwd(): def get_pwd():