update.
[chise/xemacs-chise.git.1] / src / concord.c
index eb9f1b9..7b1b9cc 100644 (file)
@@ -1,5 +1,5 @@
 /* XEmacs routines to deal with CONCORD.
-   Copyright (C) 2005,2006 MORIOKA Tomohiko
+   Copyright (C) 2005,2006,2008,2010 MORIOKA Tomohiko
 
 This file is part of XEmacs.
 
@@ -27,36 +27,34 @@ Boston, MA 02111-1307, USA.  */
 #include "sysfile.h"
 #include "buffer.h"
 #include <errno.h>
-#include <concord.h>
+#include "elconcord.h"
+#ifdef HAVE_LIBCHISE
+#  include <chise.h>
+#endif
 
 
 EXFUN (Fread_from_string, 3);
 
 
 Lisp_Object Qconcord;
-
+Lisp_Object Qconcord_object;
+Lisp_Object Qgenre, Q_id;
+#ifdef HAVE_LIBCHISE
+Lisp_Object Qcharacter;
+Lisp_Object Qfeature;
+#endif
 
 Lisp_Object Vconcord_ds_hash_table;
 Lisp_Object Vconcord_genre_hash_table;
+Lisp_Object Vconcord_genre_object_hash_table;
 
 
-typedef struct Lisp_CONCORD_DS Lisp_CONCORD_DS;
-DECLARE_LRECORD (concord_ds, Lisp_CONCORD_DS);
+/*
+ * data source
+ */
 
 Lisp_Object Qconcord_dsp;
 
