2 * \file ControlCommandBuffer.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Asger and Jürgen
10 * Full author contact details are available in file CREDITS.
15 #include "ControlCommandBuffer.h"
16 #include "bufferview_funcs.h"
18 #include "LyXAction.h"
19 #include "funcrequest.h"
20 #include "frontends/LyXView.h"
21 #include "support/lyxalgo.h"
22 #include "support/lstrings.h"
24 using bv_funcs::currentState;
26 using lyx::support::prefixIs;
28 using std::back_inserter;
38 prefix_p(string const & s)
40 bool operator()(string const & s) const {
41 return prefixIs(s, p);
45 } // end of anon namespace
48 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
49 : lv_(lv), history_pos_(history_.end())
51 transform(lyxaction.func_begin(), lyxaction.func_end(),
52 back_inserter(commands_), lyx::firster());
56 string const ControlCommandBuffer::historyUp()
58 if (history_pos_ == history_.begin())
61 return *(--history_pos_);
65 string const ControlCommandBuffer::historyDown()
67 if (history_pos_ == history_.end())
69 if (history_pos_ + 1 == history_.end())
72 return *(++history_pos_);
76 string const ControlCommandBuffer::getCurrentState() const
78 return currentState(lv_.view().get());
83 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
87 lyx::copy_if(commands_.begin(), commands_.end(),
88 back_inserter(comp), prefix_p(prefix));
95 if (comp.size() == 1) {
97 return vector<string>();
100 // find maximal avaliable prefix
101 string const tmp = comp[0];
103 if (tmp.length() > test.length())
104 test += tmp[test.length()];
105 while (test.length() < tmp.length()) {
107 lyx::copy_if(comp.begin(), comp.end(),
108 back_inserter(vtmp), prefix_p(test));
109 if (vtmp.size() != comp.size()) {
110 test.erase(test.length() - 1);
113 test += tmp[test.length()];
121 void ControlCommandBuffer::dispatch(string const & str)
126 history_.push_back(str);
127 history_pos_ = history_.end();
128 lv_.getLyXFunc().dispatch(lyxaction.lookupFunc(str), true);