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