forked from minad/jinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jinx-mod.c
159 lines (137 loc) · 5.86 KB
/
jinx-mod.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
/* Jinx bindings to libenchant
Copyright (C) 2023 Free Software Foundation, Inc.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <emacs-module.h>
#include <enchant.h>
#include <string.h>
int plugin_is_GPL_compatible;
static EnchantBroker* broker = 0;
#define jinx_unused(var) _##var __attribute__((unused))
#define jinx_autofree __attribute__((cleanup(jinx_autofree_cleanup)))
static void jinx_autofree_cleanup(char **p) {
free(*(void**)p);
}
static emacs_value jinx_str(emacs_env* env, const char* str) {
return env->make_string(env, str, strlen(str));
}
static emacs_value jinx_cons(emacs_env* env, emacs_value a, emacs_value b) {
return env->funcall(env, env->intern(env, "cons"), 2, (emacs_value[]){a, b});
}
static char* jinx_cstr(emacs_env* env, emacs_value val) {
ptrdiff_t size = 0;
if (!env->copy_string_contents(env, val, 0, &size))
return 0;
char* str = malloc(size);
if (!str) {
env->non_local_exit_signal(env,
env->intern (env, "memory-full"),
env->intern (env, "nil"));
return 0;
}
if (!env->copy_string_contents(env, val, str, &size)) {
free(str);
return 0;
}
return str;
}
static void jinx_defun(emacs_env* env, const char* name,
ptrdiff_t min, ptrdiff_t max, void* fun) {
env->funcall(env, env->intern(env, "defalias"), 2,
(emacs_value[]){
env->intern(env, name),
env->make_function(env, min, max, fun, 0, 0)
});
}
static void jinx_free_dict(void* dict) {
enchant_broker_free_dict(broker, dict);
}
static emacs_value jinx_dict(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
if (!broker)
broker = enchant_broker_init();
jinx_autofree char* str = jinx_cstr(env, args[0]);
EnchantDict* dict = str ? enchant_broker_request_dict(broker, str) : 0;
return dict
? env->make_user_ptr(env, jinx_free_dict, dict)
: env->intern(env, "nil");
}
static void jinx_describe_cb(const char* const lang_tag,
const char* const provider_name,
const char* const jinx_unused(provider_desc),
const char* const jinx_unused(provider_file),
void* data) {
emacs_env* env = ((emacs_env**)data)[0];
((emacs_value*)data)[1] = jinx_cons(env,
jinx_str(env, lang_tag),
jinx_str(env, provider_name));
}
static emacs_value jinx_describe(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
EnchantDict* dict = env->get_user_ptr(env, args[0]);
if (!dict)
return env->intern(env, "nil");
void* data[] = { env, 0 };
enchant_dict_describe(dict, jinx_describe_cb, data);
return data[1];
}
static emacs_value jinx_check(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
EnchantDict* dict = env->get_user_ptr(env, args[0]);
jinx_autofree char* str = jinx_cstr(env, args[1]);
return env->intern(env, !dict || !str || enchant_dict_check(dict, str, -1)
? "nil" : "t");
}
static emacs_value jinx_add(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
EnchantDict* dict = env->get_user_ptr(env, args[0]);
jinx_autofree char* str = jinx_cstr(env, args[1]);
if (dict && str)
enchant_dict_add(dict, str, -1);
return env->intern(env, "nil");
}
static emacs_value jinx_wordchars(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
EnchantDict* dict = env->get_user_ptr(env, args[0]);
return dict
? jinx_str(env, enchant_dict_get_extra_word_characters(dict))
: env->intern(env, "nil");
}
static emacs_value jinx_suggest(emacs_env* env, ptrdiff_t jinx_unused(nargs),
emacs_value args[], void* jinx_unused(data)) {
EnchantDict* dict = env->get_user_ptr(env, args[0]);
jinx_autofree char* str = jinx_cstr(env, args[1]);
emacs_value list = env->intern(env, "nil");
size_t count = 0;
char** suggs = str && dict ? enchant_dict_suggest(dict, str, -1, &count) : 0;
if (suggs) {
while (count > 0) {
char* sugg = suggs[--count];
list = jinx_cons(env, jinx_str(env, sugg), list);
}
enchant_dict_free_string_list(dict, suggs);
}
return list;
}
int emacs_module_init(struct emacs_runtime *runtime) {
if ((size_t)runtime->size < sizeof (*runtime))
return 1;
emacs_env* env = runtime->get_environment(runtime);
if ((size_t)env->size < sizeof (*env))
return 2;
jinx_defun(env, "jinx--mod-suggest", 2, 2, jinx_suggest);
jinx_defun(env, "jinx--mod-check", 2, 2, jinx_check);
jinx_defun(env, "jinx--mod-add", 2, 2, jinx_add);
jinx_defun(env, "jinx--mod-dict", 1, 1, jinx_dict);
jinx_defun(env, "jinx--mod-describe", 1, 1, jinx_describe);
jinx_defun(env, "jinx--mod-wordchars", 1, 1, jinx_wordchars);
return 0;
}