-struct Lisp_CONCORD_DS
-{
-  struct lcrecord_header header;
-  CONCORD_DS ds;
-};
-
-#define XCONCORD_DS(x) XRECORD (x, concord_ds, Lisp_CONCORD_DS)
-#define XSET_CONCORD_DS(x, p) XSETRECORD (x, p, concord_ds)
-#define CONCORD_DS_P(x) RECORDP (x, concord_ds)
-#define CHECK_CONCORD_DS(x) CHECK_RECORD (x, concord_ds)
-#define CONCHECK_CONCORD_DS(x) CONCHECK_RECORD (x, concord_ds)
-
 static Lisp_CONCORD_DS*
 allocate_concord_ds (void)
 {
@@ -187,7 +185,7 @@ Return directory of concord-ds DS.
        (ds))
 {
   Lisp_CONCORD_DS* lds;
-  unsigned char* directory;
+  char* directory;
 
   CHECK_CONCORD_DS (ds);
   lds = XCONCORD_DS (ds);
@@ -251,6 +249,10 @@ Return concord-ds of GENRE.
   CHECK_SYMBOL (genre);
 
   retval = Fgethash (genre, Vconcord_genre_hash_table, Qunbound);
+#ifdef HAVE_LIBCHISE
+  if ( UNBOUNDP (retval) )
+    retval = Vchise_system_db_directory;
+#endif
   if ( STRINGP (retval) )
     {
       retval = Fconcord_open_ds (retval, Qnil, Qnil, Qnil);
@@ -270,7 +272,7 @@ struct closure_to_list_feature
 } *concord_feature_list_closure;
 
 static int
-add_feature_to_list_mapper (CONCORD_Genre genre, unsigned char* name)
+add_feature_to_list_mapper (CONCORD_Genre genre, char* name)
 {
   /* This function can GC */
   concord_feature_list_closure->feature_list
@@ -284,7 +286,7 @@ Return the list of all existing features in GENRE.
        (genre, ds))
 {
   Lisp_CONCORD_DS* lds;
-  unsigned char* genre_name;
+  char* genre_name;
   CONCORD_Genre c_genre;
   struct gcpro gcpro1;
 
@@ -313,28 +315,12 @@ Return the list of all existing features in GENRE.
 }
 
 
-typedef struct Lisp_CONCORD_Object Lisp_CONCORD_Object;
-DECLARE_LRECORD (concord_object, Lisp_CONCORD_Object);
+/*
+ * Concord-object
+ */
 
 Lisp_Object Qconcord_objectp;
 
-struct Lisp_CONCORD_Object
-{
-  struct lcrecord_header header;
-  CONCORD_Genre genre;
-  Lisp_Object id;
-};
-
-#define XCONCORD_OBJECT(x) XRECORD (x, concord_object, Lisp_CONCORD_Object)
-#define XSET_CONCORD_OBJECT(x, p) XSETRECORD (x, p, concord_object)
-#define CONCORD_OBJECT_P(x) RECORDP (x, concord_object)
-#define CHECK_CONCORD_OBJECT(x) CHECK_RECORD (x, concord_object)
-#define CONCHECK_CONCORD_OBJECT(x) CONCHECK_RECORD (x, concord_object)
-#define CONCORD_OBJECT_GENRE(x)        ((x)->genre)
-#define CONCORD_OBJECT_ID(x)   ((x)->id)
-#define XCONCORD_OBJECT_ID(x)  CONCORD_OBJECT_ID (XCONCORD_OBJECT(x))
-#define XCONCORD_OBJECT_GENRE(x) CONCORD_OBJECT_GENRE (XCONCORD_OBJECT(x))
-
 static Lisp_CONCORD_Object*
 allocate_concord_object (void)
 {
@@ -360,21 +346,31 @@ print_concord_object (Lisp_Object obj,
   Lisp_CONCORD_Object* lcobj = XCONCORD_OBJECT (obj);
   struct gcpro gcpro1, gcpro2;
 
-  if (print_readably)
-    error ("printing unreadable object #<concord_object 0x%x>",
-          lcobj->header.uid);
-
-  write_c_string ("#<concord_object \"", printcharfun);
-  write_c_string (concord_ds_location
-                 (concord_genre_get_data_source (lcobj->genre)),
-                 printcharfun);
-  write_c_string (":", printcharfun);
-  write_c_string (concord_genre_get_name (lcobj->genre), printcharfun);
-  write_c_string (";", printcharfun);
-  GCPRO2 (obj, printcharfun);
-  print_internal (lcobj->id, printcharfun, escapeflag);
-  UNGCPRO;
-  write_c_string ("\">", printcharfun);
+#if 0
+  if ( print_readably )
+    {
+#endif
+      write_c_string ("#s(concord-object", printcharfun);
+      write_c_string (" genre ", printcharfun);
+      write_c_string (concord_genre_get_name (lcobj->genre), printcharfun);
+      write_c_string (" =id ", printcharfun);
+      GCPRO2 (obj, printcharfun);
+      print_internal (lcobj->id, printcharfun, escapeflag);
+      UNGCPRO;
+      write_c_string (")", printcharfun);
+#if 0
+    }
+  else
+    {
+      write_c_string ("#<concord-object \"", printcharfun);
+      write_c_string (concord_genre_get_name (lcobj->genre), printcharfun);
+      write_c_string (";", printcharfun);
+      GCPRO2 (obj, printcharfun);
+      print_internal (lcobj->id, printcharfun, escapeflag);
+      UNGCPRO;
+      write_c_string ("\">", printcharfun);
+    }
+#endif
 }
 
 static void
@@ -392,6 +388,13 @@ finalize_concord_object (void *header, int for_disksave)
     }
 }
 
