~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to uim/m17nlib.c

  • Committer: Bazaar Package Importer
  • Author(s): Steinar H. Gunderson
  • Date: 2006-07-06 22:17:24 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060706221724-6sobw1fcsxj647hp
Tags: 1:1.1.0-1.2
* Non-maintainer upload.
* Added -Wno-cast-align to CFLAGS, as per the RM's recommendations:

  < vorlon> Sesse: -Wno-cast-align and to hell with it :P

  Really fixes FTBFS. (Really Closes: #375081)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 
3
 
  Copyright (c) 2003,2004,2005 uim Project http://uim.freedesktop.org/
 
3
  Copyright (c) 2003-2006 uim Project http://uim.freedesktop.org/
4
4
 
5
5
  All rights reserved.
6
6
 
31
31
 
32
32
*/
33
33
 
34
 
#include "config.h"
 
34
#include <config.h>
 
35
 
35
36
#include <stdlib.h>
36
37
#include <string.h>
 
38
#include <ctype.h>
37
39
#include <m17n.h>
 
40
#include "gettext.h"
38
41
#include "uim-scm.h"
39
42
#include "uim-util.h"
40
43
#include "plugin.h"
41
44
 
42
45
static int m17nlib_ok;
43
46
static MConverter *converter;
44
 
static char buffer_for_converter[1024]; /* Currently, if preedit strings or
 
47
static char buffer_for_converter[4096]; /* Currently, if preedit strings or
45
48
                                           candidate strings over this buffer
46
49
                                           size, they will simply ignore. */
47
50
 
52
55
  MInputMethod *im;
53
56
} *im_array;
54
57
 
55
 
static int max_input_contexts;
 
58
static int nr_input_contexts;
56
59
static struct ic_ {
57
60
  MInputContext *mic;
58
61
  char **old_candidates; /* FIXME: ugly hack for perfomance... */
60
63
  int  nr_candidates;
61
64
} *ic_array;
62
65
 
 
66
/* Utility function */
63
67
static char *
64
 
m17nlib_utf8_find_next_char(const char *p);
 
68
m17nlib_utf8_find_next_char(const char *p)
 
69
{
 
70
  if (*p) {
 
71
    for (++p; (*p & 0xc0) == 0x80; ++p)
 
72
      ;
 
73
  }
 
74
  return (char *)p;
 
75
}
65
76
 
