|
|
|
@ -43,7 +43,8 @@ TESTING = False
|
|
|
|
|
if 'AUTOJUMP_DATA_DIR' in os.environ:
|
|
|
|
|
CONFIG_DIR = os.environ.get('AUTOJUMP_DATA_DIR')
|
|
|
|
|
else:
|
|
|
|
|
xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(os.environ['HOME'], '.local', 'share')
|
|
|
|
|
xdg_data_dir = os.environ.get('XDG_DATA_HOME') or os.path.join(
|
|
|
|
|
os.environ['HOME'], '.local', 'share')
|
|
|
|
|
CONFIG_DIR = os.path.join(xdg_data_dir, 'autojump')
|
|
|
|
|
|
|
|
|
|
KEEP_ALL_ENTRIES = False
|
|
|
|
@ -63,6 +64,7 @@ if CONFIG_DIR == os.path.expanduser('~'):
|
|
|
|
|
else:
|
|
|
|
|
DB_FILE = CONFIG_DIR + '/autojump.txt'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Database:
|
|
|
|
|
"""
|
|
|
|
|
Object for interfacing with autojump database.
|
|
|
|
@ -84,7 +86,8 @@ class Database:
|
|
|
|
|
self.data[path] = increment
|
|
|
|
|
else:
|
|
|
|
|
import math
|
|
|
|
|
self.data[path] = math.sqrt((self.data[path]**2)+(increment**2))
|
|
|
|
|
self.data[path] = math.sqrt((self.data[path] ** 2)
|
|
|
|
|
+ (increment ** 2))
|
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
|
def decay(self):
|
|
|
|
@ -165,7 +168,8 @@ class Database:
|
|
|
|
|
for path, weight in sorted(self.data.items(),
|
|
|
|
|
key=itemgetter(1),
|
|
|
|
|
reverse=True):
|
|
|
|
|
temp.write((unico("%s\t%s\n")%(weight, path)).encode("utf-8"))
|
|
|
|
|
temp.write(
|
|
|
|
|
(unico("%s\t%s\n") % (weight, path)).encode("utf-8"))
|
|
|
|
|
|
|
|
|
|
# catching disk errors and skipping save when file handle can't be closed.
|
|
|
|
|
try:
|
|
|
|
@ -205,7 +209,8 @@ def options():
|
|
|
|
|
"""
|
|
|
|
|
global ARGS
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Automatically jump to directory passed as an argument.',
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
description='Automatically jump to directory passed as an argument.',
|
|
|
|
|
epilog="Please see autojump(1) man pages for full documentation.")
|
|
|
|
|
parser.add_argument('directory', metavar='DIR', nargs='*', default='',
|
|
|
|
|
help='directory to jump to')
|
|
|
|
@ -219,7 +224,8 @@ def options():
|
|
|
|
|
help='delete all database entries that no longer exist on system')
|
|
|
|
|
parser.add_argument('-s', '--stat', action="store_true", default=False,
|
|
|
|
|
help='show database entries and their key weights')
|
|
|
|
|
parser.add_argument('--version', action="version", version="%(prog)s " + VERSION,
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--version', action="version", version="%(prog)s " + VERSION,
|
|
|
|
|
help='show version information and exit')
|
|
|
|
|
|
|
|
|
|
ARGS = parser.parse_args()
|
|
|
|
@ -251,6 +257,7 @@ def options():
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def decode(text, encoding=None, errors="strict"):
|
|
|
|
|
"""
|
|
|
|
|
Decoding step for Python 2 which does not default to unicode.
|
|
|
|
@ -262,6 +269,7 @@ def decode(text, encoding=None, errors="strict"):
|
|
|
|
|
encoding = sys.getfilesystemencoding()
|
|
|
|
|
return text.decode(encoding, errors)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def output(unicode_text, encoding=None):
|
|
|
|
|
"""
|
|
|
|
|
Wrapper for the print function, using the filesystem encoding by default
|
|
|
|
@ -274,6 +282,7 @@ def output(unicode_text, encoding=None):
|
|
|
|
|
encoding = sys.getfilesystemencoding()
|
|
|
|
|
print(unicode_text.encode(encoding))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def unico(text):
|
|
|
|
|
"""
|
|
|
|
|
If Python 2, convert to a unicode object.
|
|
|
|
@ -283,6 +292,7 @@ def unico(text):
|
|
|
|
|
else:
|
|
|
|
|
return unicode(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def match_last(pattern):
|
|
|
|
|
"""
|
|
|
|
|
If the last pattern contains a full path, jump there.
|
|
|
|
@ -298,6 +308,7 @@ def match_last(pattern):
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def match(path, pattern, only_end=False, ignore_case=False):
|
|
|
|
|
"""
|
|
|
|
|
Check whether a path matches a particular pattern, and return
|
|
|
|
@ -319,6 +330,7 @@ def match(path, pattern, only_end=False, ignore_case=False):
|
|
|
|
|
else:
|
|
|
|
|
return (False, path[find_idx + len(pattern):])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
|
|
|
|
|
"""
|
|
|
|
|
Find max_matches paths that match the pattern, and add them to the result_list.
|
|
|
|
@ -370,7 +382,8 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
|
|
|
|
|
found, tmp = match(tmp, p, True, ignore_case)
|
|
|
|
|
else:
|
|
|
|
|
found, tmp = match(tmp, p, False, ignore_case)
|
|
|
|
|
if not found: break
|
|
|
|
|
if not found:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if found and (os.path.exists(path) or TESTING):
|
|
|
|
|
if path not in results:
|
|
|
|
@ -379,11 +392,13 @@ def find_matches(db, patterns, max_matches=1, ignore_case=False, fuzzy=False):
|
|
|
|
|
break
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def shell_utility():
|
|
|
|
|
"""
|
|
|
|
|
Run this when autojump is called as a shell utility.
|
|
|
|
|
"""
|
|
|
|
|
if options(): return True
|
|
|
|
|
if options():
|
|
|
|
|
return True
|
|
|
|
|
db = Database(DB_FILE)
|
|
|
|
|
|
|
|
|
|
# if no directories, add empty string
|
|
|
|
@ -401,7 +416,8 @@ def shell_utility():
|
|
|
|
|
tab_match = re.search(COMPLETION_SEPARATOR + "([0-9]+)", patterns[-1])
|
|
|
|
|
if tab_match: # user has selected a tab completion entry
|
|
|
|
|
tab_choice = int(tab_match.group(1))
|
|
|
|
|
patterns[-1] = re.sub(COMPLETION_SEPARATOR+"[0-9]+.*", "", patterns[-1])
|
|
|
|
|
patterns[-1] = re.sub(COMPLETION_SEPARATOR + "[0-9]+.*",
|
|
|
|
|
"", patterns[-1])
|
|
|
|
|
else: # user hasn't selected a tab completion, display choices again
|
|
|
|
|
tab_match = re.match("(.*)" + COMPLETION_SEPARATOR, patterns[-1])
|
|
|
|
|
if tab_match:
|
|
|
|
@ -426,7 +442,8 @@ def shell_utility():
|
|
|
|
|
results = find_matches(db, patterns, max_matches, True, True)
|
|
|
|
|
|
|
|
|
|
quotes = ""
|
|
|
|
|
if ARGS.complete and ARGS.bash: quotes = "'"
|
|
|
|
|
if ARGS.complete and ARGS.bash:
|
|
|
|
|
quotes = "'"
|
|
|
|
|
|
|
|
|
|
if tab_choice != -1:
|
|
|
|
|
if len(results) > tab_choice - 1:
|
|
|
|
@ -446,4 +463,5 @@ def shell_utility():
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
if not shell_utility(): sys.exit(1)
|
|
|
|
|
if not shell_utility():
|
|
|
|
|
sys.exit(1)
|
|
|
|
|