1 /*
2 * (c) 2004 by Kalle Wallin <kaw@linux.se>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 */
19 #include "wreadln.h"
20 #include "charset.h"
21 #include "screen_utils.h"
22 #include "config.h"
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
28 #define KEY_CTRL_A 1
29 #define KEY_CTRL_B 2
30 #define KEY_CTRL_C 3
31 #define KEY_CTRL_D 4
32 #define KEY_CTRL_E 5
33 #define KEY_CTRL_F 6
34 #define KEY_CTRL_G 7
35 #define KEY_CTRL_K 11
36 #define KEY_CTRL_N 14
37 #define KEY_CTRL_P 16
38 #define KEY_CTRL_U 21
39 #define KEY_CTRL_Z 26
40 #define KEY_BCKSPC 8
41 #define TAB 9
43 struct wreadln {
44 /** the ncurses window where this field is displayed */
45 WINDOW *const w;
47 /** the origin coordinates in the window */
48 unsigned x, y;
50 /** the screen width of the input field */
51 unsigned width;
53 /** is the input masked, i.e. characters displayed as '*'? */
54 const gboolean masked;
56 /** the byte position of the cursor */
57 size_t cursor;
59 /** the byte position displayed at the origin (for horizontal
60 scrolling) */
61 size_t start;
63 /** the current value */
64 gchar line[1024];
65 };
67 /** max items stored in the history list */
68 static const guint wrln_max_history_length = 32;
70 void *wrln_completion_callback_data = NULL;
71 wrln_gcmp_pre_cb_t wrln_pre_completion_callback = NULL;
72 wrln_gcmp_post_cb_t wrln_post_completion_callback = NULL;
74 /* move the cursor one step to the right */
75 static inline void cursor_move_right(struct wreadln *wr)
76 {
77 if (wr->cursor < strlen(wr->line)) {
78 ++wr->cursor;
79 if (wr->cursor >= (size_t)wr->width &&
80 wr->start < wr->cursor - wr->width + 1)
81 ++wr->start;
82 }
83 }
85 /* move the cursor one step to the left */
86 static inline void cursor_move_left(struct wreadln *wr)
87 {
88 if (wr->cursor > 0) {
89 if (wr->cursor == wr->start && wr->start > 0)
90 --wr->start;
91 --wr->cursor;
92 }
93 }
95 /* move the cursor to the end of the line */
96 static inline void cursor_move_to_eol(struct wreadln *wr)
97 {
98 wr->cursor = strlen(wr->line);
99 if (wr->cursor >= wr->width)
100 wr->start = wr->cursor - wr->width + 1;
101 }
103 /* draw line buffer and update cursor position */
104 static inline void drawline(const struct wreadln *wr)
105 {
106 wmove(wr->w, wr->y, wr->x);
107 /* clear input area */
108 whline(wr->w, ' ', wr->width);
109 /* print visible part of the line buffer */
110 if (wr->masked)
111 whline(wr->w, '*', utf8_width(wr->line) - wr->start);
112 else
113 waddnstr(wr->w, wr->line + wr->start, wr->width);
114 /* move the cursor to the correct position */
115 wmove(wr->w, wr->y, wr->x + wr->cursor - wr->start);
116 /* tell ncurses to redraw the screen */
117 doupdate();
118 }
120 static void
121 wreadln_insert_byte(struct wreadln *wr, gint key)
122 {
123 size_t rest = strlen(wr->line + wr->cursor) + 1;
124 const size_t length = 1;
126 memmove(wr->line + wr->cursor + length,
127 wr->line + wr->cursor, rest);
128 wr->line[wr->cursor] = key;
130 cursor_move_right(wr);
131 }
133 /* libcurses version */
135 static gchar *
136 _wreadln(WINDOW *w,
137 const gchar *prompt,
138 const gchar *initial_value,
139 unsigned x1,
140 GList **history,
141 GCompletion *gcmp,
142 gboolean masked)
143 {
144 struct wreadln wr = {
145 .w = w,
146 .masked = masked,
147 .cursor = 0,
148 .start = 0,
149 };
150 GList *hlist = NULL, *hcurrent = NULL;
151 gint key = 0;
152 size_t i;
154 /* turn off echo */
155 noecho();
156 /* make shure the cursor is visible */
157 curs_set(1);
158 /* print prompt string */
159 if (prompt)
160 waddstr(w, prompt);
161 /* retrive y and x0 position */
162 getyx(w, wr.y, wr.x);
163 /* check the x1 value */
164 if (x1 <= wr.x || x1 > (unsigned)COLS)
165 x1 = COLS;
166 wr.width = x1 - wr.x;
167 /* clear input area */
168 mvwhline(w, wr.y, wr.x, ' ', wr.width);
170 if (history) {
171 /* append the a new line to our history list */
172 *history = g_list_append(*history, g_malloc0(sizeof(wr.line)));
173 /* hlist points to the current item in the history list */
174 hlist = g_list_last(*history);
175 hcurrent = hlist;
176 }
178 if (initial_value == (char *)-1) {
179 /* get previous history entry */
180 if (history && hlist->prev) {
181 if (hlist == hcurrent)
182 /* save the current line */
183 g_strlcpy(hlist->data, wr.line, sizeof(wr.line));
185 /* get previous line */
186 hlist = hlist->prev;
187 g_strlcpy(wr.line, hlist->data, sizeof(wr.line));
188 }
189 cursor_move_to_eol(&wr);
190 drawline(&wr);
191 } else if (initial_value) {
192 /* copy the initial value to the line buffer */
193 g_strlcpy(wr.line, initial_value, sizeof(wr.line));
194 cursor_move_to_eol(&wr);
195 drawline(&wr);
196 }
198 while (key != 13 && key != '\n') {
199 key = wgetch(w);
201 /* check if key is a function key */
202 for (i = 0; i < 63; i++)
203 if (key == (int)KEY_F(i)) {
204 key = KEY_F(1);
205 i = 64;
206 }
208 switch (key) {
209 #ifdef HAVE_GETMOUSE
210 case KEY_MOUSE: /* ignore mouse events */
211 #endif
212 case ERR: /* ingnore errors */
213 break;
215 case TAB:
216 if (gcmp) {
217 char *prefix = NULL;
218 GList *list;
220 if (wrln_pre_completion_callback)
221 wrln_pre_completion_callback(gcmp, wr.line,
222 wrln_completion_callback_data);
223 list = g_completion_complete(gcmp, wr.line, &prefix);
224 if (prefix) {
225 g_strlcpy(wr.line, prefix, sizeof(wr.line));
226 cursor_move_to_eol(&wr);
227 g_free(prefix);
228 } else
229 screen_bell();
231 if (wrln_post_completion_callback)
232 wrln_post_completion_callback(gcmp, wr.line, list,
233 wrln_completion_callback_data);
234 }
235 break;
237 case KEY_CTRL_G:
238 screen_bell();
239 if (history) {
240 g_free(hcurrent->data);
241 hcurrent->data = NULL;
242 *history = g_list_delete_link(*history, hcurrent);
243 }
244 return NULL;
246 case KEY_LEFT:
247 case KEY_CTRL_B:
248 cursor_move_left(&wr);
249 break;
250 case KEY_RIGHT:
251 case KEY_CTRL_F:
252 cursor_move_right(&wr);
253 break;
254 case KEY_HOME:
255 case KEY_CTRL_A:
256 wr.cursor = 0;
257 wr.start = 0;
258 break;
259 case KEY_END:
260 case KEY_CTRL_E:
261 cursor_move_to_eol(&wr);
262 break;
263 case KEY_CTRL_K:
264 wr.line[wr.cursor] = 0;
265 break;
266 case KEY_CTRL_U:
267 wr.cursor = utf8_width(wr.line);
268 for (i = 0; i < wr.cursor; i++)
269 wr.line[i] = '\0';
270 wr.cursor = 0;
271 break;
272 case 127:
273 case KEY_BCKSPC: /* handle backspace: copy all */
274 case KEY_BACKSPACE: /* chars starting from curpos */
275 if (wr.cursor > 0) {/* - 1 from buf[n+1] to buf */
276 for (i = wr.cursor - 1; wr.line[i] != 0; i++)
277 wr.line[i] = wr.line[i + 1];
278 cursor_move_left(&wr);
279 }
280 break;
281 case KEY_DC: /* handle delete key. As above */
282 case KEY_CTRL_D:
283 if (wr.cursor <= utf8_width(wr.line) - 1) {
284 for (i = wr.cursor; wr.line[i] != 0; i++)
285 wr.line[i] = wr.line[i + 1];
286 }
287 break;
288 case KEY_UP:
289 case KEY_CTRL_P:
290 /* get previous history entry */
291 if (history && hlist->prev) {
292 if (hlist == hcurrent)
293 /* save the current line */
294 g_strlcpy(hlist->data, wr.line,
295 sizeof(wr.line));
297 /* get previous line */
298 hlist = hlist->prev;
299 g_strlcpy(wr.line, hlist->data,
300 sizeof(wr.line));
301 }
302 cursor_move_to_eol(&wr);
303 break;
304 case KEY_DOWN:
305 case KEY_CTRL_N:
306 /* get next history entry */
307 if (history && hlist->next) {
308 /* get next line */
309 hlist = hlist->next;
310 g_strlcpy(wr.line, hlist->data,
311 sizeof(wr.line));
312 }
313 cursor_move_to_eol(&wr);
314 break;
316 case '\n':
317 case 13:
318 case KEY_IC:
319 case KEY_PPAGE:
320 case KEY_NPAGE:
321 case KEY_F(1):
322 /* ignore char */
323 break;
324 default:
325 if (key >= 32)
326 wreadln_insert_byte(&wr, key);
327 }
329 drawline(&wr);
330 }
332 /* update history */
333 if (history) {
334 if (strlen(wr.line)) {
335 /* update the current history entry */
336 size_t size = strlen(wr.line) + 1;
337 hcurrent->data = g_realloc(hcurrent->data, size);
338 g_strlcpy(hcurrent->data, wr.line, size);
339 } else {
340 /* the line was empty - remove the current history entry */
341 g_free(hcurrent->data);
342 hcurrent->data = NULL;
343 *history = g_list_delete_link(*history, hcurrent);
344 }
346 while (g_list_length(*history) > wrln_max_history_length) {
347 GList *first = g_list_first(*history);
349 /* remove the oldest history entry */
350 g_free(first->data);
351 first->data = NULL;
352 *history = g_list_delete_link(*history, first);
353 }
354 }
356 return g_strdup(wr.line);
357 }
359 gchar *
360 wreadln(WINDOW *w,
361 const gchar *prompt,
362 const gchar *initial_value,
363 unsigned x1,
364 GList **history,
365 GCompletion *gcmp)
366 {
367 return _wreadln(w, prompt, initial_value, x1, history, gcmp, FALSE);
368 }
370 gchar *
371 wreadln_masked(WINDOW *w,
372 const gchar *prompt,
373 const gchar *initial_value,
374 unsigned x1,
375 GList **history,
376 GCompletion *gcmp)
377 {
378 return _wreadln(w, prompt, initial_value, x1, history, gcmp, TRUE);
379 }