66
77
static int
67
78
unused_ic_id(void)
68
79
{
69
80
  int i;
70
 
  for (i = 0; i < max_input_contexts; i++) {
71
 
    if (!ic_array[i].mic) {
 
81
 
 
82
  for (i = 0; i < nr_input_contexts; i++) {
 
83
    if (!ic_array[i].mic)
72
84
      return i;
73
 
    }
74
85
  }
75
 
  ic_array = realloc(ic_array, sizeof(struct ic_) * (max_input_contexts+1));
76
 
  ic_array[max_input_contexts].mic = NULL;
77
 
  max_input_contexts++;
78
 
  return max_input_contexts - 1;
 
86
 
 
87
  ic_array = realloc(ic_array, sizeof(struct ic_) * (nr_input_contexts + 1));
 
88
  ic_array[nr_input_contexts].mic = NULL;
 
89
  nr_input_contexts++;
 
90
 
 
91
  return nr_input_contexts - 1;
79
92
}
80
93
 
81
94
static void
82
 
pushback_input_method(MInputMethod *im,
83
 
                      char *lib_lang, char *name)
 
95
pushback_input_method(MInputMethod *im, char *lib_lang, char *name)
84
96
{
85
 
  char *lang = uim_get_language_code_from_language_name(lib_lang);
86
 
 
87
 
  im_array = realloc(im_array, 
88
 
                     sizeof(struct im_) * (nr_input_methods + 1));
89
 
 
90
 
  if (lang != NULL) {
91
 
    im_array[nr_input_methods].lang = strdup(lang);
92
 
  } else {
93
 
    im_array[nr_input_methods].lang = NULL;
94
 
  }
 
97
  const char *lang;
 
98
  
 
99
  if (!strcmp(lib_lang, "t"))
 
100
    lang = "";
 
101
  else
 
102
    lang = lib_lang;
 
103
 
 
104
  im_array = realloc(im_array, sizeof(struct im_) * (nr_input_methods + 1));
 
105
  im_array[nr_input_methods].im = im;
95
106
  im_array[nr_input_methods].name = strdup(name);
96
 
  im_array[nr_input_methods].im = im;
 
107
  im_array[nr_input_methods].lang = strdup(lang);
 
108
 
97
109
  nr_input_methods++;
98
110
}
99
111
 
 
112
#if 0
100
113
static void
101
114
preedit_start_cb(MInputContext *ic, MSymbol command)
102
115
{
165
178
  mplist_add(minput_default_driver.callback_list, Minput_candidates_draw,  (void *)candidates_draw_cb);
166
179
  mplist_add(minput_default_driver.callback_list, Minput_candidates_done,  (void *)candidates_done_cb);*/
167
180
}
 
181
#endif
168
182
 
169
183
static uim_lisp
170
184
init_m17nlib()
171
185
{
172
186
  MPlist *imlist, *elm;
173
 
  MSymbol utf8 = msymbol("utf8");
 
187
 
174
188
  M17N_INIT();
175
189
  nr_input_methods = 0;
 
190
  nr_input_contexts = 0;
176
191
  im_array = NULL;
177
 
  max_input_contexts = 0;
178
192
  ic_array = NULL;
179
193
 
180
194
  imlist = mdatabase_list(msymbol("input-method"), Mnil, Mnil, Mnil);
 
195
 
181
196
  if (!imlist) {
182
197
    /* maybe user forgot to install m17n-db */
183
198
    return uim_scm_f();
184
199
  }
 
200
 
185
201
  for (elm = imlist; mplist_key(elm) != Mnil; elm = mplist_next(elm)) {
186
202
    MDatabase *mdb = mplist_value(elm);
187
 
    MSymbol *tag = mdatabase_tag(mdb);
 
203
    MSymbol *tag = mdatabase_tag(mdb); /* tag[1]: lang, tag[2]: name */
 
204
 
188
205
    if (tag[1] != Mnil) {
189
206
      MInputMethod *im = minput_open_im(tag[1], tag[2], NULL);
190
 
      if (im) {
191
 
        MSymbol lang = msymbol_get(im->language, Mlanguage);
192
 
        pushback_input_method(im, msymbol_name(lang),
 
207
 
 
208
      if (im)
 
209
        pushback_input_method(im, msymbol_name(im->language),
193
210
                              msymbol_name(im->name));
194
 
        
195
 
      }
196
211
    }
197
212
  }
198
 
  /*  register_callbacks();*/
 
213
#if 0
 
214
  register_callbacks();
 
215
#endif
199
216
  m17n_object_unref(imlist);
200
 
  converter = mconv_buffer_converter(utf8, NULL, 0);
201
 
  if (!converter) {
 
217
  converter = mconv_buffer_converter(msymbol("utf8"), NULL, 0);
 
218
 
 
219
  if (!converter)
202
220
    return uim_scm_f();
203
 
  }
 
221
 
204
222
  m17nlib_ok = 1;
 
223
 
205
224
  return uim_scm_t();
206
225
}
207
226
 
212
231
                      sizeof(buffer_for_converter));
213
232
  mconv_encode(converter, mtext);
214
233
  buffer_for_converter[converter->nbytes] = 0;
 
234
 
215
235
  return strdup(buffer_for_converter);
216
236
}
217
237
 
218
238
static uim_lisp
219
239
compose_modep(uim_lisp id_)
220
240
{
221
 
  int id = uim_scm_c_int(id_);
222
 
  /* range check of id might need. */
223
 
  MInputContext *ic = ic_array[id].mic;
224
 
  if (!ic) {
225
 
    return uim_scm_f();
226
 
  }
227
 
  if (ic->candidate_from == ic->candidate_to ||
228
 
     ic->candidate_from > ic->candidate_to) {
229
 
    return uim_scm_f();
230
 
  } else {
 
241
  int id;
 
242
  MInputContext *ic;
 
243
 
 
244
  id = uim_scm_c_int(id_);
 
245
  ic = ic_array[id].mic;
 
246
 
 
247
  if (!ic)
 
248
    return uim_scm_f();
 
249
 
 
250
  if (ic->candidate_from == ic->candidate_to
 
251
      || ic->candidate_from > ic->candidate_to)
 
252
    return uim_scm_f();
 
253
  else
231
254
    return uim_scm_t();
232
 
  }
233
255
}
234
256
 
235
257
static uim_lisp
236
258
preedit_changedp(uim_lisp id_)
237
259
{
238
 
  int id = uim_scm_c_int(id_);
239
 
  /* range check of id might need. */
240
 
  MInputContext *ic = ic_array[id].mic;
241
 
  if (!ic) {
 
260
  int id;
 
261
  MInputContext *ic;
 
262
 
 
263
  id = uim_scm_c_int(id_);
 
264
  ic = ic_array[id].mic;
 
265
 
 
266
  if (!ic)
242
267
    return uim_scm_f();
243
 
  }
244
 
  if (ic->preedit_changed == 1) {
 
268
 
 
269
  if (ic->preedit_changed == 1)
245
270
    return uim_scm_t();
246
 
  } else {
 
271
  else
247
272
    return uim_scm_f();
248
 
  }
249
273
}
250
274
 
251
275
static uim_lisp
252
276
get_left_of_cursor(uim_lisp id_)
253
277
{
254
 
  int id = uim_scm_c_int(id_);
255
 
  int buflen;
256
 
  int i;
 
278
  int id, buflen, i;
257
279
  uim_lisp buf_;
258
 
  char *buf;
259
 
  char *p;
260
 
  MInputContext *ic = ic_array[id].mic;
261
 
  if (!ic) {
262
 
    return uim_scm_make_str("");
263
 
  }
264
 
  if (ic->cursor_pos == 0) {
265
 
    return uim_scm_make_str("");
266
 
  }
 
280
  char *buf, *p;
 
281
  MInputContext *ic;
 
282
 
 
283
  id = uim_scm_c_int(id_);
 
284
  ic = ic_array[id].mic;
 
285
 
 
286
  if (!ic)
 
287
    return uim_scm_make_str("");
 
288
 
 
289
  if (ic->cursor_pos == 0)
 
290
    return uim_scm_make_str("");
 
291
 
267
292
  buf = convert_mtext2str(ic->preedit);
268
 
  p = (char *)buf;
 
293
  p = buf;
269
294
 
270
 
  for (i=0; i<ic->cursor_pos ;i++) {
 
295
  for (i = 0; i < ic->cursor_pos ;i++)
271
296
    p = m17nlib_utf8_find_next_char(p);
272
 
  }
273
 
  *p = 0;
274
 
 
275
 
  buflen = strlen((char *)buf);
276
 
  buf_ = uim_scm_make_str((char *)buf);
 
297
  *p = '\0';
 
298
 
 
299
  buflen = strlen(buf);
 
300
  buf_ = uim_scm_make_str(buf);
 
301
  free(buf);
 
302
 
277
303
  return buf_;
278
304
}
279
305
 
280
306
static uim_lisp
281
307
get_right_of_cursor(uim_lisp id_)
282
308
{
283
 
  int id = uim_scm_c_int(id_);
284
 
  int buflen;
285
 
  int i;
 
309
  int id, buflen, i;
286
310
  uim_lisp buf_;
287
 
  char *buf;
288
 
  char *p;
289
 
  MInputContext *ic = ic_array[id].mic;
290
 
  if (!ic) {
 
311
  char *buf, *p;
 
312
  MInputContext *ic;
 
313
 
 
314
  id = uim_scm_c_int(id_);
 
315
  ic = ic_array[id].mic;
 
316
 
 
317
  if (!ic)
291
318
    return uim_scm_make_str("");
292
 
  }
293
319
 
294
320
  buf = convert_mtext2str(ic->preedit);
295
321
  p = buf;
296
322
 
297
 
  for (i=0; i<ic->cursor_pos ;i++) {
 
323
  for (i = 0; i < ic->cursor_pos ;i++)
298
324
    p = m17nlib_utf8_find_next_char(p);
299
 
  }
300
 
  buflen = strlen((char *)p);
301
 
  buf_ = uim_scm_make_str((char *)p);
 
325
 
 
326
  buflen = strlen(p);
 
327
  buf_ = uim_scm_make_str(p);
 
328
  free(buf);
 
329
 
302
330
  return buf_;
303
331
}
304
332
 
305
333
static uim_lisp
306
334
get_left_of_candidate(uim_lisp id_)
307
335
{
308
 
  int id = uim_scm_c_int(id_);
309
 
  int buflen;
310
 
  int i;
 
336
  int id, buflen, i;
311
337
  uim_lisp buf_;
312
 
  char *buf;
313
 
  char *p;
314
 
  MInputContext *ic = ic_array[id].mic;
315
 
  if (!ic) {
316
 
    return uim_scm_make_str("");
317
 
  }
318
 
  if (ic->candidate_from == 0) {
319
 
    return uim_scm_make_str("");
320
 
  }
 
338
  char *buf, *p;
 
339
  MInputContext *ic;
 
340
 
 
341
  id = uim_scm_c_int(id_);
 
342
  ic = ic_array[id].mic;
 
343
 
 
344
  if (!ic)
 
345
    return uim_scm_make_str("");
 
346
 
 
347
  if (ic->candidate_from == 0)
 
348
    return uim_scm_make_str("");
 
349
 
321
350
  buf = convert_mtext2str(ic->preedit);
322
351
  p = buf;
323
352
 
324
 
  for (i=0; i<ic->candidate_from ;i++) {
 
353
  for (i = 0; i < ic->candidate_from ;i++)
325
354
    p = m17nlib_utf8_find_next_char(p);
326
 
  }
327
 
  *p = 0;
328
 
  buflen = strlen((char *)buf);
329
 
  buf_ = uim_scm_make_str((char *)buf);
 
355
  *p = '\0';
 
356
 
 
357
  buflen = strlen(buf);
 
358
  buf_ = uim_scm_make_str(buf);
330
359
  free(buf);
 
360
 
331
361
  return buf_;
332
362
}
333
363
 
334
364
static uim_lisp
335
365
get_selected_candidate(uim_lisp id_)
336
366
{
337
 
  int id = uim_scm_c_int(id_);
338
 
  int buflen;
339
 
  int i;
 
367
  int id, buflen, i;
340
368
  uim_lisp buf_;
341
 
  char *buf;
342
 
  char *p;
343
 
  char *start;
344
 
  MInputContext *ic = ic_array[id].mic;
345
 
  if (!ic) {
 
369
  char *buf, *p, *start;
 
370
  MInputContext *ic;
 
371
 
 
372
  id = uim_scm_c_int(id_);
 
373
  ic = ic_array[id].mic;
 
374
 
 
375
  if (!ic)
346
376
    return uim_scm_make_str("");
347
 
  }
 
377
 
348
378
  buf = convert_mtext2str(ic->preedit);
349
379
  p = buf;
350
380
 
351
 
  if (!p) {
 
381
  if (!p)
352
382
    return uim_scm_make_str("");
353
 
  }
354
383
 
355
 
  for (i=0; i<ic->candidate_from ;i++) {
 
384
  for (i = 0; i < ic->candidate_from ;i++)
356
385
    p = m17nlib_utf8_find_next_char(p);
357
 
  }
358
386
  start = p;
359
387
 
360
 
  for (i=0; i<ic->candidate_to - ic->candidate_from ;i++) {
 
388
  for (i = 0; i < ic->candidate_to - ic->candidate_from ;i++)
361
389
    p = m17nlib_utf8_find_next_char(p);
362
 
  }
363
 
  *p = 0;
 
390
  *p = '\0';
364
391
 
365
392
  buflen = strlen(start);
366
393
  buf_ = uim_scm_make_str(start);
367
394
  free(buf);
 
395
 
368
396
  return buf_;
369
397
}
370
398
 
371
399
static uim_lisp
372
400
get_right_of_candidate(uim_lisp id_)
373
401
{
374
 
  int id = uim_scm_c_int(id_);
375
 
  int buflen;
376
 
  int i;
 
402
  int id, buflen, i;
377
403
  uim_lisp buf_;
378
 
  char *buf;
379
 
  char *p;
380
 
  MInputContext *ic = ic_array[id].mic;
381
 
  if (!ic) {
 
404
  char *buf, *p;
 
405
  MInputContext *ic;
 
406
 
 
407
  id = uim_scm_c_int(id_);
 
408
  ic = ic_array[id].mic;
 
409
 
 
410
  if (!ic)
382
411
    return uim_scm_make_str("");
383
 
  }
 
412
 
384
413
  buf = convert_mtext2str(ic->preedit);
385
414
  p = buf;
386
415
 
387
 
  for (i=0; i<ic->candidate_to ;i++) {
 
416
  for (i = 0; i < ic->candidate_to ;i++)
388
417
    p = m17nlib_utf8_find_next_char(p);
389
 
  }
 
418
 
390
419
  buflen = strlen(p);
391
420
  buf_ = uim_scm_make_str(p);
392
421
  free(buf);
 
422
 
393
423
  return buf_;
394
424
}
395
425
 
403
433
get_input_method_name(uim_lisp nth_)
404
434
{
405
435
  int nth = uim_scm_c_int(nth_);
 
436
 
406
437
  if (nth < nr_input_methods) {
407
438
    char *name = alloca(strlen(im_array[nth].name) + 20);
408
439
 
409
 
    if (im_array[nth].lang != NULL)
 
440
    if (!strcmp(im_array[nth].lang, ""))
 
441
      sprintf(name, "m17n-%s", im_array[nth].name);
 
442
    else
410
443
      sprintf(name, "m17n-%s-%s", im_array[nth].lang, im_array[nth].name);
411
 
    else
412
 
      sprintf(name, "m17n-%s", im_array[nth].name);
413
444
 
414
445
    return uim_scm_make_str(name);
415
446
  }
 
447
 
416
448
  return uim_scm_f();
417
449
}
418
450
 
420
452
get_input_method_lang(uim_lisp nth_)
421
453
{
422
454
  int nth = uim_scm_c_int(nth_);
 
455
 
 
456
  if (nth < nr_input_methods)
 
457
    return uim_scm_make_str(im_array[nth].lang);
 
458
 
 
459
  return uim_scm_f();
 
460
}
 
461
 
 
462
static uim_lisp
 
463
get_input_method_short_desc(uim_lisp nth_)
 
