lib/irb/extend-command.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   irb/extend-command.rb - irb command extend
   3  #       $Release Version: 0.9$
   4  #       $Revision: 1.4 $
   5  #       $Date: 2002/07/29 06:14:08 $
   6  #       by Keiju ISHITSUKA(keiju@ishitsuka.com)
   7  #
   8  # --
   9  #
  10  #   
  11  #
  12  module IRB
  13    #
  14    # IRB extended command
  15    #
  16    module ExtendCommandBundle
  17      EXCB = ExtendCommandBundle
  18  
  19      NO_OVERRIDE = 0
  20      OVERRIDE_PRIVATE_ONLY = 0x01
  21      OVERRIDE_ALL = 0x02
  22  
  23      def irb_exit(ret = 0)
  24        irb_context.exit(ret)
  25      end
  26  
  27      def irb_context
  28        IRB.CurrentContext
  29      end
  30  
  31      @ALIASES = [
  32        [:context, :irb_context, NO_OVERRIDE],
  33        [:conf, :irb_context, NO_OVERRIDE],
  34        [:irb_quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
  35        [:exit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
  36        [:quit, :irb_exit, OVERRIDE_PRIVATE_ONLY],
  37      ]
  38  
  39      @EXTEND_COMMANDS = [
  40        [:irb_current_working_workspace, :CurrentWorkingWorkspace, "irb/cmd/chws",
  41          [:irb_print_working_workspace, OVERRIDE_ALL],
  42          [:irb_cwws, OVERRIDE_ALL],
  43          [:irb_pwws, OVERRIDE_ALL],
  44  #       [:irb_cww, OVERRIDE_ALL],
  45  #       [:irb_pww, OVERRIDE_ALL],
  46          [:cwws, NO_OVERRIDE],
  47          [:pwws, NO_OVERRIDE],
  48  #       [:cww, NO_OVERRIDE],
  49  #       [:pww, NO_OVERRIDE],
  50          [:irb_current_working_binding, OVERRIDE_ALL],
  51          [:irb_print_working_binding, OVERRIDE_ALL],
  52          [:irb_cwb, OVERRIDE_ALL],
  53          [:irb_pwb, OVERRIDE_ALL],
  54  #       [:cwb, NO_OVERRIDE],
  55  #       [:pwb, NO_OVERRIDE]
  56        ],
  57        [:irb_change_workspace, :ChangeWorkspace, "irb/cmd/chws",
  58          [:irb_chws, OVERRIDE_ALL],
  59  #       [:irb_chw, OVERRIDE_ALL],
  60          [:irb_cws, OVERRIDE_ALL],
  61  #       [:irb_cw, OVERRIDE_ALL],
  62          [:chws, NO_OVERRIDE],
  63  #       [:chw, NO_OVERRIDE],
  64          [:cws, NO_OVERRIDE],
  65  #       [:cw, NO_OVERRIDE],
  66          [:irb_change_binding, OVERRIDE_ALL],
  67          [:irb_cb, OVERRIDE_ALL],
  68          [:cb, NO_OVERRIDE]],
  69  
  70        [:irb_workspaces, :Workspaces, "irb/cmd/pushws",
  71          [:workspaces, NO_OVERRIDE],
  72          [:irb_bindings, OVERRIDE_ALL],
  73          [:bindings, NO_OVERRIDE]],
  74        [:irb_push_workspace, :PushWorkspace, "irb/cmd/pushws",
  75          [:irb_pushws, OVERRIDE_ALL],
  76  #       [:irb_pushw, OVERRIDE_ALL],
  77          [:pushws, NO_OVERRIDE],
  78  #       [:pushw, NO_OVERRIDE],
  79          [:irb_push_binding, OVERRIDE_ALL],
  80          [:irb_pushb, OVERRIDE_ALL],
  81          [:pushb, NO_OVERRIDE]],
  82        [:irb_pop_workspace, :PopWorkspace, "irb/cmd/pushws",
  83          [:irb_popws, OVERRIDE_ALL],
  84  #       [:irb_popw, OVERRIDE_ALL],
  85          [:popws, NO_OVERRIDE],
  86  #       [:popw, NO_OVERRIDE],
  87          [:irb_pop_binding, OVERRIDE_ALL],
  88          [:irb_popb, OVERRIDE_ALL],
  89          [:popb, NO_OVERRIDE]],
  90  
  91        [:irb_load, :Load, "irb/cmd/load"],
  92        [:irb_require, :Require, "irb/cmd/load"],
  93        [:irb_source, :Source, "irb/cmd/load", 
  94          [:source, NO_OVERRIDE]],
  95  
  96        [:irb, :IrbCommand, "irb/cmd/subirb"],
  97        [:irb_jobs, :Jobs, "irb/cmd/subirb", 
  98          [:jobs, NO_OVERRIDE]],
  99        [:irb_fg, :Foreground, "irb/cmd/subirb", 
 100          [:fg, NO_OVERRIDE]],
 101        [:irb_kill, :Kill, "irb/cmd/subirb", 
 102          [:kill, OVERRIDE_PRIVATE_ONLY]],
 103      ]
 104  
 105      def EXCB.install_extend_commands
 106        for args in @EXTEND_COMMANDS
 107          def_extend_command(*args)
 108        end
 109      end
 110  
 111      # aliases = [commans_alias, flag], ...
 112      def EXCB.def_extend_command(cmd_name, cmd_class, load_file = nil, *aliases)
 113        case cmd_class
 114        when Symbol
 115          cmd_class = cmd_class.id2name
 116        when String
 117        when Class
 118          cmd_class = cmd_class.name
 119        end
 120  
 121        if load_file
 122          eval %[
 123            def #{cmd_name}(*opts, &b)
 124              require "#{load_file}"
 125              eval %[
 126                def #{cmd_name}(*opts, &b)
 127                  ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b)
 128                end
 129              ]
 130              send :#{cmd_name}, *opts, &b
 131            end
 132          ]
 133        else
 134          eval %[
 135            def #{cmd_name}(*opts, &b)
 136              ExtendCommand::#{cmd_class}.execute(irb_context, *opts, &b)
 137            end
 138          ]
 139        end
 140  
 141        for ali, flag in aliases
 142          @ALIASES.push [ali, cmd_name, flag]
 143        end
 144      end
 145  
 146      # override = {NO_OVERRIDE, OVERRIDE_PRIVATE_ONLY, OVERRIDE_ALL}
 147      def install_alias_method(to, from, override = NO_OVERRIDE)
 148        to = to.id2name unless to.kind_of?(String)
 149        from = from.id2name unless from.kind_of?(String)
 150  
 151        if override == OVERRIDE_ALL or
 152            (override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
 153            (override == NO_OVERRIDE) &&  !respond_to?(to, true)
 154          target = self
 155          (class<<self;self;end).instance_eval{
 156            if target.respond_to?(to, true) && 
 157                !target.respond_to?(EXCB.irb_original_method_name(to), true)
 158              alias_method(EXCB.irb_original_method_name(to), to) 
 159            end
 160            alias_method to, from
 161          }
 162        else
 163          print "irb: warn: can't alias #{to} from #{from}.\n"
 164        end
 165      end
 166  
 167      def self.irb_original_method_name(method_name)
 168        "irb_" + method_name + "_org"
 169      end
 170  
 171      def EXCB.extend_object(obj)
 172        unless (class<<obj;ancestors;end).include?(EXCB)
 173          super
 174          for ali, com, flg in @ALIASES
 175            obj.install_alias_method(ali, com, flg)
 176          end
 177        end
 178      end
 179  
 180      install_extend_commands
 181    end
 182  
 183    # extension support for Context
 184    module ContextExtender
 185      CE = ContextExtender
 186  
 187      @EXTEND_COMMANDS = [
 188        [:eval_history=, "irb/ext/history.rb"],
 189        [:use_tracer=, "irb/ext/tracer.rb"],
 190        [:math_mode=, "irb/ext/math-mode.rb"],
 191        [:use_loader=, "irb/ext/use-loader.rb"],
 192      ]
 193  
 194      def CE.install_extend_commands
 195        for args in @EXTEND_COMMANDS
 196          def_extend_command(*args)
 197        end
 198      end
 199  
 200      def CE.def_extend_command(cmd_name, load_file, *aliases)
 201        Context.module_eval %[
 202          def #{cmd_name}(*opts, &b)
 203            require "#{load_file}"
 204            send :#{cmd_name}, *opts, &b
 205          end
 206          for ali in aliases
 207            alias_method ali, cmd_name
 208          end
 209        ]
 210      end
 211  
 212      CE.install_extend_commands
 213    end
 214  end
 215