]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlCommandBuffer.C
fix crash due to invalidated iterator
[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 class prefix_p {
42 public:
43         string p;
44         prefix_p(string const & s)
45                 : p(s) {}
46         bool operator()(string const & s) const {
47                 return prefixIs(s, p);
48         }
49 };
50
51 } // end of anon namespace
52
53
54 ControlCommandBuffer::ControlCommandBuffer(LyXView & lv)
55         : lv_(lv), history_pos_(history_.end())
56 {
57         transform(lyxaction.func_begin(), lyxaction.func_end(),
58                 back_inserter(commands_), firster());
59 }
60
61
62 string const ControlCommandBuffer::historyUp()
63 {
64         if (history_pos_ == history_.begin())
65                 return string();
66
67         return *(--history_pos_);
68 }
69
70
71 string const ControlCommandBuffer::historyDown()
72 {
73         if (history_pos_ == history_.end())
74                 return string();
75         if (history_pos_ + 1 == history_.end())
76                 return string();
77
78         return *(++history_pos_);
79 }
80
81
82 string const ControlCommandBuffer::getCurrentState() const
83 {
84         return lv_.view()->cursor().currentState();
85 }
86
87
88 vector<string> const
89 ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
90 {
91         vector<string> comp;
92
93         copy_if(commands_.begin(), commands_.end(),
94                 back_inserter(comp), prefix_p(prefix));
95
96         if (comp.empty()) {
97                 new_prefix = prefix;
98                 return comp;
99         }
100
101         if (comp.size() == 1) {
102                 new_prefix = comp[0];
103                 return vector<string>();
104         }
105
106         // find maximal available prefix
107         string const tmp = comp[0];
108         string test = prefix;
109         if (tmp.length() > test.length())
110                 test += tmp[test.length()];
111         while (test.length() < tmp.length()) {
112                 vector<string> vtmp;
113                 copy_if(comp.begin(), comp.end(),
114                         back_inserter(vtmp), prefix_p(test));
115                 if (vtmp.size() != comp.size()) {
116                         test.erase(test.length() - 1);
117                         break;
118                 }
119                 test += tmp[test.length()];
120         }
121
122         new_prefix = test;
123         return comp;
124 }
125
126
127 void ControlCommandBuffer::dispatch(string const & str)
128 {
129         if (str.empty())
130                 return;
131
132         history_.push_back(str);
133         history_pos_ = history_.end();
134         FuncRequest func = lyxaction.lookupFunc(str);
135         func.origin = FuncRequest::COMMANDBUFFER;
136         lv_.getLyXFunc().dispatch(func);
137 }
138
139 } // namespace frontend
140 } // namespace lyx