| 
									
										
										
										
											2010-12-02 16:46:23 +00:00
										 |  |  | #!/usr/bin/env python | 
					
						
							| 
									
										
										
										
											2010-10-24 09:30:01 +00:00
										 |  |  | from __future__ import division, print_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2011-09-12 14:42:40 +00:00
										 |  |  | from sys import argv, stderr, version_info, exit, getfilesystemencoding | 
					
						
							| 
									
										
										
										
											2010-09-29 13:22:13 +00:00
										 |  |  | from tempfile import NamedTemporaryFile | 
					
						
							| 
									
										
										
										
											2010-11-18 15:36:38 +00:00
										 |  |  | from operator import itemgetter | 
					
						
							| 
									
										
										
										
											2008-04-21 09:43:07 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2011-09-27 13:47:24 +00:00
										 |  |  | import shutil | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-11 00:43:06 +00:00
										 |  |  | AUTOJUMP_VERSION = "release-v20" | 
					
						
							| 
									
										
										
										
											2011-01-03 22:23:26 +00:00
										 |  |  | MAX_KEYWEIGHT = 1000 | 
					
						
							|  |  |  | MAX_STORED_PATHS = 600 | 
					
						
							|  |  |  | COMPLETION_SEPARATOR = '__' | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-07-24 19:16:36 +00:00
										 |  |  | 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') | 
					
						
							|  |  |  |     CONFIG_DIR=os.path.join(xdg_data_dir, 'autojump') | 
					
						
							| 
									
										
										
										
											2008-06-02 15:43:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-03 22:23:26 +00:00
										 |  |  | def uniqadd(collection, key): | 
					
						
							|  |  |  |     """Adds a key to a list only if it is not already present""" | 
					
						
							|  |  |  |     if key not in collection: | 
					
						
							|  |  |  |         collection.append(key) | 
					
						
							| 
									
										
										
										
											2009-02-13 22:22:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  | def dicadd(dic, key, increment=1): | 
					
						
							| 
									
										
										
										
											2011-01-03 22:23:26 +00:00
										 |  |  |     """Increment a value in a dic, set it to 0 | 
					
						
							|  |  |  |     if is is not already present""" | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  |     dic[key] = dic.get(key, 0.)+increment | 
					
						
							| 
									
										
										
										
											2008-04-21 09:43:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-12 14:42:40 +00:00
										 |  |  | def output(unicode_text,encoding=None): | 
					
						
							|  |  |  |     """Wrapper for the print function, using the filesystem encoding by default | 
					
						
							|  |  |  |     to minimize encoding mismatch problems in directory names""" | 
					
						
							| 
									
										
										
										
											2011-09-12 15:04:37 +00:00
										 |  |  |     if version_info[0] > 2: | 
					
						
							|  |  |  |         print(unicode_text) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if encoding is None: | 
					
						
							|  |  |  |             encoding = getfilesystemencoding() | 
					
						
							|  |  |  |         print(unicode_text.encode(encoding)) | 
					
						
							| 
									
										
										
										
											2011-09-12 14:42:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def decode(text,encoding=None,errors="strict"): | 
					
						
							|  |  |  |     """Decoding step for python2.x which does not default to unicode""" | 
					
						
							|  |  |  |     if version_info[0] > 2: | 
					
						
							|  |  |  |         return text | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if encoding is None: | 
					
						
							|  |  |  |             encoding = getfilesystemencoding() | 
					
						
							|  |  |  |         return text.decode(encoding,errors) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-12 15:04:37 +00:00
										 |  |  | def unico(text): | 
					
						
							|  |  |  |     """if python2, convert to a unicode object""" | 
					
						
							|  |  |  |     if version_info[0] > 2: | 
					
						
							|  |  |  |         return text | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return unicode(text) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  | def save(path_dict, dic_file): | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |     """Save the database in an atomic way, and preserve | 
					
						
							|  |  |  |        a backup file.""" | 
					
						
							| 
									
										
										
										
											2011-09-27 13:47:24 +00:00
										 |  |  |     # If the dic_file exists and os supports permissions, check that dic_file belongs to us | 
					
						
							| 
									
										
										
										
											2011-03-30 18:03:38 +00:00
										 |  |  |     # Otherwise, fail quietly | 
					
						
							| 
									
										
										
										
											2011-09-15 08:05:51 +00:00
										 |  |  |     if (not os.path.exists(dic_file)) or os.name == 'nt' or os.getuid() == os.stat(dic_file)[4]: | 
					
						
							| 
									
										
										
										
											2011-03-30 18:03:38 +00:00
										 |  |  |         temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False) | 
					
						
							| 
									
										
										
										
											2011-09-13 13:28:12 +00:00
										 |  |  |         for path,weight in sorted(path_dict.items(),key=itemgetter(1),reverse=True): | 
					
						
							| 
									
										
										
										
											2011-09-12 14:42:40 +00:00
										 |  |  |             # the db is stored in utf-8 | 
					
						
							| 
									
										
										
										
											2011-09-13 13:28:12 +00:00
										 |  |  |             temp.write((unico("%s\t%s\n")%(weight,path)).encode("utf-8")) | 
					
						
							| 
									
										
										
										
											2012-02-08 03:07:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Catching disk errors and skipping save since file handle can't be closed. | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ | 
					
						
							|  |  |  |             temp.flush() | 
					
						
							|  |  |  |             os.fsync(temp) | 
					
						
							|  |  |  |             temp.close() | 
					
						
							|  |  |  |         except IOError as ex: | 
					
						
							| 
									
										
										
										
											2012-02-08 07:03:44 +00:00
										 |  |  |             print("Error while saving autojump database (disk full?)" % | 
					
						
							| 
									
										
										
										
											2012-02-08 03:07:28 +00:00
										 |  |  |                     ex, file=stderr) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-27 13:47:24 +00:00
										 |  |  |         # Use shutil.move instead of os.rename because windows doesn't support | 
					
						
							|  |  |  |         # using rename to overwrite files | 
					
						
							| 
									
										
										
										
											2011-09-21 02:26:49 +00:00
										 |  |  |         shutil.move(temp.name, dic_file) | 
					
						
							| 
									
										
										
										
											2011-03-30 18:03:38 +00:00
										 |  |  |         try: #backup file | 
					
						
							| 
									
										
										
										
											2012-01-13 21:05:43 +00:00
										 |  |  |             import time | 
					
						
							| 
									
										
										
										
											2011-03-30 18:03:38 +00:00
										 |  |  |             if (not os.path.exists(dic_file+".bak") or | 
					
						
							|  |  |  |                     time.time()-os.path.getmtime(dic_file+".bak")>86400): | 
					
						
							|  |  |  |                 shutil.copy(dic_file, dic_file+".bak") | 
					
						
							|  |  |  |         except OSError as ex: | 
					
						
							|  |  |  |             print("Error while creating backup autojump file. (%s)" % | 
					
						
							|  |  |  |                     ex, file=stderr) | 
					
						
							| 
									
										
										
										
											2009-02-18 14:13:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  | def open_dic(dic_file, error_recovery=False): | 
					
						
							|  |  |  |     """Try hard to open the database file, recovering | 
					
						
							|  |  |  |        from backup if needed. """ | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         path_dict = {} | 
					
						
							| 
									
										
										
										
											2011-09-10 18:20:08 +00:00
										 |  |  |         with open(dic_file, 'r') as aj_file: | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  |             for l in aj_file.readlines(): | 
					
						
							|  |  |  |                 weight,path = l[:-1].split("\t",1) | 
					
						
							| 
									
										
										
										
											2011-09-12 14:42:40 +00:00
										 |  |  |                 # the db is stored in utf-8 | 
					
						
							|  |  |  |                 path = decode(path,"utf-8") | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  |                 path_dict[path] = float(weight) | 
					
						
							|  |  |  |             return path_dict | 
					
						
							|  |  |  |     except (IOError, EOFError): | 
					
						
							|  |  |  |         if not error_recovery and os.path.exists(dic_file+".bak"): | 
					
						
							|  |  |  |             print('Problem with autojump database,\ | 
					
						
							|  |  |  |                     trying to recover from backup...', file=stderr) | 
					
						
							|  |  |  |             shutil.copy(dic_file+".bak", dic_file) | 
					
						
							|  |  |  |             return open_dic(dic_file, True) | 
					
						
							| 
									
										
										
										
											2011-09-10 18:20:08 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             # Temporary migration code | 
					
						
							|  |  |  |             old_dic_file = get_dic_file("autojump_py") | 
					
						
							|  |  |  |             if os.path.exists(old_dic_file): | 
					
						
							|  |  |  |                 try: # fix to get optimised pickle in python < 3 | 
					
						
							|  |  |  |                     import cPickle as pickle | 
					
						
							|  |  |  |                 except ImportError: | 
					
						
							|  |  |  |                     import pickle | 
					
						
							|  |  |  |                 try: | 
					
						
							|  |  |  |                     with open(old_dic_file, 'rb') as aj_file: | 
					
						
							|  |  |  |                         if version_info[0] > 2: | 
					
						
							|  |  |  |                             #encoding is only specified for python2.x compatibility | 
					
						
							|  |  |  |                             path_dict = pickle.load(aj_file, encoding="utf-8") | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             path_dict = pickle.load(aj_file) | 
					
						
							| 
									
										
										
										
											2011-09-12 14:42:40 +00:00
										 |  |  |                     unicode_dict = {} #we now use unicode internally | 
					
						
							|  |  |  |                     for k,v in path_dict.items(): | 
					
						
							|  |  |  |                         print(k) | 
					
						
							|  |  |  |                         unicode_dict[decode(k,errors="replace")] = v | 
					
						
							|  |  |  |                     return unicode_dict | 
					
						
							| 
									
										
										
										
											2011-09-10 18:20:08 +00:00
										 |  |  |                 except (IOError, EOFError, pickle.UnpicklingError): | 
					
						
							|  |  |  |                     pass | 
					
						
							|  |  |  |             return {} #if everything fails, return an empty file | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  | def forget(path_dict, dic_file): | 
					
						
							|  |  |  |     """Gradually forget about directories. Only call | 
					
						
							|  |  |  |     from the actual jump since it can take time""" | 
					
						
							| 
									
										
										
										
											2012-01-13 21:05:43 +00:00
										 |  |  |     keyweight = sum(path_dict.values()) | 
					
						
							|  |  |  |     if keyweight > MAX_KEYWEIGHT: | 
					
						
							| 
									
										
										
										
											2009-02-18 13:12:13 +00:00
										 |  |  |         for k in path_dict.keys(): | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |             path_dict[k] *= 0.9 * MAX_KEYWEIGHT / keyweight | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  |         save(path_dict, dic_file) | 
					
						
							| 
									
										
										
										
											2009-02-18 13:12:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  | def clean_dict(sorted_dirs, path_dict): | 
					
						
							| 
									
										
										
										
											2012-01-13 21:05:43 +00:00
										 |  |  |     """Limits the sized of the path_dict to MAX_STORED_PATHS. | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  |     Returns True if keys were deleted""" | 
					
						
							| 
									
										
										
										
											2011-01-03 22:23:26 +00:00
										 |  |  |     if len(sorted_dirs) > MAX_STORED_PATHS: | 
					
						
							| 
									
										
										
										
											2010-07-21 14:48:35 +00:00
										 |  |  |         #remove 25 more than needed, to avoid doing it every time | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |         for path, dummy in sorted_dirs[MAX_STORED_PATHS-25:]: | 
					
						
							|  |  |  |             del path_dict[path] | 
					
						
							| 
									
										
										
										
											2010-07-21 14:44:43 +00:00
										 |  |  |         return True | 
					
						
							|  |  |  |     else: return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  | def match(path, pattern, ignore_case=False, only_end=False): | 
					
						
							| 
									
										
										
										
											2011-09-09 11:04:21 +00:00
										 |  |  |     """Check whether a path matches a particular pattern, and return | 
					
						
							|  |  |  |        the remaning part of the string""" | 
					
						
							| 
									
										
										
										
											2010-07-21 14:44:43 +00:00
										 |  |  |     if only_end: | 
					
						
							|  |  |  |         match_string = "/".join(path.split('/')[-1-pattern.count('/'):]) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         match_string = path | 
					
						
							|  |  |  |     if ignore_case: | 
					
						
							| 
									
										
										
										
											2011-09-09 11:04:21 +00:00
										 |  |  |         find_idx = match_string.lower().find(pattern.lower()) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         find_idx = match_string.find(pattern) | 
					
						
							|  |  |  |     does_match = (find_idx != -1) | 
					
						
							|  |  |  |     # Eat the path to avoid two patterns matching the same part of the string | 
					
						
							|  |  |  |     if does_match: | 
					
						
							|  |  |  |         eaten_path = path[find_idx+len(pattern):] | 
					
						
							| 
									
										
										
										
											2010-07-21 14:44:43 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2011-09-09 11:04:21 +00:00
										 |  |  |         eaten_path = path | 
					
						
							|  |  |  |     return (does_match, eaten_path) | 
					
						
							| 
									
										
										
										
											2010-07-21 14:44:43 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-15 15:06:15 +00:00
										 |  |  | def find_matches(dirs, patterns, result_list, ignore_case, max_matches, current_dir): | 
					
						
							| 
									
										
										
										
											2012-01-13 21:05:43 +00:00
										 |  |  |     """Find max_matches paths that match the pattern, | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  |     and add them to the result_list""" | 
					
						
							|  |  |  |     for path, count in dirs: | 
					
						
							| 
									
										
										
										
											2011-09-09 11:04:21 +00:00
										 |  |  |         # Don't jump to where we alread are | 
					
						
							| 
									
										
										
										
											2011-09-15 15:06:15 +00:00
										 |  |  |         if current_dir == path : | 
					
						
							| 
									
										
										
										
											2011-09-09 11:04:21 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         does_match, eaten_path = True, path | 
					
						
							|  |  |  |         for n,p in enumerate(patterns): | 
					
						
							|  |  |  |             #For the last pattern, only match the end of the pattern | 
					
						
							|  |  |  |             does_match, eaten_path = match(eaten_path, p, ignore_case, only_end=(n == len(patterns)-1)) | 
					
						
							|  |  |  |             if not does_match: break | 
					
						
							| 
									
										
										
										
											2011-09-15 15:06:15 +00:00
										 |  |  |         #If a path doesn't exist, don't jump there | 
					
						
							|  |  |  |         #We still keep it in db in case it's from a removable drive | 
					
						
							|  |  |  |         if does_match and os.path.exists(path): | 
					
						
							| 
									
										
										
										
											2010-12-23 10:05:20 +00:00
										 |  |  |             uniqadd(result_list, path) | 
					
						
							| 
									
										
										
										
											2011-09-09 11:04:21 +00:00
										 |  |  |             if len(result_list) >= max_matches : | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2009-02-18 13:12:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-10 18:20:08 +00:00
										 |  |  | def get_dic_file(filename="autojump.txt"): | 
					
						
							| 
									
										
										
										
											2011-08-19 09:22:12 +00:00
										 |  |  |     if CONFIG_DIR == os.path.expanduser("~"): | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  |         dic_file = CONFIG_DIR+"/." + filename | 
					
						
							| 
									
										
										
										
											2011-08-19 09:22:12 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2011-09-06 14:21:59 +00:00
										 |  |  |         dic_file = CONFIG_DIR+"/" + filename | 
					
						
							| 
									
										
										
										
											2011-08-19 09:22:12 +00:00
										 |  |  |     return dic_file | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  | def shell_utility(): | 
					
						
							|  |  |  |     """Run this when autojump is called as a shell utility""" | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |     parser = argparse.ArgumentParser(description='Automatically jump to directory passed as an argument.', | 
					
						
							|  |  |  |             epilog="Please see autojump(1) man pages for full documentation.") | 
					
						
							| 
									
										
										
										
											2012-04-12 09:29:13 +00:00
										 |  |  |     parser.add_argument('directory', metavar='DIR', nargs='*', default='', | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |             help='directory to jump to') | 
					
						
							| 
									
										
										
										
											2012-04-12 09:29:13 +00:00
										 |  |  |     parser.add_argument('-a', '--add', metavar='DIR', | 
					
						
							| 
									
										
										
										
											2012-04-07 16:57:16 +00:00
										 |  |  |             help='manually add path to database') | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |     parser.add_argument('-b', '--bash', action="store_true", default=False, | 
					
						
							| 
									
										
										
										
											2012-04-08 15:52:47 +00:00
										 |  |  |             help='enclose directory quotes to prevent errors') | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |     parser.add_argument('--completion', action="store_true", default=False, | 
					
						
							| 
									
										
										
										
											2012-04-07 16:57:16 +00:00
										 |  |  |             help='prevent key weight decay over time') | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |     parser.add_argument('--stat', action="store_true", default=False, | 
					
						
							|  |  |  |             help='show database entries and their key weights') | 
					
						
							|  |  |  |     parser.add_argument('--version', action="version", version="%(prog)s " + AUTOJUMP_VERSION, | 
					
						
							|  |  |  |             help='show version information and exit') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     args = parser.parse_args() | 
					
						
							| 
									
										
										
										
											2011-08-19 09:22:12 +00:00
										 |  |  |     dic_file = get_dic_file() | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |     path_dict = open_dic(dic_file) | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # The home dir can be reached quickly by "cd" and may interfere with other directories | 
					
						
							|  |  |  |     if (args.add): | 
					
						
							|  |  |  |         if(args.add != os.path.expanduser("~")): | 
					
						
							|  |  |  |             dicadd(path_dict, decode(args.add)) | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |             save(path_dict, dic_file) | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (args.stat): | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |         paths = list(path_dict.items()) | 
					
						
							|  |  |  |         paths.sort(key=itemgetter(1)) | 
					
						
							|  |  |  |         for path, count in paths[-100:]: | 
					
						
							| 
									
										
										
										
											2011-09-12 15:04:37 +00:00
										 |  |  |             output(unico("%.1f:\t%s") % (count, path)) | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |         print("Total key weight: %d. Number of stored paths: %d" % | 
					
						
							|  |  |  |                 (sum(path_dict.values()), len(paths))) | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import re | 
					
						
							|  |  |  |     #userchoice is i if the pattern is __pattern__i, otherwise -1 | 
					
						
							|  |  |  |     userchoice = -1 | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     #default: gradually forget about old directories | 
					
						
							|  |  |  |     if (not args.completion): forget(path_dict, dic_file) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-12 09:29:13 +00:00
										 |  |  |     if (args.directory == ''): | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |         patterns = [unico("")] | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         patterns = [decode(a) for a in args.directory] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 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, "", patterns[-1]) | 
					
						
							|  |  |  |     if (len(last_pattern_path)>0 and | 
					
						
							|  |  |  |             last_pattern_path[0] == "/" and | 
					
						
							|  |  |  |             os.path.exists(last_pattern_path)): | 
					
						
							|  |  |  |         if not args.completion: output(last_pattern_path) | 
					
						
							| 
									
										
										
										
											2009-02-18 13:12:13 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |         #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(path_dict.items()) | 
					
						
							|  |  |  |         dirs.sort(key=itemgetter(1), reverse=True) | 
					
						
							|  |  |  |         if args.completion or userchoice != -1: | 
					
						
							|  |  |  |             max_matches = 9 | 
					
						
							| 
									
										
										
										
											2010-07-20 12:44:45 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |             max_matches = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Don't jump to the current directory | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             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.completion or not results: | 
					
						
							|  |  |  |             find_matches(dirs, patterns, results, | 
					
						
							|  |  |  |                     ignore_case=True, | 
					
						
							|  |  |  |                     max_matches=max_matches, current_dir=current_dir) | 
					
						
							|  |  |  |         # Keep the database to a reasonable size | 
					
						
							|  |  |  |         if not args.completion and clean_dict(dirs, path_dict): | 
					
						
							|  |  |  |             save(path_dict, dic_file) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-07 14:21:07 +00:00
										 |  |  |         if args.completion and args.bash: quotes = "'" | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |         else: quotes = "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if userchoice != -1: | 
					
						
							|  |  |  |             if len(results) > userchoice-1 : | 
					
						
							|  |  |  |                 output(unico("%s%s%s") % (quotes,results[userchoice-1],quotes)) | 
					
						
							|  |  |  |         elif len(results) > 1 and args.completion: | 
					
						
							|  |  |  |             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)) | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2012-04-07 14:14:19 +00:00
										 |  |  |             return False | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-04 20:00:59 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2011-02-07 21:47:22 +00:00
										 |  |  |     success=shell_utility() | 
					
						
							|  |  |  |     if not success: exit(1) |