1 /* ###########################################################################
4 * read minibuffer.h for more
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2001 The LyX Team.
10 * ###########################################################################
18 #pragma implementation
21 #include "minibuffer.h"
23 #include "support/lyxalgo.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
27 #include "XFormsView.h"
29 #include "LyXAction.h"
30 #include "BufferView.h"
36 extern LyXAction lyxaction;
43 prefix(string const & s)
45 bool operator()(string const & s) const {
46 return prefixIs(s, p);
50 } // end of anon namespace
53 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
54 FL_Coord h, FL_Coord w)
55 : stored_(false), owner_(o), state_(spaces)
57 add(FL_NORMAL_INPUT, x, y, h, w);
58 timer.setTimeout(6000);
59 timer.timeout.connect(slot(this, &MiniBuffer::init));
60 stored_timer.setTimeout(1500);
61 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
66 void MiniBuffer::stored_slot()
70 set_input(stored_input);
75 void MiniBuffer::stored_set(string const & str)
83 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
88 char const * tmp = fl_get_input(ob);
89 string input = tmp ? tmp : "";
99 if (hist_iter != history_->end()) {
102 if (hist_iter == history_->end()) {
103 // no further history
105 set_input(_("[End of history]"));
107 set_input((*hist_iter));
111 if (hist_iter == history_->begin()) {
112 // no further history
114 set_input(_("[Beginning of history]"));
117 set_input((*hist_iter));
123 // Completion handling.
126 lyx::copy_if(completion_.begin(),
128 std::back_inserter(comp), prefix(input));
132 string const tmp = input + _(" [no match]");
135 } else if (comp.size() == 1) {
138 comp[0] + _(" [sole completion]");
142 // More that one match
143 // Find maximal avaliable prefix
144 string const tmp = comp[0];
146 test += tmp[test.length()];
147 while (test.length() < tmp.length()) {
149 lyx::copy_if(comp.begin(),
151 std::back_inserter(vtmp),
153 if (vtmp.size() != comp.size()) {
154 test.erase(test.length() - 1);
157 test += tmp[test.length()];
161 // How should the possible matches
163 std::copy(comp.begin(), comp.end(),
164 std::ostream_iterator<string>(std::cerr, "\n"));
171 owner_->view()->focus(true);
180 // This will go in again in a little while
181 // we need to be able to declare what types
182 // of argumetns LFUN's should have first. (Lgb)
183 // First check for match
184 vector<string>::const_iterator cit =
185 std::find(completion_.begin(),
188 if (cit == completion_.end()) {
191 string const tmp = input + _(" [no match]");
195 // Return the inputted string
197 owner_->view()->focus(true);
198 history_->push_back(input);
199 stringReady.emit(input);
207 // Depending on the input state spaces might not
215 string const tmp = input + _(" [no match]");
228 //lyxerr << "Unhandled minibuffer event!" << endl;
237 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
239 int key, void * /*xev*/)
241 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
242 return mini->peek_event(ob, event, key);
246 void MiniBuffer::prepare()
251 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
256 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
257 FL_Coord w, FL_Coord h)
261 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
262 fl_set_object_boxtype(obj, FL_DOWN_BOX);
263 fl_set_object_resize(obj, FL_RESIZE_ALL);
264 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
265 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
266 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
268 // To intercept Up, Down, Table for history
269 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
271 obj->wantkey = FL_KEY_TAB;
279 void MiniBuffer::message(string const & str)
282 string const ntext = strip(str);
283 if (!the_buffer->focus) {
290 void MiniBuffer::messagePush(string const & str)
297 void MiniBuffer::messagePop()
299 if (!text_stored.empty()) {
300 message(text_stored);
306 void MiniBuffer::addSet(string const & s1, string const & s2)
308 string const str = text + ' ' + s1 + ' ' + s2;
313 void MiniBuffer::getString(State spaces,
314 vector<string> const & completion,
315 vector<string> & history)
318 completion_ = completion;
320 hist_iter = history_->end();
325 void MiniBuffer::init()
327 // If we have focus, we don't want to change anything.
328 if (the_buffer->focus)
336 void MiniBuffer::activate()
338 fl_activate_object(the_buffer);
343 void MiniBuffer::deactivate()
346 fl_deactivate_object(the_buffer);
351 void MiniBuffer::redraw()
353 fl_redraw_object(the_buffer);
358 void MiniBuffer::set_input(string const & str)
360 fl_set_input(the_buffer, str.c_str());