-
Notifications
You must be signed in to change notification settings - Fork 1
/
elisp.c
351 lines (265 loc) · 7.93 KB
/
elisp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
int plugin_is_GPL_compatible;
#include "emacs-module.h"
#include <string.h>
#include "format.h"
#include "index.h"
#include "utils.h"
#define FREE(p) if (p!=NULL) free(p);
#define SYM(sym) env->intern(env, (sym))
#define STRING(val) env->make_string(env, (val), strlen(val))
#define INTEGER(val) env->make_integer(env,(val))
#define NON_LOCAL_EXIT_CHECK \
if (env->non_local_exit_check(env) != emacs_funcall_exit_return) { \
return SYM("nil"); \
}
static emacs_value
make_list(emacs_env *env, int n, emacs_value elts[]) {
emacs_value list=env->funcall(env, SYM("list"), n, elts);
NON_LOCAL_EXIT_CHECK
return list;
}
static int
extract_string_arg(emacs_env *env, emacs_value arg, char **str) {
ptrdiff_t size = 0;
if (!env->copy_string_contents(env, arg, NULL, &size))
return 1;
*str = xmalloc(size);
if (!env->copy_string_contents(env, arg, *str, &size)) {
if ((*str) != 0) free(*str);
*str = 0;
return 1;
}
return 0;
}
static void
close_db(void *ptr) {
struct index_mm *index=(struct index_mm *)ptr;
if (index)
index_mm_close(index);
}
static emacs_value
open_database(emacs_env *env, ptrdiff_t n, emacs_value *args, void *ptr)
{
(void)ptr;
(void)n;
char *index_path;
if (extract_string_arg(env, args[0], &index_path)) {
return SYM("nil");
}
struct index_mm *index;
index=index_mm_open(index_path);
if(!index)
{
emacs_value signal = env->intern(env, "no index");
emacs_value message = STRING(index_path);
free(index_path);
env->non_local_exit_signal(env, signal, message);
}
free(index_path);
return env->make_user_ptr(env, close_db, (void *)index);
}
static emacs_value
render_emacs(emacs_env *env,struct format_ *format)
{
char *src=format->string;
struct
{
TAG type;
size_t open; //open and close in utf8
size_t close;
char *property;
int id;
} T[format->N];
size_t tags_N=0;
size_t utf_opened[N_TAG]={0};
TAG type;
int opened=0;
size_t utf_pos=0;
size_t pos;
for(int i=0; i<format->N;i++)
{
pos=format->tag[i].pos;
type=format->tag[i].type;
for(size_t j=opened;j<pos;j++)
if ((src[j] & 0xC0) != 0x80)
utf_pos++;
opened=pos;
if(format->tag[i].open==1)
{
T[tags_N].type=type;
T[tags_N].open=utf_pos;
T[tags_N].close=utf_pos; //in the case tag is not closed.
T[tags_N].property=format->tag[i].properties;
T[tags_N].id=i;
utf_opened[type]=tags_N;
tags_N++;
}
else
T[utf_opened[type]].close=utf_pos;
}
emacs_value *elts =xmalloc(tags_N*sizeof(emacs_value));
emacs_value tag_param[4];
emacs_value string_param[2];
for(size_t i=0;i<tags_N;i++)
{
tag_param[0]=INTEGER(T[i].type);
tag_param[1]=INTEGER(T[i].open);
tag_param[2]=INTEGER(T[i].close);
tag_param[3]=T[i].property ? STRING(T[i].property) : SYM("nil");
elts[i]=make_list(env, 4, tag_param);
}
string_param[0]=STRING(src);
string_param[1]=make_list(env, tags_N, elts);
FREE(elts);
emacs_value result=make_list(env, 2, string_param);
return result;
}
static emacs_value
word_defs(emacs_env *env, ptrdiff_t n, emacs_value *args, void *ptr)
{
(void)ptr;
(void)n;
if (!env->is_not_nil(env, args[0])) {
return SYM( "nil");
}
struct index_mm *index=(struct index_mm *)env->get_user_ptr(env, args[0]);
NON_LOCAL_EXIT_CHECK;
char *Word;
if (extract_string_arg(env, args[1], &Word)) {
return SYM( "nil");
}
int art=0;
int rc=0;
struct article *Art=NULL;
emacs_value art_param[5];
if((rc=decode_articles(Word,&Art,&art,index))==0)
{
emacs_value *elts =xmalloc(art*sizeof(emacs_value));
for(int i=0;i<art;i++)
{
struct dictionary *Dic=get_dictionary(index->Dic , index->N, Art[i].dic);
art_param[0]=STRING(Dic->data[NAME]);
art_param[1]=INTEGER(Art[i].dic);
art_param[2]=INTEGER(Art[i].start);
art_param[3]=INTEGER(Art[i].size);
art_param[4]=Dic->data[RESOURCES] ? STRING(Dic->data[RESOURCES]) : SYM( "nil");
elts[i]=make_list(env, 5,art_param);
free(Art[i].word);
}
emacs_value result = make_list(env, art,elts);
FREE(elts);
free(Art);
free(Word);
return result;
}
free(Word);
return SYM( "nil");
}
static emacs_value
word_from_dictionary(emacs_env *env, ptrdiff_t n, emacs_value *args, void *ptr)
{
(void)ptr;
(void)n;
if (!env->is_not_nil(env, args[0])) {
return SYM( "nil");
}
struct index_mm *index=(struct index_mm *)env->get_user_ptr(env, args[0]);
NON_LOCAL_EXIT_CHECK;
int dic = env->extract_integer(env, args[1]);
NON_LOCAL_EXIT_CHECK;
int start = env->extract_integer(env, args[2]);
NON_LOCAL_EXIT_CHECK;
int end = env->extract_integer(env, args[3]);
NON_LOCAL_EXIT_CHECK;
char *path_unzip;
if (extract_string_arg(env, args[4], &path_unzip)) {
return SYM( "nil");
}
struct dictionary *Dic=get_dictionary(index->Dic , index->N, dic);
char *definition = word_fetch(Dic, start, end);
struct format_ format;
format.tag=NULL;
strip_tags(definition,&format);
emacs_value result=render_emacs(env,&format);
if(strcmp(path_unzip,"")&&Dic->data[RESOURCES]!=NULL&&strcmp(Dic->data[RESOURCES],""))
unzip_resources(&format, path_unzip, Dic->data[RESOURCES]);
free_format(&format);
free(path_unzip);
free(definition);
return result;
}
static emacs_value
word_lookup(emacs_env *env, ptrdiff_t n, emacs_value *args, void *ptr)
{
(void)ptr;
(void)n;
if (!env->is_not_nil(env, args[0])) {
return SYM( "nil");
}
struct index_mm *index=(struct index_mm *)env->get_user_ptr(env, args[0]);
NON_LOCAL_EXIT_CHECK;
char *Word;
if (extract_string_arg(env, args[1], &Word)) {
return SYM( "nil");
}
int is_prefix = env->extract_integer(env, args[2]);
NON_LOCAL_EXIT_CHECK;
struct article *Art=NULL;
int art=0;
look_for_a_word(index,Word,&art,&Art,is_prefix);
free(Word);
emacs_value res;
if(art>0)
{
emacs_value *elts=xmalloc(art*sizeof(emacs_value));
for(int j=0;j<art;j++)
{
elts[j] = STRING(Art[j].word);
free(Art[j].word);
}
free(Art);
res = make_list(env, art, elts);
FREE(elts);
}
else
res=SYM( "nil");
return res;
}
static emacs_value
close_database(emacs_env *env, ptrdiff_t n, emacs_value *args, void *ptr)
{
(void)ptr;
(void)n;
if (!env->is_not_nil(env, args[0]))
return SYM( "nil");
struct index_mm *index=(struct index_mm *)env->get_user_ptr(env, args[0]);
NON_LOCAL_EXIT_CHECK;
if (index ) {
index_mm_close(index);
env->set_user_ptr(env, args[0], 0);
}
return SYM( "nil");
}
void bind_func(emacs_env *env, const char *name, ptrdiff_t min, ptrdiff_t max,
emacs_value (*function) (emacs_env *env, ptrdiff_t nargs, emacs_value args[],void *) EMACS_NOEXCEPT, const char *doc)
{
emacs_value fset = SYM( "fset");
emacs_value args[2];
args[0] = SYM( name);
args[1] = env->make_function(env, min, max, function, doc, 0);
env->funcall(env, fset, 2, args);
}
int
emacs_module_init(struct emacs_runtime *ert)
{
emacs_env *env = ert->get_environment(ert);
bind_func(env,"gdcv-database-open",1,1,open_database, "Open database with index and return pointer");
bind_func(env,"gdcv-database-close",1,1,close_database, "Close database with given pointer");
bind_func(env,"gdcv-look-word",3,3,word_lookup, "Look for words similar to given word");
bind_func(env,"gdcv-word-defs",2,2,word_defs, "List of all dictionaries with current word");
bind_func(env,"gdcv-word-fetch",5,5,word_from_dictionary, "Return article from specific dictionary ");
emacs_value provide = SYM( "provide");
emacs_value gdcv[] = {SYM( "gdcv-elisp")};
env->funcall(env, provide, 1, gdcv);
return 0;
}