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 public header */
26 #ifndef HH_SENF_Utils_Termlib_Editor_
27 #define HH_SENF_Utils_Termlib_Editor_ 1
33 #include <senf/Scheduler/ClockService.hh>
34 #include <senf/Scheduler/TimerEvent.hh>
35 #include "AbstractTerminal.hh"
36 #include "Terminfo.hh"
38 //#include "Editor.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
45 : public AbstractTerminal::Callbacks
48 typedef KeyParser::keycode_t keycode_t;
50 static unsigned const DEFAULT_KEY_TIMEOUT_MS = 500u;
52 explicit BaseEditor(AbstractTerminal & terminal);
54 void newline(); ///< Move to beginning of a new, empty line
55 void toColumn(unsigned c); ///< Move cursor to column \p c
56 void put(char ch); ///< Write \p ch at current column
57 void put(std::string const & text); ///< Write \a text starting at current column
58 void clearLine(); ///< Clear current line and move cursor to first column
59 void setBold(); ///< Set bold char display
60 void setNormal(); ///< Set normal char display
61 void maybeClrScr(); ///< Clear screen if possible
63 void toLine(unsigned l); ///< Move to relative display line \a l
64 void reset(); ///< Reset display area to single line
66 unsigned currentColumn() const; ///< Return number of current column
67 unsigned currentLine() const; ///< Return number of current relative line
68 unsigned width(); ///< Return current screen width
69 unsigned height(); ///< Return current screen height
72 virtual bool cb_init();
73 virtual void cb_windowSizeChanged();
76 virtual void v_keyReceived(keycode_t key) = 0;
78 virtual void cb_charReceived(char c);
80 void keySequenceTimeout();
84 void write(std::string const & s);
86 AbstractTerminal * terminal_;
89 std::string inputBuffer_;
90 ClockService::clock_type keyTimeout_;
91 scheduler::TimerEvent timer_;
93 unsigned displayHeight_;
101 ///////////////////////////////////////////////////////////////////////////
104 typedef boost::function<void (LineEditor&)> KeyBinding;
105 typedef boost::function<void (std::string const &)> AcceptCallback;
107 static unsigned const MAX_HISTORY_SIZE = 1024u;
109 ///////////////////////////////////////////////////////////////////////////
111 LineEditor(AbstractTerminal & terminal, AcceptCallback cb);
113 ///////////////////////////////////////////////////////////////////////////
115 void prompt(std::string const & text);
116 void set(std::string const & text, unsigned pos = 0u);
118 // Overall edit control
124 void forceRedisplay();
126 // Cursor and display movement
127 void gotoChar(unsigned n);
128 void scrollTo(unsigned n);
131 void deleteChar(unsigned n=1);
132 void insert(char ch);
133 void insert(std::string const & text);
136 void pushHistory(std::string const & text);
141 void auxDisplay(int line, std::string const & text);
142 unsigned maxAuxDisplayHeight();
143 void clearAuxDisplay();
146 std::string const & text();
148 unsigned displayPos();
152 void defineKey(keycode_t key, KeyBinding binding);
153 void unsetKey(keycode_t key);
156 virtual bool cb_init();
157 virtual void cb_windowSizeChanged();
158 virtual void v_keyReceived(keycode_t key);
160 typedef std::map<keycode_t, KeyBinding> KeyMap;
161 typedef std::vector<std::string> History;
162 typedef std::vector<std::string> AuxDisplay;
165 bool redisplayNeeded_;
167 unsigned promptWidth_;
171 unsigned displayPos_;
173 AcceptCallback callback_;
176 unsigned historyPoint_;
177 AuxDisplay auxDisplay_;
182 void selfInsertCommand (LineEditor & editor);
183 void forwardChar (LineEditor & editor);
184 void backwardChar (LineEditor & editor);
185 void accept (LineEditor & editor);
186 void backwardDeleteChar (LineEditor & editor);
187 void deleteChar (LineEditor & editor);
188 void beginningOfLine (LineEditor & editor);
189 void endOfLine (LineEditor & editor);
190 void deleteToEndOfLine (LineEditor & editor);
191 void restartEdit (LineEditor & editor);
192 void prevHistory (LineEditor & editor);
193 void nextHistory (LineEditor & editor);
194 void clearScreen (LineEditor & editor);
196 typedef boost::function<void (LineEditor &, unsigned b, unsigned e, std::vector<std::string> &)> Completer;
197 void complete (LineEditor & editor, Completer completer);
203 ///////////////////////////////hh.e////////////////////////////////////////
204 //#include "Editor.cci"
205 //#include "Editor.ct"
206 //#include "Editor.cti"
213 // comment-column: 40
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"