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 lyx::support::prefixIs;
30 using std::back_inserter;
40 prefix_p(string const & s)
42 bool operator()(string const & s) const {
43 return prefixIs(s, p);
47 } // end of anon namespace
50 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
51 : lv_(lv), history_pos_(history_.end())
53 transform(lyxaction.func_begin(), lyxaction.func_end(),
54 back_inserter(commands_), lyx::firster());
58 string const ControlCommandBuffer::historyUp()
60 if (history_pos_ == history_.begin())
63 return *(--history_pos_);
67 string const ControlCommandBuffer::historyDown()
69 if (history_pos_ == history_.end())
71 if (history_pos_ + 1 == history_.end())
74 return *(++history_pos_);
78 string const ControlCommandBuffer::getCurrentState() const
80 return lv_.view()->cursor().currentState();
85 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
89 lyx::copy_if(commands_.begin(), commands_.end(),
90 back_inserter(comp), prefix_p(prefix));
97 if (comp.size() == 1) {
99 return vector<string>();
102 // find maximal available prefix
103 string const tmp = comp[0];
104 string test = prefix;
105 if (tmp.length() > test.length())
106 test += tmp[test.length()];
107 while (test.length() < tmp.length()) {
109 lyx::copy_if(comp.begin(), comp.end(),
110 back_inserter(vtmp), prefix_p(test));
111 if (vtmp.size() != comp.size()) {
112 test.erase(test.length() - 1);
115 test += tmp[test.length()];
123 void ControlCommandBuffer::dispatch(string const & str)
128 history_.push_back(str);
129 history_pos_ = history_.end();
130 lv_.getLyXFunc().dispatch(lyxaction.lookupFunc(str), true);