Skip to content

Commit 3ee79e8

Browse files
yuguiaycabta
authored andcommitted
* lib/irb/init.rb (IRB.opt_parse): (M17N) adds -U and -E as command
line options. [ruby-dev:37161]. Fixes #711. improved long optinos. * lib/irb/init.rb (IRB.set_encoding): new subroutine for IRB.opt_parse * lib/irb/input-method.rb (IRB::StdioInputMethod): (M17N) regards scripts that user types as encoded in the external_encoding. * lib/irb/input-method.rb (IRB::ReadlineInputMethod): ditto. * lib/irb/input-method.rb (IRB::FileInputMethod): (M17N) respects magic comment. * lib/irb/help.rb (IRB.print_usage): (M17N) respects magic comment in the resource file. * lib/irb/lc/help-message: adds -U and -E. * lib/irb/lc/ja/help-message: ditto. re-encoded from ISO-2022-JP into UTF-8. * lib/irb/lc/ja/encoding_aliases.rb: new file. provides Japanese specific character encoding name table for backward compatibility. * lib/irb/lc/ja/error.rb: re-eoncoded from ISO-2022-JP into UTF-8. magic comment. * lib/irb/locale.rb: integrated with Ruby 1.9's M17N support. * lib/irb/magic-file.rb: new file. utility to handle files with magic comment and opens in the correct encoding. * lib/irb/ruby-lex.rb (RubyLex#each_top_level_statement): recovers character encoding for a statement after it lexed so that irb can eval the statement in correct encoding. * lib/irb/src_encoding.rb: new file. utility. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20862 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent a1fd03f commit 3ee79e8

File tree

11 files changed

+232
-108
lines changed

11 files changed

+232
-108
lines changed

lib/irb/help.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@
99
#
1010
#
1111

12+
require 'irb/magic-file'
13+
1214
module IRB
1315
def IRB.print_usage
1416
lc = IRB.conf[:LC_MESSAGES]
1517
path = lc.find("irb/help-message")
1618
space_line = false
17-
File.foreach(path) do
18-
|l|
19-
if /^\s*$/ =~ l
20-
lc.puts l unless space_line
21-
space_line = true
22-
next
19+
IRB::MagicFile.open(path){|f|
20+
f.each_line do |l|
21+
if /^\s*$/ =~ l
22+
lc.puts l unless space_line
23+
space_line = true
24+
next
25+
end
26+
space_line = false
27+
28+
l.sub!(/#.*$/, "")
29+
next if /^\s*$/ =~ l
30+
lc.puts l
2331
end
24-
space_line = false
25-
26-
l.sub!(/#.*$/, "")
27-
next if /^\s*$/ =~ l
28-
lc.puts l
29-
end
32+
}
3033
end
3134
end
3235

lib/irb/init.rb

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ def IRB.parse_opts
139139
when /^-I(.+)?/
140140
opt = $1 || ARGV.shift
141141
load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
142+
when '-U'
143+
set_encoding("UTF-8", "UTF-8")
144+
when /^-E(.+)?/, /^--encoding(?:=(.+))?/
145+
opt = $1 || ARGV.shift
146+
set_encoding(*opt.split(':', 2))
142147
when "--inspect"
143148
@CONF[:INSPECT_MODE] = true
144149
when "--noinspect"
@@ -155,8 +160,9 @@ def IRB.parse_opts
155160
@CONF[:VERBOSE] = true
156161
when "--noverbose"
157162
@CONF[:VERBOSE] = false
158-
when "--prompt-mode", "--prompt"
159-
prompt_mode = ARGV.shift.upcase.tr("-", "_").intern
163+
when /^--prompt-mode(?:=(.+))?/, /^--prompt(?:=(.+))?/
164+
opt = $1 || ARGV.shift
165+
prompt_mode = opt.upcase.tr("-", "_").intern
160166
@CONF[:PROMPT_MODE] = prompt_mode
161167
when "--noprompt"
162168
@CONF[:PROMPT_MODE] = :NULL
@@ -166,21 +172,27 @@ def IRB.parse_opts
166172
@CONF[:PROMPT_MODE] = :SIMPLE
167173
when "--tracer"
168174
@CONF[:USE_TRACER] = true
169-
when "--back-trace-limit"
170-
@CONF[:BACK_TRACE_LIMIT] = ARGV.shift.to_i
171-
when "--context-mode"
172-
@CONF[:CONTEXT_MODE] = ARGV.shift.to_i
175+
when /^--back-trace-limit(?:=(.+))?/
176+
@CONF[:BACK_TRACE_LIMIT] = ($1 || ARGV.shift).to_i
177+
when /^--context-mode(?:=(.+))?/
178+
@CONF[:CONTEXT_MODE] = ($1 || ARGV.shift).to_i
173179
when "--single-irb"
174180
@CONF[:SINGLE_IRB] = true
175-
when "--irb_debug"
176-
@CONF[:DEBUG_LEVEL] = ARGV.shift.to_i
181+
when /^--irb_debug=(?:=(.+))?/
182+
@CONF[:DEBUG_LEVEL] = ($1 || ARGV.shift).to_i
177183
when "-v", "--version"
178184
print IRB.version, "\n"
179185
exit 0
180186
when "-h", "--help"
181187
require "irb/help"
182188
IRB.print_usage
183189
exit 0
190+
when "--"
191+
if opt = ARGV.shfit
192+
@CONF[:SCRIPT] = opt
193+
$0 = opt
194+
end
195+
break
184196
when /^-/
185197
IRB.fail UnrecognizedSwitch, opt
186198
else
@@ -195,6 +207,7 @@ def IRB.parse_opts
195207
end
196208
end
197209
$LOAD_PATH.unshift(*load_path)
210+
198211
end
199212

200213
# running config
@@ -253,4 +266,21 @@ def IRB.load_modules
253266
end
254267
end
255268

269+
270+
DefaultEncodings = Struct.new(:external, :internal)
271+
class << IRB
272+
private
273+
def set_encoding(extern, intern = nil)
274+
verbose, $VERBOSE = $VERBOSE, nil
275+
Encoding.default_external = extern unless extern.nil? || extern.empty?
276+
Encoding.default_internal = intern unless intern.nil? || intern.empty?
277+
@CONF[:ENCODINGS] = IRB::DefaultEncodings.new(extern, intern)
278+
[$stdin, $stdout, $stderr].each do |io|
279+
io.set_encoding(extern, intern)
280+
end
281+
@CONF[:LC_MESSAGES].instance_variable_set(:@encoding, extern)
282+
ensure
283+
$VERBOSE = verbose
284+
end
285+
end
256286
end

lib/irb/input-method.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#
99
#
1010
#
11+
require 'irb/src_encoding'
12+
require 'irb/magic-file'
13+
1114
module IRB
1215
#
1316
# InputMethod
@@ -41,15 +44,19 @@ def initialize
4144
super
4245
@line_no = 0
4346
@line = []
47+
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
48+
@stdout = IO.open(STDOUT.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
4449
end
4550

4651
def gets
4752
print @prompt
48-
@line[@line_no += 1] = $stdin.gets
53+
line = @stdin.gets
54+
p line.encoding
55+
@line[@line_no += 1] = line
4956
end
5057

5158
def eof?
52-
$stdin.eof?
59+
@stdin.eof?
5360
end
5461

5562
def readable_atfer_eof?
@@ -59,12 +66,16 @@ def readable_atfer_eof?
5966
def line(line_no)
6067
@line[line_no]
6168
end
69+
70+
def encoding
71+
@stdin.external_encoding
72+
end
6273
end
6374

6475
class FileInputMethod < InputMethod
6576
def initialize(file)
6677
super
67-
@io = open(file)
78+
@io = IRB::MagicFile.open(file)
6879
end
6980
attr_reader :file_name
7081

@@ -78,6 +89,10 @@ def gets
7889
# print @prompt, l
7990
l
8091
end
92+
93+
def encoding
94+
@io.external_encoding
95+
end
8196
end
8297

8398
begin
@@ -90,11 +105,14 @@ def initialize
90105
@line_no = 0
91106
@line = []
92107
@eof = false
108+
109+
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
110+
@stdout = IO.open(STDOUT.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
93111
end
94112

95113
def gets
96-
Readline.input = STDIN
97-
Readline.output = STDOUT
114+
Readline.input = @stdin
115+
Readline.output = @stdout
98116
if l = readline(@prompt, false)
99117
HISTORY.push(l) if !l.empty?
100118
@line[@line_no += 1] = l + "\n"
@@ -115,6 +133,10 @@ def readable_atfer_eof?
115133
def line(line_no)
116134
@line[line_no]
117135
end
136+
137+
def encoding
138+
@stdin.external_encoding
139+
end
118140
end
119141
rescue LoadError
120142
end

lib/irb/lc/help-message

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: US-ASCII -*-
12
#
23
# irb/lc/help-message.rb -
34
# $Release Version: 0.9.5$
@@ -14,6 +15,8 @@ Usage: irb.rb [options] [programfile] [arguments]
1415
-d Set $DEBUG to true (same as `ruby -d')
1516
-r load-module Same as `ruby -r'
1617
-I path Specify $LOAD_PATH directory
18+
-U Same as `ruby -U`
19+
-E enc Same as `ruby -E`
1720
--inspect Use `inspect' for output (default except for bc mode)
1821
--noinspect Don't use inspect for output
1922
--readline Use Readline extension module
@@ -32,3 +35,4 @@ Usage: irb.rb [options] [programfile] [arguments]
3235
value is 16.
3336
--irb_debug n Set internal debug level to n (not for popular use)
3437
-v, --version Print the version of irb
38+
# vim:fileencoding=us-ascii

lib/irb/lc/ja/encoding_aliases.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module IRB
2+
class Locale
3+
@@legacy_encoding_alias_map = {
4+
'ujis' => Encoding::EUC_JP,
5+
'euc' => Encoding::EUC_JP
6+
}.freeze
7+
end
8+
end

lib/irb/lc/ja/error.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#
1+
# -*- coding: utf-8 -*-
22
# irb/lc/ja/error.rb -
33
# $Release Version: 0.9.5$
44
# $Revision$
@@ -13,14 +13,15 @@
1313
module IRB
1414
# exceptions
1515
extend Exception2MessageMapper
16-
def_exception :UnrecognizedSwitch, '$B%9%$%C%A(B(%s)$B$,J,$j$^$;$s(B'
17-
def_exception :NotImplementedError, '`%s\'$B$NDj5A$,I,MW$G$9(B'
18-
def_exception :CantReturnToNormalMode, 'Normal$B%b!<%I$KLa$l$^$;$s(B.'
19-
def_exception :IllegalParameter, '$B%Q%i%a!<%?(B(%s)$B$,4V0c$C$F$$$^$9(B.'
20-
def_exception :IrbAlreadyDead, 'Irb$B$O4{$K;`$s$G$$$^$9(B.'
21-
def_exception :IrbSwitchedToCurrentThread, '$B%+%l%s%H%9%l%C%I$K@Z$jBX$o$j$^$7$?(B.'
22-
def_exception :NoSuchJob, '$B$=$N$h$&$J%8%g%V(B(%s)$B$O$"$j$^$;$s(B.'
23-
def_exception :CantShiftToMultiIrbMode, 'multi-irb mode$B$K0\$l$^$;$s(B.'
24-
def_exception :CantChangeBinding, '$B%P%$%s%G%#%s%0(B(%s)$B$KJQ99$G$-$^$;$s(B.'
25-
def_exception :UndefinedPromptMode, '$B%W%m%s%W%H%b!<%I(B(%s)$B$ODj5A$5$l$F$$$^$;$s(B.'
16+
def_exception :UnrecognizedSwitch, 'スイッチ(%s)が分りません'
17+
def_exception :NotImplementedError, '`%s\'の定義が必要です'
18+
def_exception :CantReturnToNormalMode, 'Normalモードに戻れません.'
19+
def_exception :IllegalParameter, 'パラメータ(%s)が間違っています.'
20+
def_exception :IrbAlreadyDead, 'Irbは既に死んでいます.'
21+
def_exception :IrbSwitchedToCurrentThread, 'カレントスレッドに切り替わりました.'
22+
def_exception :NoSuchJob, 'そのようなジョブ(%s)はありません.'
23+
def_exception :CantShiftToMultiIrbMode, 'multi-irb modeに移れません.'
24+
def_exception :CantChangeBinding, 'バインディング(%s)に変更できません.'
25+
def_exception :UndefinedPromptMode, 'プロンプトモード(%s)は定義されていません.'
2626
end
27+
# vim:fileencoding=utf-8

lib/irb/lc/ja/help-message

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#
1+
# -*- coding: utf-8 -*-
22
# irb/lc/ja/help-message.rb -
33
# $Release Version: 0.9.5$
44
# $Revision$
@@ -9,27 +9,31 @@
99
#
1010
#
1111
Usage: irb.rb [options] [programfile] [arguments]
12-
-f ~/.irbrc $B$rFI$_9~$^$J$$(B.
13-
-m bc$B%b!<%I(B($BJ,?t(B, $B9TNs$N7W;;$,$G$-$k(B)
14-
-d $DEBUG $B$r(Btrue$B$K$9$k(B(ruby -d $B$HF1$8(B)
15-
-r load-module ruby -r $B$HF1$8(B.
16-
-I path $LOAD_PATH $B$K(B path $B$rDI2C$9$k(B.
17-
--inspect $B7k2L=PNO$K(Binspect$B$rMQ$$$k(B(bc$B%b!<%I0J30$O%G%U%)%k%H(B).
18-
--noinspect $B7k2L=PNO$K(Binspect$B$rMQ$$$J$$(B.
19-
--readline readline$B%i%$%V%i%j$rMxMQ$9$k(B.
20-
--noreadline readline$B%i%$%V%i%j$rMxMQ$7$J$$(B.
12+
-f ~/.irbrc を読み込まない.
13+
-m bcモード(分数, 行列の計算ができる)
14+
-d $DEBUG をtrueにする(ruby -d と同じ)
15+
-r load-module ruby -r と同じ.
16+
-I path $LOAD_PATH に path を追加する.
17+
-U ruby -U と同じ.
18+
-E enc ruby -E と同じ.
19+
--inspect 結果出力にinspectを用いる(bcモード以外はデフォルト).
20+
--noinspect 結果出力にinspectを用いない.
21+
--readline readlineライブラリを利用する.
22+
--noreadline readlineライブラリを利用しない.
2123
--prompt prompt-mode/--prompt-mode prompt-mode
22-
$B%W%m%s%W%H%b!<%I$r@ZBX$($^$9(B. $B8=:_Dj5A$5$l$F$$$k%W(B
23-
$B%m%s%W%H%b!<%I$O(B, default, simple, xmp, inf-ruby$B$,(B
24-
$BMQ0U$5$l$F$$$^$9(B.
25-
--inf-ruby-mode emacs$B$N(Binf-ruby-mode$BMQ$N%W%m%s%W%HI=<($r9T$J$&(B. $BFC(B
26-
$B$K;XDj$,$J$$8B$j(B, readline$B%i%$%V%i%j$O;H$o$J$/$J$k(B.
27-
--simple-prompt $BHs>o$K%7%s%W%k$J%W%m%s%W%H$rMQ$$$k%b!<%I$G$9(B.
28-
--noprompt $B%W%m%s%W%HI=<($r9T$J$o$J$$(B.
29-
--tracer $B%3%^%s%I<B9T;~$K%H%l!<%9$r9T$J$&(B.
24+
プロンプトモードを切替えます. 現在定義されているプ
25+
ロンプトモードは, default, simple, xmp, inf-rubyが
26+
用意されています.
27+
--inf-ruby-mode emacsのinf-ruby-mode用のプロンプト表示を行なう. 特
28+
に指定がない限り, readlineライブラリは使わなくなる.
29+
--simple-prompt 非常にシンプルなプロンプトを用いるモードです.
30+
--noprompt プロンプト表示を行なわない.
31+
--tracer コマンド実行時にトレースを行なう.
3032
--back-trace-limit n
31-
$B%P%C%/%H%l!<%9I=<($r%P%C%/%H%l!<%9$NF,$+$i(B n, $B8e$m(B
32-
$B$+$i(Bn$B$@$19T$J$&(B. $B%G%U%)%k%H$O(B16
33-
--irb_debug n irb$B$N%G%P%C%0%G%P%C%0%l%Y%k$r(Bn$B$K@_Dj$9$k(B($BMxMQ$7$J(B
34-
$B$$J}$,L5Fq$G$7$g$&(B).
35-
-v, --version irb$B$N%P!<%8%g%s$rI=<($9$k(B
33+
バックトレース表示をバックトレースの頭から n, 後ろ
34+
からnだけ行なう. デフォルトは16
35+
--irb_debug n irbのデバッグデバッグレベルをnに設定する(利用しな
36+
い方が無難でしょう).
37+
-v, --version irbのバージョンを表示する
38+
39+
# vim:fileencoding=utf-8

0 commit comments

Comments
 (0)