Class: Reline::LineEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/reline/line_editor.rb

Defined Under Namespace

Modules: CompletionState Classes: CompletionJourneyState, Dialog, DialogProcScope, MenuInfo, RenderedScreen

Constant Summary collapse

VI_MOTIONS =
%i{
  ed_prev_char
  ed_next_char
  vi_zero
  ed_move_to_beg
  ed_move_to_end
  vi_to_column
  vi_next_char
  vi_prev_char
  vi_next_word
  vi_prev_word
  vi_to_next_char
  vi_to_prev_char
  vi_end_word
  vi_next_big_word
  vi_prev_big_word
  vi_end_big_word
}
MINIMUM_SCROLLBAR_HEIGHT =
1
DIALOG_DEFAULT_HEIGHT =
20
ARGUMENT_DIGIT_METHODS =
%i[ed_digit vi_zero ed_argument_digit]
VI_WAITING_ACCEPT_METHODS =
%i[vi_change_meta vi_delete_meta vi_yank ed_insert ed_argument_digit]
MAX_UNDO_REDO_HISTORY_SIZE =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ LineEditor

Returns a new instance of LineEditor.



73
74
75
76
77
78
# File 'lib/reline/line_editor.rb', line 73

def initialize(config)
  @config = config
  @completion_append_character = ''
  @screen_size = [0, 0] # Should be initialized with actual winsize in LineEditor#reset
  reset_variables
end

Instance Attribute Details

#auto_indent_procObject

Returns the value of attribute auto_indent_proc.



14
15
16
# File 'lib/reline/line_editor.rb', line 14

def auto_indent_proc
  @auto_indent_proc
end

#byte_pointerObject

TODO: Use "private alias_method" idiom after drop Ruby 2.5.



8
9
10
# File 'lib/reline/line_editor.rb', line 8

def byte_pointer
  @byte_pointer
end

#completion_append_characterObject

Returns the value of attribute completion_append_character.



11
12
13
# File 'lib/reline/line_editor.rb', line 11

def completion_append_character
  @completion_append_character
end

#completion_procObject

Returns the value of attribute completion_proc.



10
11
12
# File 'lib/reline/line_editor.rb', line 10

def completion_proc
  @completion_proc
end

#confirm_multiline_termination_procObject

Returns the value of attribute confirm_multiline_termination_proc.



9
10
11
# File 'lib/reline/line_editor.rb', line 9

def confirm_multiline_termination_proc
  @confirm_multiline_termination_proc
end

#dig_perfect_match_procObject

Returns the value of attribute dig_perfect_match_proc.



15
16
17
# File 'lib/reline/line_editor.rb', line 15

def dig_perfect_match_proc
  @dig_perfect_match_proc
end

#output_modifier_procObject

Returns the value of attribute output_modifier_proc.



12
13
14
# File 'lib/reline/line_editor.rb', line 12

def output_modifier_proc
  @output_modifier_proc
end

#prompt_procObject

Returns the value of attribute prompt_proc.



13
14
15
# File 'lib/reline/line_editor.rb', line 13

def prompt_proc
  @prompt_proc
end

#rpromptObject

Returns the value of attribute rprompt.



16
17
18
# File 'lib/reline/line_editor.rb', line 16

def rprompt
  @rprompt
end

Instance Method Details

#add_dialog_proc(name, p, context = nil) ⇒ Object



699
700
701
702
703
704
705
706
# File 'lib/reline/line_editor.rb', line 699

def add_dialog_proc(name, p, context = nil)
  dialog = Dialog.new(name, @config, DialogProcScope.new(self, @config, p, context))
  if index = @dialogs.find_index { |d| d.name == name }
    @dialogs[index] = dialog
  else
    @dialogs << dialog
  end
end

#calculate_overlay_levels(overlay_levels) ⇒ Object



398
399
400
401
402
403
404
# File 'lib/reline/line_editor.rb', line 398

def calculate_overlay_levels(overlay_levels)
  levels = []
  overlay_levels.each do |x, w, l|
    levels.fill(l, x, w)
  end
  levels
end

