1 /* ###########################################################################
4 * read minibuffer.h for more
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ###########################################################################
16 #pragma implementation
19 #include "minibuffer.h"
21 #include "support/lyxalgo.h"
22 #include "support/filetools.h"
25 #include "LyXAction.h"
26 #include "BufferView.h"
32 extern LyXAction lyxaction;
39 prefix(string const & s)
41 bool operator()(string const & s) const {
42 return prefixIs(s, p);
46 } // end of anon namespace
49 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
50 FL_Coord h, FL_Coord w)
51 : stored_(false), owner_(o), state_(spaces)
53 add(FL_NORMAL_INPUT, x, y, h, w);
54 timer.setTimeout(6000);
55 timer.timeout.connect(slot(this, &MiniBuffer::init));
56 stored_timer.setTimeout(1500);
57 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
62 void MiniBuffer::stored_slot()
66 fl_set_input(the_buffer, stored_input.c_str());
71 void MiniBuffer::stored_set(string const & str)
79 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
84 char const * tmp = fl_get_input(ob);
85 string input = tmp ? tmp : "";
89 fl_set_input(ob, input.c_str());
95 if (hist_iter != history_->end()) {
98 if (hist_iter == history_->end()) {
101 fl_set_input(ob, _("[End of history]"));
103 fl_set_input(ob, (*hist_iter).c_str());
107 if (hist_iter == history_->begin()) {
108 // no further history
110 fl_set_input(ob, _("[Beginning of history]"));
113 fl_set_input(ob, (*hist_iter).c_str());
119 // Completion handling.
122 lyx::copy_if(completion_.begin(),
124 back_inserter(comp), prefix(input));
128 string const tmp = input + _(" [no match]");
130 fl_set_input(ob, tmp.c_str());
131 } else if (comp.size() == 1) {
134 comp[0] + _(" [sole completion]");
136 fl_set_input(ob, tmp.c_str());
138 // More that one match
139 // Find maximal avaliable prefix
140 string const tmp = comp[0];
142 test += tmp[test.length()];
143 while (test.length() < tmp.length()) {
145 lyx::copy_if(comp.begin(),
149 if (vtmp.size() != comp.size()) {
150 test.erase(test.length() - 1);
153 test += tmp[test.length()];
155 fl_set_input(ob, test.c_str());
157 // How should the possible matches
159 std::copy(comp.begin(), comp.end(),
160 ostream_iterator<string>(cerr, "\n"));
167 owner_->view()->focus(true);
175 // First check for match
176 vector<string>::const_iterator cit =
177 std::find(completion_.begin(),
180 if (cit == completion_.end()) {
183 string const tmp = input + _(" [no match]");
184 fl_set_input(ob, tmp.c_str());
186 // Return the inputted string
188 owner_->view()->focus(true);
189 history_->push_back(input);
190 stringReady.emit(input);
196 // Depending on the input state spaces might not
204 string const tmp = input + _(" [no match]");
205 fl_set_input(ob, tmp.c_str());
217 //lyxerr << "Unhandled minibuffer event!" << endl;
226 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
228 int key, void * /*xev*/)
230 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
231 return mini->peek_event(ob, event, key);
235 void MiniBuffer::prepare()
238 fl_set_input(the_buffer, "");
240 fl_set_focus_object(owner_->getForm(), the_buffer);
244 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
245 FL_Coord w, FL_Coord h)
249 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
250 fl_set_object_boxtype(obj, FL_DOWN_BOX);
251 fl_set_object_resize(obj, FL_RESIZE_ALL);
252 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
253 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
254 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
256 // To intercept Up, Down, Table for history
257 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
259 obj->wantkey = FL_KEY_TAB;
261 fl_set_input(the_buffer, text.c_str());
267 void MiniBuffer::message(string const & str)
270 string const ntext = strip(str);
271 if (!the_buffer->focus) {
272 fl_set_input(the_buffer, ntext.c_str());
278 void MiniBuffer::messagePush(string const & str)
285 void MiniBuffer::messagePop()
287 if (!text_stored.empty()) {
288 message(text_stored);
294 void MiniBuffer::addSet(string const & s1, string const & s2)
296 string const str = text + ' ' + s1 + ' ' + s2;
301 void MiniBuffer::getString(State spaces,
302 vector<string> const & completion,
303 vector<string> & history)
306 completion_ = completion;
308 hist_iter = history_->end();
313 void MiniBuffer::init()
315 // If we have focus, we don't want to change anything.
316 if (the_buffer->focus)
324 void MiniBuffer::activate()
326 fl_activate_object(the_buffer);
327 fl_redraw_object(the_buffer);
331 void MiniBuffer::deactivate()
333 fl_redraw_object(the_buffer);
334 fl_deactivate_object(the_buffer);
338 void MiniBuffer::redraw()
340 fl_redraw_object(the_buffer);