Utils/Termlib: Extend the completion API
[senf.git] / Utils / Termlib / Editor.cc
index 0023322..cceb4d8 100644 (file)
@@ -39,14 +39,15 @@ prefix_ senf::term::BaseEditor::BaseEditor(AbstractTerminal & terminal)
       keyTimeout_ (senf::ClockService::milliseconds(DEFAULT_KEY_TIMEOUT_MS)),
       timer_ ("senf::term::BaseEditor::keySequenceTimeout", 
               senf::membind(&BaseEditor::keySequenceTimeout, this)),
-      column_ (0u)
+      column_ (0u), displayHeight_ (1u), line_ (0u)
 {
     terminal_->setCallbacks(*this);
 }
 
 prefix_ void senf::term::BaseEditor::newline()
 {
-    write("\r\n");
+    reset();
+    write("\n");
     write(tifo_.getString(Terminfo::properties::ClrEol));
     column_ = 0;
 }
@@ -124,25 +125,96 @@ prefix_ void senf::term::BaseEditor::setNormal()
         write(tifo_.getString(Terminfo::properties::ExitAttributeMode));
 }
 
+prefix_ void senf::term::BaseEditor::maybeClrScr()
+{
+    if (tifo_.hasProperty(Terminfo::properties::ClearScreen))
+        write(tifo_.getString(Terminfo::properties::ClearScreen));
+}
+
+prefix_ void senf::term::BaseEditor::toLine(unsigned l)
+{
+    if (l >= height())
+        l = height() - 1;
+    unsigned ll (l);
+    if (ll >= displayHeight_)
+        ll = displayHeight_-1;
+    if (ll > line_) {
+        if (tifo_.hasProperty(Terminfo::properties::ParmDownCursor)) {
+            write(tifo_.formatString(Terminfo::properties::ParmDownCursor, ll - line_));
+            line_ = ll;
+        }
+        else {
+            char const * cud1 (tifo_.getString(Terminfo::properties::CursorDown));
+            while (ll > line_) {
+                write(cud1);
+                ++line_;
+            }
+        }
+    }
+    else if (ll < line_) {
+        if (tifo_.hasProperty(Terminfo::properties::ParmUpCursor)) {
+            write(tifo_.formatString(Terminfo::properties::ParmUpCursor, line_ - ll));
+            line_ = ll;
+        }
+        else {
+            char const * cuu1 (tifo_.getString(Terminfo::properties::CursorUp));
+            while (ll < line_) {
+                write(cuu1);
+                --line_;
+            }
+        }
+    }
+    while (line_ < l) {
+        write("\n");
+        write(tifo_.getString(Terminfo::properties::ClrEol));
+        ++displayHeight_;
+        ++line_;
+    }
+    write('\r');
+    column_ = 0;
+}
+
+prefix_ void senf::term::BaseEditor::reset()
+{
+    for (unsigned i (1); i < displayHeight_; ++i) {
+        toLine(i);
+        clearLine();
+    }
+    toLine(0);
+    displayHeight_ = 1;
+}
+
 prefix_ unsigned senf::term::BaseEditor::currentColumn()
    const
 {
     return column_;
 }
 
-prefix_ void senf::term::BaseEditor::cb_init()
+prefix_ unsigned senf::term::BaseEditor::currentLine()
+    const
+{
+    return line_;
+}
+
+prefix_ bool senf::term::BaseEditor::cb_init()
 {
-    tifo_.load(terminal_->terminalType());
-    keyParser_.load(tifo_);
+    try {
+        tifo_.load(terminal_->terminalType());
+        keyParser_.load(tifo_);
+    }
+    catch (Terminfo::InvalidTerminfoException & ex) {
+        return false;
+    }
 
     typedef Terminfo::properties p;
     if (! (tifo_.hasProperty(p::ClrEol) &&
            (tifo_.hasProperty(p::ParmRightCursor) || tifo_.hasProperty(p::CursorRight)) &&
            (tifo_.hasProperty(p::ParmLeftCursor) || tifo_.hasProperty(p::CursorLeft))))
-        throw Terminfo::InvalidTerminfoException();
+        return false;
 
     if (tifo_.hasProperty(Terminfo::properties::KeypadXmit))
         write(tifo_.getString(Terminfo::properties::KeypadXmit));
+    return true;
 }
 
 prefix_ void senf::term::BaseEditor::cb_charReceived(char c)