#call_completion_proc(pre, target, post, quote) ⇒ Object



1088
1089
1090
1091
1092
1093
# File 'lib/reline/line_editor.rb', line 1088

def call_completion_proc(pre, target, post, quote)
  Reline.core.instance_variable_set(:@completion_quote_character, quote)
  result = call_completion_proc_with_checking_args(pre, target, post)
  Reline.core.instance_variable_set(:@completion_quote_character, nil)
  result
end

#call_completion_proc_with_checking_args(pre, target, post) ⇒ Object



1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
# File 'lib/reline/line_editor.rb', line 1095

def call_completion_proc_with_checking_args(pre, target, post)
  if @completion_proc and target
    argnum = @completion_proc.parameters.inject(0) { |result, item|
      case item.first
      when :req, :opt
        result + 1
      when :rest
        break 3
      end
    }
    case argnum
    when 1
      result = @completion_proc.(target)
    when 2
      result = @completion_proc.(target, pre)
    when 3..Float::INFINITY
      result = @completion_proc.(target, pre, post)
    end
  end
  result
end

#clear_dialogsObject



446
447
448
449
450
451
# File 'lib/reline/line_editor.rb', line 446

def clear_dialogs
  @dialogs.each do |dialog|
    dialog.contents = nil
    dialog.trap_key = nil
  end
end

#confirm_multiline_terminationObject



1189
1190
1191
1192
# File 'lib/reline/line_editor.rb', line 1189

def confirm_multiline_termination
  temp_buffer = @buffer_of_lines.dup
  @confirm_multiline_termination_proc.(temp_buffer.join("\n") + "\n")
end

#current_byte_pointer_cursorObject



304
305
306
# File 'lib/reline/line_editor.rb', line 304

def current_byte_pointer_cursor
  calculate_width(current_line.byteslice(0, @byte_pointer))
end

#current_lineObject



1138
1139
1140
# File 'lib/reline/line_editor.rb', line 1138

def current_line
  @buffer_of_lines[@line_index]
end

#delete_text(start = nil, length = nil) ⇒ Object



1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
# File 'lib/reline/line_editor.rb', line 1214

def delete_text(start = nil, length = nil)
  if start.nil? and length.nil?
    if @buffer_of_lines.size == 1
      @buffer_of_lines[@line_index] = ''
      @byte_pointer = 0
    elsif @line_index == (@buffer_of_lines.size - 1) and @line_index > 0
      @buffer_of_lines.pop
      @line_index -= 1
      @byte_pointer = 0
    elsif @line_index < (@buffer_of_lines.size - 1)
      @buffer_of_lines.delete_at(@line_index)
      @byte_pointer = 0
    end
  elsif not start.nil? and not length.nil?
    if current_line
      before = current_line.byteslice(0, start)
      after = current_line.byteslice(start + length, current_line.bytesize)
      set_current_line(before + after)
    end
  elsif start.is_a?(Range)
    range = start
    first = range.first
    last = range.last
    last = current_line.bytesize - 1 if last > current_line.bytesize
    last += current_line.bytesize if last < 0
    first += current_line.bytesize if first < 0
    range = range.exclude_end? ? first...last : first..last
    line = current_line.bytes.reject.with_index{ |c, i| range.include?(i) }.map{ |c| c.chr(Encoding::ASCII_8BIT) }.join.force_encoding(encoding)
    set_current_line(line)
  else
    set_current_line(current_line.byteslice(0, start))
  end
end

#dialog_proc_scope_completion_journey_dataObject



881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/reline/line_editor.rb', line 881

def dialog_proc_scope_completion_journey_data
  return nil unless @completion_journey_state
  line_index = @completion_journey_state.line_index
  pre_lines = @buffer_of_lines[0...line_index].map { |line| line + "\n" }
  post_lines = @buffer_of_lines[(line_index + 1)..-1].map { |line| line + "\n" }
  DialogProcScope::CompletionJourneyData.new(
    pre_lines.join + @completion_journey_state.pre,
    @completion_journey_state.post + post_lines.join,
    @completion_journey_state.list,
    @completion_journey_state.pointer
  )
end

