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