2 # irb/context.rb - irb context
3 # $Release Version: 0.9.5$
5 # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
6 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
12 require "irb/workspace"
18 # input_method: nil -- stdin or readline
20 # other -- using this as InputMethod
22 def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
25 @workspace = workspace
27 @workspace = WorkSpace.new
29 @thread = Thread.current if defined? Thread
32 # copy of default configuration
33 @ap_name = IRB.conf[:AP_NAME]
35 @load_modules = IRB.conf[:LOAD_MODULES]
37 @use_readline = IRB.conf[:USE_READLINE]
38 @inspect_mode = IRB.conf[:INSPECT_MODE]
40 self.math_mode = IRB.conf[:MATH_MODE] if IRB.conf[:MATH_MODE]
41 self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
42 self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
43 self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]
45 @ignore_sigint = IRB.conf[:IGNORE_SIGINT]
46 @ignore_eof = IRB.conf[:IGNORE_EOF]
48 @back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
50 self.prompt_mode = IRB.conf[:PROMPT_MODE]
52 if IRB.conf[:SINGLE_IRB] or !defined?(JobManager)
53 @irb_name = IRB.conf[:IRB_NAME]
55 @irb_name = "irb#"+IRB.JobManager.n_jobs.to_s
57 @irb_path = "(" + @irb_name + ")"
63 if (defined?(ReadlineInputMethod) && STDIN.tty? &&
64 IRB.conf[:PROMPT_MODE] != :INF_RUBY)
65 @io = ReadlineInputMethod.new
67 @io = StdioInputMethod.new
70 @io = StdioInputMethod.new
72 if defined?(ReadlineInputMethod)
73 @io = ReadlineInputMethod.new
75 @io = StdioInputMethod.new
80 @io = FileInputMethod.new(input_method)
81 @irb_name = File.basename(input_method)
82 @irb_path = input_method
86 self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]
89 @output_method = output_method
91 @output_method = StdioOutputMethod.new
94 @verbose = IRB.conf[:VERBOSE]
95 @echo = IRB.conf[:ECHO]
99 @debug_level = IRB.conf[:DEBUG_LEVEL]
106 attr_reader :workspace_home
107 attr_accessor :workspace
112 attr_accessor :ap_name
114 attr_accessor :load_modules
115 attr_accessor :irb_name
116 attr_accessor :irb_path
118 attr_reader :use_readline
119 attr_reader :inspect_mode
121 attr_reader :prompt_mode
122 attr_accessor :prompt_i
123 attr_accessor :prompt_s
124 attr_accessor :prompt_c
125 attr_accessor :prompt_n
126 attr_accessor :auto_indent_mode
127 attr_accessor :return_format
129 attr_accessor :ignore_sigint
130 attr_accessor :ignore_eof
132 attr_accessor :verbose
133 attr_reader :debug_level
135 attr_accessor :back_trace_limit
137 alias use_readline? use_readline
139 alias ignore_sigint? ignore_sigint
140 alias ignore_eof? ignore_eof
145 if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)
147 elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
156 verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
157 (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
160 attr_reader :last_value
162 def set_last_value(value)
164 @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
167 attr_reader :irb_name
169 def prompt_mode=(mode)
171 pconf = IRB.conf[:PROMPT][mode]
172 @prompt_i = pconf[:PROMPT_I]
173 @prompt_s = pconf[:PROMPT_S]
174 @prompt_c = pconf[:PROMPT_C]
175 @prompt_n = pconf[:PROMPT_N]
176 @return_format = pconf[:RETURN]
177 if ai = pconf.include?(:AUTO_INDENT)
178 @auto_indent_mode = ai
180 @auto_indent_mode = IRB.conf[:AUTO_INDENT]
185 @inspect_mode.nil? or @inspect_mode
189 @io.class == FileInputMethod
192 def inspect_mode=(opt)
196 @inspect_mode = !@inspect_mode
198 print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose?
202 def use_readline=(opt)
204 print "use readline module\n" if @use_readline
207 def debug_level=(value)
209 RubyLex.debug_level = value
210 SLex.debug_level = value
217 def evaluate(line, line_no)
219 set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
220 # @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._")
221 # @_ = @workspace.evaluate(line, irb_path, line_no)
226 IRB.irb_exit(@irb, ret)
229 NOPRINTING_IVARS = ["@last_value"]
230 NO_INSPECTING_IVARS = ["@irb", "@io"]
231 IDNAME_IVARS = ["@prompt_mode"]
233 alias __inspect__ inspect
236 for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
237 name = ivar.sub(/^@(.*)$/){$1}
238 val = instance_eval(ivar)
240 when *NOPRINTING_IVARS
241 array.push format("conf.%s=%s", name, "...")
242 when *NO_INSPECTING_IVARS
243 array.push format("conf.%s=%s", name, val.to_s)
245 array.push format("conf.%s=:%s", name, val.id2name)
247 array.push format("conf.%s=%s", name, val.inspect)