[uim-commit] r2570 - in trunk: scm uim

ekato at freedesktop.org ekato at freedesktop.org
Wed Dec 14 02:34:53 PST 2005


Author: ekato
Date: 2005-12-14 02:34:49 -0800 (Wed, 14 Dec 2005)
New Revision: 2570

Modified:
   trunk/scm/skk-custom.scm
   trunk/scm/skk.scm
   trunk/uim/skk.c
Log:
* Add functionality to use UNIX look command for completion in
  latin conversion state of uim-skk.

* scm/skk-custom.scm (skk-use-look?) : New custom setting.
  Default is #f.
* scm/skk.scm (skk-get-nth-completion) : Check skk-use-look?
  for getting completion.
(skk-append-string) : Ditto.
(skk-begin-completion) : Ditto.
(skk-commit-with-conv-completion) : Ditto.
(skk-proc-state-kanji) : Ditto.
(skk-change-completion-index) : Ditto.
(skk-proc-state-completion) : Ditto.
* uim/skk.c : Define LOOK_COMMAND.
(skk_dic_open) : Set SIGPIPE, SIG_IGN here.
(make_comp_array_from_cache) : Use use_look_ argument and get
  completion with look.
(find_comp_array) : Use use_look_ argument.
(find_comp_array_lisp) : Ditto.
(skk_get_completion) : Ditto.
(skk_get_nth_completion) : Ditto.
(skk_get_nr_completion) : Ditto.
(skk_get_dcomp_word) : Ditto.  Get completion using look.
(look_popen) : New.  Call popen() for look and return fp.
(look_get_top_word) : Return lisp str using look.
(look_get_comp) : Add completion words array using look.
(uim_plugin_instance_init) : Follow the changes in arguments.
(open_skkserv) : Don't call signal(SIGPIPE, SIG_IGN) here.


Modified: trunk/scm/skk-custom.scm
===================================================================
--- trunk/scm/skk-custom.scm	2005-12-14 10:09:37 UTC (rev 2569)
+++ trunk/scm/skk-custom.scm	2005-12-14 10:34:49 UTC (rev 2570)
@@ -400,6 +400,11 @@
   (_ "Enable dynamic completion")
   (_ "long description will be here."))
 
+(define-custom 'skk-use-look? #f
+  '(skk-advanced)
+  '(boolean)
+  (_ "Use UNIX look command for completion")
+  (_ "long description will be here."))
 ;;
 ;; annotation
 ;;

Modified: trunk/scm/skk.scm
===================================================================
--- trunk/scm/skk.scm	2005-12-14 10:09:37 UTC (rev 2569)
+++ trunk/scm/skk.scm	2005-12-14 10:34:49 UTC (rev 2570)
@@ -517,7 +517,8 @@
     (skk-lib-get-nth-completion
      n
      (skk-make-string (skk-context-head sc) skk-type-hiragana)
-     skk-use-numeric-conversion?)))
+     skk-use-numeric-conversion?
+     skk-use-look?)))
 
 (define skk-get-current-completion
   (lambda (sc)
@@ -620,7 +621,8 @@
 	  (skk-lib-get-dcomp-word
 	   (skk-make-string
 	    (skk-context-head sc) (skk-context-kana-mode sc))
-	   skk-use-numeric-conversion?))))))
+	   skk-use-numeric-conversion?
+	   skk-use-look?))))))
 
 (define skk-append-okuri-string
   (lambda (sc str)
@@ -670,7 +672,8 @@
 	(skk-append-residual-kana sc))
     (skk-lib-get-completion
      (skk-make-string (skk-context-head sc) (skk-context-kana-mode sc))
-     skk-use-numeric-conversion?)
+     skk-use-numeric-conversion?
+     skk-use-look?)
     (skk-context-set-completion-nth! sc 0)
     (skk-context-set-state! sc 'skk-state-completion)))
 
@@ -1198,7 +1201,8 @@
 			(skk-make-string
 			 (skk-context-head sc)
 			 (skk-context-kana-mode sc))
-			skk-use-numeric-conversion?)))
+			skk-use-numeric-conversion?
+			skk-use-look?)))
 	    (if (not (string=? dcomp ""))
 		(begin
 		  (skk-string-list-to-context-head
@@ -1295,7 +1299,8 @@
 		       (skk-make-string
 			(skk-context-head sc)
 			(skk-context-kana-mode sc))
-		       skk-use-numeric-conversion?)
+		       skk-use-numeric-conversion?
+		       skk-use-look?)
 			"")))
 	     #f)
 	   #t)
