forked from hhirsch/abook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabook_rl.c
190 lines (154 loc) · 3.54 KB
/
abook_rl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* $Id$
*
* by JH <[email protected]>
*
* Copyright (C) Jaakko Heinonen
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "abook.h"
#include "abook_rl.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if defined(HAVE_READLINE_READLINE_H)
# include <readline/readline.h>
#elif defined(HAVE_READLINE_H)
# include <readline.h>
#else
# error "You don't seem to have readline.h"
# error "No HAVE_READLINE_READLINE_H or HAVE_READLINE_H defined"
#endif
#if defined(HAVE_READLINE_HISTORY_H)
# include <readline/history.h>
#elif defined(HAVE_HISTORY_H)
# include <history.h>
#else
# error "You don't seem to have history.h"
# error "No HAVE_READLINE_HISTORY_H or HAVE_HISTORY_H defined"
#endif
#ifdef HANDLE_MULTIBYTE
# include <mbswidth.h>
#endif
#define RL_READLINE_NAME "Abook"
static int rl_x, rl_y;
static WINDOW *rl_win;
static bool rl_cancelled;
static void
rl_refresh()
{
/* refresh(); */
wrefresh(rl_win);
}
#ifdef HANDLE_MULTIBYTE
static int
rline_calc_point()
{
return (int)mbsnwidth(rl_line_buffer, rl_point, 0);
}
#endif
static void
rline_update()
{
#ifdef HANDLE_MULTIBYTE
int real_point = rline_calc_point() + rl_x;
#else
int real_point = rl_point + rl_x;
#endif
if(real_point > (COLS - 1))
mvwaddnstr(rl_win, rl_y, rl_x,
rl_line_buffer + (1 + real_point - COLS),
COLS - rl_x - 1);
else
mvwaddnstr(rl_win, rl_y, rl_x, rl_line_buffer, rl_end);
wclrtoeol(rl_win);
wmove(rl_win, rl_y, min(real_point, COLS - 1));
rl_refresh();
}
static void
rline_compdisp(char **matches, int n, int max_len)
{
/* dummy */
}
static void
rline_prep_terminal(int dummy)
{
#if (RL_VERSION_MAJOR == 4 && RL_VERSION_MINOR > 2) || (RL_VERSION_MAJOR > 4)
/* nothing */
#else
/*
* #warning is an extension. Not all compilers support it.
*/
# ifdef __GNUC__
# warning "You seem to have rather old readline version or \
non-GNU version of it. If you have problems please use \
GNU readline 4.3 or newer. \
GNU readline versions 4.0, 4.1 and 4.2 should be OK despite \
of this warning."
# endif
/*
* this kludge avoids older readline libraries to print a newline
*/
extern int readline_echoing_p;
readline_echoing_p = 0;
#endif
raw();
keypad(rl_win, FALSE);
}
static void
rline_deprep_terminal(void)
{
cbreak();
keypad(rl_win, TRUE);
}
static int
rl_cancel(int dummy1, int dummy2)
{
rl_cancelled = TRUE;
rl_done = 1;
return 0;
}
static void
abook_rl_init(bool use_completion)
{
rl_readline_name = RL_READLINE_NAME;
#if RL_VERSION_MAJOR >= 4
rl_already_prompted = 1;
#endif
rl_catch_sigwinch = 0;
rl_erase_empty_line = 0;
rl_redisplay_function = rline_update;
rl_completion_display_matches_hook = rline_compdisp;
rl_prep_term_function = rline_prep_terminal;
rl_deprep_term_function = rline_deprep_terminal;
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);
} else {
rl_unbind_function_in_map(rl_complete, rl_get_keymap());
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, bool use_completion)
{
char *ret;
abook_rl_init(use_completion);
wmove(rl_win = w, rl_y = y, rl_x = x);
rl_refresh();
if(s && *s)
add_history(s);
ret = readline(NULL);
if(rl_cancelled && ret) {
free(ret);
ret = NULL;
}
return ret;
}