Class: Linguistics::Latin::Util::LatinIRB

Inherits:
Object
  • Object
show all
Defined in:
lib/LatinIRB.rb,
lib/latinirb/version.rb

Constant Summary collapse

VERSION =

:nodoc:

"0.3.4"

Class Method Summary collapse

Class Method Details

.beginObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/LatinIRB.rb', line 25

def self.begin
  #---
  #
  # This method is taken from irb.rb's IRB.start method.  I trimmed
  # out some of the conditional possibilities that I did not want to
  # handle here (because they're not necessary).
  #
  # Run the basic setup script and pull the configuration object
  # (IRB::Context) back into the present scope.  Then we set that
  # object's options and proceed.
  #
  #+++

  IRB.setup(nil)
  @CONF = IRB.conf

  # This will be the script IRB sources on execution. You can
  # pre-define variables (@aFirst, etc.) and convenience methods here.

  @CONF[:SCRIPT]=File.join(File.dirname(__FILE__),  %w|latirb.rb|)

  # No, do not tell me what you read in
  @CONF[:ECHO]=false

  # Nor tell me how it evaluated
  @CONF[:VERBOSE]=false

  # We need this module
  @CONF[:LOAD_MODULES]=["latinverb"]

  # Create an irb object that is programmed to (silently, per above)
  # source a configuration file that ends with a call to 'irb' itself
  # after defining several instance variables

  irb = IRB::Irb.new(nil, @CONF[:SCRIPT])

  # Create a LatinIRB prompt
  @CONF[:PROMPT][:LATINIRB] = {
                                :PROMPT_I    => "LatinIRB > ",
                                :PROMPT_S    => "LatinIRB%l> ",
                                :PROMPT_C    => "LatinIRB > ",
                                :PROMPT_N    => "LatinIRB ?> ",
                                :RETURN      => " => %s \n",
                                :AUTO_INDENT => true
                              }
  @CONF[:PROMPT_MODE]=:LATINIRB

  # Unless this is set, eval_input will fail.  Make sure this is
  # set.
  @CONF[:MAIN_CONTEXT] = irb.context

  # This corrects the tab-completion behavior as provided by
  # irb/completion.rb.  In the even that what's tabbed-after matches
  # the RegExp, it should invoke this process.  If the receiver is a
  # LatinVerb, the full complement of vectors should be provided as
  # complet-able.  IF NOT, then the pairing is passed to the standard
  # CompletionProc.

  Readline.completion_proc = calculate_completion_proc

  # We have finished the configuration at this point, so now we need
  # to kick up the REPL after providing preliminary instruction.
  puts "Beginning a LatinVerb session."

  puts "The following verbs have been made available to this session:"

  # Open the file and extract the names of the variables that can be
  # used for autocompletion
  @irb_ivars =
    File::open(irb.context.io.file_name).readlines.grep(/^@/).map do |x|
      x.sub(/(@\w+)\s.*\n/, "\\1")
    end
  @irb_ivars.each{|x| puts "  * #{x}\n"}


  puts "Tab-completion of the conjugation \"vectors\" is supported."

  trap("SIGINT") do
    irb.signal_handle
  end

  begin
    catch(:IRB_EXIT) do
      # Start the REPL
      irb.eval_input
    end
  end

  puts "Vale!  Come back to LatinIRB soon."
end

.calculate_completion_procObject

As part of the TAB completion, Readline must be provided a completion proc that will be used to generate the matching results that will be appended to the line at whose end the TAB key was struck. This method provides that proc.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/LatinIRB.rb', line 150

def self.calculate_completion_proc
  proc do |input|
    bind = IRB.conf[:MAIN_CONTEXT].workspace.binding

    input =~ /^([^."].*)\.([^.]*)$/
    begin
      receiver = $1
      message = Regexp.quote($2)

      # Pull the object from the binding
      rObj = eval("instance_variable_get(:#{receiver})", bind)
    rescue Exception
    end

    if rObj.class == Linguistics::Latin::Verb::LatinVerb
      IRB::InputCompletor::select_message(receiver, message, rObj.instance_methods.grep(/^#{message}/))
    elsif input =~ /^@/
      # This handles instance variables.  input is @someInstanceVariable's @aSomeIn<TAB>
      self.select_message input, input, eval("instance_variables", bind).grep(/@[at]/)
    else
      IRB::InputCompletor::CompletionProc.call input
    end
  end
end

.select_message(receiver, message, candidates) ⇒ Object

Used to override IRB::InputCompletor::select_message for handling tab-completion of instance variables. Code is largely taken from that method with the addition of the /^@/ condition. In IRB::Completor, when an array of matches has been identified, they are sent as the “candidates” while the “receiver” bears the match based on the regex of “message.”



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/LatinIRB.rb', line 127

def self.select_message(receiver, message, candidates)
  candidates.grep(/^#{message}/).collect do |e|
    case e
      when /^[a-zA-Z_]/
        receiver + "." + e
      when /^[0-9]/
      when /^@/
        e
      when *Operators
        #receiver + " " + e
    end
  end
end