2 # irb/init.rb - irb initialize module
3 # $Release Version: 0.9.5$
5 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
15 def IRB.setup(ap_path)
16 IRB.init_config(ap_path)
22 unless @CONF[:PROMPT][@CONF[:PROMPT_MODE]]
23 IRB.fail(UndefinedPromptMode, @CONF[:PROMPT_MODE])
27 # @CONF default setting
28 def IRB.init_config(ap_path)
29 # class instance variables
30 @TRACER_INITIALIZED = false
32 # default configurations
33 unless ap_path and @CONF[:AP_NAME]
34 ap_path = File.join(File.dirname(File.dirname(__FILE__)), "irb.rb")
36 @CONF[:AP_NAME] = File::basename(ap_path, ".rb")
38 @CONF[:IRB_NAME] = "irb"
39 @CONF[:IRB_LIB_PATH] = File.dirname(__FILE__)
42 @CONF[:LOAD_MODULES] = []
45 @CONF[:MATH_MODE] = false
46 @CONF[:USE_READLINE] = false unless defined?(ReadlineInputMethod)
47 @CONF[:INSPECT_MODE] = nil
48 @CONF[:USE_TRACER] = false
49 @CONF[:USE_LOADER] = false
50 @CONF[:IGNORE_SIGINT] = true
51 @CONF[:IGNORE_EOF] = false
55 @CONF[:EVAL_HISTORY] = nil
56 @CONF[:SAVE_HISTORY] = nil
58 @CONF[:BACK_TRACE_LIMIT] = 16
69 :PROMPT_I => "%N(%m):%03n:%i> ",
70 :PROMPT_N => "%N(%m):%03n:%i> ",
71 :PROMPT_S => "%N(%m):%03n:%i%l ",
72 :PROMPT_C => "%N(%m):%03n:%i* ",
76 :PROMPT_I => "%N(%m):%03n:%i> ",
77 :PROMPT_N => "%N(%m):%03n:%i> ",
78 :PROMPT_S => "%N(%m):%03n:%i%l ",
79 :PROMPT_C => "%N(%m):%03n:%i* ",
90 :PROMPT_I => "%N(%m):%03n:%i> ",
91 # :PROMPT_N => "%N(%m):%03n:%i> ",
103 :RETURN => " ==>%s\n"
107 @CONF[:PROMPT_MODE] = (STDIN.tty? ? :DEFAULT : :NULL)
108 @CONF[:AUTO_INDENT] = false
110 @CONF[:CONTEXT_MODE] = 3 # use binding in function on TOPLEVEL_BINDING
111 @CONF[:SINGLE_IRB] = false
113 # @CONF[:LC_MESSAGES] = "en"
114 @CONF[:LC_MESSAGES] = Locale.new
116 @CONF[:DEBUG_LEVEL] = 1
120 @CONF[:LC_MESSAGES].load("irb/error.rb")
123 FEATURE_IOPT_CHANGE_VERSION = "1.9.0"
128 while opt = ARGV.shift
133 @CONF[:MATH_MODE] = true
137 opt = $1 || ARGV.shift
138 @CONF[:LOAD_MODULES].push opt if opt
140 opt = $1 || ARGV.shift
141 load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
143 @CONF[:INSPECT_MODE] = true
145 @CONF[:INSPECT_MODE] = false
147 @CONF[:USE_READLINE] = true
149 @CONF[:USE_READLINE] = false
155 @CONF[:VERBOSE] = true
157 @CONF[:VERBOSE] = false
158 when "--prompt-mode", "--prompt"
159 prompt_mode = ARGV.shift.upcase.tr("-", "_").intern
160 @CONF[:PROMPT_MODE] = prompt_mode
162 @CONF[:PROMPT_MODE] = :NULL
163 when "--inf-ruby-mode"
164 @CONF[:PROMPT_MODE] = :INF_RUBY
165 when "--sample-book-mode", "--simple-prompt"
166 @CONF[:PROMPT_MODE] = :SIMPLE
168 @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
174 @CONF[:SINGLE_IRB] = true
176 @CONF[:DEBUG_LEVEL] = ARGV.shift.to_i
177 when "-v", "--version"
178 print IRB.version, "\n"
185 IRB.fail UnrecognizedSwitch, opt
192 if RUBY_VERSION >= FEATURE_IOPT_CHANGE_VERSION
193 load_path.collect! do |path|
194 /\A\.\// =~ path ? path : File.expand_path(path)
197 $LOAD_PATH.unshift(*load_path)
205 rescue LoadError, Errno::ENOENT
206 rescue # StandardError, ScriptError
207 print "load error: #{rc_file}\n"
208 print $!.class, ": ", $!, "\n"
209 for err in $@[0, $@.size - 2]
210 print "\t", err, "\n"
217 def IRB.rc_file(ext = IRBRC_EXT)
218 if !@CONF[:RC_NAME_GENERATOR]
219 rc_file_generators do |rcgen|
220 @CONF[:RC_NAME_GENERATOR] ||= rcgen
221 if File.exist?(rcgen.call(IRBRC_EXT))
222 @CONF[:RC_NAME_GENERATOR] = rcgen
227 @CONF[:RC_NAME_GENERATOR].call ext
230 # enumerate possible rc-file base name generators
231 def IRB.rc_file_generators
232 if irbrc = ENV["IRBRC"]
233 yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc}
235 if home = ENV["HOME"]
236 yield proc{|rc| home+"/.irb#{rc}"}
239 yield proc{|rc| home+"/.irb#{rc}"}
240 yield proc{|rc| home+"/irb#{rc.sub(/\A_?/, '.')}"}
241 yield proc{|rc| home+"/_irb#{rc}"}
242 yield proc{|rc| home+"/$irb#{rc}"}
247 for m in @CONF[:LOAD_MODULES]
250 rescue # StandardError, ScriptError
251 print $@[0], ":", $!.class, ": ", $!, "\n"