In Files

  • reline/line_editor.rb

Reline::LineEditor

Attributes

auto_indent_proc[RW]
byte_pointer[R]
completion_append_character[RW]
completion_proc[RW]
confirm_multiline_termination_proc[RW]
dig_perfect_match_proc[RW]
line[R]

TODO: undo

output[W]
output_modifier_proc[RW]
pre_input_hook[RW]
prompt_proc[RW]

Public Class Methods

new(config, encoding) click to toggle source
 
               # File reline/line_editor.rb, line 53
def initialize(config, encoding)
  @config = config
  @completion_append_character = ''
  reset_variables(encoding: encoding)
end
            

Public Instance Methods

backward_char(key, arg: 1) click to toggle source
Alias for: ed_prev_char
backward_delete_char(key) click to toggle source
Alias for: em_delete_prev_char
backward_word(key) click to toggle source
Alias for: ed_prev_word
beginning_of_line(key) click to toggle source
Alias for: ed_move_to_beg
byte_pointer=(val) click to toggle source
 
               # File reline/line_editor.rb, line 1014
def byte_pointer=(val)
  @byte_pointer = val
  str = @line.byteslice(0, @byte_pointer)
  @cursor = calculate_width(str)
  @cursor_max = calculate_width(@line)
end
            
call_completion_proc() click to toggle source
 
               # File reline/line_editor.rb, line 844
def call_completion_proc
  result = retrieve_completion_block(true)
  slice = result[1]
  result = @completion_proc.(slice) if @completion_proc and slice
  Reline.core.instance_variable_set(:@completion_quote_character, nil)
  result
end
            
capitalize_word(key) click to toggle source
Alias for: em_capitol_case
clear_screen(key) click to toggle source
Alias for: ed_clear_screen
confirm_multiline_termination() click to toggle source
 
               # File reline/line_editor.rb, line 954
def confirm_multiline_termination
  temp_buffer = @buffer_of_lines.dup
  if @previous_line_index and @line_index == (@buffer_of_lines.size - 1)
    temp_buffer[@previous_line_index] = @line
  else
    temp_buffer[@line_index] = @line
  end
  @confirm_multiline_termination_proc.(temp_buffer.join("\n") + "\n")
end
            
delete_char(key) click to toggle source
Alias for: em_delete
delete_char_or_list(key) click to toggle source
Alias for: em_delete_or_list
delete_text(start = nil, length = nil) click to toggle source
 
               # File reline/line_editor.rb, line 976
def delete_text(start = nil, length = nil)
  if start.nil? and length.nil?
    @line&.clear
    @byte_pointer = 0
    @cursor = 0
    @cursor_max = 0
  elsif not start.nil? and not length.nil?
    if @line
      before = @line.byteslice(0, start)
      after = @line.byteslice(start + length, @line.bytesize)
      @line = before + after
      @byte_pointer = @line.bytesize if @byte_pointer > @line.bytesize
      str = @line.byteslice(0, @byte_pointer)
      @cursor = calculate_width(str)
      @cursor_max = calculate_width(@line)
    end
  elsif start.is_a?(Range)
    range = start
    first = range.first
    last = range.last
    last = @line.bytesize - 1 if last > @line.bytesize
    last += @line.bytesize if last < 0
    first += @line.bytesize if first < 0
    range = range.exclude_end? ? first...last : first..last
    @line = @line.bytes.reject.with_index{ |c, i| range.include?(i) }.map{ |c| c.chr(Encoding::ASCII_8BIT) }.join.force_encoding(@encoding)
    @byte_pointer = @line.bytesize if @byte_pointer > @line.bytesize
    str = @line.byteslice(0, @byte_pointer)
    @cursor = calculate_width(str)
    @cursor_max = calculate_width(@line)
  else
    @line = @line.byteslice(0, start)
    @byte_pointer = @line.bytesize if @byte_pointer > @line.bytesize
    str = @line.byteslice(0, @byte_pointer)
    @cursor = calculate_width(str)
    @cursor_max = calculate_width(@line)
  end
end
            
downcase_word(key) click to toggle source
Alias for: em_lower_case
ed_digit(key) click to toggle source
Alias for: ed_insert
editing_mode() click to toggle source
 
               # File reline/line_editor.rb, line 549
def editing_mode
  @config.editing_mode
end
            
end_of_line(key) click to toggle source
Alias for: ed_move_to_end
eof?() click to toggle source
 
               # File reline/line_editor.rb, line 155
def eof?
  @eof
end
            
exchange_point_and_mark(key) click to toggle source
Alias for: em_exchange_mark
finalize() click to toggle source
 
               # File reline/line_editor.rb, line 151
def finalize
  Signal.trap('SIGINT', @old_trap)
end
            
finish() click to toggle source
 
               # File reline/line_editor.rb, line 1039
