irb 1.1.0.pre.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +10 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +55 -0
  5. data/Rakefile +10 -0
  6. data/bin/console +6 -0
  7. data/bin/setup +6 -0
  8. data/doc/irb/irb-tools.rd.ja +184 -0
  9. data/doc/irb/irb.rd.ja +411 -0
  10. data/exe/irb +11 -0
  11. data/irb.gemspec +84 -0
  12. data/lib/irb.rb +870 -0
  13. data/lib/irb/cmd/chws.rb +34 -0
  14. data/lib/irb/cmd/fork.rb +39 -0
  15. data/lib/irb/cmd/help.rb +46 -0
  16. data/lib/irb/cmd/load.rb +67 -0
  17. data/lib/irb/cmd/nop.rb +39 -0
  18. data/lib/irb/cmd/pushws.rb +41 -0
  19. data/lib/irb/cmd/subirb.rb +43 -0
  20. data/lib/irb/color.rb +233 -0
  21. data/lib/irb/completion.rb +339 -0
  22. data/lib/irb/context.rb +458 -0
  23. data/lib/irb/ext/change-ws.rb +46 -0
  24. data/lib/irb/ext/history.rb +157 -0
  25. data/lib/irb/ext/loader.rb +129 -0
  26. data/lib/irb/ext/multi-irb.rb +265 -0
  27. data/lib/irb/ext/save-history.rb +117 -0
  28. data/lib/irb/ext/tracer.rb +72 -0
  29. data/lib/irb/ext/use-loader.rb +77 -0
  30. data/lib/irb/ext/workspaces.rb +67 -0
  31. data/lib/irb/extend-command.rb +328 -0
  32. data/lib/irb/frame.rb +81 -0
  33. data/lib/irb/help.rb +37 -0
  34. data/lib/irb/init.rb +312 -0
  35. data/lib/irb/input-method.rb +298 -0
  36. data/lib/irb/inspector.rb +142 -0
  37. data/lib/irb/lc/.document +4 -0
  38. data/lib/irb/lc/error.rb +32 -0
  39. data/lib/irb/lc/help-message +52 -0
  40. data/lib/irb/lc/ja/encoding_aliases.rb +11 -0
  41. data/lib/irb/lc/ja/error.rb +31 -0
  42. data/lib/irb/lc/ja/help-message +55 -0
  43. data/lib/irb/locale.rb +182 -0
  44. data/lib/irb/magic-file.rb +38 -0
  45. data/lib/irb/notifier.rb +232 -0
  46. data/lib/irb/output-method.rb +92 -0
  47. data/lib/irb/ruby-lex.rb +499 -0
  48. data/lib/irb/ruby_logo.aa +38 -0
  49. data/lib/irb/slex.rb +282 -0
  50. data/lib/irb/src_encoding.rb +7 -0
  51. data/lib/irb/version.rb +17 -0
  52. data/lib/irb/workspace.rb +181 -0
  53. data/lib/irb/ws-for-case-2.rb +15 -0
  54. data/lib/irb/xmp.rb +170 -0
  55. data/man/irb.1 +207 -0
  56. metadata +140 -0
