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