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 Juergen
10 * Full author contact details are available in file CREDITS
15 #include "ControlCommandBuffer.h"
16 #include "bufferview_funcs.h"
19 #include "LyXAction.h"
20 #include "frontends/LyXView.h"
21 #include "support/lyxalgo.h"
22 #include "support/lstrings.h"
25 using std::back_inserter;
28 using namespace bv_funcs;
34 prefix_p(string const & s)
36 bool operator()(string const & s) const {
37 return prefixIs(s, p);
41 } // end of anon namespace
44 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
45 : lv_(lv), history_pos_(history_.end())
47 transform(lyxaction.func_begin(), lyxaction.func_end(),
48 back_inserter(commands_), lyx::firster());
52 string const ControlCommandBuffer::historyUp()
54 if (history_pos_ == history_.begin())
57 return *(--history_pos_);
61 string const ControlCommandBuffer::historyDown()
63 if (history_pos_ == history_.end())
65 if (history_pos_ + 1 == history_.end())
68 return *(++history_pos_);
72 string const ControlCommandBuffer::getCurrentState() const
74 return currentState(lv_.view().get());
79 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
83 lyx::copy_if(commands_.begin(), commands_.end(),
84 back_inserter(comp), prefix_p(prefix));
91 if (comp.size() == 1) {
93 return vector<string>();
96 // find maximal avaliable prefix
97 string const tmp = comp[0];
99 if (tmp.length() > test.length())
100 test += tmp[test.length()];
101 while (test.length() < tmp.length()) {
103 lyx::copy_if(comp.begin(), comp.end(),
104 back_inserter(vtmp), prefix_p(test));
105 if (vtmp.size() != comp.size()) {
106 test.erase(test.length() - 1);
109 test += tmp[test.length()];
117 void ControlCommandBuffer::dispatch(string const & str)
122 history_.push_back(str);
123 history_pos_ = history_.end();
124 lv_.getLyXFunc().dispatch(str, true);