2010-12-02 16:46:23 +00:00
|
|
|
#!/usr/bin/env python
|
2012-05-07 06:50:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-05-07 06:19:19 +00:00
|
|
|
"""
|
|
|
|
Copyright © 2008-2012 Joel Schaerer
|
2016-05-04 06:44:43 +00:00
|
|
|
Copyright © 2012-2016 William Ting
|
2012-05-07 06:19:19 +00:00
|
|
|
|
2013-12-28 17:34:13 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2012-05-07 06:19:19 +00:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
2013-12-28 17:34:13 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2012-05-07 06:19:19 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2013-12-28 17:34:13 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2012-05-07 06:19:19 +00:00
|
|
|
"""
|
2013-12-17 18:51:39 +00:00
|
|
|
from __future__ import print_function
|
2010-10-24 09:30:01 +00:00
|
|
|
|
2017-07-20 06:15:06 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2013-12-17 19:52:34 +00:00
|
|
|
from itertools import chain
|
2013-12-17 02:28:54 +00:00
|
|
|
from math import sqrt
|
|
|
|
from operator import attrgetter
|
|
|
|
from operator import itemgetter
|
2011-09-06 14:21:59 +00:00
|
|
|
|
2013-12-18 22:51:26 +00:00
|
|
|
if sys.version_info[0] == 3:
|
|
|
|
ifilter = filter
|
|
|
|
imap = map
|
|
|
|
os.getcwdu = os.getcwd
|
|
|
|
else:
|
|
|
|
from itertools import ifilter
|
|
|
|
from itertools import imap
|
|
|
|
|
2016-05-04 06:32:38 +00:00
|
|
|
# Vendorized argparse for Python 2.6 support
|
2014-01-07 15:27:05 +00:00
|
|
|
from autojump_argparse import ArgumentParser
|
|
|
|
|
2016-05-04 06:38:31 +00:00
|
|
|
# autojump is not a standard python package but rather installed as a mixed
|
|
|
|
# shell + Python app with no outside dependencies (except Python). As a
|
|
|
|
# consequence we use relative imports and depend on file prefixes to prevent
|
|
|
|
# module conflicts.
|
2013-12-30 20:49:34 +00:00
|
|
|
from autojump_data import dictify
|
|
|
|
from autojump_data import entriefy
|
|
|
|
from autojump_data import Entry
|
|
|
|
from autojump_data import load
|
|
|
|
from autojump_data import save
|
2016-05-04 06:55:01 +00:00
|
|
|
from autojump_match import match_anywhere
|
|
|
|
from autojump_match import match_consecutive
|
|
|
|
from autojump_match import match_fuzzy
|
2013-12-30 20:49:34 +00:00
|
|
|
from autojump_utils import first
|
|
|
|
from autojump_utils import get_pwd
|
2014-10-05 02:41:41 +00:00
|
|
|
from autojump_utils import get_tab_entry_info
|
2013-12-30 20:49:34 +00:00
|
|
|
from autojump_utils import has_uppercase
|
2014-10-05 02:41:41 +00:00
|
|
|
from autojump_utils import is_autojump_sourced
|
2013-12-30 20:49:34 +00:00
|
|
|
from autojump_utils import is_osx
|
2014-01-12 19:34:28 +00:00
|
|
|
from autojump_utils import is_windows
|
2013-12-30 20:49:34 +00:00
|
|
|
from autojump_utils import last
|
|
|
|
from autojump_utils import print_entry
|
2014-01-07 17:44:44 +00:00
|
|
|
from autojump_utils import print_local
|
2013-12-30 20:49:34 +00:00
|
|
|
from autojump_utils import print_tab_menu
|
|
|
|
from autojump_utils import sanitize
|
|
|
|
from autojump_utils import take
|
2014-01-07 17:44:44 +00:00
|
|
|
from autojump_utils import unico
|
2013-05-21 14:28:39 +00:00
|
|
|
|
2018-09-07 21:28:09 +00:00
|
|
|
VERSION = '22.5.2'
|
2013-12-17 21:57:36 +00:00
|
|
|
FUZZY_MATCH_THRESHOLD = 0.6
|
2013-12-18 18:03:43 +00:00
|
|
|
TAB_ENTRIES_COUNT = 9
|
2013-12-18 17:08:05 +00:00
|
|
|
TAB_SEPARATOR = '__'
|
2013-09-26 20:40:34 +00:00
|
|
|
|
2013-07-07 01:23:34 +00:00
|
|
|
|
2013-05-14 22:34:19 +00:00
|
|
|
def set_defaults():
|
|
|
|
config = {}
|
2012-05-06 23:12:39 +00:00
|
|
|
|
2013-12-17 02:28:54 +00:00
|
|
|
if is_osx():
|
2017-10-05 17:26:59 +00:00
|
|
|
data_home = os.path.join(os.path.expanduser('~'), 'Library')
|
2014-01-12 19:34:28 +00:00
|
|
|
elif is_windows():
|
2017-10-05 17:26:59 +00:00
|
|
|
data_home = os.getenv('APPDATA')
|
2013-12-17 02:28:54 +00:00
|
|
|
else:
|
|
|
|
data_home = os.getenv(
|
2014-08-10 02:59:41 +00:00
|
|
|
'XDG_DATA_HOME',
|
|
|
|
os.path.join(
|
|
|
|
os.path.expanduser('~'),
|
|
|
|
'.local',
|
|
|
|
'share',
|
2017-07-20 06:17:11 +00:00
|
|
|
),
|
|
|
|
)
|
2017-10-05 17:26:59 +00:00
|
|
|
config['data_path'] = os.path.join(data_home, 'autojump', 'autojump.txt')
|
|
|
|
config['backup_path'] = os.path.join(data_home, 'autojump', 'autojump.txt.bak')
|
2013-05-14 22:34:19 +00:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2013-12-17 19:52:34 +00:00
|
|
|
def parse_arguments():
|
2013-12-17 02:28:54 +00:00
|
|
|
parser = ArgumentParser(
|
2014-10-05 01:45:36 +00:00
|
|
|
description='Automatically jump to directory passed as an argument.',
|
2017-07-20 06:17:11 +00:00
|
|
|
epilog='Please see autojump(1) man pages for full documentation.',
|
|
|
|
)
|
2013-02-25 05:49:45 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'directory', metavar='DIRECTORY', nargs='*', default='',
|
2017-07-20 06:17:11 +00:00
|
|
|
help='directory to jump to',
|
|
|
|
)
|
2013-02-25 05:49:45 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'-a', '--add', metavar='DIRECTORY',
|
2017-07-20 06:17:11 +00:00
|
|
|
help='add path',
|
|
|
|
)
|
2013-05-15 00:03:08 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'-i', '--increase', metavar='WEIGHT', nargs='?', type=int,
|
|
|
|
const=10, default=False,
|
2017-07-20 06:17:11 +00:00
|
|
|
help='increase current directory weight',
|
|
|
|
)
|
2013-02-25 05:49:45 +00:00
|
|
|
parser.add_argument(
|
2014-08-10 02:59:41 +00:00
|
|
|
'-d', '--decrease', metavar='WEIGHT', nargs='?', type=int,
|
|
|
|
const=15, default=False,
|
2017-07-20 06:17:11 +00:00
|
|
|
help='decrease current directory weight',
|
|
|
|
)
|
2013-12-17 22:46:01 +00:00
|
|
|
parser.add_argument(
|
2017-07-20 06:13:01 +00:00
|
|
|
'--complete', action='store_true', default=False,
|
2017-07-20 06:17:11 +00:00
|
|
|
help='used for tab completion',
|
|
|
|
)
|
2013-02-25 05:49:45 +00:00
|
|
|
parser.add_argument(
|
2017-07-20 06:13:01 +00:00
|
|
|
'--purge', action='store_true', default=False,
|
2017-07-20 06:17:11 +00:00
|
|
|
help='remove non-existent paths from database',
|
|
|
|
)
|
2013-02-25 05:49:45 +00:00
|
|
|
parser.add_argument(
|
2017-07-20 06:13:01 +00:00
|
|
|
'-s', '--stat', action='store_true', default=False,
|
2017-07-20 06:17:11 +00:00
|
|
|
help='show database entries and their key weights',
|
|
|
|
)
|
2013-02-25 05:49:45 +00:00
|
|
|
parser.add_argument(
|
2017-07-20 06:13:01 +00:00
|
|
|
'-v', '--version', action='version', version='%(prog)s v' +
|
2017-07-20 06:17:11 +00:00
|
|
|
VERSION, help='show version information',
|
|
|
|
)
|
2013-05-14 22:34:19 +00:00
|
|
|
|
2013-12-17 19:52:34 +00:00
|
|
|
return parser.parse_args()
|
2013-05-15 02:58:00 +00:00
|
|
|
|
2012-05-07 01:09:37 +00:00
|
|
|
|
2013-12-30 03:27:13 +00:00
|
|
|
def add_path(data, path, weight=10):
|
2013-12-17 15:52:41 +00:00
|
|
|
"""
|
|
|
|
Add a new path or increment an existing one.
|
|
|
|
|
2013-12-30 03:27:13 +00:00
|
|
|
os.path.realpath() is not used because it's preferable to use symlinks
|
|
|
|
with resulting duplicate entries in the database than a single canonical
|
|
|
|
path.
|
2013-12-17 15:52:41 +00:00
|
|
|
"""
|
2014-01-07 17:44:44 +00:00
|
|
|
path = unico(path).rstrip(os.sep)
|
2013-12-17 02:28:54 +00:00
|
|
|
if path == os.path.expanduser('~'):
|
2013-12-17 20:48:12 +00:00
|
|
|
return data, Entry(path, 0)
|
2013-12-17 02:28:54 +00:00
|
|
|
|
2013-12-30 03:27:13 +00:00
|
|
|
data[path] = sqrt((data.get(path, 0) ** 2) + (weight ** 2))
|
2010-07-21 14:44:43 +00:00
|
|
|
|
2013-12-17 20:48:12 +00:00
|
|
|
return data, Entry(path, data[path])
|
2012-05-07 00:34:03 +00:00
|
|
|
|
2012-04-07 14:14:19 +00:00
|
|
|
|
2013-12-30 03:27:13 +00:00
|
|
|
def decrease_path(data, path, weight=15):
|
|
|
|
"""Decrease or zero out a path."""
|
2014-01-07 17:44:44 +00:00
|
|
|
path = unico(path).rstrip(os.sep)
|
2013-12-30 03:27:13 +00:00
|
|
|
data[path] = max(0, data.get(path, 0) - weight)
|
2013-12-17 20:48:12 +00:00
|
|
|
return data, Entry(path, data[path])
|
2013-12-17 02:28:54 +00:00
|
|
|
|
|
|
|
|
2013-12-17 18:04:11 +00:00
|
|
|
def detect_smartcase(needles):
|
|
|
|
"""
|
|
|
|
If any needles contain an uppercase letter then use case sensitive
|
|
|
|
searching. Otherwise use case insensitive searching.
|
|
|
|
"""
|
|
|
|
return not any(imap(has_uppercase, needles))
|
|
|
|
|
|
|
|
|
2014-01-29 05:55:15 +00:00
|
|
|
def find_matches(entries, needles, check_entries=True):
|
2013-12-17 19:52:34 +00:00
|
|
|
"""Return an iterator to matching entries."""
|
2014-02-24 13:54:47 +00:00
|
|
|
# TODO(wting|2014-02-24): replace assertion with unit test
|
2017-07-20 06:13:01 +00:00
|
|
|
assert isinstance(needles, list), 'Needles must be a list.'
|
2014-01-29 05:55:15 +00:00
|
|
|
ignore_case = detect_smartcase(needles)
|
|
|
|
|
2013-12-17 20:48:12 +00:00
|
|
|
try:
|
2014-01-29 05:55:15 +00:00
|
|
|
pwd = os.getcwdu()
|
2013-12-17 20:48:12 +00:00
|
|
|
except OSError:
|
2014-01-29 05:55:15 +00:00
|
|
|
pwd = None
|
|
|
|
|
2014-03-05 05:01:11 +00:00
|
|
|
# using closure to prevent constantly hitting hdd
|
|
|
|
def is_cwd(entry):
|
2014-06-23 20:19:14 +00:00
|
|
|
return os.path.realpath(entry.path) == pwd
|
2014-01-29 05:55:15 +00:00
|
|
|
|
|
|
|
if check_entries:
|
2014-03-05 05:01:11 +00:00
|
|
|
path_exists = lambda entry: os.path.exists(entry.path)
|
2014-01-29 05:55:15 +00:00
|
|
|
else:
|
2014-03-05 05:01:11 +00:00
|
|
|
path_exists = lambda _: True
|
2013-12-17 20:48:12 +00:00
|
|
|
|
2013-12-17 02:28:54 +00:00
|
|
|
data = sorted(
|
2014-08-10 02:59:41 +00:00
|
|
|
entries,
|
2016-01-06 11:40:09 +00:00
|
|
|
key=attrgetter('weight', 'path'),
|
2017-07-20 06:17:11 +00:00
|
|
|
reverse=True,
|
|
|
|
)
|
2013-12-17 02:28:54 +00:00
|
|
|
|
2013-12-17 21:57:36 +00:00
|
|
|
return ifilter(
|
2014-08-10 02:59:41 +00:00
|
|
|
lambda entry: not is_cwd(entry) and path_exists(entry),
|
|
|
|
chain(
|
|
|
|
match_consecutive(needles, data, ignore_case),
|
|
|
|
match_fuzzy(needles, data, ignore_case),
|
2017-07-20 06:17:11 +00:00
|
|
|
match_anywhere(needles, data, ignore_case),
|
|
|
|
),
|
|
|
|
)
|
2013-12-17 22:25:45 +00:00
|
|
|
|
|
|
|
|
2013-12-30 23:44:39 +00:00
|
|
|
def handle_tab_completion(needle, entries):
|
2013-12-31 16:39:52 +00:00
|
|
|
tab_needle, tab_index, tab_path = get_tab_entry_info(needle, TAB_SEPARATOR)
|
2013-12-28 18:15:07 +00:00
|
|
|
|
2013-12-31 16:39:52 +00:00
|
|
|
if tab_path:
|
2014-01-07 17:44:44 +00:00
|
|
|
print_local(tab_path)
|
2013-12-31 16:39:52 +00:00
|
|
|
elif tab_index:
|
|
|
|
get_ith_path = lambda i, iterable: last(take(i, iterable)).path
|
2014-01-29 05:55:15 +00:00
|
|
|
print_local(get_ith_path(
|
|
|
|
tab_index,
|
2017-07-20 06:17:11 +00:00
|
|
|
find_matches(entries, [tab_needle], check_entries=False),
|
|
|
|
))
|
2013-12-28 18:15:07 +00:00
|
|
|
elif tab_needle:
|
|
|
|
# found partial tab completion entry
|
|
|
|
print_tab_menu(
|
2014-08-10 02:59:41 +00:00
|
|
|
tab_needle,
|
2017-07-20 06:17:11 +00:00
|
|
|
take(
|
|
|
|
TAB_ENTRIES_COUNT, find_matches(
|
|
|
|
entries,
|
|
|
|
[tab_needle],
|
|
|
|
check_entries=False,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
TAB_SEPARATOR,
|
|
|
|
)
|
2013-12-28 18:15:07 +00:00
|
|
|
else:
|
2013-12-30 23:44:39 +00:00
|
|
|
print_tab_menu(
|
2014-08-10 02:59:41 +00:00
|
|
|
needle,
|
2017-07-20 06:17:11 +00:00
|
|
|
take(
|
|
|
|
TAB_ENTRIES_COUNT, find_matches(
|
|
|
|
entries,
|
|
|
|
[needle],
|
|
|
|
check_entries=False,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
TAB_SEPARATOR,
|
|
|
|
)
|
2013-12-30 23:44:39 +00:00
|
|
|
|
2013-12-28 18:15:07 +00:00
|
|
|
|
2013-12-17 20:48:12 +00:00
|
|
|
def purge_missing_paths(entries):
|
|
|
|
"""Remove non-existent paths from a list of entries."""
|
|
|
|
exists = lambda entry: os.path.exists(entry.path)
|
|
|
|
return ifilter(exists, entries)
|
2013-12-17 02:28:54 +00:00
|
|
|
|
|
|
|
|
2013-12-17 20:48:12 +00:00
|
|
|
def print_stats(data, data_path):
|
2013-12-18 22:51:26 +00:00
|
|
|
for path, weight in sorted(data.items(), key=itemgetter(1)):
|
2013-12-17 20:48:12 +00:00
|
|
|
print_entry(Entry(path, weight))
|
2013-12-17 02:28:54 +00:00
|
|
|
|
2017-07-20 06:13:01 +00:00
|
|
|
print('________________________________________\n')
|
|
|
|
print('%d:\t total weight' % sum(data.values()))
|
|
|
|
print('%d:\t number of entries' % len(data))
|
2013-12-17 02:28:54 +00:00
|
|
|
|
|
|
|
try:
|
2014-01-07 17:44:44 +00:00
|
|
|
print_local(
|
2017-07-20 06:17:11 +00:00
|
|
|
'%.2f:\t current directory weight' % data.get(os.getcwdu(), 0),
|
|
|
|
)
|
2013-12-17 02:28:54 +00:00
|
|
|
except OSError:
|
2013-12-18 22:51:26 +00:00
|
|
|
# current directory no longer exists
|
2013-12-17 02:28:54 +00:00
|
|
|
pass
|
|
|
|
|
2017-07-20 06:13:01 +00:00
|
|
|
print('\ndata:\t %s' % data_path)
|
2013-12-17 02:28:54 +00:00
|
|
|
|
|
|
|
|
2014-01-07 15:27:05 +00:00
|
|
|
def main(args): # noqa
|
2014-10-05 02:41:41 +00:00
|
|
|
if not is_autojump_sourced() and not is_windows():
|
|
|
|
print("Please source the correct autojump file in your shell's")
|
2017-07-20 06:13:01 +00:00
|
|
|
print('startup file. For more information, please reinstall autojump')
|
|
|
|
print('and read the post installation instructions.')
|
2014-10-05 02:41:41 +00:00
|
|
|
return 1
|
|
|
|
|
2013-12-17 22:30:46 +00:00
|
|
|
config = set_defaults()
|
2014-03-10 02:41:44 +00:00
|
|
|
|
2013-12-28 18:15:07 +00:00
|
|
|
# all arguments are mutually exclusive
|
2013-12-17 19:52:34 +00:00
|
|
|
if args.add:
|
2013-12-17 20:48:12 +00:00
|
|
|
save(config, first(add_path(load(config), args.add)))
|
2013-12-17 22:46:01 +00:00
|
|
|
elif args.complete:
|
2013-12-30 23:44:39 +00:00
|
|
|
handle_tab_completion(
|
2014-08-10 02:59:41 +00:00
|
|
|
needle=first(chain(sanitize(args.directory), [''])),
|
2017-07-20 06:17:11 +00:00
|
|
|
entries=entriefy(load(config)),
|
|
|
|
)
|
2013-12-17 19:52:34 +00:00
|
|
|
elif args.decrease:
|
2013-12-17 20:48:12 +00:00
|
|
|
data, entry = decrease_path(load(config), get_pwd(), args.decrease)
|
|
|
|
save(config, data)
|
|
|
|
print_entry(entry)
|
2013-12-17 19:52:34 +00:00
|
|
|
elif args.increase:
|
2013-12-18 22:51:26 +00:00
|
|
|
data, entry = add_path(load(config), get_pwd(), args.increase)
|
2013-12-17 20:48:12 +00:00
|
|
|
save(config, data)
|
|
|
|
print_entry(entry)
|
2013-12-17 19:52:34 +00:00
|
|
|
elif args.purge:
|
2013-12-17 20:48:12 +00:00
|
|
|
old_data = load(config)
|
|
|
|
new_data = dictify(purge_missing_paths(entriefy(old_data)))
|
|
|
|
save(config, new_data)
|
2017-07-20 06:13:01 +00:00
|
|
|
print('Purged %d entries.' % (len(old_data) - len(new_data)))
|
2013-12-17 19:52:34 +00:00
|
|
|
elif args.stat:
|
2013-12-17 20:48:12 +00:00
|
|
|
print_stats(load(config), config['data_path'])
|
2013-12-28 18:15:07 +00:00
|
|
|
elif not args.directory:
|
2014-06-25 14:18:28 +00:00
|
|
|
# Return best match.
|
|
|
|
entries = entriefy(load(config))
|
|
|
|
print_local(first(chain(
|
2014-08-10 02:59:41 +00:00
|
|
|
imap(attrgetter('path'), find_matches(entries, [''])),
|
|
|
|
# always return a path to calling shell functions
|
2017-07-20 06:17:11 +00:00
|
|
|
['.'],
|
|
|
|
)))
|
2013-12-17 19:52:34 +00:00
|
|
|
else:
|
2013-12-18 17:08:05 +00:00
|
|
|
entries = entriefy(load(config))
|
|
|
|
needles = sanitize(args.directory)
|
2013-12-31 16:39:52 +00:00
|
|
|
tab_needle, tab_index, tab_path = \
|
|
|
|
get_tab_entry_info(first(needles), TAB_SEPARATOR)
|
|
|
|
|
2014-06-23 16:39:07 +00:00
|
|
|
# Handle `j foo__`, assuming first index.
|
|
|
|
if not tab_path and not tab_index \
|
|
|
|
and tab_needle and needles[0] == tab_needle + TAB_SEPARATOR:
|
|
|
|
tab_index = 1
|
|
|
|
|
2013-12-31 16:39:52 +00:00
|
|
|
if tab_path:
|
2014-01-07 17:44:44 +00:00
|
|
|
print_local(tab_path)
|
2013-12-31 16:39:52 +00:00
|
|
|
elif tab_index:
|
|
|
|
get_ith_path = lambda i, iterable: last(take(i, iterable)).path
|
2014-01-07 17:44:44 +00:00
|
|
|
print_local(
|
2014-08-10 02:59:41 +00:00
|
|
|
get_ith_path(
|
|
|
|
tab_index,
|
2017-07-20 06:17:11 +00:00
|
|
|
find_matches(entries, [tab_needle]),
|
|
|
|
),
|
|
|
|
)
|
2013-12-30 23:44:39 +00:00
|
|
|
else:
|
2014-03-26 17:15:37 +00:00
|
|
|
print_local(first(chain(
|
2014-08-10 02:59:41 +00:00
|
|
|
imap(attrgetter('path'), find_matches(entries, needles)),
|
|
|
|
# always return a path to calling shell functions
|
2017-07-20 06:17:11 +00:00
|
|
|
['.'],
|
|
|
|
)))
|
2013-12-17 19:52:34 +00:00
|
|
|
|
|
|
|
return 0
|
2013-12-17 18:03:57 +00:00
|
|
|
|
2012-04-07 14:14:19 +00:00
|
|
|
|
2017-07-20 06:13:01 +00:00
|
|
|
if __name__ == '__main__':
|
2013-12-18 22:25:46 +00:00
|
|
|
sys.exit(main(parse_arguments()))
|