@@ -0,0 +1,339 @@
1
+ # frozen_string_literal: false
2
+ #
3
+ # irb/completion.rb -
4
+ # $Release Version: 0.9$
5
+ # $Revision$
6
+ # by Keiju ISHITSUKA(keiju@ishitsuka.com)
7
+ # From Original Idea of shugo@ruby-lang.org
8
+ #
9
+
10
+ require "readline"
11
+ autoload :RDoc, "rdoc"
12
+
13
+ module IRB
14
+ module InputCompletor # :nodoc:
15
+
16
+
17
+ # Set of reserved words used by Ruby, you should not use these for
18
+ # constants or variables
19
+ ReservedWords = %w[
20
+ BEGIN END
21
+ alias and
22
+ begin break
23
+ case class
24
+ def defined do
25
+ else elsif end ensure
26
+ false for
27
+ if in
28
+ module
29
+ next nil not
30
+ or
31
+ redo rescue retry return
32
+ self super
33
+ then true
34
+ undef unless until
35
+ when while
36
+ yield
37
+ ]
38
+
39
+ BASIC_WORD_BREAK_CHARACTERS = " \t\n`><=;|&{("
40
+
41
+ CompletionProc = proc { |input|
42
+ retrieve_completion_data(input).compact.map{ |i| i.encode(Encoding.default_external) }
43
+ }
44
+
45
+ def self.retrieve_completion_data(input, doc_namespace = false)
46
+ bind = IRB.conf[:MAIN_CONTEXT].workspace.binding
47
+
48
+ case input
49
+ when /^((["'`]).*\2)\.([^.]*)$/
50
+ # String
51
+ receiver = $1
52
+ message = Regexp.quote($3)
53
+
54
+ candidates = String.instance_methods.collect{|m| m.to_s}
55
+ if doc_namespace
56
+ "String.#{message}"
57
+ else
58
+ select_message(receiver, message, candidates)
59
+ end
60
+
61
+ when /^(\/[^\/]*\/)\.([^.]*)$/
62
+ # Regexp
63
+ receiver = $1
64
+ message = Regexp.quote($2)
65
+
66
+ candidates = Regexp.instance_methods.collect{|m| m.to_s}
67
+ if doc_namespace
68
+ "Regexp.#{message}"
69
+ else
70
+ select_message(receiver, message, candidates)
71
+ end
72
+
73
+ when /^([^\]]*\])\.([^.]*)$/
74
+ # Array
75
+ receiver = $1
76
+ message = Regexp.quote($2)
77
+
78
+ candidates = Array.instance_methods.collect{|m| m.to_s}
79
+ if doc_namespace
80
+ "Array.#{message}"
81
+ else
82
+ select_message(receiver, message, candidates)
83
+ end
84
+
85
+ when /^([^\}]*\})\.([^.]*)$/
86
+ # Proc or Hash
87
+ receiver = $1
88
+ message = Regexp.quote($2)
89
+
90
+ proc_candidates = Proc.instance_methods.collect{|m| m.to_s}
91
+ hash_candidates = Hash.instance_methods.collect{|m| m.to_s}
92
+ if doc_namespace
93
+ ["Proc.#{message}", "Hash.#{message}"]
94
+ else
95
+ select_message(receiver, message, proc_candidates | hash_candidates)
96
+ end
97
+
98
+ when /^(:[^:.]*)$/
99
+ # Symbol
100
+ return nil if doc_namespace
101
+ if Symbol.respond_to?(:all_symbols)
102
+ sym = $1
103
+ candidates = Symbol.all_symbols.collect{|s| ":" + s.id2name}
104
+ candidates.grep(/^#{Regexp.quote(sym)}/)
105
+ else
106
+ []
107
+ end
108
+
109
+ when /^::([A-Z][^:\.\(]*)$/
110
+ # Absolute Constant or class methods
111
+ receiver = $1
112
+ candidates = Object.constants.collect{|m| m.to_s}
113
+ if doc_namespace
114
+ candidates.find { |i| i == receiver }
115
+ else
116
+ candidates.grep(/^#{receiver}/).collect{|e| "::" + e}
117
+ end
118
+
119
+ when /^([A-Z].*)::([^:.]*)$/
120
+ # Constant or class methods
121
+ receiver = $1
122
+ message = Regexp.quote($2)
123
+ begin
124
+ candidates = eval("#{receiver}.constants.collect{|m| m.to_s}", bind)
125
+ candidates |= eval("#{receiver}.methods.collect{|m| m.to_s}", bind)
126
+ rescue Exception
127
+ candidates = []
128
+ end
129
+ if doc_namespace
130
+ "#{receiver}::#{message}"
131
+ else
132
+ select_message(receiver, message, candidates, "::")
133
+ end
134
+
135
+ when /^(:[^:.]+)(\.|::)([^.]*)$/
136
+ # Symbol
137
+ receiver = $1
138
+ sep = $2
139
+ message = Regexp.quote($3)
140
+
141
+ candidates = Symbol.instance_methods.collect{|m| m.to_s}
142
+ if doc_namespace
143
+ "Symbol.#{message}"
144
+ else
145
+ select_message(receiver, message, candidates, sep)
146
+ end
147
+
148
+ when /^(?<num>-?(0[dbo])?[0-9_]+(\.[0-9_]+)?([eE][+-]?[0-9]+i?|r)?)(?<sep>\.|::)(?<mes>[^.]*)$/
149
+ # Numeric
150
+ receiver = $~[:num]
151
+ sep = $~[:sep]
152
+ message = Regexp.quote($~[:mes])
153
+
154
+ begin
155
+ instance = eval(receiver, bind)
156
+ if doc_namespace
157
+ "#{instance.class.name}.#{message}"
158
+ else
159
+ candidates = instance.methods.collect{|m| m.to_s}
160
+ select_message(receiver, message, candidates, sep)
161
+ end
162
+ rescue Exception
163
+ if doc_namespace
164
+ nil
165
+ else
166
+ candidates = []
167
+ end
168
+ end
169
+
170
+ when /^(-?0x[0-9a-fA-F_]+)(\.|::)([^.]*)$/
171
+ # Numeric(0xFFFF)
172
+ receiver = $1
173
+ sep = $2
174
+ message = Regexp.quote($3)
175
+
176
+ begin
177
+ instance = eval(receiver, bind)
178
+ if doc_namespace
179
+ "#{instance.class.name}.#{message}"
180
+ else
181
+ candidates = instance.methods.collect{|m| m.to_s}
182
+ select_message(receiver, message, candidates, sep)
183
+ end
184
+ rescue Exception
185
+ if doc_namespace
186
+ nil
187
+ else
188
+ candidates = []
189
+ end
190
+ end
191
+
192
+ when /^(\$[^.]*)$/
193
+ # global var
194
+ gvar = $1
195
+ all_gvars = global_variables.collect{|m| m.to_s}
196
+ if doc_namespace
197
+ all_gvars.find{ |i| i == gvar }
198
+ else
199
+ all_gvars.grep(Regexp.new(Regexp.quote(gvar)))
200
+ end
201
+
202
+ when /^([^."].*)(\.|::)([^.]*)$/
203
+ # variable.func or func.func
204
+ receiver = $1
205
+ sep = $2
206
+ message = Regexp.quote($3)
207
+
208
+ gv = eval("global_variables", bind).collect{|m| m.to_s}.append("true", "false", "nil")
209
+ lv = eval("local_variables", bind).collect{|m| m.to_s}
210
+ iv = eval("instance_variables", bind).collect{|m| m.to_s}
211
+ cv = eval("self.class.constants", bind).collect{|m| m.to_s}
212
+
213
+ if (gv | lv | iv | cv).include?(receiver) or /^[A-Z]/ =~ receiver && /\./ !~ receiver
214
+ # foo.func and foo is var. OR
215
+ # foo::func and foo is var. OR
216
+ # foo::Const and foo is var. OR
217
+ # Foo::Bar.func
218
+ begin
219
+ candidates = []
220
+ rec = eval(receiver, bind)
221
+ if sep == "::" and rec.kind_of?(Module)
222
+ candidates = rec.constants.collect{|m| m.to_s}
223
+ end
224
+ candidates |= rec.methods.collect{|m| m.to_s}
225
+ rescue Exception
226
+ candidates = []
227
+ end
228
+ else
229
+ # func1.func2
230
+ candidates = []
231
+ to_ignore = ignored_modules
232
+ ObjectSpace.each_object(Module){|m|
233
+ next if (to_ignore.include?(m) rescue true)
234
+ candidates.concat m.instance_methods(false).collect{|x| x.to_s}
235
+ }
236
+ candidates.sort!
237
+ candidates.uniq!
238
+ end
239
+ if doc_namespace
240
+ "#{rec.class.name}#{sep}#{candidates.find{ |i| i == message }}"
241
+ else
242
+ select_message(receiver, message, candidates, sep)
243
+ end
244
+
245
+ when /^\.([^.]*)$/
246
+ # unknown(maybe String)
247
+
248
+ receiver = ""
249
+ message = Regexp.quote($1)
250
+
251
+ candidates = String.instance_methods(true).collect{|m| m.to_s}
252
+ if doc_namespace
253
+ "String.#{candidates.find{ |i| i == message }}"
254
+ else
255
+ select_message(receiver, message, candidates)
256
+ end
257
+
258
+ else
259
+ candidates = eval("methods | private_methods | local_variables | instance_variables | self.class.constants", bind).collect{|m| m.to_s}
260
+ conditions |= ReservedWords
261
+
262
+ if doc_namespace
263
+ candidates.find{ |i| i == input }
264
+ else
265
+ candidates.grep(/^#{Regexp.quote(input)}/)
266
+ end
267
+ end
268
+ end
269
+
270
+ PerfectMatchedProc = ->(matched) {
271
+ RDocRIDriver ||= RDoc::RI::Driver.new
272
+ if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
273
+ File.open(File.join(__dir__, 'ruby_logo.aa')) do |f|
274
+ RDocRIDriver.page do |io|
275
+ IO.copy_stream(f, io)
276
+ end
277
+ end
278
+ return
279
+ end
280
+ namespace = retrieve_completion_data(matched, true)
281
+ return unless matched
282
+ if namespace.is_a?(Array)
283
+ out = RDoc::Markup::Document.new
284
+ namespace.each do |m|
285
+ begin
286
+ RDocRIDriver.add_method(out, m)
287
+ rescue RDoc::RI::Driver::NotFoundError
288
+ end
289
+ end
290
+ RDocRIDriver.display(out)
291
+ else
292
+ begin
293
+ RDocRIDriver.display_names([namespace])
294
+ rescue RDoc::RI::Driver::NotFoundError
295
+ end
296
+ end
297
+ }
298
+
299
+ # Set of available operators in Ruby
300
+ Operators = %w[% & * ** + - / < << <= <=> == === =~ > >= >> [] []= ^ ! != !~]
301
+
302
+ def self.select_message(receiver, message, candidates, sep = ".")
303
+ candidates.grep(/^#{message}/).collect do |e|
304
+ case e
305
+ when /^[a-zA-Z_]/
306
+ receiver + sep + e
307
+ when /^[0-9]/
308
+ when *Operators
309
+ #receiver + " " + e
310
+ end
311
+ end
312
+ end
313
+
314
+ def self.ignored_modules
315
+ # We could cache the result, but this is very fast already.
316
+ # By using this approach, we avoid Module#name calls, which are
317
+ # relatively slow when there are a lot of anonymous modules defined.
318
+ s = {}
319
+
320
+ scanner = lambda do |m|
321
+ next if s.include?(m) # IRB::ExtendCommandBundle::EXCB recurses.
322
+ s[m] = true
323
+ m.constants(false).each do |c|
324
+ value = m.const_get(c)
325
+ scanner.call(value) if value.is_a?(Module)
326
+ end
327
+ end
328
+
329
+ %i(IRB SLex RubyLex RubyToken).each do |sym|
330
+ next unless Object.const_defined?(sym)
331
+ scanner.call(Object.const_get(sym))
332
+ end
333
+
334
+ s.delete(IRB::Context) if defined?(IRB::Context)
335
+
336
+ s
337
+ end
338
+ end
339
+ end
@@ -0,0 +1,458 @@
1
+ # frozen_string_literal: false
2
+ #
3
+ # irb/context.rb - irb context
4
+ # $Release Version: 0.9.6$
5
+ # $Revision$
6
+ # by Keiju ISHITSUKA(keiju@ruby-lang.org)
7
+ #
8
+ # --
9
+ #
10
+ #
11
+ #
12
+ require_relative "workspace"
13
+ require_relative "inspector"
14
+ require_relative "input-method"
15
+ require_relative "output-method"
16
+
17
+ module IRB
18
+ # A class that wraps the current state of the irb session, including the
19
+ # configuration of IRB.conf.
20
+ class Context
21
+ # Creates a new IRB context.
22
+ #
23
+ # The optional +input_method+ argument:
24
+ #
25
+ # +nil+:: uses stdin or Reidline or Readline
26
+ # +String+:: uses a File
27
+ # +other+:: uses this as InputMethod
28
+ def initialize(irb, workspace = nil, input_method = nil)
29
+ @irb = irb
30
+ if workspace
31
+ @workspace = workspace
32
+ else
33
+ @workspace = WorkSpace.new
34
+ end
35
+ @thread = Thread.current if defined? Thread
36
+
37
+ # copy of default configuration
38
+ @ap_name = IRB.conf[:AP_NAME]
39
+ @rc = IRB.conf[:RC]
40
+ @load_modules = IRB.conf[:LOAD_MODULES]
41
+
42
+ if IRB.conf.has_key?(:USE_SINGLELINE)
43
+ @use_singleline = IRB.conf[:USE_SINGLELINE]
44
+ elsif IRB.conf.has_key?(:USE_READLINE) # backward compatibility
45
+ @use_singleline = IRB.conf[:USE_READLINE]
46
+ else
47
+ @use_singleline = nil
48
+ end
49
+ if IRB.conf.has_key?(:USE_MULTILINE)
50
+ @use_multiline = IRB.conf[:USE_MULTILINE]
51
+ elsif IRB.conf.has_key?(:USE_REIDLINE) # backward compatibility
52
+ @use_multiline = IRB.conf[:USE_REIDLINE]
53
+ else
54
+ @use_multiline = nil
55
+ end
56
+ @use_colorize = IRB.conf[:USE_COLORIZE]
57
+ @verbose = IRB.conf[:VERBOSE]
58
+ @io = nil
59
+
60
+ self.inspect_mode = IRB.conf[:INSPECT_MODE]
61
+ self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
62
+ self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
63
+ self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]
64
+
65
+ @ignore_sigint = IRB.conf[:IGNORE_SIGINT]
66
+ @ignore_eof = IRB.conf[:IGNORE_EOF]
67
+
68
+ @back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
69
+
70
+ self.prompt_mode = IRB.conf[:PROMPT_MODE]
71
+
72
+ if IRB.conf[:SINGLE_IRB] or !defined?(IRB::JobManager)
73
+ @irb_name = IRB.conf[:IRB_NAME]
74
+ else
75
+ @irb_name = IRB.conf[:IRB_NAME]+"#"+IRB.JobManager.n_jobs.to_s
76
+ end
77
+ @irb_path = "(" + @irb_name + ")"
78
+
79
+ case input_method
80
+ when nil
81
+ @io = nil
82
+ case use_multiline?
83
+ when nil
84
+ if STDIN.tty? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && !use_singleline?
85
+ @io = ReidlineInputMethod.new
86
+ else
87
+ @io = nil
88
+ end
89
+ when false
90
+ @io = nil
91
+ when true
92
+ @io = ReidlineInputMethod.new
93
+ end
94
+ unless @io
95
+ case use_singleline?
96
+ when nil
97
+ if (defined?(ReadlineInputMethod) && STDIN.tty? &&
98
+ IRB.conf[:PROMPT_MODE] != :INF_RUBY)
99
+ @io = ReadlineInputMethod.new
100
+ else
101
+ @io = nil
102
+ end
103
+ when false
104
+ @io = nil
105
+ when true
106
+ if defined?(ReadlineInputMethod)
107
+ @io = ReadlineInputMethod.new
108
+ else
109
+ @io = nil
110
+ end
111
+ else
112
+ @io = nil
113
+ end
114
+ end
115
+ @io = StdioInputMethod.new unless @io
116
+
117
+ when String
118
+ @io = FileInputMethod.new(input_method)
119
+ @irb_name = File.basename(input_method)
120
+ @irb_path = input_method
121
+ else
122
+ @io = input_method
123
+ end
124
+ self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]
125
+
126
+ @echo = IRB.conf[:ECHO]
127
+ if @echo.nil?
128
+ @echo = true
129
+ end
130
+
131
+ @echo_on_assignment = IRB.conf[:ECHO_ON_ASSIGNMENT]
132
+ if @echo_on_assignment.nil?
133
+ @echo_on_assignment = false
134
+ end
135
+ end
136
+
137
+ # The top-level workspace, see WorkSpace#main
138
+ def main
139
+ @workspace.main
140
+ end
141
+
142
+ # The toplevel workspace, see #home_workspace
143
+ attr_reader :workspace_home
144
+ # WorkSpace in the current context
145
+ attr_accessor :workspace
146
+ # The current thread in this context
147
+ attr_reader :thread
148
+ # The current input method
149
+ #
150
+ # Can be either StdioInputMethod, ReadlineInputMethod,
151
+ # ReidlineInputMethod, FileInputMethod or other specified when the
152
+ # context is created. See ::new for more # information on +input_method+.
153
+ attr_accessor :io
154
+
155
+ # Current irb session
156
+ attr_accessor :irb
157
+ # A copy of the default <code>IRB.conf[:AP_NAME]</code>
158
+ attr_accessor :ap_name
159
+ # A copy of the default <code>IRB.conf[:RC]</code>
160
+ attr_accessor :rc
161
+ # A copy of the default <code>IRB.conf[:LOAD_MODULES]</code>
162
+ attr_accessor :load_modules
163
+ # Can be either name from <code>IRB.conf[:IRB_NAME]</code>, or the number of
164
+ # the current job set by JobManager, such as <code>irb#2</code>
165
+ attr_accessor :irb_name
166
+ # Can be either the #irb_name surrounded by parenthesis, or the
167
+ # +input_method+ passed to Context.new
168
+ attr_accessor :irb_path
169
+
170
+ # Whether multiline editor mode is enabled or not.
171
+ #
172
+ # A copy of the default <code>IRB.conf[:USE_MULTILINE]</code>
173
+ attr_reader :use_multiline
174
+ # Whether singleline editor mode is enabled or not.
175
+ #
176
+ # A copy of the default <code>IRB.conf[:USE_SINGLELINE]</code>
177
+ attr_reader :use_singleline
178
+ # Whether colorization is enabled or not.
179
+ #
180
+ # A copy of the default <code>IRB.conf[:USE_COLORIZE]</code>
181
+ attr_reader :use_colorize
182
+ # A copy of the default <code>IRB.conf[:INSPECT_MODE]</code>
183
+ attr_reader :inspect_mode
184
+
185
+ # A copy of the default <code>IRB.conf[:PROMPT_MODE]</code>
186
+ attr_reader :prompt_mode
187
+ # Standard IRB prompt
188
+ #
189
+ # See IRB@Customizing+the+IRB+Prompt for more information.
190
+ attr_accessor :prompt_i
191
+ # IRB prompt for continuated strings
192
+ #
193
+ # See IRB@Customizing+the+IRB+Prompt for more information.
194
+ attr_accessor :prompt_s
195
+ # IRB prompt for continuated statement (e.g. immediately after an +if+)
196
+ #
197
+ # See IRB@Customizing+the+IRB+Prompt for more information.
198
+ attr_accessor :prompt_c
199
+ # See IRB@Customizing+the+IRB+Prompt for more information.
200
+ attr_accessor :prompt_n
201
+ # Can be either the default <code>IRB.conf[:AUTO_INDENT]</code>, or the
202
+ # mode set by #prompt_mode=
203
+ #
204
+ # To disable auto-indentation in irb:
205
+ #
206
+ # IRB.conf[:AUTO_INDENT] = false
207
+ #
208
+ # or
209
+ #
210
+ # irb_context.auto_indent_mode = false
211
+ #
212
+ # or
213
+ #
214
+ # IRB.CurrentContext.auto_indent_mode = false
215
+ #
216
+ # See IRB@Configuration for more information.
217
+ attr_accessor :auto_indent_mode
218
+ # The format of the return statement, set by #prompt_mode= using the
219
+ # +:RETURN+ of the +mode+ passed to set the current #prompt_mode.
220
+ attr_accessor :return_format
221
+
222
+ # Whether <code>^C</code> (+control-c+) will be ignored or not.
223
+ #
224
+ # If set to +false+, <code>^C</code> will quit irb.
225
+ #
226
+ # If set to +true+,
227
+ #
228
+ # * during input: cancel input then return to top level.
229
+ # * during execute: abandon current execution.
230
+ attr_accessor :ignore_sigint
231
+ # Whether <code>^D</code> (+control-d+) will be ignored or not.
232
+ #
233
+ # If set to +false+, <code>^D</code> will quit irb.
234
+ attr_accessor :ignore_eof
235
+ # Whether to echo the return value to output or not.
236
+ #
237
+ # Uses IRB.conf[:ECHO] if available, or defaults to +true+.
238
+ #
239
+ # puts "hello"
240
+ # # hello
241
+ # #=> nil
242
+ # IRB.CurrentContext.echo = false
243
+ # puts "omg"
244
+ # # omg
245
+ attr_accessor :echo
246
+ # Whether to echo for assignment expressions
247
+ #
248
+ # Uses IRB.conf[:ECHO_ON_ASSIGNMENT] if available, or defaults to +false+.
249
+ #
250
+ # a = "omg"
251
+ # IRB.CurrentContext.echo_on_assignment = true
252
+ # a = "omg"
253
+ # #=> omg
254
+ attr_accessor :echo_on_assignment
255
+ # Whether verbose messages are displayed or not.
256
+ #
257
+ # A copy of the default <code>IRB.conf[:VERBOSE]</code>
258
+ attr_accessor :verbose
259
+
260
+ # The limit of backtrace lines displayed as top +n+ and tail +n+.
261
+ #
262
+ # The default value is 16.
263
+ #
264
+ # Can also be set using the +--back-trace-limit+ command line option.
265
+ #
266
+ # See IRB@Command+line+options for more command line options.
267
+ attr_accessor :back_trace_limit
268
+
269
+ # Alias for #use_multiline
270
+ alias use_multiline? use_multiline
271
+ # Alias for #use_singleline
272
+ alias use_singleline? use_singleline
273
+ # backward compatibility
274
+ alias use_reidline use_multiline
275
+ # backward compatibility
276
+ alias use_reidline? use_multiline
277
+ # backward compatibility
278
+ alias use_readline use_singleline
279
+ # backward compatibility
280
+ alias use_readline? use_singleline
281
+ # Alias for #use_colorize
282
+ alias use_colorize? use_colorize
283
+ # Alias for #rc
284
+ alias rc? rc
285
+ alias ignore_sigint? ignore_sigint
286
+ alias ignore_eof? ignore_eof
287
+ alias echo? echo
288
+ alias echo_on_assignment? echo_on_assignment
289
+
290
+ # Returns whether messages are displayed or not.
291
+ def verbose?
292
+ if @verbose.nil?
293
+ if @io.kind_of?(ReidlineInputMethod)
294
+ false
295
+ elsif defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)
296
+ false
297
+ elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
298
+ true
299
+ else
300
+ false
301
+ end
302
+ else
303
+ @verbose
304
+ end
305
+ end
306
+
307
+ # Whether #verbose? is +true+, and +input_method+ is either
308
+ # StdioInputMethod or ReidlineInputMethod or ReadlineInputMethod, see #io
309
+ # for more information.
310
+ def prompting?
311
+ verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
312
+ @io.kind_of?(ReidlineInputMethod) ||
313
+ (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
314
+ end
315
+
316
+ # The return value of the last statement evaluated.
317
+ attr_reader :last_value
318
+
319
+ # Sets the return value from the last statement evaluated in this context
320
+ # to #last_value.
321
+ def set_last_value(value)
322
+ @last_value = value
323
+ @workspace.local_variable_set :_, value
324
+ end
325
+
326
+ # Sets the +mode+ of the prompt in this context.
327
+ #
328
+ # See IRB@Customizing+the+IRB+Prompt for more information.
329
+ def prompt_mode=(mode)
330
+ @prompt_mode = mode
331
+ pconf = IRB.conf[:PROMPT][mode]
332
+ @prompt_i = pconf[:PROMPT_I]
333
+ @prompt_s = pconf[:PROMPT_S]
334
+ @prompt_c = pconf[:PROMPT_C]
335
+ @prompt_n = pconf[:PROMPT_N]
336
+ @return_format = pconf[:RETURN]
337
+ if ai = pconf.include?(:AUTO_INDENT)
338
+ @auto_indent_mode = ai
339
+ else
340
+ @auto_indent_mode = IRB.conf[:AUTO_INDENT]
341
+ end
342
+ end
343
+
344
+ # Whether #inspect_mode is set or not, see #inspect_mode= for more detail.
345
+ def inspect?
346
+ @inspect_mode.nil? or @inspect_mode
347
+ end
348
+
349
+ # Whether #io uses a File for the +input_method+ passed when creating the
350
+ # current context, see ::new
351
+ def file_input?
352
+ @io.class == FileInputMethod
353
+ end
354
+
355
+ # Specifies the inspect mode with +opt+:
356
+ #
357
+ # +true+:: display +inspect+
358
+ # +false+:: display +to_s+
359
+ # +nil+:: inspect mode in non-math mode,
360
+ # non-inspect mode in math mode
361
+ #
362
+ # See IRB::Inspector for more information.
363
+ #
364
+ # Can also be set using the +--inspect+ and +--noinspect+ command line
365
+ # options.
366
+ #
367
+ # See IRB@Command+line+options for more command line options.
368
+ def inspect_mode=(opt)
369
+
370
+ if i = Inspector::INSPECTORS[opt]
371
+ @inspect_mode = opt
372
+ @inspect_method = i
373
+ i.init
374
+ else
375
+ case opt
376
+ when nil
377
+ if Inspector.keys_with_inspector(Inspector::INSPECTORS[true]).include?(@inspect_mode)
378
+ self.inspect_mode = false
379
+ elsif Inspector.keys_with_inspector(Inspector::INSPECTORS[false]).include?(@inspect_mode)
380
+ self.inspect_mode = true
381
+ else
382
+ puts "Can't switch inspect mode."
383
+ return
384
+ end
385
+ when /^\s*\{.*\}\s*$/
386
+ begin
387
+ inspector = eval "proc#{opt}"
388
+ rescue Exception
389
+ puts "Can't switch inspect mode(#{opt})."
390
+ return
391
+ end
392
+ self.inspect_mode = inspector
393
+ when Proc
394
+ self.inspect_mode = IRB::Inspector(opt)
395
+ when Inspector
396
+ prefix = "usr%d"
397
+ i = 1
398
+ while Inspector::INSPECTORS[format(prefix, i)]; i += 1; end
399
+ @inspect_mode = format(prefix, i)
400
+ @inspect_method = opt
401
+ Inspector.def_inspector(format(prefix, i), @inspect_method)
402
+ else
403
+ puts "Can't switch inspect mode(#{opt})."
404
+ return
405
+ end
406
+ end
407
+ print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose?
408
+ @inspect_mode
409
+ end
410
+
411
+ def evaluate(line, line_no, exception: nil) # :nodoc:
412
+ @line_no = line_no
413
+ if exception
414
+ line_no -= 1
415
+ line = "begin ::Kernel.raise _; rescue _.class\n#{line}\n""end"
416
+ @workspace.local_variable_set(:_, exception)
417
+ end
418
+ set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
419
+ end
420
+
421
+ def inspect_last_value # :nodoc:
422
+ @inspect_method.inspect_value(@last_value)
423
+ end
424
+
425
+ alias __exit__ exit
426
+ # Exits the current session, see IRB.irb_exit
427
+ def exit(ret = 0)
428
+ IRB.irb_exit(@irb, ret)
429
+ end
430
+
431
+ NOPRINTING_IVARS = ["@last_value"] # :nodoc:
432
+ NO_INSPECTING_IVARS = ["@irb", "@io"] # :nodoc:
433
+ IDNAME_IVARS = ["@prompt_mode"] # :nodoc:
434
+
435
+ alias __inspect__ inspect
436
+ def inspect # :nodoc:
437
+ array = []
438
+ for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
439
+ ivar = ivar.to_s
440
+ name = ivar.sub(/^@(.*)$/, '\1')
441
+ val = instance_eval(ivar)
442
+ case ivar
443
+ when *NOPRINTING_IVARS
444
+ array.push format("conf.%s=%s", name, "...")
445
+ when *NO_INSPECTING_IVARS
446
+ array.push format("conf.%s=%s", name, val.to_s)
447
+ when *IDNAME_IVARS
448
+ array.push format("conf.%s=:%s", name, val.id2name)
449
+ else
450
+ array.push format("conf.%s=%s", name, val.inspect)
451
+ end
452
+ end
453
+ array.join("\n")
454
+ end
455
+ alias __to_s__ to_s
456
+ alias to_s inspect
457
+ end
458
+ end