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