Class: Reline::LineEditor

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

Defined Under Namespace

Modules: CompletionState Classes: CompletionJourneyData, 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
  vi_repeat_next_char
  vi_repeat_prev_char
}
MINIMUM_SCROLLBAR_HEIGHT =
1
DIALOG_DEFAULT_HEIGHT =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, encoding) ⇒ LineEditor

Returns a new instance of LineEditor.



56
57
58
59
60
61
# File 'lib/reline/line_editor.rb', line 56

def initialize(config, encoding)
  @config = config
  @completion_append_character = ''
  @screen_size = Reline::IOGate.get_screen_size
  reset_variables(encoding: encoding)
end

Instance Attribute Details

#auto_indent_procObject

Returns the value of attribute auto_indent_proc.



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

def auto_indent_proc
  @auto_indent_proc
end

#byte_pointerObject

TODO: undo TODO: Use “private alias_method” idiom after drop Ruby 2.5.



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

def byte_pointer
  @byte_pointer
end

#completion_append_characterObject

Returns the value of attribute completion_append_character.



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

def completion_append_character
  @completion_append_character
end

#completion_procObject

Returns the value of attribute completion_proc.



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

def completion_proc
  @completion_proc
end

#confirm_multiline_termination_procObject

Returns the value of attribute confirm_multiline_termination_proc.



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

def confirm_multiline_termination_proc
  @confirm_multiline_termination_proc
end

#dig_perfect_match_procObject

Returns the value of attribute dig_perfect_match_proc.



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

def dig_perfect_match_proc
  @dig_perfect_match_proc
end

#output=(value) ⇒ Object (writeonly)

Sets the attribute output

Parameters:

  • value

    the value to set the attribute output to.



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

def output=(value)
  @output = value
end

#output_modifier_procObject

Returns the value of attribute output_modifier_proc.



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

def output_modifier_proc
  @output_modifier_proc
end

#prompt_procObject

Returns the value of attribute prompt_proc.



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

def prompt_proc
  @prompt_proc
end

Instance Method Details

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



662
663
664
665
666
667
668
669
# File 'lib/reline/line_editor.rb', line 662

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



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

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

#call_completion_procObject



1158
1159
1160
1161
1162
1163
1164
# File 'lib/reline/line_editor.rb', line 1158

def call_completion_proc
  result = retrieve_completion_block(true)
  pre, target, post = result
  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



1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
# File 'lib/reline/line_editor.rb', line 1166

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



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

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

#clear_rendered_linesObject



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/reline/line_editor.rb', line 418

def clear_rendered_lines
  Reline::IOGate.move_cursor_up @rendered_screen.cursor_y
  Reline::IOGate.move_cursor_column 0

  num_lines = @rendered_screen.lines.size
  return unless num_lines && num_lines >= 1

  Reline::IOGate.move_cursor_down num_lines - 1
  (num_lines - 1).times do
    Reline::IOGate.erase_after_cursor
    Reline::IOGate.move_cursor_up 1
  end
  Reline::IOGate.erase_after_cursor
  @rendered_screen.lines = []
  @rendered_screen.cursor_y = 0
end

#confirm_multiline_terminationObject



1301
1302
1303
1304
# File 'lib/reline/line_editor.rb', line 1301

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

#current_byte_pointer_cursorObject



266
267
268
# File 'lib/reline/line_editor.rb', line 266

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

#current_lineObject



1208
1209
1210
# File 'lib/reline/line_editor.rb', line 1208

def current_line
  @buffer_of_lines[@line_index]
end

#current_rowObject



511
512
513
# File 'lib/reline/line_editor.rb', line 511

def current_row
  wrapped_lines.flatten[wrapped_cursor_y]
end

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



1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
# File 'lib/reline/line_editor.rb', line 1316

