lib/irb/context.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   irb/context.rb - irb context
   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  require "irb/workspace"
  13  
  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)
  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
  31  
  32        # copy of default configuration
  33        @ap_name = IRB.conf[:AP_NAME]
  34        @rc = IRB.conf[:RC]
  35        @load_modules = IRB.conf[:LOAD_MODULES]
  36  
  37        @use_readline = IRB.conf[:USE_READLINE]
  38        @inspect_mode = IRB.conf[:INSPECT_MODE]
  39  
  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_TRASER]
  42        self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
  43        self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVEL_HISTORY]
  44  
  45        @ignore_sigint = IRB.conf[:IGNORE_SIGINT]
  46        @ignore_eof = IRB.conf[:IGNORE_EOF]
  47  
  48        @back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
  49        
  50        self.prompt_mode = IRB.conf[:PROMPT_MODE]
  51  
  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 + ")"
  58  
  59        case input_method
  60        when nil
  61          if (use_readline.nil? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && STDIN.tty? ||
  62               use_readline?)
  63            @io = ReadlineInputMethod.new
  64          else
  65            @io = StdioInputMethod.new
  66          end
  67        when String
  68          @io = FileInputMethod.new(input_method)
  69          @irb_name = File.basename(input_method)
  70          @irb_path = input_method
  71        else
  72          @io = input_method
  73        end
  74  
  75        @verbose = IRB.conf[:VERBOSE] 
  76        @echo = IRB.conf[:ECHO]
  77        if @echo.nil?
  78          @echo = true
  79        end
  80        @debug_level = IRB.conf[:DEBUG_LEVEL]
  81      end
  82  
  83      def main
  84        @workspace.main
  85      end
  86  
  87      attr_reader :workspace_home
  88      attr_accessor :workspace
  89      attr_reader :thread
  90      attr_accessor :io
  91      
  92      attr_accessor :irb
  93      attr_accessor :ap_name
  94      attr_accessor :rc
  95      attr_accessor :load_modules
  96      attr_accessor :irb_name
  97      attr_accessor :irb_path
  98  
  99      attr_accessor :use_readline
 100      attr_reader :inspect_mode
 101  
 102      attr_reader :prompt_mode
 103      attr_accessor :prompt_i
 104      attr_accessor :prompt_s
 105      attr_accessor :prompt_c
 106      attr_accessor :auto_indent_mode
 107      attr_accessor :return_format
 108  
 109      attr_accessor :ignore_sigint
 110      attr_accessor :ignore_eof
 111      attr_accessor :echo
 112      attr_accessor :verbose
 113      attr_reader :debug_level
 114  
 115      attr_accessor :back_trace_limit
 116  
 117      alias use_readline? use_readline
 118      alias rc? rc
 119      alias ignore_sigint? ignore_sigint
 120      alias ignore_eof? ignore_eof
 121      alias echo? echo
 122  
 123      def verbose?
 124        if @verbose.nil?
 125          if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod) 
 126            false
 127          elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
 128            true
 129          else
 130            false
 131          end
 132        end
 133      end
 134  
 135      def prompting?
 136        verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
 137                  (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
 138      end
 139  
 140      attr_reader :last_value
 141  
 142      def set_last_value(value)
 143        @last_value = value
 144      end
 145  
 146      attr_reader :irb_name
 147  
 148      def prompt_mode=(mode)
 149        @prompt_mode = mode
 150        pconf = IRB.conf[:PROMPT][mode]
 151        @prompt_i = pconf[:PROMPT_I]
 152        @prompt_s = pconf[:PROMPT_S]
 153        @prompt_c = pconf[:PROMPT_C]
 154        @return_format = pconf[:RETURN]
 155        if ai = pconf.include?(:AUTO_INDENT)
 156          @auto_indent_mode = ai
 157        else
 158          @auto_indent_mode = IRB.conf[:AUTO_INDENT]
 159        end
 160      end
 161      
 162      def inspect?
 163        @inspect_mode.nil? or @inspect_mode
 164      end
 165  
 166      def file_input?
 167        @io.type == FileInputMethod
 168      end
 169  
 170      def inspect_mode=(opt)
 171        if opt
 172          @inspect_mode = opt
 173        else
 174          @inspect_mode = !@inspect_mode
 175        end
 176        print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose?
 177        @inspect_mode
 178      end
 179  
 180      def use_readline=(opt)
 181        @use_readline = opt
 182        print "use readline module\n" if @use_readline
 183      end
 184  
 185      def debug_level=(value)
 186        @debug_level = value
 187        RubyLex.debug_level = value
 188        SLex.debug_level = value
 189      end
 190  
 191      def debug?
 192        @debug_level > 0
 193      end
 194  
 195      def evaluate(line, line_no)
 196        @line_no = line_no
 197        set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
 198  #      @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._")
 199  #      @_ = @workspace.evaluate(line, irb_path, line_no)
 200      end
 201  
 202      alias __exit__ exit
 203      def exit(ret = 0)
 204        IRB.irb_exit(@irb, ret)
 205      end
 206  
 207      NOPRINTING_IVARS = ["@last_value"]
 208      NO_INSPECTING_IVARS = ["@irb", "@io"]
 209      IDNAME_IVARS = ["@prompt_mode"]
 210  
 211      alias __inspect__ inspect
 212      def inspect
 213        array = []
 214        for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
 215          name = ivar.sub(/^@(.*)$/){$1}
 216          val = instance_eval(ivar)
 217          case ivar
 218          when *NOPRINTING_IVARS
 219            array.push format("conf.%s=%s", name, "...")
 220          when *NO_INSPECTING_IVARS
 221            array.push format("conf.%s=%s", name, val.to_s)
 222          when *IDNAME_IVARS
 223            array.push format("conf.%s=:%s", name, val.id2name)
 224          else
 225            array.push format("conf.%s=%s", name, val.inspect)
 226          end
 227        end
 228        array.join("\n")
 229      end
 230      alias __to_s__ to_s
 231      alias to_s inspect
 232    end
 233  end