1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/irb/input-method.rb
yugui 5c1bd53c92 * 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
2008-12-18 13:09:26 +00:00

143 lines
2.5 KiB
Ruby

#
# irb/input-method.rb - input methods used irb
# $Release Version: 0.9.5$
# $Revision$
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
#
#
require 'irb/src_encoding'
require 'irb/magic-file'
module IRB
#
# InputMethod
# StdioInputMethod
# FileInputMethod
# (ReadlineInputMethod)
#
STDIN_FILE_NAME = "(line)"
class InputMethod
@RCS_ID='-$Id$-'
def initialize(file = STDIN_FILE_NAME)
@file_name = file
end
attr_reader :file_name
attr_accessor :prompt
def gets
IRB.fail NotImplementedError, "gets"
end
public :gets
def readable_atfer_eof?
false
end
end
class StdioInputMethod < InputMethod
def initialize
super
@line_no = 0
@line = []
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
@stdout = IO.open(STDOUT.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
end
def gets
print @prompt
line = @stdin.gets
p line.encoding
@line[@line_no += 1] = line
end
def eof?
@stdin.eof?
end
def readable_atfer_eof?
true
end
def line(line_no)
@line[line_no]
end
def encoding
@stdin.external_encoding
end
end
class FileInputMethod < InputMethod
def initialize(file)
super
@io = IRB::MagicFile.open(file)
end
attr_reader :file_name
def eof?
@io.eof?
end
def gets
print @prompt
l = @io.gets
# print @prompt, l
l
end
def encoding
@io.external_encoding
end
end
begin
require "readline"
class ReadlineInputMethod < InputMethod
include Readline
def initialize
super
@line_no = 0
@line = []
@eof = false
@stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
@stdout = IO.open(STDOUT.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
end
def gets
Readline.input = @stdin
Readline.output = @stdout
if l = readline(@prompt, false)
HISTORY.push(l) if !l.empty?
@line[@line_no += 1] = l + "\n"
else
@eof = true
l
end
end
def eof?
@eof
end
def readable_atfer_eof?
true
end
def line(line_no)
@line[line_no]
end
def encoding
@stdin.external_encoding
end
end
rescue LoadError
end
end