]> git.decadent.org.uk Git - ion3.git/blob - mod_query/main.c
Imported Upstream version 20090110
[ion3.git] / mod_query / main.c
1 /*
2  * ion/mod_query/main.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2009. 
5  *
6  * See the included file LICENSE for details.
7  */
8
9 #include <libextl/readconfig.h>
10 #include <libextl/extl.h>
11 #include <libtu/minmax.h>
12 #include <ioncore/binding.h>
13 #include <ioncore/conf-bindings.h>
14 #include <ioncore/frame.h>
15 #include <ioncore/saveload.h>
16 #include <ioncore/bindmaps.h>
17 #include <ioncore/ioncore.h>
18
19 #include "query.h"
20 #include "edln.h"
21 #include "wedln.h"
22 #include "input.h"
23 #include "complete.h"
24 #include "history.h"
25 #include "exports.h"
26 #include "main.h"
27
28
29 /*{{{ Module information */
30
31
32 #include "../version.h"
33
34 char mod_query_ion_api_version[]=ION_API_VERSION;
35
36
37 /*}}}*/
38
39
40 /*{{{ Bindmaps */
41
42
43 WBindmap *mod_query_input_bindmap=NULL;
44 WBindmap *mod_query_wedln_bindmap=NULL;
45
46
47 /*}}}*/
48
49
50 /*{{{ Set & get */
51
52
53 ModQueryConfig mod_query_config={
54     250,
55     TRUE,
56     FALSE,
57     TRUE
58 };
59
60
61 /*EXTL_DOC
62  * Set module configuration. The following are supported:
63  *
64  * \begin{tabularx}{\linewidth}{lX}
65  *  \tabhead{Field & Description}
66  *  \var{autoshowcompl} & (boolean) Is auto-show-completions enabled?
67  *      (default: true). \\
68  *  \var{autoshowcompl_delay} & (integer) auto-show-completions delay
69  *      in milliseconds (default: 250). \\
70  *  \var{caseicompl} & (boolean) Turn some completions case-insensitive
71  *      (default: false). \\
72  *  \var{substrcompl} & (boolean) Complete on sub-strings in some cases
73  *      (default: ftrue). \\
74  * \end{tabularx}
75  */
76 EXTL_EXPORT
77 void mod_query_set(ExtlTab tab)
78 {
79     ModQueryConfig *c=&mod_query_config;
80
81     extl_table_gets_b(tab, "autoshowcompl", &c->autoshowcompl);
82     extl_table_gets_b(tab, "caseicompl", &c->caseicompl);
83     extl_table_gets_b(tab, "substrcompl", &c->substrcompl);
84     
85     if(extl_table_gets_i(tab, "autoshowcompl_delay",
86                          &c->autoshowcompl_delay)){
87         c->autoshowcompl_delay=maxof(c->autoshowcompl_delay, 0);
88     }
89 }
90
91 /*EXTL_DOC
92  * Get module configuration. For more information see
93  * \fnref{mod_query.set}.
94  */
95 EXTL_SAFE
96 EXTL_EXPORT
97 ExtlTab mod_query_get()
98 {
99     ModQueryConfig *c=&mod_query_config;
100     ExtlTab tab=extl_create_table();
101     
102     extl_table_sets_b(tab, "autoshowcompl", c->autoshowcompl);
103     extl_table_sets_i(tab, "autoshowcompl_delay", c->autoshowcompl_delay);
104     extl_table_sets_b(tab, "caseicompl", c->caseicompl);
105     extl_table_sets_b(tab, "substrcompl", c->substrcompl);
106     
107     return tab;
108 }
109
110
111 /*}}}*/
112
113
114 /*{{{ Init & deinit */
115
116
117 static void load_history()
118 {
119     ExtlTab tab;
120     int i, n;
121
122     if(!extl_read_savefile("saved_queryhist", &tab))
123         return;
124     
125     n=extl_table_get_n(tab);
126     
127     for(i=n; i>=1; i--){
128         char *s=NULL;
129         if(extl_table_geti_s(tab, i, &s)){
130             mod_query_history_push(s);
131             free(s);
132         }
133     }
134     
135     extl_unref_table(tab);
136 }
137
138
139 static void save_history()
140 {
141     ExtlTab tab=mod_query_history_table();
142     
143     extl_write_savefile("saved_queryhist", tab);
144     
145     extl_unref_table(tab);
146 }
147
148
149 static bool loaded_ok=FALSE;
150
151 void mod_query_deinit()
152 {
153     mod_query_unregister_exports();
154
155     if(mod_query_input_bindmap!=NULL){
156         ioncore_free_bindmap("WInput", mod_query_input_bindmap);
157         mod_query_input_bindmap=NULL;
158     }
159     
160     if(mod_query_wedln_bindmap!=NULL){
161         ioncore_free_bindmap("WEdln", mod_query_wedln_bindmap);
162         mod_query_wedln_bindmap=NULL;
163     }
164     
165     hook_remove(ioncore_snapshot_hook, save_history);
166 }
167
168
169 bool mod_query_init()
170 {
171     if(!mod_query_register_exports())
172         goto err;
173     
174     mod_query_input_bindmap=ioncore_alloc_bindmap("WInput", NULL);
175     mod_query_wedln_bindmap=ioncore_alloc_bindmap("WEdln", NULL);
176     
177     if(mod_query_wedln_bindmap==NULL ||
178        mod_query_input_bindmap==NULL){
179         goto err;
180     }
181
182     /*ioncore_read_config("cfg_query", NULL, TRUE);*/
183
184     load_history();
185     
186     loaded_ok=TRUE;
187
188     hook_add(ioncore_snapshot_hook, save_history);
189     
190     return TRUE;
191     
192 err:
193     mod_query_deinit();
194     return FALSE;
195 }
196
197
198 /*}}}*/
199