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