+static int
+concord_object_equal (Lisp_Object cobj1, Lisp_Object cobj2, int depth)
+{
+  return internal_equal ( XCONCORD_OBJECT_ID(cobj1),
+                         XCONCORD_OBJECT_ID(cobj2), depth);
+}
+
 static const struct lrecord_description concord_object_description[] = {
   { XD_LISP_OBJECT, offsetof (Lisp_CONCORD_Object, id) },
   { XD_END }
@@ -399,46 +402,95 @@ static const struct lrecord_description concord_object_description[] = {
 
 DEFINE_LRECORD_IMPLEMENTATION ("concord_object", concord_object,
                                mark_concord_object, print_concord_object,
-                              finalize_concord_object, 0, 0,
+                              finalize_concord_object,
+                              concord_object_equal, 0,
                               concord_object_description,
                               Lisp_CONCORD_Object);
 
-DEFUN ("concord-make-object",
-       Fconcord_make_object, 1, 3, 0, /*
+static Lisp_Object
+concord_genre_cache_get_object (Lisp_Object genre, Lisp_Object id)
+{
+  Lisp_Object obj_hash;
+  
+  obj_hash = Fgethash (genre, Vconcord_genre_object_hash_table, Qunbound);
+  if (UNBOUNDP (obj_hash))
+    return Qunbound;
+  return Fgethash (id, obj_hash, Qunbound);
+}
+
+static Lisp_Object
+concord_genre_cache_put_object (Lisp_Object genre, Lisp_Object id,
+                               Lisp_Object object)
+{
+  Lisp_Object obj_hash;
+
+  obj_hash = Fgethash (genre, Vconcord_genre_object_hash_table, Qunbound);
+  if (UNBOUNDP (obj_hash))
+    {
+      obj_hash
+       = make_lisp_hash_table (256, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
+      Fputhash (genre, obj_hash, Vconcord_genre_object_hash_table);
+    }
+  return Fputhash (id, object, obj_hash);
+}
+
+DEFUN ("concord-make-object", Fconcord_make_object, 1, 3, 0, /*
 Make and return a Concord-object from ID and GENRE.
 Optional argument DS specifies the data-source of the GENRE.
 */
-       (id, genre, ds))
+       (genre, id, ds))
 {
   Lisp_CONCORD_DS* lds;
-  unsigned char* genre_name;
+  Lisp_Object genre_string;
+  char* genre_name_str;
   CONCORD_Genre c_genre;
   Lisp_CONCORD_Object* lcobj;
   Lisp_Object retval;
 
+  if (!NILP (id))
+    {
+      retval = concord_genre_cache_get_object (genre, id);
+      if (!UNBOUNDP (retval))
+       {
+         return retval;
+       }
+    }
   if (NILP (ds))
     ds = Fconcord_genre_ds (genre);
   CHECK_CONCORD_DS (ds);
   lds = XCONCORD_DS (ds);
   if (lds->ds == NULL)
     return Qnil;
-  if ( !STRINGP(genre) )
-    genre = Fsymbol_name (genre);
-  TO_EXTERNAL_FORMAT (LISP_STRING, genre,
-                     C_STRING_ALLOCA, genre_name,
+  if ( STRINGP(genre) )
+    genre_string = genre;
+  else
+    genre_string = Fsymbol_name (genre);
+  TO_EXTERNAL_FORMAT (LISP_STRING, genre_string,
+                     C_STRING_ALLOCA, genre_name_str,
                      Qfile_name);
-  c_genre = concord_ds_get_genre (lds->ds, genre_name);
+  c_genre = concord_ds_get_genre (lds->ds, genre_name_str);
   if (c_genre == NULL)
     return Qnil;
   lcobj = allocate_concord_object ();
   lcobj->genre = c_genre;
   lcobj->id = id;
   XSET_CONCORD_OBJECT (retval, lcobj);
+  if (!NILP (id))
+    {
+      struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
+
+      GCPRO4 (retval, id, genre, ds);
+      concord_genre_cache_put_object (genre, id, retval);
+#if 1
+      if (!EQ (Fconcord_object_get (retval, Q_id), id))
+       Fconcord_object_put (retval, Q_id, id);
+#endif
+      UNGCPRO;
+    }
   return retval;
 }
 
-DEFUN ("concord-object-p",
-       Fconcord_object_p, 1, 1, 0, /*
+DEFUN ("concord-object-p", Fconcord_object_p, 1, 1, 0, /*
 Return t if OBJECT is a concord-object.
 */
        (object))
@@ -446,8 +498,7 @@ Return t if OBJECT is a concord-object.
   return CONCORD_OBJECT_P (object) ? Qt : Qnil;
 }
 
-DEFUN ("concord-object-id",
-       Fconcord_object_id, 1, 1, 0, /*
+DEFUN ("concord-object-id", Fconcord_object_id, 1, 1, 0, /*
 Return an id of Concord-object OBJECT.
 */
        (object))
@@ -456,8 +507,16 @@ Return an id of Concord-object OBJECT.
   return XCONCORD_OBJECT_ID (object);
 }
 
