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"
26 #include "XFormsView.h"
28 #include "LyXAction.h"
29 #include "BufferView.h"
35 extern LyXAction lyxaction;
42 prefix(string const & s)
44 bool operator()(string const & s) const {
45 return prefixIs(s, p);
49 } // end of anon namespace
52 MiniBuffer::MiniBuffer(LyXView * o, FL_Coord x, FL_Coord y,
53 FL_Coord h, FL_Coord w)
54 : stored_(false), owner_(o), state_(spaces)
56 add(FL_NORMAL_INPUT, x, y, h, w);
57 timer.setTimeout(6000);
58 timer.timeout.connect(slot(this, &MiniBuffer::init));
59 stored_timer.setTimeout(1500);
60 stored_timer.timeout.connect(slot(this, &MiniBuffer::stored_slot));
65 void MiniBuffer::stored_slot()
69 fl_set_input(the_buffer, stored_input.c_str());
74 void MiniBuffer::stored_set(string const & str)
82 int MiniBuffer::peek_event(FL_OBJECT * ob, int event, int key)
87 char const * tmp = fl_get_input(ob);
88 string input = tmp ? tmp : "";
92 fl_set_input(ob, input.c_str());
98 if (hist_iter != history_->end()) {
101 if (hist_iter == history_->end()) {
102 // no further history
104 fl_set_input(ob, _("[End of history]"));
106 fl_set_input(ob, hist_iter->c_str());
110 if (hist_iter == history_->begin()) {
111 // no further history
113 fl_set_input(ob, _("[Beginning of history]"));
116 fl_set_input(ob, hist_iter->c_str());
122 // Completion handling.
125 lyx::copy_if(completion_.begin(),
127 std::back_inserter(comp), prefix(input));
131 string const tmp = input + _(" [no match]");
133 fl_set_input(ob, tmp.c_str());
134 } else if (comp.size() == 1) {
137 comp[0] + _(" [sole completion]");
139 fl_set_input(ob, tmp.c_str());
141 // More that one match
142 // Find maximal avaliable prefix
143 string const tmp = comp[0];
145 test += tmp[test.length()];
146 while (test.length() < tmp.length()) {
148 lyx::copy_if(comp.begin(),
150 std::back_inserter(vtmp),
152 if (vtmp.size() != comp.size()) {
153 test.erase(test.length() - 1);
156 test += tmp[test.length()];
158 fl_set_input(ob, test.c_str());
160 // How should the possible matches
162 std::copy(comp.begin(), comp.end(),
163 std::ostream_iterator<string>(std::cerr, "\n"));
170 owner_->view()->focus(true);
179 // This will go in again in a little while
180 // we need to be able to declare what types
181 // of argumetns LFUN's should have first. (Lgb)
182 // First check for match
183 vector<string>::const_iterator cit =
184 std::find(completion_.begin(),
187 if (cit == completion_.end()) {
190 string const tmp = input + _(" [no match]");
191 fl_set_input(ob, tmp.c_str());
194 // Return the inputted string
196 owner_->view()->focus(true);
197 history_->push_back(input);
198 stringReady.emit(input);
206 // Depending on the input state spaces might not
214 string const tmp = input + _(" [no match]");
215 fl_set_input(ob, tmp.c_str());
227 //lyxerr << "Unhandled minibuffer event!" << endl;
236 int C_MiniBuffer_peek_event(FL_OBJECT * ob, int event,
238 int key, void * /*xev*/)
240 MiniBuffer * mini = static_cast<MiniBuffer*>(ob->u_vdata);
241 return mini->peek_event(ob, event, key);
245 void MiniBuffer::prepare()
248 fl_set_input(the_buffer, "");
250 fl_set_focus_object(static_cast<XFormsView *>(owner_)->getForm(),
255 FL_OBJECT * MiniBuffer::add(int type, FL_Coord x, FL_Coord y,
256 FL_Coord w, FL_Coord h)
260 the_buffer = obj = fl_add_input(type, x, y, w, h, text.c_str());
261 fl_set_object_boxtype(obj, FL_DOWN_BOX);
262 fl_set_object_resize(obj, FL_RESIZE_ALL);
263 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
264 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
265 fl_set_object_lsize(obj, FL_NORMAL_SIZE);
267 // To intercept Up, Down, Table for history
268 fl_set_object_prehandler(obj, C_MiniBuffer_peek_event);
270 obj->wantkey = FL_KEY_TAB;
272 fl_set_input(the_buffer, text.c_str());
278 void MiniBuffer::message(string const & str)
281 string const ntext = strip(str);
282 if (!the_buffer->focus) {
283 fl_set_input(the_buffer, ntext.c_str());
289 void MiniBuffer::messagePush(string const & str)
296 void MiniBuffer::messagePop()
298 if (!text_stored.empty()) {
299 message(text_stored);
305 void MiniBuffer::addSet(string const & s1, string const & s2)
307 string const str = text + ' ' + s1 + ' ' + s2;
312 void MiniBuffer::getString(State spaces,
313 vector<string> const & completion,
314 vector<string> & history)
317 completion_ = completion;
319 hist_iter = history_->end();
324 void MiniBuffer::init()
326 // If we have focus, we don't want to change anything.
327 if (the_buffer->focus)
335 void MiniBuffer::activate()
337 fl_activate_object(the_buffer);
338 fl_redraw_object(the_buffer);
342 void MiniBuffer::deactivate()
344 fl_redraw_object(the_buffer);
345 fl_deactivate_object(the_buffer);
349 void MiniBuffer::redraw()
351 fl_redraw_object(the_buffer);