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 Jürgen
10 * Full author contact details are available in file CREDITS.
15 #include "ControlCommandBuffer.h"
16 #include "bufferview_funcs.h"
18 #include "LyXAction.h"
19 #include "funcrequest.h"
21 #include "frontends/LyXView.h"
22 #include "support/lyxalgo.h"
23 #include "support/lstrings.h"
25 using bv_funcs::currentState;
27 using lyx::support::prefixIs;
29 using std::back_inserter;
39 prefix_p(string const & s)
41 bool operator()(string const & s) const {
42 return prefixIs(s, p);
46 } // end of anon namespace
49 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
50 : lv_(lv), history_pos_(history_.end())
52 transform(lyxaction.func_begin(), lyxaction.func_end(),
53 back_inserter(commands_), lyx::firster());
57 string const ControlCommandBuffer::historyUp()
59 if (history_pos_ == history_.begin())
62 return *(--history_pos_);
66 string const ControlCommandBuffer::historyDown()
68 if (history_pos_ == history_.end())
70 if (history_pos_ + 1 == history_.end())
73 return *(++history_pos_);
77 string const ControlCommandBuffer::getCurrentState() const
79 return currentState(lv_.view().get());
84 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
88 lyx::copy_if(commands_.begin(), commands_.end(),
89 back_inserter(comp), prefix_p(prefix));
96 if (comp.size() == 1) {
98 return vector<string>();
101 // find maximal avaliable prefix
102 string const tmp = comp[0];
104 if (tmp.length() > test.length())
105 test += tmp[test.length()];
106 while (test.length() < tmp.length()) {
108 lyx::copy_if(comp.begin(), comp.end(),
109 back_inserter(vtmp), prefix_p(test));
110 if (vtmp.size() != comp.size()) {
111 test.erase(test.length() - 1);
114 test += tmp[test.length()];
122 void ControlCommandBuffer::dispatch(string const & str)
127 history_.push_back(str);
128 history_pos_ = history_.end();
129 lv_.getLyXFunc().dispatch(lyxaction.lookupFunc(str), true);