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"
24 using namespace lyx::support;
27 using std::back_inserter;
30 using namespace bv_funcs;
36 prefix_p(string const & s)
38 bool operator()(string const & s) const {
39 return prefixIs(s, p);
43 } // end of anon namespace
46 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
47 : lv_(lv), history_pos_(history_.end())
49 transform(lyxaction.func_begin(), lyxaction.func_end(),
50 back_inserter(commands_), lyx::firster());
54 string const ControlCommandBuffer::historyUp()
56 if (history_pos_ == history_.begin())
59 return *(--history_pos_);
63 string const ControlCommandBuffer::historyDown()
65 if (history_pos_ == history_.end())
67 if (history_pos_ + 1 == history_.end())
70 return *(++history_pos_);
74 string const ControlCommandBuffer::getCurrentState() const
76 return currentState(lv_.view().get());
81 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
85 lyx::copy_if(commands_.begin(), commands_.end(),
86 back_inserter(comp), prefix_p(prefix));
93 if (comp.size() == 1) {
95 return vector<string>();
98 // find maximal avaliable prefix
99 string const tmp = comp[0];
101 if (tmp.length() > test.length())
102 test += tmp[test.length()];
103 while (test.length() < tmp.length()) {
105 lyx::copy_if(comp.begin(), comp.end(),
106 back_inserter(vtmp), prefix_p(test));
107 if (vtmp.size() != comp.size()) {
108 test.erase(test.length() - 1);
111 test += tmp[test.length()];
119 void ControlCommandBuffer::dispatch(string const & str)
124 history_.push_back(str);
125 history_pos_ = history_.end();
126 lv_.getLyXFunc().dispatch(str, true);