464
{
 
465
  int nth;
 
466
  char *str = NULL, *p;
 
467
  uim_lisp ret;
 
468
 
 
469
  nth = uim_scm_c_int(nth_);
 
470
 
423
471
  if (nth < nr_input_methods) {
424
 
    char *lang = im_array[nth].lang;
425
 
    if (lang != NULL) {
426
 
      return uim_scm_make_str(lang);
 
472
    MInputMethod *im;
 
473
    MText *desc;
 
474
 
 
475
    im = im_array[nth].im;
 
476
    desc = minput_get_description(im->language, im->name);
 
477
    if (desc) {
 
478
      int i, len;
 
479
 
 
480
      str = convert_mtext2str(desc);
 
481
      p = strchr(str, '.');
 
482
      if (p)
 
483
        *p = '\0';
 
484
      len = strlen(str);
 
485
 
 
486
      /*
 
487
       * Workaround for the descriptions which lack period.
 
488
       * Also we avoid the description with non English words.
 
489
       * See https://bugs.freedesktop.org/show_bug.cgi?id=6972
 
490
       */
 
491
      for (i = 0; i < len; i++) {
 
492
        if (str[i] == '\n') {
 
493
          str[i] = '\0';
 
494
          break;
 
495
        }
 
496
#ifdef HAVE_ISASCII
 
497
        else if (!isascii((int)str[i])) {
 
498
#else
 
499
        else if ((int)str[i] & ~0x7f) {
 
500
#endif
 
501
          free(str);
 
502
          str = NULL;
 
503
          break;
 
504
        }
 
505
      }
 
506
      m17n_object_unref(desc);
 
507
    }
 
508
 
 
509
    if (str) {
 
510
      ret = uim_scm_make_str(str);
 
511
      free(str);
427
512
    } else {
428
 
      return uim_scm_make_str("unknown");
 
513
      ret = uim_scm_make_str(N_("An input method provided by the m17n library"));
429
514
    }
430
 
  }
431
 
  return uim_scm_f();
 
515
  } else
 
516
    ret = uim_scm_f();
 
517
 
 
518
  return ret;
432
519
}
433
520
 
434
521
static MInputMethod *
435
 
find_im_by_name(char *name)
 
522
find_im_by_name(const char *name)
436
523
{
437
524
  int i;
438
 
  if (strncmp(name, "m17n-", 5) != 0) {
 
525
  const char *im_name;
 
526
 
 
527
  if (strncmp(name, "m17n-", 5) != 0)
439
528
    return NULL;
440
 
  }
441
 
  name = &name[5];
 
529
 
 
530
  im_name = &name[5];
 
531
 
442
532
  for (i = 0; i < nr_input_methods; i++) {
443
533
    char buf[100];
444
 
    if (im_array[i].lang == NULL) {
 
534
 
 
535
    if (!strcmp(im_array[i].lang, ""))
445
536
      snprintf(buf, 100, "%s", im_array[i].name);
446
 
    } else {
 
537
    else
447
538
      snprintf(buf, 100, "%s-%s", im_array[i].lang, im_array[i].name);
448
 
    }
449
 
    if (!strcmp(name, buf)) {
 
539
 
 
540
    if (!strcmp(im_name, buf))
450
541
      return im_array[i].im;
451
 
    }
452
542
  }
 
543
 
453
544
  return NULL;
454
545
}
455
546
 
456
547
static uim_lisp
457
548
alloc_id(uim_lisp name_)
458
549
{
459
 
  int id = unused_ic_id();
460
 
  char *name = uim_scm_c_str(name_);
461
 
  MInputMethod *im = find_im_by_name(name);
462
 
  if (im) {
 
550
  int id;
 
551
  const char *name;
 
552
  MInputMethod *im;
 
553
 
 
554
  id = unused_ic_id();
 
555
  name = uim_scm_refer_c_str(name_);
 
556
 
 
557
  im = find_im_by_name(name);
 
558
 
 
559
  if (im)
463
560
    ic_array[id].mic = minput_create_ic(im, NULL);
464
 
  }
 
561
 
465
562
  ic_array[id].old_candidates = NULL;
466
563
  ic_array[id].new_candidates = NULL;
467
 
  free(name);
 
564
 
468
565
  return uim_scm_make_int(id);
469
566
}
470
567
 
472
569
free_id(uim_lisp id_)
473
570
{
474
571
  int id = uim_scm_c_int(id_);
475
 
  if (id < max_input_contexts) {
 
572
 
 
573
  if (id < nr_input_contexts) {
476
574
    struct ic_ *ic = &ic_array[id];
 
575
 
477
576
    if (ic->mic) {
478
577
      minput_destroy_ic(ic->mic);
479
578
      ic->mic = NULL;
480
579
    }
481
580
  }
 
581
 
482
582
  return uim_scm_f();
483
583
}
484
584
 
485
585
static uim_lisp
486
586
push_symbol_key(uim_lisp id_, uim_lisp key_)
487
587
{
488
 
  int id = uim_scm_c_int(id_);
 
588
  int id;
489
589
  MSymbol key;
490
 
  MInputContext *ic = ic_array[id].mic;
 
590
  MInputContext *ic;
 
591
 
 
592
  id = uim_scm_c_int(id_);
 
593
  ic = ic_array[id].mic;
491
594
  key = msymbol(uim_scm_c_str(key_));
492
 
  if (key == Mnil) {
493
 
    return uim_scm_t();
494
 
  }
495
 
 
496
 
  if(minput_filter(ic, key, NULL)== 1) {
497
 
    return uim_scm_t();
498
 
  } else {
 
595
 
 
596
  if (key == Mnil)
 
597
    return uim_scm_t();
 
598
 
 
599
  if (minput_filter(ic, key, NULL) == 1)
 
600
    return uim_scm_t();
 
601
  else
499
602
    return uim_scm_f();
500
 
  }
501
603
}
502
604
 
503
605
static uim_lisp
505
607
{
506
608
  MText *produced;
507
609
  char *commit_string;
508
 
  int consumed;
509
 
  int id = uim_scm_c_int(id_);
510
 
  MInputContext *ic = ic_array[id].mic;
 
610
  int consumed, id;
 
611
  MInputContext *ic;
511
612
  uim_lisp  consumed_, commit_string_;
512
613
 
 
614
  id = uim_scm_c_int(id_);
 
615
  ic = ic_array[id].mic;
 
616
 
513
617
  produced  = mtext();
514
 
 
515
618
  consumed  = minput_lookup(ic, NULL, NULL, produced);
516
619
 
517
 
  if (consumed == -1) {
 
620
  if (consumed == -1)
518
621
    consumed_ = uim_scm_f();
519
 
  } else {
 
622
  else
520
623
    consumed_ = uim_scm_t();
521
 
  }
522
624
 
523
625
  commit_string = convert_mtext2str(produced);
524
626
  m17n_object_unref(produced);
531
633
static uim_lisp
532
634
commit(uim_lisp id_)
533
635
{
534
 
  int id = uim_scm_c_int(id_);
535
 
  MInputContext *ic = ic_array[id].mic;
536
 
 
537
 
/* To avoid a bug of m17n-lib */
538
 
  ic->candidate_to   = 0;
 
636
  int id;
 
637
  MInputContext *ic;
 
638
 
 
639
  id = uim_scm_c_int(id_);
 
640
  ic = ic_array[id].mic;
 
641
 
 
642
  /* To avoid a bug of m17n-lib */
 
643
  ic->candidate_to = 0;
 
644
 
539
645
  return uim_scm_f();
540
646
}
541
647
 
542
648
static uim_lisp
543
649
candidate_showp(uim_lisp id_)
544
650
{
545
 
  int id = uim_scm_c_int(id_);
546
 
  MInputContext *ic = ic_array[id].mic;
547
 
 
548
 
  if (ic->candidate_show == 1) {
 
651
  int id;
 
652
  MInputContext *ic;
 
653
 
 
654
  id = uim_scm_c_int(id_);
 
655
  ic = ic_array[id].mic;
 
656
 
 
657
  if (ic->candidate_show == 1)
549
658
    return uim_scm_t();
550
 
  }
 
659
 
551
660
  return uim_scm_f();
552
661
}
553
662
 
556
665
{
557
666
  int result = 0;
558
667
  MPlist *group; 
559
 
  MInputContext *ic = ic_array[id].mic;
 
668
  MInputContext *ic;
 
669
 
 
670
  ic = ic_array[id].mic;
560
671
 
561
672
  if (!ic || !ic->candidate_list)
562
673
    return 0;
565
676
 
566
677
  while (mplist_value(group) != Mnil) {
567
678
    if (mplist_key(group) == Mtext) {
568
 
      for (; mplist_key(group) != Mnil; group = mplist_next(group)) {
569
 
        result += mtext_len(mplist_value(group));
570
 
      }
 
679
      for (; mplist_key(group) != Mnil; group = mplist_next(group))
 
680
        result += mtext_len(mplist_value(group));
571
681
    } else {
572
 
      for (; mplist_key(group) != Mnil; group = mplist_next(group)) {
573
 
        result += mplist_length(mplist_value(group));
574
 
      }
 
682
      for (; mplist_key(group) != Mnil; group = mplist_next(group))
 
683
        result += mplist_length(mplist_value(group));
575
684
    }
576
685
  }
 
686
 
577
687
  return result;
578
688
}
579
689
 
581
691
old_cands_free(char **old_cands)
582
692
{
583
693
  int i = 0;
 
694
 
584
695
  if (old_cands) {
585
 
    for (i=0; old_cands[i]; i++) {
 
696
    for (i = 0; old_cands[i]; i++)
586
697
      free(old_cands[i]);
587
 
    }
588
698
    free(old_cands);
589
699
  }
590
700
}
592
702
static uim_lisp
593
703
fill_new_candidates(uim_lisp id_)
594
704
{
595
 
  MText *produced = NULL; /* Quiet gcc */
596
 
  MPlist *group;
597
 
  MPlist *elm;
598
 
  int i;
599
 
  char *buf = NULL; /* Quiet gcc */
600
 
  int id = uim_scm_c_int(id_);
601
 
  MInputContext *ic = ic_array[id].mic;
602
 
  int cands_num = calc_cands_num(id);
 
705
  MText *produced;
 
706
  MPlist *group, *elm;
 
707
  int i, id, cands_num;
603
708
  char **new_cands;
604
 
  uim_lisp buf_;
 
709
  MInputContext *ic;
 
710
 
 
711
  id = uim_scm_c_int(id_);
 
712
  ic = ic_array[id].mic;
 
713
  cands_num = calc_cands_num(id);
605
714
 
606
715
  if (!ic || !ic->candidate_list)
607
716
    return uim_scm_f();
614
723
  new_cands = malloc (cands_num * sizeof(char *) + 2);
615
724
 
616
725
  if (mplist_key(group) == Mtext) {
617
 
 
618
 
    for (i=0; mplist_key(group) != Mnil; group = mplist_next(group)) {
 
726
    for (i = 0; mplist_key(group) != Mnil; group = mplist_next(group)) {
619
727
      int j;
620
 
      for (j=0; j < mtext_len(mplist_value(group)); j++, i++) {
621
 
          produced = mtext();
622
 
          mtext_cat_char(produced, mtext_ref_char(mplist_value(group), j));
623
 
          new_cands[i] = convert_mtext2str(produced);
624
 
          m17n_object_unref(produced);
 
728
      for (j = 0; j < mtext_len(mplist_value(group)); j++, i++) {
 
729
          produced = mtext();
 
730
          mtext_cat_char(produced, mtext_ref_char(mplist_value(group), j));
 
731
          new_cands[i] = convert_mtext2str(produced);
 
732
          m17n_object_unref(produced);
625
733
      }
626
734
    }
627
735
  } else {
628
 
    for (i=0; mplist_key(group) != Mnil; group = mplist_next(group)) {
 
736
    for (i = 0; mplist_key(group) != Mnil; group = mplist_next(group)) {
629
737
 
630
 
      for (elm = mplist_value(group); mplist_key(elm) != Mnil; elm = mplist_next(elm),i++) {
 
738
      for (elm = mplist_value(group); mplist_key(elm) != Mnil;
 
739
           elm = mplist_next(elm),i++) {
631
740
        produced = mplist_value(elm);
632
741
        new_cands[i] = convert_mtext2str(produced);
633
742
      }
639
748
  ic_array[id].nr_candidates = i;
640
749
  
641
750
  return uim_scm_t();
642
 
 
643
 
  if (!buf) {
644
 
    return uim_scm_make_str("");
645
 
  } else {
646
 
    buf_ = uim_scm_make_str(buf);
647
 
    free(buf);
648
 
    return buf_;
649
 
  }
650
751
}
651
752
 
652
753
static uim_bool
653
 
same_candidatesp(const char **old, const char **new)
 
754
same_candidatesp(char **old, char **new)
654
755
{
655
756
  int i;
 
757
 
656
758
  if (!old)
657
759
    return UIM_FALSE;
658
760
 
659
 
  for (i=0; old[i] && new[i]; i++) {
660
 
    if (strcmp(old[i], new[i]) != 0) {
 
761
  for (i = 0; old[i] && new[i]; i++) {
 
762
    if (strcmp(old[i], new[i]) != 0)
661
763
      return UIM_FALSE;
662
 
    }
663
764
  }
 
765
 
664
766
  return UIM_TRUE;
665
767
}
666
768
 
668
770
candidates_changedp(uim_lisp id_)
669
771
{
670
772
  int id = uim_scm_c_int(id_);
671
 
  char **old_cands = ic_array[id].old_candidates;
672
 
  char **new_cands = ic_array[id].new_candidates;
673
773
 
674
 
  if (!same_candidatesp(old_cands, new_cands)) {
 
774
  if (!same_candidatesp(ic_array[id].old_candidates,
 
775
                        ic_array[id].new_candidates))
675
776
    return uim_scm_t();
676
 
  }
 
777
 
677
778
  return uim_scm_f();
678
779
}
679
780
 
681
782
get_nr_candidates(uim_lisp id_)
682
783
{
683
784
  int id = uim_scm_c_int(id_);
 
785
 
684
786
  return uim_scm_make_int(calc_cands_num(id));
685
787
}
686
788
 
687
789
static uim_lisp
688
790
get_nth_candidate(uim_lisp id_, uim_lisp nth_)
689
791
{
690
 
  int id = uim_scm_c_int(id_);
691
 
  int nth = uim_scm_c_int(nth_);
692
 
  int nr = ic_array[id].nr_candidates;
 
792
  int id, nth, nr;
693
793
  
694
 
  if (nr >= nth) {
 
794
  id = uim_scm_c_int(id_);
 
795
  nth = uim_scm_c_int(nth_);
 
796
  nr = ic_array[id].nr_candidates;
 
797
 
 
798
  if (nr >= nth)
695
799
    return uim_scm_make_str(ic_array[id].new_candidates[nth]);
696
 
  } else {
 
800
  else
697
801
    return uim_scm_make_str("");
698
 
  }
699
802
}
700
803
 
701
804
static uim_lisp
702
805
get_candidate_index(uim_lisp id_)
703
806
{
704
 
  int id = uim_scm_c_int(id_);
705
 
  MInputContext *ic = ic_array[id].mic;
 
807
  int id;
 
808
  MInputContext *ic;
 
809
 
 
810
  id = uim_scm_c_int(id_);
 
811
  ic = ic_array[id].mic;
 
812
 
706
813
  return uim_scm_make_int(ic->candidate_index);
707
814
}
708
815
 
709
 
/* Utility function */
710
 
static char *
711
 
m17nlib_utf8_find_next_char(const char *p)
712
 
{
713
 
  if (*p) {
714
 
    for (++p; (*p & 0xc0) == 0x80; ++p)
715
 
      ;
716
 
  }
717
 
  return (char *)p;
718
 
}
719
 
 
720
816
void
721
817
uim_plugin_instance_init(void)
722
818
{
723
819
  uim_scm_init_subr_0("m17nlib-lib-init", init_m17nlib);
724
 
  uim_scm_init_subr_0("m17nlib-lib-nr-input-methods", get_nr_input_methods);
725
 
  uim_scm_init_subr_1("m17nlib-lib-nth-input-method-lang", get_input_method_lang);
726
 
  uim_scm_init_subr_1("m17nlib-lib-nth-input-method-name", get_input_method_name);
 
820
  uim_scm_init_subr_0("m17nlib-lib-nr-input-methods",
 
821
                      get_nr_input_methods);
 
822
  uim_scm_init_subr_1("m17nlib-lib-nth-input-method-lang",
 
823
                      get_input_method_lang);
 
824
  uim_scm_init_subr_1("m17nlib-lib-nth-input-method-name",
 
825
                      get_input_method_name);
 
826
  uim_scm_init_subr_1("m17nlib-lib-nth-input-method-short-desc",
 
827
                      get_input_method_short_desc);
727
828
  uim_scm_init_subr_1("m17nlib-lib-alloc-context", alloc_id);
728
829
  uim_scm_init_subr_1("m17nlib-lib-free-context", free_id);
729
830
  uim_scm_init_subr_2("m17nlib-lib-push-symbol-key", push_symbol_key);
730
831
  uim_scm_init_subr_1("m17nlib-lib-compose-mode?", compose_modep);
731
832
  uim_scm_init_subr_1("m17nlib-lib-preedit-changed?", preedit_changedp);
732
 
  uim_scm_init_subr_1("m17nlib-lib-get-left-of-cursor",     get_left_of_cursor);
733
 
  uim_scm_init_subr_1("m17nlib-lib-get-right-of-cursor",    get_right_of_cursor);
734
 
  uim_scm_init_subr_1("m17nlib-lib-get-left-of-candidate",  get_left_of_candidate);
735
 
  uim_scm_init_subr_1("m17nlib-lib-get-selected-candidate", get_selected_candidate);
736
 
  uim_scm_init_subr_1("m17nlib-lib-get-right-of-candidate", get_right_of_candidate);
 
833
  uim_scm_init_subr_1("m17nlib-lib-get-left-of-cursor", get_left_of_cursor);
 
834
  uim_scm_init_subr_1("m17nlib-lib-get-right-of-cursor", get_right_of_cursor);
 
835
  uim_scm_init_subr_1("m17nlib-lib-get-left-of-candidate",
 
836
                      get_left_of_candidate);
 
837
  uim_scm_init_subr_1("m17nlib-lib-get-selected-candidate",
 
838
                      get_selected_candidate);
 
839
  uim_scm_init_subr_1("m17nlib-lib-get-right-of-candidate",
 
840
                      get_right_of_candidate);
737
841
  uim_scm_init_subr_1("m17nlib-lib-get-result", get_result);
738
842
  uim_scm_init_subr_1("m17nlib-lib-commit", commit);
739
843
  uim_scm_init_subr_1("m17nlib-lib-candidate-show?", candidate_showp);