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