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;
44 prefix_p(string const & s)
46 bool operator()(string const & s) const {
47 return prefixIs(s, p);
51 } // end of anon namespace
54 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
55 : lv_(lv), history_pos_(history_.end())
57 transform(lyxaction.func_begin(), lyxaction.func_end(),
58 back_inserter(commands_), firster());
62 string const ControlCommandBuffer::historyUp()
64 if (history_pos_ == history_.begin())
67 return *(--history_pos_);
71 string const ControlCommandBuffer::historyDown()
73 if (history_pos_ == history_.end())
75 if (history_pos_ + 1 == history_.end())
78 return *(++history_pos_);
82 string const ControlCommandBuffer::getCurrentState() const
84 return lv_.view()->cursor().currentState();
89 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
93 copy_if(commands_.begin(), commands_.end(),
94 back_inserter(comp), prefix_p(prefix));
101 if (comp.size() == 1) {
102 new_prefix = comp[0];
103 return vector<string>();
106 // find maximal available prefix
107 string const tmp = comp[0];
108 string test = prefix;
109 if (tmp.length() > test.length())
110 test += tmp[test.length()];
111 while (test.length() < tmp.length()) {
113 copy_if(comp.begin(), comp.end(),
114 back_inserter(vtmp), prefix_p(test));
115 if (vtmp.size() != comp.size()) {
116 test.erase(test.length() - 1);
119 test += tmp[test.length()];
127 void ControlCommandBuffer::dispatch(string const & str)
132 history_.push_back(str);
133 history_pos_ = history_.end();
134 FuncRequest func = lyxaction.lookupFunc(str);
135 func.origin = FuncRequest::COMMANDBUFFER;
136 lv_.getLyXFunc().dispatch(func);
139 } // namespace frontend