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;
31 prefix_p(string const & s)
33 bool operator()(string const & s) const {
34 return prefixIs(s, p);
38 } // end of anon namespace
41 ControlCommandBuffer::ControlCommandBuffer(LyXFunc & lf)
42 : lyxfunc_(lf), history_pos_(history_.end())
44 transform(lyxaction.func_begin(), lyxaction.func_end(),
45 back_inserter(commands_), lyx::firster());
49 string const ControlCommandBuffer::historyUp()
51 if (history_pos_ == history_.begin())
54 return *(--history_pos_);
58 string const ControlCommandBuffer::historyDown()
60 if (history_pos_ == history_.end())
62 if (history_pos_ + 1 == history_.end())
65 return *(++history_pos_);
69 vector<string> const ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
73 lyx::copy_if(commands_.begin(), commands_.end(),
74 back_inserter(comp), prefix_p(prefix));
81 if (comp.size() == 1) {
83 return vector<string>();
86 // find maximal avaliable prefix
87 string const tmp = comp[0];
89 if (tmp.length() > test.length())
90 test += tmp[test.length()];
91 while (test.length() < tmp.length()) {
93 lyx::copy_if(comp.begin(), comp.end(),
94 back_inserter(vtmp), prefix_p(test));
95 if (vtmp.size() != comp.size()) {
96 test.erase(test.length() - 1);
99 test += tmp[test.length()];
107 void ControlCommandBuffer::dispatch(string const & str)
112 history_.push_back(str);
113 history_pos_ = history_.end();
114 lyxfunc_.dispatch(str, true);