@@ -1336,7 +1341,8 @@
 			   (skk-make-string
 			    (skk-context-head sc)
 			    (skk-context-kana-mode sc))
-			   skk-use-numeric-conversion?))))
+			   skk-use-numeric-conversion?
+			   skk-use-look?))))
 		 (if (not (null? sl))
 		     (begin
 		       (skk-string-list-to-context-head sc sl)
@@ -1729,7 +1735,8 @@
 	(begin
 	  (if (> (- (skk-lib-get-nr-completions
 		     (skk-make-string (skk-context-head sc) skk-type-hiragana)
-		     skk-use-numeric-conversion?)
+		     skk-use-numeric-conversion?
+		     skk-use-look?)
 		    1)
 		 (skk-context-completion-nth sc))
 	      (skk-context-set-completion-nth!
@@ -1795,7 +1802,8 @@
 	     (if (not (null? sl))
 		 (begin (skk-lib-get-completion
 			 (skk-get-current-completion sc)
-			 skk-use-numeric-conversion?)))
+			 skk-use-numeric-conversion?
+			 skk-use-look?)))
 	     (skk-lib-clear-completions
 	      (skk-make-string
 	       (skk-context-head sc)

Modified: trunk/uim/skk.c
===================================================================
--- trunk/uim/skk.c	2005-12-14 10:09:37 UTC (rev 2569)
+++ trunk/uim/skk.c	2005-12-14 10:34:49 UTC (rev 2570)
@@ -66,6 +66,7 @@
 #define USE_SKK_JISYO_S_BUF	1	/* use SKK-JISYO.S as a cache for
 					   word completion */
 #define SKK_JISYO_S	DATADIR "/skk/SKK-JISYO.S"
+#define LOOK_COMMAND	"look"		/* UNIX look command for completion */
 
 /*
  * cand : candidate
@@ -162,6 +163,8 @@
 		struct skk_cand_array *dst_ca, char *purged_cand);
 static void update_personal_dictionary_cache_with_file(const char *fn,
 		int is_personal);
+static void look_get_comp(struct skk_comp_array *ca, const char *str);
+static uim_lisp look_get_top_word(const char *str);
 
 /* skkserv connection */
 #define SKK_SERVICENAME	"skkserv"
@@ -413,6 +416,8 @@
   uim_bool use_skkserv = uim_scm_c_bool(use_skkserv_);
   int skkserv_portnum = uim_scm_c_int(skkserv_portnum_);
   
+  signal(SIGPIPE, SIG_IGN);
+
   if (!skk_dic) {
     skk_dic = open_dic(fn, use_skkserv, skkserv_portnum);
   }
@@ -1750,7 +1755,7 @@
 }
 
 static struct skk_comp_array *
-make_comp_array_from_cache(struct dic_info *di, const char *s)
+make_comp_array_from_cache(struct dic_info *di, const char *s, uim_lisp use_look_)
 {
   struct skk_line *sl;
   struct skk_comp_array *ca;
@@ -1779,6 +1784,9 @@
     }
   }
 
+  if NFALSEP(use_look_)
+    look_get_comp(ca, s);
+
   if (ca->nr_comps == 0) {
     free(ca);
     ca = NULL;
@@ -1791,7 +1799,7 @@
 }
 
 static struct skk_comp_array *
-find_comp_array(struct dic_info *di, const char *s)
+find_comp_array(struct dic_info *di, const char *s, uim_lisp use_look_)
 {
   struct skk_comp_array *ca;
 
@@ -1803,14 +1811,14 @@
       break;
   }
   if (ca == NULL) {
-    ca = make_comp_array_from_cache(di, s);
+    ca = make_comp_array_from_cache(di, s, use_look_);
   }
 
   return ca;
 }
 
 static struct skk_comp_array *
-find_comp_array_lisp(uim_lisp head_, uim_lisp numeric_conv_)
+find_comp_array_lisp(uim_lisp head_, uim_lisp numeric_conv_, uim_lisp use_look_)
 {
   const char *hs;
   struct skk_comp_array *ca;
@@ -1822,32 +1830,33 @@
     rs = replace_numeric(hs);
 
   if (!rs)
-    ca = find_comp_array(skk_dic, hs);
+    ca = find_comp_array(skk_dic, hs, use_look_);
   else {
-    ca = find_comp_array(skk_dic, rs);
+    ca = find_comp_array(skk_dic, rs, use_look_);
     free(rs);
   }
   return ca;
 }
 
 static uim_lisp