def finish
  @finished = true
  @config.reset
end
            
finished?() click to toggle source
 
               # File reline/line_editor.rb, line 1035
def finished?
  @finished
end
            
forward_char(key, arg: 1) click to toggle source
Alias for: ed_next_char
forward_search_history(key) click to toggle source
Alias for: vi_search_next
forward_word(key) click to toggle source
Alias for: em_next_word
history_search_backward(key, arg: 1) click to toggle source
history_search_forward(key, arg: 1) click to toggle source
input_key(key) click to toggle source
 
               # File reline/line_editor.rb, line 805
def input_key(key)
  if key.char.nil?
    if @first_char
      @line = nil
    end
    finish
    return
  end
  @first_char = false
  completion_occurs = false
  if @config.editing_mode_is?(:emacs, :vi_insert) and key.char == "\C-i".ord
    unless @config.disable_completion
      result = call_completion_proc
      if result.is_a?(Array)
        completion_occurs = true
        complete(result)
      end
    end
  elsif not @config.disable_completion and @config.editing_mode_is?(:vi_insert) and ["\C-p".ord, "\C-n".ord].include?(key.char)
    unless @config.disable_completion
      result = call_completion_proc
      if result.is_a?(Array)
        completion_occurs = true
        move_completed_list(result, "\C-p".ord == key.char ? :up : :down)
      end
    end
  elsif Symbol === key.char and respond_to?(key.char, true)
    process_key(key.char, key.char)
  else
    normal_char(key)
  end
  unless completion_occurs
    @completion_state = CompletionState::NORMAL
  end
  if @is_multiline and @auto_indent_proc
    process_auto_indent
  end
end
            
insert_text(text) click to toggle source
 
               # File reline/line_editor.rb, line 964
def insert_text(text)
  width = calculate_width(text)
  if @cursor == @cursor_max
    @line += text
  else
    @line = byteinsert(@line, @byte_pointer, text)
  end
  @byte_pointer += text.bytesize
  @cursor += width
  @cursor_max += width
end
            
multiline_off() click to toggle source
 
               # File reline/line_editor.rb, line 206
def multiline_off
  @is_multiline = false
end
            
multiline_on() click to toggle source
 
               # File reline/line_editor.rb, line 202
def multiline_on
  @is_multiline = true
end
            
quoted_insert(str, arg: 1) click to toggle source
Alias for: ed_quoted_insert
rerender() click to toggle source
 
               # File reline/line_editor.rb, line 300