def delete_text(start = nil, length = nil)
  if start.nil? and length.nil?
    if @is_multiline
      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
    else
      set_current_line('', 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

#editing_modeObject



778
779
780
# File 'lib/reline/line_editor.rb', line 778

def editing_mode
  @config.editing_mode
end

#eof?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/reline/line_editor.rb', line 184

def eof?
  @eof
end

#finalizeObject



180
181
182
# File 'lib/reline/line_editor.rb', line 180

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

#finishObject



1370
1371
1372
1373
# File 'lib/reline/line_editor.rb', line 1370

def finish
  @finished = true
  @config.reset
end

#finished?Boolean

Returns:

  • (Boolean)


1366
1367
1368
# File 'lib/reline/line_editor.rb', line 1366

def finished?
  @finished
end

#handle_clearedObject



523
524
525
526
527
528
529
530
531
532
# File 'lib/reline/line_editor.rb', line 523

def handle_cleared
  return unless @cleared

  @cleared = false
  Reline::IOGate.clear_screen
  @screen_size = Reline::IOGate.get_screen_size
  @rendered_screen.lines = []
  @rendered_screen.base_y = 0
  @rendered_screen.cursor_y = 0
end

#input_key(key) ⇒ Object



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
# File 'lib/reline/line_editor.rb', line 1084

def input_key(key)
  @config.reset_oneshot_key_bindings
  @dialogs.each do |dialog|
    if key.char.instance_of?(Symbol) and key.char == dialog.name
      return
    end
  end
  if key.char.nil?
    if @first_char
      @eof = true
    end
    finish
    return
  end
  old_lines = @buffer_of_lines.dup
  @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
        process_insert
        if @config.autocompletion
          move_completed_list(result, :down)
        else
          complete(result)
        end
      end
    end
  elsif @config.editing_mode_is?(:emacs, :vi_insert) and key.char == :completion_journey_up
    if not @config.disable_completion and @config.autocompletion
      result = call_completion_proc
      if result.is_a?(Array)
        completion_occurs = true
        process_insert
        move_completed_list(result, :up)
      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
        process_insert
        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
    @completion_journey_data = nil
  end
  if @in_pasting
    clear_dialogs
  else
    return old_lines != @buffer_of_lines
  end
end

#insert_text(text) ⇒ Object



1306
1307
1308
1309
1310
1311
1312
1313
1314
# File 'lib/reline/line_editor.rb', line 1306

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



63
64
65
# File 'lib/reline/line_editor.rb', line 63

def io_gate
  Reline::IOGate
end

#lineObject



1204
1205
1206
# File 'lib/reline/line_editor.rb', line 1204

def line()
  current_line unless eof?
end

#modified_linesObject



313
314
315
316
317
# File 'lib/reline/line_editor.rb', line 313

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

#multiline_offObject



235
236
237
# File 'lib/reline/line_editor.rb', line 235

def multiline_off
  @is_multiline = false
end

#multiline_onObject



231
232
233
# File 'lib/reline/line_editor.rb', line 231

def multiline_on
  @is_multiline = true
end


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

def print_nomultiline_prompt(prompt)
  return unless prompt && !@is_multiline

  # Readline's test `TestRelineAsReadline#test_readline` requires first output to be prompt, not cursor reset escape sequence.
  @rendered_screen.lines = [[[0, Reline::Unicode.calculate_width(prompt, true), prompt]]]
  @rendered_screen.cursor_y = 0
  @output.write prompt
end

#prompt_listObject



319
320
321
322
323
# File 'lib/reline/line_editor.rb', line 319

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

#render_differentialObject



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
# File 'lib/reline/line_editor.rb', line 453

def render_differential
  wrapped_cursor_x, wrapped_cursor_y = wrapped_cursor_position

  rendered_lines = @rendered_screen.lines
  new_lines = wrapped_lines.flatten[screen_scroll_top, screen_height].map do |l|
    [[0, Reline::Unicode.calculate_width(l, true), l]]
  end
  if @menu_info
    @menu_info.list.sort!.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] ||= []
      dialog_rows[index + 1] = [x_range.begin, dialog.width, dialog.contents[row - y_range.begin]]
    end
  end

  cursor_y = @rendered_screen.cursor_y
  if new_lines != rendered_lines
    # Hide cursor while rendering to avoid cursor flickering.
    Reline::IOGate.hide_cursor
    num_lines = [[new_lines.size, rendered_lines.size].max, screen_height].min
    if @rendered_screen.base_y + num_lines > screen_height
      Reline::IOGate.scroll_down(num_lines - cursor_y - 1)
      @rendered_screen.base_y = screen_height - num_lines
      cursor_y = num_lines - 1
    end
    num_lines.times do |i|
      rendered_line = rendered_lines[i] || []
      line_to_render = new_lines[i] || []
      next if rendered_line == line_to_render

      Reline::IOGate.move_cursor_down i - cursor_y
      cursor_y = i
      unless rendered_lines[i]
        Reline::IOGate.move_cursor_column 0
        Reline::IOGate.erase_after_cursor
      end
      render_line_differential(rendered_line, line_to_render)
    end
    @rendered_screen.lines = new_lines
    Reline::IOGate.show_cursor
  end
  y = wrapped_cursor_y - screen_scroll_top
  Reline::IOGate.move_cursor_column wrapped_cursor_x
  Reline::IOGate.move_cursor_down y - cursor_y
  @rendered_screen.cursor_y = y
  new_lines.size - y