#editing_modeObject



808
809
810
# File 'lib/reline/line_editor.rb', line 808

def editing_mode
  @config.editing_mode
end

#encodingObject



84
85
86
# File 'lib/reline/line_editor.rb', line 84

def encoding
  io_gate.encoding
end

#eof?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/reline/line_editor.rb', line 220

def eof?
  @eof
end

#finalizeObject



216
217
218
# File 'lib/reline/line_editor.rb', line 216

def finalize
  Signal.trap('INT', @old_trap)
end

#finishObject



1268
1269
1270
1271
# File 'lib/reline/line_editor.rb', line 1268

def finish
  @finished = true
  @config.reset
end

#finished?Boolean

Returns:

  • (Boolean)


1264
1265
1266
# File 'lib/reline/line_editor.rb', line 1264

def finished?
  @finished
end

#handle_signalObject



168
169
170
171
# File 'lib/reline/line_editor.rb', line 168

def handle_signal
  handle_interrupted
  handle_resized
end

#input_key(key) ⇒ Object



1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/reline/line_editor.rb', line 1020

def input_key(key)
  old_buffer_of_lines = @buffer_of_lines.dup
  @config.reset_oneshot_key_bindings
  if key.char.nil?
    process_insert(force: true)
    @eof = buffer_empty?
    finish
    return
  end
  return if @dialogs.any? { |dialog| dialog.name == key.method_symbol }

  @completion_occurs = false

  process_key(key.char, key.method_symbol)
  if @config.editing_mode_is?(:vi_command) and @byte_pointer > 0 and @byte_pointer == current_line.bytesize
    byte_size = Reline::Unicode.get_prev_mbchar_size(@buffer_of_lines[@line_index], @byte_pointer)
    @byte_pointer -= byte_size
  end

  @prev_action_state, @next_action_state = @next_action_state, {}

  unless @completion_occurs
    @completion_state = CompletionState::NORMAL
    @completion_journey_state = nil
  end

  modified = old_buffer_of_lines != @buffer_of_lines

  push_undo_redo(modified) unless @restoring
  @restoring = false

  if @in_pasting
    clear_dialogs
    return
  end

  if !@completion_occurs && modified && !@config.disable_completion && @config.autocompletion
    # Auto complete starts only when edited
    process_insert(force: true)
    @completion_journey_state = retrieve_completion_journey_state
  end
  modified
end

#insert_multiline_text(text) ⇒ Object



1194
1195
1196
1197
1198
1199
1200
1201
1202
# File 'lib/reline/line_editor.rb', line 1194

def insert_multiline_text(text)
  pre = @buffer_of_lines[@line_index].byteslice(0, @byte_pointer)
  post = @buffer_of_lines[@line_index].byteslice(@byte_pointer..)
  lines = (pre + Reline::Unicode.safe_encode(text, encoding).gsub(/\r\n?/, "\n") + post).split("\n", -1)
  lines << '' if lines.empty?
  @buffer_of_lines[@line_index, 1] = lines
  @line_index += lines.size - 1
  @byte_pointer = @buffer_of_lines[@line_index].bytesize - post.bytesize
end

#insert_text(text) ⇒ Object



1204
1205
1206
1207
1208
1209
1210
1211
1212
# File 'lib/reline/line_editor.rb', line 1204

def insert_text(text)
  if @buffer_of_lines[@line_index].bytesize == @byte_pointer
    @buffer_of_lines[@line_index] += text
  else
    @buffer_of_lines[@line_index] = byteinsert(@buffer_of_lines[@line_index], @byte_pointer, text)
  end
  @byte_pointer += text.bytesize
  process_auto_indent
end

#io_gateObject



80
81
82
# File 'lib/reline/line_editor.rb', line 80

def io_gate
  Reline::IOGate
end

#lineObject



1134
1135
1136
# File 'lib/reline/line_editor.rb', line 1134

def line()
  @buffer_of_lines.join("\n") unless eof?
end

#modified_linesObject



351
352
353
354
355
# File 'lib/reline/line_editor.rb', line 351

