mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* irb 0.9
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2627 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
93602810e9
commit
af064b04b1
36 changed files with 1286 additions and 497 deletions
|
@ -1,6 +1,6 @@
|
|||
#
|
||||
# irb/extend-command.rb - irb command extend
|
||||
# $Release Version: 0.7.3$
|
||||
# $Release Version: 0.9$
|
||||
# $Revision$
|
||||
# $Date$
|
||||
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
|
||||
|
@ -13,104 +13,149 @@ module IRB
|
|||
#
|
||||
# IRB extended command
|
||||
#
|
||||
module ExtendCommand
|
||||
# include Loader
|
||||
|
||||
def irb_exit(ret = 0)
|
||||
irb_context.exit(ret)
|
||||
end
|
||||
alias irb_quit irb_exit
|
||||
module ExtendCommandBundle
|
||||
EXCB = ExtendCommandBundle
|
||||
|
||||
def irb_fork(&block)
|
||||
pid = send ExtendCommand.irb_original_method_name("fork")
|
||||
unless pid
|
||||
class<<self
|
||||
alias_method :exit, ExtendCommand.irb_original_method_name('exit')
|
||||
end
|
||||
if iterator?
|
||||
begin
|
||||
yield
|
||||
ensure
|
||||
exit
|
||||
end
|
||||
end
|
||||
end
|
||||
pid
|
||||
end
|
||||
|
||||
def irb_change_binding(*main)
|
||||
irb_context.change_binding(*main)
|
||||
end
|
||||
alias irb_change_workspace irb_change_binding
|
||||
|
||||
def irb_source(file)
|
||||
irb_context.source(file)
|
||||
end
|
||||
|
||||
def irb(*obj)
|
||||
require "irb/multi-irb"
|
||||
IRB.irb(nil, *obj)
|
||||
end
|
||||
|
||||
def irb_context
|
||||
IRB.conf[:MAIN_CONTEXT]
|
||||
end
|
||||
|
||||
def irb_jobs
|
||||
require "irb/multi-irb"
|
||||
IRB.JobManager
|
||||
end
|
||||
|
||||
def irb_fg(key)
|
||||
require "irb/multi-irb"
|
||||
IRB.JobManager.switch(key)
|
||||
end
|
||||
|
||||
def irb_kill(*keys)
|
||||
require "irb/multi-irb"
|
||||
IRB.JobManager.kill(*keys)
|
||||
end
|
||||
|
||||
# extend command functions
|
||||
def ExtendCommand.extend_object(obj)
|
||||
super
|
||||
unless (class<<obj;ancestors;end).include?(ExtendCommand)
|
||||
obj.install_aliases
|
||||
end
|
||||
end
|
||||
|
||||
OVERRIDE_NOTHING = 0
|
||||
NO_OVERRIDE = 0
|
||||
OVERRIDE_PRIVATE_ONLY = 0x01
|
||||
OVERRIDE_ALL = 0x02
|
||||
|
||||
def install_aliases(override = OVERRIDE_NOTHING)
|
||||
|
||||
install_alias_method(:exit, :irb_exit, override | OVERRIDE_PRIVATE_ONLY)
|
||||
install_alias_method(:quit, :irb_quit, override | OVERRIDE_PRIVATE_ONLY)
|
||||
install_alias_method(:fork, :irb_fork, override | OVERRIDE_PRIVATE_ONLY)
|
||||
install_alias_method(:kill, :irb_kill, override | OVERRIDE_PRIVATE_ONLY)
|
||||
|
||||
install_alias_method(:irb_cb, :irb_change_binding, override)
|
||||
install_alias_method(:irb_ws, :irb_change_workspace, override)
|
||||
install_alias_method(:source, :irb_source, override)
|
||||
install_alias_method(:conf, :irb_context, override)
|
||||
install_alias_method(:jobs, :irb_jobs, override)
|
||||
install_alias_method(:fg, :irb_fg, override)
|
||||
def irb_exit(ret = 0)
|
||||
irb_context.exit(ret)
|
||||
end
|
||||
|
||||
# override = {OVERRIDE_NOTHING, OVERRIDE_PRIVATE_ONLY, OVERRIDE_ALL}
|
||||
def install_alias_method(to, from, override = OVERRIDE_NOTHING)
|
||||
def irb_context
|
||||
IRB.CurrentContext
|
||||
end
|
||||
|
||||
@ALIASES = [
|
||||
[:context, :irb_context, NO_OVERRIDE],
|
||||
[:conf, :irb_context, NO_OVERRIDE],
|
||||
[:irb_quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
|
||||
[:exit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
|
||||
[:quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
|
||||
]
|
||||
|
||||
@EXTEND_COMMANDS = [
|
||||
[:irb_current_working_workspace, :CurrentWorkingWorkspace, "irb/cmd/chws",
|
||||
[:irb_print_working_workspace, OVERRIDE_ALL],
|
||||
[:irb_cwws, OVERRIDE_ALL],
|
||||
[:irb_pwws, OVERRIDE_ALL],
|
||||
# [:irb_cww, OVERRIDE_ALL],
|
||||
# [:irb_pww, OVERRIDE_ALL],
|
||||
[:cwws, NO_OVERRIDE],
|
||||
[:pwws, NO_OVERRIDE],
|
||||
# [:cww, NO_OVERRIDE],
|
||||
# [:pww, NO_OVERRIDE],
|
||||
[:irb_current_working_binding, OVERRIDE_ALL],
|
||||
[:irb_print_working_binding, OVERRIDE_ALL],
|
||||
[:irb_cwb, OVERRIDE_ALL],
|
||||
[:irb_pwb, OVERRIDE_ALL],
|
||||
# [:cwb, NO_OVERRIDE],
|
||||
# [:pwb, NO_OVERRIDE]
|
||||
],
|
||||
[:irb_change_workspace, :ChangeWorkspace, "irb/cmd/chws",
|
||||
[:irb_chws, OVERRIDE_ALL],
|
||||
# [:irb_chw, OVERRIDE_ALL],
|
||||
[:irb_cws, OVERRIDE_ALL],
|
||||
# [:irb_cw, OVERRIDE_ALL],
|
||||
[:chws, NO_OVERRIDE],
|
||||
# [:chw, NO_OVERRIDE],
|
||||
[:cws, NO_OVERRIDE],
|
||||
# [:cw, NO_OVERRIDE],
|
||||
[:irb_change_binding, OVERRIDE_ALL],
|
||||
[:irb_cb, OVERRIDE_ALL],
|
||||
[:cb, NO_OVERRIDE]],
|
||||
|
||||
[:irb_workspaces, :Workspaces, "irb/cmd/pushws",
|
||||
[:workspaces, NO_OVERRIDE],
|
||||
[:irb_bindings, OVERRIDE_ALL],
|
||||
[:bindings, NO_OVERRIDE]],
|
||||
[:irb_push_workspace, :PushWorkspace, "irb/cmd/pushws",
|
||||
[:irb_pushws, OVERRIDE_ALL],
|
||||
# [:irb_pushw, OVERRIDE_ALL],
|
||||
[:pushws, NO_OVERRIDE],
|
||||
# [:pushw, NO_OVERRIDE],
|
||||
[:irb_push_binding, OVERRIDE_ALL],
|
||||
[:irb_pushb, OVERRIDE_ALL],
|
||||
[:pushb, NO_OVERRIDE]],
|
||||
[:irb_pop_workspace, :PopWorkspace, "irb/cmd/pushws",
|
||||
[:irb_popws, OVERRIDE_ALL],
|
||||
# [:irb_popw, OVERRIDE_ALL],
|
||||
[:popws, NO_OVERRIDE],
|
||||
# [:popw, NO_OVERRIDE],
|
||||
[:irb_pop_binding, OVERRIDE_ALL],
|
||||
[:irb_popb, OVERRIDE_ALL],
|
||||
[:popb, NO_OVERRIDE]],
|
||||
|
||||
[:irb_load, :Load, "irb/cmd/load"],
|
||||
[:irb_require, :Require, "irb/cmd/load"],
|
||||
[:irb_source, :Source, "irb/cmd/load",
|
||||
[:source, NO_OVERRIDE]],
|
||||
|
||||
[:irb, :IrbCommand, "irb/cmd/subirb"],
|
||||
[:irb_jobs, :Jobs, "irb/cmd/subirb",
|
||||
[:jobs, NO_OVERRIDE]],
|
||||
[:irb_fg, :Foreground, "irb/cmd/subirb",
|
||||
[:fg, NO_OVERRIDE]],
|
||||
[:irb_kill, :Kill, "irb/cmd/subirb",
|
||||
[:kill, OVERRIDE_PRIVATE_ONLY]],
|
||||
]
|
||||
|
||||
def EXCB.install_extend_commands
|
||||
for args in @EXTEND_COMMANDS
|
||||
def_extend_command *args
|
||||
end
|
||||
end
|
||||
|
||||
# aliases = [commans_alias, flag], ...
|
||||
def EXCB.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases)
|
||||
case cmd_class
|
||||
when Symbol
|
||||
cmd_class = cmd_class.id2name
|
||||
when String
|
||||
when Class
|
||||
cmd_class = cmd_class.name
|
||||
end
|
||||
|
||||
if load_file
|
||||
eval %[
|
||||
def #{cmd_name}(*opts, &b)
|
||||
require "#{load_file}"
|
||||
eval %[
|
||||
def #{cmd_name}(*opts, &b)
|
||||
ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b)
|
||||
end
|
||||
]
|
||||
send :#{cmd_name}, *opts, &b
|
||||
end
|
||||
]
|
||||
else
|
||||
eval %[
|
||||
def #{cmd_name}(*opts, &b)
|
||||
ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b)
|
||||
end
|
||||
]
|
||||
end
|
||||
|
||||
for ali, flag in aliases
|
||||
@ALIASES.push [ali, cmd_name, flag]
|
||||
end
|
||||
end
|
||||
|
||||
# override = {NO_OVERRIDE, OVERRIDE_PRIVATE_ONLY, OVERRIDE_ALL}
|
||||
def install_alias_method(to, from, override = NO_OVERRIDE)
|
||||
to = to.id2name unless to.kind_of?(String)
|
||||
from = from.id2name unless from.kind_of?(String)
|
||||
|
||||
if override == OVERRIDE_ALL or
|
||||
(override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
|
||||
(override == OVERRIDE_NOTHING) && !respond_to?(to, true)
|
||||
(override == NO_OVERRIDE) && !respond_to?(to, true)
|
||||
target = self
|
||||
(class<<self;self;end).instance_eval{
|
||||
if target.respond_to?(to, true) &&
|
||||
!target.respond_to?(ExtendCommand.irb_original_method_name(to), true)
|
||||
alias_method(ExtendCommand.irb_original_method_name(to), to)
|
||||
!target.respond_to?(EXCB.irb_original_method_name(to), true)
|
||||
alias_method(EXCB.irb_original_method_name(to), to)
|
||||
end
|
||||
alias_method to, from
|
||||
}
|
||||
|
@ -122,5 +167,49 @@ module IRB
|
|||
def self.irb_original_method_name(method_name)
|
||||
"irb_" + method_name + "_org"
|
||||
end
|
||||
|
||||
def EXCB.extend_object(obj)
|
||||
unless (class<<obj;ancestors;end).include?(EXCB)
|
||||
super
|
||||
for ali, com, flg in @ALIASES
|
||||
obj.install_alias_method(ali, com, flg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
install_extend_commands
|
||||
end
|
||||
|
||||
# extension support for Context
|
||||
module ContextExtender
|
||||
CE = ContextExtender
|
||||
|
||||
@EXTEND_COMMANDS = [
|
||||
[:eval_history=, "irb/ext/history.rb"],
|
||||
[:use_tracer=, "irb/ext/tracer.rb"],
|
||||
[:math_mode=, "irb/ext/math-mode.rb"],
|
||||
[:use_loader=, "irb/ext/use-loader.rb"],
|
||||
]
|
||||
|
||||
def CE.install_extend_commands
|
||||
for args in @EXTEND_COMMANDS
|
||||
def_extend_command *args
|
||||
end
|
||||
end
|
||||
|
||||
def CE.def_extend_command(cmd_name, load_file, *aliases)
|
||||
Context.module_eval %[
|
||||
def #{cmd_name}(*opts, &b)
|
||||
require "#{load_file}"
|
||||
send :#{cmd_name}, *opts, &b
|
||||
end
|
||||
for ali in aliases
|
||||
alias_method ali, cmd_name
|
||||
end
|
||||
]
|
||||
end
|
||||
|
||||
CE.install_extend_commands
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue