1 /* mimx-anthy.c -- Anthy input method external module. -*- coding: euc-jp; -*-
2 Copyright (C) 2003, 2004
3 National Institute of Advanced Industrial Science and Technology (AIST)
4 Registration Number H15PRO112
6 This file is part of the m17n library.
8 The m17n library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public License
10 as published by the Free Software Foundation; either version 2.1 of
11 the License, or (at your option) any later version.
13 The m17n library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the m17n library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 @enpage mimx-anthy external module for the input method <ja, anthy>
26 @section mimx-anthy-description DESCRIPTION
28 The shared library mimx-anthy.so is an external module used by the
29 input method <ja, anthy>. It exports these functions.
34 Initialize this module.
42 Convert the current preedit text (Hiragana sequence) into
43 Kana-Kanji mixed text.
47 Record the change of candidate of the current segment.
51 Enlarge or shorten the length of the current segment.
55 Commit the lastly selected candidates of all the segments.
59 @section mimx-anthy-seealso See also
63 @japage mimx-anthy ÆþÎϥ᥽¥Ã¥É <ja, anthy> Íѳ°Éô¥â¥¸¥å¡¼¥ë.
65 @section mimx-anthy-description DESCRIPTION
67 ¶¦Í¥é¥¤¥Ö¥é¥ê mimx-anthy.so ¤ÏÆþÎϥ᥽¥Ã¥É<ja, anthy> ¤ËÍѤ¤¤é¤ì
68 ¤ë³°Éô¥â¥¸¥å¡¼¥ë¤Ç¤¢¤ê¡¢°Ê²¼¤Î´Ø¿ô¤ò export ¤·¤Æ¤¤¤ë¡£
81 ¸½ºß¤Î preedit ¥Æ¥¥¹¥È (¤Ò¤é¤¬¤ÊÎó) ¤ò¤«¤Ê´Á»ú¥Æ¥¥¹¥È¤ËÊÑ´¹¤¹¤ë¡£
85 ¸½ºß¤Î¥»¥°¥á¥ó¥È¤Î¸õÊä¤ÎÊÑÁ«¤òµÏ¿¤¹¤ë¡£
89 ¸½ºß¤Î¥»¥°¥á¥ó¥È¤ÎŤµ¤òÊѹ¹¤¹¤ë¡£
93 Á´¥»¥°¥á¥ó¥È¤ÎºÇ¿·¤Î¸õÊä¤ò¥³¥ß¥Ã¥È¤¹¤ë¡£
97 @section mimx-anthy-seealso »²¾È
110 #include <anthy/anthy.h>
112 static int initialized;
113 static MSymbol Manthy, Msegment;
115 /* A structure to record in MInputContext->plist with key Manthy. */
119 /* Which candidate is selected in each segment. */
120 int *candidate_numbers;
121 /* Size of the above array. */
123 /* Converter for this context. */
124 MConverter *converter;
127 static AnthyContext *
128 new_context (MInputContext *ic)
130 AnthyContext *context = NULL;
131 MSymbol euc_jp = msymbol ("euc-jp");
132 /* Rebound to an actual buffer just before being used. */
133 MConverter *converter = mconv_buffer_converter (euc_jp, NULL, 0);
137 context = calloc (1, sizeof (AnthyContext));
138 context->ac = anthy_create_context ();
139 context->num_segments = 0;
140 context->candidate_numbers = NULL;
141 context->converter = converter;
147 free_context (AnthyContext *context)
149 anthy_release_context (context->ac);
150 if (context->candidate_numbers)
151 free (context->candidate_numbers);
152 mconv_free_converter (context->converter);
157 allocate_candidate_numbers (AnthyContext *context, int num)
159 if (context->num_segments < num)
161 if (context->num_segments == 0)
162 context->candidate_numbers = malloc (sizeof (int) * num);
164 context->candidate_numbers = realloc (context->candidate_numbers,
166 context->num_segments = num;
171 add_action (MPlist *actions, MSymbol name, MSymbol key, void *val)
173 MPlist *action = mplist ();
175 mplist_add (action, Msymbol, name);
176 mplist_add (action, key, val);
177 mplist_add (actions, Mplist, action);
178 m17n_object_unref (action);
181 /* Return a list of all candidates of the Nth segment. The return
182 value is a plist whose elements are plists who contains at most 5
186 make_candidate_list (AnthyContext *context, int n)
188 MPlist *plist = mplist (), *pl;
191 struct anthy_segment_stat ss;
194 anthy_get_segment_stat (context->ac, n, &ss);
195 for (i = 0, pl = mplist (); i < ss.nr_candidate; i++)
197 anthy_get_segment (context->ac, n, i, buf, sizeof (buf));
198 mconv_rebind_buffer (context->converter,
199 (unsigned char *) buf, strlen (buf));
200 mt = mconv_decode (context->converter, mtext ());
201 mtext_put_prop (mt, 0, mtext_len (mt), Msegment, (void *) (n + 1));
202 mplist_add (pl, Mtext, mt);
203 m17n_object_unref (mt);
206 mplist_add (plist, Mplist, pl);
207 m17n_object_unref (pl);
211 if (mplist_key (pl) != Mnil)
212 mplist_add (plist, Mplist, pl);
213 m17n_object_unref (pl);
220 MInputContext *ic = mplist_value (args);
225 Manthy = msymbol (" anthy");
226 Msegment = msymbol (" segment");
229 mplist_push (ic->plist, Manthy, new_context (ic));
236 MInputContext *ic = mplist_value (args);
237 AnthyContext *context = mplist_get (ic->plist, Manthy);
240 free_context (context);
245 convert (MPlist *args)
247 MInputContext *ic = mplist_value (args);
248 AnthyContext *context = mplist_get (ic->plist, Manthy);
249 struct anthy_conv_stat cs;
250 MPlist *action, *actions;
252 unsigned char buf[1024];
257 mconv_rebind_buffer (context->converter, buf, sizeof (buf));
258 mconv_encode (context->converter, ic->preedit);
259 buf[context->converter->nbytes] = '\0';
260 anthy_set_string (context->ac, (char *) buf);
261 anthy_get_stat (context->ac, &cs);
262 allocate_candidate_numbers (context, cs.nr_segment);
265 add_action (actions, msymbol ("move"), Msymbol, msymbol ("@<"));
266 add_action (actions, msymbol ("delete"), Msymbol, msymbol ("@>"));
267 for (i = 0; i < cs.nr_segment; i++)
269 context->candidate_numbers[i] = 0;
271 add_action (actions, msymbol ("mark"), Msymbol, msymbol ("@anthy"));
272 action = make_candidate_list (context, i);
273 mplist_add (actions, Mplist, action);
274 m17n_object_unref (action);
276 if (cs.nr_segment > 1)
277 add_action (actions, msymbol ("move"), Msymbol, msymbol ("@anthy"));
283 change (MPlist *args)
285 MInputContext *ic = mplist_value (args);
286 AnthyContext *context = mplist_get (ic->plist, Manthy);
291 if (! ic->candidate_list || ic->cursor_pos == 0)
293 segment = (int) mtext_get_prop (ic->preedit, ic->cursor_pos - 1, Msegment);
297 context->candidate_numbers[segment] = ic->candidate_index;
302 resize (MPlist *args)
304 MInputContext *ic = mplist_value (args);
305 AnthyContext *context = mplist_get (ic->plist, Manthy);
306 struct anthy_conv_stat cs;
309 MPlist *actions, *action;
314 if (! ic->candidate_list || ic->cursor_pos == 0)
316 segment = (int) mtext_get_prop (ic->preedit, ic->cursor_pos - 1, Msegment);
320 args = mplist_next (args);
321 shorten = mplist_value (args);
322 anthy_resize_segment (context->ac, segment, shorten == Mt ? -1 : 1);
323 anthy_get_stat (context->ac, &cs);
324 allocate_candidate_numbers (context, cs.nr_segment);
328 add_action (actions, msymbol ("move"), Msymbol, msymbol ("@<"));
330 add_action (actions, msymbol ("move"), Msymbol, msymbol ("@["));
331 add_action (actions, msymbol ("delete"), Msymbol, msymbol ("@>"));
332 for (i = segment; i < cs.nr_segment; i++)
334 context->candidate_numbers[i] = 0;
335 if (i == segment + 1)
336 add_action (actions, msymbol ("mark"), Msymbol, msymbol ("@anthy"));
337 action = make_candidate_list (context, i);
338 mplist_add (actions, Mplist, action);
339 m17n_object_unref (action);
341 if (segment + 1 < cs.nr_segment)
342 add_action (actions, msymbol ("move"), Msymbol, msymbol ("@anthy"));
347 commit (MPlist *args)
349 MInputContext *ic = mplist_value (args);
350 AnthyContext *context = mplist_get (ic->plist, Manthy);
351 struct anthy_conv_stat cs;
354 anthy_get_stat (context->ac, &cs);
355 for (i = 0; i < cs.nr_segment; i++)
356 anthy_commit_segment (context->ac, i, context->candidate_numbers[i]);
360 #else /* not HAVE_ANTHY */
362 MPlist *convert (MPlist *args) { return NULL; }
363 MPlist *change (MPlist *args) { return NULL; }
364 MPlist *resize (MPlist *args) { return NULL; }
365 MPlist *commit (MPlist *args) { return NULL; }
367 #endif /* not HAVE_ANTHY */
368 #endif /* not FOR_DOXYGEN */