rubysl-irb 1.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -2
  3. data/lib/irb/cmd/chws.rb +6 -6
  4. data/lib/irb/cmd/fork.rb +10 -10
  5. data/lib/irb/cmd/help.rb +24 -14
  6. data/lib/irb/cmd/load.rb +8 -7
  7. data/lib/irb/cmd/nop.rb +8 -8
  8. data/lib/irb/cmd/pushws.rb +6 -5
  9. data/lib/irb/cmd/subirb.rb +6 -7
  10. data/lib/irb/completion.rb +90 -58
  11. data/lib/irb/context.rb +197 -30
  12. data/lib/irb/ext/change-ws.rb +17 -10
  13. data/lib/irb/ext/history.rb +20 -10
  14. data/lib/irb/ext/loader.rb +22 -12
  15. data/lib/irb/ext/math-mode.rb +16 -6
  16. data/lib/irb/ext/multi-irb.rb +69 -24
  17. data/lib/irb/ext/save-history.rb +87 -37
  18. data/lib/irb/ext/tracer.rb +17 -7
  19. data/lib/irb/ext/use-loader.rb +14 -6
  20. data/lib/irb/ext/workspaces.rb +16 -6
  21. data/lib/irb/extend-command.rb +92 -34
  22. data/lib/irb/frame.rb +18 -5
  23. data/lib/irb/help.rb +20 -19
  24. data/lib/irb/init.rb +156 -104
  25. data/lib/irb/input-method.rb +96 -23
  26. data/lib/irb/inspector.rb +145 -0
  27. data/lib/irb/lc/.document +4 -0
  28. data/lib/irb/lc/error.rb +8 -7
  29. data/lib/irb/lc/{help-message.rb → help-message} +14 -11
  30. data/lib/irb/lc/ja/encoding_aliases.rb +10 -0
  31. data/lib/irb/lc/ja/error.rb +19 -16
  32. data/lib/irb/lc/ja/help-message +33 -28
  33. data/lib/irb/locale.rb +83 -85
  34. data/lib/irb/magic-file.rb +37 -0
  35. data/lib/irb/notifier.rb +101 -15
  36. data/lib/irb/output-method.rb +38 -32
  37. data/lib/irb/ruby-lex.rb +143 -81
  38. data/lib/irb/ruby-token.rb +13 -19
  39. data/lib/irb/slex.rb +26 -27
  40. data/lib/irb/src_encoding.rb +4 -0
  41. data/lib/irb/version.rb +6 -7
  42. data/lib/irb/workspace.rb +22 -15
  43. data/lib/irb/ws-for-case-2.rb +5 -6
  44. data/lib/irb/xmp.rb +91 -4
  45. data/lib/rubysl/irb/irb.rb +523 -175
  46. data/lib/rubysl/irb/version.rb +1 -1
  47. data/rubysl-irb.gemspec +7 -6
  48. metadata +35 -15
@@ -1,25 +1,29 @@
1
1
  #
2
- # loader.rb -
3
- # $Release Version: 0.9.5$
4
- # $Revision: 11708 $
5
- # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
2
+ # loader.rb -
3
+ # $Release Version: 0.9.6$
4
+ # $Revision$
6
5
  # by Keiju ISHITSUKA(keiju@ruby-lang.org)
7
6
  #
8
7
  # --
9
8
  #
10
- #
9
+ #
11
10
  #
12
11
 
13
12
 
14
- module IRB
13
+ module IRB # :nodoc:
14
+ # Raised in the event of an exception in a file loaded from an Irb session
15
15
  class LoadAbort < Exception;end
16
16
 
17
+ # Provides a few commands for loading files within an irb session.
18
+ #
19
+ # See ExtendCommandBundle for more information.
17
20
  module IrbLoader
18
- @RCS_ID='-$Id: loader.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
21
+ @RCS_ID='-$Id$-'
19
22
 
20
23
  alias ruby_load load
21
24
  alias ruby_require require
22
25
 
26
+ # Loads the given file similarly to Kernel#load
23
27
  def irb_load(fn, priv = nil)
24
28
  path = search_file_from_ruby_path(fn)