-DEFUN ("concord-decode-object",
-       Fconcord_decode_object, 2, 4, 0, /*
+DEFUN ("concord-object-genre", Fconcord_object_genre, 1, 1, 0, /*
+Return genre of Concord-object OBJECT.
+*/
+       (object))
+{
+  CHECK_CONCORD_OBJECT (object);
+  return intern (concord_genre_get_name (XCONCORD_OBJECT_GENRE (object)));
+}
+
+DEFUN ("concord-decode-object", Fconcord_decode_object, 2, 4, 0, /*
 Make and return a Concord-object from FEATURE and VALUE.
 Optional argument GENRE specifies the GENRE of the object.
 Optional argument DS specifies the data-source of the GENRE.
@@ -465,15 +524,16 @@ Optional argument DS specifies the data-source of the GENRE.
        (feature, value, genre, ds))
 {
   Lisp_CONCORD_DS* lds;
-  unsigned char* genre_name;
+  char* genre_name;
   CONCORD_Genre c_genre;
-  unsigned char* feature_name;
+  char* feature_name;
   CONCORD_INDEX c_index;
   Lisp_Object value_string;
-  unsigned char* strid;
+  char* strid;
   CONCORD_String_Tank st_id;
   int status;
   Lisp_Object obj;
+  int previous_print_readably;
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
 
   if (NILP (ds))
@@ -504,43 +564,49 @@ Optional argument DS specifies the data-source of the GENRE.
       return Qnil;
     }
 
+  previous_print_readably = print_readably;
+  print_readably = 1;
   GCPRO5 (feature, value, genre, ds, value_string);
   value_string = Fprin1_to_string (value, Qnil);
   UNGCPRO;
+  print_readably = previous_print_readably;
   TO_EXTERNAL_FORMAT (LISP_STRING,
                      value_string, C_STRING_ALLOCA, strid,
                      Qfile_name);
   status = concord_index_strid_get_obj_string (c_index, strid, &st_id);
   if (!status)
     {
-      GCPRO3 (genre, ds, obj);
+      Lisp_Object retval;
+
+      GCPRO4 (genre, ds, obj, retval);
 #if 0
       obj = read_from_c_string (CONCORD_String_data (&st_id),
                                CONCORD_String_size (&st_id) );
 #else
       obj = Fcar (Fread_from_string (make_ext_string
-                                    (CONCORD_String_data (&st_id),
+                                    ((char*)CONCORD_String_data (&st_id),
                                      CONCORD_String_size (&st_id),
                                      Qfile_name),
                                     Qnil, Qnil));
 #endif
+      retval = Fconcord_make_object (genre, obj, ds);
       UNGCPRO;
-      return Fconcord_make_object (obj, genre, ds);
+      return retval;
     }
   return Qnil;
 }
 
-DEFUN ("concord-object-get",
-       Fconcord_object_get, 2, 2, 0, /*
+DEFUN ("concord-object-get", Fconcord_object_get, 2, 2, 0, /*
 Return the value of OBJECT's FEATURE.
 */
        (object, feature))
 {
   struct gcpro gcpro1, gcpro2;
+  int previous_print_readably;
   Lisp_Object obj_string;
-  unsigned char* c_obj;
+  char* c_obj;
   CONCORD_Genre c_genre;
-  unsigned char* feature_name;
+  char* feature_name;
   CONCORD_Feature c_feature;
   int status;
   CONCORD_String_Tank st_value;
@@ -548,9 +614,12 @@ Return the value of OBJECT's FEATURE.
   CHECK_CONCORD_OBJECT (object);
   if ( !STRINGP(feature) )
     feature = Fsymbol_name (feature);
+  previous_print_readably = print_readably;
+  print_readably = 1;
   GCPRO2 (object, feature);
   obj_string = Fprin1_to_string (XCONCORD_OBJECT_ID(object), Qnil);
   UNGCPRO;
+  print_readably = previous_print_readably;
   TO_EXTERNAL_FORMAT (LISP_STRING, obj_string,
                      C_STRING_ALLOCA, c_obj, Qfile_name);
   c_genre = XCONCORD_OBJECT_GENRE(object);
@@ -568,7 +637,7 @@ Return the value of OBJECT's FEATURE.
     {
       return
        Fcar (Fread_from_string (make_ext_string
-                                (CONCORD_String_data (&st_value),
+                                ((char*)CONCORD_String_data (&st_value),
                                  CONCORD_String_size (&st_value),
                                  Qfile_name),
                                 Qnil, Qnil));
@@ -576,28 +645,29 @@ Return the value of OBJECT's FEATURE.
   return Qnil;
 }
 
-DEFUN ("concord-object-put",
-       Fconcord_object_put, 3, 3, 0, /*
-Store a VALUE of OBJECT's FEATURE.
-*/
-       (object, feature, value))
+static Lisp_Object
+concord_object_put (Lisp_Object object, Lisp_Object feature,
+                   Lisp_Object value)
 {
   struct gcpro gcpro1, gcpro2, gcpro3;
+  int previous_print_readably;
   Lisp_Object obj_string;
-  unsigned char* c_obj;
+  char* c_obj;
   CONCORD_Genre c_genre;
-  unsigned char* feature_name;
+  char* feature_name;
   CONCORD_Feature c_feature;
   int status;
   Lisp_Object value_string;
-  unsigned char* c_value;
+  char* c_value;
 
-  CHECK_CONCORD_OBJECT (object);
   if ( !STRINGP(feature) )
     feature = Fsymbol_name (feature);
+  previous_print_readably = print_readably;
+  print_readably = 1;
   GCPRO3 (object, feature, value);
   obj_string = Fprin1_to_string (XCONCORD_OBJECT_ID(object), Qnil);
   UNGCPRO;
+  print_readably = previous_print_readably;
   TO_EXTERNAL_FORMAT (LISP_STRING, obj_string,
                      C_STRING_ALLOCA, c_obj, Qfile_name);
   c_genre = XCONCORD_OBJECT_GENRE(object);
@@ -609,29 +679,145 @@ Store a VALUE of OBJECT's FEATURE.
     {
       return Qnil;
     }
+  previous_print_readably = print_readably;
+  print_readably = 1;
   GCPRO3 (object, feature, value);
   value_string = Fprin1_to_string (value, Qnil);
   UNGCPRO;
+  print_readably = previous_print_readably;
   TO_EXTERNAL_FORMAT (LISP_STRING, value_string,
                      C_STRING_ALLOCA, c_value,
                      Qfile_name);
-  status = concord_obj_put_feature_value_str (c_obj, c_feature, c_value);
+  status = concord_obj_put_feature_value_str (c_obj, c_feature,
+                                             (unsigned char*)c_value);
   if (status)
     return Qnil;
   status = chise_feature_sync (c_feature);
   if (status)
     return Qnil;
+  if (XSTRING_DATA(feature)[0] == '=')
+    {
+      CONCORD_INDEX c_index
+       = concord_genre_get_index (c_genre, feature_name);
+
+      concord_index_strid_put_obj (c_index, c_value, c_obj);
+      concord_index_sync (c_index);
+    }
+  return Qt;
+}
+
+DEFUN ("concord-object-put", Fconcord_object_put, 3, 3, 0, /*
+Store a VALUE of OBJECT's FEATURE.
+*/
+       (object, feature, value))
+{
+  Lisp_String* name;
+  Bufbyte *name_str;
+
+  CHECK_CONCORD_OBJECT (object);
+  CHECK_SYMBOL (feature);
+  name = symbol_name (XSYMBOL (feature));
+  name_str = string_data (name);
+  if ( NILP (concord_object_put (object, feature, value)) )
+    return Qnil;
+  if ( EQ (feature, Q_subsumptive)             ||
+       EQ (feature, Q_subsumptive_from)                ||
+       EQ (feature, Q_denotational)            ||
+       EQ (feature, Q_denotational_from)       ||
+       ( ( ((name_str[0] == '-') && (name_str[1] == '>')) ||
+          ((name_str[0] == '<') && (name_str[1] == '-')) )
+        && (memchr (name_str, '*', name->size) == NULL) ) )
+    {
+      Lisp_Object rest = value;
+      Lisp_Object ret;
+      Lisp_Object rev_feature = Qnil;
+      struct gcpro gcpro1;
+
+      GCPRO1 (rev_feature);
+      if (EQ (feature, Q_subsumptive))
+       rev_feature = Q_subsumptive_from;
+      else if (EQ (feature, Q_subsumptive_from))
+       rev_feature = Q_subsumptive;
+      else if (EQ (feature, Q_denotational))
+       rev_feature = Q_denotational_from;
+      else if (EQ (feature, Q_denotational_from))
+       rev_feature = Q_denotational;
+      else
+       {
+         Bytecount length = string_length (name);
+         Bufbyte *rev_name_str = alloca (length + 1);
+
+         memcpy (rev_name_str + 2, name_str + 2, length - 2);
+         if (name_str[0] == '<')
+           {
+             rev_name_str[0] = '-';
+             rev_name_str[1] = '>';
+           }
+         else
+           {
+             rev_name_str[0] = '<';
+             rev_name_str[1] = '-';
+           }
+         rev_name_str[length] = 0;
+         rev_feature = intern (rev_name_str);
+       }
+
+      while (CONSP (rest))
+       {
+         ret = XCAR (rest);
+
+         if ( CONCORD_OBJECT_P (ret) && !EQ (ret, object) )
+           {
+             Lisp_Object ffv;
+
+             ffv = Fconcord_object_get (ret, rev_feature);
+             if (!CONSP (ffv))
+               concord_object_put (ret, rev_feature, list1 (object));
+             else if (NILP (Fmember (object, ffv)))
+               concord_object_put
+                 (ret, rev_feature,
+                  nconc2 (Fcopy_sequence (ffv), list1 (object)));
+             Fsetcar (rest, ret);
+           }
+         rest = XCDR (rest);
+       }
+      UNGCPRO;
+    }
   return Qt;
 }
 
+DEFUN ("concord-object-adjoin", Fconcord_object_adjoin, 3, 3, 0, /*
+Cons ITEM onto the front of FEATURE's value of OBJECT only if it's not already there.
+*/
+       (object, feature, item))
+{
+  Lisp_Object ret = Fconcord_object_get (object, feature);
+
+  if ( NILP (Fmember (item, ret)) )
+    return Fconcord_object_put (object, feature, Fcons (item, ret));
+  return Qnil;
+}
+
+DEFUN ("concord-object-adjoin*", Fconcord_object_adjoinX, 3, 3, 0, /*
+Append ITEM onto the end of FEATURE's value of OBJECT only if it's not already there.
+*/
+       (object, feature, item))
+{
+  Lisp_Object ret = Fconcord_object_get (object, feature);
+
+  if ( NILP (Fmember (item, ret)) )
+    return Fconcord_object_put (object, feature, nconc2 (ret, list1 (item)));
+  return Qnil;
+}
+
 struct closure_for_object_spec
 {
-  unsigned char* object_id;
+  char* object_id;
   Lisp_Object spec;
 } *concord_object_spec_closure;
 
 static int
-add_feature_to_spec_mapper (CONCORD_Genre genre, unsigned char* name)
+add_feature_to_spec_mapper (CONCORD_Genre genre, char* name)
 {
   /* This function can GC */
   CONCORD_String_Tank st_value;
@@ -651,7 +837,7 @@ add_feature_to_spec_mapper (CONCORD_Genre genre, unsigned char* name)
        = Fcons (Fcons (intern (name),
                        Fcar (Fread_from_string
                              (make_ext_string
-                              (CONCORD_String_data (&st_value),
+                              ((char*)CONCORD_String_data (&st_value),
                                CONCORD_String_size (&st_value),
                                Qfile_name),
                               Qnil, Qnil))),
@@ -666,14 +852,18 @@ Return the spec of OBJECT.
        (object))
 {
   Lisp_Object obj_string;
-  unsigned char* c_obj;
+  char* c_obj;
   CONCORD_Genre c_genre;
   struct gcpro gcpro1, gcpro2;
+  int previous_print_readably;
 
   CHECK_CONCORD_OBJECT (object);
+  previous_print_readably = print_readably;
+  print_readably = 1;
   GCPRO1 (object);
   obj_string = Fprin1_to_string (XCONCORD_OBJECT_ID(object), Qnil);
   UNGCPRO;
+  print_readably = previous_print_readably;
   TO_EXTERNAL_FORMAT (LISP_STRING, obj_string,
                      C_STRING_ALLOCA, c_obj, Qfile_name);
   c_genre = XCONCORD_OBJECT_GENRE(object);
@@ -688,6 +878,31 @@ Return the spec of OBJECT.
   return concord_object_spec_closure->spec;
 }
 
+DEFUN ("concord-define-object", Fconcord_define_object, 2, 3, 0, /*
+Define an object of which spec is a set of features SPEC.
+*/
+       (spec, genre, ds))
+{
+  Lisp_Object id = Fcdr (Fassq (Q_id, spec));
+  Lisp_Object obj;
+
+  if (!NILP (id))
+    {
+      Lisp_Object rest = spec;
+      Lisp_Object cell;
+
+      obj = Fconcord_make_object (genre, id, ds);
+      while (!NILP (rest))
+       {
+         cell = Fcar (rest);
+         Fconcord_object_put (obj, Fcar (cell), Fcdr (cell));
+         rest = Fcdr (rest);
+       }
+      return obj;
+    }
+  return Qnil;
+}
+
 struct closure_for_each_object
 {
   Lisp_Object function;
@@ -701,6 +916,7 @@ func_for_each_object (CONCORD_String object_id,
                      CONCORD_Feature feature,
                      CONCORD_String value)
 {
+  struct gcpro gcpro1, gcpro2;
   Lisp_Object obj, val, ret;
 
 #if 0
@@ -708,25 +924,29 @@ func_for_each_object (CONCORD_String object_id,
                            CONCORD_String_size (object_id) );
 #else
   obj = Fcar (Fread_from_string (make_ext_string
-                                (CONCORD_String_data (object_id),
+                                ((char*)CONCORD_String_data (object_id),
                                  CONCORD_String_size (object_id),
                                  Qfile_name),
                                 Qnil, Qnil));
 #endif
-  obj = Fconcord_make_object (obj,
-                             for_each_object_closure->genre,
+  GCPRO1 (obj);
+  obj = Fconcord_make_object (for_each_object_closure->genre,
+                             obj,
                              for_each_object_closure->ds);
 #if 0
   val = read_from_c_string (CONCORD_String_data (value),
                            CONCORD_String_size (value) );
 #else
   val = Fcar (Fread_from_string (make_ext_string
-                                (CONCORD_String_data (value),
+                                ((char*)CONCORD_String_data (value),
                                  CONCORD_String_size (value),
                                  Qfile_name),
                                 Qnil, Qnil));
 #endif
+  UNGCPRO;
+  GCPRO2 (obj, val);
   ret = call2 (for_each_object_closure->function, obj, val);
+  UNGCPRO;
   for_each_object_closure->ret = ret;
   return !NILP (ret);
 }
@@ -740,10 +960,11 @@ When the FUNCTION returns non-nil, it breaks the repeat.
 */
        (function, feature, genre, ds))
 {
+  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
   Lisp_CONCORD_DS* lds;
-  unsigned char* genre_name;
+  char* genre_name;
   CONCORD_Genre c_genre;
-  unsigned char* feature_name;
+  char* feature_name;
   CONCORD_Feature c_feature;
 
   if (NILP (ds))
@@ -776,11 +997,111 @@ When the FUNCTION returns non-nil, it breaks the repeat.
   for_each_object_closure->genre = genre;
   for_each_object_closure->ds = ds;
   for_each_object_closure->ret = Qnil;
+  GCPRO4 (for_each_object_closure->function,
+         for_each_object_closure->genre,
+         for_each_object_closure->ds,
+         for_each_object_closure->ret);
   concord_feature_foreach_obj_string (c_feature, func_for_each_object);
+  UNGCPRO;
   /* return Qt; */
   return for_each_object_closure->ret;
 }
 
+
+static int
+concord_name_validate (Lisp_Object keyword, Lisp_Object value,
+                      Error_behavior errb)
+{
+  if (ERRB_EQ (errb, ERROR_ME))
+    {
+      CHECK_SYMBOL (value);
+      return 1;
+    }
+
+  return SYMBOLP (value);
+}
+
+static int
+concord_id_validate (Lisp_Object keyword, Lisp_Object value,
+                    Error_behavior errb)
+{
+  if (ERRB_EQ (errb, ERROR_ME))
+    {
+      /* CHECK_SYMBOL (value); */
+      if ( INTP (value) || CHARP (value) || SYMBOLP (value) )
+       ;
+      else
+       dead_wrong_type_argument (Qsymbolp, value);
+      return 1;
+    }
+
+  return INTP (value) || CHARP (value) || SYMBOLP (value);
+}
+
+static int
+concord_object_validate (Lisp_Object data, Error_behavior errb)
+{
+  struct gcpro gcpro1, gcpro2, gcpro3;
+  Lisp_Object retval;
+  Lisp_Object valw = Qnil;
+  Lisp_Object genre = Qnil;
+  Lisp_Object oid = Qnil;
+
+  data = Fcdr (data); /* skip over Qconcord_object */
+  while (!NILP (data))
+    {
+      Lisp_Object keyw = Fcar (data);
+
+      data = Fcdr (data);
+      valw = Fcar (data);
+      data = Fcdr (data);
+      if (EQ (keyw, Qgenre))
+       genre = valw;
+      else if (EQ (keyw, Q_id))
+       oid = valw;
+      else
+       ABORT ();
+    }
+
+  if (NILP (genre))
+    {
+      maybe_error (Qconcord_object, errb, "No genre given");
+      return 0;
+    }
+  if (NILP (oid))
+    {
+      maybe_error (Qconcord_object, errb, "No object-id given");
+      return 0;
+    }
+
+  GCPRO3 (genre, oid, retval);
+  retval = Fconcord_make_object (genre, oid, Qnil);
+  UNGCPRO;
+  if (NILP (retval))
+    {
+      maybe_signal_simple_error_2 ("No such Concord-object",
+                                  oid, genre, Qconcord_object, errb);
+      return 0;
+    }
+
+  return 1;
+}
+
+static Lisp_Object
+concord_object_instantiate (Lisp_Object data)
+{
+  struct gcpro gcpro1, gcpro2;
+  Lisp_Object retval;
+
+  GCPRO2 (data, retval);
+  retval = Fconcord_make_object (Fplist_get (data, Qgenre, Qnil),
+                                Fplist_get (data, Q_id, Qnil),
+                                Qnil);
+  UNGCPRO;
+  return retval;
+}
+
+
 void
 syms_of_concord (void)
 {
@@ -790,6 +1111,13 @@ syms_of_concord (void)
   defsymbol (&Qconcord, "concord");
   defsymbol (&Qconcord_dsp, "concord-dsp");
   defsymbol (&Qconcord_objectp, "concord-objectp");
+  defsymbol (&Qconcord_object, "concord-object");
+  defsymbol (&Qgenre, "genre");
+  defsymbol (&Q_id, "=id");
+#ifdef HAVE_LIBCHISE
+  defsymbol (&Qcharacter, "character");
+  defsymbol (&Qfeature, "feature");
+#endif
 
   DEFSUBR (Fconcord_open_ds);
   DEFSUBR (Fconcord_ds_p);
@@ -804,14 +1132,31 @@ syms_of_concord (void)
   DEFSUBR (Fconcord_make_object);
   DEFSUBR (Fconcord_object_p);
   DEFSUBR (Fconcord_object_id);
+  DEFSUBR (Fconcord_object_genre);
   DEFSUBR (Fconcord_decode_object);
   DEFSUBR (Fconcord_object_get);
   DEFSUBR (Fconcord_object_put);
+  DEFSUBR (Fconcord_object_adjoin);
+  DEFSUBR (Fconcord_object_adjoinX);
+  DEFSUBR (Fconcord_define_object);
   DEFSUBR (Fconcord_object_spec);
   DEFSUBR (Fconcord_foreach_object_in_feature);
 }
 
 void
+structure_type_create_concord (void)
+{
+  struct structure_type *st;
+
+  st = define_structure_type (Qconcord_object,
+                             concord_object_validate,
+                             concord_object_instantiate);
+
+  define_structure_type_keyword (st, Qgenre, concord_name_validate);
+  define_structure_type_keyword (st, Q_id, concord_id_validate);
+}
+
+void
 vars_of_concord (void)
 {
   Fprovide (Qconcord);
@@ -823,4 +1168,17 @@ vars_of_concord (void)
   staticpro (&Vconcord_genre_hash_table);
   Vconcord_genre_hash_table
     = make_lisp_hash_table (16, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
+
+  staticpro (&Vconcord_genre_object_hash_table);
+  Vconcord_genre_object_hash_table
+    = make_lisp_hash_table (16, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
+}
+
+void
+complex_vars_of_concord (void)
+{
+#ifdef HAVE_LIBCHISE
+  Fconcord_assign_genre (Qcharacter, Vchise_system_db_directory);
+  Fconcord_assign_genre (Qfeature, Vchise_system_db_directory);
+#endif
 }