end

#render_finishedObject



413
414
415
416
# File 'lib/reline/line_editor.rb', line 413

def render_finished
  clear_rendered_lines
  render_full_content
end

#render_full_contentObject



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

def render_full_content
  lines = @buffer_of_lines.size.times.map do |i|
    line = prompt_list[i] + modified_lines[i]
    wrapped_lines, = split_by_width(line, screen_width)
    wrapped_lines.last.empty? ? "#{line} " : line
  end
  @output.puts lines.map { |l| "#{l}\r\n" }.join
end

#render_line_differential(old_items, new_items) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/reline/line_editor.rb', line 363

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
      @output.write "#{Reline::IOGate::RESET_COLOR}#{' ' * width}"
    else
      x, w, content = new_items[level]
      content = Reline::Unicode.take_range(content, base_x - x, width) unless x == base_x && w == width
      Reline::IOGate.move_cursor_column base_x
      @output.write "#{Reline::IOGate::RESET_COLOR}#{content}#{Reline::IOGate::RESET_COLOR}"
    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



534
535
536
537
# File 'lib/reline/line_editor.rb', line 534

def rerender
  handle_cleared
  render_differential unless @in_pasting
end

#reset(prompt = '', encoding:) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/reline/line_editor.rb', line 116

def reset(prompt = '', encoding:)
  @screen_size = Reline::IOGate.get_screen_size
  reset_variables(prompt, encoding: encoding)
  @rendered_screen.base_y = Reline::IOGate.cursor_pos.y
  Reline::IOGate.set_winch_handler do
    @resized = true
  end
  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



222
223
224
225
226
227
228
229
# File 'lib/reline/line_editor.rb', line 222

def reset_line
  @byte_pointer = 0
  @buffer_of_lines = [String.new(encoding: @encoding)]
  @line_index = 0
  @cache.clear
  @line_backup_in_history = nil
  @multibyte_buffer = String.new(encoding: 'ASCII-8BIT')
end

#reset_variables(prompt = '', encoding:) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/reline/line_editor.rb', line 188

def reset_variables(prompt = '', encoding:)
  @prompt = prompt.gsub("\n", "\\n")
  @mark_pointer = nil
  @encoding = encoding
  @is_multiline = false
  @finished = false
  @cleared = false
  @history_pointer = nil
  @kill_ring ||= Reline::KillRing.new
  @vi_clipboard = ''
  @vi_arg = nil
  @waiting_proc = nil
  @waiting_operator_proc = nil
  @waiting_operator_vi_arg = nil
  @completion_journey_data = nil
  @completion_state = CompletionState::NORMAL
  @perfect_matched = nil
  @menu_info = nil
  @searching_prompt = nil
  @first_char = true
  @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 = []
  @resized = false
  @cache = {}
  @rendered_screen = RenderedScreen.new(base_y: 0, lines: [], cursor_y: 0)
  reset_line
end

#resizeObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/reline/line_editor.rb', line 146

def resize
  return unless @resized

  @screen_size = Reline::IOGate.get_screen_size
  @resized = false
  scroll_into_view
  Reline::IOGate.move_cursor_up @rendered_screen.cursor_y
  @rendered_screen.base_y = Reline::IOGate.cursor_pos.y
  @rendered_screen.lines = []
  @rendered_screen.cursor_y = 0
  render_differential
end

#rest_height(wrapped_cursor_y) ⇒ Object



519
520
521
# File 'lib/reline/line_editor.rb', line 519

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

#retrieve_completion_block(set_completion_quote_character = false) ⇒ Object



1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
# File 'lib/reline/line_editor.rb', line 1223

