なかだです。

At Mon, 4 Mar 2002 22:34:20 +0900,
Nobuyoshi-Nakada wrote:
> > なるほど。とりあえず合わせてみますか。ってかなり手抜き。

ONIGURUMA対応。

# 一条直也? 龍虎の拳?


Index: intern.h =================================================================== RCS file: /cvs/ruby/src/ruby/intern.h,v retrieving revision 1.81 diff -u -2 -p -r1.81 intern.h --- intern.h 2002/02/20 06:33:24 1.81 +++ intern.h 2002/03/03 10:13:34 @@ -332,4 +332,5 @@ void ruby_load_script _((void)); void ruby_init_loadpath _((void)); void ruby_incpush _((const char*)); +void ruby_modeset _((const char*, int)); /* signal.c */ VALUE rb_f_kill _((int, VALUE*)); Index: parse.y =================================================================== RCS file: /cvs/ruby/src/ruby/parse.y,v retrieving revision 1.156 diff -u -2 -p -r1.156 parse.y --- parse.y 2002/02/20 04:31:50 1.156 +++ parse.y 2002/03/04 12:59:09 @@ -2098,4 +2098,5 @@ yycompile(f, line) int n; NODE *node = 0; + const char *kcode_save; if (!compile_for_eval && rb_safe_level() == 0 && @@ -2121,4 +2122,5 @@ yycompile(f, line) } + kcode_save = rb_get_kcode(); ruby__end__seen = 0; ruby_eval_tree = 0; @@ -2137,4 +2139,5 @@ yycompile(f, line) in_def = 0; cur_mid = 0; + rb_set_kcode(kcode_save); if (n == 0) node = ruby_eval_tree; @@ -2985,7 +2988,7 @@ yylex() case '#': /* it's a comment */ - while ((c = nextc()) != '\n') { - if (c == -1) - return 0; + while (lex_p < lex_pend) { + ruby_modeset(lex_p, lex_pend - lex_p); + lex_p = lex_pend; } /* fall through */ Index: ruby.c =================================================================== RCS file: /cvs/ruby/src/ruby/ruby.c,v retrieving revision 1.56 diff -u -2 -p -r1.56 ruby.c --- ruby.c 2002/02/10 07:55:40 1.56 +++ ruby.c 2002/03/05 09:57:44 @@ -19,4 +19,5 @@ #include "dln.h" #include "node.h" +#include "regex.h" #include <stdio.h> #include <sys/types.h> @@ -335,5 +336,105 @@ require_libraries() } +/* emacsen -*- hack */ +typedef void (*rb_modeset_setter_t) _((const char *val, const char *name)); + +struct modeset { + const char *name; + rb_modeset_setter_t func; +}; + +static const struct modeset modesets[] = { + {"coding", (rb_modeset_setter_t)rb_set_kcode}, +}; + +static const char modeset_pattern[] = ".*-\\*-\\s*(.*?)\\s*-\\*-"; +static const char modeset_scan_pattern[] = +"([^\\s\'\":;]+)\\s*:\\s*(\"(?:\\\\.|[^\"])*\"|[^\"\\s;]+)[\\s;]*"; + +#ifdef ONIGURUMA_VERSION +static regex_t *modeset_re, *modeset_scan_re; +#else +static struct re_pattern_buffer modeset_re, modeset_scan_re; +#endif + static void +Init_modeset _((void)) +{ +#if ONIGURUMA_VERSION == 110 + RegCharEncoding enc; +# ifdef M17N_H + m17n_init(); + enc = *m17n_encoding_table; /* default encoding */ +# else + enc = REG_MBLEN_TABLE[MBCTYPE_ASCII]; +# endif +#define endof(array) ((array)+sizeof(array)) + RegexNew(&modeset_re, (UChar *)modeset_pattern, + (UChar *)endof(modeset_pattern) - 1, 0, enc, 0); + RegexNew(&modeset_scan_re, (UChar *)modeset_scan_pattern, + (UChar *)endof(modeset_scan_pattern) - 1, 0, enc, 0); +#else + re_compile_pattern(modeset_pattern, + sizeof(modeset_pattern) - 1, + &modeset_re); + re_compile_pattern(modeset_scan_pattern, + sizeof(modeset_scan_pattern) - 1, + &modeset_scan_re); +#endif +} + +void +ruby_modeset(str, len) + const char *str; + int len; +{ +#define str_copy(_s, _p, _n) ((_s) \ + ? (rb_str_resize((_s), (_n)), \ + MEMCPY(RSTRING(_s)->ptr, (_p), char, (_n)), (_s)) \ + : ((_s) = rb_str_new((_p), (_n)))) +#ifdef ONIGURUMA_VERSION +#define match(_re, _p, _n) (MEMZERO(&regs, struct re_registers, 1), \ + re_match((_re), (_p), (_n), 0, &regs)) +#else +#define match(_re, _p, _n) (MEMZERO(&regs, struct re_registers, 1), \ + re_match(&(_re), (_p), (_n), 0, &regs)) +#endif + struct re_registers regs; + VALUE name = 0, val = 0; + + if (match(modeset_re, str, len) < 0) return; + + len = regs.end[1] - regs.beg[1]; + str += regs.beg[1]; + re_free_registers(&regs); + + while (match(modeset_scan_re, str, len) >= 0) { + const struct modeset *p = modesets; + int e0 = regs.end[0]; + int n1 = regs.end[1] - regs.beg[1]; + int s2 = regs.beg[2]; + int n2 = regs.end[2] - s2; + re_free_registers(&regs); + + str_copy(name, str, n1); + rb_funcall(name, rb_intern("downcase!"), 0); + + do { + if (strncmp(p->name, RSTRING(name)->ptr, n1) == 0) { + str_copy(val, str + s2, n2); + (*p->func)(RSTRING(val)->ptr, RSTRING(name)->ptr); + break; + } + } while (++p < modesets + sizeof(modesets) / sizeof(*p)); + + len -= e0; + str += e0; + } + + if (name) rb_gc_force_recycle(name); + if (val) rb_gc_force_recycle(val); +} + +static void process_sflag() { @@ -843,4 +944,7 @@ load_file(fname, script) } } + if (RSTRING(line)->len > 1) { + ruby_modeset(RSTRING(line)->ptr, RSTRING(line)->len); + } } else if (!NIL_P(c)) { @@ -1007,4 +1111,6 @@ ruby_prog_init() rb_define_readonly_variable("$-a", &do_split); rb_global_variable(&rb_argv0); + + Init_modeset(); #ifdef MSDOS
-- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