def rerender
  return if @line.nil?
  if @menu_info
    scroll_down(@highest_in_all - @first_line_started_from)
    @rerender_all = true
    @menu_info.list.sort!.each do |item|
      Reline::IOGate.move_cursor_column(0)
      @output.write item
      @output.flush
      scroll_down(1)
    end
    scroll_down(@highest_in_all - 1)
    move_cursor_up(@highest_in_all - 1 - @first_line_started_from)
    @menu_info = nil
  end
  prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines, prompt)
  if @cleared
    Reline::IOGate.clear_screen
    @cleared = false
    back = 0
    modify_lines(whole_lines).each_with_index do |line, index|
      if @prompt_proc
        pr = prompt_list[index]
        height = render_partial(pr, calculate_width(pr), line, false)
      else
        height = render_partial(prompt, prompt_width, line, false)
      end
      if index < (@buffer_of_lines.size - 1)
        move_cursor_down(height)
        back += height
      end
    end
    move_cursor_up(back)
    move_cursor_down(@first_line_started_from + @started_from)
    Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
    return
  end
  new_highest_in_this = calculate_height_by_width(prompt_width + calculate_width(@line.nil? ? '' : @line))
  # FIXME: end of logical line sometimes breaks
  if @previous_line_index or new_highest_in_this != @highest_in_this
    if @previous_line_index
      new_lines = whole_lines(index: @previous_line_index, line: @line)
    else
      new_lines = whole_lines
    end
    prompt, prompt_width, prompt_list = check_multiline_prompt(new_lines, prompt)
    all_height = calculate_height_by_lines(new_lines, prompt_list || prompt)
    diff = all_height - @highest_in_all
    move_cursor_down(@highest_in_all - @first_line_started_from - @started_from - 1)
    if diff > 0
      scroll_down(diff)
      move_cursor_up(all_height - 1)
    elsif diff < 0
      (-diff).times do
        Reline::IOGate.move_cursor_column(0)
        Reline::IOGate.erase_after_cursor
        move_cursor_up(1)
      end
      move_cursor_up(all_height - 1)
    else
      move_cursor_up(all_height - 1)
    end
    @highest_in_all = all_height
    back = 0
    modify_lines(new_lines).each_with_index do |line, index|
      if @prompt_proc
        prompt = prompt_list[index]
        prompt_width = calculate_width(prompt, true)
      end
      height = render_partial(prompt, prompt_width, line, false)
      if index < (new_lines.size - 1)
        scroll_down(1)
        back += height
      else
        back += height - 1
      end
    end
    move_cursor_up(back)
    if @previous_line_index
      @buffer_of_lines[@previous_line_index] = @line
      @line = @buffer_of_lines[@line_index]
    end
    @first_line_started_from =
      if @line_index.zero?
        0
      else
        calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
      end
    if @prompt_proc
      prompt = prompt_list[@line_index]
      prompt_width = calculate_width(prompt, true)
    end
    move_cursor_down(@first_line_started_from)
    calculate_nearest_cursor
    @started_from = calculate_height_by_width(prompt_width + @cursor) - 1
    move_cursor_down(@started_from)
    Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
    @highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
    @previous_line_index = nil
    rendered = true
  elsif @rerender_all
    move_cursor_up(@first_line_started_from + @started_from)
    Reline::IOGate.move_cursor_column(0)
    back = 0
    new_buffer = whole_lines
    prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer, prompt)
    new_buffer.each_with_index do |line, index|
      prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc
      width = prompt_width + calculate_width(line)
      height = calculate_height_by_width(width)
      back += height
    end
    if back > @highest_in_all
      scroll_down(back - 1)
      move_cursor_up(back - 1)
    elsif back < @highest_in_all
      scroll_down(back)
      Reline::IOGate.erase_after_cursor
      (@highest_in_all - back - 1).times do
        scroll_down(1)
        Reline::IOGate.erase_after_cursor
      end
      move_cursor_up(@highest_in_all - 1)
    end
    modify_lines(new_buffer).each_with_index do |line, index|
      if @prompt_proc
        prompt = prompt_list[index]
        prompt_width = calculate_width(prompt, true)
      end
      render_partial(prompt, prompt_width, line, false)
      if index < (new_buffer.size - 1)
        move_cursor_down(1)
      end
    end
    move_cursor_up(back - 1)
    if @prompt_proc
      prompt = prompt_list[@line_index]
      prompt_width = calculate_width(prompt, true)
    end
    @highest_in_all = back
    @highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
    @first_line_started_from =
      if @line_index.zero?
        0
      else
        calculate_height_by_lines(new_buffer[0..(@line_index - 1)], prompt_list || prompt)
      end
    @started_from = calculate_height_by_width(prompt_width + @cursor) - 1
    move_cursor_down(@first_line_started_from + @started_from)
    Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
    @rerender_all = false
    rendered = true
  end
  line = modify_lines(whole_lines)[@line_index]
  if @is_multiline
    prompt, prompt_width, prompt_list = check_multiline_prompt(whole_lines, prompt)
    if finished?
      # Always rerender on finish because output_modifier_proc may return a different output.
      render_partial(prompt, prompt_width, line)
      scroll_down(1)
      Reline::IOGate.move_cursor_column(0)
      Reline::IOGate.erase_after_cursor
    elsif not rendered
      render_partial(prompt, prompt_width, line)
    end
  else
    render_partial(prompt, prompt_width, line)
    if finished?
      scroll_down(1)
      Reline::IOGate.move_cursor_column(0)
      Reline::IOGate.erase_after_cursor
    end
  end
end
            
reset(prompt = '', encoding:) click to toggle source
 
               # File reline/line_editor.rb, line 105
def reset(prompt = '', encoding:)
  @rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y
  @screen_size = Reline::IOGate.get_screen_size
  reset_variables(prompt, encoding: encoding)
  @old_trap = Signal.trap('SIGINT') {
    @old_trap.call if @old_trap.respond_to?(:call) # can also be string, ex: "DEFAULT"
    raise Interrupt
  }
  Reline::IOGate.set_winch_handler do
    @rest_height = (Reline::IOGate.get_screen_size.first - 1) - Reline::IOGate.cursor_pos.y
    old_screen_size = @screen_size
    @screen_size = Reline::IOGate.get_screen_size
    if old_screen_size.last < @screen_size.last # columns increase
      @rerender_all = true
      rerender
    else
      back = 0
      new_buffer = whole_lines
      prompt, prompt_width, prompt_list = check_multiline_prompt(new_buffer, prompt)
      new_buffer.each_with_index do |line, index|
        prompt_width = calculate_width(prompt_list[index], true) if @prompt_proc
        width = prompt_width + calculate_width(line)
        height = calculate_height_by_width(width)
        back += height
      end
      @highest_in_all = back
      @highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
      @first_line_started_from =
        if @line_index.zero?
          0
        else
          calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
        end
      if @prompt_proc
        prompt = prompt_list[@line_index]
        prompt_width = calculate_width(prompt, true)
      end
      calculate_nearest_cursor
      @started_from = calculate_height_by_width(prompt_width + @cursor) - 1
      Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
      @highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
      @rerender_all = true
    end
  end
