]> git.deb.at Git - pkg/abook.git/blob - abook_rl.c
initial readline support
[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
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #include "abook.h"
14 #include "abook_rl.h"
15
16 #ifdef HAVE_CONFIG_H
17 #       include "config.h"
18 #endif
19
20 #if defined(HAVE_READLINE_READLINE_H)
21 #       include <readline/readline.h>
22 #elif defined(HAVE_READLINE_H)
23 #       include <readline.h>
24 #endif
25
26 #if defined(HAVE_READLINE_HISTORY_H)
27 #       include <readline/history.h>
28 #elif defined(HAVE_HISTORY_H)
29 #       include <history.h>
30 #endif
31
32 #define RL_READLINE_NAME        "Abook"
33
34 static int rl_x, rl_y;
35 static WINDOW *rl_win;
36
37 static void
38 rl_refresh()
39 {
40         /*refresh();*/
41         wrefresh(rl_win);
42 }
43
44 static void
45 rline_update()
46 {       
47         int real_point = rl_point + rl_x;
48         
49         if(real_point > (COLS - 1))
50                 mvwaddnstr(rl_win, rl_y, rl_x,
51                         rl_line_buffer + (1 + real_point - COLS),
52                         COLS - rl_x - 1);
53         else
54                 mvwaddnstr(rl_win, rl_y, rl_x, rl_line_buffer, rl_end);
55
56         wclrtoeol(rl_win);
57         wmove(rl_win, rl_y, min(real_point, COLS - 1));
58
59         rl_refresh();
60 }
61
62 void
63 rline_compdisp(char **matches, int n, int max_len)
64 {
65         /*
66          * dummy
67          */
68 }
69
70 static void
71 abook_rl_init(int use_completion)
72 {
73         rl_readline_name = RL_READLINE_NAME;
74         
75         rl_already_prompted = 1;
76         
77         rl_redisplay_function = rline_update;
78         rl_completion_display_matches_hook = rline_compdisp;
79         
80         rl_unbind_function_in_map(rl_clear_screen, rl_get_keymap());
81
82         if(use_completion)
83                 rl_bind_key('\t', rl_menu_complete);
84         else
85                 rl_unbind_function_in_map(rl_complete, rl_get_keymap());
86
87         clear_history();
88 }       
89
90 char *
91 abook_readline(WINDOW *w, int y, int x, char *s, int limit, int use_completion)
92 {
93         char *ret = NULL;
94
95         rl_win = w;
96         abook_rl_init(use_completion);
97
98         wmove(rl_win, rl_y = y, rl_x = x);
99         rl_refresh();
100
101         if(s && *s)
102                 add_history(s);
103         
104         ret = readline(NULL);
105
106         return ret;
107 }