[#3747] constants (or class vriable?) — Wakou Aoyama <wakou@...>

青山です。

20 messages 1998/12/06
[#3751] Re: constants (or class vriable?) — Shin-ichro Hara <sinara@...> 1998/12/07

原です。

[#3763] Re: constants (or class vriable?) — Wakou Aoyama <wakou@...> 1998/12/07

青山です。

[#3764] Re: constants (or class vriable?) — matz@... (Yukihiro Matsumoto) 1998/12/08

まつもと ゆきひろです

[#3767] Re: constants (or class vriable?) — kjana@... (YANAGAWA Kazuhisa) 1998/12/08

In message <199812080034.JAA05946@picachu.netlab.co.jp>

[#3826] ruby 1.1d0 released — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

35 messages 1998/12/16

[#3873] (?: ) does not work? — shugo@... (MAEDA Shugo)

前田です。

15 messages 1998/12/19

[#3881] I want to catch all jump — shugo@... (Shugo Maeda)

前田です。

17 messages 1998/12/21
[#3895] Re: I want to catch all jump — matz@... (Yukihiro Matsumoto) 1998/12/22

まつもと ゆきひろです

[#3894] ruby 1.1d1 released — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

25 messages 1998/12/22

[#3899] interpreter reinitialization — shugo@... (Shugo Maeda)

前田です。

22 messages 1998/12/22
[#3923] Re: interpreter reinitialization — matz@... (Yukihiro Matsumoto) 1998/12/23

まつもと ゆきひろです

[#3926] Re: interpreter reinitialization — shugo@... (Shugo Maeda) 1998/12/23

前田です。

[#3931] Re: interpreter reinitialization — matz@... (Yukihiro Matsumoto) 1998/12/24

まつもと ゆきひろです

[#3956] Re: interpreter reinitialization — shugo@... (Shugo Maeda) 1998/12/24

前田です。

[#3960] Re: interpreter reinitialization — matz@... (Yukihiro Matsumoto) 1998/12/24

まつもと ゆきひろです

[#4002] config.guess — Koji Arai <JCA02266@...>

新井です。

24 messages 1998/12/26
[#4039] Re: config.guess — matz@... (Yukihiro Matsumoto) 1998/12/29

まつもと ゆきひろです

[#4045] Re: config.guess — Koji Arai <JCA02266@...> 1998/12/31

新井です。

[#4047] Re: config.guess — matz@... (Yukihiro Matsumoto) 1999/01/01

まつもと ゆきひろです

[#4048] Re: config.guess — Koji Arai <JCA02266@...> 1999/01/01

新井です。

[#4049] Re: config.guess — matz@... (Yukihiro Matsumoto) 1999/01/02

まつもと ゆきひろです

[#4050] Re: config.guess — Motoyuki Kasahara <m-kasahr@...> 1999/01/04

笠原です。

[#4030] module Precision — gotoken@... (GOTO Kentaro)

ごとけんです

78 messages 1998/12/28
[#4310] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/01/21

ごとけんです

[#4311] Re: module Precision — matz@... (Yukihiro Matsumoto) 1999/01/21

まつもと ゆきひろです

[#4312] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/01/21

ごとけんです

[#4317] Re: module Precision — keiju@... (石塚圭樹 ) 1999/01/21

けいじゅ@日本ラショナルソフトウェアです.

[#4364] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/01/25

ごとけんです

[#4478] Re: module Precision — matz@... (Yukihiro Matsumoto) 1999/01/28

まつもと ゆきひろです

[#4506] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/01/30

ごとけんです

[#4552] Re: module Precision — matz@... (Yukihiro Matsumoto) 1999/02/01

まつもと ゆきひろです

[#4557] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/02/01

ごとけんです

[#4632] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/02/03

ごとけんです

[#4647] Re: module Precision — keiju@... (石塚圭樹 ) 1999/02/03

けいじゅ@日本ラショナルソフトウェアです.

[#4648] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/02/03

ごとけんです

[#4633] Re: module Precision — matz@... (Yukihiro Matsumoto) 1999/02/03

まつもと ゆきひろです

[#4636] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/02/03

ごとけんです

[#4836] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/02/08

ごとけんです

[#4843] Re: module Precision — keiju@... (石塚圭樹 ) 1999/02/08

けいじゅ@日本ラショナルソフトウェアです.

[#4849] Re: module Precision — gotoken@... (GOTO Kentaro) 1999/02/08

ごとけんです

[#4924] Re: module Precision — keiju@... (石塚圭樹 ) 1999/02/09

けいじゅ@日本ラショナルソフトウェアです.

[#4976] a genericity — gotoken@... (GOTO Kentaro) 1999/02/10

ごとけんです

[#5008] Re: a genericity — keiju@... (石塚圭樹 ) 1999/02/11

けいじゅ@日本ラショナルソフトウェアです.

[#5018] Re: a genericity — gotoken@... (GOTO Kentaro) 1999/02/11

ごとけんです

[#5171] Re: a genericity — keiju@... (石塚圭樹 ) 1999/02/16

けいじゅ@日本ラショナルソフトウェアです.

[ruby-dev:3991] [patch] ext/readline/readline.c (Re: readline)

From: shugo@... (Shugo Maeda)
Date: 1998-12-25 16:34:20 UTC
List: ruby-dev #3991
前田です。

In message "[ruby-dev:3973] Re: readline (Re: ruby 1.1d0 released)"
Motoyuki Kasahara <m-kasahr@sra.co.jp> wrote:

> すみませんが、もう一つ二つ質問させて下さい。readline 組み込みの
> 
> 	filename_completion_function()
> 	username_completion_function()
> 
> といった補完関数に相当するものは指定できるのでしょうか。

現状ではできませんが、さっき実装しましたのでパッチをつけます。

Readline.completion_proc = Readline::USERNAME_COMPLETION_PROC

でユーザ名が補完されるようになります。

> あと、Proc オブジェクトに state が引数として渡されないようですが、渡し
> た方が良くありませんか?

Readlineモジュールではrl_completion_entry_functionではなく
rl_attempted_completion_functionを使っています。
ですので、渡すとしたらstateではなくstartとend(rl_line_buffer
内のtextの位置のindex)ですが、ほとんど使わない値を渡すために、

Readline.completion_proc = proc {|x,| ... }

のように|x,|で受けなければならなくなるのが嫌だったのでこのように
しています。

-- 
前田 修吾 (mailto:shugo@aianet.ne.jp)

Attachments (1)

readline.c.diff (5.77 KB, text/x-diff)
--- readline.c.orig	Fri Dec 25 13:12:25 1998
+++ readline.c	Sat Dec 26 01:10:14 1998
@@ -8,15 +8,13 @@
 #include "ruby.h"
 #include "rubysig.h"
 
-VALUE mReadline;
-static VALUE histary;
-
-static ID id_completion_proc;
-static ID id_completion_case_fold;
-static ID id_call;
+static VALUE mReadline;
 
 #define TOLOWER(c) (isupper(c) ? tolower(c) : c)
 
+#define COMPLETION_PROC "completion_proc"
+#define COMPLETION_CASE_FOLD "completion_case_fold"
+
 static int
 readline_event()
 {
@@ -34,8 +32,7 @@
     char *buff;
 
     if (rb_scan_args(argc, argv, "02", &tmp, &add_hist) > 0) {
-	Check_Type(tmp, T_STRING);
-	prompt = RSTRING(tmp)->ptr;
+	prompt = STR2CSTR(tmp);
     }
     buff = readline(prompt);
     if (RTEST(add_hist) && buff) {
@@ -52,27 +49,27 @@
 static VALUE
 readline_s_set_completion_proc(VALUE self, VALUE proc)
 {
-    if (proc != Qnil && !rb_obj_is_instance_of(proc, rb_cProc))
-	rb_raise(rb_eArgError, "argument needs to be proc or nil");
-    return rb_ivar_set(mReadline, id_completion_proc, proc);
+    if (!rb_respond_to(proc, rb_intern("call")))
+	rb_raise(rb_eArgError, "argument have to respond to `call'");
+    return rb_iv_set(mReadline, COMPLETION_PROC, proc);
 }
 
 static VALUE
 readline_s_get_completion_proc(VALUE self)
 {
-    return rb_ivar_get(mReadline, id_completion_proc);
+    return rb_iv_get(mReadline, COMPLETION_PROC);
 }
 
 static VALUE
 readline_s_set_completion_case_fold(VALUE self, VALUE val)
 {
-    return rb_ivar_set(mReadline, id_completion_case_fold, val);
+    return rb_iv_set(mReadline, COMPLETION_CASE_FOLD, val);
 }
 
 static VALUE
 readline_s_get_completion_case_fold(VALUE self)
 {
-    return rb_ivar_get(mReadline, id_completion_case_fold);
+    return rb_iv_get(mReadline, COMPLETION_CASE_FOLD);
 }
 
 static char **
@@ -83,26 +80,19 @@
     int case_fold;
     int i, matches;
 
-    proc = rb_ivar_get(mReadline, id_completion_proc);
-    if (!rb_obj_is_instance_of(proc, rb_cProc))
-	return NULL;
+    proc = rb_iv_get(mReadline, COMPLETION_PROC);
     rl_attempted_completion_over = 1;
-    case_fold = RTEST(rb_ivar_get(mReadline, id_completion_case_fold));
-    ary = rb_funcall(proc, id_call, 1, rb_str_new2(text));
-    if (!rb_obj_is_instance_of(ary, rb_cArray))
-	return NULL;
+    case_fold = RTEST(rb_iv_get(mReadline, COMPLETION_CASE_FOLD));
+    ary = rb_funcall(proc, rb_intern("call"), 1, rb_str_new2(text));
+    if (TYPE(ary) != T_ARRAY)
+	ary = rb_Array(ary);
     matches = RARRAY(ary)->len;
     if (matches == 0)
 	return NULL;
-    result = (char **) xmalloc(sizeof(char *) * (matches + 2));
+    result = ALLOC_N(char *, matches + 2);
     for (i = 0; i < matches; i++) {
-	temp = RARRAY(ary)->ptr[i];
-	if (!rb_obj_is_instance_of(temp, rb_cString)) {
-	    if (result) free(result);
-	    return NULL;
-	}
-	result[i + 1]
-	    = (char *) xmalloc(sizeof(char) * (RSTRING(temp)->len + 1));
+	temp = rb_obj_as_string(RARRAY(ary)->ptr[i]);
+	result[i + 1] = ALLOC_N(char, RSTRING(temp)->len + 1);
 	strcpy(result[i + 1], RSTRING(temp)->ptr);
     }
     result[matches + 1] = NULL;
@@ -134,8 +124,8 @@
 	    if (low > si) low = si;
 	    i++;
 	}
-	result[0] = xmalloc (low + 1);
-	strncpy (result[0], result[1], low);
+	result[0] = ALLOC_N(char, low + 1);
+	strncpy(result[0], result[1], low);
 	result[0][low] = '\0';
     }
 
@@ -187,16 +177,14 @@
     if (i < 0 || i > state->length - 1) {
 	rb_raise(rb_eIndexError, "Invalid index");
     }
-    Check_Type(str, T_STRING);
-    replace_history_entry(i, RSTRING(str)->ptr, NULL);
+    replace_history_entry(i, STR2CSTR(str), NULL);
     return str;
 }
 
 static VALUE
 hist_push(VALUE self, VALUE str)
 {
-    Check_Type(str, T_STRING);
-    add_history(RSTRING(str)->ptr);
+    add_history(STR2CSTR(str));
     return self;
 }
 
@@ -208,8 +196,7 @@
     
     while (argc--) {
 	str = *argv++;
-	Check_Type(str, T_STRING);
-	add_history(RSTRING(str)->ptr);
+	add_history(STR2CSTR(str));
     }
     return self;
 }
@@ -294,14 +281,48 @@
     return rb_str_new2(entry->line);
 }
 
+static VALUE
+filename_completion_proc_call(VALUE self, VALUE str)
+{
+    VALUE result = rb_ary_new();
+    char **matches;
+    int i;
+
+    matches = completion_matches(STR2CSTR(str),
+				 filename_completion_function);
+    for (i = 0; matches[i]; i++) {
+	rb_ary_push(result, rb_str_new2(matches[i]));
+	free(matches[i]);
+    }
+    free(matches);
+    if (RARRAY(result)->len >= 2)
+	rb_ary_shift(result);
+    return result;
+}
+
+static VALUE
+username_completion_proc_call(VALUE self, VALUE str)
+{
+    VALUE result = rb_ary_new();
+    char **matches;
+    int i;
+
+    matches = completion_matches(STR2CSTR(str),
+				 username_completion_function);
+    for (i = 0; matches[i]; i++) {
+	rb_ary_push(result, rb_str_new2(matches[i]));
+	free(matches[i]);
+    }
+    free(matches);
+    if (RARRAY(result)->len >= 2)
+	rb_ary_shift(result);
+    return result;
+}
+
 void
 Init_readline(void)
 {
-    extern VALUE rb_mEnumerable;
-
-    id_completion_proc = rb_intern("completion_proc");
-    id_completion_case_fold = rb_intern("completion_case_fold");
-    id_call = rb_intern("call");
+    VALUE histary, fcomp, ucomp;
 
     using_history();
 
@@ -335,6 +356,16 @@
     rb_define_singleton_method(histary,"empty?", hist_empty_p, 0);
     rb_define_singleton_method(histary,"delete_at", hist_delete_at, 1);
     rb_define_const(mReadline, "HISTORY", histary);
+
+    fcomp = rb_obj_alloc(rb_cObject);
+    rb_define_singleton_method(fcomp, "call",
+			       filename_completion_proc_call, 1);
+    rb_define_const(mReadline, "FILENAME_COMPLETION_PROC", fcomp);
+
+    ucomp = rb_obj_alloc(rb_cObject);
+    rb_define_singleton_method(ucomp, "call",
+			       username_completion_proc_call, 1);
+    rb_define_const(mReadline, "USERNAME_COMPLETION_PROC", ucomp);
 
     rl_attempted_completion_function
 	= (CPPFunction *) readline_attempted_completion_function;

In This Thread