25
29
  raise LoadError, "No such file to load -- #{fn}" unless path
@@ -27,7 +31,7 @@ module IRB
27
31
  load_file(path, priv)
28
32
  end
29
33
 
30
- def search_file_from_ruby_path(fn)
34
+ def search_file_from_ruby_path(fn) # :nodoc:
31
35
  if /^#{Regexp.quote(File::Separator)}/ =~ fn
32
36
  return fn if File.exist?(fn)
33
37
  return nil
@@ -41,11 +45,14 @@ module IRB
41
45
  return nil
42
46
  end
43
47
 
48
+ # Loads a given file in the current session and displays the source lines
49
+ #
50
+ # See Irb#suspend_input_method for more information.
44
51
  def source_file(path)
45
52
  irb.suspend_name(path, File.basename(path)) do
46
53
  irb.suspend_input_method(FileInputMethod.new(path)) do
47
54
  |back_io|
48
- irb.signal_status(:IN_LOAD) do
55
+ irb.signal_status(:IN_LOAD) do
49
56
  if back_io.kind_of?(FileInputMethod)
50
57
  irb.eval_input
51
58
  else
@@ -60,9 +67,12 @@ module IRB
60
67
  end
61
68
  end
62
69
 
70
+ # Loads the given file in the current session's context and evaluates it.
71
+ #
72
+ # See Irb#suspend_input_method for more information.
63
73
  def load_file(path, priv = nil)
64
74
  irb.suspend_name(path, File.basename(path)) do
65
-
75
+
66
76
  if priv
67
77
  ws = WorkSpace.new(Module.new)
68
78
  else
@@ -71,7 +81,7 @@ module IRB
71
81
  irb.suspend_workspace(ws) do
72
82
  irb.suspend_input_method(FileInputMethod.new(path)) do
73
83
  |back_io|
74
- irb.signal_status(:IN_LOAD) do
84
+ irb.signal_status(:IN_LOAD) do
75
85
  # p irb.conf
76
86
  if back_io.kind_of?(FileInputMethod)
77
87
  irb.eval_input
@@ -88,7 +98,7 @@ module IRB
88
98
  end
89
99
  end
90
100
 
91
- def old
101
+ def old # :nodoc:
92
102
  back_io = @io
93
103
  back_path = @irb_path
94
104
  back_name = @irb_name
@@ -1,23 +1,33 @@
1
1
  #
2
- # math-mode.rb -
3
- # $Release Version: 0.9.5$
4
- # $Revision: 11708 $
5
- # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
2
+ # math-mode.rb -
3
+ # $Release Version: 0.9.6$
4
+ # $Revision$
6
5
  # by Keiju ISHITSUKA(keiju@ruby-lang.org)
7
6
  #
8
7
  # --
9
8
  #
10
- #
9
+ #
11
10
  #
12
11
  require "mathn"
13
12
 
14
13
  module IRB
15
14
  class Context
15
+ # Returns whether bc mode is enabled.
16
+ #
17
+ # See #math_mode=
16
18
  attr_reader :math_mode
19
+ # Alias for #math_mode
17
20
  alias math? math_mode
18
21
 
22
+ # Sets bc mode, which loads +lib/mathn.rb+ so fractions or matrix are
23
+ # available.
24
+ #
25
+ # Also available as the +-m+ command line option.
26
+ #
27
+ # See IRB@Command+line+options and the unix manpage <code>bc(1)</code> for
28
+ # more information.
19
29
  def math_mode=(opt)
20
- if @math_mode == true && opt == false
30
+ if @math_mode == true && !opt
21
31
  IRB.fail CantReturnToNormalMode
22
32
  return
23
33
  end
@@ -1,56 +1,72 @@
1
1
  #
2
2
  # irb/multi-irb.rb - multiple irb module
3
- # $Release Version: 0.9.5$
4
- # $Revision: 11708 $
5
- # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
3
+ # $Release Version: 0.9.6$
4
+ # $Revision$
6
5
  # by Keiju ISHITSUKA(keiju@ruby-lang.org)
7
6
  #
8
7
  # --
9
8
  #
10
- #
9
+ #
11
10
  #
