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