irb 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,8 @@
11
11
  #
12
12
  require_relative 'src_encoding'
13
13
  require_relative 'magic-file'
14
+ require_relative 'completion'
15
+ require 'reline'
14
16
 
15
17
  module IRB
16
18
  STDIN_FILE_NAME = "(line)" # :nodoc:
@@ -30,7 +32,7 @@ module IRB
30
32
  #
31
33
  # See IO#gets for more information.
32
34
  def gets
33
- IRB.fail NotImplementedError, "gets"
35
+ fail NotImplementedError, "gets"
34
36
  end
35
37
  public :gets
36
38
 
@@ -116,8 +118,7 @@ module IRB
116
118
  # See IO#gets for more information.
117
119
  def gets
118
120
  print @prompt
119
- l = @io.gets
120
- l
121
+ @io.gets
121
122
  end
122
123
 
123
124
  # The external encoding for standard input.
@@ -140,6 +141,12 @@ module IRB
140
141
 
141
142
  @stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
142
143
  @stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
144
+
145
+ if Readline.respond_to?("basic_word_break_characters=")
146
+ Readline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS
147
+ end
148
+ Readline.completion_append_character = nil
149
+ Readline.completion_proc = IRB::InputCompletor::CompletionProc
143
150
  end
144
151
 
145
152
  # Reads the next line from this input method.
@@ -186,7 +193,105 @@ module IRB
186
193
  def encoding
187
194
  @stdin.external_encoding
188
195
  end
196
+
197
+ if Readline.respond_to?("basic_word_break_characters=")
198
+ Readline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS
199
+ end
200
+ Readline.completion_append_character = nil
201
+ Readline.completion_proc = IRB::InputCompletor::CompletionProc
189
202
  end
190
203
  rescue LoadError
191
204
  end
205
+
206
+ class ReidlineInputMethod < InputMethod
207
+ include Reline
208
+ # Creates a new input method object using Readline
209
+ def initialize
210
+ super
211
+
212
+ @line_no = 0
213
+ @line = []
214
+ @eof = false
215
+
216
+ @stdin = ::IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
217
+ @stdout = ::IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-")
218
+
219
+ if Reline.respond_to?("basic_word_break_characters=")
220
+ Reline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS
221
+ end
222
+ Reline.completion_append_character = nil
223
+ Reline.completion_proc = IRB::InputCompletor::CompletionProc
224
+ Reline.output_modifier_proc =
225
+ if IRB.conf[:USE_COLORIZE]
226
+ proc do |output, complete:|
227
+ next unless IRB::Color.colorable?
228
+ IRB::Color.colorize_code(output, complete: complete)
229
+ end
230
+ else
231
+ proc do |output|
232
+ Reline::Unicode.escape_for_print(output)
233
+ end
234
+ end
235
+ Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc
236
+ end
237
+
238
+ def check_termination(&block)
239
+ @check_termination_proc = block
240
+ end
241
+
242
+ def dynamic_prompt(&block)
243
+ @prompt_proc = block
244
+ end
245
+
246
+ def auto_indent(&block)
247
+ @auto_indent_proc = block
248
+ end
249
+
250
+ # Reads the next line from this input method.
251
+ #
252
+ # See IO#gets for more information.
253
+ def gets
254
+ Reline.input = @stdin
255
+ Reline.output = @stdout
256
+ Reline.prompt_proc = @prompt_proc
257
+ Reline.auto_indent_proc = @auto_indent_proc if @auto_indent_proc
258
+ if l = readmultiline(@prompt, false, &@check_termination_proc)
259
+ HISTORY.push(l) if !l.empty?
260
+ @line[@line_no += 1] = l + "\n"
261
+ else
262
+ @eof = true
263
+ l
264
+ end
265
+ end
266
+
267
+ # Whether the end of this input method has been reached, returns +true+
268
+ # if there is no more data to read.
269
+ #
270
+ # See IO#eof? for more information.
271
+ def eof?
272
+ @eof
273
+ end
274
+
275
+ # Whether this input method is still readable when there is no more data to
276
+ # read.
277
+ #
278
+ # See IO#eof for more information.
279
+ def readable_after_eof?
280
+ true
281
+ end
282
+
283
+ # Returns the current line number for #io.
284
+ #
285
+ # #line counts the number of times #gets is called.
286
+ #
287
+ # See IO#lineno for more information.
288
+ def line(line_no)
289
+ @line[line_no]
290
+ end
291
+
292
+ # The external encoding for standard input.
293
+ def encoding
294
+ @stdin.external_encoding
295
+ end
296
+ end
192
297
  end
