Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / fe-text / gui-entry.c
index 7544539a304c4059ef586f1d263bfe2e7aee4764..7dcec35801a35b7c8631f240822d70e57aa35a26 100644 (file)
 */
 
 #include "module.h"
+#include "misc.h"
+#include "utf8.h"
 #include "formats.h"
 
+#include "gui-entry.h"
 #include "gui-printtext.h"
-#include "screen.h"
+#include "term.h"
 
-static GString *entry;
-static int promptlen, permanent_prompt, pos, scrstart, scrpos;
-static int prompt_hidden;
-static char *prompt;
+const unichar empty_str[] = { 0 };
 
-static void entry_screenpos(void)
+GUI_ENTRY_REC *active_entry;
+
+static void entry_text_grow(GUI_ENTRY_REC *entry, int grow_size)
 {
-       if (pos-scrstart < COLS-2-promptlen && pos-scrstart > 0) {
-               scrpos = pos-scrstart;
+       if (entry->text_len+grow_size < entry->text_alloc)
                return;
+
+       entry->text_alloc = nearest_power(entry->text_alloc+grow_size);
+       entry->text = g_realloc(entry->text,
+                               sizeof(unichar) * entry->text_alloc);
+}
+
+GUI_ENTRY_REC *gui_entry_create(int xpos, int ypos, int width, int utf8)
+{
+       GUI_ENTRY_REC *rec;
+
+       rec = g_new0(GUI_ENTRY_REC, 1);
+       rec->xpos = xpos;
+       rec->ypos = ypos;
+        rec->width = width;
+        rec->text_alloc = 1024;
+       rec->text = g_new(unichar, rec->text_alloc);
+        rec->text[0] = '\0';
+        rec->utf8 = utf8;
+       return rec;
+}
+
+void gui_entry_destroy(GUI_ENTRY_REC *entry)
+{
+        g_return_if_fail(entry != NULL);
+
+       if (active_entry == entry)
+               gui_entry_set_active(NULL);
+
+        g_free(entry->text);
+       g_free(entry->prompt);
+        g_free(entry);
+}
+
+/* big5 functions */
+#define big5_width(ch) ((ch)>0xff ? 2:1)
+
+void unichars_to_big5(const unichar *str, char *out)
+{
+       for (; *str != '\0'; str++) {
+               if (*str > 0xff)
+                       *out++ = (*str >> 8) & 0xff;
+               *out++ = *str & 0xff;
        }
+       *out = '\0';
+}
 
-       if (pos < COLS-1-promptlen) {
-               scrstart = 0;
-               scrpos = pos;
-       } else {
-               scrpos = (COLS-promptlen)*2/3;
-               scrstart = pos-scrpos;
+int strlen_big5(const unsigned char *str)
+{
+       int len=0;
+
+       if (term_type != TERM_TYPE_BIG5)
+               return strlen(str);
+
+       while (*str != '\0') {
+               if (is_big5(str[0], str[1]))
+                       str++;
+               len++;
+               str++;
        }
+       return len;
 }
 
-static void entry_update(void)
+void unichars_to_big5_with_pos(const unichar *str, int spos, char *out, int *opos)
 {
-       char *p;
-       int n, len;
+       const unichar *sstart = str;
+       char *ostart = out;
+
+       *opos = 0;
+       while(*str != '\0')
+       {
+               if(*str > 0xff)
+                       *out ++ = (*str >> 8) & 0xff;
+               *out ++ = *str & 0xff;
+               str ++;
+               if(str - sstart == spos)
+                       *opos = out - ostart;
+       }
+       *out = '\0';
+}
 
-       len = entry->len-scrstart > COLS-1-promptlen ?
-               COLS-1-promptlen : entry->len-scrstart;
+void big5_to_unichars(const char *str, unichar *out)
+{
+       const unsigned char *p = (const unsigned char *) str;
+
+       while (*p != '\0') {
+               if (is_big5(p[0], p[1])) {
+                       *out++ = p[0] << 8 | p[1];
+                       p += 2;
+               } else {
+                       *out++ = *p++;
+               }
+       }
+       *out = '\0';
+}
+
+/* ----------------------------- */
+
+static int pos2scrpos(GUI_ENTRY_REC *entry, int pos)
+{
+       unichar *p;
+       int xpos = 0;
+
+       for (p = entry->text; p - entry->text < pos; p++) {
+               if (term_type == TERM_TYPE_BIG5)
+                       xpos += big5_width(*p);
+               else if (entry->utf8)
+                       xpos += utf8_width(*p);
+               else
+                       xpos++;
+       }
+       return xpos;
+}
+
+static int scrpos2pos(GUI_ENTRY_REC *entry, int pos)
+{
+       int i, width, xpos;
+
+       for (i = 0, xpos = 0; entry->text[i]; i++) {
+               unichar *p = entry->text+i;
+
+               if (term_type == TERM_TYPE_BIG5)
+                       width = big5_width(*p);
+               else if (entry->utf8)
+                       width = utf8_width(*p);
+               else
+                       width = 1;
+
+               if (xpos + width > pos)
+                       break;
+               xpos += width;
+       }
+
+       if (xpos == pos)
+               return i;
+       else
+               return i-1;
+}
+
+/* Fixes the cursor position in screen */
+static void gui_entry_fix_cursor(GUI_ENTRY_REC *entry)
+{
+       int old_scrstart;
+
+       /* assume prompt len == prompt scrlen */
+       int start = pos2scrpos(entry, entry->scrstart);
+       int now = pos2scrpos(entry, entry->pos);
+
+       old_scrstart = entry->scrstart;
+       if (now-start < entry->width - 2 - entry->promptlen && now-start > 0)
+               entry->scrpos = now-start;
+       else if (now < entry->width - 1 - entry->promptlen) {
+               entry->scrstart = 0;
+               entry->scrpos = now;
+       } else {
+               entry->scrstart = scrpos2pos(entry, now-(entry->width -
+                                                        entry->promptlen)*2/3);
+               start = pos2scrpos(entry, entry->scrstart);
+               entry->scrpos = now - start;
+       }
 
-       set_color(stdscr, 0);
-       move(LINES-1, promptlen);
+       if (old_scrstart != entry->scrstart)
+                entry->redraw_needed_from = 0;
+}
 
-       for (p = entry->str+scrstart, n = 0; n < len; n++, p++) {
-               if (prompt_hidden)
-                        addch(' ');
-               else if ((unsigned char) *p >= 32)
-                       addch((unsigned char) *p);
+static void gui_entry_draw_from(GUI_ENTRY_REC *entry, int pos)
+{
+       const unichar *p;
+       int xpos, end_xpos;
+
+       xpos = entry->xpos + entry->promptlen + 
+               pos2scrpos(entry, pos + entry->scrstart) - 
+               pos2scrpos(entry, entry->scrstart);
+        end_xpos = entry->xpos + entry->width;
+
+       if (xpos > end_xpos)
+                return;
+
+       term_set_color(root_window, ATTR_RESET);
+       term_move(root_window, xpos, entry->ypos);
+
+       p = entry->scrstart + pos < entry->text_len ?
+               entry->text + entry->scrstart + pos : empty_str;
+       for (; *p != '\0'; p++) {
+               if (entry->hidden)
+                       xpos++;
+               else if (term_type == TERM_TYPE_BIG5)
+                       xpos += big5_width(*p);
+               else if (entry->utf8)
+                       xpos += utf8_width(*p);
+               else
+                       xpos++;
+
+               if (xpos > end_xpos)
+                       break;
+
+               if (entry->hidden)
+                        term_addch(root_window, ' ');
+               else if (*p >= 32 && (entry->utf8 || (*p & 127) >= 32))
+                       term_add_unichar(root_window, *p);
                else {
-                       set_color(stdscr, ATTR_REVERSE);
-                       addch(*p+'A'-1);
-                       set_color(stdscr, 0);
+                       term_set_color(root_window, ATTR_RESET|ATTR_REVERSE);
+                       term_addch(root_window, *p+'A'-1);
+                       term_set_color(root_window, ATTR_RESET);
                }
        }
-       clrtoeol();
 
-       move_cursor(LINES-1, scrpos+promptlen);
-       screen_refresh(NULL);
+        /* clear the rest of the input line */
+        if (end_xpos == term_width-1)
+               term_clrtoeol(root_window);
+       else {
+               while (xpos < end_xpos) {
+                        term_addch(root_window, ' ');
+                        xpos++;
+               }
+       }
 }
 
-void gui_entry_set_prompt(const char *str)
+static void gui_entry_draw(GUI_ENTRY_REC *entry)
 {
+       if (entry->redraw_needed_from >= 0) {
+               gui_entry_draw_from(entry, entry->redraw_needed_from);
+                entry->redraw_needed_from = -1;
+       }
+
+       term_move_cursor(entry->xpos + entry->scrpos + entry->promptlen,
+                        entry->ypos);
+       term_refresh(NULL);
+}
+
+static void gui_entry_redraw_from(GUI_ENTRY_REC *entry, int pos)
+{
+       pos -= entry->scrstart;
+       if (pos < 0) pos = 0;
+
+       if (entry->redraw_needed_from == -1 ||
+           entry->redraw_needed_from > pos)
+               entry->redraw_needed_from = pos;
+}
+
+void gui_entry_move(GUI_ENTRY_REC *entry, int xpos, int ypos, int width)
+{
+       int old_width;
+
+        g_return_if_fail(entry != NULL);
+
+       if (entry->xpos != xpos || entry->ypos != ypos) {
+                /* position in screen changed - needs a full redraw */
+               entry->xpos = xpos;
+               entry->ypos = ypos;
+               entry->width = width;
+               gui_entry_redraw(entry);
+                return;
+       }
+
+       if (entry->width == width)
+                return; /* no changes */
+
+       if (width > entry->width) {
+                /* input line grew - need to draw text at the end */
+                old_width = width;
+               entry->width = width;
+               gui_entry_redraw_from(entry, old_width);
+       } else {
+               /* input line shrinked - make sure the cursor
+                  is inside the input line */
+               entry->width = width;
+               if (entry->pos - entry->scrstart >
+                   entry->width-2 - entry->promptlen) {
+                       gui_entry_fix_cursor(entry);
+               }
+       }
+
+       gui_entry_draw(entry);
+}
+
+void gui_entry_set_active(GUI_ENTRY_REC *entry)
+{
+       active_entry = entry;
+
+       if (entry != NULL) {
+               term_move_cursor(entry->xpos + entry->scrpos +
+                                entry->promptlen, entry->ypos);
+               term_refresh(NULL);
+       }
+}
+
+void gui_entry_set_prompt(GUI_ENTRY_REC *entry, const char *str)
+{
+       int oldlen;
+
+        g_return_if_fail(entry != NULL);
+
+        oldlen = entry->promptlen;
        if (str != NULL) {
-               if (permanent_prompt) return;
+               g_free_not_null(entry->prompt);
+               entry->prompt = g_strdup(str);
+               entry->promptlen = format_get_length(str);
+       }
 
-               g_free_not_null(prompt);
-               prompt = g_strdup(str);
-               promptlen = format_get_length(prompt);
+        if (entry->prompt != NULL)
+               gui_printtext(entry->xpos, entry->ypos, entry->prompt);
+
+       if (entry->promptlen != oldlen) {
+               gui_entry_fix_cursor(entry);
+               gui_entry_draw(entry);
        }
+}
 
-        if (prompt != NULL)
-               gui_printtext(0, LINES-1, prompt);
+void gui_entry_set_hidden(GUI_ENTRY_REC *entry, int hidden)
+{
+        g_return_if_fail(entry != NULL);
+
+        entry->hidden = hidden;
+}
+
+void gui_entry_set_utf8(GUI_ENTRY_REC *entry, int utf8)
+{
+        g_return_if_fail(entry != NULL);
 
-       entry_screenpos();
-       entry_update();
+        entry->utf8 = utf8;
 }
 
-void gui_entry_set_perm_prompt(const char *str)
+void gui_entry_set_text(GUI_ENTRY_REC *entry, const char *str)
 {
+       g_return_if_fail(entry != NULL);
        g_return_if_fail(str != NULL);
 
-       g_free_not_null(prompt);
-       prompt = g_strdup(str);
-       promptlen = format_get_length(prompt);
+       entry->text_len = 0;
+       entry->pos = 0;
+       entry->text[0] = '\0';
 
-       permanent_prompt = TRUE;
-       gui_entry_set_prompt(NULL);
+       gui_entry_insert_text(entry, str);
 }
 
-void gui_entry_set_hidden(int hidden)
+char *gui_entry_get_text(GUI_ENTRY_REC *entry)
 {
-        prompt_hidden = hidden;
+       char *buf;
+        int i;
+
+       g_return_val_if_fail(entry != NULL, NULL);
+
+       buf = g_malloc(entry->text_len*6 + 1);
+       if (entry->utf8)
+               utf16_to_utf8(entry->text, buf);
+       else {
+               if (term_type == TERM_TYPE_BIG5)
+                       unichars_to_big5(entry->text, buf);
+               else
+                       for (i = 0; i <= entry->text_len; i++)
+                               buf[i] = entry->text[i];
+       }
+       return buf;
 }
 
-void gui_entry_remove_perm_prompt(void)
+char *gui_entry_get_text_and_pos(GUI_ENTRY_REC *entry, int *pos)
 {
-        permanent_prompt = FALSE;
+       char *buf;
+        int i;
+
+       g_return_val_if_fail(entry != NULL, NULL);
+
+       buf = g_malloc(entry->text_len*6 + 1);
+       if (entry->utf8)
+               utf16_to_utf8_with_pos(entry->text, entry->pos, buf, pos);
+       else {
+               if(term_type==TERM_TYPE_BIG5)
+                       unichars_to_big5_with_pos(entry->text, entry->pos, buf, pos);
+               else
+               {
+                       for (i = 0; i <= entry->text_len; i++)
+                               buf[i] = entry->text[i];
+                       *pos = entry->pos;
+               }
+       }
+       return buf;
 }
 
-void gui_entry_set_text(const char *str)
+void gui_entry_insert_text(GUI_ENTRY_REC *entry, const char *str)
 {
+        unichar chr;
+       int i, len;
+
+        g_return_if_fail(entry != NULL);
        g_return_if_fail(str != NULL);
 
-       g_string_assign(entry, str);
-       pos = entry->len;
+        gui_entry_redraw_from(entry, entry->pos);
+
+       len = !entry->utf8 ? strlen_big5(str) : strlen_utf8(str);
+        entry_text_grow(entry, len);
+
+        /* make space for the string */
+       g_memmove(entry->text + entry->pos + len, entry->text + entry->pos,
+                 (entry->text_len-entry->pos + 1) * sizeof(unichar));
+
+       if (!entry->utf8) {
+               if (term_type == TERM_TYPE_BIG5) {
+                       chr = entry->text[entry->pos + len];
+                       big5_to_unichars(str, entry->text + entry->pos);
+                       entry->text[entry->pos + len] = chr;
+               } else {
+                       for (i = 0; i < len; i++)
+                               entry->text[entry->pos + i] = str[i];
+               }
+       } else {
+                chr = entry->text[entry->pos+len];
+               utf8_to_utf16(str, entry->text+entry->pos);
+                entry->text[entry->pos+len] = chr;
+       }
+
+       entry->text_len += len;
+        entry->pos += len;
 
-       entry_screenpos();
-       entry_update();
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-char *gui_entry_get_text(void)
+void gui_entry_insert_char(GUI_ENTRY_REC *entry, unichar chr)
 {
-       return entry->str;
+        g_return_if_fail(entry != NULL);
+
+       if (chr == 0 || chr == 13 || chr == 10)
+               return; /* never insert NUL, CR or LF characters */
+
+        gui_entry_redraw_from(entry, entry->pos);
+
+       entry_text_grow(entry, 1);
+
+       /* make space for the string */
+       g_memmove(entry->text + entry->pos + 1, entry->text + entry->pos,
+                 (entry->text_len-entry->pos + 1) * sizeof(unichar));
+
+       entry->text[entry->pos] = chr;
+       entry->text_len++;
+        entry->pos++;
+
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-void gui_entry_insert_text(const char *str)
+char *gui_entry_get_cutbuffer(GUI_ENTRY_REC *entry)
 {
-       g_return_if_fail(str != NULL);
+       char *buf;
+        int i;
+
+       g_return_val_if_fail(entry != NULL, NULL);
 
-       g_string_insert(entry, pos, str);
-       pos += strlen(str);
+       if (entry->cutbuffer == NULL)
+                return NULL;
 
-       entry_screenpos();
-       entry_update();
+       buf = g_malloc(entry->cutbuffer_len*6 + 1);
+       if (entry->utf8)
+               utf16_to_utf8(entry->cutbuffer, buf);
+       else if (term_type == TERM_TYPE_BIG5) {
+               unichars_to_big5(entry->cutbuffer, buf);
+       } else {
+               for (i = 0; i <= entry->cutbuffer_len; i++)
+                       buf[i] = entry->cutbuffer[i];
+       }
+       return buf;
 }
 
-void gui_entry_insert_char(char chr)
+void gui_entry_erase_to(GUI_ENTRY_REC *entry, int pos, int update_cutbuffer)
 {
-       g_string_insert_c(entry, pos, chr);
-       pos++;
+       int newpos, size = 0;
 
-       entry_screenpos();
-       entry_update();
+       g_return_if_fail(entry != NULL);
+
+       for (newpos = gui_entry_get_pos(entry); newpos > pos; size++)
+               newpos = newpos - 1;
+       gui_entry_erase(entry, size, update_cutbuffer);
 }
 
-void gui_entry_erase(int size)
+void gui_entry_erase(GUI_ENTRY_REC *entry, int size, int update_cutbuffer)
 {
-       if (pos < size) return;
+        g_return_if_fail(entry != NULL);
+
+       if (entry->pos < size)
+               return;
+
+       if (update_cutbuffer) {
+               /* put erased text to cutbuffer */
+               if (entry->cutbuffer == NULL || entry->cutbuffer_len < size) {
+                       g_free(entry->cutbuffer);
+                       entry->cutbuffer = g_new(unichar, size+1);
+               }
+
+               entry->cutbuffer_len = size;
+               entry->cutbuffer[size] = '\0';
+               memcpy(entry->cutbuffer, entry->text + entry->pos - size,
+                      size * sizeof(unichar));
+       }
+
+       if (size == 0) {
+                /* we just wanted to clear the cutbuffer */
+               return;
+       }
 
-#ifdef WANT_BIG5
-       if (is_big5(entry->str[pos-2], entry->str[pos-1]))
-               size++;
-#endif WANT_BIG5
+       g_memmove(entry->text + entry->pos - size, entry->text + entry->pos,
+                 (entry->text_len-entry->pos+1) * sizeof(unichar));
 
-       pos -= size;
-       g_string_erase(entry, pos, size);
+       entry->pos -= size;
+        entry->text_len -= size;
 
-       entry_screenpos();
-       entry_update();
+       gui_entry_redraw_from(entry, entry->pos);
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-void gui_entry_erase_word(void)
+void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space)
 {
        int to;
-       
-       if (pos == 0) return;
 
-       to = pos - 1;
+        g_return_if_fail(entry != NULL);
+       if (entry->pos == 0)
+               return;
+
+       to = entry->pos - 1;
 
-       while (entry->str[to] == ' ' && to > 0)
-               to--;
+       if (to_space) {
+               while (entry->text[to] == ' ' && to > 0)
+                       to--;
+               while (entry->text[to] != ' ' && to > 0)
+                       to--;
+       } else {
+               while (!i_isalnum(entry->text[to]) && to > 0)
+                       to--;
+               while (i_isalnum(entry->text[to]) && to > 0)
+                       to--;
+       }
+       if (to > 0) to++;
 
-       while (entry->str[to] != ' ' && to > 0)
-               to--;
+        gui_entry_erase(entry, entry->pos-to, TRUE);
+}
 
-       if (entry->str[to] == ' ' && to > 0) 
-               to++;
+void gui_entry_erase_next_word(GUI_ENTRY_REC *entry, int to_space)
+{
+       int to, size;
 
-       g_string_erase(entry, to, pos - to);
-       pos = to;
+        g_return_if_fail(entry != NULL);
+       if (entry->pos == entry->text_len)
+               return;
 
-       entry_screenpos();
-       entry_update();
+        to = entry->pos;
+       if (to_space) {
+               while (entry->text[to] == ' ' && to < entry->text_len)
+                       to++;
+               while (entry->text[to] != ' ' && to < entry->text_len)
+                       to++;
+       } else {
+               while (!i_isalnum(entry->text[to]) && to < entry->text_len)
+                       to++;
+               while (i_isalnum(entry->text[to]) && to < entry->text_len)
+                       to++;
+       }
+
+        size = to-entry->pos;
+       entry->pos = to;
+        gui_entry_erase(entry, size, TRUE);
 }
 
-void gui_entry_erase_next_word(void)
+void gui_entry_transpose_chars(GUI_ENTRY_REC *entry)
 {
-       int to = pos;
-       
-       if (pos == entry->len) return;
+        unichar chr;
+
+       if (entry->pos == 0 || entry->text_len < 2)
+                return;
 
-       while (entry->str[to] == ' ' && to < entry->len)
-               to++;
+       if (entry->pos == entry->text_len)
+                entry->pos--;
 
-       while (entry->str[to] != ' ' && to < entry->len)
-               to++;
+        /* swap chars */
+       chr = entry->text[entry->pos];
+       entry->text[entry->pos] = entry->text[entry->pos-1];
+        entry->text[entry->pos-1] = chr;
 
-       g_string_erase(entry, pos, to - pos);
+        entry->pos++;
 
-       entry_screenpos();
-       entry_update();
+       gui_entry_redraw_from(entry, entry->pos-2);
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-int gui_entry_get_pos(void)
+void gui_entry_transpose_words(GUI_ENTRY_REC *entry)
 {
-       return pos;
+       int spos1, epos1, spos2, epos2;
+
+       /* find last position */
+       epos2 = entry->pos;
+       while (epos2 < entry->text_len && !i_isalnum(entry->text[epos2]))
+               epos2++;
+       while (epos2 < entry->text_len &&  i_isalnum(entry->text[epos2]))
+               epos2++;
+
+       /* find other position */
+       spos2 = epos2;
+       while (spos2 > 0 && !i_isalnum(entry->text[spos2-1]))
+               spos2--;
+       while (spos2 > 0 &&  i_isalnum(entry->text[spos2-1]))
+               spos2--;
+
+       epos1 = spos2;
+       while (epos1 > 0 && !i_isalnum(entry->text[epos1-1]))
+               epos1--;
+
+       spos1 = epos1;
+       while (spos1 > 0 && i_isalnum(entry->text[spos1-1]))
+               spos1--;
+
+       /* do wordswap if any found */
+       if (spos1 < epos1 && epos1 < spos2 && spos2 < epos2) {
+               unichar *first, *sep, *second;
+               int i;
+
+               first  = (unichar *) g_malloc( (epos1 - spos1) * sizeof(unichar) );
+               sep    = (unichar *) g_malloc( (spos2 - epos1) * sizeof(unichar) );
+               second = (unichar *) g_malloc( (epos2 - spos2) * sizeof(unichar) );
+
+               for (i = spos1; i < epos1; i++)
+                       first[i-spos1] = entry->text[i];
+               for (i = epos1; i < spos2; i++)
+                       sep[i-epos1] = entry->text[i];
+               for (i = spos2; i < epos2; i++)
+                       second[i-spos2] = entry->text[i];
+
+               entry->pos = spos1;
+               for (i = 0; i < epos2-spos2; i++)
+                       entry->text[entry->pos++] = second[i];
+               for (i = 0; i < spos2-epos1; i++)
+                       entry->text[entry->pos++] = sep[i];
+               for (i = 0; i < epos1-spos1; i++)
+                       entry->text[entry->pos++] = first[i];
+
+               g_free(first);
+               g_free(sep);
+               g_free(second);
+
+       }
+       
+       gui_entry_redraw_from(entry, spos1);
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-void gui_entry_set_pos(int p)
+void gui_entry_capitalize_word(GUI_ENTRY_REC *entry)
 {
-       if (p >= 0 && p <= entry->len)
-               pos = p;
+       int pos = entry->pos;
+       while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
+               pos++;
+
+       if (pos < entry->text_len) {
+               entry->text[pos] = i_toupper(entry->text[pos]);
+               pos++;
+       }
+
+       while (pos < entry->text_len && i_isalnum(entry->text[pos])) {
+               entry->text[pos] = i_tolower(entry->text[pos]);
+               pos++;
+       }
 
-       entry_screenpos();
-       entry_update();
+       gui_entry_redraw_from(entry, entry->pos);
+       entry->pos = pos;
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-void gui_entry_move_pos(int p)
+void gui_entry_downcase_word(GUI_ENTRY_REC *entry)
 {
-#ifdef WANT_BIG5
-       if (p > 0 && is_big5 (entry->str[pos], entry->str[pos+1]))
-               p++;
-       else if (p < 0 && is_big5 (entry->str[pos-1], entry->str[pos]))
-               p--;
-#endif WANT_BIG5
+       int pos = entry->pos;
+       while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
+               pos++;
 
-       if (pos+p >= 0 && pos+p <= entry->len)
-               pos += p;
+       while (pos < entry->text_len && i_isalnum(entry->text[pos])) {
+               entry->text[pos] = i_tolower(entry->text[pos]);
+               pos++;
+       }
 
-       entry_screenpos();
-       entry_update();
+       gui_entry_redraw_from(entry, entry->pos);
+       entry->pos = pos;
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-static void gui_entry_move_words_left(int count)
+void gui_entry_upcase_word(GUI_ENTRY_REC *entry)
 {
-       if (pos == 0) return;
+       int pos = entry->pos;
+       while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
+               pos++;
 
-       while (count > 0 && pos > 0) {
-               while (pos > 0 && entry->str[pos-1] == ' ')
-                       pos--;
-               while (pos > 0 && entry->str[pos-1] != ' ')
-                       pos--;
-               count--;
+       while (pos < entry->text_len && i_isalnum(entry->text[pos])) {
+               entry->text[pos] = i_toupper(entry->text[pos]);
+               pos++;
        }
+
+       gui_entry_redraw_from(entry, entry->pos);
+       entry->pos = pos;
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-static void gui_entry_move_words_right(int count)
+int gui_entry_get_pos(GUI_ENTRY_REC *entry)
 {
-       if (pos == entry->len) return;
+        g_return_val_if_fail(entry != NULL, 0);
 
-       while (count > 0 && pos < entry->len) {
-               while (pos < entry->len && entry->str[pos] != ' ')
-                       pos++;
-               while (pos < entry->len && entry->str[pos] == ' ')
-                       pos++;
-               count--;
-       }
+       return entry->pos;
 }
 
-void gui_entry_move_words(int count)
+void gui_entry_set_pos(GUI_ENTRY_REC *entry, int pos)
 {
-       if (count < 0)
-               gui_entry_move_words_left(-count);
-       else if (count > 0)
-               gui_entry_move_words_right(count);
+        g_return_if_fail(entry != NULL);
+
+       if (pos >= 0 && pos <= entry->text_len)
+               entry->pos = pos;
+
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
+}
+
+void gui_entry_move_pos(GUI_ENTRY_REC *entry, int pos)
+{
+        g_return_if_fail(entry != NULL);
+
+       if (entry->pos + pos >= 0 && entry->pos + pos <= entry->text_len)
+               entry->pos += pos;
+
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
+}
+
+static void gui_entry_move_words_left(GUI_ENTRY_REC *entry, int count, int to_space)
+{
+       int pos;
+
+       pos = entry->pos;
+       while (count > 0 && pos > 0) {
+               if (to_space) {
+                       while (pos > 0 && entry->text[pos-1] == ' ')
+                               pos--;
+                       while (pos > 0 && entry->text[pos-1] != ' ')
+                               pos--;
+               } else {
+                       while (pos > 0 && !i_isalnum(entry->text[pos-1]))
+                               pos--;
+                       while (pos > 0 &&  i_isalnum(entry->text[pos-1]))
+                               pos--;
+               }
+               count--;
+       }
 
-       entry_screenpos();
-       entry_update();
+        entry->pos = pos;
 }
 
-void gui_entry_redraw(void)
+static void gui_entry_move_words_right(GUI_ENTRY_REC *entry, int count, int to_space)
 {
-       gui_entry_set_prompt(NULL);
+       int pos;
+
+       pos = entry->pos;
+       while (count > 0 && pos < entry->text_len) {
+               if (to_space) {
+                       while (pos < entry->text_len && entry->text[pos] == ' ')
+                               pos++;
+                       while (pos < entry->text_len && entry->text[pos] != ' ')
+                               pos++;
+               } else {
+                       while (pos < entry->text_len && !i_isalnum(entry->text[pos]))
+                               pos++;
+                       while (pos < entry->text_len &&  i_isalnum(entry->text[pos]))
+                               pos++;
+               }
+               count--;
+       }
 
-       entry_screenpos();
-       entry_update();
+        entry->pos = pos;
 }
 
-void gui_entry_init(void)
+void gui_entry_move_words(GUI_ENTRY_REC *entry, int count, int to_space)
 {
-       entry = g_string_new(NULL);
+        g_return_if_fail(entry != NULL);
 
-       pos = scrpos = 0;
-       prompt = NULL; promptlen = 0;
-       permanent_prompt = FALSE;
-        prompt_hidden = FALSE;
+       if (count < 0)
+               gui_entry_move_words_left(entry, -count, to_space);
+       else if (count > 0)
+               gui_entry_move_words_right(entry, count, to_space);
+
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }
 
-void gui_entry_deinit(void)
+void gui_entry_redraw(GUI_ENTRY_REC *entry)
 {
-       if (prompt != NULL) g_free(prompt);
-       g_string_free(entry, TRUE);
+        g_return_if_fail(entry != NULL);
+
+       gui_entry_set_prompt(entry, NULL);
+        gui_entry_redraw_from(entry, 0);
+       gui_entry_fix_cursor(entry);
+       gui_entry_draw(entry);
 }