Author: yamakenz
Date: Fri Jul 20 15:25:44 2007
New Revision: 4767
Modified:
trunk/scm/anthy-utf8.scm
trunk/scm/anthy.scm
trunk/uim/anthy-utf8.c
Log:
* uim/anthy-utf8.c
- (initialized, context_list): New static variable
- (MAX_CONTEXT, struct context, context_slot, get_anthy_context,
anthy_version_major, anthy_version_minor, get_anthy_version): Removed
- (anthy_version): Return bare Anthy version string
- (init_anthy_lib):
* Remove get_anthy_version() invocation
* Replace integer-based 'id' with pointer object
- (create_context, release_context, set_string,
get_nr_segments, get_nr_candidates, get_unconv_candidate,
resize_segment, commit_segment, set_prediction_src_string,
get_nr_predictions, get_nth_prediction, commit_nth_prediction,
uim_anthy_plugin_instance_init, uim_anthy_plugin_instance_quit):
Replace integer-based 'id' with pointer object
- (get_nth_candidate, get_segment_length):
* Ditto
* Add lacking negative index check
* scm/anthy-utf8.scm
- Require SRFI-1, SRFI-8
- (anthy-utf8-version->major.minor): New procedure
- (anthy-utf8-context-new): Follow the change of anthy-lib-get-anthy-version
- (anthy-utf8-release-handler, anthy-utf8-begin-conv): Replace ac-id
validity check by number? with not-false to follow the id->ptr
change
* scm/anthy.scm
- (anthy-release-handler): Follow the id->ptr change
Modified: trunk/scm/anthy-utf8.scm
==============================================================================
--- trunk/scm/anthy-utf8.scm (original)
+++ trunk/scm/anthy-utf8.scm Fri Jul 20 15:25:44 2007
@@ -30,6 +30,8 @@
;;; SUCH DAMAGE.
;;;;
+(require-extension (srfi 1 8))
+
(require "util.scm")
(require "ustr.scm")
(require "japanese.scm")
@@ -72,6 +74,15 @@
;; I don't think the key needs to be customizable.
(define-key anthy-space-key? '(" "))
+;; Handle Anthy's version scheme like 7100b, 8158memm.
+(define anthy-utf8-version->major.minor
+ (lambda (vstr)
+ (if (string=? vstr "(unknown)")
+ '("-1" . "")
+ (receive (maj min) (span char-numeric? (string->list vstr))
+ (cons (list->string maj)
+ (list->string min))))))
+
(define anthy-utf8-prepare-input-rule-activation
(lambda (ac)
(cond
@@ -323,7 +334,8 @@
(if (symbol-bound? 'anthy-utf8-lib-init)
(begin
(set! anthy-utf8-lib-initialized? (anthy-utf8-lib-init))
- (set! anthy-version (anthy-utf8-lib-get-anthy-version))))
+ (set! anthy-version (anthy-utf8-version->major.minor
+ (anthy-utf8-lib-get-anthy-version)))))
(if anthy-utf8-lib-initialized?
(anthy-utf8-context-set-ac-id!
ac (anthy-utf8-lib-alloc-context anthy-utf8-encoding)))
@@ -446,7 +458,7 @@
(define anthy-utf8-release-handler
(lambda (ac)
(let ((ac-id (anthy-utf8-context-ac-id ac)))
- (if (number? ac-id)
+ (if ac-id
(anthy-utf8-lib-free-context ac-id)))))
(define anthy-utf8-flush
@@ -537,7 +549,7 @@
(let* ((ac-id (anthy-utf8-context-ac-id ac))
(kana (anthy-utf8-context-kana-mode ac))
(preconv-str (anthy-utf8-make-whole-string ac #t
anthy-type-hiragana)))
- (if (and (number? (anthy-utf8-context-ac-id ac))
+ (if (and ac-id
(> (string-length preconv-str)
0))
(begin
Modified: trunk/scm/anthy.scm
==============================================================================
--- trunk/scm/anthy.scm (original)
+++ trunk/scm/anthy.scm Fri Jul 20 15:25:44 2007
@@ -453,7 +453,7 @@
(define anthy-release-handler
(lambda (ac)
(let ((ac-id (anthy-context-ac-id ac)))
- (if (number? ac-id)
+ (if ac-id
(anthy-lib-free-context ac-id)))))
(define anthy-flush
Modified: trunk/uim/anthy-utf8.c
==============================================================================
--- trunk/uim/anthy-utf8.c (original)
+++ trunk/uim/anthy-utf8.c Fri Jul 20 15:25:44 2007
@@ -29,7 +29,7 @@
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
-/**/
+
#include <config.h>
#include <stdlib.h>
@@ -44,88 +44,29 @@
#include "uim-util.h"
#include "plugin.h"
-#define MAX_CONTEXT 256
-/* handle anthy's version scheme like 7100b, 8158memm */
-static char *anthy_version_major;
-static char *anthy_version_minor;
+static uim_bool initialized;
+static uim_lisp context_list;
static iconv_t iconv_cd_e2u;
static iconv_t iconv_cd_u2e;
-static struct context {
- anthy_context_t ac;
-} *context_slot;
-
-static anthy_context_t
-get_anthy_context(int id)
-{
- if (id < 0 || id >= MAX_CONTEXT) {
- return NULL;
- }
- return context_slot[id].ac;
-}
static uim_lisp
anthy_version()
{
- return uim_scm_cons(uim_scm_make_str(anthy_version_major),
- uim_scm_make_str(anthy_version_minor));
-}
-
-static void
-get_anthy_version()
-{
- const char *str;
-
- free(anthy_version_major);
- free(anthy_version_minor);
-
- str = anthy_get_version_string();
-
- if (!str || (!strcmp(str, "(unknown)"))) {
- anthy_version_major = strdup("-1");
- anthy_version_minor = strdup("");
- } else {
- int len, i;
-
- len = strlen(str);
- for (i = 0; str[i] != '\0'; i++) {
- if (isalpha(str[i]))
- break;
- }
-
- if (i != len) {
- anthy_version_major = malloc(i + 1);
- anthy_version_minor = malloc(len - i + 1);
- strlcpy(anthy_version_major, str, i + 1);
- strlcpy(anthy_version_minor, &str[i], len - i + 1);
- } else {
- anthy_version_major = strdup(str);
- anthy_version_minor = strdup("");
- }
- }
+ return uim_scm_make_str(anthy_get_version_string());
}
static uim_lisp
init_anthy_lib(void)
{
- int i;
-
- if (context_slot)
- return uim_scm_t();
-
- if (anthy_init() == -1)
- return uim_scm_f();
-
- get_anthy_version();
-
- context_slot = malloc(sizeof(struct context) * MAX_CONTEXT);
- if (!context_slot)
- return uim_scm_f();
+ if (!initialized) {
+ if (anthy_init() == -1)
+ return uim_scm_f();
- for (i = 0; i < MAX_CONTEXT; i++)
- context_slot[i].ac = NULL;
+ initialized = UIM_TRUE;
+ }
return uim_scm_t();
}
@@ -133,10 +74,9 @@
static uim_lisp
create_context(uim_lisp encoding_)
{
- int i, encoding;
-
- if (!context_slot)
- return uim_scm_f();
+ anthy_context_t ac;
+ uim_lisp ac_;
+ int encoding;
/* 0: compiled, 1: EUC-JP, 2: UTF-8 */
encoding = uim_scm_c_int(encoding_);
@@ -147,186 +87,178 @@
if (!iconv_cd_u2e)
iconv_cd_u2e = (iconv_t)uim_iconv->create("EUC-JP", "UTF-8");
- for (i = 0; i < MAX_CONTEXT; i++) {
- if (!context_slot[i].ac) {
- anthy_context_t ac = anthy_create_context();
- if (!ac)
- return uim_scm_f();
-
- anthy_context_set_encoding(ac, encoding);
- context_slot[i].ac = ac;
- return uim_scm_make_int(i);
- }
+ ac = anthy_create_context();
+ if (ac) {
+ anthy_context_set_encoding(ac, encoding);
+ ac_ = uim_scm_make_ptr(ac);
+ context_list = uim_scm_callf("cons", "oo", ac_, context_list);
+ return ac_;
}
+
return uim_scm_f();
}
static uim_lisp
-release_context(uim_lisp id_)
+release_context(uim_lisp ac_)
{
- int id = uim_scm_c_int(id_);
- if (context_slot[id].ac) {
- anthy_release_context(context_slot[id].ac);
- context_slot[id].ac = NULL;
- }
+ anthy_context_t ac;
+
+ context_list = uim_scm_callf("delete!", "oo", ac_, context_list);
+
+ ac = uim_scm_c_ptr(ac_);
+ anthy_release_context(ac);
+
return uim_scm_f();
}
static uim_lisp
-set_string(uim_lisp id_, uim_lisp str_)
+set_string(uim_lisp ac_, uim_lisp str_)
{
- int id = uim_scm_c_int(id_);
- char *str;
- anthy_context_t ac = get_anthy_context(id);
- if (!ac) {
- return uim_scm_f();
- }
- str = uim_scm_c_str(str_);
+ anthy_context_t ac;
+ const char *str;
+
+ ac = uim_scm_c_ptr(ac_);
+ str = uim_scm_refer_c_str(str_);
anthy_set_string(ac, str);
- free(str);
+
return uim_scm_f();
}
static uim_lisp
-get_nr_segments(uim_lisp id_)
+get_nr_segments(uim_lisp ac_)
{
- int id = uim_scm_c_int(id_);
- struct anthy_conv_stat acs;
- anthy_context_t ac = get_anthy_context(id);
- if (!ac) {
- return uim_scm_f();
- }
- anthy_get_stat(ac, &acs);
+ anthy_context_t ac;
+ struct anthy_conv_stat cs;
+
+ ac = uim_scm_c_ptr(ac_);
+ anthy_get_stat(ac, &cs);
- return uim_scm_make_int(acs.nr_segment);
+ return uim_scm_make_int(cs.nr_segment);
}
static uim_lisp
-get_nr_candidates(uim_lisp id_, uim_lisp nth_)
+get_nr_candidates(uim_lisp ac_, uim_lisp nth_)
{
- int id, nth;
anthy_context_t ac;
+ int nth;
struct anthy_conv_stat cs;
- id = uim_scm_c_int(id_);
+ struct anthy_segment_stat ss;
+
+ ac = uim_scm_c_ptr(ac_);
nth = uim_scm_c_int(nth_);
- ac = get_anthy_context(id);
- if (!ac) {
- return uim_scm_f();
- }
+
anthy_get_stat(ac, &cs);
- if (nth < cs.nr_segment) {
- struct anthy_segment_stat ss;
- anthy_get_segment_stat(ac, nth, &ss);
- return uim_scm_make_int(ss.nr_candidate);
- }
- return uim_scm_f();
+ if (!(0 <= nth && nth < cs.nr_segment))
+ return uim_scm_f(); /* FIXME: uim_scm_error() */
+
+ anthy_get_segment_stat(ac, nth, &ss);
+ return uim_scm_make_int(ss.nr_candidate);
}
static uim_lisp
-get_nth_candidate(uim_lisp id_, uim_lisp seg_, uim_lisp nth_)
+get_nth_candidate(uim_lisp ac_, uim_lisp seg_, uim_lisp nth_)
{
- int id, seg, nth, buflen;
+ anthy_context_t ac;
+ int seg, nth, buflen;
char *buf;
uim_lisp buf_;
- anthy_context_t ac;
- id = uim_scm_c_int(id_);
+ ac = uim_scm_c_ptr(ac_);
seg = uim_scm_c_int(seg_);
nth = uim_scm_c_int(nth_);
- ac = get_anthy_context(id);
-
- if (!ac)
- return uim_scm_f();
buflen = anthy_get_segment(ac, seg, nth, NULL, 0);
if (buflen == -1)
- return uim_scm_f();
+ return uim_scm_f(); /* FIXME: uim_scm_error() */
buf = malloc(buflen + 1);
anthy_get_segment(ac, seg, nth, buf, buflen + 1);
- buf_ = uim_scm_make_str(buf);
- free(buf);
+ buf_ = uim_scm_make_str_directly(buf);
return buf_;
}
static uim_lisp
-get_unconv_candidate(uim_lisp id_, uim_lisp seg_)
+get_unconv_candidate(uim_lisp ac_, uim_lisp seg_)
{
- return get_nth_candidate(id_, seg_,
uim_scm_make_int(NTH_UNCONVERTED_CANDIDATE));
+ uim_lisp nth_;
+
+ nth_ = uim_scm_make_int(NTH_UNCONVERTED_CANDIDATE);
+ return get_nth_candidate(ac_, seg_, nth_);
}
static uim_lisp
-get_segment_length(uim_lisp id_, uim_lisp nth_)
+get_segment_length(uim_lisp ac_, uim_lisp nth_)
{
- int id, nth;
anthy_context_t ac;
+ int nth;
struct anthy_conv_stat cs;
- id = uim_scm_c_int(id_);
+ struct anthy_segment_stat ss;
+
+ ac = uim_scm_c_ptr(ac_);
nth = uim_scm_c_int(nth_);
- ac = get_anthy_context(id);
- if (!ac) {
- return uim_scm_f();
- }
+
anthy_get_stat(ac, &cs);
- if (nth < cs.nr_segment) {
- struct anthy_segment_stat ss;
- anthy_get_segment_stat(ac, nth, &ss);
- return uim_scm_make_int(ss.seg_len);
- }
- return uim_scm_f();
+ if (!(0 <= nth && nth < cs.nr_segment))
+ return uim_scm_f(); /* FIXME: uim_scm_error() */
+
+ anthy_get_segment_stat(ac, nth, &ss);
+ return uim_scm_make_int(ss.seg_len);
}
static uim_lisp
-resize_segment(uim_lisp id_, uim_lisp seg_, uim_lisp cnt_)
+resize_segment(uim_lisp ac_, uim_lisp seg_, uim_lisp cnt_)
{
- int id = uim_scm_c_int(id_);
- int seg = uim_scm_c_int(seg_);
- int cnt = uim_scm_c_int(cnt_);
- anthy_context_t ac = get_anthy_context(id);
+ anthy_context_t ac;
+ int seg, cnt;
+
+ ac = uim_scm_c_ptr(ac_);
+ seg = uim_scm_c_int(seg_);
+ cnt = uim_scm_c_int(cnt_);
+
anthy_resize_segment(ac, seg, cnt);
return uim_scm_f();
}
static uim_lisp
-commit_segment(uim_lisp id_, uim_lisp s_, uim_lisp nth_)
+commit_segment(uim_lisp ac_, uim_lisp s_, uim_lisp nth_)
{
- int id = uim_scm_c_int(id_);
- int s = uim_scm_c_int(s_);
- int nth = uim_scm_c_int(nth_);
- anthy_context_t ac = get_anthy_context(id);
+ anthy_context_t ac;
+ int s, nth;
+
+ ac = uim_scm_c_ptr(ac_);
+ s = uim_scm_c_int(s_);
+ nth = uim_scm_c_int(nth_);
+
anthy_commit_segment(ac, s, nth);
return uim_scm_f();
}
static uim_lisp
-set_prediction_src_string(uim_lisp id_, uim_lisp str_)
+set_prediction_src_string(uim_lisp ac_, uim_lisp str_)
{
#ifdef HAS_ANTHY_PREDICTION
- int id = uim_scm_c_int(id_);
- const char *str = uim_scm_refer_c_str(str_);
- anthy_context_t ac = get_anthy_context(id);
- if (!ac) {
- return uim_scm_f();
- }
+ anthy_context_t ac;
+ const char *str;
+
+ ac = uim_scm_c_ptr(ac_);
+ str = uim_scm_refer_c_str(str_);
+
anthy_set_prediction_string(ac, str);
- return uim_scm_f();
-#else
- return uim_scm_f();
#endif
+ return uim_scm_f();
}
static uim_lisp
-get_nr_predictions(uim_lisp id_)
+get_nr_predictions(uim_lisp ac_)
{
#ifdef HAS_ANTHY_PREDICTION
- int id = uim_scm_c_int(id_);
- anthy_context_t ac = get_anthy_context(id);
+ anthy_context_t ac;
struct anthy_prediction_stat ps;
- if (!ac) {
- return uim_scm_f();
- }
+
+ ac = uim_scm_c_ptr(ac_);
+
anthy_get_prediction_stat(ac, &ps);
return uim_scm_make_int(ps.nr_prediction);
#else
@@ -335,26 +267,25 @@
}
static uim_lisp
-get_nth_prediction(uim_lisp id_, uim_lisp nth_)
+get_nth_prediction(uim_lisp ac_, uim_lisp nth_)
{
#ifdef HAS_ANTHY_PREDICTION
- int id = uim_scm_c_int(id_);
- int nth = uim_scm_c_int(nth_);
- int buflen;
+ anthy_context_t ac;
+ int nth, buflen;
char *buf;
uim_lisp buf_;
- anthy_context_t ac = get_anthy_context(id);
- if (!ac) {
- return uim_scm_f();
- }
+
+ ac = uim_scm_c_ptr(ac_);
+ nth = uim_scm_c_int(nth_);
+
buflen = anthy_get_prediction(ac, nth, NULL, 0);
- if (buflen == -1) {
+ if (buflen == -1)
return uim_scm_f();
- }
- buf = (char *)malloc(buflen + 1);
+
+ buf = malloc(buflen + 1);
anthy_get_prediction(ac, nth, buf, buflen + 1);
- buf_ = uim_scm_make_str(buf);
- free(buf);
+ buf_ = uim_scm_make_str_directly(buf);
+
return buf_;
#else
return uim_scm_f();
@@ -362,16 +293,14 @@
}
static uim_lisp
-commit_nth_prediction(uim_lisp id_, uim_lisp nth_)
+commit_nth_prediction(uim_lisp ac_, uim_lisp nth_)
{
#ifdef HAS_ANTHY_COMMIT_PREDICTION
- int id = uim_scm_c_int(id_);
- int nth = uim_scm_c_int(nth_);
- anthy_context_t ac = get_anthy_context(id);
+ anthy_context_t ac;
+ int nth;
- if (!ac) {
- return uim_scm_f();
- }
+ ac = uim_scm_c_ptr(ac_);
+ nth = uim_scm_c_int(nth_);
return anthy_commit_prediction(ac, nth) ? uim_scm_f() : uim_scm_t();
#else
@@ -423,6 +352,9 @@
uim_anthy_plugin_instance_init(void)
#endif
{
+ context_list = uim_scm_null();
+ uim_scm_gc_protect(&context_list);
+
uim_scm_init_subr_0("anthy-utf8-lib-init", init_anthy_lib);
uim_scm_init_subr_1("anthy-utf8-lib-alloc-context", create_context);
uim_scm_init_subr_1("anthy-utf8-lib-free-context", release_context);
@@ -452,31 +384,22 @@
uim_anthy_plugin_instance_quit(void)
#endif
{
- int i;
-
- if (!context_slot) {
- return;
- }
-
- for (i = 0; i < MAX_CONTEXT; i++) {
- if (context_slot[i].ac) {
- anthy_release_context(context_slot[i].ac);
+ if (initialized) {
+ uim_scm_callf("for-each", "vo",
+ "anthy-utf8-lib-free-context", context_list);
+ context_list = uim_scm_null();
+ uim_scm_gc_unprotect(&context_list);
+
+ anthy_quit();
+ initialized = UIM_FALSE;
+
+ if (iconv_cd_e2u) {
+ uim_iconv->release(iconv_cd_e2u);
+ iconv_cd_e2u = NULL;
+ }
+ if (iconv_cd_u2e) {
+ uim_iconv->release(iconv_cd_u2e);
+ iconv_cd_u2e = NULL;
}
- }
-
- anthy_quit();
-
- if (context_slot) {
- free(context_slot);
- context_slot = NULL;
- }
-
- if (iconv_cd_e2u) {
- uim_iconv->release(iconv_cd_e2u);
- iconv_cd_e2u = NULL;
- }
- if (iconv_cd_u2e) {
- uim_iconv->release(iconv_cd_u2e);
- iconv_cd_u2e = NULL;
}
}