@@ -181,10 +253,17 @@ prefix_ void senf::term::BaseEditor::processKeys()
 }
 
 prefix_ unsigned senf::term::BaseEditor::width()
+    const
 {
     return terminal_->width();
 }
 
+prefix_ unsigned senf::term::BaseEditor::height()
+    const
+{
+    return terminal_->height();
+}
+
 prefix_ void senf::term::BaseEditor::write(char ch)
 {
     terminal_->write(ch);
@@ -199,12 +278,14 @@ prefix_ void senf::term::BaseEditor::write(std::string const & s)
 ///////////////////////////////////////////////////////////////////////////
 
 prefix_ senf::term::LineEditor::LineEditor(AbstractTerminal & terminal, AcceptCallback cb)
-    : BaseEditor(terminal), enabled_ (true), prompt_ ("$"), promptWidth_ (1u), editWidth_ (0u), 
-      text_ (""), point_ (0u), displayPos_ (0u), lastKey_ (0u), callback_ (cb)
+    : BaseEditor(terminal), enabled_ (false), prompt_ ("$"), promptWidth_ (1u), editWidth_ (0u), 
+      text_ (""), point_ (0u), displayPos_ (0u), lastKey_ (0u), callback_ (cb), historyPoint_ (0u)
 {
     defineKey(KeyParser::Return,    &bindings::accept);
     defineKey(KeyParser::Right,     &bindings::forwardChar);
     defineKey(KeyParser::Left,      &bindings::backwardChar);
+    defineKey(KeyParser::Up,        &bindings::prevHistory);
+    defineKey(KeyParser::Down,      &bindings::nextHistory);
     defineKey(KeyParser::Backspace, &bindings::backwardDeleteChar);
     defineKey(KeyParser::Delete,    &bindings::deleteChar);
     defineKey(KeyParser::Home,      &bindings::beginningOfLine);
@@ -214,12 +295,15 @@ prefix_ senf::term::LineEditor::LineEditor(AbstractTerminal & terminal, AcceptCa
     defineKey(KeyParser::Ctrl('E'), &bindings::endOfLine);
     defineKey(KeyParser::Ctrl('D'), &bindings::deleteChar);
     defineKey(KeyParser::Ctrl('C'), &bindings::restartEdit);
+    defineKey(KeyParser::Ctrl('L'), &bindings::clearScreen);
 }
 
 prefix_ void senf::term::LineEditor::prompt(std::string const & text)
 {
     prompt_ = text;
     promptWidth_ = prompt_.size();
+    if (promptWidth_ > width() - 4 && width() > 4)
+        promptWidth_ = width() - 4;
     editWidth_ = width() - promptWidth_ - 3;
     if (enabled_)
         redisplay();
@@ -242,13 +326,19 @@ prefix_ void senf::term::LineEditor::show()
     if (enabled_)
         return;
     enabled_ = true;
-    redisplay();
+    for (unsigned n (0); n < auxDisplay_.size(); ++n) {
+        toLine(n+1);
+        put(auxDisplay_[n]);
+    }
+    toLine(0);
+    forceRedisplay();
 }
 
 prefix_ void senf::term::LineEditor::hide()
 {
     if (! enabled_)
         return;
+    reset();
     clearLine();
     enabled_ = false;
 }
@@ -258,6 +348,7 @@ prefix_ void senf::term::LineEditor::accept()
     if (enabled_)
         newline();
     hide();
+    pushHistory(text_, true);
     callback_(text_);
     clear();
 }
@@ -265,6 +356,7 @@ prefix_ void senf::term::LineEditor::accept()
 prefix_ void senf::term::LineEditor::clear()
 {
     set("");
+    historyPoint_ = history_.size();
 }
 
 prefix_ void senf::term::LineEditor::redisplay()
@@ -278,7 +370,10 @@ prefix_ void senf::term::LineEditor::forceRedisplay()
         return;
     clearLine();
     setBold();
-    put(prompt_);
+    if (prompt_.size() > promptWidth_)
+        put(prompt_.substr(prompt_.size()-promptWidth_));
+    else
+        put(prompt_);
     put( displayPos_ > 0 ? '<' : ' ' );
     if (text_.size() > displayPos_ + editWidth_) {
         toColumn(editWidth_ + promptWidth_ + 1);
@@ -337,6 +432,63 @@ prefix_ void senf::term::LineEditor::insert(std::string const & text)
     redisplay();
 }
 
+prefix_ void senf::term::LineEditor::pushHistory(std::string const & text, bool accept)
+{
+    if (! text.empty()
+        && (accept || historyPoint_ == history_.size() || history_[historyPoint_] != text)
+        && (history_.empty() || history_.back() != text)) {
+        history_.push_back(text);
+        while (history_.size() > MAX_HISTORY_SIZE)
+            history_.erase(history_.begin());
+        if (accept)
+            historyPoint_ = history_.size() - 1;
+    }
+}
+
+prefix_ void senf::term::LineEditor::prevHistory()
+{
+    if (historyPoint_ <= 0)
+        return;
+    pushHistory(text_);
+    std::string entry (history_[--historyPoint_]);
+    set(entry, entry.size());
+}
+
+prefix_ void senf::term::LineEditor::nextHistory()
+{
+    if (historyPoint_ >= history_.size())
+        return;
+    pushHistory(text_);
+    ++ historyPoint_;
+    if (historyPoint_ >= history_.size())
+        set("");
+    else {
+        std::string entry (history_[historyPoint_]);
+        set(entry, entry.size());
+    }
+}
+
+prefix_ void senf::term::LineEditor::auxDisplay(unsigned line, std::string const & text)
+{
+    toLine(line+1);
+    clearLine();
+    put(text);
+    while (auxDisplay_.size() < line+1)
+        auxDisplay_.push_back("");
+    auxDisplay_[line] = text;
+}
+
+prefix_ unsigned senf::term::LineEditor::maxAuxDisplayHeight()
+{
+    return height()-1;
+}
+
+prefix_ void senf::term::LineEditor::clearAuxDisplay()
+{
+    reset();
+    auxDisplay_.clear();
+}
+
 prefix_ std::string const & senf::term::LineEditor::text()
 {
     return text_;
@@ -367,31 +519,41 @@ prefix_ void senf::term::LineEditor::unsetKey(keycode_t key)
     bindings_.erase(key);
 }
 
-prefix_ void senf::term::LineEditor::cb_init()
+prefix_ bool senf::term::LineEditor::cb_init()
 {
-    BaseEditor::cb_init();
-    editWidth_ = width() - promptWidth_ - 3;
-    forceRedisplay();
+    if (!BaseEditor::cb_init())
+        return false;
+    prompt(prompt_);
+    show();
+    return true;
 }
 
 prefix_ void senf::term::LineEditor::cb_windowSizeChanged()
 {
     BaseEditor::cb_windowSizeChanged();
-    editWidth_ = width() - promptWidth_ - 3;
+    clearAuxDisplay();
+    prompt(prompt_);
     gotoChar(point_);
     forceRedisplay();
 }
 
 prefix_ void senf::term::LineEditor::v_keyReceived(keycode_t key)
 {
+    if (! enabled_)
+        return;
+    clearAuxDisplay();
     lastKey_ = key;
     KeyMap::iterator i (bindings_.find(key));
     if (i != bindings_.end())
         i->second(*this);
     else if (key >= ' ' && key < 256)
         insert(char(key));
+    if (currentLine() != 0)
+        toLine(0);
     if (redisplayNeeded_)
         forceRedisplay();
+    else
+        toColumn(point_ - displayPos_ + promptWidth_ + 1);
 }
 
 ///////////////////////////////////////////////////////////////////////////
@@ -420,6 +582,15 @@ prefix_ void senf::term::bindings::accept(LineEditor & editor)
     editor.accept();
 }
 
