]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlCommandBuffer.C
Really dull and boring header shit
[lyx.git] / src / frontends / controllers / ControlCommandBuffer.C
1 /**
2  * \file ControlCommandBuffer.C
3  * Read the file COPYING
4  *
5  * \author Lars
6  * \author Asger and Juergen
7  * \author John Levon 
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "ControlCommandBuffer.h"
19 #include "support/lyxalgo.h"
20 #include "support/lstrings.h"
21 #include "LyXAction.h"
22 #include "lyxfunc.h"
23 #include "debug.h"
24
25 using std::vector;
26 using std::back_inserter;
27 using std::transform;
28 using std::endl;
29  
30 namespace {
31  
32 struct prefix_p {
33         string p;
34         prefix_p(string const & s) 
35                 : p(s) {}
36         bool operator()(string const & s) const {
37                 return prefixIs(s, p);
38         }
39 };
40  
41 } // end of anon namespace
42
43  
44 ControlCommandBuffer::ControlCommandBuffer(LyXFunc & lf)
45         : lyxfunc_(lf), history_pos_(history_.end())
46 {
47         transform(lyxaction.func_begin(), lyxaction.func_end(),
48                 back_inserter(commands_), lyx::firster()); 
49 }
50  
51         
52 string const ControlCommandBuffer::historyUp()
53 {
54         if (history_pos_ == history_.begin())
55                 return "";
56
57         return *(--history_pos_);
58 }
59  
60
61 string const ControlCommandBuffer::historyDown()
62 {
63         if (history_pos_ == history_.end())
64                 return "";
65         if (history_pos_ + 1 == history_.end())
66                 return "";
67
68         return *(++history_pos_);
69 }
70
71
72 vector<string> const ControlCommandBuffer::completions(string const & prefix, string & new_prefix)
73 {
74         vector<string> comp;
75
76         lyx::copy_if(commands_.begin(), commands_.end(),
77                 back_inserter(comp), prefix_p(prefix));
78
79         if (comp.empty()) {
80                 new_prefix = prefix;
81                 return comp;
82         }
83
84         if (comp.size() == 1) {
85                 new_prefix = comp[0];
86                 return vector<string>();
87         }
88
89         // find maximal avaliable prefix
90         string const tmp = comp[0];
91         string test(prefix);
92         if (tmp.length() > test.length())
93                 test += tmp[test.length()];
94         while (test.length() < tmp.length()) {
95                 vector<string> vtmp;
96                 lyx::copy_if(comp.begin(), comp.end(),
97                         back_inserter(vtmp), prefix_p(test));
98                 if (vtmp.size() != comp.size()) {
99                         test.erase(test.length() - 1);
100                         break;
101                 }
102                 test += tmp[test.length()];
103         }
104  
105         new_prefix = test;
106         return comp;
107 }
108  
109
110 void ControlCommandBuffer::dispatch(string const & str)
111 {
112         if (str.empty())
113                 return;
114  
115         history_.push_back(str);
116         history_pos_ = history_.end();
117         lyxfunc_.dispatch(str, true);
118 }