def retrieve_completion_block(set_completion_quote_character = false)
  if Reline.completer_word_break_characters.empty?
    word_break_regexp = nil
  else
    word_break_regexp = /\A[#{Regexp.escape(Reline.completer_word_break_characters)}]/
  end
  if Reline.completer_quote_characters.empty?
    quote_characters_regexp = nil
  else
    quote_characters_regexp = /\A[#{Regexp.escape(Reline.completer_quote_characters)}]/
  end
  before = current_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 = current_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 quote_characters_regexp and 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 word_break_regexp and not quote and slice =~ word_break_regexp
      rest = $'
      i += 1
      before = current_line.byteslice(i, @byte_pointer - i)
      break_pointer = i
    else
      i += 1
    end
  end
  postposing = current_line.byteslice(@byte_pointer, current_line.bytesize - @byte_pointer)
  if rest
    preposing = current_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 = current_line.byteslice(0, break_pointer)
    else
      preposing = ''
    end
    target = before
  end
  if @is_multiline
    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
  end
  [preposing.encode(@encoding), target.encode(@encoding), postposing.encode(@encoding)]
end

#screen_heightObject



325
326
327
# File 'lib/reline/line_editor.rb', line 325

def screen_height
  @screen_size.first
end

#screen_scroll_topObject



333
334
335
# File 'lib/reline/line_editor.rb', line 333

def screen_scroll_top
  @scroll_partial_screen
end

#screen_widthObject



329
330
331
# File 'lib/reline/line_editor.rb', line 329

def screen_width
  @screen_size.last
end

#scroll_into_viewObject



1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'lib/reline/line_editor.rb', line 1148

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



1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/reline/line_editor.rb', line 1212

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



67
68
69
70
71
72
# File 'lib/reline/line_editor.rb', line 67

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/reline/line_editor.rb', line 159

def set_signal_handlers
  @old_trap = Signal.trap('INT') {
    clear_dialogs
    scrolldown = render_differential
    Reline::IOGate.scroll_down scrolldown
    Reline::IOGate.move_cursor_column 0
    @rendered_screen.lines = []
    @rendered_screen.cursor_y = 0
    case @old_trap
    when 'DEFAULT', 'SYSTEM_DEFAULT'
      raise Interrupt
    when 'IGNORE'
      # Do nothing
    when 'EXIT'
      exit
    else
      @old_trap.call if @old_trap.respond_to?(:call)
    end
  }
end

#update(key) ⇒ Object



1074
1075
1076
1077
1078
1079
1080
1081
1082
# File 'lib/reline/line_editor.rb', line 1074

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



405
406
407
408
409
410
411
# File 'lib/reline/line_editor.rb', line 405

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



515
516
517
# File 'lib/reline/line_editor.rb', line 515

def upper_space_height(wrapped_cursor_y)
  wrapped_cursor_y - screen_scroll_top
end

#whole_bufferObject



1362
1363
1364
# File 'lib/reline/line_editor.rb', line 1362

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

#whole_linesObject



1358
1359
1360
# File 'lib/reline/line_editor.rb', line 1358

def whole_lines
  @buffer_of_lines.dup
end

#with_cache(key, *deps) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/reline/line_editor.rb', line 305

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, method_obj, key, with_operator = false) ⇒ Object



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

def wrap_method_call(method_symbol, method_obj, key, with_operator = false)
  if @config.editing_mode_is?(:emacs, :vi_insert) and @waiting_proc.nil? and @waiting_operator_proc.nil?
    not_insertion = method_symbol != :ed_insert
    process_insert(force: not_insertion)
  end
  if @vi_arg and argumentable?(method_obj)
    if with_operator and inclusive?(method_obj)
      method_obj.(key, arg: @vi_arg, inclusive: true)
    else
      method_obj.(key, arg: @vi_arg)
    end
  else
    if with_operator and inclusive?(method_obj)
      method_obj.(key, inclusive: true)
    else
      method_obj.(key)
    end
  end
end

#wrapped_cursor_positionObject

Calculate cursor position in word wrapped content.



389
390
391
392
393
394
395
396
# File 'lib/reline/line_editor.rb', line 389

def wrapped_cursor_position
  prompt_width = calculate_width(prompt_list[@line_index], true)
  line_before_cursor = whole_lines[@line_index].byteslice(0, @byte_pointer)
  wrapped_line_before_cursor = split_by_width(' ' * prompt_width + line_before_cursor, screen_width).first.compact
  wrapped_cursor_y = wrapped_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_linesObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/reline/line_editor.rb', line 337

def wrapped_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]
      cached_wraps[[prompt, line]] || split_by_width("#{prompt}#{line}", width).first.compact
    end
  end
end