Csaba Henk <csaba / phony_for_avoiding_spam.org> wrote:
> 
> Uh-oh. Stupid me, again.
> 
>  irb.context.workspace.main
> 
> starts a new reader first, and when that's completes, it gives you the
> respective IRB::Irb object. So even if that's the toplevel object again,
> I don't think you really want this. IRB.context.workspace.main is the
> correct solution, under the right circumstances... :) no wonder it
> doesn't work for you. For my irb hacks, I use
> 
>  module IRB
>     def self.context
>       conf[:MAIN_CONTEXT]
>     end
>  end
> 
> and having this, what I told you will work.

I'm a bit confused about lifecycle issues now. What I have at the moment
is this:

module IRB

  def IRB.start_in_fxirb(im)
    if RUBY_VERSION < "1.7.3"
      IRB.initialize(nil)
      IRB.parse_opts
      IRB.load_modules
    else
      IRB.setup(nil)
    end

    irb = Irb.new(nil, im)    

    @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
    @CONF[:MAIN_CONTEXT] = irb.context
    trap("SIGINT") do
      irb.signal_handle
    end

    class << irb.context.workspace.main
      def gets
        inp = IRB.conf[:MAIN_CONTEXT].io
        inp.gets_mode = true
        retval = IRB.conf[:MAIN_CONTEXT].io.gets
        inp.gets_mode = false
        retval
      end
    end
    
    catch(:IRB_EXIT) do
      irb.eval_input
    end
    print "\n"

  end

end

and inside the FXIrb class:

def create
    super
    setFocus
    # IRB initialization
    @inputAdded = 0
    @input = IO.pipe
    $DEFAULT_OUTPUT = self

    @im = FXIRBInputMethod.new
    @irb = Thread.new {
      IRB.start_in_fxirb(@im)
      self.crash
    }

    @multiline = false
    
    @exit_proc = lambda {exit}
  end
end

As far as I can make out, inside IRB.start_in_fxirb is the only place I
have access to the IRB.conf[:MAIN_CONTEXT] object after it is
initialised to Irb.new, so that I can create the appropriate singleton
method. Also I looked at the Context and Workspace code, and don't see
why irb.context.workspace.main would create a new reader. (Thanks for
being patient, btw - the IRB code is fairly labyrinthine until one gets
used to it.)

martin