]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlCommandBuffer.C
Another 49 #includes bite the dust.
[lyx.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 "lyxfunc.h"
18 #include "LyXAction.h"
19 #include "frontends/LyXView.h"
20 #include "support/lyxalgo.h"
21 #include "support/lstrings.h"
22
23 using namespace lyx::support;
24
25 using std::vector;
26 using std::back_inserter;
27 using std::transform;
28 using std::endl;
29 using namespace bv_funcs;
30
31 namespace {
32
33 struct prefix_p {
34         string p;
35         prefix_p(string const & s)
36                 : p(s) {}
37         bool operator()(string const & s) const {
38                 return prefixIs(s, p);
39         }
40 };
41
42 } // end of anon namespace
43
44
45 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
46         : lv_(lv), history_pos_(history_.end())
47 {
48         transform(lyxaction.func_begin(), lyxaction.func_end(),
49                 back_inserter(commands_), lyx::firster());
50 }
51
52
53 string const ControlCommandBuffer::historyUp()
54 {
55         if (history_pos_ == history_.begin())
56                 return string();
57
58         return *(--history_pos_);
59 }
60
61
62 string const ControlCommandBuffer::historyDown()
63 {
64         if (history_pos_ == history_.end())
65                 return "";
66         if (history_pos_ + 1 == history_.end())
67                 return "";
68
69         return *(++history_pos_);
70 }
71
72
73 string const ControlCommandBuffer::getCurrentState() const
74 {
75         return currentState(lv_.view().get());
76 }
77
78
79 vector<string> const
80 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
81 {
82         vector<string> comp;
83
84         lyx::copy_if(commands_.begin(), commands_.end(),
85                 back_inserter(comp), prefix_p(prefix));
86
87         if (comp.empty()) {
88                 new_prefix = prefix;
89                 return comp;
90         }
91
92         if (comp.size() == 1) {
93                 new_prefix = comp[0];
94                 return vector<string>();
95         }
96
97         // find maximal avaliable prefix
98         string const tmp = comp[0];
99         string test(prefix);
100         if (tmp.length() > test.length())
101                 test += tmp[test.length()];
102         while (test.length() < tmp.length()) {
103                 vector<string> vtmp;
104                 lyx::copy_if(comp.begin(), comp.end(),
105                         back_inserter(vtmp), prefix_p(test));
106                 if (vtmp.size() != comp.size()) {
107                         test.erase(test.length() - 1);
108                         break;
109                 }
110                 test += tmp[test.length()];
111         }
112
113         new_prefix = test;
114         return comp;
115 }
116
117
118 void ControlCommandBuffer::dispatch(string const & str)
119 {
120         if (str.empty())
121                 return;
122
123         history_.push_back(str);
124         history_pos_ = history_.end();
125         lv_.getLyXFunc().dispatch(str, true);
126 }