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"
33 extern LyXAction lyxaction;
40 prefix(string const & s)
42 bool operator()(string const & s) const {
43 return prefixIs(s, p);
47 } // end of anon namespace
50 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
51 FL_Coord h, FL_Coord w)
52 : stored_(false), owner_(o), state_(spaces)
54 add(FL_NORMAL_INPUT, x, y, h, w);
55 timer.setTimeout(6000);
56 timer.timeout.connect(slot(this, &MiniBuffer::init));
57 stored_timer.setTimeout(1500);
58 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
63 void MiniBuffer::stored_slot()
67 fl_set_input(the_buffer, stored_input.c_str());
72 void MiniBuffer::stored_set(string const & str)
80 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
85 char const * tmp = fl_get_input(ob);
86 string input = tmp ? tmp : "";
90 fl_set_input(ob, input.c_str());
96 if (hist_iter != history_->end()) {
99 if (hist_iter == history_->end()) {
100 // no further history
102 fl_set_input(ob, _("[End of history]"));
104 fl_set_input(ob, (*hist_iter).c_str());
108 if (hist_iter == history_->begin()) {
109 // no further history
111 fl_set_input(ob, _("[Beginning of history]"));
114 fl_set_input(ob, (*hist_iter).c_str());
120 // Completion handling.
123 lyx::copy_if(completion_.begin(),
125 std::back_inserter(comp), prefix(input));
129 string const tmp = input + _(" [no match]");
131 fl_set_input(ob, tmp.c_str());
132 } else if (comp.size() == 1) {
135 comp[0] + _(" [sole completion]");
137 fl_set_input(ob, tmp.c_str());
139 // More that one match
140 // Find maximal avaliable prefix
141 string const tmp = comp[0];
143 test += tmp[test.length()];
144 while (test.length() < tmp.length()) {
146 lyx::copy_if(comp.begin(),
148 std::back_inserter(vtmp),
150 if (vtmp.size() != comp.size()) {
151 test.erase(test.length() - 1);
154 test += tmp[test.length()];
156 fl_set_input(ob, test.c_str());
158 // How should the possible matches
160 std::copy(comp.begin(), comp.end(),
161 std::ostream_iterator<string>(std::cerr, "\n"));
168 owner_->view()->focus(true);
176 // First check for match
177 vector<string>::const_iterator cit =
178 std::find(completion_.begin(),
181 if (cit == completion_.end()) {
184 string const tmp = input + _(" [no match]");
185 fl_set_input(ob, tmp.c_str());
187 // Return the inputted string
189 owner_->view()->focus(true);
190 history_->push_back(input);
191 stringReady.emit(input);
197 // Depending on the input state spaces might not
205 string const tmp = input + _(" [no match]");
206 fl_set_input(ob, tmp.c_str());
218 //lyxerr << "Unhandled minibuffer event!" << endl;
227 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
229 int key, void * /*xev*/)
231 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
232 return mini->peek_event(ob, event, key);
236 void MiniBuffer::prepare()
239 fl_set_input(the_buffer, "");
241 fl_set_focus_object(owner_->getForm(), the_buffer);
245 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
246 FL_Coord w, FL_Coord h)
250 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
251 fl_set_object_boxtype(obj, FL_DOWN_BOX);
252 fl_set_object_resize(obj, FL_RESIZE_ALL);
253 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
254 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
255 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
257 // To intercept Up, Down, Table for history
258 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
260 obj->wantkey = FL_KEY_TAB;
262 fl_set_input(the_buffer, text.c_str());
268 void MiniBuffer::message(string const & str)
271 string const ntext = strip(str);
272 if (!the_buffer->focus) {
273 fl_set_input(the_buffer, ntext.c_str());
279 void MiniBuffer::messagePush(string const & str)
286 void MiniBuffer::messagePop()
288 if (!text_stored.empty()) {
289 message(text_stored);
295 void MiniBuffer::addSet(string const & s1, string const & s2)
297 string const str = text + ' ' + s1 + ' ' + s2;
302 void MiniBuffer::getString(State spaces,
303 vector<string> const & completion,
304 vector<string> & history)
307 completion_ = completion;
309 hist_iter = history_->end();
314 void MiniBuffer::init()
316 // If we have focus, we don't want to change anything.
317 if (the_buffer->focus)
325 void MiniBuffer::activate()
327 fl_activate_object(the_buffer);
328 fl_redraw_object(the_buffer);
332 void MiniBuffer::deactivate()
334 fl_redraw_object(the_buffer);
335 fl_deactivate_object(the_buffer);
339 void MiniBuffer::redraw()
341 fl_redraw_object(the_buffer);