mirror of
https://github.com/wting/autojump
synced 2024-10-27 20:34:07 +00:00
move match last pattern into separate funciton and disabled
This commit is contained in:
parent
31001c9456
commit
7343ef1825
124
bin/autojump
124
bin/autojump
@ -34,6 +34,7 @@ class Database:
|
|||||||
self.data = {}
|
self.data = {}
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
|
# TODO: increase logarithmically as opposed to linear
|
||||||
def add(self, key, increment = 1):
|
def add(self, key, increment = 1):
|
||||||
""" Increment existing paths or initialize new ones to 0. """
|
""" Increment existing paths or initialize new ones to 0. """
|
||||||
self.data[key] = self.data.get(key, 0.) + increment
|
self.data[key] = self.data.get(key, 0.) + increment
|
||||||
@ -207,6 +208,19 @@ def unico(text):
|
|||||||
else:
|
else:
|
||||||
return unicode(text)
|
return unicode(text)
|
||||||
|
|
||||||
|
def match_last(pattern):
|
||||||
|
""" If the last pattern contains a full path, jump there.
|
||||||
|
The regexp is because we need to support stuff like
|
||||||
|
"j wo jo__3__/home/joel/workspace/joel" for zsh. """
|
||||||
|
last_pattern_path = re.sub("(.*)"+COMPLETION_SEPARATOR, "", pattern[-1])
|
||||||
|
if (len(last_pattern_path) > 0 and
|
||||||
|
last_pattern_path[0] == "/" and
|
||||||
|
os.path.exists(last_pattern_path)):
|
||||||
|
if not ARGS.preserve:
|
||||||
|
output(last_pattern_path)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def match(path, pattern, ignore_case=False, only_end=False):
|
def match(path, pattern, ignore_case=False, only_end=False):
|
||||||
""" Check whether a path matches a particular pattern, and return
|
""" Check whether a path matches a particular pattern, and return
|
||||||
the remaining part of the string. """
|
the remaining part of the string. """
|
||||||
@ -250,74 +264,70 @@ def shell_utility():
|
|||||||
options()
|
options()
|
||||||
db = Database(DB_FILE)
|
db = Database(DB_FILE)
|
||||||
|
|
||||||
#userchoice is i if the pattern is __pattern__i, otherwise -1
|
# if no directories, add empty string
|
||||||
userchoice = -1
|
|
||||||
results = []
|
|
||||||
|
|
||||||
if (ARGS.directory == ''):
|
if (ARGS.directory == ''):
|
||||||
patterns = [unico('')]
|
patterns = [unico('')]
|
||||||
else:
|
else:
|
||||||
patterns = [decode(a) for a in ARGS.directory]
|
patterns = [decode(a) for a in ARGS.directory]
|
||||||
|
|
||||||
# If the last pattern contains a full path, jump there
|
# check last pattern for full path
|
||||||
# The regexp is because we need to support stuff like
|
# FIXME: disabled until tab completion is fixed on the shell side
|
||||||
# "j wo jo__3__/home/joel/workspace/joel" for zsh
|
# if match_last(patterns): return True
|
||||||
last_pattern_path = re.sub("(.*)"+COMPLETION_SEPARATOR, "", patterns[-1])
|
|
||||||
if (len(last_pattern_path)>0 and
|
#userchoice is i if the pattern is __pattern__i, otherwise -1
|
||||||
last_pattern_path[0] == "/" and
|
userchoice = -1
|
||||||
os.path.exists(last_pattern_path)):
|
results = []
|
||||||
if not ARGS.complete: output(last_pattern_path)
|
|
||||||
|
#check for ongoing completion, and act accordingly
|
||||||
|
endmatch = re.search(COMPLETION_SEPARATOR+"([0-9]+)", patterns[-1])
|
||||||
|
if endmatch: #user has selected a completion
|
||||||
|
userchoice = int(endmatch.group(1))
|
||||||
|
patterns[-1] = re.sub(COMPLETION_SEPARATOR+"[0-9]+.*",
|
||||||
|
"", patterns[-1])
|
||||||
|
else: #user hasn't selected a completion, display the same choices again
|
||||||
|
endmatch = re.match("(.*)"+COMPLETION_SEPARATOR, patterns[-1])
|
||||||
|
if endmatch: patterns[-1] = endmatch.group(1)
|
||||||
|
|
||||||
|
dirs = list(db.data.items())
|
||||||
|
dirs.sort(key=itemgetter(1), reverse=True)
|
||||||
|
if ARGS.complete or userchoice != -1:
|
||||||
|
max_matches = 9
|
||||||
else:
|
else:
|
||||||
#check for ongoing completion, and act accordingly
|
max_matches = 1
|
||||||
endmatch = re.search(COMPLETION_SEPARATOR+"([0-9]+)", patterns[-1])
|
|
||||||
if endmatch: #user has selected a completion
|
|
||||||
userchoice = int(endmatch.group(1))
|
|
||||||
patterns[-1] = re.sub(COMPLETION_SEPARATOR+"[0-9]+.*",
|
|
||||||
"", patterns[-1])
|
|
||||||
else: #user hasn't selected a completion, display the same choices again
|
|
||||||
endmatch = re.match("(.*)"+COMPLETION_SEPARATOR, patterns[-1])
|
|
||||||
if endmatch: patterns[-1] = endmatch.group(1)
|
|
||||||
|
|
||||||
dirs = list(db.data.items())
|
# Don't jump to the current directory
|
||||||
dirs.sort(key=itemgetter(1), reverse=True)
|
try:
|
||||||
if ARGS.complete or userchoice != -1:
|
current_dir = decode(os.path.realpath(os.curdir))
|
||||||
max_matches = 9
|
#Sometimes the current path doesn't exist anymore.
|
||||||
else:
|
#In that case, jump if possible.
|
||||||
max_matches = 1
|
except OSError:
|
||||||
|
current_dir = None
|
||||||
|
find_matches(dirs, patterns, results, False, max_matches, current_dir)
|
||||||
|
# If not found, try ignoring case.
|
||||||
|
# On completion always show all results
|
||||||
|
if ARGS.complete or not results:
|
||||||
|
find_matches(dirs, patterns, results,
|
||||||
|
ignore_case=True,
|
||||||
|
max_matches=max_matches, current_dir=current_dir)
|
||||||
|
|
||||||
# Don't jump to the current directory
|
if ARGS.complete and ARGS.bash: quotes = "'"
|
||||||
try:
|
else: quotes = ""
|
||||||
current_dir = decode(os.path.realpath(os.curdir))
|
|
||||||
#Sometimes the current path doesn't exist anymore.
|
|
||||||
#In that case, jump if possible.
|
|
||||||
except OSError:
|
|
||||||
current_dir = None
|
|
||||||
find_matches(dirs, patterns, results, False, max_matches, current_dir)
|
|
||||||
# If not found, try ignoring case.
|
|
||||||
# On completion always show all results
|
|
||||||
if ARGS.complete or not results:
|
|
||||||
find_matches(dirs, patterns, results,
|
|
||||||
ignore_case=True,
|
|
||||||
max_matches=max_matches, current_dir=current_dir)
|
|
||||||
|
|
||||||
if ARGS.complete and ARGS.bash: quotes = "'"
|
if userchoice != -1:
|
||||||
else: quotes = ""
|
if len(results) > userchoice-1 :
|
||||||
|
output(unico("%s%s%s") % (quotes,results[userchoice-1],quotes))
|
||||||
|
elif len(results) > 1 and ARGS.complete:
|
||||||
|
output("\n".join(("%s%s%d%s%s" % (patterns[-1],
|
||||||
|
COMPLETION_SEPARATOR, n+1, COMPLETION_SEPARATOR, r)
|
||||||
|
for n, r in enumerate(results[:8]))))
|
||||||
|
elif results: output(unico("%s%s%s")%(quotes,results[0],quotes))
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
if userchoice != -1:
|
if not ARGS.preserve:
|
||||||
if len(results) > userchoice-1 :
|
db.maintenance()
|
||||||
output(unico("%s%s%s") % (quotes,results[userchoice-1],quotes))
|
|
||||||
elif len(results) > 1 and ARGS.complete:
|
|
||||||
output("\n".join(("%s%s%d%s%s" % (patterns[-1],
|
|
||||||
COMPLETION_SEPARATOR, n+1, COMPLETION_SEPARATOR, r)
|
|
||||||
for n, r in enumerate(results[:8]))))
|
|
||||||
elif results: output(unico("%s%s%s")%(quotes,results[0],quotes))
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if not ARGS.preserve:
|
return True
|
||||||
db.maintenance()
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if not shell_utility(): sys.exit(1)
|
if not shell_utility(): sys.exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user