4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
11 #include <sys/types.h>
19 #if defined(HAVE_READLINE_READLINE_H)
20 # include <readline/readline.h>
21 #elif defined(HAVE_READLINE_H)
22 # include <readline.h>
24 # error "You don't seem to have readline.h"
25 # error "No HAVE_READLINE_READLINE_H or HAVE_READLINE_H defined"
28 #if defined(HAVE_READLINE_HISTORY_H)
29 # include <readline/history.h>
30 #elif defined(HAVE_HISTORY_H)
33 # error "You don't seem to have history.h"
34 # error "No HAVE_READLINE_HISTORY_H or HAVE_HISTORY_H defined"
37 #ifdef HANDLE_MULTIBYTE
38 # include <mbswidth.h>
41 #define RL_READLINE_NAME "Abook"
43 static int rl_x, rl_y;
44 static WINDOW *rl_win;
46 static bool rl_cancelled;
55 #ifdef HANDLE_MULTIBYTE
59 return (int)mbsnwidth(rl_line_buffer, rl_point, 0);
66 #ifdef HANDLE_MULTIBYTE
67 int real_point = rline_calc_point() + rl_x;
69 int real_point = rl_point + rl_x;
72 if(real_point > (COLS - 1))
73 mvwaddnstr(rl_win, rl_y, rl_x,
74 rl_line_buffer + (1 + real_point - COLS),
77 mvwaddnstr(rl_win, rl_y, rl_x, rl_line_buffer, rl_end);
80 wmove(rl_win, rl_y, min(real_point, COLS - 1));
86 rline_compdisp(char **matches, int n, int max_len)
92 rline_prep_terminal(int dummy)
94 #if (RL_VERSION_MAJOR == 4 && RL_VERSION_MINOR > 2) || (RL_VERSION_MAJOR > 4)
98 * #warning is an extension. Not all compilers support it.
101 # warning "You seem to have rather old readline version or \
102 non-GNU version of it. If you have problems please use \
103 GNU readline 4.3 or newer. \
104 GNU readline versions 4.0, 4.1 and 4.2 should be OK despite \
108 * this kludge avoids older readline libraries to print a newline
110 extern int readline_echoing_p;
111 readline_echoing_p = 0;
114 keypad(rl_win, FALSE);
118 rline_deprep_terminal(void)
121 keypad(rl_win, TRUE);
125 rl_cancel(int dummy1, int dummy2)
135 abook_rl_init(bool use_completion)
137 rl_readline_name = RL_READLINE_NAME;
139 #if RL_VERSION_MAJOR >= 4
140 rl_already_prompted = 1;
142 rl_catch_sigwinch = 0;
143 rl_erase_empty_line = 0;
145 rl_redisplay_function = rline_update;
146 rl_completion_display_matches_hook = rline_compdisp;
147 rl_prep_term_function = rline_prep_terminal;
148 rl_deprep_term_function = rline_deprep_terminal;
150 rl_unbind_function_in_map(rl_clear_screen, rl_get_keymap());
151 rl_unbind_function_in_map(rl_reverse_search_history, rl_get_keymap());
152 rl_unbind_function_in_map(rl_re_read_init_file, rl_get_keymap());
155 rl_bind_key('\t', rl_menu_complete);
157 rl_unbind_function_in_map(rl_complete, rl_get_keymap());
158 rl_unbind_function_in_map(rl_menu_complete, rl_get_keymap());
161 rl_bind_key('g' & 31, rl_cancel); /* C-g */
165 rl_cancelled = FALSE;
169 abook_readline(WINDOW *w, int y, int x, char *s, bool use_completion)
173 abook_rl_init(use_completion);
175 wmove(rl_win = w, rl_y = y, rl_x = x);
181 ret = readline(NULL);
183 if(rl_cancelled && ret) {