use automake 1.11, autoconf 2.65
[abook.git] / abook_rl.c
blob3eb28fbb579c541679a4be8f6ef3a7808b00ac55
1 /*
2 * $Id: abook_rl.c,v 1.16 2005/09/23 15:42:11 jheinonen Exp $
4 * by JH <jheinonen@users.sourceforge.net>
6 * Copyright (C) Jaakko Heinonen
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include "abook.h"
13 #include "abook_rl.h"
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #if defined(HAVE_READLINE_READLINE_H)
20 # include <readline/readline.h>
21 #elif defined(HAVE_READLINE_H)
22 # include <readline.h>
23 #else
24 # error "You don't seem to have readline.h"
25 # error "No HAVE_READLINE_READLINE_H or HAVE_READLINE_H defined"
26 #endif
28 #if defined(HAVE_READLINE_HISTORY_H)
29 # include <readline/history.h>
30 #elif defined(HAVE_HISTORY_H)
31 # include <history.h>
32 #else
33 # error "You don't seem to have history.h"
34 # error "No HAVE_READLINE_HISTORY_H or HAVE_HISTORY_H defined"
35 #endif
37 #ifdef HANDLE_MULTIBYTE
38 # include <mbswidth.h>
39 #endif
41 #define RL_READLINE_NAME "Abook"
43 static int rl_x, rl_y;
44 static WINDOW *rl_win;
46 static bool rl_cancelled;
48 static void
49 rl_refresh()
51 /* refresh(); */
52 wrefresh(rl_win);
55 #ifdef HANDLE_MULTIBYTE
56 static int
57 rline_calc_point()
59 return (int)mbsnwidth(rl_line_buffer, rl_point, 0);
61 #endif
63 static void
64 rline_update()
66 #ifdef HANDLE_MULTIBYTE
67 int real_point = rline_calc_point() + rl_x;
68 #else
69 int real_point = rl_point + rl_x;
70 #endif
72 if(real_point > (COLS - 1))
73 mvwaddnstr(rl_win, rl_y, rl_x,
74 rl_line_buffer + (1 + real_point - COLS),
75 COLS - rl_x - 1);
76 else
77 mvwaddnstr(rl_win, rl_y, rl_x, rl_line_buffer, rl_end);
79 wclrtoeol(rl_win);
80 wmove(rl_win, rl_y, min(real_point, COLS - 1));
82 rl_refresh();
85 static void
86 rline_compdisp(char **matches, int n, int max_len)
88 /* dummy */
91 static void
92 rline_prep_terminal(int dummy)
94 #if (RL_VERSION_MAJOR == 4 && RL_VERSION_MINOR > 2) || (RL_VERSION_MAJOR > 4)
95 /* nothing */
96 #else
98 * #warning is an extension. Not all compilers support it.
100 # ifdef __GNUC__
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 \
105 of this warning."
106 # endif
108 * this kludge avoids older readline libraries to print a newline
110 extern int readline_echoing_p;
111 readline_echoing_p = 0;
112 #endif
113 raw();
114 keypad(rl_win, FALSE);
117 static void
118 rline_deprep_terminal(void)
120 cbreak();
121 keypad(rl_win, TRUE);
124 static int
125 rl_cancel(int dummy1, int dummy2)
127 rl_cancelled = TRUE;
129 rl_done = 1;
131 return 0;
134 static void
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;
141 #endif
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());
154 if(use_completion) {
155 rl_bind_key('\t', rl_menu_complete);
156 } else {
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 */
163 clear_history();
165 rl_cancelled = FALSE;
168 char *
169 abook_readline(WINDOW *w, int y, int x, char *s, bool use_completion)
171 char *ret;
173 abook_rl_init(use_completion);
175 wmove(rl_win = w, rl_y = y, rl_x = x);
176 rl_refresh();
178 if(s && *s)
179 add_history(s);
181 ret = readline(NULL);
183 if(rl_cancelled && ret) {
184 free(ret);
185 ret = NULL;
188 return ret;