2 * \file ControlCommandBuffer.C
3 * Read the file COPYING
6 * \author Asger and Juergen
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
18 #include "ControlCommandBuffer.h"
19 #include "support/lyxalgo.h"
20 #include "support/lstrings.h"
21 #include "LyXAction.h"
26 using std::back_inserter;
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(LyXFunc & lf)
45 : lyxfunc_(lf), 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 vector<string> const ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
76 lyx::copy_if(commands_.begin(), commands_.end(),
77 back_inserter(comp), prefix_p(prefix));
84 if (comp.size() == 1) {
86 return vector<string>();
89 // find maximal avaliable prefix
90 string const tmp = comp[0];
92 if (tmp.length() > test.length())
93 test += tmp[test.length()];
94 while (test.length() < tmp.length()) {
96 lyx::copy_if(comp.begin(), comp.end(),
97 back_inserter(vtmp), prefix_p(test));
98 if (vtmp.size() != comp.size()) {
99 test.erase(test.length() - 1);
102 test += tmp[test.length()];
110 void ControlCommandBuffer::dispatch(string const & str)
115 history_.push_back(str);
116 history_pos_ = history_.end();
117 lyxfunc_.dispatch(str, true);