12
11
  IRB.fail CantShiftToMultiIrbMode unless defined?(Thread)
13
12
  require "thread"
14
13
 
15
14
  module IRB
16
- # job management class
17
15
  class JobManager
18
- @RCS_ID='-$Id: multi-irb.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
16
+ @RCS_ID='-$Id$-'
19
17
 
18
+ # Creates a new JobManager object
20
19
  def initialize
21
20
  # @jobs = [[thread, irb],...]
22
21
  @jobs = []
23
22
  @current_job = nil
24
23
  end
25
24
 
25
+ # The active irb session
26
26
  attr_accessor :current_job
27
27
 
28
+ # The total number of irb sessions, used to set +irb_name+ of the current
29
+ # Context.
28
30
  def n_jobs
29
31
  @jobs.size
30
32
  end
31
33
 
34
+ # Returns the thread for the given +key+ object, see #search for more
35
+ # information.
32
36
  def thread(key)
33
- th, irb = search(key)
37
+ th, = search(key)
34
38
  th
35
39
  end
36
40
 
41
+ # Returns the irb session for the given +key+ object, see #search for more
42
+ # information.
37
43
  def irb(key)
38
- th, irb = search(key)
44
+ _, irb = search(key)
39
45
  irb
40
46
  end
41
47
 
48
+ # Returns the top level thread.
42
49
  def main_thread
43
50
  @jobs[0][0]
44
51
  end
45
52
 
53
+ # Returns the top level irb session.
46
54
  def main_irb
47
55
  @jobs[0][1]
48
56
  end
49
57
 
58
+ # Add the given +irb+ session to the jobs Array.
50
59
  def insert(irb)
51
60
  @jobs.push [Thread.current, irb]
52
61
  end
53
62
 
63
+ # Changes the current active irb session to the given +key+ in the jobs
64
+ # Array.
65
+ #
66
+ # Raises an IrbAlreadyDead exception if the given +key+ is no longer alive.
67
+ #
68
+ # If the given irb session is already active, an IrbSwitchedToCurrentThread
69
+ # exception is raised.
54
70
  def switch(key)
55
71
  th, irb = search(key)
56
72
  IRB.fail IrbAlreadyDead unless th.alive?
@@ -61,16 +77,36 @@ module IRB
61
77
  @current_job = irb(Thread.current)
62
78
  end
63
79
 
80
+ # Terminates the irb sessions specified by the given +keys+.
81
+ #
82
+ # Raises an IrbAlreadyDead exception if one of the given +keys+ is already
83
+ # terminated.
84
+ #
85
+ # See Thread#exit for more information.
64
86
  def kill(*keys)
65
87
  for key in keys
66
- th, irb = search(key)
88
+ th, _ = search(key)
67
89
  IRB.fail IrbAlreadyDead unless th.alive?
68
90
  th.exit
69
91
  end
70
- end
92
+ end
71
93
 
94
+ # Returns the associated job for the given +key+.
95
+ #
96
+ # If given an Integer, it will return the +key+ index for the jobs Array.
97
+ #
98
+ # When an instance of Irb is given, it will return the irb session
99
+ # associated with +key+.
100
+ #
101
+ # If given an instance of Thread, it will return the associated thread
102
+ # +key+ using Object#=== on the jobs Array.
103
+ #
104
+ # Otherwise returns the irb session with the same top-level binding as the
105
+ # given +key+.
106
+ #
107
+ # Raises a NoSuchJob exception if no job can be found with the given +key+.
72
108
  def search(key)
73
- case key
109
+ job = case key
74
110
  when Integer
75
111
  @jobs[key]
76
112
  when Irb
@@ -78,12 +114,13 @@ module IRB
78
114
  when Thread
79
115
  @jobs.assoc(key)
80
116
  else
81
- assoc = @jobs.find{|k, v| v.context.main.equal?(key)}
82
- IRB.fail NoSuchJob, key if assoc.nil?
83
- assoc
117
+ @jobs.find{|k, v| v.context.main.equal?(key)}
84
118
  end
119
+ IRB.fail NoSuchJob, key if job.nil?
120
+ job
85
121
  end
