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