+prefix_ void senf::term::bindings::acceptWithRepeat(LineEditor & editor)
+{
+    if (editor.text().empty()) {
+        editor.prevHistory();
+        editor.forceRedisplay();
+    }
+    editor.accept();
+}
+
 prefix_ void senf::term::bindings::backwardDeleteChar(LineEditor & editor)
 {
     unsigned p (editor.point());
@@ -456,6 +627,89 @@ prefix_ void senf::term::bindings::restartEdit(LineEditor & editor)
     editor.redisplay();
 }
 
+prefix_ void senf::term::bindings::prevHistory(LineEditor & editor)
+{
+    editor.prevHistory();
+}
+
+prefix_ void senf::term::bindings::nextHistory(LineEditor & editor)
+{
+    editor.nextHistory();
+}
+
+prefix_ void senf::term::bindings::clearScreen(LineEditor & editor)
+{
+    editor.maybeClrScr();
+    editor.clearLine();
+    editor.forceRedisplay();
+}
+
+prefix_ void senf::term::bindings::complete(LineEditor & editor, Completer completer)
+{
+    typedef std::vector<std::string> Completions;
+
+    std::string text (editor.text());
+    Completions completions;
+    unsigned b (0);
+    unsigned e (editor.point());
+    std::string prefix;
+    completer(editor, b, e, prefix, completions);
+    if (completions.empty())
+        return;
+    if (e > text.size()) 
+        e = text.size();
+    if (b > e)
+        b = e;
+    
+    // Find common start string of all completions
+    unsigned commonStart (completions[0].size());
+    unsigned maxLen (commonStart);
+    for (Completions::const_iterator i (boost::next(completions.begin()));
+         i != completions.end(); ++i) {
+        if (i->size() > maxLen)
+            maxLen = i->size();
+        unsigned n (0u);
+        for (; n < commonStart && n < i->size() && completions[0][n] == (*i)[n]; ++n) ;
+        commonStart = n;
+    }
+
+    // Replace to-be-completed string with the common start string shared by all completions
+    std::string completion (prefix+completions[0].substr(0, commonStart));
+    bool didComplete (false);
+    if (text.substr(b, e) != completion) {
+        text.erase(b, e);
+        text.insert(b, completion);
+        didComplete = true;
+    }
+
+    // Otherwise place cursor directly after the (possibly partial) completion
+    editor.set(text, b+prefix.size()+commonStart);
+    if (didComplete || completions.size() == 1)
+        return;
+
+    // Text was not changed, show list of possible completions
+    unsigned colWidth (maxLen+2);
+    unsigned nColumns ((editor.width()-1) / colWidth);
+    if (nColumns < 1) nColumns = 1;
+    unsigned nRows ((completions.size()+nColumns-1) / nColumns);
+    if (nRows > editor.maxAuxDisplayHeight()) {
+        editor.auxDisplay(0, "(too many completions)");
+        return;
+    }
+    Completions::iterator i (completions.begin());
+    for (unsigned row (0); row < nRows; ++row) {
+        std::string line;
+        for (unsigned column (0); column < nColumns && i != completions.end(); ++column, ++i) {
+            std::string entry (colWidth, ' ');
+            std::copy(i->begin(), 
+                      i->size() > colWidth-2 ? i->begin()+colWidth-2 : i->end(), 
+                      entry.begin());
+            line += entry;
+        }
+        editor.auxDisplay(row, line);
+    }
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Editor.mpp"