irb/input-method.rb - input methods used irb $Release Version: 0.9.5$ $Revision: 11708 $ $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $ by Keiju ISHITSUKA(keiju@ruby-lang.org)
—
FEATURE_IOPT_CHANGE_VERSION | = | "1.9.0" | ||
IRBRC_EXT | = | "rc" | ||
STDIN_FILE_NAME | = | "(line)" |
InputMethod
StdioInputMethod FileInputMethod (ReadlineInputMethod) |
# File lib/irb/ext/multi-irb.rb, line 143 143: def IRB.CurrentContext 144: IRB.JobManager.irb(Thread.current).context 145: end
@CONF default setting
# File lib/irb/init.rb, line 29 29: def IRB.init_config(ap_path) 30: # class instance variables 31: @TRACER_INITIALIZED = false 32: 33: # default configurations 34: unless ap_path and @CONF[:AP_NAME] 35: ap_path = File.join(File.dirname(File.dirname(__FILE__)), "irb.rb") 36: end 37: @CONF[:AP_NAME] = File::basename(ap_path, ".rb") 38: 39: @CONF[:IRB_NAME] = "irb" 40: @CONF[:IRB_LIB_PATH] = File.dirname(__FILE__) 41: 42: @CONF[:RC] = true 43: @CONF[:LOAD_MODULES] = [] 44: @CONF[:IRB_RC] = nil 45: 46: @CONF[:MATH_MODE] = false 47: @CONF[:USE_READLINE] = false unless defined?(ReadlineInputMethod) 48: @CONF[:INSPECT_MODE] = nil 49: @CONF[:USE_TRACER] = false 50: @CONF[:USE_LOADER] = false 51: @CONF[:IGNORE_SIGINT] = true 52: @CONF[:IGNORE_EOF] = false 53: @CONF[:ECHO] = nil 54: @CONF[:VERBOSE] = nil 55: 56: @CONF[:EVAL_HISTORY] = nil 57: @CONF[:SAVE_HISTORY] = nil 58: 59: @CONF[:BACK_TRACE_LIMIT] = 16 60: 61: @CONF[:PROMPT] = { 62: :NULL => { 63: :PROMPT_I => nil, 64: :PROMPT_N => nil, 65: :PROMPT_S => nil, 66: :PROMPT_C => nil, 67: :RETURN => "%s\n" 68: }, 69: :DEFAULT => { 70: :PROMPT_I => "%N(%m):%03n:%i> ", 71: :PROMPT_N => "%N(%m):%03n:%i> ", 72: :PROMPT_S => "%N(%m):%03n:%i%l ", 73: :PROMPT_C => "%N(%m):%03n:%i* ", 74: :RETURN => "=> %s\n" 75: }, 76: :CLASSIC => { 77: :PROMPT_I => "%N(%m):%03n:%i> ", 78: :PROMPT_N => "%N(%m):%03n:%i> ", 79: :PROMPT_S => "%N(%m):%03n:%i%l ", 80: :PROMPT_C => "%N(%m):%03n:%i* ", 81: :RETURN => "%s\n" 82: }, 83: :SIMPLE => { 84: :PROMPT_I => ">> ", 85: :PROMPT_N => ">> ", 86: :PROMPT_S => nil, 87: :PROMPT_C => "?> ", 88: :RETURN => "=> %s\n" 89: }, 90: :INF_RUBY => { 91: :PROMPT_I => "%N(%m):%03n:%i> ", 92: # :PROMPT_N => "%N(%m):%03n:%i> ", 93: :PROMPT_N => nil, 94: :PROMPT_S => nil, 95: :PROMPT_C => nil, 96: :RETURN => "%s\n", 97: :AUTO_INDENT => true 98: }, 99: :XMP => { 100: :PROMPT_I => nil, 101: :PROMPT_N => nil, 102: :PROMPT_S => nil, 103: :PROMPT_C => nil, 104: :RETURN => " ==>%s\n" 105: } 106: } 107: 108: @CONF[:PROMPT_MODE] = (STDIN.tty? ? :DEFAULT : :NULL) 109: @CONF[:AUTO_INDENT] = false 110: 111: @CONF[:CONTEXT_MODE] = 3 # use binding in function on TOPLEVEL_BINDING 112: @CONF[:SINGLE_IRB] = false 113: 114: # @CONF[:LC_MESSAGES] = "en" 115: @CONF[:LC_MESSAGES] = Locale.new 116: 117: @CONF[:DEBUG_LEVEL] = 1 118: end
# File lib/irb/init.rb, line 120 120: def IRB.init_error 121: @CONF[:LC_MESSAGES].load("irb/error.rb") 122: end
initialize tracing function
# File lib/irb/ext/tracer.rb, line 17 17: def IRB.initialize_tracer 18: Tracer.verbose = false 19: Tracer.add_filter { 20: |event, file, line, id, binding, *rests| 21: /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and 22: File::basename(file) != "irb.rb" 23: } 24: end
invoke multi-irb
# File lib/irb/ext/multi-irb.rb, line 148 148: def IRB.irb(file = nil, *main) 149: workspace = WorkSpace.new(*main) 150: parent_thread = Thread.current 151: Thread.start do 152: begin 153: irb = Irb.new(workspace, file) 154: rescue 155: print "Subirb can't start with context(self): ", workspace.main.inspect, "\n" 156: print "return to main irb\n" 157: Thread.pass 158: Thread.main.wakeup 159: Thread.exit 160: end 161: @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC] 162: @JobManager.insert(irb) 163: @JobManager.current_job = irb 164: begin 165: system_exit = false 166: catch(:IRB_EXIT) do 167: irb.eval_input 168: end 169: rescue SystemExit 170: system_exit = true 171: raise 172: #fail 173: ensure 174: unless system_exit 175: @JobManager.delete(irb) 176: if parent_thread.alive? 177: @JobManager.current_job = @JobManager.irb(parent_thread) 178: parent_thread.run 179: else 180: @JobManager.current_job = @JobManager.main_irb 181: @JobManager.main_thread.run 182: end 183: end 184: end 185: end 186: Thread.stop 187: @JobManager.current_job = @JobManager.irb(Thread.current) 188: end
# File lib/irb.rb, line 79 79: def IRB.irb_abort(irb, exception = Abort) 80: if defined? Thread 81: irb.context.thread.raise exception, "abort then interrupt!!" 82: else 83: raise exception, "abort then interrupt!!" 84: end 85: end
loading modules
# File lib/irb/init.rb, line 249 249: def IRB.load_modules 250: for m in @CONF[:LOAD_MODULES] 251: begin 252: require m 253: rescue 254: print $@[0], ":", $!.class, ": ", $!, "\n" 255: end 256: end 257: end
option analyzing
# File lib/irb/init.rb, line 127 127: def IRB.parse_opts 128: load_path = [] 129: while opt = ARGV.shift 130: case opt 131: when "-f" 132: @CONF[:RC] = false 133: when "-m" 134: @CONF[:MATH_MODE] = true 135: when "-d" 136: $DEBUG = true 137: when /^-r(.+)?/ 138: opt = $1 || ARGV.shift 139: @CONF[:LOAD_MODULES].push opt if opt 140: when /^-I(.+)?/ 141: opt = $1 || ARGV.shift 142: load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt 143: when /^-K(.)/ 144: $KCODE = $1 145: when "--inspect" 146: @CONF[:INSPECT_MODE] = true 147: when "--noinspect" 148: @CONF[:INSPECT_MODE] = false 149: when "--readline" 150: @CONF[:USE_READLINE] = true 151: when "--noreadline" 152: @CONF[:USE_READLINE] = false 153: when "--echo" 154: @CONF[:ECHO] = true 155: when "--noecho" 156: @CONF[:ECHO] = false 157: when "--verbose" 158: @CONF[:VERBOSE] = true 159: when "--noverbose" 160: @CONF[:VERBOSE] = false 161: when "--prompt-mode", "--prompt" 162: prompt_mode = ARGV.shift.upcase.tr("-", "_").intern 163: @CONF[:PROMPT_MODE] = prompt_mode 164: when "--noprompt" 165: @CONF[:PROMPT_MODE] = :NULL 166: when "--inf-ruby-mode" 167: @CONF[:PROMPT_MODE] = :INF_RUBY 168: when "--sample-book-mode", "--simple-prompt" 169: @CONF[:PROMPT_MODE] = :SIMPLE 170: when "--tracer" 171: @CONF[:USE_TRACER] = true 172: when "--back-trace-limit" 173: @CONF[:BACK_TRACE_LIMIT] = ARGV.shift.to_i 174: when "--context-mode" 175: @CONF[:CONTEXT_MODE] = ARGV.shift.to_i 176: when "--single-irb" 177: @CONF[:SINGLE_IRB] = true 178: when "--irb_debug" 179: @CONF[:DEBUG_LEVEL] = ARGV.shift.to_i 180: when "-v", "--version" 181: print IRB.version, "\n" 182: exit 0 183: when "-h", "--help" 184: require "irb/help" 185: IRB.print_usage 186: exit 0 187: when /^-/ 188: IRB.fail UnrecognizedSwitch, opt 189: else 190: @CONF[:SCRIPT] = opt 191: $0 = opt 192: break 193: end 194: end 195: if RUBY_VERSION >= FEATURE_IOPT_CHANGE_VERSION 196: load_path.collect! do |path| 197: /\A\.\// =~ path ? path : File.expand_path(path) 198: end 199: end 200: $LOAD_PATH.unshift(*load_path) 201: end
# File lib/irb/help.rb, line 14 14: def IRB.print_usage 15: lc = IRB.conf[:LC_MESSAGES] 16: path = lc.find("irb/help-message") 17: space_line = false 18: File.foreach(path) do 19: |l| 20: if /^\s*$/ =~ l 21: lc.puts l unless space_line 22: space_line = true 23: next 24: end 25: space_line = false 26: 27: l.sub!(/#.*$/, "") 28: next if /^\s*$/ =~ l 29: lc.puts l 30: end 31: end
# File lib/irb/init.rb, line 220 220: def IRB.rc_file(ext = IRBRC_EXT) 221: if !@CONF[:RC_NAME_GENERATOR] 222: rc_file_generators do |rcgen| 223: @CONF[:RC_NAME_GENERATOR] ||= rcgen 224: if File.exist?(rcgen.call(IRBRC_EXT)) 225: @CONF[:RC_NAME_GENERATOR] = rcgen 226: break 227: end 228: end 229: end 230: @CONF[:RC_NAME_GENERATOR].call ext 231: end
enumerate possible rc-file base name generators
# File lib/irb/init.rb, line 234 234: def IRB.rc_file_generators 235: if irbrc = ENV["IRBRC"] 236: yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc} 237: end 238: if home = ENV["HOME"] 239: yield proc{|rc| home+"/.irb#{rc}"} 240: end 241: home = Dir.pwd 242: yield proc{|rc| home+"/.irb#{rc}"} 243: yield proc{|rc| home+"/irb#{rc.sub(/\A_?/, '.')}"} 244: yield proc{|rc| home+"/_irb#{rc}"} 245: yield proc{|rc| home+"/$irb#{rc}"} 246: end
running config
# File lib/irb/init.rb, line 204 204: def IRB.run_config 205: if @CONF[:RC] 206: begin 207: load rc_file 208: rescue LoadError, Errno::ENOENT 209: rescue 210: print "load error: #{rc_file}\n" 211: print $!.class, ": ", $!, "\n" 212: for err in $@[0, $@.size - 2] 213: print "\t", err, "\n" 214: end 215: end 216: end 217: end
initialize config
# File lib/irb/init.rb, line 16 16: def IRB.setup(ap_path) 17: IRB.init_config(ap_path) 18: IRB.init_error 19: IRB.parse_opts 20: IRB.run_config 21: IRB.load_modules 22: 23: unless @CONF[:PROMPT][@CONF[:PROMPT_MODE]] 24: IRB.fail(UndefinedPromptMode, @CONF[:PROMPT_MODE]) 25: end 26: end
initialize IRB and start TOP_LEVEL irb
# File lib/irb.rb, line 51 51: def IRB.start(ap_path = nil) 52: $0 = File::basename(ap_path, ".rb") if ap_path 53: 54: IRB.setup(ap_path) 55: 56: if @CONF[:SCRIPT] 57: irb = Irb.new(nil, @CONF[:SCRIPT]) 58: else 59: irb = Irb.new 60: end 61: 62: @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC] 63: @CONF[:MAIN_CONTEXT] = irb.context 64: 65: trap("SIGINT") do 66: irb.signal_handle 67: end 68: 69: catch(:IRB_EXIT) do 70: irb.eval_input 71: end 72: # print "\n" 73: end