-skk_get_completion(uim_lisp head_, uim_lisp numeric_conv_)
+skk_get_completion(uim_lisp head_, uim_lisp numeric_conv_, uim_lisp use_look_)
 {
   struct skk_comp_array *ca;
-  ca = find_comp_array_lisp(head_, numeric_conv_);
+  ca = find_comp_array_lisp(head_, numeric_conv_, use_look_);
   if (ca) {
     ca->refcount++;
     return uim_scm_t();
   }
 
   if (NFALSEP(numeric_conv_) && has_numeric_in_head(head_))
-    return skk_get_completion(head_, uim_scm_f());
+    return skk_get_completion(head_, uim_scm_f(), use_look_);
 
   return uim_scm_f();
 }
 
 static uim_lisp
-skk_get_nth_completion(uim_lisp nth_, uim_lisp head_, uim_lisp numeric_conv_)
+skk_get_nth_completion(uim_lisp nth_, uim_lisp head_,
+		uim_lisp numeric_conv_, uim_lisp use_look_)
 {
   int n;
   struct skk_comp_array *ca;
@@ -1858,13 +1867,13 @@
     numlst_ = skk_store_replaced_numeric_str(head_);
 
   if (!uim_scm_nullp(numlst_))
-    ca = find_comp_array_lisp(head_, numeric_conv_);
+    ca = find_comp_array_lisp(head_, numeric_conv_, use_look_);
   else
-    ca = find_comp_array_lisp(head_, uim_scm_f());
+    ca = find_comp_array_lisp(head_, uim_scm_f(), use_look_);
 
   if (!ca) {
     if (!uim_scm_nullp(numlst_))
-      return skk_get_nth_completion(nth_, head_, uim_scm_f());
+      return skk_get_nth_completion(nth_, head_, uim_scm_f(), use_look_);
     else
       return uim_scm_make_str("");
   }
@@ -1880,24 +1889,24 @@
 
   if (!uim_scm_nullp(numlst_) && n >= ca->nr_comps)
     return skk_get_nth_completion(uim_scm_make_int(n - ca->nr_comps),
-		    head_, uim_scm_f());
+		    head_, uim_scm_f(), use_look_);
 
   return uim_scm_make_str("");
 }
 
 static uim_lisp
-skk_get_nr_completions(uim_lisp head_, uim_lisp numeric_conv_)
+skk_get_nr_completions(uim_lisp head_, uim_lisp numeric_conv_, uim_lisp use_look_)
 {
   int n = 0;
   struct skk_comp_array *ca;
 
-  ca = find_comp_array_lisp(head_, numeric_conv_);
+  ca = find_comp_array_lisp(head_, numeric_conv_, use_look_);
   if (ca)
     n = ca->nr_comps;
 
   if (NFALSEP(numeric_conv_) && has_numeric_in_head(head_))
     return uim_scm_make_int(n +
-		    uim_scm_c_int(skk_get_nr_completions(head_, uim_scm_f()))); 
+		    uim_scm_c_int(skk_get_nr_completions(head_, uim_scm_f(), use_look_))); 
 
   return uim_scm_make_int(n);
 }
@@ -1992,14 +2001,15 @@
 }
 
 static uim_lisp
