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);
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 unsigned currentColumn() const; ///< Return number of current column
67 virtual bool cb_init();
68 virtual void cb_windowSizeChanged();
71 virtual void v_keyReceived(keycode_t key) = 0;
73 virtual void cb_charReceived(char c);
75 void keySequenceTimeout();
79 void write(std::string const & s);
81 AbstractTerminal * terminal_;
84 std::string inputBuffer_;
85 ClockService::clock_type keyTimeout_;
86 scheduler::TimerEvent timer_;
94 ///////////////////////////////////////////////////////////////////////////
97 typedef boost::function<void (LineEditor&)> KeyBinding;
98 typedef boost::function<void (std::string const &)> AcceptCallback;
100 static unsigned const MAX_HISTORY_SIZE = 1024u;
102 ///////////////////////////////////////////////////////////////////////////
104 LineEditor(AbstractTerminal & terminal, AcceptCallback cb);
106 ///////////////////////////////////////////////////////////////////////////
108 void prompt(std::string const & text);
109 void set(std::string const & text, unsigned pos = 0u);
111 // Overall edit control
117 void forceRedisplay();
119 // Cursor and display movement
120 void gotoChar(unsigned n);
121 void scrollTo(unsigned n);
124 void deleteChar(unsigned n=1);
125 void insert(char ch);
126 void insert(std::string const & text);
129 void pushHistory(std::string const & text);
134 std::string const & text();
136 unsigned displayPos();
140 void defineKey(keycode_t key, KeyBinding binding);
141 void unsetKey(keycode_t key);
144 virtual bool cb_init();
145 virtual void cb_windowSizeChanged();
146 virtual void v_keyReceived(keycode_t key);
148 typedef std::map<keycode_t, KeyBinding> KeyMap;
149 typedef std::vector<std::string> History;
152 bool redisplayNeeded_;
154 unsigned promptWidth_;
158 unsigned displayPos_;
160 AcceptCallback callback_;
163 unsigned historyPoint_;
168 void selfInsertCommand (LineEditor & editor);
169 void forwardChar (LineEditor & editor);
170 void backwardChar (LineEditor & editor);
171 void accept (LineEditor & editor);
172 void backwardDeleteChar (LineEditor & editor);
173 void deleteChar (LineEditor & editor);
174 void beginningOfLine (LineEditor & editor);
175 void endOfLine (LineEditor & editor);
176 void deleteToEndOfLine (LineEditor & editor);
177 void restartEdit (LineEditor & editor);
178 void prevHistory (LineEditor & editor);
179 void nextHistory (LineEditor & editor);
180 void clearScreen (LineEditor & editor);
186 ///////////////////////////////hh.e////////////////////////////////////////
187 //#include "Editor.cci"
188 //#include "Editor.ct"
189 //#include "Editor.cti"
196 // comment-column: 40
197 // c-file-style: "senf"
198 // indent-tabs-mode: nil
199 // ispell-local-dictionary: "american"
200 // compile-command: "scons -u test"