def modified_lines
  with_cache(__method__, whole_lines, finished?) do |whole, complete|
    modify_lines(whole, complete)
  end
end

#multiline_offObject



273
274
275
# File 'lib/reline/line_editor.rb', line 273

def multiline_off
  @is_multiline = false
end

#multiline_onObject



269
270
271
# File 'lib/reline/line_editor.rb', line 269

def multiline_on
  @is_multiline = true
end

#prompt_listObject



357
358
359
360
361
# File 'lib/reline/line_editor.rb', line 357

def prompt_list
  with_cache(__method__, whole_lines, check_mode_string, @vi_arg, @searching_prompt) do |lines, mode_string|
    check_multiline_prompt(lines, mode_string)
  end
end

#push_undo_redo(modified) ⇒ Object



1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/reline/line_editor.rb', line 1065

def push_undo_redo(modified)
  if modified
    @undo_redo_history = @undo_redo_history[0..@undo_redo_index]
    @undo_redo_history.push([@buffer_of_lines.dup, @byte_pointer, @line_index])
    if @undo_redo_history.size > MAX_UNDO_REDO_HISTORY_SIZE
      @undo_redo_history.shift
    end
    @undo_redo_index = @undo_redo_history.size - 1
  else
    @undo_redo_history[@undo_redo_index] = [@buffer_of_lines.dup, @byte_pointer, @line_index]
  end
end

#renderObject



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/reline/line_editor.rb', line 473

def render
  wrapped_cursor_x, wrapped_cursor_y = wrapped_cursor_position
  new_lines = wrapped_prompt_and_input_lines.flatten(1)[screen_scroll_top, screen_height].map do |prompt, line|
    prompt_width = Reline::Unicode.calculate_width(prompt, true)
    [[0, prompt_width, prompt], [prompt_width, Reline::Unicode.calculate_width(line, true), line]]
  end

  # Add rprompt to the first visible line if set and there's room
  if @rprompt && !@rprompt.empty? && new_lines[0]
    rprompt_width = Reline::Unicode.calculate_width(@rprompt, true)
    right_col = screen_width - rprompt_width
    first_line = new_lines[0]
    # Calculate the end of the current content (prompt + input)
    content_end = first_line.sum { |_, width, _| width }
    # Only show rprompt if there's at least 1 char gap between content and rprompt
    if right_col > content_end
      first_line << [right_col, rprompt_width, @rprompt]
    end
  end

  if @menu_info
    @menu_info.lines(screen_width).each do |item|
      new_lines << [[0, Reline::Unicode.calculate_width(item), item]]
    end
    @menu_info = nil # TODO: do not change state here
  end

  @dialogs.each_with_index do |dialog, index|
    next unless dialog.contents

    x_range, y_range = dialog_range dialog, wrapped_cursor_y - screen_scroll_top
    y_range.each do |row|
      next if row < 0 || row >= screen_height

      dialog_rows = new_lines[row] ||= []
      # index 0 is for prompt, index 1 is for line, index 2 is for rprompt, index 3.. is for dialog
      dialog_rows[index + 3] = [x_range.begin, dialog.width, dialog.contents[row - y_range.begin]]
    end
  end

  Reline::IOGate.buffered_output do
    render_differential new_lines, wrapped_cursor_x, wrapped_cursor_y - screen_scroll_top
  end
end

#render_finishedObject



461
462
463
464
465
466
467
468
469
470
471
# File 'lib/reline/line_editor.rb', line 461

def render_finished
  Reline::IOGate.buffered_output do
    render_differential([], 0, 0)
    lines = @buffer_of_lines.size.times.map do |i|
      line = Reline::Unicode.strip_non_printing_start_end(prompt_list[i]) + modified_lines[i]
      wrapped_lines = split_line_by_width(line, screen_width)
      wrapped_lines.last.empty? ? "#{line} " : line
    end
    Reline::IOGate.write lines.map { |l| "#{l}\r\n" }.join
  end
end

#render_line_differential(old_items, new_items) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/reline/line_editor.rb', line 406

