lib/irb/xmp.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   xmp.rb - irb version of gotoken xmp
   3  #       $Release Version: 0.9$
   4  #       $Revision: 1.3 $
   5  #       $Date: 2002/07/09 11:17:16 $
   6  #       by Keiju ISHITSUKA(Nippon Rational Inc.)
   7  #
   8  # --
   9  #
  10  #   
  11  #
  12  
  13  require "irb"
  14  require "irb/frame"
  15  
  16  class XMP
  17    @RCS_ID='-$Id: xmp.rb,v 1.3 2002/07/09 11:17:16 keiju Exp $-'
  18  
  19    def initialize(bind = nil)
  20      IRB.init_config(nil)
  21      #IRB.parse_opts
  22      #IRB.load_modules
  23  
  24      IRB.conf[:PROMPT_MODE] = :XMP
  25  
  26      bind = IRB::Frame.top(1) unless bind
  27      ws = IRB::WorkSpace.new(bind)
  28      @io = StringInputMethod.new
  29      @irb = IRB::Irb.new(ws, @io)
  30      @irb.context.ignore_sigint = false
  31  
  32  #    IRB.conf[:IRB_RC].call(@irb.context) if IRB.conf[:IRB_RC]
  33      IRB.conf[:MAIN_CONTEXT] = @irb.context
  34    end
  35  
  36    def puts(exps)
  37      @io.puts exps
  38  
  39      if @irb.context.ignore_sigint
  40        begin
  41          trap_proc_b = trap("SIGINT"){@irb.signal_handle}
  42          catch(:IRB_EXIT) do
  43            @irb.eval_input
  44          end
  45        ensure
  46          trap("SIGINT", trap_proc_b)
  47        end
  48      else
  49        catch(:IRB_EXIT) do
  50          @irb.eval_input
  51        end
  52      end
  53    end
  54  
  55    class StringInputMethod < IRB::InputMethod
  56      def initialize
  57        super
  58        @exps = []
  59      end
  60  
  61      def eof?
  62        @exps.empty?
  63      end
  64  
  65      def gets
  66        while l = @exps.shift
  67          next if /^\s+$/ =~ l
  68          l.concat "\n"
  69          print @prompt, l
  70          break
  71        end
  72        l
  73      end
  74  
  75      def puts(exps)
  76        @exps.concat exps.split(/\n/)
  77      end
  78    end
  79  end
  80  
  81  def xmp(exps, bind = nil)
  82    bind = IRB::Frame.top(1) unless bind
  83    xmp = XMP.new(bind)
  84    xmp.puts exps
  85    xmp
  86  end