3
Copyright (c) 2003-2008 uim Project http://code.google.com/p/uim/
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions
11
1. Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
2. Redistributions in binary form must reproduce the above copyright
14
notice, this list of conditions and the following disclaimer in the
15
documentation and/or other materials provided with the distribution.
16
3. Neither the name of authors nor the names of its contributors
17
may be used to endorse or promote products derived from this software
18
without specific prior written permission.
20
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
21
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39
#include <anthy/anthy.h>
43
#include "uim-scm-abbrev.h"
48
#ifdef ENABLE_ANTHY_UTF8_STATIC
49
void uim_anthy_utf8_plugin_instance_init(void);
50
void uim_anthy_utf8_plugin_instance_quit(void);
53
static uim_bool initialized;
54
static uim_lisp context_list;
56
static void *iconv_cd_e2u;
57
static void *iconv_cd_u2e;
60
validate_segment_index(anthy_context_t ac, int i)
63
struct anthy_conv_stat cs;
65
err = anthy_get_stat(ac, &cs);
67
uim_fatal_error("anthy_get_stat() failed");
68
if (!(0 <= i && i < cs.nr_segment))
69
ERROR_OBJ("invalid segment index", MAKE_INT(i));
72
static anthy_context_t
73
get_anthy_context(uim_lisp ac_)
79
uim_fatal_error("NULL anthy_context_t");
87
return MAKE_STR(anthy_get_version_string());
94
if (anthy_init() == -1)
95
uim_fatal_error("anthy_init() failed");
97
initialized = UIM_TRUE;
104
create_context(uim_lisp encoding_)
110
/* 0: compiled, 1: EUC-JP, 2: UTF-8 */
111
encoding = C_INT(encoding_);
114
iconv_cd_e2u = uim_iconv->create("UTF-8", "EUC-JP");
117
iconv_cd_u2e = uim_iconv->create("EUC-JP", "UTF-8");
119
ac = anthy_create_context();
121
uim_fatal_error("anthy_create_context() failed");
123
anthy_context_set_encoding(ac, encoding);
125
context_list = uim_scm_callf("cons", "oo", ac_, context_list);
132
release_context(uim_lisp ac_)
136
context_list = uim_scm_callf("delete!", "oo", ac_, context_list);
138
ac = get_anthy_context(ac_);
139
anthy_release_context(ac);
140
uim_scm_nullify_c_ptr(ac_);
146
set_string(uim_lisp ac_, uim_lisp str_)
151
ac = get_anthy_context(ac_);
152
str = REFER_C_STR(str_);
153
anthy_set_string(ac, str);
159
get_nr_segments(uim_lisp ac_)
162
struct anthy_conv_stat cs;
165
ac = get_anthy_context(ac_);
166
err = anthy_get_stat(ac, &cs);
168
uim_fatal_error("anthy_get_stat() failed");
170
return MAKE_INT(cs.nr_segment);
174
get_nr_candidates(uim_lisp ac_, uim_lisp seg_)
178
struct anthy_segment_stat ss;
180
ac = get_anthy_context(ac_);
183
validate_segment_index(ac, seg);
185
err = anthy_get_segment_stat(ac, seg, &ss);
187
uim_fatal_error("anthy_get_segment_stat() failed");
189
return MAKE_INT(ss.nr_candidate);
193
get_nth_candidate(uim_lisp ac_, uim_lisp seg_, uim_lisp nth_)
196
int seg, nth, buflen;
200
ac = get_anthy_context(ac_);
204
buflen = anthy_get_segment(ac, seg, nth, NULL, 0);
206
uim_fatal_error("anthy_get_segment() failed");
208
buf = uim_malloc(buflen + 1);
209
buflen = anthy_get_segment(ac, seg, nth, buf, buflen + 1);
212
uim_fatal_error("anthy_get_segment() failed");
214
buf_ = MAKE_STR_DIRECTLY(buf);
220
get_unconv_candidate(uim_lisp ac_, uim_lisp seg_)
224
nth_ = MAKE_INT(NTH_UNCONVERTED_CANDIDATE);
225
return get_nth_candidate(ac_, seg_, nth_);
229
get_segment_length(uim_lisp ac_, uim_lisp seg_)
233
struct anthy_segment_stat ss;
235
ac = get_anthy_context(ac_);
238
validate_segment_index(ac, seg);
240
err = anthy_get_segment_stat(ac, seg, &ss);
242
uim_fatal_error("anthy_get_segment_stat() failed");
244
return MAKE_INT(ss.seg_len);
248
resize_segment(uim_lisp ac_, uim_lisp seg_, uim_lisp delta_)
253
ac = get_anthy_context(ac_);
255
delta = C_INT(delta_);
257
anthy_resize_segment(ac, seg, delta);
262
commit_segment(uim_lisp ac_, uim_lisp seg_, uim_lisp nth_)
267
ac = get_anthy_context(ac_);
271
anthy_commit_segment(ac, seg, nth);
276
set_prediction_src_string(uim_lisp ac_, uim_lisp str_)
278
#ifdef HAS_ANTHY_PREDICTION
282
ac = get_anthy_context(ac_);
283
str = REFER_C_STR(str_);
285
anthy_set_prediction_string(ac, str);
291
get_nr_predictions(uim_lisp ac_)
293
#ifdef HAS_ANTHY_PREDICTION
295
struct anthy_prediction_stat ps;
298
ac = get_anthy_context(ac_);
300
err = anthy_get_prediction_stat(ac, &ps);
302
uim_fatal_error("anthy_get_prediction_stat() failed");
303
return MAKE_INT(ps.nr_prediction);
310
get_nth_prediction(uim_lisp ac_, uim_lisp nth_)
312
#ifdef HAS_ANTHY_PREDICTION
318
ac = get_anthy_context(ac_);
321
buflen = anthy_get_prediction(ac, nth, NULL, 0);
323
uim_fatal_error("anthy_get_prediction() failed");
325
buf = uim_malloc(buflen + 1);
326
buflen = anthy_get_prediction(ac, nth, buf, buflen + 1);
329
uim_fatal_error("anthy_get_prediction() failed");
331
buf_ = MAKE_STR_DIRECTLY(buf);
340
commit_nth_prediction(uim_lisp ac_, uim_lisp nth_)
342
#ifdef HAS_ANTHY_COMMIT_PREDICTION
346
ac = get_anthy_context(ac_);
349
err = anthy_commit_prediction(ac, nth);
351
return MAKE_BOOL(!err);
358
eucjp_to_utf8(uim_lisp str_)
365
return MAKE_STR("〓");
367
str = REFER_C_STR(str_);
368
convstr = uim_iconv->convert(iconv_cd_e2u, str);
369
utf8_ = MAKE_STR(convstr);
376
utf8_to_eucjp(uim_lisp str_)
385
str = REFER_C_STR(str_);
386
convstr = uim_iconv->convert(iconv_cd_u2e, str);
387
eucjp_ = MAKE_STR(convstr);
393
#ifndef ENABLE_ANTHY_UTF8_STATIC
395
uim_plugin_instance_init(void)
398
uim_anthy_utf8_plugin_instance_init(void)
401
context_list = uim_scm_null();
402
uim_scm_gc_protect(&context_list);
404
uim_scm_eval_c_string("(require-extension (srfi 1))"); /* for delete! */
406
uim_scm_init_proc0("anthy-utf8-lib-init", init_anthy_lib);
407
uim_scm_init_proc1("anthy-utf8-lib-alloc-context", create_context);
408
uim_scm_init_proc1("anthy-utf8-lib-free-context", release_context);
409
uim_scm_init_proc2("anthy-utf8-lib-set-string", set_string);
410
uim_scm_init_proc1("anthy-utf8-lib-get-nr-segments",get_nr_segments);
411
uim_scm_init_proc2("anthy-utf8-lib-get-nr-candidates", get_nr_candidates);
412
uim_scm_init_proc3("anthy-utf8-lib-get-nth-candidate", get_nth_candidate);
413
uim_scm_init_proc2("anthy-utf8-lib-get-unconv-candidate", get_unconv_candidate);
414
uim_scm_init_proc2("anthy-utf8-lib-get-segment-length", get_segment_length);
415
uim_scm_init_proc3("anthy-utf8-lib-resize-segment", resize_segment);
416
uim_scm_init_proc3("anthy-utf8-lib-commit-segment", commit_segment);
417
uim_scm_init_proc0("anthy-utf8-lib-get-anthy-version", anthy_version);
418
uim_scm_init_proc2("anthy-utf8-lib-set-prediction-src-string", set_prediction_src_string);
419
uim_scm_init_proc1("anthy-utf8-lib-get-nr-predictions", get_nr_predictions);
420
uim_scm_init_proc2("anthy-utf8-lib-get-nth-prediction", get_nth_prediction);
421
uim_scm_init_proc2("anthy-utf8-lib-commit-nth-prediction",
422
commit_nth_prediction);
423
uim_scm_init_proc1("anthy-utf8-lib-eucjp-to-utf8", eucjp_to_utf8);
424
uim_scm_init_proc1("anthy-utf8-lib-utf8-to-eucjp", utf8_to_eucjp);
427
#ifndef ENABLE_ANTHY_UTF8_STATIC
429
uim_plugin_instance_quit(void)
432
uim_anthy_utf8_plugin_instance_quit(void)
436
uim_scm_callf("for-each", "vo",
437
"anthy-utf8-lib-free-context", context_list);
438
context_list = uim_scm_null();
439
uim_scm_gc_unprotect(&context_list);
442
initialized = UIM_FALSE;
445
uim_iconv->release(iconv_cd_e2u);
449
uim_iconv->release(iconv_cd_u2e);