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