def render_line_differential(old_items, new_items)
  old_levels = calculate_overlay_levels(old_items.zip(new_items).each_with_index.map {|((x, w, c), (nx, _nw, nc)), i| [x, w, c == nc && x == nx ? i : -1] if x }.compact)
  new_levels = calculate_overlay_levels(new_items.each_with_index.map { |(x, w), i| [x, w, i] if x }.compact).take(screen_width)
  base_x = 0
  new_levels.zip(old_levels).chunk { |n, o| n == o ? :skip : n || :blank }.each do |level, chunk|
    width = chunk.size
    if level == :skip
      # do nothing
    elsif level == :blank
      Reline::IOGate.move_cursor_column base_x
      Reline::IOGate.write "#{Reline::IOGate.reset_color_sequence}#{' ' * width}"
    else
      x, w, content = new_items[level]
      cover_begin = base_x != 0 && new_levels[base_x - 1] == level
      cover_end = new_levels[base_x + width] == level
      pos = 0
      unless x == base_x && w == width
        content, pos = Reline::Unicode.take_mbchar_range(content, base_x - x, width, cover_begin: cover_begin, cover_end: cover_end, padding: true)
      end
      Reline::IOGate.move_cursor_column x + pos
      Reline::IOGate.write "#{Reline::IOGate.reset_color_sequence}#{content}#{Reline::IOGate.reset_color_sequence}"
    end
    base_x += width
  end
  if old_levels.size > new_levels.size
    Reline::IOGate.move_cursor_column new_levels.size
    Reline::IOGate.erase_after_cursor
  end
end

#rerenderObject



571
572
573
# File 'lib/reline/line_editor.rb', line 571

def rerender
  render unless @in_pasting
end

#reset(prompt = '') ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/reline/line_editor.rb', line 141

def reset(prompt = '')
  @screen_size = Reline::IOGate.get_screen_size
  reset_variables(prompt)
  @rendered_screen.base_y = Reline::IOGate.cursor_pos.y
  if ENV.key?('RELINE_ALT_SCROLLBAR')
    @full_block = '::'
    @upper_half_block = "''"
    @lower_half_block = '..'
    @block_elem_width = 2
  elsif Reline::IOGate.win?
    @full_block = ''
    @upper_half_block = ''
    @lower_half_block = ''
    @block_elem_width = 1
  elsif encoding == Encoding::UTF_8
    @full_block = ''
    @upper_half_block = ''
    @lower_half_block = ''
    @block_elem_width = Reline::Unicode.calculate_width('')
  else
    @full_block = '::'
    @upper_half_block = "''"
    @lower_half_block = '..'
    @block_elem_width = 2
  end
end

#reset_lineObject



261
262
263
264
265
266
267
# File 'lib/reline/line_editor.rb', line 261

def reset_line
  @byte_pointer = 0
  @buffer_of_lines = [String.new(encoding: encoding)]
  @line_index = 0
  @cache.clear
  @line_backup_in_history = nil
end

#reset_variables(prompt = '') ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/reline/line_editor.rb', line 224

def reset_variables(prompt = '')
  @prompt = prompt.gsub("\n", "\\n")
  @mark_position = nil
  @is_multiline = false
  @finished = false
  @history_pointer = nil
  @kill_ring ||= Reline::KillRing.new
  @vi_clipboard = ''
  @vi_arg = nil
  @waiting_proc = nil
  @vi_waiting_operator = nil
  @vi_waiting_operator_arg = nil
  @completion_journey_state = nil
  @completion_state = CompletionState::NORMAL
  @perfect_matched = nil
  @menu_info = nil
  @searching_prompt = nil
  @just_cursor_moving = false
  @eof = false
  @continuous_insertion_buffer = String.new(encoding: encoding)
  @scroll_partial_screen = 0
  @drop_terminate_spaces = false
  @in_pasting = false
  @auto_indent_proc = nil
  @dialogs = []
  @interrupted = false
  @resized = false
  @cache = {}
  @rendered_screen = RenderedScreen.new(base_y: 0, lines: [], cursor_y: 0)
  @undo_redo_history = [[[""], 0, 0]]
  @undo_redo_index = 0
  @restoring = false
  @prev_action_state = {}
  @next_action_state = {}
  reset_line
