Packets: Add StringParser ostream operation
[senf.git] / Utils / Console / LineEditor.cc
index df9d065..00faa8b 100644 (file)
@@ -87,6 +87,7 @@ LineEditorClientReader(Client & client, LineEditorSwitcher & switcher)
                       boost::bind(&term::bindings::complete,
                                   _1, 
                                   senf::membind(&LineEditorClientReader::completePath, this)));
+    editor_.defineKey(senf::term::KeyParser::Return, &senf::term::bindings::acceptWithRepeat);
 }
 
 prefix_ void senf::console::detail::LineEditorClientReader::v_setupFailed()
@@ -140,10 +141,27 @@ senf::console::detail::LineEditorClientReader::deleteCharOrExit(term::LineEditor
 }
 
 prefix_ void senf::console::detail::LineEditorClientReader::
-completePath(term::LineEditor & editor, unsigned b, unsigned e,
+completePath(term::LineEditor & editor, unsigned & b, unsigned & e, std::string & prefix,
              std::vector<std::string> & completions)
 {
-    std::string base (editor.text().substr(b,e));
+    std::string const & t (editor.text());
+    // Search backward from e finding the longest valid path. This does *not* accept all valid
+    // path's, only those without embedded white-space. However, this is only for completion so
+    // it's ok. 
+    if (b<e) {
+        unsigned bb (e-1);
+        for (;;) {
+            if (! CommandParser::isWordChar(t[bb]) && t[bb] != '/') {
+                ++bb;
+                break;
+            }
+            if (bb == b)
+                break;
+            --bb;
+        }
+        b = bb;
+    }
+    std::string base (t.substr(b,e));
     CommandParser parser;
     ParseCommandInfo cmd;
     try {
@@ -164,21 +182,20 @@ completePath(term::LineEditor & editor, unsigned b, unsigned e,
     ParseCommandInfo::TokensRange::const_iterator i (path.begin());
     ParseCommandInfo::TokensRange::const_iterator const i_end (boost::prior(path.end()));
     DirectoryNode * dir (& client().cwd());
-    std::string basePath;
     for (; i != i_end; ++i)
         if (*i == NoneToken()) {
             if (i == path.begin()) {
                 dir = & client().root();
-                basePath = "/";
+                prefix = "/";
             }
         }
         else if (*i == WordToken("..")) {
             DirectoryNode * parent (dir->parent().get());
             if (parent) dir = parent;
-            basePath += "../";
+            prefix += "../";
         }
         else if (*i == WordToken(".")) 
-            basePath += "./";
+            prefix += "./";
         else {
             if (dir->hasChild(i->value())) {
                 try {
@@ -187,7 +204,7 @@ completePath(term::LineEditor & editor, unsigned b, unsigned e,
                 catch (std::bad_cast &) {
                     return;
                 }
-                basePath += i->value() + "/";
+                prefix += i->value() + "/";
             } 
             else {
                 DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
@@ -196,7 +213,7 @@ completePath(term::LineEditor & editor, unsigned b, unsigned e,
                     if (!node.isDirectory())
                         return;
                     dir = static_cast<DirectoryNode*>(&node);
-                    basePath += cs.begin()->first + "/";
+                    prefix += cs.begin()->first + "/";
                 }
                 else
                     return;
@@ -205,8 +222,7 @@ completePath(term::LineEditor & editor, unsigned b, unsigned e,
 
     DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
     for (DirectoryNode::ChildrenRange::iterator j (cs.begin()); j != cs.end(); ++j)
-        completions.push_back(basePath + j->first 
-                              + (j->second->followLink().isDirectory() ? "/" : " "));
+        completions.push_back(j->first + (j->second->followLink().isDirectory() ? "/" : " "));
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////