lib/irb/init.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   irb/init.rb - irb initialize module
   3  #       $Release Version: 0.9$
   4  #       $Revision: 1.3 $
   5  #       $Date: 2002/07/09 11:17:16 $
   6  #       by Keiju ISHITSUKA(keiju@ishitsuka.com)
   7  #
   8  # --
   9  #
  10  #   
  11  #
  12  
  13  module IRB
  14  
  15    # initialize config
  16    def IRB.initialize(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
  27  
  28    # @CONF default setting
  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  
  58      @CONF[:BACK_TRACE_LIMIT] = 16
  59  
  60      @CONF[:PROMPT] = {
  61        :NULL => {
  62          :PROMPT_I => nil,
  63          :PROMPT_S => nil,
  64          :PROMPT_C => nil,
  65          :RETURN => "%s\n"
  66        },
  67        :DEFAULT => {
  68          :PROMPT_I => "%N(%m):%03n:%i> ",
  69          :PROMPT_S => "%N(%m):%03n:%i%l ",
  70          :PROMPT_C => "%N(%m):%03n:%i* ",
  71          :RETURN => "%s\n"
  72        },
  73        :SIMPLE => {
  74          :PROMPT_I => ">> ",
  75          :PROMPT_S => nil,
  76          :PROMPT_C => "?> ",
  77          :RETURN => "=> %s\n"
  78        },
  79        :INF_RUBY => {
  80          :PROMPT_I => "%N(%m):%03n:%i> ",
  81          :PROMPT_S => nil,
  82          :PROMPT_C => nil,
  83          :RETURN => "%s\n",
  84          :AUTO_INDENT => true
  85        },
  86        :XMP => {
  87          :PROMPT_I => nil,
  88          :PROMPT_S => nil,
  89          :PROMPT_C => nil,
  90          :RETURN => "    ==>%s\n"
  91        }
  92      }
  93  
  94      @CONF[:PROMPT_MODE] = :DEFAULT
  95      @CONF[:AUTO_INDENT] = false
  96  
  97      @CONF[:CONTEXT_MODE] = 3 # use binding in function on TOPLEVEL_BINDING
  98      @CONF[:SINGLE_IRB] = false
  99  
 100  #    @CONF[:LC_MESSAGES] = "en"
 101      @CONF[:LC_MESSAGES] = Locale.new
 102      
 103      @CONF[:DEBUG_LEVEL] = 1
 104    end
 105  
 106    def IRB.init_error
 107      @CONF[:LC_MESSAGES].load("irb/error.rb")
 108    end
 109  
 110    # option analyzing
 111    def IRB.parse_opts
 112      while opt = ARGV.shift
 113        case opt
 114        when "-f"
 115          @CONF[:RC] = false
 116        when "-m"
 117          @CONF[:MATH_MODE] = true
 118        when "-d"
 119          $DEBUG = true
 120        when "-r"
 121          opt = ARGV.shift
 122          @CONF[:LOAD_MODULES].push opt if opt
 123        when /^-K(.)/
 124          $KCODE = $1
 125        when "--inspect"
 126          @CONF[:INSPECT_MODE] = true
 127        when "--noinspect"
 128          @CONF[:INSPECT_MODE] = false
 129        when "--readline"
 130          @CONF[:USE_READLINE] = true
 131        when "--noreadline"
 132          @CONF[:USE_READLINE] = false
 133        when "--echo"
 134          @CONF[:ECHO] = true
 135        when "--noecho"
 136          @CONF[:ECHO] = false
 137        when "--verbose"
 138          @CONF[:VERBOSE] = true
 139        when "--noverbose"
 140          @CONF[:VERBOSE] = false
 141        when "--prompt-mode", "--prompt"
 142          prompt_mode = ARGV.shift.upcase.tr("-", "_").intern
 143          @CONF[:PROMPT_MODE] = prompt_mode
 144        when "--noprompt"
 145          @CONF[:PROMPT_MODE] = :NULL
 146        when "--inf-ruby-mode"
 147          @CONF[:PROMPT_MODE] = :INF_RUBY
 148        when "--sample-book-mode", "--simple-prompt"
 149          @CONF[:PROMPT_MODE] = :SIMPLE
 150        when "--tracer"
 151          @CONF[:USE_TRACER] = true
 152        when "--back-trace-limit"
 153          @CONF[:BACK_TRACE_LIMIT] = ARGV.shift.to_i
 154        when "--context-mode"
 155          @CONF[:CONTEXT_MODE] = ARGV.shift.to_i
 156        when "--single-irb"
 157          @CONF[:SINGLE_IRB] = true
 158        when "--irb_debug"
 159          @CONF[:DEBUG_LEVEL] = ARGV.shift.to_i
 160        when "-v", "--version"
 161          print IRB.version, "\n"
 162          exit 0
 163        when "-h", "--help"
 164          require "irb/help"
 165          IRB.print_usage
 166          exit 0
 167        when /^-/
 168          IRB.fail UnrecognizedSwitch, opt
 169        else
 170          @CONF[:SCRIPT] = opt
 171          $0 = opt
 172          break
 173        end
 174      end
 175    end
 176  
 177    # running config
 178    def IRB.run_config
 179      if @CONF[:RC]
 180        rcs = []
 181        rcs.push File.expand_path("~/.irbrc") if ENV.key?("HOME")
 182        rcs.push ".irbrc"
 183        rcs.push "irb.rc"
 184        rcs.push "_irbrc"
 185        rcs.push "$irbrc"
 186        catch(:EXIT) do
 187          for rc in rcs
 188            begin
 189              load rc
 190              throw :EXIT
 191            rescue LoadError, Errno::ENOENT
 192            rescue
 193              print "load error: #{rc}\n"
 194              print $!.type, ": ", $!, "\n"
 195              for err in $@[0, $@.size - 2]
 196                print "\t", err, "\n"
 197              end
 198              throw :EXIT
 199            end
 200          end
 201        end
 202      end
 203    end
 204  
 205    # loading modules
 206    def IRB.load_modules
 207      for m in @CONF[:LOAD_MODULES]
 208        begin
 209          require m
 210        rescue
 211          print $@[0], ":", $!.type, ": ", $!, "\n"
 212        end
 213      end
 214    end
 215  
 216  end