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"
17 #include "BufferView.h"
20 #include "LyXAction.h"
21 #include "funcrequest.h"
23 #include "frontends/LyXView.h"
25 #include "support/lyxalgo.h"
26 #include "support/lstrings.h"
28 using std::back_inserter;
35 using support::prefixIs;
43 prefix_p(string const & s)
45 bool operator()(string const & s) const {
46 return prefixIs(s, p);
50 } // end of anon namespace
53 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
54 : lv_(lv), history_pos_(history_.end())
56 transform(lyxaction.func_begin(), lyxaction.func_end(),
57 back_inserter(commands_), firster());
61 string const ControlCommandBuffer::historyUp()
63 if (history_pos_ == history_.begin())
66 return *(--history_pos_);
70 string const ControlCommandBuffer::historyDown()
72 if (history_pos_ == history_.end())
74 if (history_pos_ + 1 == history_.end())
77 return *(++history_pos_);
81 string const ControlCommandBuffer::getCurrentState() const
83 return lv_.view()->cursor().currentState();
88 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
92 copy_if(commands_.begin(), commands_.end(),
93 back_inserter(comp), prefix_p(prefix));
100 if (comp.size() == 1) {
101 new_prefix = comp[0];
102 return vector<string>();
105 // find maximal available prefix
106 string const tmp = comp[0];
107 string test = prefix;
108 if (tmp.length() > test.length())
109 test += tmp[test.length()];
110 while (test.length() < tmp.length()) {
112 copy_if(comp.begin(), comp.end(),
113 back_inserter(vtmp), prefix_p(test));
114 if (vtmp.size() != comp.size()) {
115 test.erase(test.length() - 1);
118 test += tmp[test.length()];
126 void ControlCommandBuffer::dispatch(string const & str)
131 history_.push_back(str);
132 history_pos_ = history_.end();
133 lv_.getLyXFunc().dispatch(lyxaction.lookupFunc(str), true);
136 } // namespace frontend