1 /* ###########################################################################
4 * read minibuffer.h for more
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ###########################################################################
18 #pragma implementation
21 #include "minibuffer.h"
23 #include "support/lyxalgo.h"
24 #include "support/filetools.h"
27 #include "LyXAction.h"
28 #include "BufferView.h"
34 extern LyXAction lyxaction;
41 prefix(string const & s)
43 bool operator()(string const & s) const {
44 return prefixIs(s, p);
48 } // end of anon namespace
51 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
52 FL_Coord h, FL_Coord w)
53 : stored_(false), owner_(o), state_(spaces)
55 add(FL_NORMAL_INPUT, x, y, h, w);
56 timer.setTimeout(6000);
57 timer.timeout.connect(slot(this, &MiniBuffer::init));
58 stored_timer.setTimeout(1500);
59 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
64 void MiniBuffer::stored_slot()
68 fl_set_input(the_buffer, stored_input.c_str());
73 void MiniBuffer::stored_set(string const & str)
81 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
86 char const * tmp = fl_get_input(ob);
87 string input = tmp ? tmp : "";
91 fl_set_input(ob, input.c_str());
97 if (hist_iter != history_->end()) {
100 if (hist_iter == history_->end()) {
101 // no further history
103 fl_set_input(ob, _("[End of history]"));
105 fl_set_input(ob, (*hist_iter).c_str());
109 if (hist_iter == history_->begin()) {
110 // no further history
112 fl_set_input(ob, _("[Beginning of history]"));
115 fl_set_input(ob, (*hist_iter).c_str());
121 // Completion handling.
124 lyx::copy_if(completion_.begin(),
126 std::back_inserter(comp), prefix(input));
130 string const tmp = input + _(" [no match]");
132 fl_set_input(ob, tmp.c_str());
133 } else if (comp.size() == 1) {
136 comp[0] + _(" [sole completion]");
138 fl_set_input(ob, tmp.c_str());
140 // More that one match
141 // Find maximal avaliable prefix
142 string const tmp = comp[0];
144 test += tmp[test.length()];
145 while (test.length() < tmp.length()) {
147 lyx::copy_if(comp.begin(),
149 std::back_inserter(vtmp),
151 if (vtmp.size() != comp.size()) {
152 test.erase(test.length() - 1);
155 test += tmp[test.length()];
157 fl_set_input(ob, test.c_str());
159 // How should the possible matches
161 std::copy(comp.begin(), comp.end(),
162 std::ostream_iterator<string>(std::cerr, "\n"));
169 owner_->view()->focus(true);
178 // This will go in again in a little while
179 // we need to be able to declare what types
180 // of argumetns LFUN's should have first. (Lgb)
181 // First check for match
182 vector<string>::const_iterator cit =
183 std::find(completion_.begin(),
186 if (cit == completion_.end()) {
189 string const tmp = input + _(" [no match]");
190 fl_set_input(ob, tmp.c_str());
193 // Return the inputted string
195 owner_->view()->focus(true);
196 history_->push_back(input);
197 stringReady.emit(input);
205 // Depending on the input state spaces might not
213 string const tmp = input + _(" [no match]");
214 fl_set_input(ob, tmp.c_str());
226 //lyxerr << "Unhandled minibuffer event!" << endl;
235 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
237 int key, void * /*xev*/)
239 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
240 return mini->peek_event(ob, event, key);
244 void MiniBuffer::prepare()
247 fl_set_input(the_buffer, "");
249 fl_set_focus_object(owner_->getForm(), the_buffer);
253 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
254 FL_Coord w, FL_Coord h)
258 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
259 fl_set_object_boxtype(obj, FL_DOWN_BOX);
260 fl_set_object_resize(obj, FL_RESIZE_ALL);
261 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
262 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
263 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
265 // To intercept Up, Down, Table for history
266 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
268 obj->wantkey = FL_KEY_TAB;
270 fl_set_input(the_buffer, text.c_str());
276 void MiniBuffer::message(string const & str)
279 string const ntext = strip(str);
280 if (!the_buffer->focus) {
281 fl_set_input(the_buffer, ntext.c_str());
287 void MiniBuffer::messagePush(string const & str)
294 void MiniBuffer::messagePop()
296 if (!text_stored.empty()) {
297 message(text_stored);
303 void MiniBuffer::addSet(string const & s1, string const & s2)
305 string const str = text + ' ' + s1 + ' ' + s2;
310 void MiniBuffer::getString(State spaces,
311 vector<string> const & completion,
312 vector<string> & history)
315 completion_ = completion;
317 hist_iter = history_->end();
322 void MiniBuffer::init()
324 // If we have focus, we don't want to change anything.
325 if (the_buffer->focus)
333 void MiniBuffer::activate()
335 fl_activate_object(the_buffer);
336 fl_redraw_object(the_buffer);
340 void MiniBuffer::deactivate()
342 fl_redraw_object(the_buffer);
343 fl_deactivate_object(the_buffer);
347 void MiniBuffer::redraw()
349 fl_redraw_object(the_buffer);