]> git.deb.at Git - pkg/abook.git/blob - abook_rl.c
try to work around abook_readline() misbehavior on Solaris
[pkg/abook.git] / abook_rl.c
1 /*
2  * $Id$
3  *
4  * by JH <jheinonen@users.sourceforge.net>
5  *
6  * Copyright (C) Jaakko Heinonen
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include "abook.h"
13 #include "abook_rl.h"
14
15 #ifdef HAVE_CONFIG_H
16 #       include "config.h"
17 #endif
18
19 #if defined(HAVE_READLINE_READLINE_H)
20 #       include <readline/readline.h>
21 #elif defined(HAVE_READLINE_H)
22 #       include <readline.h>
23 #endif
24
25 #if defined(HAVE_READLINE_HISTORY_H)
26 #       include <readline/history.h>
27 #elif defined(HAVE_HISTORY_H)
28 #       include <history.h>
29 #endif
30
31 #define RL_READLINE_NAME        "Abook"
32
33 static int rl_x, rl_y;
34 static WINDOW *rl_win;
35
36 static bool rl_cancelled;
37
38 static void
39 rl_refresh()
40 {
41         /*refresh();*/
42         wrefresh(rl_win);
43 }
44
45 static void
46 rline_update()
47 {       
48         int real_point = rl_point + rl_x;
49         
50         if(real_point > (COLS - 1))
51                 mvwaddnstr(rl_win, rl_y, rl_x,
52                         rl_line_buffer + (1 + real_point - COLS),
53                         COLS - rl_x - 1);
54         else
55                 mvwaddnstr(rl_win, rl_y, rl_x, rl_line_buffer, rl_end);
56
57         wclrtoeol(rl_win);
58         wmove(rl_win, rl_y, min(real_point, COLS - 1));
59
60         rl_refresh();
61 }
62
63 static void
64 rline_compdisp(char **matches, int n, int max_len)
65 {
66         /* dummy */
67 }
68
69 static int
70 rl_cancel(int dummy1, int dummy2)
71 {
72         rl_cancelled = TRUE;
73
74         rl_done = 1;
75
76         return 0;
77 }
78
79 static void
80 abook_rl_init(bool use_completion)
81 {
82         rl_readline_name = RL_READLINE_NAME;
83         
84         rl_already_prompted = 1;
85         rl_catch_sigwinch = 0;
86         
87         rl_redisplay_function = rline_update;
88         rl_completion_display_matches_hook = rline_compdisp;
89
90         rl_unbind_function_in_map(rl_clear_screen, rl_get_keymap());
91         rl_unbind_function_in_map(rl_reverse_search_history, rl_get_keymap());
92         rl_unbind_function_in_map(rl_re_read_init_file, rl_get_keymap());
93         
94         if(use_completion) {
95                 rl_bind_key('\t', rl_menu_complete);
96         } else {
97                 rl_unbind_function_in_map(rl_complete, rl_get_keymap());
98                 rl_unbind_function_in_map(rl_menu_complete, rl_get_keymap());
99         }
100
101         rl_bind_key('g' & 31, rl_cancel); /* C-g */
102
103         clear_history();
104
105         rl_cancelled = FALSE;
106 }       
107
108 char *
109 abook_readline(WINDOW *w, int y, int x, char *s, int limit, bool use_completion)
110 {
111         char *ret;
112
113         abook_rl_init(use_completion);
114
115         wmove(rl_win = w, rl_y = y, rl_x = x);
116         rl_refresh();
117
118         if(s && *s)
119                 add_history(s);
120         
121         nocbreak();
122         ret = readline(NULL);
123         cbreak();
124
125         if(rl_cancelled && ret) {
126                 free(ret);
127                 ret = NULL;
128         }
129
130         return ret;
131 }
132