]> git.decadent.org.uk Git - ion3.git/blob - mod_query/main.c
[svn-upgrade] Integrating new upstream version, ion3 (20070506)
[ion3.git] / mod_query / main.c
1 /*
2  * ion/mod_query/main.c
3  *
4  * Copyright (c) Tuomo Valkonen 1999-2007. 
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 };
58
59
60 /*EXTL_DOC
61  * Set module configuration. The following are supported:
62  *
63  * \begin{tabularx}{\linewidth}{lX}
64  *  \tabhead{Field & Description}
65  *  \var{autoshowcompl} & (boolean) Is auto-show-completions enabled?
66  *      (default: true). \\
67  *  \var{autoshowcompl_delay} & (integer) auto-show-completions delay
68  *      in milliseconds (default: 250). \\
69  *  \var{caseicompl} & (boolean) Turn some completions case-insensitive
70  *      (default: false). \\
71  * \end{tabularx}
72  */
73 EXTL_EXPORT
74 void mod_query_set(ExtlTab tab)
75 {
76     ModQueryConfig *c=&mod_query_config;
77
78     extl_table_gets_b(tab, "autoshowcompl", &c->autoshowcompl);
79     extl_table_gets_b(tab, "caseicompl", &c->caseicompl);
80     
81     if(extl_table_gets_i(tab, "autoshowcompl_delay",
82                          &c->autoshowcompl_delay)){
83         c->autoshowcompl_delay=maxof(c->autoshowcompl_delay, 0);
84     }
85 }
86
87 /*EXTL_DOC
88  * Get module configuration. For more information see
89  * \fnref{mod_query.set}.
90  */
91 EXTL_SAFE
92 EXTL_EXPORT
93 ExtlTab mod_query_get()
94 {
95     ModQueryConfig *c=&mod_query_config;
96     ExtlTab tab=extl_create_table();
97     
98     extl_table_sets_b(tab, "autoshowcompl", c->autoshowcompl);
99     extl_table_sets_b(tab, "caseicompl", c->caseicompl);
100     extl_table_sets_i(tab, "autoshowcompl_delay", c->autoshowcompl_delay);
101     
102     return tab;
103 }
104
105
106 /*}}}*/
107
108
109 /*{{{ Init & deinit */
110
111
112 static void load_history()
113 {
114     ExtlTab tab;
115     int i, n;
116
117     if(!extl_read_savefile("saved_queryhist", &tab))
118         return;
119     
120     n=extl_table_get_n(tab);
121     
122     for(i=n; i>=1; i--){
123         char *s=NULL;
124         if(extl_table_geti_s(tab, i, &s)){
125             mod_query_history_push(s);
126             free(s);
127         }
128     }
129     
130     extl_unref_table(tab);
131 }
132
133
134 static void save_history()
135 {
136     ExtlTab tab=mod_query_history_table();
137     
138     extl_write_savefile("saved_queryhist", tab);
139     
140     extl_unref_table(tab);
141 }
142
143
144 static bool loaded_ok=FALSE;
145
146 void mod_query_deinit()
147 {
148     mod_query_unregister_exports();
149
150     if(mod_query_input_bindmap!=NULL){
151         ioncore_free_bindmap("WInput", mod_query_input_bindmap);
152         mod_query_input_bindmap=NULL;
153     }
154     
155     if(mod_query_wedln_bindmap!=NULL){
156         ioncore_free_bindmap("WEdln", mod_query_wedln_bindmap);
157         mod_query_wedln_bindmap=NULL;
158     }
159     
160     hook_remove(ioncore_snapshot_hook, save_history);
161 }
162
163
164 bool mod_query_init()
165 {
166     if(!mod_query_register_exports())
167         goto err;
168     
169     mod_query_input_bindmap=ioncore_alloc_bindmap("WInput", NULL);
170     mod_query_wedln_bindmap=ioncore_alloc_bindmap("WEdln", NULL);
171     
172     if(mod_query_wedln_bindmap==NULL ||
173        mod_query_input_bindmap==NULL){
174         goto err;
175     }
176
177     /*ioncore_read_config("cfg_query", NULL, TRUE);*/
178
179     load_history();
180     
181     loaded_ok=TRUE;
182
183     hook_add(ioncore_snapshot_hook, save_history);
184     
185     return TRUE;
186     
187 err:
188     mod_query_deinit();
189     return FALSE;
190 }
191
192
193 /*}}}*/
194