@@ -106,12 +106,22 @@ module IRB # :nodoc:
106
106
  Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
107
107
  Inspector.def_inspector([true, :p, :inspect]){|v|
108
108
  begin
109
- v.inspect
109
+ result = v.inspect
110
+ if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
111
+ result = Color.colorize_code(result)
112
+ end
113
+ result
110
114
  rescue NoMethodError
111
115
  puts "(Object doesn't support #inspect)"
112
116
  end
113
117
  }
114
- Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v| v.pretty_inspect.chomp}
118
+ Inspector.def_inspector([:pp, :pretty_inspect], proc{require "pp"}){|v|
119
+ result = v.pretty_inspect.chomp
120
+ if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
121
+ result = Color.colorize_code(result)
122
+ end
123
+ result
124
+ }
115
125
  Inspector.def_inspector([:yaml, :YAML], proc{require "yaml"}){|v|
116
126
  begin
117
127
  YAML.dump(v)
@@ -9,24 +9,63 @@
9
9
  #
10
10
  #
11
11
  #
12
- require "e2mmap"
13
12
 
14
13
  # :stopdoc:
15
14
  module IRB
16
-
17
- # exceptions
18
- extend Exception2MessageMapper
19
- def_exception :UnrecognizedSwitch, "Unrecognized switch: %s"
20
- def_exception :NotImplementedError, "Need to define `%s'"
21
- def_exception :CantReturnToNormalMode, "Can't return to normal mode."
22
- def_exception :IllegalParameter, "Invalid parameter(%s)."
23
- def_exception :IrbAlreadyDead, "Irb is already dead."
24
- def_exception :IrbSwitchedToCurrentThread, "Switched to current thread."
25
- def_exception :NoSuchJob, "No such job(%s)."
26
- def_exception :CantShiftToMultiIrbMode, "Can't shift to multi irb mode."
27
- def_exception :CantChangeBinding, "Can't change binding to (%s)."
28
- def_exception :UndefinedPromptMode, "Undefined prompt mode(%s)."
29
- def_exception :IllegalRCGenerator, 'Define illegal RC_NAME_GENERATOR.'
30
-
15
+ class UnrecognizedSwitch < StandardError
16
+ def initialize(val)
17
+ super("Unrecognized switch: #{val}")
18
+ end
19
+ end
20
+ class NotImplementedError < StandardError
21
+ def initialize(val)
22
+ super("Need to define `#{val}'")
23
+ end
24
+ end
25
+ class CantReturnToNormalMode < StandardError
26
+ def initialize
27
+ super("Can't return to normal mode.")
28
+ end
29
+ end
30
+ class IllegalParameter < StandardError
31
+ def initialize(val)
32
+ super("Invalid parameter(#{val}).")
33
+ end
34
+ end
35
+ class IrbAlreadyDead < StandardError
36
+ def initialize
37
+ super("Irb is already dead.")
38
+ end
39
+ end
40
+ class IrbSwitchedToCurrentThread < StandardError
41
+ def initialize
42
+ super("Switched to current thread.")
43
+ end
44
+ end
45
+ class NoSuchJob < StandardError
46
+ def initialize(val)
47
+ super("No such job(#{val}).")
48
+ end
49
+ end
50
+ class CantShiftToMultiIrbMode < StandardError
51
+ def initialize
52
+ super("Can't shift to multi irb mode.")
53
+ end
54
+ end
55
+ class CantChangeBinding < StandardError
56
+ def initialize(val)
57
+ super("Can't change binding to (#{val}).")
58
+ end
59
+ end
60
+ class UndefinedPromptMode < StandardError
61
+ def initialize(val)
62
+ super("Undefined prompt mode(#{val}).")
63
+ end
64
+ end
65
+ class IllegalRCGenerator < StandardError
66
+ def initialize
67
+ super("Define illegal RC_NAME_GENERATOR.")
68
+ end
69
+ end
31
70
  end
32
71
  # :startdoc:
@@ -22,15 +22,19 @@ Usage: irb.rb [options] [programfile] [arguments]
22
22
  when new workspace was created
23
23
  --echo Show result(default)
24
24
  --noecho Don't show result
25
- --inspect Use `inspect' for output (default except for bc mode)
26
- --noinspect Don't use inspect for output
27
- --readline Use Readline extension module
28
- --noreadline Don't use Readline extension module
25
+ --inspect Use `inspect' for output
26
+ --noinspect Don't use inspect for output
27
+ --multiline Use multiline editor module
28
+ --nomultiline Don't use multiline editor module
29
+ --singleline Use singleline editor module
30
+ --nosingleline Don't use singleline editor module
31
+ --colorize Use colorization
32
+ --nocolorize Don't use colorization
29
33
  --prompt prompt-mode/--prompt-mode prompt-mode
30
34
  Switch prompt mode. Pre-defined prompt modes are
31
35
  `default', `simple', `xmp' and `inf-ruby'
32
36
  --inf-ruby-mode Use prompt appropriate for inf-ruby-mode on emacs.
33
- Suppresses --readline.
37
+ Suppresses --multiline and --singleline.
34
38
  --sample-book-mode/--simple-prompt
35
39
  Simple prompt mode
36
40
  --noprompt No prompt mode
@@ -39,7 +43,6 @@ Usage: irb.rb [options] [programfile] [arguments]
39
43
  --back-trace-limit n
40
44
  Display backtrace top n and tail n. The default
41
45
  value is 16.
42
- --irb_debug n Set internal debug level to n (not for popular use)
43
46
  --verbose Show details
44
47
  --noverbose Don't show details
45
48
  -v, --version Print the version of irb
@@ -9,23 +9,64 @@
9
9
  #
10
10
  #
11
11
  #
12
- require "e2mmap"
13
12
 
14
13
  # :stopdoc:
15
14
  module IRB
16
- # exceptions
17
- extend Exception2MessageMapper
18
- def_exception :UnrecognizedSwitch, 'スイッチ(%s)が分りません'
19
- def_exception :NotImplementedError, '`%s\'の定義が必要です'
20
- def_exception :CantReturnToNormalMode, 'Normalモードに戻れません.'
21
- def_exception :IllegalParameter, 'パラメータ(%s)が間違っています.'
22
- def_exception :IrbAlreadyDead, 'Irbは既に死んでいます.'
23
- def_exception :IrbSwitchedToCurrentThread, 'カレントスレッドに切り替わりました.'
24
- def_exception :NoSuchJob, 'そのようなジョブ(%s)はありません.'
25
- def_exception :CantShiftToMultiIrbMode, 'multi-irb modeに移れません.'
26
- def_exception :CantChangeBinding, 'バインディング(%s)に変更できません.'
27
- def_exception :UndefinedPromptMode, 'プロンプトモード(%s)は定義されていません.'
28
- def_exception :IllegalRCNameGenerator, 'RC_NAME_GENERATORが正しく定義されていません.'
15
+ class UnrecognizedSwitch < StandardError
16
+ def initialize(val)
17
+ super("スイッチ(#{val})が分りません")
18
+ end
19
+ end
20
+ class NotImplementedError < StandardError
21
+ def initialize(val)
22
+ super("`#{val}'の定義が必要です")
23
+ end
24
+ end
25
+ class CantReturnToNormalMode < StandardError
26
+ def initialize
27
+ super("Normalモードに戻れません.")
28
+ end
29
+ end
30
+ class IllegalParameter < StandardError
31
+ def initialize(val)
32
+ super("パラメータ(#{val})が間違っています.")
33
+ end
34
+ end
35
+ class IrbAlreadyDead < StandardError
36
+ def initialize
37
+ super("Irbは既に死んでいます.")
38
+ end
39
+ end
40
+ class IrbSwitchedToCurrentThread < StandardError
41
+ def initialize
42
+ super("カレントスレッドに切り替わりました.")
43
+ end
44
+ end
45
+ class NoSuchJob < StandardError
46
+ def initialize(val)
47
+ super("そのようなジョブ(#{val})はありません.")
48
+ end
49
+ end
50
+ class CantShiftToMultiIrbMode < StandardError
51
+ def initialize
52
+ super("multi-irb modeに移れません.")
53
+ end
54
+ end
55
+ class CantChangeBinding < StandardError
56
+ def initialize(val)
57
+ super("バインディング(#{val})に変更できません.")
58
+ end
59
+ end
60
+ class UndefinedPromptMode < StandardError
61
+ def initialize(val)
62
+ super("プロンプトモード(#{val})は定義されていません.")
63
+ end
64
+ end
65
+ class IllegalRCGenerator < StandardError
66
+ def initialize
67
+ super("RC_NAME_GENERATORが正しく定義されていません.")
68
+ end
69
+ end
29
70
  end
30
71
  # :startdoc:
31
72
  # vim:fileencoding=utf-8
@@ -21,16 +21,21 @@ Usage: irb.rb [options] [programfile] [arguments]
21
21
  オブジェクトの作成方法を 0 から 3 のいずれかに設定する.
22
22
  --echo 実行結果を表示する(デフォルト).
23
23
  --noecho 実行結果を表示しない.
24
- --inspect 結果出力にinspectを用いる(bcモード以外はデフォルト).
24
+ --inspect 結果出力にinspectを用いる.
25
25
  --noinspect 結果出力にinspectを用いない.
26
- --readline readlineライブラリを利用する.
27
- --noreadline readlineライブラリを利用しない.
26
+ --multiline マルチラインエディタを利用する.
27
+ --nomultiline マルチラインエディタを利用しない.
28
+ --singleline シングルラインエディタを利用する.
29
+ --nosingleline シングルラインエディタを利用しない.
30
+ --colorize 色付けを利用する.
31
+ --nocolorize 色付けを利用しない.
28
32
  --prompt prompt-mode/--prompt-mode prompt-mode
29
33
  プロンプトモードを切替えます. 現在定義されているプ
30
34
  ロンプトモードは, default, simple, xmp, inf-rubyが
31
35
  用意されています.
32
36
  --inf-ruby-mode emacsのinf-ruby-mode用のプロンプト表示を行なう. 特
33
- に指定がない限り, readlineライブラリは使わなくなる.
37
+ に指定がない限り, シングルラインエディタとマルチラ
38
+ インエディタは使わなくなる.
34
39
  --sample-book-mode/--simple-prompt
35
40
  非常にシンプルなプロンプトを用いるモードです.
36
41
  --noprompt プロンプト表示を行なわない.
@@ -41,8 +46,6 @@ Usage: irb.rb [options] [programfile] [arguments]
41
46
  バックトレース表示をバックトレースの頭から n, 後ろ
42
47
  からnだけ行なう. デフォルトは16
43
48
 
44
- --irb_debug n irbのデバッグレベルをnに設定する(非推奨).
45
-
46
49
  --verbose 詳細なメッセージを出力する.
47
50
  --noverbose 詳細なメッセージを出力しない(デフォルト).
48
51
  -v, --version irbのバージョンを表示する.
@@ -21,6 +21,7 @@ module IRB # :nodoc:
21
21
  LOCALE_DIR = "/lc/"
22
22
 
23
23
  @@legacy_encoding_alias_map = {}.freeze
24
+ @@loaded = []
24
25
 
25
26
  def initialize(locale = nil)
26
27
  @lang = @territory = @encoding_name = @modifier = nil
@@ -107,7 +108,10 @@ module IRB # :nodoc:
107
108
  def load(file, priv=nil)
108
109
  found = find(file)
109
110
  if found
110
- return real_load(found, priv)
111
+ unless @@loaded.include?(found)
112
+ @@loaded << found # cache
113
+ return real_load(found, priv)
114
+ end
111
115
  else
112
116
  raise LoadError, "No such file to load -- #{file}"
113
117
  end
@@ -10,17 +10,21 @@
10
10
  #
11
11
  #
12
12
 
13
- require "e2mmap"
14
13
  require_relative "output-method"
15
14
 
16
15
  module IRB
17
16
  # An output formatter used internally by the lexer.
18
17
  module Notifier
19
- extend Exception2MessageMapper
20
- def_exception :ErrUndefinedNotifier,
21
- "undefined notifier level: %d is specified"
22
- def_exception :ErrUnrecognizedLevel,
23
- "unrecognized notifier level: %s is specified"
18
+ class ErrUndefinedNotifier < StandardError
19
+ def initialize(val)
20
+ super("undefined notifier level: #{val} is specified")
21
+ end
22
+ end
23
+ class ErrUnrecognizedLevel < StandardError
24
+ def initialize(val)
25
+ super("unrecognized notifier level: #{val} is specified")
26
+ end
27
+ end
24
28
 
25
29
  # Define a new Notifier output source, returning a new CompositeNotifier
26
30
  # with the given +prefix+ and +output_method+.
@@ -162,10 +166,10 @@ module IRB
162
166
  @level_notifier = value
163
167
  when Integer
164
168
  l = @notifiers[value]
165
- Notifier.Raise ErrUndefinedNotifier, value unless l
169
+ raise ErrUndefinedNotifier, value unless l
166
170
  @level_notifier = l
167
171
  else
168
- Notifier.Raise ErrUnrecognizedLevel, value unless l
172
+ raise ErrUnrecognizedLevel, value unless l
169
173
  end
170
174
  end
171
175