Utils/Termlib: Implement LineEditor
[senf.git] / Utils / Termlib / Editor.hh
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 public header */
25
26 #ifndef HH_SENF_Utils_Termlib_Editor_
27 #define HH_SENF_Utils_Termlib_Editor_ 1
28
29 // Custom includes
30 #include <map>
31 #include <senf/Scheduler/ClockService.hh>
32 #include <senf/Scheduler/TimerEvent.hh>
33 #include "AbstractTerminal.hh"
34 #include "Terminfo.hh"
35
36 //#include "Editor.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace term {
41
42     class BaseEditor
43         : public AbstractTerminal::Callbacks
44     {
45     public:
46         typedef KeyParser::keycode_t keycode_t;
47
48         static unsigned const DEFAULT_KEY_TIMEOUT_MS = 500u;
49
50         explicit BaseEditor(AbstractTerminal & terminal);
51
52         void newline();                 ///< Move to beginning of a new, empty line
53         void toColumn(unsigned c);      ///< Move cursor to column \p c
54         void put(char ch);              ///< Write \p ch at current column
55         void put(std::string const & text);
56         void clearLine();               ///< Clear current line and move cursor to first column
57         void setBold();                 ///< Set bold char display
58         void setNormal();               ///< Set normal char display
59
60         unsigned currentColumn() const; ///< Return number of current column
61         unsigned width();
62
63     protected:
64         virtual void cb_init();
65         virtual void cb_windowSizeChanged();
66
67     private:
68         virtual void v_keyReceived(keycode_t key) = 0;
69
70         virtual void cb_charReceived(char c);
71
72         void keySequenceTimeout();
73         void processKeys();
74
75         void write(char ch);
76         void write(std::string const & s);
77         
78         AbstractTerminal * terminal_;
79         Terminfo tifo_;
80         KeyParser keyParser_;
81         std::string inputBuffer_;
82         ClockService::clock_type keyTimeout_;
83         scheduler::TimerEvent timer_;
84         unsigned column_;
85     };
86
87     class LineEditor
88         : public BaseEditor
89     {
90     public:
91         ///////////////////////////////////////////////////////////////////////////
92         // Types
93
94         typedef boost::function<void (LineEditor&)> KeyBinding;
95         typedef boost::function<void (std::string const &)> AcceptCallback;
96
97         ///////////////////////////////////////////////////////////////////////////
98
99         LineEditor(AbstractTerminal & terminal, AcceptCallback cb);
100         
101         ///////////////////////////////////////////////////////////////////////////
102
103         void prompt(std::string const & text);
104         void set(std::string const & text, unsigned pos = 0u);
105
106         // Overall edit control
107         void show();
108         void hide();
109         void accept();
110         void clear();
111         void redisplay();
112         void forceRedisplay();
113         
114         // Cursor and display movement
115         void gotoChar(unsigned n);
116         void scrollTo(unsigned n);
117
118         // Text manipulation
119         void deleteChar(unsigned n=1);
120         void insert(char ch);
121         void insert(std::string const & text);
122
123         // Get information
124         std::string const & text();
125         unsigned point();
126         unsigned displayPos();
127         keycode_t lastKey();
128
129         // Key bindings
130         void defineKey(keycode_t key, KeyBinding binding);
131         void unsetKey(keycode_t key);
132         
133     private:
134         virtual void cb_init();
135         virtual void cb_windowSizeChanged();
136         virtual void v_keyReceived(keycode_t key);
137
138         typedef std::map<keycode_t, KeyBinding> KeyMap;
139
140         bool enabled_;
141         bool redisplayNeeded_;
142         std::string prompt_;
143         unsigned promptWidth_;
144         unsigned editWidth_;
145         std::string text_;
146         unsigned point_;
147         unsigned displayPos_;
148         keycode_t lastKey_;
149         AcceptCallback callback_;
150         KeyMap bindings_;
151     };
152
153 namespace bindings {
154
155     void selfInsertCommand   (LineEditor & editor);
156     void forwardChar         (LineEditor & editor);
157     void backwardChar        (LineEditor & editor);
158     void accept              (LineEditor & editor);
159     void backwardDeleteChar  (LineEditor & editor);
160     void deleteChar          (LineEditor & editor);
161     void beginningOfLine     (LineEditor & editor);
162     void endOfLine           (LineEditor & editor);
163     void deleteToEndOfLine   (LineEditor & editor);
164     void restartEdit         (LineEditor & editor);
165
166 }
167
168 }}
169
170 ///////////////////////////////hh.e////////////////////////////////////////
171 //#include "Editor.cci"
172 //#include "Editor.ct"
173 //#include "Editor.cti"
174 #endif
175
176 \f
177 // Local Variables:
178 // mode: c++
179 // fill-column: 100
180 // comment-column: 40
181 // c-file-style: "senf"
182 // indent-tabs-mode: nil
183 // ispell-local-dictionary: "american"
184 // compile-command: "scons -u test"
185 // End: