2001-04-30 13:38:21 -04:00
|
|
|
#
|
|
|
|
# irb/context.rb - irb context
|
2002-07-09 07:17:17 -04:00
|
|
|
# $Release Version: 0.9$
|
2001-04-30 13:38:21 -04:00
|
|
|
# $Revision$
|
|
|
|
# $Date$
|
|
|
|
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
2002-07-09 07:17:17 -04:00
|
|
|
require "irb/workspace"
|
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
module IRB
|
|
|
|
class Context
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# input_method: nil -- stdin or readline
|
|
|
|
# String -- File
|
|
|
|
# other -- using this as InputMethod
|
|
|
|
#
|
|
|
|
def initialize(irb, workspace = nil, input_method = nil)
|
|
|
|
@irb = irb
|
|
|
|
if workspace
|
|
|
|
@workspace = workspace
|
|
|
|
else
|
2002-07-09 07:17:17 -04:00
|
|
|
@workspace = WorkSpace.new
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
@thread = Thread.current if defined? Thread
|
2002-07-09 07:17:17 -04:00
|
|
|
# @irb_level = 0
|
2001-04-30 13:38:21 -04:00
|
|
|
|
|
|
|
# copy of default configuration
|
|
|
|
@ap_name = IRB.conf[:AP_NAME]
|
|
|
|
@rc = IRB.conf[:RC]
|
|
|
|
@load_modules = IRB.conf[:LOAD_MODULES]
|
|
|
|
|
|
|
|
@use_readline = IRB.conf[:USE_READLINE]
|
|
|
|
@inspect_mode = IRB.conf[:INSPECT_MODE]
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
self.math_mode = IRB.conf[:MATH_MODE] if IRB.conf[:MATH_MODE]
|
|
|
|
self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRASER]
|
|
|
|
self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
|
|
|
|
self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVEL_HISTORY]
|
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
@ignore_sigint = IRB.conf[:IGNORE_SIGINT]
|
|
|
|
@ignore_eof = IRB.conf[:IGNORE_EOF]
|
|
|
|
|
|
|
|
@back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
self.prompt_mode = IRB.conf[:PROMPT_MODE]
|
2001-04-30 13:38:21 -04:00
|
|
|
|
|
|
|
if IRB.conf[:SINGLE_IRB] or !defined?(JobManager)
|
|
|
|
@irb_name = IRB.conf[:IRB_NAME]
|
|
|
|
else
|
|
|
|
@irb_name = "irb#"+IRB.JobManager.n_jobs.to_s
|
|
|
|
end
|
|
|
|
@irb_path = "(" + @irb_name + ")"
|
|
|
|
|
|
|
|
case input_method
|
|
|
|
when nil
|
2002-07-09 07:17:17 -04:00
|
|
|
if (use_readline.nil? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && STDIN.tty? ||
|
2001-04-30 13:38:21 -04:00
|
|
|
use_readline?)
|
|
|
|
@io = ReadlineInputMethod.new
|
|
|
|
else
|
|
|
|
@io = StdioInputMethod.new
|
|
|
|
end
|
|
|
|
when String
|
|
|
|
@io = FileInputMethod.new(input_method)
|
|
|
|
@irb_name = File.basename(input_method)
|
|
|
|
@irb_path = input_method
|
|
|
|
else
|
|
|
|
@io = input_method
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
|
|
|
|
@verbose = IRB.conf[:VERBOSE]
|
|
|
|
@echo = IRB.conf[:ECHO]
|
|
|
|
if @echo.nil?
|
|
|
|
@echo = true
|
|
|
|
end
|
|
|
|
@debug_level = IRB.conf[:DEBUG_LEVEL]
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def main
|
|
|
|
@workspace.main
|
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
attr_reader :workspace_home
|
2001-04-30 13:38:21 -04:00
|
|
|
attr_accessor :workspace
|
|
|
|
attr_reader :thread
|
|
|
|
attr_accessor :io
|
|
|
|
|
|
|
|
attr_accessor :irb
|
|
|
|
attr_accessor :ap_name
|
|
|
|
attr_accessor :rc
|
|
|
|
attr_accessor :load_modules
|
|
|
|
attr_accessor :irb_name
|
|
|
|
attr_accessor :irb_path
|
|
|
|
|
|
|
|
attr_accessor :use_readline
|
|
|
|
attr_reader :inspect_mode
|
|
|
|
|
|
|
|
attr_reader :prompt_mode
|
|
|
|
attr_accessor :prompt_i
|
|
|
|
attr_accessor :prompt_s
|
|
|
|
attr_accessor :prompt_c
|
|
|
|
attr_accessor :auto_indent_mode
|
|
|
|
attr_accessor :return_format
|
|
|
|
|
|
|
|
attr_accessor :ignore_sigint
|
|
|
|
attr_accessor :ignore_eof
|
2002-07-09 07:17:17 -04:00
|
|
|
attr_accessor :echo
|
|
|
|
attr_accessor :verbose
|
|
|
|
attr_reader :debug_level
|
2001-04-30 13:38:21 -04:00
|
|
|
|
|
|
|
attr_accessor :back_trace_limit
|
|
|
|
|
|
|
|
alias use_readline? use_readline
|
|
|
|
alias rc? rc
|
|
|
|
alias ignore_sigint? ignore_sigint
|
|
|
|
alias ignore_eof? ignore_eof
|
2002-07-09 07:17:17 -04:00
|
|
|
alias echo? echo
|
|
|
|
|
|
|
|
def verbose?
|
|
|
|
if @verbose.nil?
|
|
|
|
if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)
|
|
|
|
false
|
|
|
|
elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
def prompting?
|
|
|
|
verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
|
|
|
|
(defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
attr_reader :last_value
|
|
|
|
|
|
|
|
def set_last_value(value)
|
|
|
|
@last_value = value
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
attr_reader :irb_name
|
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
def prompt_mode=(mode)
|
|
|
|
@prompt_mode = mode
|
|
|
|
pconf = IRB.conf[:PROMPT][mode]
|
|
|
|
@prompt_i = pconf[:PROMPT_I]
|
|
|
|
@prompt_s = pconf[:PROMPT_S]
|
|
|
|
@prompt_c = pconf[:PROMPT_C]
|
|
|
|
@return_format = pconf[:RETURN]
|
|
|
|
if ai = pconf.include?(:AUTO_INDENT)
|
|
|
|
@auto_indent_mode = ai
|
|
|
|
else
|
|
|
|
@auto_indent_mode = IRB.conf[:AUTO_INDENT]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect?
|
2002-07-09 07:17:17 -04:00
|
|
|
@inspect_mode.nil? or @inspect_mode
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def file_input?
|
2002-10-02 12:45:35 -04:00
|
|
|
@io.class == FileInputMethod
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect_mode=(opt)
|
|
|
|
if opt
|
|
|
|
@inspect_mode = opt
|
|
|
|
else
|
|
|
|
@inspect_mode = !@inspect_mode
|
|
|
|
end
|
|
|
|
print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose?
|
|
|
|
@inspect_mode
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_readline=(opt)
|
|
|
|
@use_readline = opt
|
|
|
|
print "use readline module\n" if @use_readline
|
|
|
|
end
|
|
|
|
|
|
|
|
def debug_level=(value)
|
|
|
|
@debug_level = value
|
|
|
|
RubyLex.debug_level = value
|
|
|
|
SLex.debug_level = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def debug?
|
|
|
|
@debug_level > 0
|
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
def evaluate(line, line_no)
|
|
|
|
@line_no = line_no
|
|
|
|
set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
|
|
|
|
# @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._")
|
|
|
|
# @_ = @workspace.evaluate(line, irb_path, line_no)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
alias __exit__ exit
|
|
|
|
def exit(ret = 0)
|
2002-07-09 07:17:17 -04:00
|
|
|
IRB.irb_exit(@irb, ret)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
NOPRINTING_IVARS = ["@last_value"]
|
2001-04-30 13:38:21 -04:00
|
|
|
NO_INSPECTING_IVARS = ["@irb", "@io"]
|
|
|
|
IDNAME_IVARS = ["@prompt_mode"]
|
|
|
|
|
|
|
|
alias __inspect__ inspect
|
|
|
|
def inspect
|
|
|
|
array = []
|
|
|
|
for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
|
|
|
|
name = ivar.sub(/^@(.*)$/){$1}
|
|
|
|
val = instance_eval(ivar)
|
|
|
|
case ivar
|
|
|
|
when *NOPRINTING_IVARS
|
2002-07-09 07:17:17 -04:00
|
|
|
array.push format("conf.%s=%s", name, "...")
|
2001-04-30 13:38:21 -04:00
|
|
|
when *NO_INSPECTING_IVARS
|
|
|
|
array.push format("conf.%s=%s", name, val.to_s)
|
|
|
|
when *IDNAME_IVARS
|
|
|
|
array.push format("conf.%s=:%s", name, val.id2name)
|
|
|
|
else
|
|
|
|
array.push format("conf.%s=%s", name, val.inspect)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
array.join("\n")
|
|
|
|
end
|
|
|
|
alias __to_s__ to_s
|
|
|
|
alias to_s inspect
|
|
|
|
end
|
|
|
|
end
|