4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
11 #include <sys/types.h>
15 #define KEYPAD_HACK 1 /* enable keypad hack */
16 #define CBREAK_HACK 1 /* enable cbreak hack */
22 #if defined(HAVE_READLINE_READLINE_H)
23 # include <readline/readline.h>
24 #elif defined(HAVE_READLINE_H)
25 # include <readline.h>
28 #if defined(HAVE_READLINE_HISTORY_H)
29 # include <readline/history.h>
30 #elif defined(HAVE_HISTORY_H)
34 #ifdef HANDLE_MULTIBYTE
38 #define RL_READLINE_NAME "Abook"
40 static int rl_x, rl_y;
41 static WINDOW *rl_win;
43 static bool rl_cancelled;
52 #ifdef HANDLE_MULTIBYTE
59 mbtowc(NULL, NULL, 0);
60 for(p = rl_line_buffer;(p - rl_line_buffer) < rl_point;) {
64 if((a = mbtowc(&wc, p, MB_CUR_MAX)) == 0)
67 return rl_point; /* fall back */
83 #ifdef HANDLE_MULTIBYTE
84 int real_point = rline_calc_point() + rl_x;
86 int real_point = rl_point + rl_x;
89 if(real_point > (COLS - 1))
90 mvwaddnstr(rl_win, rl_y, rl_x,
91 rl_line_buffer + (1 + real_point - COLS),
94 mvwaddnstr(rl_win, rl_y, rl_x, rl_line_buffer, rl_end);
97 wmove(rl_win, rl_y, min(real_point, COLS - 1));
103 rline_compdisp(char **matches, int n, int max_len)
109 rl_cancel(int dummy1, int dummy2)
119 abook_rl_init(bool use_completion)
121 rl_readline_name = RL_READLINE_NAME;
123 rl_already_prompted = 1;
124 rl_catch_sigwinch = 0;
126 rl_redisplay_function = rline_update;
127 rl_completion_display_matches_hook = rline_compdisp;
129 rl_unbind_function_in_map(rl_clear_screen, rl_get_keymap());
130 rl_unbind_function_in_map(rl_reverse_search_history, rl_get_keymap());
131 rl_unbind_function_in_map(rl_re_read_init_file, rl_get_keymap());
134 rl_bind_key('\t', rl_menu_complete);
136 rl_unbind_function_in_map(rl_complete, rl_get_keymap());
137 rl_unbind_function_in_map(rl_menu_complete, rl_get_keymap());
140 rl_bind_key('g' & 31, rl_cancel); /* C-g */
144 rl_cancelled = FALSE;
148 abook_readline(WINDOW *w, int y, int x, char *s, int limit, bool use_completion)
152 abook_rl_init(use_completion);
154 wmove(rl_win = w, rl_y = y, rl_x = x);
166 ret = readline(NULL);
174 if(rl_cancelled && ret) {