86
122
 
123
+ # Deletes the job at the given +key+.
87
124
  def delete(key)
88
125
  case key
89
126
  when Integer
@@ -107,6 +144,7 @@ module IRB
107
144
  @jobs.push assoc
108
145
  end
109
146
 
147
+ # Outputs a list of jobs, see the irb command +irb_jobs+, or +jobs+.
110
148
  def inspect
111
149
  ary = []
112
150
  @jobs.each_index do
@@ -124,8 +162,8 @@ module IRB
124
162
  t_status = "exited"
125
163
  end
126
164
  ary.push format("#%d->%s on %s (%s: %s)",
127
- i,
128
- irb.context.irb_name,
165
+ i,
166
+ irb.context.irb_name,
129
167
  irb.context.main,
130
168
  th,
131
169
  t_status)
@@ -136,22 +174,27 @@ module IRB
136
174
 
137
175
  @JobManager = JobManager.new
138
176
 
177
+ # The current JobManager in the session
139
178
  def IRB.JobManager
140
179
  @JobManager
141
180
  end
142
181
 
182
+ # The current Context in this session
143
183
  def IRB.CurrentContext
144
184
  IRB.JobManager.irb(Thread.current).context
145
185
  end
146
186
 
147
- # invoke multi-irb
187
+ # Creates a new IRB session, see Irb.new.
188
+ #
189
+ # The optional +file+ argument is given to Context.new, along with the
190
+ # workspace created with the remaining arguments, see WorkSpace.new
148
191
  def IRB.irb(file = nil, *main)
149
192
  workspace = WorkSpace.new(*main)
150
193
  parent_thread = Thread.current
151
194
  Thread.start do
152
195
  begin
153
196
  irb = Irb.new(workspace, file)
154
- rescue
197
+ rescue
155
198
  print "Subirb can't start with context(self): ", workspace.main.inspect, "\n"
156
199
  print "return to main irb\n"
157
200
  Thread.pass
@@ -173,12 +216,14 @@ module IRB
173
216
  ensure
174
217
  unless system_exit
175
218
  @JobManager.delete(irb)
176
- if parent_thread.alive?
177
- @JobManager.current_job = @JobManager.irb(parent_thread)
178
- parent_thread.run
179
- else
180
- @JobManager.current_job = @JobManager.main_irb
181
- @JobManager.main_thread.run
219
+ if @JobManager.current_job == irb
220
+ if parent_thread.alive?
221
+ @JobManager.current_job = @JobManager.irb(parent_thread)
222
+ parent_thread.run
223
+ else
224
+ @JobManager.current_job = @JobManager.main_irb
225
+ @JobManager.main_thread.run
226
+ end
182
227
  end
183
228
  end
184
229
  end
@@ -1,10 +1,7 @@
1
- #!/usr/local/bin/ruby
2
- #
3
1
  # save-history.rb -
4
- # $Release Version: 0.9.5$
5
- # $Revision: 11708 $
6
- # $Date: 2007-02-12 15:01:19 -0800 (Mon, 12 Feb 2007) $
7
- # by Keiju ISHITSUKAkeiju@ruby-lang.org)
2
+ # $Release Version: 0.9.6$
3
+ # $Revision$
4
+ # by Keiju ISHITSUKA(keiju@ruby-lang.org)
8
5
  #
9
6
  # --
10
7
  #
@@ -14,57 +11,110 @@
14
11
  require "readline"
15
12
 
16
13
  module IRB
17
- class Context
18
- def init_save_history
19
- hist = IRB.conf[:HISTORY_FILE]
20
- hist = IRB.rc_file("_history") unless hist
21
- if File.exist?(hist)
22
- open(hist) do |f|
23
- f.each {|l| Readline::HISTORY << l.chomp}
24
- end
25
- end
26
-
27
- at_exit do
28
- if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
29
- if hf = IRB.conf[:HISTORY_FILE]
30
- file = File.expand_path(hf)
31
- end
32
- file = IRB.rc_file("_history") unless file
33
-
34
- open(file, 'w' ) do |f|
35
- prev_line = nil
36
- Readline::HISTORY.each do |line|
37
- unless line == prev_line
38
- f.puts line
39
- end
14
+ module HistorySavingAbility # :nodoc:
15
+ @RCS_ID='-$Id$-'
16
+ end
40
17
 
