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