Code

wreadln: use memmove() instead of an temporary buffer
[ncmpc.git] / src / wreadln.c
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         gint x, y;
50         /** the screen width of the input field */
51         gint width;
53         /** is the input masked, i.e. characters displayed as '*'? */
54         const gboolean masked;
56         /** the byte position of the cursor */
57         gint cursor;
59         /** the byte position displayed at the origin (for horizontal
60             scrolling) */
61         gint 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 < (int)strlen(wr->line)) {
78                 ++wr->cursor;
79                 if (wr->cursor >= 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;
103 /* draw line buffer and update cursor position */
104 static inline void drawline(const struct wreadln *wr)
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();
120 static void
121 wreadln_insert_byte(struct wreadln *wr, gint key)
123         if (strlen(wr->line + wr->cursor)) { /* if the cursor is */
124                 /* not at the last pos */
125                 gsize rest = strlen(wr->line + wr->cursor) + 1;
127                 memmove(wr->line + wr->cursor + 1,
128                         wr->line + wr->cursor, rest);
129                 wr->line[wr->cursor] = key;
130         } else {
131                 wr->line[wr->cursor + 1] = 0;
132                 wr->line[wr->cursor] = key;
133         }
135         cursor_move_right(wr);
138 /* libcurses version */
140 static gchar *
141 _wreadln(WINDOW *w,
142          const gchar *prompt,
143          const gchar *initial_value,
144          gint x1,
145          GList **history,
146          GCompletion *gcmp,
147          gboolean masked)
149         struct wreadln wr = {
150                 .w = w,
151                 .masked = masked,
152                 .cursor = 0,
153                 .start = 0,
154         };
155         GList *hlist = NULL, *hcurrent = NULL;
156         gint key = 0, i;
158         /* turn off echo */
159         noecho();
160         /* make shure the cursor is visible */
161         curs_set(1);
162         /* print prompt string */
163         if (prompt)
164                 waddstr(w, prompt);
165         /* retrive y and x0 position */
166         getyx(w, wr.y, wr.x);
167         /* check the x1 value */
168         if (x1 <= wr.x || x1 > COLS)
169                 x1 = COLS;
170         wr.width = x1 - wr.x;
171         /* clear input area */
172         mvwhline(w, wr.y, wr.x, ' ', wr.width);
174         if (history) {
175                 /* append the a new line to our history list */
176                 *history = g_list_append(*history, g_malloc0(sizeof(wr.line)));
177                 /* hlist points to the current item in the history list */
178                 hlist = g_list_last(*history);
179                 hcurrent = hlist;
180         }
182         if (initial_value == (char *)-1) {
183                 /* get previous history entry */
184                 if (history && hlist->prev) {
185                         if (hlist == hcurrent)
186                                 /* save the current line */
187                                 g_strlcpy(hlist->data, wr.line, sizeof(wr.line));
189                         /* get previous line */
190                         hlist = hlist->prev;
191                         g_strlcpy(wr.line, hlist->data, sizeof(wr.line));
192                 }
193                 cursor_move_to_eol(&wr);
194                 drawline(&wr);
195         } else if (initial_value) {
196                 /* copy the initial value to the line buffer */
197                 g_strlcpy(wr.line, initial_value, sizeof(wr.line));
198                 cursor_move_to_eol(&wr);
199                 drawline(&wr);
200         }
202         while (key != 13 && key != '\n') {
203                 key = wgetch(w);
205                 /* check if key is a function key */
206                 for (i = 0; i < 63; i++)
207                         if (key == KEY_F(i)) {
208                                 key = KEY_F(1);
209                                 i = 64;
210                         }
212                 switch (key) {
213 #ifdef HAVE_GETMOUSE
214                 case KEY_MOUSE: /* ignore mouse events */
215 #endif
216                 case ERR: /* ingnore errors */
217                         break;
219                 case TAB:
220                         if (gcmp) {
221                                 char *prefix = NULL;
222                                 GList *list;
224                                 if (wrln_pre_completion_callback)
225                                         wrln_pre_completion_callback(gcmp, wr.line,
226                                                                      wrln_completion_callback_data);
227                                 list = g_completion_complete(gcmp, wr.line, &prefix);
228                                 if (prefix) {
229                                         g_strlcpy(wr.line, prefix, sizeof(wr.line));
230                                         cursor_move_to_eol(&wr);
231                                         g_free(prefix);
232                                 } else
233                                         screen_bell();
235                                 if (wrln_post_completion_callback)
236                                         wrln_post_completion_callback(gcmp, wr.line, list,
237                                                                       wrln_completion_callback_data);
238                         }
239                         break;
241                 case KEY_CTRL_G:
242                         screen_bell();
243                         if (history) {
244                                 g_free(hcurrent->data);
245                                 hcurrent->data = NULL;
246                                 *history = g_list_delete_link(*history, hcurrent);
247                         }
248                         return NULL;
250                 case KEY_LEFT:
251                 case KEY_CTRL_B:
252                         cursor_move_left(&wr);
253                         break;
254                 case KEY_RIGHT:
255                 case KEY_CTRL_F:
256                         cursor_move_right(&wr);
257                         break;
258                 case KEY_HOME:
259                 case KEY_CTRL_A:
260                         wr.cursor = 0;
261                         wr.start = 0;
262                         break;
263                 case KEY_END:
264                 case KEY_CTRL_E:
265                         cursor_move_to_eol(&wr);
266                         break;
267                 case KEY_CTRL_K:
268                         wr.line[wr.cursor] = 0;
269                         break;
270                 case KEY_CTRL_U:
271                         wr.cursor = utf8_width(wr.line);
272                         for (i = 0; i < wr.cursor; i++)
273                                 wr.line[i] = '\0';
274                         wr.cursor = 0;
275                         break;
276                 case 127:
277                 case KEY_BCKSPC:        /* handle backspace: copy all */
278                 case KEY_BACKSPACE:     /* chars starting from curpos */
279                         if (wr.cursor > 0) {/* - 1 from buf[n+1] to buf   */
280                                 for (i = wr.cursor - 1; wr.line[i] != 0; i++)
281                                         wr.line[i] = wr.line[i + 1];
282                                 cursor_move_left(&wr);
283                         }
284                         break;
285                 case KEY_DC:            /* handle delete key. As above */
286                 case KEY_CTRL_D:
287                         if (wr.cursor <= (gint)utf8_width(wr.line) - 1) {
288                                 for (i = wr.cursor; wr.line[i] != 0; i++)
289                                         wr.line[i] = wr.line[i + 1];
290                         }
291                         break;
292                 case KEY_UP:
293                 case KEY_CTRL_P:
294                         /* get previous history entry */
295                         if (history && hlist->prev) {
296                                 if (hlist == hcurrent)
297                                         /* save the current line */
298                                         g_strlcpy(hlist->data, wr.line,
299                                                   sizeof(wr.line));
301                                 /* get previous line */
302                                 hlist = hlist->prev;
303                                 g_strlcpy(wr.line, hlist->data,
304                                           sizeof(wr.line));
305                         }
306                         cursor_move_to_eol(&wr);
307                         break;
308                 case KEY_DOWN:
309                 case KEY_CTRL_N:
310                         /* get next history entry */
311                         if (history && hlist->next) {
312                                 /* get next line */
313                                 hlist = hlist->next;
314                                 g_strlcpy(wr.line, hlist->data,
315                                           sizeof(wr.line));
316                         }
317                         cursor_move_to_eol(&wr);
318                         break;
320                 case '\n':
321                 case 13:
322                 case KEY_IC:
323                 case KEY_PPAGE:
324                 case KEY_NPAGE:
325                 case KEY_F(1):
326                         /* ignore char */
327                         break;
328                 default:
329                         if (key >= 32)
330                                 wreadln_insert_byte(&wr, key);
331                 }
333                 drawline(&wr);
334         }
336         /* update history */
337         if (history) {
338                 if (strlen(wr.line)) {
339                         /* update the current history entry */
340                         size_t size = strlen(wr.line) + 1;
341                         hcurrent->data = g_realloc(hcurrent->data, size);
342                         g_strlcpy(hcurrent->data, wr.line, size);
343                 } else {
344                         /* the line was empty - remove the current history entry */
345                         g_free(hcurrent->data);
346                         hcurrent->data = NULL;
347                         *history = g_list_delete_link(*history, hcurrent);
348                 }
350                 while (g_list_length(*history) > wrln_max_history_length) {
351                         GList *first = g_list_first(*history);
353                         /* remove the oldest history entry  */
354                         g_free(first->data);
355                         first->data = NULL;
356                         *history = g_list_delete_link(*history, first);
357                 }
358         }
360         return g_strdup(wr.line);
363 gchar *
364 wreadln(WINDOW *w,
365         const gchar *prompt,
366         const gchar *initial_value,
367         gint x1,
368         GList **history,
369         GCompletion *gcmp)
371         return  _wreadln(w, prompt, initial_value, x1, history, gcmp, FALSE);
374 gchar *
375 wreadln_masked(WINDOW *w,
376                const gchar *prompt,
377                const gchar *initial_value,
378                gint x1,
379                GList **history,
380                GCompletion *gcmp)
382         return  _wreadln(w, prompt, initial_value, x1, history, gcmp, TRUE);