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>
23 from polysh
.control_commands_helpers
import complete_control_command
24 from polysh
.control_commands_helpers
import expand_local_path
26 def complete_local_path(path
):
31 path
= expand_local_path(path
)
32 paths
= [p
+ get_suffix(p
) for p
in glob
.glob(path
+ '*')]
35 def remove_dupes(words
):
39 stripped
= w
.rstrip('/ ')
40 if stripped
not in added
:
45 def read_commands_in_path():
48 for path
in (os
.getenv('PATH') or '').split(':'):
51 listing
= os
.listdir(path
)
55 commands |
= set(listing
)
58 # All the words that have been typed in polysh. Used by the completion
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()
72 lib_readline
= ctypes
.cdll
.LoadLibrary(ctypes
.util
.find_library("readline"))
73 rl_completion_append_character
= ctypes
.c_char
.in_dll(lib_readline
,
74 "rl_completion_append_character")
76 class rl_completion_append_character
:
80 def complete(text
, state
):
81 """On tab press, return the next possible completion"""
82 rl_completion_append_character
.value
= '\0'
83 global completion_results
85 line
= readline
.get_line_buffer()
86 if line
.startswith(':'):
87 # Control command completion
88 completion_results
= complete_control_command(line
, text
)
90 if line
.startswith('!') and text
and line
.startswith(text
):
94 dropped_exclam
= False
95 completion_results
= []
96 # Complete local paths
97 completion_results
+= complete_local_path(text
)
98 # Complete from history
100 completion_results
+= [w
+ ' ' for w
in history_words
if \
101 len(w
) > l
and w
.startswith(text
)]
102 if readline
.get_begidx() == 0:
103 # Completing first word from $PATH
104 completion_results
+= [w
+ ' ' for w
in user_commands_in_path \
105 if len(w
) > l
and w
.startswith(text
)]
106 completion_results
= remove_dupes(completion_results
)
108 completion_results
= ['!' + r
for r
in completion_results
]
110 if state
< len(completion_results
):
111 return completion_results
[state
]
112 completion_results
= None
114 def add_to_history(cmd
):
115 words
= [w
for w
in cmd
.split() if len(w
) > 1]
116 history_words
.update(words
)
117 if len(history_words
) > 10000:
118 del history_words
[:-10000]
120 def remove_last_history_item():
121 """The user just typed a password..."""
122 last
= readline
.get_current_history_length() - 1
123 readline
.remove_history_item(last
)
125 def install_completion_handler():
126 readline
.set_completer(complete
)
127 readline
.parse_and_bind('tab: complete')
128 readline
.set_completer_delims(' \t\n')