;;
;;

(define m17nlib-context-id
  (lambda (mc)
    (car (nthcdr 0 mc))))

(define m17nlib-context-set-id!
  (lambda (mc id)
    (set-car! (nthcdr 0 mc) id)))

(define m17nlib-context-new
  (lambda (name)
    (let ((c ()))
      (set! c (copy-list '(())))
      (m17nlib-context-set-id! c
			       (m17nlib-lib-alloc-context name))
      c)))

(define m17nlib-init-handler
  (lambda (id arg)
    (let* ((c (find-context id)))
      (set-context-data! c (m17nlib-context-new arg)))))

(define m17nlib-release-handler
  (lambda (id)))

(define m17nlib-mode-handler
  (lambda (id mode)))

(define m17nlib-press-key-handler
  (lambda (id key key-state)
    (let* ((c (find-context id))
	   (mc (context-data c))
	   (mid (m17nlib-context-id mc)))
      (if
       (m17nlib-lib-push-key mid key key-state)
       (im-commit-raw id))
      (let ((str (m17nlib-lib-get-str mid)))
	(if str
	    (im-commit id str))))))

(define m17nlib-release-key-handler
  (lambda (id key key-state)))

(define m17nlib-reset-handler
  (lambda (id)
    ()))

(define m17nlib-get-candidate-handler
  (lambda (id idx accel-enum-hint)))

(define m17nlib-set-candidate-index-handler
  (lambda (id idx)))

(define m17nlib-prop-handler
  (lambda (id message)))

(define m17nlib-register
  (lambda (i nr-im)
    (if (> nr-im i)
	(begin
	  (register-im
	   (m17nlib-lib-nth-input-method-name i)
	   (m17nlib-lib-nth-input-method-lang i)
	   "UTF-8"
	   (m17nlib-lib-nth-input-method-name i)
	   m17nlib-init-handler
	   m17nlib-release-handler
	   m17nlib-mode-handler
	   m17nlib-press-key-handler
	   m17nlib-release-key-handler
	   m17nlib-reset-handler
	   m17nlib-get-candidate-handler
	   m17nlib-set-candidate-index-handler
	   m17nlib-prop-handler
	   )
	  (m17nlib-register (+ i 1) nr-im))
	())))

(m17nlib-lib-init)
(m17nlib-register 0 (m17nlib-lib-nr-input-methods))