end
            
reset_line() click to toggle source
 
               # File reline/line_editor.rb, line 184
def reset_line
  @cursor = 0
  @cursor_max = 0
  @byte_pointer = 0
  @buffer_of_lines = [String.new(encoding: @encoding)]
  @line_index = 0
  @previous_line_index = nil
  @line = @buffer_of_lines[0]
  @first_line_started_from = 0
  @move_up = 0
  @started_from = 0
  @highest_in_this = 1
  @highest_in_all = 1
  @line_backup_in_history = nil
  @multibyte_buffer = String.new(encoding: 'ASCII-8BIT')
  @check_new_auto_indent = false
end
            
reset_variables(prompt = '', encoding:) click to toggle source
 
               # File reline/line_editor.rb, line 159
def reset_variables(prompt = '', encoding:)
  @prompt = prompt
  @mark_pointer = nil
  @encoding = encoding
  @is_multiline = false
  @finished = false
  @cleared = false
  @rerender_all = false
  @history_pointer = nil
  @kill_ring = Reline::KillRing.new
  @vi_clipboard = ''
  @vi_arg = nil
  @waiting_proc = nil
  @waiting_operator_proc = nil
  @completion_journey_data = nil
  @completion_state = CompletionState::NORMAL
  @perfect_matched = nil
  @menu_info = nil
  @first_prompt = true
  @searching_prompt = nil
  @first_char = true
  @eof = false
  reset_line
end
            
retrieve_completion_block(set_completion_quote_character = false) click to toggle source
 
               # File reline/line_editor.rb, line 893
def retrieve_completion_block(set_completion_quote_character = false)
  word_break_regexp = /\A[#{Regexp.escape(Reline.completer_word_break_characters)}]/
  quote_characters_regexp = /\A[#{Regexp.escape(Reline.completer_quote_characters)}]/
  before = @line.byteslice(0, @byte_pointer)
  rest = nil
  break_pointer = nil
  quote = nil
  closing_quote = nil
  escaped_quote = nil
  i = 0
  while i < @byte_pointer do
    slice = @line.byteslice(i, @byte_pointer - i)
    unless slice.valid_encoding?
      i += 1
      next
    end
    if quote and slice.start_with?(closing_quote)
      quote = nil
      i += 1
      rest = nil
    elsif quote and slice.start_with?(escaped_quote)
      # skip
      i += 2
    elsif slice =~ quote_characters_regexp # find new "
      rest = $'
      quote = $&
      closing_quote = /(?!\\)#{Regexp.escape(quote)}/
      escaped_quote = /\\#{Regexp.escape(quote)}/
      i += 1
      break_pointer = i - 1
    elsif not quote and slice =~ word_break_regexp
      rest = $'
      i += 1
      before = @line.byteslice(i, @byte_pointer - i)
      break_pointer = i
    else
      i += 1
    end
  end
  postposing = @line.byteslice(@byte_pointer, @line.bytesize - @byte_pointer)
  if rest
    preposing = @line.byteslice(0, break_pointer)
    target = rest
    if set_completion_quote_character and quote
      Reline.core.instance_variable_set(:@completion_quote_character, quote)
      if postposing !~ /(?!\\)#{Regexp.escape(quote)}/ # closing quote
        insert_text(quote)
      end
    end
  else
    preposing = ''
    if break_pointer
      preposing = @line.byteslice(0, break_pointer)
    else
      preposing = ''
    end
    target = before
  end
  [preposing.encode(@encoding), target.encode(@encoding), postposing.encode(@encoding)]
end
            
reverse_search_history(key) click to toggle source
Alias for: vi_search_prev
self_insert(key) click to toggle source
Alias for: ed_insert
set_mark(key) click to toggle source
Alias for: em_set_mark
transpose_chars(key) click to toggle source
Alias for: ed_transpose_chars
transpose_words(key) click to toggle source
Alias for: ed_transpose_words
upcase_word(key) click to toggle source
Alias for: em_upper_case
vi_end_of_transmission(key) click to toggle source
Alias for: vi_list_or_eof
vi_eof_maybe(key) click to toggle source
Alias for: vi_list_or_eof
whole_buffer() click to toggle source
 
               # File reline/line_editor.rb, line 1027
def whole_buffer
  if @buffer_of_lines.size == 1 and @line.nil?
    nil
  else
    whole_lines.join("\n")
  end
end
            
whole_lines(index: @line_index, line: @line) click to toggle source
 
               # File reline/line_editor.rb, line 1021
def whole_lines(index: @line_index, line: @line)
  temp_lines = @buffer_of_lines.dup
  temp_lines[index] = line
  temp_lines
end
            
There is an updated format of the API docs for this version here.