Utils/Termlib: Implement LineEditor
[senf.git] / Utils / Termlib / Editor.cc
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Editor non-inline non-template implementation */
25
26 #include "Editor.hh"
27 //#include "Editor.ih"
28
29 // Custom includes
30 #include <senf/Utils/membind.hh>
31 #include <senf/Scheduler/Scheduler.hh>
32
33 //#include "Editor.mpp"
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 prefix_ senf::term::BaseEditor::BaseEditor(AbstractTerminal & terminal)
38     : terminal_ (&terminal),
39       keyTimeout_ (senf::ClockService::milliseconds(DEFAULT_KEY_TIMEOUT_MS)),
40       timer_ ("senf::term::BaseEditor::keySequenceTimeout", 
41               senf::membind(&BaseEditor::keySequenceTimeout, this)),
42       column_ (0u)
43 {
44     terminal_->setCallbacks(*this);
45 }
46
47 prefix_ void senf::term::BaseEditor::newline()
48 {
49     write("\r\n");
50     write(tifo_.getString(Terminfo::properties::ClrEol));
51     column_ = 0;
52 }
53
54 prefix_ void senf::term::BaseEditor::toColumn(unsigned c)
55 {
56     if (c >= width())
57         c = width();
58     if (c > column_) {
59         if (tifo_.hasProperty(Terminfo::properties::ParmRightCursor)) {
60             write(tifo_.formatString(Terminfo::properties::ParmRightCursor, c - column_));
61             column_ = c;
62         }
63         else {
64             char const * cuf1 (tifo_.getString(Terminfo::properties::CursorRight));
65             while (c > column_) {
66                 write(cuf1);
67                 ++column_;
68             }
69         }
70     }
71     else if (c < column_) {
72         if (tifo_.hasProperty(Terminfo::properties::ParmLeftCursor)) {
73             write(tifo_.formatString(Terminfo::properties::ParmLeftCursor, column_ - c));
74             column_ = c;
75         }
76         else {
77             char const * cub1 (tifo_.getString(Terminfo::properties::CursorLeft));
78             while (c < column_) {
79                 write(cub1);
80                 --column_;
81             }
82         }
83     }
84 }
85
86 prefix_ void senf::term::BaseEditor::put(char ch)
87 {
88     if (column_ >= width()-1)
89         return;
90     write(ch);
91     ++ column_;
92 }
93
94 prefix_ void senf::term::BaseEditor::put(std::string const & text)
95 {
96     if (text.size() > width()-column_-1) {
97         write(text.substr(0,width()-column_-1));
98         column_ = width() - 1;
99     }
100     else {
101         write(text);
102         column_ += text.size();
103     }
104 }
105
106 prefix_ void senf::term::BaseEditor::clearLine()
107 {
108     write("\r");
109     write(tifo_.getString(Terminfo::properties::ClrEol));
110     column_ = 0;
111 }
112
113 prefix_ void senf::term::BaseEditor::setBold()
114 {
115     if (tifo_.hasProperty(Terminfo::properties::EnterBoldMode) &&
116         tifo_.hasProperty(Terminfo::properties::ExitAttributeMode))
117         write(tifo_.getString(Terminfo::properties::EnterBoldMode));
118 }
119
120 prefix_ void senf::term::BaseEditor::setNormal()
121 {
122     if (tifo_.hasProperty(Terminfo::properties::EnterBoldMode) &&
123         tifo_.hasProperty(Terminfo::properties::ExitAttributeMode))
124         write(tifo_.getString(Terminfo::properties::ExitAttributeMode));
125 }
126
127 prefix_ unsigned senf::term::BaseEditor::currentColumn()
128    const
129 {
130     return column_;
131 }
132
133 prefix_ void senf::term::BaseEditor::cb_init()
134 {
135     tifo_.load(terminal_->terminalType());
136     keyParser_.load(tifo_);
137
138     typedef Terminfo::properties p;
139     if (! (tifo_.hasProperty(p::ClrEol) &&
140            (tifo_.hasProperty(p::ParmRightCursor) || tifo_.hasProperty(p::CursorRight)) &&
141            (tifo_.hasProperty(p::ParmLeftCursor) || tifo_.hasProperty(p::CursorLeft))))
142         throw Terminfo::InvalidTerminfoException();
143
144     if (tifo_.hasProperty(Terminfo::properties::KeypadXmit))
145         write(tifo_.getString(Terminfo::properties::KeypadXmit));
146 }
147
148 prefix_ void senf::term::BaseEditor::cb_charReceived(char c)
149 {
150     inputBuffer_ += c;
151     timer_.timeout(senf::scheduler::eventTime() + keyTimeout_);
152     processKeys();
153 }
154
155 prefix_ void senf::term::BaseEditor::cb_windowSizeChanged()
156 {
157     if (column_ >= width())
158         column_ = width()-1;
159 }
160
161 prefix_ void senf::term::BaseEditor::keySequenceTimeout()
162 {
163     while (!inputBuffer_.empty()) {
164         processKeys();
165         v_keyReceived(keycode_t(inputBuffer_[0]));
166         inputBuffer_.erase(0, 1);
167     }
168 }
169
170 prefix_ void senf::term::BaseEditor::processKeys()
171 {
172     do {
173         std::pair<senf::term::KeyParser::keycode_t, std::string::size_type> result
174             (keyParser_.lookup(inputBuffer_));
175         if (result.first == senf::term::KeyParser::Incomplete)
176             return;
177         v_keyReceived(result.first);
178         inputBuffer_.erase(0, result.second);
179     } while (! inputBuffer_.empty());
180     timer_.disable();
181 }
182
183 prefix_ unsigned senf::term::BaseEditor::width()
184 {
185     return terminal_->width();
186 }
187
188 prefix_ void senf::term::BaseEditor::write(char ch)
189 {
190     terminal_->write(ch);
191 }
192
193 prefix_ void senf::term::BaseEditor::write(std::string const & s)
194 {
195     for (std::string::const_iterator i (s.begin()); i != s.end(); ++i)
196         write(*i);
197 }
198
199 ///////////////////////////////////////////////////////////////////////////
200
201 prefix_ senf::term::LineEditor::LineEditor(AbstractTerminal & terminal, AcceptCallback cb)
202     : BaseEditor(terminal), enabled_ (true), prompt_ ("$"), promptWidth_ (1u), editWidth_ (0u), 
203       text_ (""), point_ (0u), displayPos_ (0u), lastKey_ (0u), callback_ (cb)
204 {
205     defineKey(KeyParser::Return,    &bindings::accept);
206     defineKey(KeyParser::Right,     &bindings::forwardChar);
207     defineKey(KeyParser::Left,      &bindings::backwardChar);
208     defineKey(KeyParser::Backspace, &bindings::backwardDeleteChar);
209     defineKey(KeyParser::Delete,    &bindings::deleteChar);
210     defineKey(KeyParser::Home,      &bindings::beginningOfLine);
211     defineKey(KeyParser::End,       &bindings::endOfLine);
212     defineKey(KeyParser::Ctrl('K'), &bindings::deleteToEndOfLine);
213     defineKey(KeyParser::Ctrl('A'), &bindings::beginningOfLine);
214     defineKey(KeyParser::Ctrl('E'), &bindings::endOfLine);
215     defineKey(KeyParser::Ctrl('D'), &bindings::deleteChar);
216     defineKey(KeyParser::Ctrl('C'), &bindings::restartEdit);
217 }
218
219 prefix_ void senf::term::LineEditor::prompt(std::string const & text)
220 {
221     prompt_ = text;
222     promptWidth_ = prompt_.size();
223     editWidth_ = width() - promptWidth_ - 3;
224     if (enabled_)
225         redisplay();
226 }
227
228 prefix_ void senf::term::LineEditor::set(std::string const & text, unsigned pos)
229 {
230     text_ = text;
231     point_ = pos;
232     if (point_ > text.size())
233         point_ = text.size();
234     displayPos_ = 0u;
235     if (point_ > editWidth_)
236         displayPos_ = point_ - editWidth_;
237     redisplay();
238 }
239
240 prefix_ void senf::term::LineEditor::show()
241 {
242     if (enabled_)
243         return;
244     enabled_ = true;
245     redisplay();
246 }
247
248 prefix_ void senf::term::LineEditor::hide()
249 {
250     if (! enabled_)
251         return;
252     clearLine();
253     enabled_ = false;
254 }
255
256 prefix_ void senf::term::LineEditor::accept()
257 {
258     if (enabled_)
259         newline();
260     hide();
261     callback_(text_);
262     clear();
263 }
264
265 prefix_ void senf::term::LineEditor::clear()
266 {
267     set("");
268 }
269
270 prefix_ void senf::term::LineEditor::redisplay()
271 {
272     redisplayNeeded_ = true;
273 }
274
275 prefix_ void senf::term::LineEditor::forceRedisplay()
276 {
277     if (! enabled_)
278         return;
279     clearLine();
280     setBold();
281     put(prompt_);
282     put( displayPos_ > 0 ? '<' : ' ' );
283     if (text_.size() > displayPos_ + editWidth_) {
284         toColumn(editWidth_ + promptWidth_ + 1);
285         put('>');
286         toColumn(promptWidth_ + 1);
287     }
288     setNormal();
289     put(text_.substr(displayPos_, editWidth_));
290     toColumn(point_ - displayPos_ + promptWidth_ + 1);
291     redisplayNeeded_ = false;
292 }
293
294 prefix_ void senf::term::LineEditor::gotoChar(unsigned n)
295 {
296     point_ = n;
297     if (point_ > text_.size())
298         point_ = text_.size();
299     if (point_ < displayPos_)
300         displayPos_ = point_;
301     if (point_ > displayPos_+editWidth_)
302         displayPos_ = point_-editWidth_;
303     redisplay();
304 }
305
306 prefix_ void senf::term::LineEditor::scrollTo(unsigned n)
307 {
308     displayPos_ = n;
309     if (displayPos_ > text_.size())
310         displayPos_ = text_.size();
311     if (point_ < displayPos_)
312         point_ = displayPos_;
313     if (point_ > displayPos_+editWidth_)
314         point_ = displayPos_+editWidth_;
315     redisplay();
316 }
317
318 prefix_ void senf::term::LineEditor::deleteChar(unsigned n)
319 {
320     if (point_ >= text_.size())
321         return;
322     text_.erase(point_, n);
323     redisplay();
324 }
325
326 prefix_ void senf::term::LineEditor::insert(char ch)
327 {
328     text_.insert(point_, std::string(1, ch));
329     gotoChar(point_+1);
330     redisplay();
331 }
332
333 prefix_ void senf::term::LineEditor::insert(std::string const & text)
334 {
335     text_.insert(point_, text);
336     gotoChar(point_+text.size());
337     redisplay();
338 }
339
340 prefix_ std::string const & senf::term::LineEditor::text()
341 {
342     return text_;
343 }
344
345 prefix_ unsigned senf::term::LineEditor::point()
346 {
347     return point_;
348 }
349
350 prefix_ unsigned senf::term::LineEditor::displayPos()
351 {
352     return displayPos_;
353 }
354
355 prefix_ senf::term::LineEditor::keycode_t senf::term::LineEditor::lastKey()
356 {
357     return lastKey_;
358 }
359
360 prefix_ void senf::term::LineEditor::defineKey(keycode_t key, KeyBinding binding)
361 {
362     bindings_[key] = binding;
363 }
364
365 prefix_ void senf::term::LineEditor::unsetKey(keycode_t key)
366 {
367     bindings_.erase(key);
368 }
369
370 prefix_ void senf::term::LineEditor::cb_init()
371 {
372     BaseEditor::cb_init();
373     editWidth_ = width() - promptWidth_ - 3;
374     forceRedisplay();
375 }
376
377 prefix_ void senf::term::LineEditor::cb_windowSizeChanged()
378 {
379     BaseEditor::cb_windowSizeChanged();
380     editWidth_ = width() - promptWidth_ - 3;
381     gotoChar(point_);
382     forceRedisplay();
383 }
384
385 prefix_ void senf::term::LineEditor::v_keyReceived(keycode_t key)
386 {
387     lastKey_ = key;
388     KeyMap::iterator i (bindings_.find(key));
389     if (i != bindings_.end())
390         i->second(*this);
391     else if (key >= ' ' && key < 256)
392         insert(char(key));
393     if (redisplayNeeded_)
394         forceRedisplay();
395 }
396
397 ///////////////////////////////////////////////////////////////////////////
398
399 prefix_ void senf::term::bindings::selfInsertCommand(LineEditor & editor)
400 {
401     LineEditor::keycode_t key (editor.lastKey());
402     if (key >= ' ' && key < 256)
403         editor.insert(key);
404 }
405
406 prefix_ void senf::term::bindings::forwardChar(LineEditor & editor)
407 {
408     editor.gotoChar(editor.point()+1);
409 }
410
411 prefix_ void senf::term::bindings::backwardChar(LineEditor & editor)
412 {
413     unsigned p (editor.point());
414     if (p>0)
415         editor.gotoChar(p-1);
416 }
417
418 prefix_ void senf::term::bindings::accept(LineEditor & editor)
419 {
420     editor.accept();
421 }
422
423 prefix_ void senf::term::bindings::backwardDeleteChar(LineEditor & editor)
424 {
425     unsigned p (editor.point());
426     if (p>0) {
427         editor.gotoChar(p-1);
428         editor.deleteChar();
429     }
430 }
431
432 prefix_ void senf::term::bindings::deleteChar(LineEditor & editor)
433 {
434     editor.deleteChar();
435 }
436
437 prefix_ void senf::term::bindings::beginningOfLine(LineEditor & editor)
438 {
439     editor.gotoChar(0u);
440 }
441
442 prefix_ void senf::term::bindings::endOfLine(LineEditor & editor)
443 {
444     editor.gotoChar(editor.text().size());
445 }
446
447 prefix_ void senf::term::bindings::deleteToEndOfLine(LineEditor & editor)
448 {
449     editor.deleteChar(editor.text().size()-editor.point());
450 }
451
452 prefix_ void senf::term::bindings::restartEdit(LineEditor & editor)
453 {
454     editor.newline();
455     editor.clear();
456     editor.redisplay();
457 }
458
459 ///////////////////////////////cc.e////////////////////////////////////////
460 #undef prefix_
461 //#include "Editor.mpp"
462
463 \f
464 // Local Variables:
465 // mode: c++
466 // fill-column: 100
467 // comment-column: 40
468 // c-file-style: "senf"
469 // indent-tabs-mode: nil
470 // ispell-local-dictionary: "american"
471 // compile-command: "scons -u test"
472 // End: