diff options
Diffstat (limited to 'lib/irb/input-method.rb')
-rw-r--r-- | lib/irb/input-method.rb | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index f491d5a760..e29d282d2c 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -11,6 +11,8 @@ # require_relative 'src_encoding' require_relative 'magic-file' +require_relative "completion" +require 'reline' module IRB STDIN_FILE_NAME = "(line)" # :nodoc: @@ -140,6 +142,12 @@ module IRB @stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") @stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") + + if Readline.respond_to?("basic_word_break_characters=") + Readline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS + end + Readline.completion_append_character = nil + Readline.completion_proc = IRB::InputCompletor::CompletionProc end # Reads the next line from this input method. @@ -186,7 +194,84 @@ module IRB def encoding @stdin.external_encoding end + + if Readline.respond_to?("basic_word_break_characters=") + Readline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS + end + Readline.completion_append_character = nil + Readline.completion_proc = IRB::InputCompletor::CompletionProc end rescue LoadError end + + class ReidlineInputMethod < InputMethod + include Reline + # Creates a new input method object using Readline + def initialize + super + + @line_no = 0 + @line = [] + @eof = false + + @stdin = IO.open(STDIN.to_i, :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") + @stdout = IO.open(STDOUT.to_i, 'w', :external_encoding => IRB.conf[:LC_MESSAGES].encoding, :internal_encoding => "-") + + if Reline.respond_to?("basic_word_break_characters=") + Reline.basic_word_break_characters = IRB::InputCompletor::BASIC_WORD_BREAK_CHARACTERS + end + Reline.completion_append_character = nil + Reline.completion_proc = IRB::InputCompletor::CompletionProc + Reline.dig_perfect_match_proc = IRB::InputCompletor::PerfectMatchedProc + end + + def check_termination(&block) + @check_termination_proc = block + end + + # Reads the next line from this input method. + # + # See IO#gets for more information. + def gets + Reline.input = @stdin + Reline.output = @stdout + if l = readmultiline(@prompt, false, &@check_termination_proc) + HISTORY.push(l) if !l.empty? + @line[@line_no += 1] = l + "\n" + else + @eof = true + l + end + end + + # Whether the end of this input method has been reached, returns +true+ + # if there is no more data to read. + # + # See IO#eof? for more information. + def eof? + @eof + end + + # Whether this input method is still readable when there is no more data to + # read. + # + # See IO#eof for more information. + def readable_after_eof? + true + end + + # Returns the current line number for #io. + # + # #line counts the number of times #gets is called. + # + # See IO#lineno for more information. + def line(line_no) + @line[line_no] + end + + # The external encoding for standard input. + def encoding + @stdin.external_encoding + end + end end |