]> git.deb.at Git - pkg/abook.git/blob - abook_rl.c
readline tweaks
[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 void
37 rl_refresh()
38 {
39         /*refresh();*/
40         wrefresh(rl_win);
41 }
42
43 static void
44 rline_update()
45 {       
46         int real_point = rl_point + rl_x;
47         
48         if(real_point > (COLS - 1))
49                 mvwaddnstr(rl_win, rl_y, rl_x,
50                         rl_line_buffer + (1 + real_point - COLS),
51                         COLS - rl_x - 1);
52         else
53                 mvwaddnstr(rl_win, rl_y, rl_x, rl_line_buffer, rl_end);
54
55         wclrtoeol(rl_win);
56         wmove(rl_win, rl_y, min(real_point, COLS - 1));
57
58         rl_refresh();
59 }
60
61 void
62 rline_compdisp(char **matches, int n, int max_len)
63 {
64         /*
65          * dummy
66          */
67 }
68
69 static void
70 abook_rl_init(int use_completion)
71 {
72         rl_readline_name = RL_READLINE_NAME;
73         
74         rl_already_prompted = 1;
75         
76         rl_redisplay_function = rline_update;
77         rl_completion_display_matches_hook = rline_compdisp;
78         
79         rl_unbind_function_in_map(rl_clear_screen, rl_get_keymap());
80
81         if(use_completion)
82                 rl_bind_key('\t', rl_menu_complete);
83         else {
84                 rl_unbind_function_in_map(rl_complete, rl_get_keymap());
85                 rl_unbind_function_in_map(rl_menu_complete, rl_get_keymap());
86         }
87
88         clear_history();
89 }       
90
91 char *
92 abook_readline(WINDOW *w, int y, int x, char *s, int limit, int use_completion)
93 {
94         char *ret = NULL;
95
96         rl_win = w;
97         abook_rl_init(use_completion);
98
99         wmove(rl_win, rl_y = y, rl_x = x);
100         rl_refresh();
101
102         if(s && *s)
103                 add_history(s);
104         
105         ret = readline(NULL);
106
107         return ret;
108 }
109