2 * \file ControlCommandBuffer.C
3 * Copyright 1995-2002 the LyX Team
4 * Read the file COPYING
7 * \author Asger and Juergen
8 * \author John Levon <levon@movementarian.org>
12 #pragma implementation
15 #include "ControlCommandBuffer.h"
16 #include "support/lyxalgo.h"
17 #include "support/lstrings.h"
18 #include "LyXAction.h"
23 using std::back_inserter;
27 extern LyXAction lyxaction;
33 prefix_p(string const & s)
35 bool operator()(string const & s) const {
36 return prefixIs(s, p);
40 } // end of anon namespace
43 ControlCommandBuffer::ControlCommandBuffer(LyXFunc & lf)
44 : lyxfunc_(lf), history_pos_(history_.end())
46 transform(lyxaction.func_begin(), lyxaction.func_end(),
47 back_inserter(commands_), lyx::firster());
51 string const ControlCommandBuffer::historyUp()
53 if (history_pos_ == history_.begin())
56 return *(--history_pos_);
60 string const ControlCommandBuffer::historyDown()
62 if (history_pos_ == history_.end())
64 if (history_pos_ + 1 == history_.end())
67 return *(++history_pos_);
71 vector<string> const ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
75 lyx::copy_if(commands_.begin(), commands_.end(),
76 back_inserter(comp), prefix_p(prefix));
83 if (comp.size() == 1) {
85 return vector<string>();
88 // find maximal avaliable prefix
89 string const tmp = comp[0];
91 if (tmp.length() > test.length())
92 test += tmp[test.length()];
93 while (test.length() < tmp.length()) {
95 lyx::copy_if(comp.begin(), comp.end(),
96 back_inserter(vtmp), prefix_p(test));
97 if (vtmp.size() != comp.size()) {
98 test.erase(test.length() - 1);
101 test += tmp[test.length()];
109 void ControlCommandBuffer::dispatch(string const & str)
114 history_.push_back(str);
115 history_pos_ = history_.end();
116 lyxfunc_.dispatch(str, true);