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