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 #include "ControlCommandBuffer.h"
17 #include "support/lyxalgo.h"
18 #include "support/lstrings.h"
19 #include "LyXAction.h"
24 using std::back_inserter;
32 prefix_p(string const & s)
34 bool operator()(string const & s) const {
35 return prefixIs(s, p);
39 } // end of anon namespace
42 ControlCommandBuffer::ControlCommandBuffer(LyXFunc & lf)
43 : lyxfunc_(lf), history_pos_(history_.end())
45 transform(lyxaction.func_begin(), lyxaction.func_end(),
46 back_inserter(commands_), lyx::firster());
50 string const ControlCommandBuffer::historyUp()
52 if (history_pos_ == history_.begin())
55 return *(--history_pos_);
59 string const ControlCommandBuffer::historyDown()
61 if (history_pos_ == history_.end())
63 if (history_pos_ + 1 == history_.end())
66 return *(++history_pos_);
71 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
75 lyx::copy_if(commands_.begin(), commands_.end(),
76 back_inserter(comp), prefix_p(prefix));
83 if (comp.size() == 1) {
85 return vector<string>();
88 // find maximal avaliable prefix
89 string const tmp = comp[0];
91 if (tmp.length() > test.length())
92 test += tmp[test.length()];
93 while (test.length() < tmp.length()) {
95 lyx::copy_if(comp.begin(), comp.end(),
96 back_inserter(vtmp), prefix_p(test));
97 if (vtmp.size() != comp.size()) {
98 test.erase(test.length() - 1);
101 test += tmp[test.length()];
109 void ControlCommandBuffer::dispatch(string const & str)
114 history_.push_back(str);
115 history_pos_ = history_.end();
116 lyxfunc_.dispatch(str, true);