41
- prev_line = line
42
- end
43
- end
44
- end
18
+ class Context
19
+ def init_save_history# :nodoc:
20
+ unless (class<<@io;self;end).include?(HistorySavingAbility)
21
+ @io.extend(HistorySavingAbility)
45
22
  end
46
23
  end
47
24
 
25
+ # A copy of the default <code>IRB.conf[:SAVE_HISTORY]</code>
48
26
  def save_history
49
27
  IRB.conf[:SAVE_HISTORY]
50
28
  end
51
29
 
30
+ # Sets <code>IRB.conf[:SAVE_HISTORY]</code> to the given +val+ and calls
31
+ # #init_save_history with this context.
32
+ #
33
+ # Will store the number of +val+ entries of history in the #history_file
34
+ #
35
+ # Add the following to your +.irbrc+ to change the number of history
36
+ # entries stored to 1000:
37
+ #
38
+ # IRB.conf[:SAVE_HISTORY] = 1000
52
39
  def save_history=(val)
53
40
  IRB.conf[:SAVE_HISTORY] = val
54
41
  if val
55
- main_context = IRB.conf[:MAIN_CONTEXT]
56
- main_context = self unless main_context
57
- main_context.init_save_history
42
+ main_context = IRB.conf[:MAIN_CONTEXT]
43
+ main_context = self unless main_context
44
+ main_context.init_save_history
58
45
  end
59
46
  end
60
47
 
48
+ # A copy of the default <code>IRB.conf[:HISTORY_FILE]</code>
61
49
  def history_file
62
50
  IRB.conf[:HISTORY_FILE]
63
51
  end
64
52
 
53
+ # Set <code>IRB.conf[:HISTORY_FILE]</code> to the given +hist+.
65
54
  def history_file=(hist)
66
55
  IRB.conf[:HISTORY_FILE] = hist
67
56
  end
68
57
  end
69
- end
70
58
 
59
+ module HistorySavingAbility # :nodoc:
60
+ include Readline
61
+
62
+ # def HistorySavingAbility.create_finalizer
63
+ # proc do
64
+ # if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
65
+ # if hf = IRB.conf[:HISTORY_FILE]
66
+ # file = File.expand_path(hf)
67
+ # end
68
+ # file = IRB.rc_file("_history") unless file
69
+ # open(file, 'w' ) do |f|
70
+ # hist = HISTORY.to_a
71
+ # f.puts(hist[-num..-1] || hist)
72
+ # end
73
+ # end
74
+ # end
75
+ # end
76
+
77
+ def HistorySavingAbility.extended(obj)
78
+ # ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
79
+ IRB.conf[:AT_EXIT].push proc{obj.save_history}
80
+ obj.load_history
81
+ obj
82
+ end
83
+
84
+ def load_history
85
+ if history_file = IRB.conf[:HISTORY_FILE]
86
+ history_file = File.expand_path(history_file)
87
+ end
88
+ history_file = IRB.rc_file("_history") unless history_file
89
+ if File.exist?(history_file)
90
+ open(history_file) do |f|
91
+ f.each {|l| HISTORY << l.chomp}
92
+ end
93
+ end
94
+ end
95
+
96
+ def save_history
97
+ if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
98
+ if history_file = IRB.conf[:HISTORY_FILE]
99
+ history_file = File.expand_path(history_file)
100
+ end
101
+ history_file = IRB.rc_file("_history") unless history_file
102
+
103
+ # Change the permission of a file that already exists[BUG #7694]
104
+ begin
105
+ if File.stat(history_file).mode & 066 != 0
106
+ File.chmod(0600, history_file)
107
+ end
108
+ rescue Errno::ENOENT
109
+ rescue
110
+ raise
111
+ end
112
+
113
+ open(history_file, 'w', 0600 ) do |f|
114
+ hist = HISTORY.to_a
115
+ f.puts(hist[-num..-1] || hist)
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end