end

#rest_height(wrapped_cursor_y) ⇒ Object



567
568
569
# File 'lib/reline/line_editor.rb', line 567

def rest_height(wrapped_cursor_y)
  screen_height - wrapped_cursor_y + screen_scroll_top - @rendered_screen.base_y - 1
end

#retrieve_completion_blockObject



1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
# File 'lib/reline/line_editor.rb', line 1153

def retrieve_completion_block
  quote_characters = Reline.completer_quote_characters
  before = current_line.byteslice(0, @byte_pointer).grapheme_clusters
  quote = nil
  # Calculate closing quote when cursor is at the end of the line
  if current_line.bytesize == @byte_pointer && !quote_characters.empty?
    escaped = false
    before.each do |c|
      if escaped
        escaped = false
        next
      elsif c == '\\'
        escaped = true
      elsif quote
        quote = nil if c == quote
      elsif quote_characters.include?(c)
        quote = c
      end
    end
  end

  word_break_characters = quote_characters + Reline.completer_word_break_characters
  break_index = before.rindex { |c| word_break_characters.include?(c) || quote_characters.include?(c) } || -1
  preposing = before.take(break_index + 1).join
  target = before.drop(break_index + 1).join
  postposing = current_line.byteslice(@byte_pointer, current_line.bytesize - @byte_pointer)
  lines = whole_lines
  if @line_index > 0
    preposing = lines[0..(@line_index - 1)].join("\n") + "\n" + preposing
  end
  if (lines.size - 1) > @line_index
    postposing = postposing + "\n" + lines[(@line_index + 1)..-1].join("\n")
  end
  [preposing.encode(encoding), target.encode(encoding), postposing.encode(encoding), quote&.encode(encoding)]
end

#screen_heightObject



363
364
365
# File 'lib/reline/line_editor.rb', line 363

def screen_height
  @screen_size.first
end

#screen_scroll_topObject



371
372
373
# File 'lib/reline/line_editor.rb', line 371

def screen_scroll_top
  @scroll_partial_screen
end

#screen_widthObject



367
368
369
# File 'lib/reline/line_editor.rb', line 367

def screen_width
  @screen_size.last
end

#scroll_into_viewObject



1078
1079
1080
1081
1082
1083
1084
1085
1086
# File 'lib/reline/line_editor.rb', line 1078

def scroll_into_view
  _wrapped_cursor_x, wrapped_cursor_y = wrapped_cursor_position
  if wrapped_cursor_y < screen_scroll_top
    @scroll_partial_screen = wrapped_cursor_y
  end
  if wrapped_cursor_y >= screen_scroll_top + screen_height
    @scroll_partial_screen = wrapped_cursor_y - screen_height + 1
  end
end

#set_current_line(line, byte_pointer = nil) ⇒ Object



1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
# File 'lib/reline/line_editor.rb', line 1142

def set_current_line(line, byte_pointer = nil)
  cursor = current_byte_pointer_cursor
  @buffer_of_lines[@line_index] = line
  if byte_pointer
    @byte_pointer = byte_pointer
  else
    calculate_nearest_cursor(cursor)
  end
  process_auto_indent
end

#set_pasting_state(in_pasting) ⇒ Object



88
89
90
91
92
93
# File 'lib/reline/line_editor.rb', line 88

def set_pasting_state(in_pasting)
  # While pasting, text to be inserted is stored to @continuous_insertion_buffer.
  # After pasting, this buffer should be force inserted.
  process_insert(force: true) if @in_pasting && !in_pasting
  @in_pasting = in_pasting
end

#set_signal_handlersObject



207
208
209
210
211
212
213
214
# File 'lib/reline/line_editor.rb', line 207

def set_signal_handlers
  Reline::IOGate.set_winch_handler do
    @resized = true
  end
  @old_trap = Signal.trap('INT') do
    @interrupted = true
  end
end

#update(key) ⇒ Object



1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'lib/reline/line_editor.rb', line 1010

def update(key)
  modified = input_key(key)
  unless @in_pasting
    scroll_into_view
    @just_cursor_moving = !modified
    update_dialogs(key)
    @just_cursor_moving = false
  end
