# frozen_string_literal: false
# xmp.rb - irb version of gotoken xmp
# by Keiju ISHITSUKA(Nippon Rational Inc.)
require_relative "../irb"
# An example printer for irb.
# It's much like the standard library PrettyPrint, that shows the value of each
# In order to use this library, you must first require it:
# Now, you can take advantage of the Object#xmp convenience method.
# You can also create an XMP object, with an optional binding to print
# expressions in the given binding:
# #=> today = "a good day"
# ctx.eval 'today # is what?'
# Creates a new XMP object.
# The top-level binding or, optional +bind+ parameter will be used when
# creating the workspace. See WorkSpace.new for more information.
# This uses the +:XMP+ prompt mode, see IRB@Customizing+the+IRB+Prompt for
def initialize(bind = nil)
IRB.conf[:PROMPT_MODE] = :XMP
bind = IRB::Frame.top(1) unless bind
ws = IRB::WorkSpace.new(bind)
@io = StringInputMethod.new
@irb = IRB::Irb.new(ws, @io)
@irb.context.ignore_sigint = false
IRB.conf[:MAIN_CONTEXT] = @irb.context
# Evaluates the given +exps+, for example:
# x.puts '{:a => 1, :b => 2, :c => 3}'
# #=> {:a => 1, :b => 2, :c => 3}
# # ==>{:a=>1, :b=>2, :c=>3}
if @irb.context.ignore_sigint
trap_proc_b = trap("SIGINT"){@irb.signal_handle}
trap("SIGINT", trap_proc_b)
# A custom InputMethod class used by XMP for evaluating string io.
class StringInputMethod < IRB::InputMethod
# Creates a new StringInputMethod object
# Whether there are any expressions left in this printer.
# Reads the next expression from this printer.
# See IO#gets for more information.
# Concatenates all expressions in this printer, separated by newlines.
# An Encoding::CompatibilityError is raised of the given +exps+'s encoding
# doesn't match the previous expression evaluated.
if @encoding and exps.encoding != @encoding
enc = Encoding.compatible?(@exps.join("\n"), exps)
raise Encoding::CompatibilityError, "Encoding in which the passed expression is encoded is not compatible to the preceding's one"
@encoding = exps.encoding
@exps.concat exps.split(/\n/)
# Returns the encoding of last expression printed by #puts.
# A convenience method that's only available when the you require the IRB::XMP standard library.
# Creates a new XMP object, using the given expressions as the +exps+
# parameter, and optional binding as +bind+ or uses the top-level binding. Then
# evaluates the given expressions using the +:XMP+ prompt mode.
# See XMP.new for more information.
def xmp(exps, bind = nil)
bind = IRB::Frame.top(1) unless bind