]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlCommandBuffer.C
include sys/time.h
[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
16 #include "ControlCommandBuffer.h"
17 #include "support/lyxalgo.h"
18 #include "support/lstrings.h"
19 #include "LyXAction.h"
20 #include "lyxfunc.h"
21 #include "debug.h"
22
23 using std::vector;
24 using std::back_inserter;
25 using std::transform;
26 using std::endl;
27
28 namespace {
29
30 struct prefix_p {
31         string p;
32         prefix_p(string const & s)
33                 : p(s) {}
34         bool operator()(string const & s) const {
35                 return prefixIs(s, p);
36         }
37 };
38
39 } // end of anon namespace
40
41
42 ControlCommandBuffer::ControlCommandBuffer(LyXFunc & lf)
43         : lyxfunc_(lf), history_pos_(history_.end())
44 {
45         transform(lyxaction.func_begin(), lyxaction.func_end(),
46                 back_inserter(commands_), lyx::firster());
47 }
48
49
50 string const ControlCommandBuffer::historyUp()
51 {
52         if (history_pos_ == history_.begin())
53                 return string();
54
55         return *(--history_pos_);
56 }
57
58
59 string const ControlCommandBuffer::historyDown()
60 {
61         if (history_pos_ == history_.end())
62                 return "";
63         if (history_pos_ + 1 == history_.end())
64                 return "";
65
66         return *(++history_pos_);
67 }
68
69
70 vector<string> const
71 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 }