Change soft-fail to use the config, rather than env
[rbx.git] / lib / irb / context.rb
blob36e6069594559ca5bf28326ba0fb0f14a9b5ccae
2 #   irb/context.rb - irb context
3 #       $Release Version: 0.9.5$
4 #       $Revision: 11708 $
5 #       $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
6 #       by Keiju ISHITSUKA(keiju@ruby-lang.org)
8 # --
10 #   
12 require "irb/workspace"
14 module IRB
15   class Context
16     #
17     # Arguments:
18     #   input_method: nil -- stdin or readline
19     #                 String -- File
20     #                 other -- using this as InputMethod
21     #
22     def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
23       @irb = irb
24       if workspace
25         @workspace = workspace
26       else
27         @workspace = WorkSpace.new
28       end
29       @thread = Thread.current if defined? Thread
30 #      @irb_level = 0
32       # copy of default configuration
33       @ap_name = IRB.conf[:AP_NAME]
34       @rc = IRB.conf[:RC]
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]
49       
50       self.prompt_mode = IRB.conf[:PROMPT_MODE]
52       if IRB.conf[:SINGLE_IRB] or !defined?(JobManager)
53         @irb_name = IRB.conf[:IRB_NAME]
54       else
55         @irb_name = "irb#"+IRB.JobManager.n_jobs.to_s
56       end
57       @irb_path = "(" + @irb_name + ")"
59       case input_method
60       when nil
61         case use_readline?
62         when nil
63           if (defined?(ReadlineInputMethod) && STDIN.tty? &&
64               IRB.conf[:PROMPT_MODE] != :INF_RUBY)
65             @io = ReadlineInputMethod.new
66           else
67             @io = StdioInputMethod.new
68           end
69         when false
70           @io = StdioInputMethod.new
71         when true
72           if defined?(ReadlineInputMethod)
73             @io = ReadlineInputMethod.new
74           else
75             @io = StdioInputMethod.new
76           end
77         end
79       when String
80         @io = FileInputMethod.new(input_method)
81         @irb_name = File.basename(input_method)
82         @irb_path = input_method
83       else
84         @io = input_method
85       end
86       self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]
88       if output_method
89         @output_method = output_method
90       else
91         @output_method = StdioOutputMethod.new
92       end
94       @verbose = IRB.conf[:VERBOSE] 
95       @echo = IRB.conf[:ECHO]
96       if @echo.nil?
97         @echo = true
98       end
99       @debug_level = IRB.conf[:DEBUG_LEVEL]
100     end
102     def main
103       @workspace.main
104     end
106     attr_reader :workspace_home
107     attr_accessor :workspace
108     attr_reader :thread
109     attr_accessor :io
110     
111     attr_accessor :irb
112     attr_accessor :ap_name
113     attr_accessor :rc
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
131     attr_accessor :echo
132     attr_accessor :verbose
133     attr_reader :debug_level
135     attr_accessor :back_trace_limit
137     alias use_readline? use_readline
138     alias rc? rc
139     alias ignore_sigint? ignore_sigint
140     alias ignore_eof? ignore_eof
141     alias echo? echo
143     def verbose?
144       if @verbose.nil?
145         if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod) 
146           false
147         elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
148           true
149         else
150           false
151         end
152       end
153     end
155     def prompting?
156       verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
157                 (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
158     end
160     attr_reader :last_value
162     def set_last_value(value)
163       @last_value = value
164       @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
165     end
167     attr_reader :irb_name
169     def prompt_mode=(mode)
170       @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
179       else
180         @auto_indent_mode = IRB.conf[:AUTO_INDENT]
181       end
182     end
183     
184     def inspect?
185       @inspect_mode.nil? or @inspect_mode
186     end
188     def file_input?
189       @io.class == FileInputMethod
190     end
192     def inspect_mode=(opt)
193       if opt
194         @inspect_mode = opt
195       else
196         @inspect_mode = !@inspect_mode
197       end
198       print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose?
199       @inspect_mode
200     end
202     def use_readline=(opt)
203       @use_readline = opt
204       print "use readline module\n" if @use_readline
205     end
207     def debug_level=(value)
208       @debug_level = value
209       RubyLex.debug_level = value
210       SLex.debug_level = value
211     end
213     def debug?
214       @debug_level > 0
215     end
217     def evaluate(line, line_no)
218       @line_no = 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)
222     end
224     alias __exit__ exit
225     def exit(ret = 0)
226       IRB.irb_exit(@irb, ret)
227     end
229     NOPRINTING_IVARS = ["@last_value"]
230     NO_INSPECTING_IVARS = ["@irb", "@io"]
231     IDNAME_IVARS = ["@prompt_mode"]
233     alias __inspect__ inspect
234     def inspect
235       array = []
236       for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
237         name = ivar.sub(/^@(.*)$/){$1}
238         val = instance_eval(ivar)
239         case 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)
244         when *IDNAME_IVARS
245           array.push format("conf.%s=:%s", name, val.id2name)
246         else
247           array.push format("conf.%s=%s", name, val.inspect)
248         end
249       end
250       array.join("\n")
251     end
252     alias __to_s__ to_s
253     alias to_s inspect
254   end