The ':' was already in the prefix.
[polysh.git] / gsh / completion.py
blob2f8120d67674d88656e48def9b7b46c5cc44bca0
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2008 Guillaume Chazarain <guichaz@gmail.com>
19 import os
20 import readline
22 def complete_local_absolute_path(path):
23 if not path.startswith('/'):
24 return []
25 dirname, basename = os.path.split(path)
26 if not dirname.endswith('/'):
27 dirname += '/'
28 paths = [dirname + p for p in os.listdir(dirname) if p.startswith(basename)]
29 def get_suffix(p):
30 if os.path.isdir(p):
31 return '/'
32 return ''
33 paths = [p + get_suffix(p) for p in paths]
34 return paths
36 def remove_dupes(words):
37 added = set()
38 results = list()
39 for w in words:
40 stripped = w.rstrip('/ ')
41 if stripped not in added:
42 added.add(stripped)
43 results.append(w)
44 return results
46 def read_commands_in_path():
47 commands = set()
49 for path in (os.getenv('PATH') or '').split(':'):
50 if path:
51 try:
52 listing = os.listdir(path)
53 except OSError:
54 pass
55 else:
56 commands |= set(listing)
57 return list(commands)
59 # All the words that have been typed in gsh. Used by the completion mechanism.
60 history_words = set()
62 # When listing possible completions, the complete() function is called with
63 # an increasing state parameter until it returns None. Cache the completion
64 # list instead of regenerating it for each completion item.
65 completion_results = None
67 # Commands in $PATH, used for the completion of the first word
68 user_commands_in_path = read_commands_in_path()
70 def complete(text, state):
71 """On tab press, return the next possible completion"""
72 from gsh.control_commands_helpers import complete_control_command
73 global completion_results
74 if state == 0:
75 line = readline.get_line_buffer()
76 if line.startswith(':'):
77 # Control command completion
78 completion_results = complete_control_command(line, text)
79 else:
80 if line.startswith('!'):
81 dropped_exclam = True
82 text = text[1:]
83 else:
84 dropped_exclam = False
85 completion_results = []
86 # Complete absolute paths
87 completion_results += complete_local_absolute_path(text)
88 # Complete from history
89 l = len(text)
90 completion_results += [w + ' ' for w in history_words if \
91 len(w) > l and w.startswith(text)]
92 if readline.get_begidx() == 0:
93 # Completing first word from $PATH
94 completion_results += [w + ' ' for w in user_commands_in_path \
95 if len(w) > l and w.startswith(text)]
96 completion_results = remove_dupes(completion_results)
97 if dropped_exclam:
98 completion_results = ['!' + r for r in completion_results]
100 if state < len(completion_results):
101 return completion_results[state]
102 completion_results = None
104 def add_to_history(cmd):
105 words = [w for w in cmd.split() if len(w) > 1]
106 history_words.update(words)
107 if len(history_words) > 10000:
108 del history_words[:-10000]
110 def remove_last_history_item():
111 """The user just typed a password..."""
112 last = readline.get_current_history_length() - 1
113 readline.remove_history_item(last)
115 def install_completion_handler():
116 readline.set_completer(complete)
117 readline.parse_and_bind('tab: complete')
118 readline.set_completer_delims(' \t\n')