]> git.deb.at Git - pkg/abook.git/commitdiff
cancel key support added
authorJaakko Heinonen <jheinonen@users.sourceforge.net>
Tue, 12 Mar 2002 18:54:29 +0000 (18:54 +0000)
committerJaakko Heinonen <jheinonen@users.sourceforge.net>
Tue, 12 Mar 2002 18:54:29 +0000 (18:54 +0000)
abook_rl.c
abook_rl.h
edit.c
ui.c
ui.h

index be8ac908b0062f436bc9b12afb2a9c282fade5fb..43ab31a0fe7d45323a6d06c6834e334c9a9aace2 100644 (file)
@@ -33,6 +33,8 @@
 static int rl_x, rl_y;
 static WINDOW *rl_win;
 
+static bool rl_cancelled;
+
 static void
 rl_refresh()
 {
@@ -58,26 +60,36 @@ rline_update()
        rl_refresh();
 }
 
-void
+static void
 rline_compdisp(char **matches, int n, int max_len)
 {
-       /*
-        * dummy
-        */
+       /* dummy */
+}
+
+static int
+rl_cancel(int dummy1, int dummy2)
+{
+       rl_cancelled = TRUE;
+
+       rl_done = 1;
+
+       return 0;
 }
 
 static void
-abook_rl_init(int use_completion)
+abook_rl_init(bool use_completion)
 {
        rl_readline_name = RL_READLINE_NAME;
        
        rl_already_prompted = 1;
+       rl_catch_sigwinch = 0;
        
        rl_redisplay_function = rline_update;
        rl_completion_display_matches_hook = rline_compdisp;
-       
+
        rl_unbind_function_in_map(rl_clear_screen, rl_get_keymap());
        rl_unbind_function_in_map(rl_reverse_search_history, rl_get_keymap());
+       rl_unbind_function_in_map(rl_re_read_init_file, rl_get_keymap());
        
        if(use_completion) {
                rl_bind_key('\t', rl_menu_complete);
@@ -86,18 +98,21 @@ abook_rl_init(int use_completion)
                rl_unbind_function_in_map(rl_menu_complete, rl_get_keymap());
        }
 
+       rl_bind_key('g' & 31, rl_cancel); /* C-g */
+
        clear_history();
+
+       rl_cancelled = FALSE;
 }      
 
 char *
-abook_readline(WINDOW *w, int y, int x, char *s, int limit, int use_completion)
+abook_readline(WINDOW *w, int y, int x, char *s, int limit, bool use_completion)
 {
-       char *ret = NULL;
+       char *ret;
 
-       rl_win = w;
        abook_rl_init(use_completion);
 
-       wmove(rl_win, rl_y = y, rl_x = x);
+       wmove(rl_win = w, rl_y = y, rl_x = x);
        rl_refresh();
 
        if(s && *s)
@@ -105,6 +120,12 @@ abook_readline(WINDOW *w, int y, int x, char *s, int limit, int use_completion)
        
        ret = readline(NULL);
 
+       if(rl_cancelled) {
+               if(ret)
+                       free(ret);
+               ret = NULL;
+       }
+
        return ret;
 }
 
index d6c2c22594465482456d4b4eb475e5ef767d6d44..22b67ba6f64f11d89f030db9efb983c286a68d6c 100644 (file)
@@ -4,6 +4,6 @@
 #include "abook_curses.h"
 
 char           *abook_readline(WINDOW *w, int y, int x, char *s, int limit,
-               int use_completion);
+               bool use_completion);
 
 #endif
diff --git a/edit.c b/edit.c
index 90cc07d763241195b57b322d7ed98cc993fec956..9403975263bc1520a48031cc81eb0f1b2e943d9b 100644 (file)
--- a/edit.c
+++ b/edit.c
@@ -187,6 +187,7 @@ change_field(char *msg, char **field)
 {
        int max_len = MAX_FIELD_LEN;
        char *old;
+       int ret = 0;
 
        if( !strncmp("E-mail", msg, 6) )
                max_len = MAX_EMAIL_LEN;
@@ -195,12 +196,19 @@ change_field(char *msg, char **field)
 
        *field = ui_readline(msg, old, max_len - 1, 0);
 
-       free(old);
+       if(*field) {
+               free(old);
+               if(!**field)
+                       my_free(*field);
+       } else {
+               *field = old;
+               ret = 1;
+       }
 
        clear_statusline();
        refresh_statusline();
 
-       return 0;
+       return ret;
 }
 
 static void
diff --git a/ui.c b/ui.c
index b15d8ba1f756d0e59d03baa2ff94f754e187ad8b..a7d1a5428322aa11503a484486f8c3685af7aecc 100644 (file)
--- a/ui.c
+++ b/ui.c
@@ -239,7 +239,7 @@ statusline_addstr(char *str)
 }
 
 char *
-ui_readline(char *prompt, char *s, int limit, int use_completion)
+ui_readline(char *prompt, char *s, int limit, bool use_completion)
 {
        int y, x;
        char *ret;
@@ -250,9 +250,6 @@ ui_readline(char *prompt, char *s, int limit, int use_completion)
 
        ret = abook_readline(bottom, y, x, s, limit, use_completion);
 
-       if(ret && !*ret)
-               my_free(ret);
-
        if(ret)
                strtrim(ret);
 
diff --git a/ui.h b/ui.h
index da7cdfa05f0db211dbbec6e413a4189e0dfa6969..33107f8dabbf28d5319a43801c309c804cc3548c 100644 (file)
--- a/ui.h
+++ b/ui.h
@@ -20,7 +20,7 @@ int           statusline_ask_boolean(char *msg, int def);
 void            clear_statusline();
 void           display_help(int help);
 void           statusline_addstr(char *str);
-char *         ui_readline(char *prompt, char *s, int limit, int use_completion);
+char *         ui_readline(char *prompt, char *s, int limit, bool use_completion);
 void           refresh_statusline();
 void           get_commands();
 void           ui_remove_items();