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
16 #pragma implementation
19 #include "ControlCommandBuffer.h"
20 #include "support/lyxalgo.h"
21 #include "support/lstrings.h"
22 #include "LyXAction.h"
27 using std::back_inserter;
35 prefix_p(string const & s)
37 bool operator()(string const & s) const {
38 return prefixIs(s, p);
42 } // end of anon namespace
45 ControlCommandBuffer::ControlCommandBuffer(LyXFunc & lf)
46 : lyxfunc_(lf), history_pos_(history_.end())
48 transform(lyxaction.func_begin(), lyxaction.func_end(),
49 back_inserter(commands_), lyx::firster());
53 string const ControlCommandBuffer::historyUp()
55 if (history_pos_ == history_.begin())
58 return *(--history_pos_);
62 string const ControlCommandBuffer::historyDown()
64 if (history_pos_ == history_.end())
66 if (history_pos_ + 1 == history_.end())
69 return *(++history_pos_);
73 vector<string> const ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
77 lyx::copy_if(commands_.begin(), commands_.end(),
78 back_inserter(comp), prefix_p(prefix));
85 if (comp.size() == 1) {
87 return vector<string>();
90 // find maximal avaliable prefix
91 string const tmp = comp[0];
93 if (tmp.length() > test.length())
94 test += tmp[test.length()];
95 while (test.length() < tmp.length()) {
97 lyx::copy_if(comp.begin(), comp.end(),
98 back_inserter(vtmp), prefix_p(test));
99 if (vtmp.size() != comp.size()) {
100 test.erase(test.length() - 1);
103 test += tmp[test.length()];
111 void ControlCommandBuffer::dispatch(string const & str)
116 history_.push_back(str);
117 history_pos_ = history_.end();
118 lyxfunc_.dispatch(str, true);