]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlCommandBuffer.C
e3468234c0c588ff69593506b0af2932bc9ceba3
[features.git] / src / frontends / controllers / ControlCommandBuffer.C
1 /**
2  * \file ControlCommandBuffer.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars
7  * \author Asger and Juergen
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 #include <config.h>
14
15 #include "ControlCommandBuffer.h"
16 #include "bufferview_funcs.h"
17 #include "debug.h"
18 #include "lyxfunc.h"
19 #include "LyXAction.h"
20 #include "frontends/LyXView.h"
21 #include "support/lyxalgo.h"
22 #include "support/lstrings.h"
23
24 using std::vector;
25 using std::back_inserter;
26 using std::transform;
27 using std::endl;
28 using namespace bv_funcs;
29
30 namespace {
31
32 struct prefix_p {
33         string p;
34         prefix_p(string const & s)
35                 : p(s) {}
36         bool operator()(string const & s) const {
37                 return prefixIs(s, p);
38         }
39 };
40
41 } // end of anon namespace
42
43
44 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
45         : lv_(lv), history_pos_(history_.end())
46 {
47         transform(lyxaction.func_begin(), lyxaction.func_end(),
48                 back_inserter(commands_), lyx::firster());
49 }
50
51
52 string const ControlCommandBuffer::historyUp()
53 {
54         if (history_pos_ == history_.begin())
55                 return string();
56
57         return *(--history_pos_);
58 }
59
60
61 string const ControlCommandBuffer::historyDown()
62 {
63         if (history_pos_ == history_.end())
64                 return "";
65         if (history_pos_ + 1 == history_.end())
66                 return "";
67
68         return *(++history_pos_);
69 }
70
71
72 string const ControlCommandBuffer::getCurrentState() const
73 {
74         return currentState(lv_.view().get());
75 }
76
77
78 vector<string> const
79 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
80 {
81         vector<string> comp;
82
83         lyx::copy_if(commands_.begin(), commands_.end(),
84                 back_inserter(comp), prefix_p(prefix));
85
86         if (comp.empty()) {
87                 new_prefix = prefix;
88                 return comp;
89         }
90
91         if (comp.size() == 1) {
92                 new_prefix = comp[0];
93                 return vector<string>();
94         }
95
96         // find maximal avaliable prefix
97         string const tmp = comp[0];
98         string test(prefix);
99         if (tmp.length() > test.length())
100                 test += tmp[test.length()];
101         while (test.length() < tmp.length()) {
102                 vector<string> vtmp;
103                 lyx::copy_if(comp.begin(), comp.end(),
104                         back_inserter(vtmp), prefix_p(test));
105                 if (vtmp.size() != comp.size()) {
106                         test.erase(test.length() - 1);
107                         break;
108                 }
109                 test += tmp[test.length()];
110         }
111
112         new_prefix = test;
113         return comp;
114 }
115
116
117 void ControlCommandBuffer::dispatch(string const & str)
118 {
119         if (str.empty())
120                 return;
121
122         history_.push_back(str);
123         history_pos_ = history_.end();
124         lv_.getLyXFunc().dispatch(str, true);
125 }