-skk_get_dcomp_word(uim_lisp head_, uim_lisp numeric_conv_)
+skk_get_dcomp_word(uim_lisp head_, uim_lisp numeric_conv_, uim_lisp use_look_)
 {
   const char *hs;
   struct skk_line *sl;
   int len;
-  uim_lisp numlst_ = uim_scm_null_list();
+  uim_lisp numlst_, look_;
   char *rs = NULL;
-  
+
+  numlst_ = uim_scm_null_list();
   hs = uim_scm_refer_c_str(head_);
 
   if NFALSEP(numeric_conv_)
@@ -2013,14 +2023,19 @@
 
   if (len != 0) {
     /* Search from cache using same way as in make_comp_array_from_cache(). */
-    if (!rs)
+    if (!rs) {
       for (sl = skk_dic->head.next; sl; sl = sl->next) {
 	if (!strncmp(sl->head, hs, len) && strcmp(sl->head, hs) &&
 			sl->okuri_head == '\0' &&
 			sl->state & SKK_LINE_USE_FOR_COMPLETION)
 	  return uim_scm_make_str(sl->head);
       }
-    else {
+      if NFALSEP(use_look_) {
+	look_ = look_get_top_word(hs);
+	if NFALSEP(look_)
+	  return look_;
+      }
+    } else {
       for (sl = skk_dic->head.next; sl; sl = sl->next) {
 	if (!strncmp(sl->head, rs, len) && strcmp(sl->head, rs) &&
 			sl->okuri_head == '\0' &&
@@ -2029,8 +2044,15 @@
 	  return restore_numeric(sl->head, numlst_);
 	}
       }
-      free(rs);
-      return skk_get_dcomp_word(head_, uim_scm_f());
+      if NFALSEP(use_look_) {
+	look_ = look_get_top_word(rs);
+	free(rs);
+	if NFALSEP(look_)
+	  return look_;
+      } else {
+	free(rs);
+      }
+      return skk_get_dcomp_word(head_, uim_scm_f(), use_look_);
     }
   }
   return uim_scm_make_str("");
@@ -3316,7 +3338,85 @@
   return ret;
 }
 
+static FILE *
+look_popen(const char *str)
+{
+  char *command;
+  FILE *fp;
+  int len;
+  
+  len = strlen(LOOK_COMMAND) + strlen(str) + 1;
+  command = malloc(len + 1);
+  if (!command)
+    return NULL;
 
+  snprintf(command, len + 1, "%s %s", LOOK_COMMAND, str);
+  fp = popen(command, "r");
+  free(command);
+
+  return fp;
+}
+
+static uim_lisp
+look_get_top_word(const char *str)
+{
+  char buf[512], *p;
+  FILE *fp;
+  uim_lisp ret_ = uim_scm_f();
+
+  if (!skk_isalpha(str[0]))
+    return ret_;
+
+  fp = look_popen(str);
+  if (!fp) {
+    perror("popen look");
+    return ret_;
+  }
+
+  while (fgets(buf, 512, fp) != NULL) {
+    p = strchr(buf, '\n');
+    if (p != NULL)
+      *p = '\0';
+    if (strcmp(buf, str)) { /* don't use the word itself */
+      ret_ = uim_scm_make_str(buf);
+      break;
+    }
+  }
+  /* read to the end */
+  while (fgets(buf, 512, fp) != NULL) ;
+  pclose(fp);
+
+  return ret_;
+}
+
+static void
+look_get_comp(struct skk_comp_array *ca, const char *str)
+{
+  char buf[512], *p;
+  FILE *fp;
+
+  if (!skk_isalpha(str[0]))
+    return;
+
+  fp = look_popen(str);
+  if (!fp) {
+    perror("popen look");
+    return;
+  }
+
+  while (fgets(buf, 512, fp) != NULL) {
+    p = strchr(buf, '\n');
+    if (p != NULL)
+      *p = '\0';
+    if (strcmp(buf, str)) { /* don't use the word itself */
+      ca->nr_comps++;
+      ca->comps = realloc(ca->comps, sizeof(char *) * ca->nr_comps);
+      ca->comps[ca->nr_comps - 1] = strdup(buf);
+    }
+  }
+  pclose(fp);
+}
+
 void
 uim_plugin_instance_init(void)
 {
@@ -3334,11 +3434,11 @@
   uim_scm_init_subr_5("skk-lib-learn-word", skk_learn_word);
   uim_scm_init_subr_1("skk-lib-get-annotation", skk_get_annotation);
   uim_scm_init_subr_1("skk-lib-remove-annotation", skk_remove_annotation);
-  uim_scm_init_subr_2("skk-lib-get-completion", skk_get_completion);
-  uim_scm_init_subr_3("skk-lib-get-nth-completion", skk_get_nth_completion);
-  uim_scm_init_subr_2("skk-lib-get-nr-completions", skk_get_nr_completions);
+  uim_scm_init_subr_3("skk-lib-get-completion", skk_get_completion);
+  uim_scm_init_subr_4("skk-lib-get-nth-completion", skk_get_nth_completion);
+  uim_scm_init_subr_3("skk-lib-get-nr-completions", skk_get_nr_completions);
   uim_scm_init_subr_2("skk-lib-clear-completions", skk_clear_completions);
-  uim_scm_init_subr_2("skk-lib-get-dcomp-word", skk_get_dcomp_word);
+  uim_scm_init_subr_3("skk-lib-get-dcomp-word", skk_get_dcomp_word);
   uim_scm_init_subr_1("skk-lib-eval-candidate", skk_eval_candidate);
   uim_scm_init_subr_3("skk-lib-substring", skk_substring);
 }
@@ -3379,8 +3479,6 @@
   struct protoent *proto;
   char *hostname;
 
-  signal(SIGPIPE, SIG_IGN);
-
   /* serv = getservbyname(SKK_SERVICENAME, "tcp"); */
   memset((char*)&hostaddr, 0, sizeof(struct sockaddr_in));
   if ((proto = getprotobyname("tcp")) == NULL) {



More information about the uim-commit mailing list