]> arthur.barton.de Git - bup.git/blobdiff - cmd/ftp-cmd.py
Work around extra space added by some readline versions.
[bup.git] / cmd / ftp-cmd.py
index d40e72ac1956205eba97230c6a1e9de3fabdc516..7c2823fa1f7f214a6bf04324d9ac50500fc686d0 100755 (executable)
@@ -56,11 +56,44 @@ def _completer_get_subs(line):
     return (dir, name, qtype, lastword, subs)
 
 
+def find_readline_lib():
+    """Return the name (and possibly the full path) of the readline library
+    linked to the given readline module.
+    """
+    import readline
+    f = open(readline.__file__, "rb")
+    try:
+        data = f.read()
+    finally:
+        f.close()
+    import re
+    m = re.search('\0([^\0]*libreadline[^\0]*)\0', data)
+    if m:
+        return m.group(1)
+    return None
+
+
+def init_readline_vars():
+    """Work around trailing space automatically inserted by readline.
+    See http://bugs.python.org/issue5833"""
+    import ctypes
+    lib_name = find_readline_lib()
+    if lib_name is not None:
+        lib = ctypes.cdll.LoadLibrary(lib_name)
+        global rl_completion_suppress_append
+        rl_completion_suppress_append = ctypes.c_int.in_dll(lib,
+                                    "rl_completion_suppress_append")
+
+
+rl_completion_suppress_append = None
 _last_line = None
 _last_res = None
 def completer(text, state):
     global _last_line
     global _last_res
+    global rl_completion_suppress_append
+    if rl_completion_suppress_append is not None:
+        rl_completion_suppress_append.value = 1
     try:
         line = readline.get_line_buffer()[:readline.get_endidx()]
         if _last_line != line:
@@ -81,7 +114,7 @@ def completer(text, state):
     except Exception, e:
         log('\nerror in completion: %s\n' % e)
 
-            
+
 optspec = """
 bup ftp
 """
@@ -100,6 +133,7 @@ else:
         readline.set_completer_delims(' \t\n\r/')
         readline.set_completer(completer)
         readline.parse_and_bind("tab: complete")
+        init_readline_vars()
     lines = inputiter()
 
 for line in lines: