4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Editor non-inline non-template implementation */
27 //#include "Editor.ih"
30 #include <senf/Utils/membind.hh>
31 #include <senf/Scheduler/Scheduler.hh>
33 //#include "Editor.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
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)),
44 terminal_->setCallbacks(*this);
47 prefix_ void senf::term::BaseEditor::newline()
50 write(tifo_.getString(Terminfo::properties::ClrEol));
54 prefix_ void senf::term::BaseEditor::toColumn(unsigned c)
59 if (tifo_.hasProperty(Terminfo::properties::ParmRightCursor)) {
60 write(tifo_.formatString(Terminfo::properties::ParmRightCursor, c - column_));
64 char const * cuf1 (tifo_.getString(Terminfo::properties::CursorRight));
71 else if (c < column_) {
72 if (tifo_.hasProperty(Terminfo::properties::ParmLeftCursor)) {
73 write(tifo_.formatString(Terminfo::properties::ParmLeftCursor, column_ - c));
77 char const * cub1 (tifo_.getString(Terminfo::properties::CursorLeft));
86 prefix_ void senf::term::BaseEditor::put(char ch)
88 if (column_ >= width()-1)
94 prefix_ void senf::term::BaseEditor::put(std::string const & text)
96 if (text.size() > width()-column_-1) {
97 write(text.substr(0,width()-column_-1));
98 column_ = width() - 1;
102 column_ += text.size();
106 prefix_ void senf::term::BaseEditor::clearLine()
109 write(tifo_.getString(Terminfo::properties::ClrEol));
113 prefix_ void senf::term::BaseEditor::setBold()
115 if (tifo_.hasProperty(Terminfo::properties::EnterBoldMode) &&
116 tifo_.hasProperty(Terminfo::properties::ExitAttributeMode))
117 write(tifo_.getString(Terminfo::properties::EnterBoldMode));
120 prefix_ void senf::term::BaseEditor::setNormal()
122 if (tifo_.hasProperty(Terminfo::properties::EnterBoldMode) &&
123 tifo_.hasProperty(Terminfo::properties::ExitAttributeMode))
124 write(tifo_.getString(Terminfo::properties::ExitAttributeMode));
127 prefix_ unsigned senf::term::BaseEditor::currentColumn()
133 prefix_ bool senf::term::BaseEditor::cb_init()
136 tifo_.load(terminal_->terminalType());
137 keyParser_.load(tifo_);
139 catch (Terminfo::InvalidTerminfoException & ex) {
143 typedef Terminfo::properties p;
144 if (! (tifo_.hasProperty(p::ClrEol) &&
145 (tifo_.hasProperty(p::ParmRightCursor) || tifo_.hasProperty(p::CursorRight)) &&
146 (tifo_.hasProperty(p::ParmLeftCursor) || tifo_.hasProperty(p::CursorLeft))))
149 if (tifo_.hasProperty(Terminfo::properties::KeypadXmit))
150 write(tifo_.getString(Terminfo::properties::KeypadXmit));
153 prefix_ void senf::term::BaseEditor::cb_charReceived(char c)
156 timer_.timeout(senf::scheduler::eventTime() + keyTimeout_);
160 prefix_ void senf::term::BaseEditor::cb_windowSizeChanged()
162 if (column_ >= width())
166 prefix_ void senf::term::BaseEditor::keySequenceTimeout()
168 while (!inputBuffer_.empty()) {
170 v_keyReceived(keycode_t(inputBuffer_[0]));
171 inputBuffer_.erase(0, 1);
175 prefix_ void senf::term::BaseEditor::processKeys()
178 std::pair<senf::term::KeyParser::keycode_t, std::string::size_type> result
179 (keyParser_.lookup(inputBuffer_));
180 if (result.first == senf::term::KeyParser::Incomplete)
182 v_keyReceived(result.first);
183 inputBuffer_.erase(0, result.second);
184 } while (! inputBuffer_.empty());
188 prefix_ unsigned senf::term::BaseEditor::width()
190 return terminal_->width();
193 prefix_ void senf::term::BaseEditor::write(char ch)
195 terminal_->write(ch);
198 prefix_ void senf::term::BaseEditor::write(std::string const & s)
200 for (std::string::const_iterator i (s.begin()); i != s.end(); ++i)
204 ///////////////////////////////////////////////////////////////////////////
206 prefix_ senf::term::LineEditor::LineEditor(AbstractTerminal & terminal, AcceptCallback cb)
207 : BaseEditor(terminal), enabled_ (true), prompt_ ("$"), promptWidth_ (1u), editWidth_ (0u),
208 text_ (""), point_ (0u), displayPos_ (0u), lastKey_ (0u), callback_ (cb)
210 defineKey(KeyParser::Return, &bindings::accept);
211 defineKey(KeyParser::Right, &bindings::forwardChar);
212 defineKey(KeyParser::Left, &bindings::backwardChar);
213 defineKey(KeyParser::Backspace, &bindings::backwardDeleteChar);
214 defineKey(KeyParser::Delete, &bindings::deleteChar);
215 defineKey(KeyParser::Home, &bindings::beginningOfLine);
216 defineKey(KeyParser::End, &bindings::endOfLine);
217 defineKey(KeyParser::Ctrl('K'), &bindings::deleteToEndOfLine);
218 defineKey(KeyParser::Ctrl('A'), &bindings::beginningOfLine);
219 defineKey(KeyParser::Ctrl('E'), &bindings::endOfLine);
220 defineKey(KeyParser::Ctrl('D'), &bindings::deleteChar);
221 defineKey(KeyParser::Ctrl('C'), &bindings::restartEdit);
224 prefix_ void senf::term::LineEditor::prompt(std::string const & text)
227 promptWidth_ = prompt_.size();
228 if (promptWidth_ > width() - 4 && width() > 4)
229 promptWidth_ = width() - 4;
230 editWidth_ = width() - promptWidth_ - 3;
235 prefix_ void senf::term::LineEditor::set(std::string const & text, unsigned pos)
239 if (point_ > text.size())
240 point_ = text.size();
242 if (point_ > editWidth_)
243 displayPos_ = point_ - editWidth_;
247 prefix_ void senf::term::LineEditor::show()
255 prefix_ void senf::term::LineEditor::hide()
263 prefix_ void senf::term::LineEditor::accept()
272 prefix_ void senf::term::LineEditor::clear()
277 prefix_ void senf::term::LineEditor::redisplay()
279 redisplayNeeded_ = true;
282 prefix_ void senf::term::LineEditor::forceRedisplay()
288 if (prompt_.size() > promptWidth_)
289 put(prompt_.substr(prompt_.size()-promptWidth_));
292 put( displayPos_ > 0 ? '<' : ' ' );
293 if (text_.size() > displayPos_ + editWidth_) {
294 toColumn(editWidth_ + promptWidth_ + 1);
296 toColumn(promptWidth_ + 1);
299 put(text_.substr(displayPos_, editWidth_));
300 toColumn(point_ - displayPos_ + promptWidth_ + 1);
301 redisplayNeeded_ = false;
304 prefix_ void senf::term::LineEditor::gotoChar(unsigned n)
307 if (point_ > text_.size())
308 point_ = text_.size();
309 if (point_ < displayPos_)
310 displayPos_ = point_;
311 if (point_ > displayPos_+editWidth_)
312 displayPos_ = point_-editWidth_;
316 prefix_ void senf::term::LineEditor::scrollTo(unsigned n)
319 if (displayPos_ > text_.size())
320 displayPos_ = text_.size();
321 if (point_ < displayPos_)
322 point_ = displayPos_;
323 if (point_ > displayPos_+editWidth_)
324 point_ = displayPos_+editWidth_;
328 prefix_ void senf::term::LineEditor::deleteChar(unsigned n)
330 if (point_ >= text_.size())
332 text_.erase(point_, n);
336 prefix_ void senf::term::LineEditor::insert(char ch)
338 text_.insert(point_, std::string(1, ch));
343 prefix_ void senf::term::LineEditor::insert(std::string const & text)
345 text_.insert(point_, text);
346 gotoChar(point_+text.size());
350 prefix_ std::string const & senf::term::LineEditor::text()
355 prefix_ unsigned senf::term::LineEditor::point()
360 prefix_ unsigned senf::term::LineEditor::displayPos()
365 prefix_ senf::term::LineEditor::keycode_t senf::term::LineEditor::lastKey()
370 prefix_ void senf::term::LineEditor::defineKey(keycode_t key, KeyBinding binding)
372 bindings_[key] = binding;
375 prefix_ void senf::term::LineEditor::unsetKey(keycode_t key)
377 bindings_.erase(key);
380 prefix_ bool senf::term::LineEditor::cb_init()
382 if (!BaseEditor::cb_init())
389 prefix_ void senf::term::LineEditor::cb_windowSizeChanged()
391 BaseEditor::cb_windowSizeChanged();
397 prefix_ void senf::term::LineEditor::v_keyReceived(keycode_t key)
402 KeyMap::iterator i (bindings_.find(key));
403 if (i != bindings_.end())
405 else if (key >= ' ' && key < 256)
407 if (redisplayNeeded_)
411 ///////////////////////////////////////////////////////////////////////////
413 prefix_ void senf::term::bindings::selfInsertCommand(LineEditor & editor)
415 LineEditor::keycode_t key (editor.lastKey());
416 if (key >= ' ' && key < 256)
420 prefix_ void senf::term::bindings::forwardChar(LineEditor & editor)
422 editor.gotoChar(editor.point()+1);
425 prefix_ void senf::term::bindings::backwardChar(LineEditor & editor)
427 unsigned p (editor.point());
429 editor.gotoChar(p-1);
432 prefix_ void senf::term::bindings::accept(LineEditor & editor)
437 prefix_ void senf::term::bindings::backwardDeleteChar(LineEditor & editor)
439 unsigned p (editor.point());
441 editor.gotoChar(p-1);
446 prefix_ void senf::term::bindings::deleteChar(LineEditor & editor)
451 prefix_ void senf::term::bindings::beginningOfLine(LineEditor & editor)
456 prefix_ void senf::term::bindings::endOfLine(LineEditor & editor)
458 editor.gotoChar(editor.text().size());
461 prefix_ void senf::term::bindings::deleteToEndOfLine(LineEditor & editor)
463 editor.deleteChar(editor.text().size()-editor.point());
466 prefix_ void senf::term::bindings::restartEdit(LineEditor & editor)
473 ///////////////////////////////cc.e////////////////////////////////////////
475 //#include "Editor.mpp"
481 // comment-column: 40
482 // c-file-style: "senf"
483 // indent-tabs-mode: nil
484 // ispell-local-dictionary: "american"
485 // compile-command: "scons -u test"