end

#update_dialogs(key = nil) ⇒ Object



453
454
455
456
457
458
459
# File 'lib/reline/line_editor.rb', line 453

def update_dialogs(key = nil)
  wrapped_cursor_x, wrapped_cursor_y = wrapped_cursor_position
  @dialogs.each do |dialog|
    dialog.trap_key = nil
    update_each_dialog(dialog, wrapped_cursor_x, wrapped_cursor_y - screen_scroll_top, key)
  end
end

#upper_space_height(wrapped_cursor_y) ⇒ Object



563
564
565
# File 'lib/reline/line_editor.rb', line 563

def upper_space_height(wrapped_cursor_y)
  wrapped_cursor_y - screen_scroll_top
end

#whole_bufferObject



1256
1257
1258
# File 'lib/reline/line_editor.rb', line 1256

def whole_buffer
  whole_lines.join("\n")
end

#whole_linesObject



1252
1253
1254
# File 'lib/reline/line_editor.rb', line 1252

def whole_lines
  @buffer_of_lines.dup
end

#with_cache(key, *deps) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/reline/line_editor.rb', line 343

def with_cache(key, *deps)
  cached_deps, value = @cache[key]
  if cached_deps != deps
    @cache[key] = [deps, value = yield(*deps, cached_deps, value)]
  end
  value
end

#wrap_method_call(method_symbol, key, with_operator) ⇒ Object



963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# File 'lib/reline/line_editor.rb', line 963

def wrap_method_call(method_symbol, key, with_operator)
  if @waiting_proc
    @waiting_proc.call(key, method_symbol)
    return
  end

  return unless respond_to?(method_symbol, true)
  method_obj = method(method_symbol)
  if @vi_arg and argumentable?(method_obj)
    if inclusive?(method_obj)
      method_obj.(key, arg: @vi_arg, inclusive: with_operator)
    else
      method_obj.(key, arg: @vi_arg)
    end
  else
    if inclusive?(method_obj)
      method_obj.(key, inclusive: with_operator)
    else
      method_obj.(key)
    end
  end
end

#wrapped_cursor_positionObject

Calculate cursor position in word wrapped content.



437
438
439
440
441
442
443
444
# File 'lib/reline/line_editor.rb', line 437

def wrapped_cursor_position
  prompt_width = calculate_width(prompt_list[@line_index], true)
  line_before_cursor = Reline::Unicode.escape_for_print(whole_lines[@line_index].byteslice(0, @byte_pointer))
  wrapped_line_before_cursor = split_line_by_width(' ' * prompt_width + line_before_cursor, screen_width)
  wrapped_cursor_y = wrapped_prompt_and_input_lines[0...@line_index].sum(&:size) + wrapped_line_before_cursor.size - 1
  wrapped_cursor_x = calculate_width(wrapped_line_before_cursor.last)
  [wrapped_cursor_x, wrapped_cursor_y]
end

#wrapped_prompt_and_input_linesObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/reline/line_editor.rb', line 375

def wrapped_prompt_and_input_lines
  with_cache(__method__, @buffer_of_lines.size, modified_lines, prompt_list, screen_width) do |n, lines, prompts, width, prev_cache_key, cached_value|
    prev_n, prev_lines, prev_prompts, prev_width = prev_cache_key
    cached_wraps = {}
    if prev_width == width
      prev_n.times do |i|
        cached_wraps[[prev_prompts[i], prev_lines[i]]] = cached_value[i]
      end
    end

    n.times.map do |i|
      prompt = prompts[i] || ''
      line = lines[i] || ''
      if (cached = cached_wraps[[prompt, line]])
        next cached
      end
      *wrapped_prompts, code_line_prompt = split_line_by_width(prompt, width)
      wrapped_lines = split_line_by_width(line, width, offset: calculate_width(code_line_prompt, true))
      wrapped_prompts.map { |p| [p, ''] } + [[code_line_prompt, wrapped_lines.first]] + wrapped_lines.drop(1).map { |c| ['', c] }
    end
  end
end